New BPF helpers to accelerate synproxy
Subsystems (6)
cover New BPF helpers to accelerate synproxy
This series starts with some cleanup and bugfixing in the existing BPF helpers for SYN cookies. The second half adds new functionality that allows XDP to accelerate iptables synproxy.
struct nf_conn is exposed to BPF, new helpers are added to query conntrack info by 5-tuple. The only field exposed for now is status, but it can be extended easily in the future.
New helpers are added to issue SYN and timestamp cookies and to check SYN cookies without binding to a socket, which is useful in the synproxy scenario.
Finally, a sample XDP and userspace program is added that show how all components work together. The XDP program uses socketless SYN cookie helpers and queries conntrack status instead of socket status. A demo script shows how to deploy the synproxy+XDP solution.
The draft of the new functionality was presented on Netdev 0x15:
https://netdevconf.info/0x15/session.html?Accelerating-synproxy-with-XDP
Maxim Mikityanskiy (10):
bpf: Use ipv6_only_sock in bpf_tcp_gen_syncookie
bpf: Support dual-stack sockets in bpf_tcp_check_syncookie
bpf: Use EOPNOTSUPP in bpf_tcp_check_syncookie
bpf: Make errors of bpf_tcp_check_syncookie distinguishable
bpf: Fix documentation of th_len in bpf_tcp_{gen,check}_syncookie
bpf: Expose struct nf_conn to BPF
bpf: Add helpers to query conntrack info
bpf: Add helpers to issue and check SYN cookies in XDP
bpf: Add a helper to issue timestamp cookies in XDP
bpf: Add sample for raw syncookie helpers
include/linux/bpf.h | 46 +++ include/net/tcp.h | 2 + include/uapi/linux/bpf.h | 193 ++++++++++- kernel/bpf/verifier.c | 104 +++++- net/core/filter.c | 433 +++++++++++++++++++++++- net/ipv4/syncookies.c | 60 ++++ net/ipv4/tcp_input.c | 3 +- samples/bpf/.gitignore | 1 + samples/bpf/Makefile | 3 + samples/bpf/syncookie_kern.c | 591 +++++++++++++++++++++++++++++++++ samples/bpf/syncookie_test.sh | 55 +++ samples/bpf/syncookie_user.c | 388 ++++++++++++++++++++++ scripts/bpf_doc.py | 1 + tools/include/uapi/linux/bpf.h | 193 ++++++++++- 14 files changed, 2047 insertions(+), 26 deletions(-) create mode 100644 samples/bpf/syncookie_kern.c create mode 100755 samples/bpf/syncookie_test.sh create mode 100644 samples/bpf/syncookie_user.c
Signature · 1 line
2.30.2
PATCH 1/10 bpf: Use ipv6_only_sock in bpf_tcp_gen_syncookie
Instead of querying the sk_ipv6only field directly, use the dedicated ipv6_only_sock helper.
--- net/core/filter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/filter.c +1 -1
@@ -6788,7 +6788,7 @@ BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,*/ switch (((struct iphdr *)iph)->version) {case 4 : -if (sk->sk_family ==AF_INET6 && sk->sk_ipv6only) +if (sk->sk_family ==AF_INET6 &&ipv6_only_sock (sk))return -EINVAL ; mss =tcp_v4_get_syncookie (sk, iph, th, &cookie);
Signature · 1 line
2.30.2
PATCH 2/10 bpf: Support dual-stack sockets in bpf_tcp_check_syncookie
In reply to Maxim Mikityanskiybpf_tcp_gen_syncookie looks at the IP version in the IP header and validates the address family of the socket. It supports IPv4 packets in AF_INET6 dual-stack sockets.
On the other hand, bpf_tcp_check_syncookie looks only at the address family of the socket, ignoring the real IP version in headers, and validates only the packet size. This implementation has some drawbacks:
1. Packets are not validated properly, allowing a BPF program to trick bpf_tcp_check_syncookie into handling an IPv6 packet on an IPv4 socket.
2. Dual-stack sockets fail the checks on IPv4 packets. IPv4 clients end up receiving a SYNACK with the cookie, but the following ACK gets dropped.
This patch fixes these issues by changing the checks in bpf_tcp_check_syncookie to match the ones in bpf_tcp_gen_syncookie. IP version from the header is taken into account, and it is validated properly with address family.
--- net/core/filter.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
net/core/filter.c +13 -4
@@ -6714,24 +6714,33 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_lenif (!th->ack || th->rst || th->syn)return -ENOENT ; +if (unlikely (iph_len <sizeof (struct iphdr ))) +return -EINVAL ; +if (tcp_synq_no_recent_overflow (sk))return -ENOENT ; cookie =ntohl (th->ack_seq) -1 ; -switch (sk->sk_family) { -case AF_INET : -if (unlikely (iph_len <sizeof (struct iphdr ))) +/* Both struct iphdr and struct ipv6hdr have the version field at the +* same offset so we can cast to the shorter header (struct iphdr). +*/ +switch (((struct iphdr *)iph)->version) { +case 4 : +if (sk->sk_family ==AF_INET6 &&ipv6_only_sock (sk))return -EINVAL ; ret =__cookie_v4_check ((struct iphdr *)iph, th, cookie);break ;#if IS_BUILTIN (CONFIG_IPV6 ) -case AF_INET6 : +case 6 :if (unlikely (iph_len <sizeof (struct ipv6hdr )))return -EINVAL ; +if (sk->sk_family !=AF_INET6 ) +return -EINVAL ; + ret =__cookie_v6_check ((struct ipv6hdr *)iph, th, cookie);break ;#endif /* CONFIG_IPV6 */
Signature · 1 line
2.30.2
PATCH 3/10 bpf: Use EOPNOTSUPP in bpf_tcp_check_syncookie
In reply to Maxim MikityanskiyWhen CONFIG_SYN_COOKIES is off, bpf_tcp_check_syncookie returns ENOTSUPP. It's a non-standard and deprecated code. The related function bpf_tcp_gen_syncookie and most of the other functions use EOPNOTSUPP if some feature is not available. This patch changes ENOTSUPP to EOPNOTSUPP in bpf_tcp_check_syncookie.
--- net/core/filter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/filter.c +1 -1
@@ -6754,7 +6754,7 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_lenreturn -ENOENT ;#else -return -ENOTSUPP ; +return -EOPNOTSUPP ;#endif }
Signature · 1 line
2.30.2
PATCH 4/10 bpf: Make errors of bpf_tcp_check_syncookie distinguishable
In reply to Maxim Mikityanskiybpf_tcp_check_syncookie returns errors when SYN cookie generation is disabled (EINVAL) or when no cookies were recently generated (ENOENT). The same error codes are used for other kinds of errors: invalid parameters (EINVAL), invalid packet (EINVAL, ENOENT), bad cookie (ENOENT). Such an overlap makes it impossible for a BPF program to distinguish different cases that may require different handling.
For a BPF program that accelerates generating and checking SYN cookies, typical logic looks like this (with current error codes annotated):
1. Drop invalid packets (EINVAL, ENOENT).
2. Drop packets with bad cookies (ENOENT).
3. Pass packets with good cookies (0).
4. Pass all packets when cookies are not in use (EINVAL, ENOENT).
The last point also matches the behavior of cookie_v4_check and cookie_v6_check that skip all checks if cookie generation is disabled or no cookies were recently generated. Overlapping error codes, however, make it impossible to distinguish case 4 from cases 1 and 2.
The original commit message of commit 399040847084 ("bpf: add helper to check for a valid SYN cookie") mentions another use case, though:
traffic classification, where it's important to distinguish new connections from existing ones, and case 4 should be distinguishable from case 3.
To match the requirements of both use cases, this patch reassigns error codes of bpf_tcp_check_syncookie and adds missing documentation:
1. EINVAL: Invalid packets.
2. EACCES: Packets with bad cookies.
3. 0: Packets with good cookies.
4. ENOENT: Cookies are not in use.
This way all four cases are easily distinguishable.
--- include/uapi/linux/bpf.h | 18 ++++++++++++++++-- net/core/filter.c | 6 +++--- tools/include/uapi/linux/bpf.h | 18 ++++++++++++++++-- 3 files changed, 35 insertions(+), 7 deletions(-)
include/uapi/linux/bpf.h +16 -2
@@ -3545,8 +3545,22 @@ union bpf_attr {* *th* points to the start of the TCP header, while *th_len* * contains **sizeof**\ (**struct tcphdr**). * Return -* 0 if *iph* and *th* are a valid SYN cookie ACK, or a negative -* error otherwise. +* 0 if *iph* and *th* are a valid SYN cookie ACK. +* +* On failure, the returned value is one of the following: +* +* **-EACCES** if the SYN cookie is not valid. +* +* **-EINVAL** if the packet or input arguments are invalid. +* +* **-ENOENT** if SYN cookies are not issued (no SYN flood, or SYN +* cookies are disabled in sysctl). +* +* **-EOPNOTSUPP** if the kernel configuration does not enable SYN +* cookies (CONFIG_SYN_COOKIES is off). +* +* **-EPROTONOSUPPORT** if the IP version is not 4 or 6 (or 6, but +* CONFIG_IPV6 is disabled). * * long bpf_sysctl_get_name(struct bpf_sysctl *ctx, char *buf, size_t buf_len, u64 flags) * Description
net/core/filter.c +3 -3
@@ -6709,10 +6709,10 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_lenreturn -EINVAL ;if (!sock_net (sk)->ipv4.sysctl_tcp_syncookies) -return -EINVAL ; +return -ENOENT ;if (!th->ack || th->rst || th->syn) -return -ENOENT ; +return -EINVAL ;if (unlikely (iph_len <sizeof (struct iphdr )))return -EINVAL ;
@@ -6752,7 +6752,7 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_lenif (ret >0 )return 0 ; -return -ENOENT ; +return -EACCES ;#else return -EOPNOTSUPP ;#endif
tools/include/uapi/linux/bpf.h +16 -2
@@ -3545,8 +3545,22 @@ union bpf_attr {* *th* points to the start of the TCP header, while *th_len* * contains **sizeof**\ (**struct tcphdr**). * Return -* 0 if *iph* and *th* are a valid SYN cookie ACK, or a negative -* error otherwise. +* 0 if *iph* and *th* are a valid SYN cookie ACK. +* +* On failure, the returned value is one of the following: +* +* **-EACCES** if the SYN cookie is not valid. +* +* **-EINVAL** if the packet or input arguments are invalid. +* +* **-ENOENT** if SYN cookies are not issued (no SYN flood, or SYN +* cookies are disabled in sysctl). +* +* **-EOPNOTSUPP** if the kernel configuration does not enable SYN +* cookies (CONFIG_SYN_COOKIES is off). +* +* **-EPROTONOSUPPORT** if the IP version is not 4 or 6 (or 6, but +* CONFIG_IPV6 is disabled). * * long bpf_sysctl_get_name(struct bpf_sysctl *ctx, char *buf, size_t buf_len, u64 flags) * Description
Signature · 1 line
2.30.2
John Fastabend
reviewerRE: [PATCH bpf-next 04/10] bpf: Make errors of bpf_tcp_check_syncookie distinguishable
Quoted 6 lines · Maxim Mikityanskiy
bpf_tcp_check_syncookiereturns errors when SYN cookie generation is disabled (EINVAL) or when no cookies were recently generated (ENOENT). The same error codes are used for other kinds of errors: invalid parameters (EINVAL), invalid packet (EINVAL, ENOENT), bad cookie (ENOENT). Such an overlap makes it impossible for a BPF program to distinguish different cases that may require different handling.
I'm not sure we can change these errors now. They are embedded in the helper API. I think a BPF program could uncover the meaning of the error anyways with some error path handling?
Anyways even if we do change these most of us who run programs on multiple kernel versions would not be able to rely on them being one way or the other easily.
Quoted 30 lines
For a BPF program that accelerates generating and checking SYN cookies, typical logic looks like this (with current error codes annotated):
1. Drop invalid packets (EINVAL, ENOENT).
2. Drop packets with bad cookies (ENOENT).
3. Pass packets with good cookies (0).
4. Pass all packets when cookies are not in use (EINVAL, ENOENT).
The last point also matches the behavior of
cookie_v4_checkandcookie_v6_checkthat skip all checks if cookie generation is disabled or no cookies were recently generated. Overlapping error codes, however, make it impossible to distinguish case 4 from cases 1 and 2.The original commit message of commit 399040847084 ("bpf: add helper to check for a valid SYN cookie") mentions another use case, though:
traffic classification, where it's important to distinguish new connections from existing ones, and case 4 should be distinguishable from case 3.To match the requirements of both use cases, this patch reassigns error codes of
bpf_tcp_check_syncookieand adds missing documentation:1. EINVAL: Invalid packets.
2. EACCES: Packets with bad cookies.
Quoted 8 lines
3. 0: Packets with good cookies.
4. ENOENT: Cookies are not in use.
This way all four cases are easily distinguishable.
Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>
At very leasst this would need a fixes tag and should be backported as a bug. Then we at least have a chance stable and LTS kernels report the same thing.
[...]
net/core/filter.c
Quoted 2 lines
net/core/filter.c +0
-0
I'll take a stab at how a program can learn the error cause today.
BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
struct tcphdr *, th, u32, th_len)
{
#ifdef CONFIG_SYN_COOKIES u32 cookie; int ret;
// BPF program should know it pass bad values and can check
if (unlikely(!sk || th_len < sizeof(*th))) return -EINVAL;
// sk_protocol and sk_state are exposed in sk and can be read directly
/* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */ if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN) return -EINVAL;
// This is a user space knob right? I think this is a misconfig user can // check before loading a program with check_syncookie?
if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies) return -EINVAL;
// We have th pointer can't we just check?
if (!th->ack || th->rst || th->syn)
return -ENOENT;
if (tcp_synq_no_recent_overflow(sk))
return -ENOENT;
cookie = ntohl(th->ack_seq) - 1;
switch (sk->sk_family) {
case AF_INET:
// misconfiguration but can be checked.
if (unlikely(iph_len < sizeof(struct iphdr)))
return -EINVAL;
ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
break;#if IS_BUILTIN(CONFIG_IPV6)
case AF_INET6:
// misconfiguration can check as well
if (unlikely(iph_len < sizeof(struct ipv6hdr))) return -EINVAL; ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie); break;
#endif /* CONFIG_IPV6 */
default: return -EPROTONOSUPPORT; } if (ret > 0) return 0; return -ENOENT; #else return -ENOTSUPP;
#endif
}
So I guess my point is we have all the fields we could write a bit of BPF to find the error cause if necessary. Might be better than dealing with changing the error code and having to deal with the differences in kernels. I do see how it would have been better to get errors correct on the first patch though :/
By the way I haven't got to the next set of patches with the actual features, but why not push everything above this patch as fixes in its own series. Then the fixes can get going why we review the feature.
Thanks,
John
Re: [PATCH bpf-next 04/10] bpf: Make errors of bpf_tcp_check_syncookie distinguishable
Quoted 8 lines · John Fastabend
Quoted 6 lines · Maxim Mikityanskiy
bpf_tcp_check_syncookiereturns errors when SYN cookie generation is disabled (EINVAL) or when no cookies were recently generated (ENOENT). The same error codes are used for other kinds of errors: invalid parameters (EINVAL), invalid packet (EINVAL, ENOENT), bad cookie (ENOENT). Such an overlap makes it impossible for a BPF program to distinguish different cases that may require different handling.
Quoted 7 lines
I'm not sure we can change these errors now. They are embedded in the helper API. I think a BPF program could uncover the meaning of the error anyways with some error path handling?
Anyways even if we do change these most of us who run programs on multiple kernel versions would not be able to rely on them being one way or the other easily.
The thing is, the error codes aren't really documented:
* 0 if *iph* and *th* are a valid SYN cookie ACK, or a negative
* error otherwise.
My patch doesn't break this assumption.
Practically speaking, there are two use cases of bpf_tcp_check_syncookie that I know about: traffic classification (find NEW ACK packets with the right cookie) and SYN flood protection.
For traffic classification, it's not important what error code we get. The logic for ACK packets is as follows:
1. Connection established => ESTABLISHED. Otherwise,
2. bpf_tcp_check_syncookie returns 0 => NEW. Otherwise,
3. INVALID (regardless of the specific error code).
My patch doesn't break this use case.
Quoted 11 lines
Quoted 11 lines
For a BPF program that accelerates generating and checking SYN cookies, typical logic looks like this (with current error codes annotated):
1. Drop invalid packets (EINVAL, ENOENT).
2. Drop packets with bad cookies (ENOENT).
3. Pass packets with good cookies (0).
4. Pass all packets when cookies are not in use (EINVAL, ENOENT).
Now that I'm reflecting on it again, it would make more sense to drop packets in case 4: it's a new packet, it's an ACK, and we don't expect any cookies.
Quoted 4 lines
Quoted 4 lines
The last point also matches the behavior of
cookie_v4_checkandcookie_v6_checkthat skip all checks if cookie generation is disabled or no cookies were recently generated. Overlapping error codes, however, make it impossible to distinguish case 4 from cases 1 and 2.
If so, we don't strictly need to distinguish case 4 from 1 and 2. The logic for ACK packets is similar:
1. Connection established => XDP_PASS. Otherwise,
2. bpf_tcp_check_syncookie returns 0 => XDP_PASS. Otherwise,
3. XDP_DROP.
So, on one hand, it looks like both use cases can be implemented without this patch. On the other hand, changing error codes to more meaningful shouldn't break existing programs and can have its benefits, for example, in debugging or in statistic counting.
Quoted 17 lines
Quoted 17 lines
The original commit message of commit 399040847084 ("bpf: add helper to check for a valid SYN cookie") mentions another use case, though:
traffic classification, where it's important to distinguish new connections from existing ones, and case 4 should be distinguishable from case 3.To match the requirements of both use cases, this patch reassigns error codes of
bpf_tcp_check_syncookieand adds missing documentation:1. EINVAL: Invalid packets.
2. EACCES: Packets with bad cookies.
3. 0: Packets with good cookies.
4. ENOENT: Cookies are not in use.
Quoted 8 lines
Quoted 4 lines
This way all four cases are easily distinguishable.
Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>At very leasst this would need a fixes tag and should be backported as a bug. Then we at least have a chance stable and LTS kernels report the same thing.
That's a good idea.
Quoted 18 lines
[...]
net/core/filter.c
Quoted 2 lines
net/core/filter.c +0
-0I'll take a stab at how a program can learn the error cause today.
BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32,
iph_len,
struct tcphdr *, th, u32,th_len)
{#ifdef CONFIG_SYN_COOKIES u32 cookie; int ret;// BPF program should know it pass bad values and can check
if (unlikely(!sk || th_len < sizeof(*th))) return -EINVAL;
Quoted 7 lines
//
sk_protocolandsk_stateare exposed in sk and can be read directly/* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */ if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN) return -EINVAL;// This is a user space knob right? I think this is a misconfig user can // check before loading a program with
check_syncookie?
bpf_tcp_check_syncookie was initially introduced for the classification use case, to be able to classify new ACK packets with the right cookie as NEW. The XDP program classifies traffic regardless of whether SYN cookies are enabled. If we need to check the sysctl in userspace, it means we need two XDP programs (or additional trickery passing this value through a map).
Quoted 4 lines
if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies) return -EINVAL;// We have th pointer can't we just check?
Yes, most of the checks can be repeated in BPF, but it's obvious it's slower to do all the checks twice.
Quoted 5 lines
if (!th->ack || th->rst || th->syn) return -ENOENT; if (tcp_synq_no_recent_overflow(sk)) return -ENOENT;
This specific check can't be done in BPF.
Quoted 42 lines
cookie = ntohl(th->ack_seq) - 1; switch (sk->sk_family) { case AF_INET: // misconfiguration but can be checked. if (unlikely(iph_len < sizeof(struct iphdr))) return -EINVAL; ret = __cookie_v4_check((struct iphdr *)iph, th, cookie); break;#if
IS_BUILTIN(CONFIG_IPV6)
caseAF_INET6:
// misconfiguration can check as wellif (unlikely(iph_len < sizeof(struct ipv6hdr))) return -EINVAL; ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie); break;#endif /*
CONFIG_IPV6*/default: return -EPROTONOSUPPORT; } if (ret > 0) return 0; return -ENOENT; #else return -ENOTSUPP;#endif
}So I guess my point is we have all the fields we could write a bit of BPF to find the error cause if necessary. Might be better than dealing with changing the error code and having to deal with the differences in kernels. I do see how it would have been better to get errors correct on the first patch though :/
Quoted 4 lines
By the way I haven't got to the next set of patches with the actual features, but why not push everything above this patch as fixes in its own series. Then the fixes can get going why we review the feature.
OK, I'll respin the fixes separately, while the discussion on the approach to expose conntrack is going on.
Thanks for reviewing!
Quoted 4 lines
Thanks,
John
Re: [PATCH bpf-next 04/10] bpf: Make errors of bpf_tcp_check_syncookie distinguishable
Quoted 14 lines · Maxim Mikityanskiy
Quoted 12 lines · John Fastabend
Quoted 6 lines · Maxim Mikityanskiy
bpf_tcp_check_syncookiereturns errors when SYN cookie generation is disabled (EINVAL) or when no cookies were recently generated (ENOENT). The same error codes are used for other kinds of errors: invalid parameters (EINVAL), invalid packet (EINVAL, ENOENT), bad cookie (ENOENT). Such an overlap makes it impossible for a BPF program to distinguish different cases that may require different handling.I'm not sure we can change these errors now. They are embedded in the helper API. I think a BPF program could uncover the meaning of the error anyways with some error path handling?
Quoted 8 lines
Quoted 3 lines
Anyways even if we do change these most of us who run programs on multiple kernel versions would not be able to rely on them being one way or the other easily.
The thing is, the error codes aren't really documented:
* 0 if *iph* and *th* are a valid SYN cookie ACK, or a negative
* error otherwise.
Yes, I kept this vague so that there is some wiggle room. FWIW your proposed change would not break our BPF. Same for the examples included in the kernel source itself. That is no guarantee of course.
Personally, I'm a bit on the fence regarding a backport of this. Either this is a legitimate extension of the API and we don't backport, or it's a bug (how?) and then we should backport.
Signature · 4 lines
Lorenz Bauer | Systems Engineer 6th Floor, County Hall/The Riverside Building, SE1 7PB, UK www.cloudflare.com
PATCH 5/10 bpf: Fix documentation of th_len in bpf_tcp_{gen,check}_syncookie
In reply to Maxim Mikityanskiybpf_tcp_gen_syncookie and bpf_tcp_check_syncookie expect the full length of the TCP header (with all extensions). Fix the documentation that says it should be sizeof(struct tcphdr).
--- include/uapi/linux/bpf.h | 6 ++++-- tools/include/uapi/linux/bpf.h | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-)
include/uapi/linux/bpf.h +4 -2
@@ -3543,7 +3543,8 @@ union bpf_attr {* **sizeof**\ (**struct ip6hdr**). * * *th* points to the start of the TCP header, while *th_len* -* contains **sizeof**\ (**struct tcphdr**). +* contains the length of the TCP header (at least +* **sizeof**\ (**struct tcphdr**)). * Return * 0 if *iph* and *th* are a valid SYN cookie ACK. *
@@ -3743,7 +3744,8 @@ union bpf_attr {* **sizeof**\ (**struct ip6hdr**). * * *th* points to the start of the TCP header, while *th_len* -* contains the length of the TCP header. +* contains the length of the TCP header (at least +* **sizeof**\ (**struct tcphdr**)). * Return * On success, lower 32 bits hold the generated SYN cookie in * followed by 16 bits which hold the MSS value for that cookie,
tools/include/uapi/linux/bpf.h +4 -2
@@ -3543,7 +3543,8 @@ union bpf_attr {* **sizeof**\ (**struct ip6hdr**). * * *th* points to the start of the TCP header, while *th_len* -* contains **sizeof**\ (**struct tcphdr**). +* contains the length of the TCP header (at least +* **sizeof**\ (**struct tcphdr**)). * Return * 0 if *iph* and *th* are a valid SYN cookie ACK. *
@@ -3743,7 +3744,8 @@ union bpf_attr {* **sizeof**\ (**struct ip6hdr**). * * *th* points to the start of the TCP header, while *th_len* -* contains the length of the TCP header. +* contains the length of the TCP header (at least +* **sizeof**\ (**struct tcphdr**)). * Return * On success, lower 32 bits hold the generated SYN cookie in * followed by 16 bits which hold the MSS value for that cookie,
Signature · 1 line
2.30.2
PATCH 6/10 bpf: Expose struct nf_conn to BPF
In reply to Maxim MikityanskiyThis commit adds struct nf_conn as a new type to BPF. For now, only the status field is mapped. It will allow to add helpers that will expose conntrack information to BPF programs.
--- include/linux/bpf.h | 46 ++++++++++++++++ include/uapi/linux/bpf.h | 4 ++ kernel/bpf/verifier.c | 95 ++++++++++++++++++++++++++++++++-- net/core/filter.c | 41 +++++++++++++++ scripts/bpf_doc.py | 1 + tools/include/uapi/linux/bpf.h | 4 ++ 6 files changed, 186 insertions(+), 5 deletions(-)
include/linux/bpf.h +46 -0
@@ -341,6 +341,7 @@ enum bpf_arg_type {ARG_PTR_TO_STACK_OR_NULL ,/* pointer to stack or NULL */ ARG_PTR_TO_CONST_STR ,/* pointer to a null terminated read-only string */ ARG_PTR_TO_TIMER ,/* pointer to bpf_timer */ +ARG_PTR_TO_NF_CONN ,/* pointer to nf_conn */ __BPF_ARG_TYPE_MAX, };
@@ -358,6 +359,7 @@ enum bpf_return_type {RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL ,/* returns a pointer to a valid memory or a btf_id or NULL */ RET_PTR_TO_MEM_OR_BTF_ID ,/* returns a pointer to a valid memory or a btf_id */ RET_PTR_TO_BTF_ID ,/* returns a pointer to a btf_id */ + RET_PTR_TO_NF_CONN_OR_NULL, /* returns a pointer to a nf_conn or NULL */ };/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
@@ -459,6 +461,8 @@ enum bpf_reg_type {PTR_TO_PERCPU_BTF_ID ,/* reg points to a percpu kernel variable */ PTR_TO_FUNC ,/* reg points to a bpf program function */ PTR_TO_MAP_KEY ,/* reg points to a map element key */ +PTR_TO_NF_CONN ,/* reg points to struct nf_conn */ +PTR_TO_NF_CONN_OR_NULL ,/* reg points to struct nf_conn or NULL */ __BPF_REG_TYPE_MAX, };
@@ -2127,6 +2131,32 @@ u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,struct bpf_insn *insn_buf, struct bpf_prog *prog, u32 *target_size); + +#if IS_BUILTIN (CONFIG_NF_CONNTRACK ) +bool bpf_ct_is_valid_access (int off,int size,enum bpf_access_type type, +struct bpf_insn_access_aux *info); +u32 bpf_ct_convert_ctx_access (enum bpf_access_type type, +const struct bpf_insn *si, +struct bpf_insn *insn_buf, +struct bpf_prog *prog,u32 *target_size); +#else +static inline bool bpf_ct_is_valid_access (int off,int size, +enum bpf_access_type type, +struct bpf_insn_access_aux *info) +{ +return false; +} + +static inline u32 bpf_ct_convert_ctx_access (enum bpf_access_type type, +const struct bpf_insn *si, +struct bpf_insn *insn_buf, +struct bpf_prog *prog, +u32 *target_size) +{ +return 0 ; +} +#endif +#else static inline bool bpf_sock_common_is_valid_access(int off, int size, enum bpf_access_type type,
@@ -2148,6 +2178,22 @@ static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, {return 0 ; } + +static inline bool bpf_ct_is_valid_access (int off,int size, +enum bpf_access_type type, +struct bpf_insn_access_aux *info) +{ +return false; +} + +static inline u32 bpf_ct_convert_ctx_access (enum bpf_access_type type, +const struct bpf_insn *si, +struct bpf_insn *insn_buf, +struct bpf_prog *prog, +u32 *target_size) +{ +return 0 ; +}#endif #ifdef CONFIG_INET
include/uapi/linux/bpf.h +4 -0
@@ -5472,6 +5472,10 @@ struct bpf_xdp_sock {__u32 queue_id; }; +struct bpf_nf_conn { +__u64 status; +}; +#define XDP_PACKET_HEADROOM 256/* User return codes for XDP prog type.
kernel/bpf/verifier.c +90 -5
213 lines not on this page · raw message
net/core/filter.c +41 -0
59 lines not on this page · raw message
scripts/bpf_doc.py +1 -0
@@ -596,6 +596,7 @@ class PrinterHelpers(Printer): 'struct socket', 'struct file', 'struct bpf_timer', + 'struct bpf_nf_conn', } mapped_types = { 'u8': '__u8',
tools/include/uapi/linux/bpf.h +4 -0
@@ -5472,6 +5472,10 @@ struct bpf_xdp_sock {__u32 queue_id; }; +struct bpf_nf_conn { +__u64 status; +}; +#define XDP_PACKET_HEADROOM 256/* User return codes for XDP prog type.
Signature · 1 line
2.30.2
PATCH 7/10 bpf: Add helpers to query conntrack info
In reply to Maxim MikityanskiyThe new helpers (bpf_ct_lookup_tcp and bpf_ct_lookup_udp) allow to query connection tracking information of TCP and UDP connections based on source and destination IP address and port. The helper returns a pointer to struct nf_conn (if the conntrack entry was found), which needs to be released with bpf_ct_release.
--- include/uapi/linux/bpf.h | 81 +++++++++++++ kernel/bpf/verifier.c | 9 +- net/core/filter.c | 205 +++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 81 +++++++++++++ 4 files changed, 373 insertions(+), 3 deletions(-)
include/uapi/linux/bpf.h +81 -0
@@ -4925,6 +4925,79 @@ union bpf_attr {* Return * The number of bytes written to the buffer, or a negative error * in case of failure. +* +* struct bpf_nf_conn *bpf_ct_lookup_tcp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 *flags_err) +* Description +* Look for conntrack info for a TCP connection matching *tuple*, +* optionally in a child network namespace *netns*. +* +* The *flags_err* argument is used as an input parameter for flags +* and output parameter for the error code. The flags can be a +* combination of one or more of the following values: +* +* **BPF_F_CT_DIR_REPLY** +* When set, the conntrack direction is IP_CT_DIR_REPLY, +* otherwise IP_CT_DIR_ORIGINAL. +* +* If the function returns **NULL**, *flags_err* will indicate the +* error code: +* +* **EAFNOSUPPORT** +* *tuple_size* doesn't match supported address families +* (AF_INET; AF_INET6 when CONFIG_IPV6 is enabled). +* +* **EINVAL** +* Input arguments are not valid. +* +* **ENOENT** +* Connection tracking entry for *tuple* wasn't found. +* +* This helper is available only if the kernel was compiled with +* **CONFIG_NF_CONNTRACK** configuration option as built-in. +* Return +* Connection tracking status (see **enum ip_conntrack_status**), +* or **NULL** in case of failure or if there is no conntrack entry +* for this tuple. +* +* struct bpf_nf_conn *bpf_ct_lookup_udp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 *flags_err) +* Description +* Look for conntrack info for a UDP connection matching *tuple*, +* optionally in a child network namespace *netns*. +* +* The *flags_err* argument is used as an input parameter for flags +* and output parameter for the error code. The flags can be a +* combination of one or more of the following values: +* +* **BPF_F_CT_DIR_REPLY** +* When set, the conntrack direction is IP_CT_DIR_REPLY, +* otherwise IP_CT_DIR_ORIGINAL. +* +* If the function returns **NULL**, *flags_err* will indicate the +* error code: +* +* **EAFNOSUPPORT** +* *tuple_size* doesn't match supported address families +* (AF_INET; AF_INET6 when CONFIG_IPV6 is enabled). +* +* **EINVAL** +* Input arguments are not valid. +* +* **ENOENT** +* Connection tracking entry for *tuple* wasn't found. +* +* This helper is available only if the kernel was compiled with +* **CONFIG_NF_CONNTRACK** configuration option as built-in. +* Return +* Connection tracking status (see **enum ip_conntrack_status**), +* or **NULL** in case of failure or if there is no conntrack entry +* for this tuple. +* +* long bpf_ct_release(void *ct) +* Description +* Release the reference held by *ct*. *ct* must be a non-**NULL** +* pointer that was returned from **bpf_ct_lookup_xxx**\ (). +* Return +* 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5105,6 +5178,9 @@ union bpf_attr {FN (task_pt_regs), \FN (get_branch_snapshot), \FN (trace_vprintk), \ +FN (ct_lookup_tcp), \ +FN (ct_lookup_udp), \ +FN (ct_release), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -5288,6 +5364,11 @@ enum { BPF_F_EXCLUDE_INGRESS = (1ULL << 4), }; +/* Flags for bpf_ct_lookup_{tcp,udp} helpers. */ +enum { +BPF_F_CT_DIR_REPLY = (1ULL <<0 ), +}; +#define __bpf_md_ptr (type, name) \ union { \ type name; \
kernel/bpf/verifier.c +6 -3
@@ -506,7 +506,8 @@ static bool is_release_function(enum bpf_func_id func_id) {return func_id == BPF_FUNC_sk_release || func_id == BPF_FUNC_ringbuf_submit || - func_id == BPF_FUNC_ringbuf_discard; + func_id == BPF_FUNC_ringbuf_discard || + func_id == BPF_FUNC_ct_release; }static bool may_be_acquire_function (enum bpf_func_id func_id)
@@ -515,7 +516,8 @@ static bool may_be_acquire_function(enum bpf_func_id func_id) func_id == BPF_FUNC_sk_lookup_udp || func_id == BPF_FUNC_skc_lookup_tcp || func_id == BPF_FUNC_map_lookup_elem || - func_id == BPF_FUNC_ringbuf_reserve; + func_id == BPF_FUNC_ringbuf_reserve || + func_id == BPF_FUNC_ct_lookup_tcp; } static bool is_acquire_function(enum bpf_func_id func_id,
@@ -526,7 +528,8 @@ static bool is_acquire_function(enum bpf_func_id func_id,if (func_id == BPF_FUNC_sk_lookup_tcp || func_id == BPF_FUNC_sk_lookup_udp || func_id == BPF_FUNC_skc_lookup_tcp || - func_id == BPF_FUNC_ringbuf_reserve) + func_id == BPF_FUNC_ringbuf_reserve || + func_id == BPF_FUNC_ct_lookup_tcp)return true; if (func_id == BPF_FUNC_map_lookup_elem &&
net/core/filter.c +205 -0
229 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +81 -0
99 lines not on this page · raw message
Signature · 1 line
2.30.2
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
Quoted 8 lines · Maxim Mikityanskiy
The new helpers (
bpf_ct_lookup_tcpandbpf_ct_lookup_udp) allow to query connection tracking information of TCP and UDP connections based on source and destination IP address and port. The helper returns a pointer to structnf_conn(if the conntrack entry was found), which needs to be released withbpf_ct_release.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>
The last discussion on this [0] suggested that stable BPF helpers for conntrack were not desired, hence the recent series [1] to extend kfunc support to modules and base the conntrack work on top of it, which I'm working on now (supporting both CT lookup and insert).
[0]: https://lore.kernel.org/bpf/CAADnVQJTJzxzig=1vvAUMXELUoOwm2vXq0ahP4mfhBWGsCm9QA@mail.gmail.com
[1]: https://lore.kernel.org/bpf/CAADnVQKDPG+U-NwoAeNSU5Ef9ZYhhGcgL4wBkFoP-E9h8-XZhw@mail.gmail.com
--
Kartikeya
Re: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
Quoted 7 lines · Kumar Kartikeya Dwivedi
Quoted 6 lines · Maxim Mikityanskiy
The new helpers (
bpf_ct_lookup_tcpandbpf_ct_lookup_udp) allow to query connection tracking information of TCP and UDP connections based on source and destination IP address and port. The helper returns a pointer to structnf_conn(if the conntrack entry was found), which needs to be released withbpf_ct_release.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>The last discussion on this [0] suggested that stable BPF helpers for conntrack were not desired, hence the recent series [1] to extend kfunc support to modules and base the conntrack work on top of it, which I'm working on now (supporting both CT lookup and insert).
This will sabotage netfilter pipeline and the way things work more and more 8-(
If you want to use netfilter with ebpf, please have a look at the RFC I posted and lets work on adding a netfilter specific program type that can run ebpf programs directly from any of the existing netfilter hook points.
Thanks.
Re: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
Quoted 11 lines · Florian Westphal
Quoted 10 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Maxim Mikityanskiy
The new helpers (
bpf_ct_lookup_tcpandbpf_ct_lookup_udp) allow to query connection tracking information of TCP and UDP connections based on source and destination IP address and port. The helper returns a pointer to structnf_conn(if the conntrack entry was found), which needs to be released withbpf_ct_release.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>
Quoted 7 lines
Quoted 4 lines
The last discussion on this [0] suggested that stable BPF helpers for conntrack were not desired, hence the recent series [1] to extend kfunc support to modules and base the conntrack work on top of it, which I'm working on now (supporting both CT lookup and insert).
This will sabotage netfilter pipeline and the way things work more and more 8-(
Why?
Quoted 4 lines
If you want to use netfilter with ebpf, please have a look at the RFC I posted and lets work on adding a netfilter specific program type that can run ebpf programs directly from any of the existing netfilter hook points.
Accelerating netfilter using BPF is a worthy goal in itself, but I also think having the ability to lookup into conntrack from XDP is useful for cases where someone wants to bypass the stack entirely (for accelerating packet forwarding, say). I don't think these goals are in conflict either, what makes you say they are?
-Toke
Re: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
Quoted 18 lines · Toke Høiland-Jørgensen
Quoted 16 lines · Florian Westphal
Quoted 14 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Maxim Mikityanskiy
The new helpers (
bpf_ct_lookup_tcpandbpf_ct_lookup_udp) allow to query connection tracking information of TCP and UDP connections based on source and destination IP address and port. The helper returns a pointer to structnf_conn(if the conntrack entry was found), which needs to be released withbpf_ct_release.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>The last discussion on this [0] suggested that stable BPF helpers for conntrack were not desired, hence the recent series [1] to extend kfunc support to modules and base the conntrack work on top of it, which I'm working on now (supporting both CT lookup and insert).
Quoted 4 lines
Quoted 2 lines
This will sabotage netfilter pipeline and the way things work more and more 8-(
Why?
Lookups should be fine. Insertions are the problem.
NAT hooks are expected to execute before the insertion into the conntrack table.
If you insert before, NAT hooks won't execute, i.e. rules that use dnat/redirect/masquerade have no effect.
Quoted 10 lines
Quoted 4 lines
If you want to use netfilter with ebpf, please have a look at the RFC I posted and lets work on adding a netfilter specific program type that can run ebpf programs directly from any of the existing netfilter hook points.
Accelerating netfilter using BPF is a worthy goal in itself, but I also think having the ability to lookup into conntrack from XDP is useful for cases where someone wants to bypass the stack entirely (for accelerating packet forwarding, say). I don't think these goals are in conflict either, what makes you say they are?
Lookup is fine, I don't see fundamental issues with XDP-based bypass, there are flowtables that also bypass classic forward path via the netfilter ingress hook (first packet needs to go via classic path to pass through all filter + nat rules and is offlloaded to HW or SW via the 'flow add' statement in nftables.
I don't think there is anything that stands in the way of replicating this via XDP.
Re: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
Quoted 24 lines · Florian Westphal
Quoted 18 lines · Toke Høiland-Jørgensen
Quoted 16 lines · Florian Westphal
Quoted 14 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Maxim Mikityanskiy
The new helpers (
bpf_ct_lookup_tcpandbpf_ct_lookup_udp) allow to query connection tracking information of TCP and UDP connections based on source and destination IP address and port. The helper returns a pointer to structnf_conn(if the conntrack entry was found), which needs to be released withbpf_ct_release.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>The last discussion on this [0] suggested that stable BPF helpers for conntrack were not desired, hence the recent series [1] to extend kfunc support to modules and base the conntrack work on top of it, which I'm working on now (supporting both CT lookup and insert).
Quoted 4 lines
Quoted 2 lines
This will sabotage netfilter pipeline and the way things work more and more 8-(
Why?
Quoted 7 lines
Lookups should be fine. Insertions are the problem.
NAT hooks are expected to execute before the insertion into the conntrack table.
If you insert before, NAT hooks won't execute, i.e. rules that use dnat/redirect/masquerade have no effect.
Well yes, if you insert the wrong state into the conntrack table, you're going to get wrong behaviour. That's sorta expected, there are lots of things XDP can do to disrupt the packet flow (like just dropping the packets :)).
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
If you want to use netfilter with ebpf, please have a look at the RFC I posted and lets work on adding a netfilter specific program type that can run ebpf programs directly from any of the existing netfilter hook points.
Accelerating netfilter using BPF is a worthy goal in itself, but I also think having the ability to lookup into conntrack from XDP is useful for cases where someone wants to bypass the stack entirely (for accelerating packet forwarding, say). I don't think these goals are in conflict either, what makes you say they are?
Quoted 8 lines
Lookup is fine, I don't see fundamental issues with XDP-based bypass, there are flowtables that also bypass classic forward path via the netfilter ingress hook (first packet needs to go via classic path to pass through all filter + nat rules and is offlloaded to HW or SW via the 'flow add' statement in nftables.
I don't think there is anything that stands in the way of replicating this via XDP.
What I want to be able to do is write an XDP program that does the following:
1. Parse the packet header and determine if it's a packet type we know how to handle. If not, just return XDP_PASS and let the stack deal with corner cases.
2. If we know how to handle the packet (say, it's TCP or UDP), do a lookup into conntrack to figure out if there's state for it and we need to do things like NAT.
3. If we need to NAT, rewrite the packet based on the information we got back from conntrack.
4. Update the conntrack state to be consistent with the packet, and then redirect it out the destination interface.
I.e., in the common case the packet doesn't go through the stack at all; but we need to make conntrack aware that we processed the packet so the entry doesn't expire (and any state related to the flow gets updated). Ideally we should also be able to create new state for a flow we haven't seen before.
This requires updating of state, but I see no reason why this shouldn't be possible?
-Toke
Re: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
Quoted 5 lines · Toke Høiland-Jørgensen
Quoted 5 lines
Lookups should be fine. Insertions are the problem.
NAT hooks are expected to execute before the insertion into the conntrack table.
Quoted 7 lines
Quoted 2 lines
If you insert before, NAT hooks won't execute, i.e. rules that use dnat/redirect/masquerade have no effect.
Well yes, if you insert the wrong state into the conntrack table, you're going to get wrong behaviour. That's sorta expected, there are lots of things XDP can do to disrupt the packet flow (like just dropping the packets :)).
Sure, but I'm not sure I understand the use case.
Insertion at XDP layer turns off netfilters NAT capability, so its incompatible with the classic forwarding path.
If thats fine, why do you need to insert into the conntrack table to begin with? The entire infrastructure its designed for is disabled...
Quoted 9 lines
Quoted 2 lines
I don't think there is anything that stands in the way of replicating this via XDP.
What I want to be able to do is write an XDP program that does the following:
1. Parse the packet header and determine if it's a packet type we know how to handle. If not, just return
XDP_PASSand let the stack deal with corner cases.
Quoted 6 lines
2. If we know how to handle the packet (say, it's TCP or UDP), do a lookup into conntrack to figure out if there's state for it and we need to do things like NAT.
3. If we need to NAT, rewrite the packet based on the information we got back from conntrack.
You could already do that by storing that info in bpf maps The ctnetlink event generated on conntrack insertion contains the NAT mapping information, so you could have a userspace daemon that intercepts those to update the map.
Quoted 6 lines
4. Update the conntrack state to be consistent with the packet, and then redirect it out the destination interface.
I.e., in the common case the packet doesn't go through the stack at all; but we need to make conntrack aware that we processed the packet so the entry doesn't expire (and any state related to the flow gets updated).
In the HW offload case, conntrack is bypassed completely. There is an IPS_(HW)_OFFLOAD_BIT that prevents the flow from expiring.
Quoted 2 lines
Ideally we should also be able to create new state for a flow we haven't seen before.
The way HW offload was intended to work is to allow users to express what flows should be offloaded via 'flow add' expression in nftables, so they can e.g. use byte counters or rate estimators etc. to make such a decision. So initial packet always passes via normal stack.
This is also needed to consider e.g. XFRM -- nft_flow_offload.c won't offload if the packet has a secpath attached (i.e., will get encrypted later).
I suspect we'd want a way to notify/call an ebpf program instead so we can avoid the ctnetlink -> userspace -> update dance and do the XDP 'flow bypass information update' from inside the kernel and ebpf/XDP reimplementation of the nf flow table (it uses the netfilter ingress hook on the configured devices; everyhing it does should be doable from XDP).
Quoted 2 lines
This requires updating of state, but I see no reason why this shouldn't be possible?
Updating ct->status is problematic, there would have to be extra checks that prevent non-atomic writes and toggling of special bits such as CONFIRMED, TEMPLATE or DYING. Adding a helper to toggle something specific, e.g. the offload state bit, should be okay.
Re: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
Quoted 14 lines · Florian Westphal
Quoted 5 lines · Toke Høiland-Jørgensen
Quoted 5 lines
Lookups should be fine. Insertions are the problem.
NAT hooks are expected to execute before the insertion into the conntrack table.
Quoted 7 lines
Quoted 2 lines
If you insert before, NAT hooks won't execute, i.e. rules that use dnat/redirect/masquerade have no effect.
Well yes, if you insert the wrong state into the conntrack table, you're going to get wrong behaviour. That's sorta expected, there are lots of things XDP can do to disrupt the packet flow (like just dropping the packets :)).
Quoted 7 lines
Sure, but I'm not sure I understand the use case.
Insertion at XDP layer turns off netfilters NAT capability, so its incompatible with the classic forwarding path.
If thats fine, why do you need to insert into the conntrack table to begin with? The entire infrastructure its designed for is disabled...
One of the major selling points of XDP is that you can reuse the existing kernel infrastructure instead of having to roll your own. So sure, one could implement their own conntrack using BPF maps (as indeed, e.g., Cilium has done), but why do that when you can take advantage of the existing one in the kernel? Same reason we have the bpf_fib_lookup() helper...
Quoted 13 lines
Quoted 13 lines
Quoted 2 lines
I don't think there is anything that stands in the way of replicating this via XDP.
What I want to be able to do is write an XDP program that does the following:
1. Parse the packet header and determine if it's a packet type we know how to handle. If not, just return
XDP_PASSand let the stack deal with corner cases.2. If we know how to handle the packet (say, it's TCP or UDP), do a lookup into conntrack to figure out if there's state for it and we need to do things like NAT.
Quoted 7 lines
Quoted 2 lines
3. If we need to NAT, rewrite the packet based on the information we got back from conntrack.
You could already do that by storing that info in bpf maps The ctnetlink event generated on conntrack insertion contains the NAT mapping information, so you could have a userspace daemon that intercepts those to update the map.
Sure, but see above.
Quoted 9 lines
Quoted 6 lines
4. Update the conntrack state to be consistent with the packet, and then redirect it out the destination interface.
I.e., in the common case the packet doesn't go through the stack at all; but we need to make conntrack aware that we processed the packet so the entry doesn't expire (and any state related to the flow gets updated).
In the HW offload case, conntrack is bypassed completely. There is an IPS_(HW)_OFFLOAD_BIT that prevents the flow from expiring.
That's comparable in execution semantics (stack is bypassed entirely), but not in control plane semantics (we lookup from XDP instead of pushing flows down to an offload).
Quoted 12 lines
Quoted 2 lines
Ideally we should also be able to create new state for a flow we haven't seen before.
The way HW offload was intended to work is to allow users to express what flows should be offloaded via 'flow add' expression in nftables, so they can e.g. use byte counters or rate estimators etc. to make such a decision. So initial packet always passes via normal stack.
This is also needed to consider e.g. XFRM --
nft_flow_offload.cwon't offload if the packet has a secpath attached (i.e., will get encrypted later).
Quoted 6 lines
I suspect we'd want a way to notify/call an ebpf program instead so we can avoid the ctnetlink -> userspace -> update dance and do the XDP 'flow bypass information update' from inside the kernel and ebpf/XDP reimplementation of the nf flow table (it uses the netfilter ingress hook on the configured devices; everyhing it does should be doable from XDP).
But the point is exactly that we don't have to duplicate the state into BPF, we can make XDP look it up directly.
Quoted 7 lines
Quoted 2 lines
This requires updating of state, but I see no reason why this shouldn't be possible?
Updating
ct->statusis problematic, there would have to be extra checks that prevent non-atomic writes and toggling of special bits such as CONFIRMED, TEMPLATE or DYING. Adding a helper to toggle something specific, e.g. the offload state bit, should be okay.
We can certainly constrain the update so it's not possible to get into an unsafe state. The primary use case is accelerating the common case, punting to the stack is fine for corner cases.
-Toke
Re: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
Quoted 5 lines · Toke Høiland-Jørgensen
Quoted 5 lines
Sure, but I'm not sure I understand the use case.
Insertion at XDP layer turns off netfilters NAT capability, so its incompatible with the classic forwarding path.
Quoted 9 lines
Quoted 2 lines
If thats fine, why do you need to insert into the conntrack table to begin with? The entire infrastructure its designed for is disabled...
One of the major selling points of XDP is that you can reuse the existing kernel infrastructure instead of having to roll your own. So sure, one could implement their own conntrack using BPF maps (as indeed, e.g., Cilium has done), but why do that when you can take advantage of the existing one in the kernel? Same reason we have the
bpf_fib_lookup()helper...
Exactly, and a key point is that it allows consistency between XDP fast path and full stack slow path. e.g., the BPF program is removed or defers a flow to full stack for some reason.
Re: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
In reply to Toke Høiland-JørgensenQuoted 24 lines · Toke Høiland-Jørgensen
Quoted 14 lines · Florian Westphal
Quoted 5 lines · Toke Høiland-Jørgensen
Quoted 5 lines
Lookups should be fine. Insertions are the problem.
NAT hooks are expected to execute before the insertion into the conntrack table.
Quoted 7 lines
Quoted 2 lines
If you insert before, NAT hooks won't execute, i.e. rules that use dnat/redirect/masquerade have no effect.
Well yes, if you insert the wrong state into the conntrack table, you're going to get wrong behaviour. That's sorta expected, there are lots of things XDP can do to disrupt the packet flow (like just dropping the packets :)).
Quoted 7 lines
Sure, but I'm not sure I understand the use case.
Insertion at XDP layer turns off netfilters NAT capability, so its incompatible with the classic forwarding path.
If thats fine, why do you need to insert into the conntrack table to begin with? The entire infrastructure its designed for is disabled...
Quoted 6 lines
One of the major selling points of XDP is that you can reuse the existing kernel infrastructure instead of having to roll your own. So sure, one could implement their own conntrack using BPF maps (as indeed, e.g., Cilium has done), but why do that when you can take advantage of the existing one in the kernel? Same reason we have the
bpf_fib_lookup()helper...
Insertion to conntrack via ebpf seems to be bad to me precisely because it bypasses the existing infra.
In the bypass scenario you're envisioning, who is responsible for fastpath-or-not decision?
Quoted 6 lines
Quoted 2 lines
In the HW offload case, conntrack is bypassed completely. There is an IPS_(HW)_OFFLOAD_BIT that prevents the flow from expiring.
That's comparable in execution semantics (stack is bypassed entirely), but not in control plane semantics (we lookup from XDP instead of pushing flows down to an offload).
I'm not following. As soon as you do insertion via XDP existing control plane (*tables ruleset, xfrm and so on) becomes irrelevant.
Say e.g. user has a iptables ruleset that disables conntrack for udp dport 53 to avoid conntrack overhead for local resolver cache.
No longer relevant, ebpf overrides or whatever generates the epbf prog needs to emulate existing config.
Quoted 9 lines
Quoted 6 lines
I suspect we'd want a way to notify/call an ebpf program instead so we can avoid the ctnetlink -> userspace -> update dance and do the XDP 'flow bypass information update' from inside the kernel and ebpf/XDP reimplementation of the nf flow table (it uses the netfilter ingress hook on the configured devices; everyhing it does should be doable from XDP).
But the point is exactly that we don't have to duplicate the state into BPF, we can make XDP look it up directly.
Normally for fast bypass I'd expect that the bypass infra would want to access all info in one lookup, but conntrack only gives you the NAT transformation, so you'll also need a sk lookup and possibly a FIB lookup later to get the route.
Also maybe an xfrm lookup as well if your bypass infra needs to support ipsec.
So I neither understand the need for conntrack lookup (*for fast bypass use case*) nor the need for insert IFF the control plane we have is to be respected.
Re: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
In reply to Kumar Kartikeya DwivediQuoted 7 lines · Kumar Kartikeya Dwivedi
Quoted 6 lines · Maxim Mikityanskiy
The new helpers (
bpf_ct_lookup_tcpandbpf_ct_lookup_udp) allow to query connection tracking information of TCP and UDP connections based on source and destination IP address and port. The helper returns a pointer to structnf_conn(if the conntrack entry was found), which needs to be released withbpf_ct_release.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>The last discussion on this [0] suggested that stable BPF helpers for conntrack were not desired, hence the recent series [1] to extend kfunc support to modules and base the conntrack work on top of it, which I'm working on now (supporting both CT lookup and insert).
If you have conntrack lookup, I can base my solution on top of yours. As it supports modules, it's even better. What is the current status of your work? When do you plan to submit a series? Please add me to Cc when you do.
Thanks for reviewing!
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
Quoted 11 lines · Maxim Mikityanskiy
Quoted 10 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Maxim Mikityanskiy
The new helpers (
bpf_ct_lookup_tcpandbpf_ct_lookup_udp) allow to query connection tracking information of TCP and UDP connections based on source and destination IP address and port. The helper returns a pointer to structnf_conn(if the conntrack entry was found), which needs to be released withbpf_ct_release.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>
Quoted 9 lines
Quoted 4 lines
The last discussion on this [0] suggested that stable BPF helpers for conntrack were not desired, hence the recent series [1] to extend kfunc support to modules and base the conntrack work on top of it, which I'm working on now (supporting both CT lookup and insert).
If you have conntrack lookup, I can base my solution on top of yours. As it supports modules, it's even better. What is the current status of your work? When do you plan to submit a series? Please add me to Cc when you do.
Great, I'll post the lookup stuff separately next week, and Cc you.
Thanks!
Quoted 9 lines
Thanks for reviewing!
--
Kartikeya
Re: [PATCH bpf-next 07/10] bpf: Add helpers to query conntrack info
In reply to Maxim MikityanskiyQuoted 2 lines
+#if
IS_BUILTIN(CONFIG_NF_CONNTRACK)
This makes the helpers all but useless on distro kernels; I don't think this is the right way to go about it. As Kumar mentioned, he's working on an approach using kfuncs in modules; maybe you can collaborate on that?
-Toke
PATCH 8/10 bpf: Add helpers to issue and check SYN cookies in XDP
In reply to Maxim MikityanskiyThe new helpers bpf_tcp_raw_{gen,check}_syncookie allow an XDP program to generate SYN cookies in response to TCP SYN packets and to check those cookies upon receiving the first ACK packet (the final packet of the TCP handshake).
Unlike bpf_tcp_{gen,check}_syncookie these new helpers don't need a listening socket on the local machine, which allows to use them together with synproxy to accelerate SYN cookie generation.
--- include/net/tcp.h | 1 + include/uapi/linux/bpf.h | 57 +++++++++++++++ net/core/filter.c | 122 +++++++++++++++++++++++++++++++++ net/ipv4/tcp_input.c | 3 +- tools/include/uapi/linux/bpf.h | 57 +++++++++++++++ 5 files changed, 239 insertions(+), 1 deletion(-)
include/net/tcp.h +1 -0
@@ -431,6 +431,7 @@ u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph, struct tcphdr *th, u32 *cookie);u16 tcp_v6_get_syncookie (struct sock *sk,struct ipv6hdr *iph,struct tcphdr *th,u32 *cookie); +u16 tcp_parse_mss_option (const struct tcphdr *th,u16 user_mss);u16 tcp_get_syncookie_mss (struct request_sock_ops *rsk_ops,const struct tcp_request_sock_ops *af_ops,struct sock *sk,struct tcphdr *th);
include/uapi/linux/bpf.h +57 -0
69 lines not on this page · raw message
net/core/filter.c +122 -0
134 lines not on this page · raw message
net/ipv4/tcp_input.c +2 -1
@@ -3961,7 +3961,7 @@ static bool smc_parse_options(const struct tcphdr *th,/* Try to parse the MSS option from the TCP header. Return 0 on failure, clamped * value on success. */ -static u16 tcp_parse_mss_option (const struct tcphdr *th,u16 user_mss) +u16 tcp_parse_mss_option (const struct tcphdr *th,u16 user_mss) {const unsigned char *ptr = (const unsigned char *)(th +1 );int length = (th->doff *4 ) -sizeof (struct tcphdr );
@@ -4000,6 +4000,7 @@ static u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss) }return mss; } +EXPORT_SYMBOL_GPL (tcp_parse_mss_option);/* Look for tcp options. Normally only called on SYN and SYNACK packets. * But, this can also be called on packets in the established flow when
tools/include/uapi/linux/bpf.h +57 -0
69 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH 9/10 bpf: Add a helper to issue timestamp cookies in XDP
In reply to Maxim MikityanskiyThe new helper bpf_tcp_raw_gen_tscookie allows an XDP program to generate timestamp cookies (to be used together with SYN cookies) which encode different options set by the client in the SYN packet: SACK support, ECN support, window scale. These options are encoded in lower bits of the timestamp, which will be returned by the client in a subsequent ACK packet. The format is the same used by synproxy.
--- include/net/tcp.h | 1 + include/uapi/linux/bpf.h | 27 +++++++++++++++ net/core/filter.c | 38 +++++++++++++++++++++ net/ipv4/syncookies.c | 60 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 27 +++++++++++++++ 5 files changed, 153 insertions(+)
include/net/tcp.h +1 -0
@@ -564,6 +564,7 @@ u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th, u16 *mssp);__u32 cookie_v4_init_sequence (const struct sk_buff *skb,__u16 *mss);u64 cookie_init_timestamp (struct request_sock *req,u64 now); +bool cookie_init_timestamp_raw (struct tcphdr *th,__be32 *tsval,__be32 *tsecr);bool cookie_timestamp_decode (const struct net *net,struct tcp_options_received *opt); bool cookie_ecn_ok(const struct tcp_options_received *opt,
include/uapi/linux/bpf.h +27 -0
@@ -5053,6 +5053,32 @@ union bpf_attr {* * **-EPROTONOSUPPORT** if the IP version is not 4 or 6 (or 6, but * CONFIG_IPV6 is disabled). +* +* int bpf_tcp_raw_gen_tscookie(struct tcphdr *th, u32 th_len, __be32 *tsopt, u32 tsopt_len) +* Description +* Try to generate a timestamp cookie which encodes some of the +* flags sent by the client in the SYN packet: SACK support, ECN +* support, window scale. To be used with SYN cookies. +* +* *th* points to the start of the TCP header of the client's SYN +* packet, while *th_len* contains the length of the TCP header (at +* least **sizeof**\ (**struct tcphdr**)). +* +* *tsopt* points to the output location where to put the resulting +* timestamp values: tsval and tsecr, in the format of the TCP +* timestamp option. +* +* Return +* On success, 0. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if the input arguments are invalid. +* +* **-ENOENT** if the TCP header doesn't have the timestamp option. +* +* **-EOPNOTSUPP** if the kernel configuration does not enable SYN +* cookies (CONFIG_SYN_COOKIES is off). */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5238,6 +5264,7 @@ union bpf_attr {FN (ct_release), \FN (tcp_raw_gen_syncookie), \FN (tcp_raw_check_syncookie), \ +FN (tcp_raw_gen_tscookie), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
net/core/filter.c +38 -0
@@ -7403,6 +7403,42 @@ static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_proto = { .arg4_type = ARG_CONST_SIZE, }; +BPF_CALL_4(bpf_tcp_raw_gen_tscookie, struct tcphdr *, th, u32, th_len, + __be32 *, tsopt, u32, tsopt_len) +{ +int err; + +#ifdef CONFIG_SYN_COOKIES +if (tsopt_len !=sizeof (u64)) { + err = -EINVAL ; + goto err_out; + } + +if (!cookie_init_timestamp_raw (th, &tsopt[0 ], &tsopt[1 ])) { + err = -ENOENT ; + goto err_out; + } + +return 0 ; +err_out: +#else + err = -EOPNOTSUPP ; +#endif +memset (tsopt,0 , tsopt_len); + return err; +} + +static const struct bpf_func_proto bpf_tcp_raw_gen_tscookie_proto = { + .func = bpf_tcp_raw_gen_tscookie, + .gpl_only = false, + .pkt_access = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_MEM , + .arg2_type =ARG_CONST_SIZE , + .arg3_type =ARG_PTR_TO_UNINIT_MEM , + .arg4_type =ARG_CONST_SIZE , +}; +#endif /* CONFIG_INET */ bool bpf_helper_changes_pkt_data (void *func)
@@ -7825,6 +7861,8 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_tcp_raw_gen_syncookie_proto;case BPF_FUNC_tcp_raw_check_syncookie:return &bpf_tcp_raw_check_syncookie_proto; +case BPF_FUNC_tcp_raw_gen_tscookie: +return &bpf_tcp_raw_gen_tscookie_proto;#endif default :return bpf_sk_base_func_proto (func_id);
net/ipv4/syncookies.c +60 -0
66 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +27 -0
@@ -5053,6 +5053,32 @@ union bpf_attr {* * **-EPROTONOSUPPORT** if the IP version is not 4 or 6 (or 6, but * CONFIG_IPV6 is disabled). +* +* int bpf_tcp_raw_gen_tscookie(struct tcphdr *th, u32 th_len, __be32 *tsopt, u32 tsopt_len) +* Description +* Try to generate a timestamp cookie which encodes some of the +* flags sent by the client in the SYN packet: SACK support, ECN +* support, window scale. To be used with SYN cookies. +* +* *th* points to the start of the TCP header of the client's SYN +* packet, while *th_len* contains the length of the TCP header (at +* least **sizeof**\ (**struct tcphdr**)). +* +* *tsopt* points to the output location where to put the resulting +* timestamp values: tsval and tsecr, in the format of the TCP +* timestamp option. +* +* Return +* On success, 0. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if the input arguments are invalid. +* +* **-ENOENT** if the TCP header doesn't have the timestamp option. +* +* **-EOPNOTSUPP** if the kernel configuration does not enable SYN +* cookies (CONFIG_SYN_COOKIES is off). */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5238,6 +5264,7 @@ union bpf_attr {FN (ct_release), \FN (tcp_raw_gen_syncookie), \FN (tcp_raw_check_syncookie), \ +FN (tcp_raw_gen_tscookie), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.30.2
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 160 lines · Maxim Mikityanskiy
The new helper
bpf_tcp_raw_gen_tscookieallows an XDP program to generate timestamp cookies (to be used together with SYN cookies) which encode different options set by the client in the SYN packet: SACK support, ECN support, window scale. These options are encoded in lower bits of the timestamp, which will be returned by the client in a subsequent ACK packet. The format is the same used by synproxy.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>--- include/net/tcp.h | 1 + include/uapi/linux/bpf.h | 27 +++++++++++++++ net/core/filter.c | 38 +++++++++++++++++++++ net/ipv4/syncookies.c | 60 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 27 +++++++++++++++ 5 files changed, 153 insertions(+)include/net/tcp.h +1
-0@@ -564,6 +564,7 @@ u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th, u16 *mssp);__u32 cookie_v4_init_sequence (const struct sk_buff *skb,__u16 *mss);u64 cookie_init_timestamp (struct request_sock *req,u64 now); +bool cookie_init_timestamp_raw (struct tcphdr *th,__be32 *tsval,__be32 *tsecr);bool cookie_timestamp_decode (const struct net *net,struct tcp_options_received *opt); bool cookie_ecn_ok(const struct tcp_options_received *opt,include/uapi/linux/bpf.h +27
-0@@ -5053,6 +5053,32 @@ union bpf_attr {* * **-EPROTONOSUPPORT** if the IP version is not 4 or 6 (or 6, but * CONFIG_IPV6 is disabled). +* +* int bpf_tcp_raw_gen_tscookie(struct tcphdr *th, u32 th_len, __be32 *tsopt, u32 tsopt_len) +* Description +* Try to generate a timestamp cookie which encodes some of the +* flags sent by the client in the SYN packet: SACK support, ECN +* support, window scale. To be used with SYN cookies. +* +* *th* points to the start of the TCP header of the client's SYN +* packet, while *th_len* contains the length of the TCP header (at +* least **sizeof**\ (**struct tcphdr**)). +* +* *tsopt* points to the output location where to put the resulting +* timestamp values: tsval and tsecr, in the format of the TCP +* timestamp option. +* +* Return +* On success, 0. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if the input arguments are invalid. +* +* **-ENOENT** if the TCP header doesn't have the timestamp option. +* +* **-EOPNOTSUPP** if the kernel configuration does not enable SYN +* cookies (CONFIG_SYN_COOKIES is off). */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -5238,6 +5264,7 @@ union bpf_attr {FN (ct_release), \FN (tcp_raw_gen_syncookie), \FN (tcp_raw_check_syncookie), \ +FN (tcp_raw_gen_tscookie), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper net/core/filter.c +38
-0@@ -7403,6 +7403,42 @@ static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_proto = { .arg4_type = ARG_CONST_SIZE, }; +BPF_CALL_4(bpf_tcp_raw_gen_tscookie, struct tcphdr *, th, u32, th_len, + __be32 *, tsopt, u32, tsopt_len) +{ +int err; + +#ifdef CONFIG_SYN_COOKIES +if (tsopt_len !=sizeof (u64)) { + err = -EINVAL ; + goto err_out; + } + +if (!cookie_init_timestamp_raw (th, &tsopt[0 ], &tsopt[1 ])) { + err = -ENOENT ; + goto err_out; + } + +return 0 ; +err_out: +#else + err = -EOPNOTSUPP ; +#endif +memset (tsopt,0 , tsopt_len); + return err; +} + +static const struct bpf_func_proto bpf_tcp_raw_gen_tscookie_proto = { + .func = bpf_tcp_raw_gen_tscookie, + .gpl_only = false, + .pkt_access = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_MEM , + .arg2_type =ARG_CONST_SIZE , + .arg3_type =ARG_PTR_TO_UNINIT_MEM , + .arg4_type =ARG_CONST_SIZE , +}; +#endif /* CONFIG_INET */ bool bpf_helper_changes_pkt_data (void *func)@@ -7825,6 +7861,8 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_tcp_raw_gen_syncookie_proto;case BPF_FUNC_tcp_raw_check_syncookie:return &bpf_tcp_raw_check_syncookie_proto; +case BPF_FUNC_tcp_raw_gen_tscookie: +return &bpf_tcp_raw_gen_tscookie_proto;#endif default :return bpf_sk_base_func_proto (func_id);net/ipv4/syncookies.c +22
-0@@ -85,6 +85,66 @@ u64 cookie_init_timestamp(struct request_sock *req, u64 now)return (u64 )ts * (NSEC_PER_SEC /TCP_TS_HZ ); } +bool cookie_init_timestamp_raw (struct tcphdr *th,__be32 *tsval,__be32 *tsecr) +{ +int length = (th->doff *4 ) -sizeof (*th); +u8 wscale =TS_OPT_WSCALE_MASK ; +bool option_timestamp = false; +bool option_sack = false; +u32 cookie; +u8 *ptr; + + ptr = (u8 *)(th +1 ); + +while (length >0 ) { +u8 opcode = *ptr++; +u8 opsize; + +if (opcode ==TCPOPT_EOL ) +break ; +if (opcode ==TCPOPT_NOP ) { + length--; +continue ; + } +
net/ipv4/syncookies.c u64 cookie_init_timestamp(struct request_sock *req, u64 now) · line 119
Quoted 10 lines
net/ipv4/syncookies.c +10
-0+if (length <2 ) +break ; + opsize = *ptr++; +if (opsize <2 ) +break ; +if (opsize > length) +break ; + +switch (opcode) { +case TCPOPT_WINDOW :
You must check osize.
net/ipv4/syncookies.c u64 cookie_init_timestamp(struct request_sock *req, u64 now) · line 122
Quoted 3 lines
net/ipv4/syncookies.c +3
-0+ wscale =min_t (u8, *ptr,TCP_MAX_WSCALE ); +break ; +case TCPOPT_TIMESTAMP :
You must check opsize.
net/ipv4/syncookies.c u64 cookie_init_timestamp(struct request_sock *req, u64 now) · line 125
Quoted 3 lines
net/ipv4/syncookies.c +3
-0+ option_timestamp = true; +/* Client's tsval becomes our tsecr. */ + *tsecr =cpu_to_be32 (get_unaligned_be32 (ptr));
Please avoid useless ntohl/htonl dance (even if compiler probably optimizes this) No need to obfuscate :)
*tsecr = get_unaligned((__be32 *)ptr);
Quoted 71 lines
net/ipv4/syncookies.c +22
-0+break ; +case TCPOPT_SACK_PERM : + option_sack = true; +break ; + } + + ptr += opsize -2 ; + length -= opsize; + } + +if (!option_timestamp) +return false; + + cookie =tcp_time_stamp_raw () & ~TSMASK ; + cookie |= wscale &TS_OPT_WSCALE_MASK ; +if (option_sack) + cookie |=TS_OPT_SACK ; +if (th->ece && th->cwr) + cookie |=TS_OPT_ECN ; + *tsval =cpu_to_be32 (cookie); +return true; +}static __u32 secure_tcp_syn_cookie (__be32 saddr,__be32 daddr,__be16 sport,__be16 dport,__u32 sseq,__u32 data)tools/include/uapi/linux/bpf.h +27
-0@@ -5053,6 +5053,32 @@ union bpf_attr {* * **-EPROTONOSUPPORT** if the IP version is not 4 or 6 (or 6, but * CONFIG_IPV6 is disabled). +* +* int bpf_tcp_raw_gen_tscookie(struct tcphdr *th, u32 th_len, __be32 *tsopt, u32 tsopt_len) +* Description +* Try to generate a timestamp cookie which encodes some of the +* flags sent by the client in the SYN packet: SACK support, ECN +* support, window scale. To be used with SYN cookies. +* +* *th* points to the start of the TCP header of the client's SYN +* packet, while *th_len* contains the length of the TCP header (at +* least **sizeof**\ (**struct tcphdr**)). +* +* *tsopt* points to the output location where to put the resulting +* timestamp values: tsval and tsecr, in the format of the TCP +* timestamp option. +* +* Return +* On success, 0. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if the input arguments are invalid. +* +* **-ENOENT** if the TCP header doesn't have the timestamp option. +* +* **-EOPNOTSUPP** if the kernel configuration does not enable SYN +* cookies (CONFIG_SYN_COOKIES is off). */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -5238,6 +5264,7 @@ union bpf_attr {FN (ct_release), \FN (tcp_raw_gen_syncookie), \FN (tcp_raw_check_syncookie), \ +FN (tcp_raw_gen_tscookie), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 176 lines · Eric Dumazet
Quoted 160 lines · Maxim Mikityanskiy
The new helper
bpf_tcp_raw_gen_tscookieallows an XDP program to generate timestamp cookies (to be used together with SYN cookies) which encode different options set by the client in the SYN packet: SACK support, ECN support, window scale. These options are encoded in lower bits of the timestamp, which will be returned by the client in a subsequent ACK packet. The format is the same used by synproxy.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>--- include/net/tcp.h | 1 + include/uapi/linux/bpf.h | 27 +++++++++++++++ net/core/filter.c | 38 +++++++++++++++++++++ net/ipv4/syncookies.c | 60 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 27 +++++++++++++++ 5 files changed, 153 insertions(+)include/net/tcp.h +1
-0@@ -564,6 +564,7 @@ u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th, u16 *mssp);__u32 cookie_v4_init_sequence (const struct sk_buff *skb,__u16 *mss);u64 cookie_init_timestamp (struct request_sock *req,u64 now); +bool cookie_init_timestamp_raw (struct tcphdr *th,__be32 *tsval,__be32 *tsecr);bool cookie_timestamp_decode (const struct net *net,struct tcp_options_received *opt); bool cookie_ecn_ok(const struct tcp_options_received *opt,include/uapi/linux/bpf.h +27
-0@@ -5053,6 +5053,32 @@ union bpf_attr {* * **-EPROTONOSUPPORT** if the IP version is not 4 or 6 (or 6, but * CONFIG_IPV6 is disabled). +* +* int bpf_tcp_raw_gen_tscookie(struct tcphdr *th, u32 th_len, __be32 *tsopt, u32 tsopt_len) +* Description +* Try to generate a timestamp cookie which encodes some of the +* flags sent by the client in the SYN packet: SACK support, ECN +* support, window scale. To be used with SYN cookies. +* +* *th* points to the start of the TCP header of the client's SYN +* packet, while *th_len* contains the length of the TCP header (at +* least **sizeof**\ (**struct tcphdr**)). +* +* *tsopt* points to the output location where to put the resulting +* timestamp values: tsval and tsecr, in the format of the TCP +* timestamp option. +* +* Return +* On success, 0. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if the input arguments are invalid. +* +* **-ENOENT** if the TCP header doesn't have the timestamp option. +* +* **-EOPNOTSUPP** if the kernel configuration does not enable SYN +* cookies (CONFIG_SYN_COOKIES is off). */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -5238,6 +5264,7 @@ union bpf_attr {FN (ct_release), \FN (tcp_raw_gen_syncookie), \FN (tcp_raw_check_syncookie), \ +FN (tcp_raw_gen_tscookie), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper net/core/filter.c +38
-0@@ -7403,6 +7403,42 @@ static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_proto = { .arg4_type = ARG_CONST_SIZE, }; +BPF_CALL_4(bpf_tcp_raw_gen_tscookie, struct tcphdr *, th, u32, th_len, + __be32 *, tsopt, u32, tsopt_len) +{ +int err; + +#ifdef CONFIG_SYN_COOKIES +if (tsopt_len !=sizeof (u64)) { + err = -EINVAL ; + goto err_out; + } + +if (!cookie_init_timestamp_raw (th, &tsopt[0 ], &tsopt[1 ])) { + err = -ENOENT ; + goto err_out; + } + +return 0 ; +err_out: +#else + err = -EOPNOTSUPP ; +#endif +memset (tsopt,0 , tsopt_len); + return err; +} + +static const struct bpf_func_proto bpf_tcp_raw_gen_tscookie_proto = { + .func = bpf_tcp_raw_gen_tscookie, + .gpl_only = false, + .pkt_access = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_MEM , + .arg2_type =ARG_CONST_SIZE , + .arg3_type =ARG_PTR_TO_UNINIT_MEM , + .arg4_type =ARG_CONST_SIZE , +}; +#endif /* CONFIG_INET */ bool bpf_helper_changes_pkt_data (void *func)@@ -7825,6 +7861,8 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_tcp_raw_gen_syncookie_proto;case BPF_FUNC_tcp_raw_check_syncookie:return &bpf_tcp_raw_check_syncookie_proto; +case BPF_FUNC_tcp_raw_gen_tscookie: +return &bpf_tcp_raw_gen_tscookie_proto;#endif default :return bpf_sk_base_func_proto (func_id);net/ipv4/syncookies.c +22
-0@@ -85,6 +85,66 @@ u64 cookie_init_timestamp(struct request_sock *req, u64 now)return (u64 )ts * (NSEC_PER_SEC /TCP_TS_HZ ); } +bool cookie_init_timestamp_raw (struct tcphdr *th,__be32 *tsval,__be32 *tsecr) +{ +int length = (th->doff *4 ) -sizeof (*th); +u8 wscale =TS_OPT_WSCALE_MASK ; +bool option_timestamp = false; +bool option_sack = false; +u32 cookie; +u8 *ptr; + + ptr = (u8 *)(th +1 ); + +while (length >0 ) { +u8 opcode = *ptr++; +u8 opsize; + +if (opcode ==TCPOPT_EOL ) +break ; +if (opcode ==TCPOPT_NOP ) { + length--; +continue ; + } +net/ipv4/syncookies.c u64 cookie_init_timestamp(struct request_sock *req, u64 now) · line 119
Quoted 10 lines
net/ipv4/syncookies.c +10
-0+if (length <2 ) +break ; + opsize = *ptr++; +if (opsize <2 ) +break ; +if (opsize > length) +break ; + +switch (opcode) { +case TCPOPT_WINDOW :You must check osize.
Quoted 5 lines
net/ipv4/syncookies.c u64 cookie_init_timestamp(struct request_sock *req, u64 now) · line 122
Quoted 3 lines
net/ipv4/syncookies.c +3
-0+ wscale =min_t (u8, *ptr,TCP_MAX_WSCALE ); +break ; +case TCPOPT_TIMESTAMP :You must check opsize.
Ack.
Quoted 6 lines
net/ipv4/syncookies.c u64 cookie_init_timestamp(struct request_sock *req, u64 now) · line 125
Quoted 3 lines
net/ipv4/syncookies.c +3
-0+ option_timestamp = true; +/* Client's tsval becomes our tsecr. */ + *tsecr =cpu_to_be32 (get_unaligned_be32 (ptr));Please avoid useless ntohl/htonl dance (even if compiler probably optimizes this) No need to obfuscate :)
No obfuscation intended - I thought I was clearer this way. I can change it.
Thanks for reviewing!
Quoted 74 lines
*tsecr = get_unaligned((__be32 *)ptr);Quoted 71 lines
net/ipv4/syncookies.c +22
-0+break ; +case TCPOPT_SACK_PERM : + option_sack = true; +break ; + } + + ptr += opsize -2 ; + length -= opsize; + } + +if (!option_timestamp) +return false; + + cookie =tcp_time_stamp_raw () & ~TSMASK ; + cookie |= wscale &TS_OPT_WSCALE_MASK ; +if (option_sack) + cookie |=TS_OPT_SACK ; +if (th->ece && th->cwr) + cookie |=TS_OPT_ECN ; + *tsval =cpu_to_be32 (cookie); +return true; +}static __u32 secure_tcp_syn_cookie (__be32 saddr,__be32 daddr,__be16 sport,__be16 dport,__u32 sseq,__u32 data)tools/include/uapi/linux/bpf.h +27
-0@@ -5053,6 +5053,32 @@ union bpf_attr {* * **-EPROTONOSUPPORT** if the IP version is not 4 or 6 (or 6, but * CONFIG_IPV6 is disabled). +* +* int bpf_tcp_raw_gen_tscookie(struct tcphdr *th, u32 th_len, __be32 *tsopt, u32 tsopt_len) +* Description +* Try to generate a timestamp cookie which encodes some of the +* flags sent by the client in the SYN packet: SACK support, ECN +* support, window scale. To be used with SYN cookies. +* +* *th* points to the start of the TCP header of the client's SYN +* packet, while *th_len* contains the length of the TCP header (at +* least **sizeof**\ (**struct tcphdr**)). +* +* *tsopt* points to the output location where to put the resulting +* timestamp values: tsval and tsecr, in the format of the TCP +* timestamp option. +* +* Return +* On success, 0. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if the input arguments are invalid. +* +* **-ENOENT** if the TCP header doesn't have the timestamp option. +* +* **-EOPNOTSUPP** if the kernel configuration does not enable SYN +* cookies (CONFIG_SYN_COOKIES is off). */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -5238,6 +5264,7 @@ union bpf_attr {FN (ct_release), \FN (tcp_raw_gen_syncookie), \FN (tcp_raw_check_syncookie), \ +FN (tcp_raw_gen_tscookie), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
In reply to Maxim MikityanskiyQuoted 35 lines · Maxim Mikityanskiy
The new helper
bpf_tcp_raw_gen_tscookieallows an XDP program to generate timestamp cookies (to be used together with SYN cookies) which encode different options set by the client in the SYN packet: SACK support, ECN support, window scale. These options are encoded in lower bits of the timestamp, which will be returned by the client in a subsequent ACK packet. The format is the same used by synproxy.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>--- include/net/tcp.h | 1 + include/uapi/linux/bpf.h | 27 +++++++++++++++ net/core/filter.c | 38 +++++++++++++++++++++ net/ipv4/syncookies.c | 60 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 27 +++++++++++++++ 5 files changed, 153 insertions(+)include/net/tcp.h +1
-0@@ -564,6 +564,7 @@ u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th, u16 *mssp);__u32 cookie_v4_init_sequence (const struct sk_buff *skb,__u16 *mss);u64 cookie_init_timestamp (struct request_sock *req,u64 now); +bool cookie_init_timestamp_raw (struct tcphdr *th,__be32 *tsval,__be32 *tsecr);bool cookie_timestamp_decode (const struct net *net,struct tcp_options_received *opt); bool cookie_ecn_ok(const struct tcp_options_received *opt,include/uapi/linux/bpf.h +0
-0
include/uapi/linux/bpf.h union bpf_attr { · line 5057
Quoted 5 lines
include/uapi/linux/bpf.h +2
-0@@ -5053,6 +5053,32 @@ union bpf_attr {* * **-EPROTONOSUPPORT** if the IP version is not 4 or 6 (or 6, but * CONFIG_IPV6 is disabled). +* +* int bpf_tcp_raw_gen_tscookie(struct tcphdr *th, u32 th_len, __be32 *tsopt, u32 tsopt_len)
flags which must be 0?
Quoted 40 lines
include/uapi/linux/bpf.h +25
-0+* Description +* Try to generate a timestamp cookie which encodes some of the +* flags sent by the client in the SYN packet: SACK support, ECN +* support, window scale. To be used with SYN cookies. +* +* *th* points to the start of the TCP header of the client's SYN +* packet, while *th_len* contains the length of the TCP header (at +* least **sizeof**\ (**struct tcphdr**)). +* +* *tsopt* points to the output location where to put the resulting +* timestamp values: tsval and tsecr, in the format of the TCP +* timestamp option. +* +* Return +* On success, 0. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if the input arguments are invalid. +* +* **-ENOENT** if the TCP header doesn't have the timestamp option. +* +* **-EOPNOTSUPP** if the kernel configuration does not enable SYN +* cookies (CONFIG_SYN_COOKIES is off). */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -5238,6 +5264,7 @@ union bpf_attr {FN (ct_release), \FN (tcp_raw_gen_syncookie), \FN (tcp_raw_check_syncookie), \ +FN (tcp_raw_gen_tscookie), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper net/core/filter.c +0
-0
net/core/filter.c static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_proto = { · line 7409
Quoted 7 lines
net/core/filter.c +4
-0@@ -7403,6 +7403,42 @@ static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_proto = { .arg4_type = ARG_CONST_SIZE, }; +BPF_CALL_4(bpf_tcp_raw_gen_tscookie, struct tcphdr *, th, u32, th_len, + __be32 *, tsopt, u32, tsopt_len) +{ +int err;
Missing check for th_len?
net/core/filter.c static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_proto = { · line 7412
Quoted 3 lines
net/core/filter.c +3
-0+ +#ifdef CONFIG_SYN_COOKIES +if (tsopt_len !=sizeof (u64)) {
sizeof(u32) * 2? That u64 isn't really relevant here.
Quoted 46 lines
net/core/filter.c +31
-0+ err = -EINVAL ; + goto err_out; + } + +if (!cookie_init_timestamp_raw (th, &tsopt[0 ], &tsopt[1 ])) { + err = -ENOENT ; + goto err_out; + } + +return 0 ; +err_out: +#else + err = -EOPNOTSUPP ; +#endif +memset (tsopt,0 , tsopt_len); +return err; +} + +static const struct bpf_func_proto bpf_tcp_raw_gen_tscookie_proto = { + .func = bpf_tcp_raw_gen_tscookie, + .gpl_only = false, + .pkt_access = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_MEM , + .arg2_type =ARG_CONST_SIZE , + .arg3_type =ARG_PTR_TO_UNINIT_MEM , + .arg4_type =ARG_CONST_SIZE , +}; +#endif /* CONFIG_INET */ bool bpf_helper_changes_pkt_data (void *func)@@ -7825,6 +7861,8 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_tcp_raw_gen_syncookie_proto;case BPF_FUNC_tcp_raw_check_syncookie:return &bpf_tcp_raw_check_syncookie_proto; +case BPF_FUNC_tcp_raw_gen_tscookie: +return &bpf_tcp_raw_gen_tscookie_proto;#endif default :return bpf_sk_base_func_proto (func_id);net/ipv4/syncookies.c +0
-0
net/ipv4/syncookies.c u64 cookie_init_timestamp(struct request_sock *req, u64 now) · line 88
Quoted 4 lines
net/ipv4/syncookies.c +1
-0@@ -85,6 +85,66 @@ u64 cookie_init_timestamp(struct request_sock *req, u64 now)return (u64 )ts * (NSEC_PER_SEC /TCP_TS_HZ ); } +bool cookie_init_timestamp_raw (struct tcphdr *th,__be32 *tsval,__be32 *tsecr)
I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
Lorenz
--
Lorenz Bauer | Systems Engineer
6th Floor, County Hall/The Riverside Building, SE1 7PB, UK
www.cloudflare.com
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 4 lines · Lorenz Bauer
Quoted 1 line
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
-Toke
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 10 lines · Toke Høiland-Jørgensen
Quoted 4 lines · Lorenz Bauer
Quoted 1 line
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
tcp_time_stamp_raw() uses ktime_get_ns(), while bpf_ktime_get_ns() uses ktime_get_mono_fast_ns(). Is it fine to use ktime_get_mono_fast_ns() instead of ktime_get_ns()? I'm a bit worried about this note in Documentation/core-api/timekeeping.rst:
Quoted 2 lines
most drivers should never call them,
since the time is allowed to jump under certain conditions.
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 8 lines · Maxim Mikityanskiy
tcp_time_stamp_raw()usesktime_get_ns(), whilebpf_ktime_get_ns()usesktime_get_mono_fast_ns(). Is it fine to usektime_get_mono_fast_ns()instead ofktime_get_ns()? I'm a bit worried about this note inDocumentation/core-api/timekeeping.rst:Quoted 2 lines
most drivers should never call them,
since the time is allowed to jump under certain conditions.
That depends on what happens when the timestamp is "off". Since you're sending this value over the network I doubt that the two methods will show a difference.
Lorenz
Signature · 4 lines
Lorenz Bauer | Systems Engineer 6th Floor, County Hall/The Riverside Building, SE1 7PB, UK www.cloudflare.com
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
In reply to Toke Høiland-JørgensenQuoted 10 lines · Toke Høiland-Jørgensen
Quoted 4 lines · Lorenz Bauer
Quoted 1 line
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
After trying to put this code into BPF (replacing the underlying ktime_get_ns with ktime_get_mono_fast_ns), I experienced issues with passing the verifier.
In addition to comparing ptr to end, I had to add checks that compare ptr to data_end, because the verifier can't deduce that end <= data_end. More branches will add a certain slowdown (not measured).
A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is 10. With more iterations, I have the following error:
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000)
max_states_per_insn 29 total_states 35489 peak_states 596 mark_read 45
I assume that BPF_COMPLEXITY_LIMIT_INSNS (1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.
The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using bpf_load_hdr_opt and calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?
One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal? Commit c04c0d2b968a ("bpf: increase complexity limit and maximum program size") says it shouldn't take more than one second in any case.
Thanks,
Max
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 21 lines · Maxim Mikityanskiy
Quoted 11 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Lorenz Bauer
Quoted 2 lines
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
After trying to put this code into BPF (replacing the underlying
ktime_get_nswithktime_get_mono_fast_ns), I experienced issues with passing the verifier.In addition to comparing ptr to end, I had to add checks that compare ptr to
data_end, because the verifier can't deduce that end <=data_end. More branches will add a certain slowdown (not measured).A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is 10. With more iterations, I have the following error:
BPF program is too large. Processed 1000001 insn
Quoted 8 lines
processed 1000001 insns (limit 1000000)
max_states_per_insn29total_states35489peak_states596mark_read45I assume that
BPF_COMPLEXITY_LIMIT_INSNS(1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using
bpf_load_hdr_optand calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal?
Maxim, this may expose a verifier bug. Do you have a reproducer I can access? I would like to debug this to see what is the root case. Thanks!
Quoted 4 lines
Commit
c04c0d2b968a("bpf: increase complexity limit and maximum program size") says it shouldn't take more than one second in any case.Thanks,
Max
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 26 lines · Yonghong Song
Quoted 23 lines · Maxim Mikityanskiy
Quoted 11 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Lorenz Bauer
Quoted 2 lines
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
After trying to put this code into BPF (replacing the underlying
ktime_get_nswithktime_get_mono_fast_ns), I experienced issues with passing the verifier.In addition to comparing ptr to end, I had to add checks that compare ptr to
data_end, because the verifier can't deduce that end <=data_end. More branches will add a certain slowdown (not measured).A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is 10. With more iterations, I have the following error:
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000)
max_states_per_insn29total_states35489peak_states596mark_read45
Quoted 7 lines
Quoted 5 lines
I assume that
BPF_COMPLEXITY_LIMIT_INSNS(1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using
bpf_load_hdr_optand calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal?
Maxim, this may expose a verifier bug. Do you have a reproducer I can access? I would like to debug this to see what is the root case. Thanks!
Thanks, I appreciate your help in debugging it. The reproducer is based on the modified XDP program from patch 10 in this series. You'll need to apply at least patches 6, 7, 8 from this series to get new BPF helpers needed for the XDP program (tell me if that's a problem, I can try to remove usage of new helpers, but it will affect the program length and may produce different results in the verifier).
See the C code of the program that passes the verifier (compiled with clang version 12.0.0-1ubuntu1) in the bottom of this email. If you increase the loop boundary from 10 to at least 11 in cookie_init_timestamp_raw(), it fails the verifier after a few minutes. If you apply this tiny change, it fails the verifier after about 3 hours:
samples/bpf/syncookie_kern.c +2 -1
@@ -167,6 +167,7 @@ static __always_inline bool cookie_init_for (i =0 ; i <10 ; i++) {u8 opcode, opsize; +skip_nop:if (ptr >= end)break ;if (ptr +1 > data_end)
@@ -178,7 +179,7 @@ static __always_inline bool cookie_init_break ;if (opcode ==TCPOPT_NOP ) { ++ptr; -continue ; + goto skip_nop; }if (ptr +1 >= end)
--cut--
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/* Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
#include <stdbool.h>
#include <stddef.h>
#include <uapi/linux/errno.h>
#include <uapi/linux/bpf.h>
#include <uapi/linux/pkt_cls.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/in.h>
#include <uapi/linux/ip.h>
#include <uapi/linux/ipv6.h>
#include <uapi/linux/tcp.h>
#include <uapi/linux/netfilter/nf_conntrack_common.h> #include <linux/minmax.h>
#include <vdso/time64.h>
#include <asm/unaligned.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#define DEFAULT_MSS4 1460
#define DEFAULT_MSS6 1440
#define DEFAULT_WSCALE 7
#define DEFAULT_TTL 64
#define MAX_ALLOWED_PORTS 8
struct bpf_map_def SEC("maps") values = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(__u32),
.value_size = sizeof(__u64),
.max_entries = 2,
};struct bpf_map_def SEC("maps") allowed_ports = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(__u32),
.value_size = sizeof(__u16),
.max_entries = MAX_ALLOWED_PORTS,
};#define IP_DF 0x4000
#define IP_MF 0x2000
#define IP_OFFSET 0x1fff
#define NEXTHDR_TCP 6
#define TCPOPT_NOP 1
#define TCPOPT_EOL 0
#define TCPOPT_MSS 2
#define TCPOPT_WINDOW 3
#define TCPOPT_SACK_PERM 4
#define TCPOPT_TIMESTAMP 8
#define TCPOLEN_MSS 4
#define TCPOLEN_WINDOW 3
#define TCPOLEN_SACK_PERM 2
#define TCPOLEN_TIMESTAMP 10
#define TCP_MAX_WSCALE 14U
#define TS_OPT_WSCALE_MASK 0xf
#define TS_OPT_SACK BIT(4)
#define TS_OPT_ECN BIT(5)
#define TSBITS 6
#define TSMASK (((__u32)1 << TSBITS) - 1)
#define TCP_TS_HZ 1000
#define IPV4_MAXLEN 60
#define TCP_MAXLEN 60
static __always_inline void swap_eth_addr(__u8 *a, __u8 *b)
{
__u8 tmp[ETH_ALEN];
__builtin_memcpy(tmp, a, ETH_ALEN);
__builtin_memcpy(a, b, ETH_ALEN);
__builtin_memcpy(b, tmp, ETH_ALEN);
}static __always_inline __u16 csum_fold(__u32 csum)
{
csum = (csum & 0xffff) + (csum >> 16);
csum = (csum & 0xffff) + (csum >> 16);
return (__u16)~csum;
}static __always_inline __u16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
__u32 len, __u8 proto,
__u32 csum)
{
__u64 s = csum;
s += (__u32)saddr;
s += (__u32)daddr;
#if defined(__BIG_ENDIAN__)
s += proto + len;
#elif defined(__LITTLE_ENDIAN__)
s += (proto + len) << 8;
#else
#error Unknown endian
#endif
s = (s & 0xffffffff) + (s >> 32);
s = (s & 0xffffffff) + (s >> 32);
return csum_fold((__u32)s);
}static __always_inline __u16 csum_ipv6_magic(const struct in6_addr *saddr,
const struct in6_addr *daddr,
__u32 len, __u8 proto, __u32 csum)
{
__u64 sum = csum;
int i;#pragma unroll
for (i = 0; i < 4; i++) sum += (__u32)saddr->s6_addr32[i];
#pragma unroll
for (i = 0; i < 4; i++) sum += (__u32)daddr->s6_addr32[i]; // Don't combine additions to avoid 32-bit overflow. sum += bpf_htonl(len); sum += bpf_htonl(proto); sum = (sum & 0xffffffff) + (sum >> 32); sum = (sum & 0xffffffff) + (sum >> 32); return csum_fold((__u32)sum); }
static __always_inline u64 tcp_clock_ns(void)
{
return bpf_ktime_get_ns();
}static __always_inline __u32 tcp_ns_to_ts(__u64 ns)
{
return div_u64(ns, NSEC_PER_SEC / TCP_TS_HZ);
}static __always_inline __u32 tcp_time_stamp_raw(void)
{
return tcp_ns_to_ts(tcp_clock_ns());
}static __always_inline bool cookie_init_timestamp_raw(struct tcphdr *tcp_header,
__u16 tcp_len,
__be32 *tsval,
__be32 *tsecr,
void *data_end)
{
u8 wscale = TS_OPT_WSCALE_MASK;
bool option_timestamp = false;
bool option_sack = false;
u8 *ptr, *end;
u32 cookie;
int i;
ptr = (u8 *)(tcp_header + 1);
end = (u8 *)tcp_header + tcp_len;
for (i = 0; i < 10; i++) {
u8 opcode, opsize;
if (ptr >= end)
break;
if (ptr + 1 > data_end)
return false;
opcode = ptr[0];
if (opcode == TCPOPT_EOL)
break;
if (opcode == TCPOPT_NOP) {
++ptr;
continue;
}
if (ptr + 1 >= end)
break;
if (ptr + 2 > data_end)
return false;
opsize = ptr[1];
if (opsize < 2)
break;
if (ptr + opsize > end)
break;
switch (opcode) {
case TCPOPT_WINDOW:
if (opsize == TCPOLEN_WINDOW) {
if (ptr + TCPOLEN_WINDOW > data_end)
return false;
wscale = min_t(u8, ptr[2], TCP_MAX_WSCALE);
}
break;
case TCPOPT_TIMESTAMP:
if (opsize == TCPOLEN_TIMESTAMP) {
if (ptr + TCPOLEN_TIMESTAMP > data_end)
return false;
option_timestamp = true;
/* Client's tsval becomes our tsecr. */
*tsecr = get_unaligned((__be32 *)(ptr + 2));
}
break;
case TCPOPT_SACK_PERM:
if (opsize == TCPOLEN_SACK_PERM)
option_sack = true;
break;
}
ptr += opsize;
}
if (!option_timestamp)
return false;
cookie = tcp_time_stamp_raw() & ~TSMASK;
cookie |= wscale & TS_OPT_WSCALE_MASK;
if (option_sack)
cookie |= TS_OPT_SACK;
if (tcp_header->ece && tcp_header->cwr)
cookie |= TS_OPT_ECN;
*tsval = cpu_to_be32(cookie);
return true;
}static __always_inline void values_get_tcpipopts(__u16 *mss, __u8 *wscale,
__u8 *ttl, bool ipv6)
{
__u32 key = 0;
__u64 *value;
value = bpf_map_lookup_elem(&values, &key);
if (value && *value != 0) {
if (ipv6)
*mss = (*value >> 32) & 0xffff;
else
*mss = *value & 0xffff;
*wscale = (*value >> 16) & 0xf;
*ttl = (*value >> 24) & 0xff;
return;
}
*mss = ipv6 ? DEFAULT_MSS6 : DEFAULT_MSS4;
*wscale = DEFAULT_WSCALE;
*ttl = DEFAULT_TTL;
}static __always_inline void values_inc_synacks(void)
{
__u32 key = 1;
__u32 *value;
value = bpf_map_lookup_elem(&values, &key);
if (value)
__sync_fetch_and_add(value, 1);
}static __always_inline bool check_port_allowed(__u16 port)
{
__u32 i;
for (i = 0; i < MAX_ALLOWED_PORTS; i++) {
__u32 key = i;
__u16 *value;
value = bpf_map_lookup_elem(&allowed_ports, &key);
if (!value)
break;
// 0 is a terminator value. Check it first to avoid matching on
// a forbidden port == 0 and returning true.
if (*value == 0)
break;
if (*value == port)
return true;
}
return false;
}struct header_pointers {
struct ethhdr *eth;
struct iphdr *ipv4;
struct ipv6hdr *ipv6;
struct tcphdr *tcp;
__u16 tcp_len;
};static __always_inline int tcp_dissect(void *data, void *data_end,
struct header_pointers *hdr)
{
hdr->eth = data;
if (hdr->eth + 1 > data_end)
return XDP_DROP;
switch (bpf_ntohs(hdr->eth->h_proto)) {
case ETH_P_IP:
hdr->ipv6 = NULL;
hdr->ipv4 = (void *)hdr->eth + sizeof(*hdr->eth);
if (hdr->ipv4 + 1 > data_end)
return XDP_DROP;
if (hdr->ipv4->ihl * 4 < sizeof(*hdr->ipv4))
return XDP_DROP;
if (hdr->ipv4->version != 4)
return XDP_DROP;
if (hdr->ipv4->protocol != IPPROTO_TCP)
return XDP_PASS;
hdr->tcp = (void *)hdr->ipv4 + hdr->ipv4->ihl * 4;
break;
case ETH_P_IPV6:
hdr->ipv4 = NULL;
hdr->ipv6 = (void *)hdr->eth + sizeof(*hdr->eth);
if (hdr->ipv6 + 1 > data_end)
return XDP_DROP;
if (hdr->ipv6->version != 6)
return XDP_DROP;
// XXX: Extension headers are not supported and could circumvent
// XDP SYN flood protection.
if (hdr->ipv6->nexthdr != NEXTHDR_TCP)
return XDP_PASS;
hdr->tcp = (void *)hdr->ipv6 + sizeof(*hdr->ipv6);
break;
default:
// XXX: VLANs will circumvent XDP SYN flood protection.
return XDP_PASS;
}
if (hdr->tcp + 1 > data_end)
return XDP_DROP;
hdr->tcp_len = hdr->tcp->doff * 4;
if (hdr->tcp_len < sizeof(*hdr->tcp))
return XDP_DROP;
return XDP_TX;
}static __always_inline __u8 tcp_mkoptions(__be32 *buf, __be32 *tsopt, __u16 mss,
__u8 wscale)
{
__be32 *start = buf;
*buf++ = bpf_htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | mss);
if (!tsopt)
return buf - start;
if (tsopt[0] & bpf_htonl(1 << 4))
*buf++ = bpf_htonl((TCPOPT_SACK_PERM << 24) |
(TCPOLEN_SACK_PERM << 16) |
(TCPOPT_TIMESTAMP << 8) |
TCPOLEN_TIMESTAMP);
else
*buf++ = bpf_htonl((TCPOPT_NOP << 24) |
(TCPOPT_NOP << 16) |
(TCPOPT_TIMESTAMP << 8) |
TCPOLEN_TIMESTAMP);
*buf++ = tsopt[0];
*buf++ = tsopt[1];
if ((tsopt[0] & bpf_htonl(0xf)) != bpf_htonl(0xf))
*buf++ = bpf_htonl((TCPOPT_NOP << 24) |
(TCPOPT_WINDOW << 16) |
(TCPOLEN_WINDOW << 8) |
wscale);
return buf - start;
}static __always_inline void tcp_gen_synack(struct tcphdr *tcp_header,
__u32 cookie, __be32 *tsopt,
__u16 mss, __u8 wscale)
{
void *tcp_options;
tcp_flag_word(tcp_header) = TCP_FLAG_SYN | TCP_FLAG_ACK;
if (tsopt && (tsopt[0] & bpf_htonl(1 << 5)))
tcp_flag_word(tcp_header) |= TCP_FLAG_ECE;
tcp_header->doff = 5; // doff is part of tcp_flag_word.
swap(tcp_header->source, tcp_header->dest);
tcp_header->ack_seq = bpf_htonl(bpf_ntohl(tcp_header->seq) + 1);
tcp_header->seq = bpf_htonl(cookie);
tcp_header->window = 0;
tcp_header->urg_ptr = 0;
tcp_header->check = 0; // Rely on hardware checksum offload.
tcp_options = (void *)(tcp_header + 1);
tcp_header->doff += tcp_mkoptions(tcp_options, tsopt, mss, wscale);
}static __always_inline void tcpv4_gen_synack(struct header_pointers *hdr,
__u32 cookie, __be32 *tsopt)
{
__u8 wscale;
__u16 mss;
__u8 ttl;
values_get_tcpipopts(&mss, &wscale, &ttl, false);
swap_eth_addr(hdr->eth->h_source, hdr->eth->h_dest);
swap(hdr->ipv4->saddr, hdr->ipv4->daddr);
hdr->ipv4->check = 0; // Rely on hardware checksum offload.
hdr->ipv4->tos = 0;
hdr->ipv4->id = 0;
hdr->ipv4->ttl = ttl;
tcp_gen_synack(hdr->tcp, cookie, tsopt, mss, wscale);
hdr->tcp_len = hdr->tcp->doff * 4;
hdr->ipv4->tot_len = bpf_htons(sizeof(*hdr->ipv4) + hdr->tcp_len);
}static __always_inline void tcpv6_gen_synack(struct header_pointers *hdr,
__u32 cookie, __be32 *tsopt)
{
__u8 wscale;
__u16 mss;
__u8 ttl;
values_get_tcpipopts(&mss, &wscale, &ttl, true);
swap_eth_addr(hdr->eth->h_source, hdr->eth->h_dest);
swap(hdr->ipv6->saddr, hdr->ipv6->daddr);
*(__be32 *)hdr->ipv6 = bpf_htonl(0x60000000);
hdr->ipv6->hop_limit = ttl;
tcp_gen_synack(hdr->tcp, cookie, tsopt, mss, wscale);
hdr->tcp_len = hdr->tcp->doff * 4;
hdr->ipv6->payload_len = bpf_htons(hdr->tcp_len);
}static __always_inline int syncookie_handle_syn(struct header_pointers *hdr,
struct xdp_md *ctx, void *data, void *data_end)
{
__u32 old_pkt_size, new_pkt_size;
// Unlike clang 10, clang 11 and 12 generate code that doesn't pass the
// BPF verifier if tsopt is not volatile. Volatile forces it to store
// the pointer value and use it directly, otherwise tcp_mkoptions is
// (mis)compiled like this:
// if (!tsopt)
// return buf - start;
// reg = stored_return_value_of_bpf_tcp_raw_gen_tscookie;
// if (reg == 0)
// tsopt = tsopt_buf;
// else
// tsopt = NULL;
// ...
// *buf++ = tsopt[1];
// It creates a dead branch where tsopt is assigned NULL, but the
// verifier can't prove it's dead and blocks the program.
__be32 * volatile tsopt = NULL;
__be32 tsopt_buf[2];
void *ip_header;
__u16 ip_len;
__u32 cookie;
__s64 value;
if (hdr->ipv4) {
// Check the IPv4 and TCP checksums before creating a SYNACK.
value = bpf_csum_diff(0, 0, (void *)hdr->ipv4, hdr->ipv4->ihl * 4, 0);
if (value < 0)
return XDP_ABORTED;
if (csum_fold(value) != 0)
return XDP_DROP; // Bad IPv4 checksum.
value = bpf_csum_diff(0, 0, (void *)hdr->tcp, hdr->tcp_len, 0);
if (value < 0)
return XDP_ABORTED;
if (csum_tcpudp_magic(hdr->ipv4->saddr, hdr->ipv4->daddr,
hdr->tcp_len, IPPROTO_TCP, value) != 0)
return XDP_DROP; // Bad TCP checksum.
ip_header = hdr->ipv4;
ip_len = sizeof(*hdr->ipv4);
} else if (hdr->ipv6) {
// Check the TCP checksum before creating a SYNACK.
value = bpf_csum_diff(0, 0, (void *)hdr->tcp, hdr->tcp_len, 0);
if (value < 0)
return XDP_ABORTED;
if (csum_ipv6_magic(&hdr->ipv6->saddr, &hdr->ipv6->daddr,
hdr->tcp_len, IPPROTO_TCP, value) != 0)
return XDP_DROP; // Bad TCP checksum.
ip_header = hdr->ipv6;
ip_len = sizeof(*hdr->ipv6);
} else {
return XDP_ABORTED;
}
// Issue SYN cookies on allowed ports, drop SYN packets on
// blocked ports.
if (!check_port_allowed(bpf_ntohs(hdr->tcp->dest)))
return XDP_DROP;
value = bpf_tcp_raw_gen_syncookie(ip_header, ip_len,
(void *)hdr->tcp, hdr->tcp_len);
if (value < 0)
return XDP_ABORTED;
cookie = (__u32)value;
if (cookie_init_timestamp_raw((void *)hdr->tcp, hdr->tcp_len,
&tsopt_buf[0], &tsopt_buf[1], data_end))
tsopt = tsopt_buf;
// Check that there is enough space for a SYNACK. It also covers
// the check that the destination of the __builtin_memmove below
// doesn't overflow.
if (data + sizeof(*hdr->eth) + ip_len + TCP_MAXLEN > data_end)
return XDP_ABORTED;
if (hdr->ipv4) {
if (hdr->ipv4->ihl * 4 > sizeof(*hdr->ipv4)) {
struct tcphdr *new_tcp_header;
new_tcp_header = data + sizeof(*hdr->eth) + sizeof(*hdr->ipv4);
__builtin_memmove(new_tcp_header, hdr->tcp, sizeof(*hdr->tcp));
hdr->tcp = new_tcp_header;
hdr->ipv4->ihl = sizeof(*hdr->ipv4) / 4;
}
tcpv4_gen_synack(hdr, cookie, tsopt);
} else if (hdr->ipv6) {
tcpv6_gen_synack(hdr, cookie, tsopt);
} else {
return XDP_ABORTED;
}
// Recalculate checksums.
hdr->tcp->check = 0;
value = bpf_csum_diff(0, 0, (void *)hdr->tcp, hdr->tcp_len, 0);
if (value < 0)
return XDP_ABORTED;
if (hdr->ipv4) {
hdr->tcp->check = csum_tcpudp_magic(hdr->ipv4->saddr,
hdr->ipv4->daddr,
hdr->tcp_len,
IPPROTO_TCP,
value);
hdr->ipv4->check = 0;
value = bpf_csum_diff(0, 0, (void *)hdr->ipv4, sizeof(*hdr->ipv4), 0);
if (value < 0)
return XDP_ABORTED;
hdr->ipv4->check = csum_fold(value);
} else if (hdr->ipv6) {
hdr->tcp->check = csum_ipv6_magic(&hdr->ipv6->saddr,
&hdr->ipv6->daddr,
hdr->tcp_len,
IPPROTO_TCP,
value);
} else {
return XDP_ABORTED;
}
// Set the new packet size.
old_pkt_size = data_end - data;
new_pkt_size = sizeof(*hdr->eth) + ip_len + hdr->tcp->doff * 4;
if (bpf_xdp_adjust_tail(ctx, new_pkt_size - old_pkt_size))
return XDP_ABORTED;
values_inc_synacks();
return XDP_TX;
}static __always_inline int syncookie_handle_ack(struct header_pointers *hdr)
{
int err;
if (hdr->ipv4)
err = bpf_tcp_raw_check_syncookie(hdr->ipv4, sizeof(*hdr->ipv4),
(void *)hdr->tcp, hdr->tcp_len);
else if (hdr->ipv6)
err = bpf_tcp_raw_check_syncookie(hdr->ipv6, sizeof(*hdr->ipv6),
(void *)hdr->tcp, hdr->tcp_len);
else
return XDP_ABORTED;
if (err)
return XDP_DROP;
return XDP_PASS;
}SEC("xdp/syncookie")
int syncookie_xdp(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct header_pointers hdr;
struct bpf_sock_tuple tup;
struct bpf_nf_conn *ct;
__u32 tup_size;
__s64 value;
int ret;
ret = tcp_dissect(data, data_end, &hdr);
if (ret != XDP_TX)
return ret;
if (hdr.ipv4) {
// TCP doesn't normally use fragments, and XDP can't reassemble them.
if ((hdr.ipv4->frag_off & bpf_htons(IP_DF | IP_MF | IP_OFFSET)) !=
bpf_htons(IP_DF))
return XDP_DROP;
tup.ipv4.saddr = hdr.ipv4->saddr;
tup.ipv4.daddr = hdr.ipv4->daddr;
tup.ipv4.sport = hdr.tcp->source;
tup.ipv4.dport = hdr.tcp->dest;
tup_size = sizeof(tup.ipv4);
} else if (hdr.ipv6) {
__builtin_memcpy(tup.ipv6.saddr, &hdr.ipv6->saddr,
sizeof(tup.ipv6.saddr));
__builtin_memcpy(tup.ipv6.daddr, &hdr.ipv6->daddr,
sizeof(tup.ipv6.daddr));
tup.ipv6.sport = hdr.tcp->source;
tup.ipv6.dport = hdr.tcp->dest;
tup_size = sizeof(tup.ipv6);
} else {
// The verifier can't track that either ipv4 or ipv6 is not NULL.
return XDP_ABORTED;
}
value = 0; // Flags.
ct = bpf_ct_lookup_tcp(ctx, &tup, tup_size, BPF_F_CURRENT_NETNS, &value);
if (ct) {
unsigned long status = ct->status;
bpf_ct_release(ct);
if (status & IPS_CONFIRMED_BIT)
return XDP_PASS;
} else if (value != -ENOENT) {
return XDP_ABORTED;
}
// value == -ENOENT || !(status & IPS_CONFIRMED_BIT)
if ((hdr.tcp->syn ^ hdr.tcp->ack) != 1)
return XDP_DROP;
// Grow the TCP header to TCP_MAXLEN to be able to pass any hdr.tcp_len
// to bpf_tcp_raw_gen_syncookie and pass the verifier.
if (bpf_xdp_adjust_tail(ctx, TCP_MAXLEN - hdr.tcp_len))
return XDP_ABORTED;
data_end = (void *)(long)ctx->data_end;
data = (void *)(long)ctx->data;
if (hdr.ipv4) {
hdr.eth = data;
hdr.ipv4 = (void *)hdr.eth + sizeof(*hdr.eth);
// IPV4_MAXLEN is needed when calculating checksum.
// At least sizeof(struct iphdr) is needed here to access ihl.
if ((void *)hdr.ipv4 + IPV4_MAXLEN > data_end)
return XDP_ABORTED;
hdr.tcp = (void *)hdr.ipv4 + hdr.ipv4->ihl * 4;
} else if (hdr.ipv6) {
hdr.eth = data;
hdr.ipv6 = (void *)hdr.eth + sizeof(*hdr.eth);
hdr.tcp = (void *)hdr.ipv6 + sizeof(*hdr.ipv6);
} else {
return XDP_ABORTED;
}
if ((void *)hdr.tcp + TCP_MAXLEN > data_end)
return XDP_ABORTED;
// We run out of registers, tcp_len gets spilled to the stack, and the
// verifier forgets its min and max values checked above in tcp_dissect.
hdr.tcp_len = hdr.tcp->doff * 4;
if (hdr.tcp_len < sizeof(*hdr.tcp))
return XDP_ABORTED;
return hdr.tcp->syn ? syncookie_handle_syn(&hdr, ctx, data, data_end) :
syncookie_handle_ack(&hdr);
}SEC("xdp/dummy")
int dummy_xdp(struct xdp_md *ctx)
{
// veth requires XDP programs to be set on both sides.
return XDP_PASS;
}char _license[] SEC("license") = "GPL";
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 31 lines · Maxim Mikityanskiy
Quoted 30 lines · Yonghong Song
Quoted 27 lines · Maxim Mikityanskiy
Quoted 11 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Lorenz Bauer
Quoted 2 lines
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
After trying to put this code into BPF (replacing the underlying
ktime_get_nswithktime_get_mono_fast_ns), I experienced issues with passing the verifier.In addition to comparing ptr to end, I had to add checks that compare ptr to
data_end, because the verifier can't deduce that end <=data_end. More branches will add a certain slowdown (not measured).A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is 10. With more iterations, I have the following error:
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000)
max_states_per_insn29total_states35489peak_states596mark_read45I assume that
BPF_COMPLEXITY_LIMIT_INSNS(1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using
bpf_load_hdr_optand calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal?
Maxim, this may expose a verifier bug. Do you have a reproducer I can access? I would like to debug this to see what is the root case. Thanks!
Thanks, I appreciate your help in debugging it. The reproducer is based on the modified XDP program from patch 10 in this series. You'll need to apply at least patches 6, 7, 8 from this series to get new BPF helpers needed for the XDP program (tell me if that's a problem, I can try to remove usage of new helpers, but it will affect the program length and may produce different results in the verifier).
See the C code of the program that passes the verifier (compiled with clang version 12.0.0-1ubuntu1) in the bottom of this email. If you increase the loop boundary from 10 to at least 11 in
cookie_init_timestamp_raw(), it fails the verifier after a few minutes.
I tried to reproduce with latest llvm (llvm-project repo), loop boundary 10 is okay and 11 exceeds the 1M complexity limit. For 10, the number of verified instructions is 563626 (more than 0.5M) so it is totally possible that one more iteration just blows past the limit.
Quoted 13 lines
If you apply this tiny change, it fails the verifier after about 3 hours:
samples/bpf/syncookie_kern.c +1
-0@@ -167,6 +167,7 @@ static __always_inline bool cookie_init_ for (i = 0; i < 10; i++) { u8 opcode, opsize; +skip_nop: if (ptr >= end) break; if (ptr + 1 > data_end)
samples/bpf/syncookie_kern.c static __always_inline bool cookie_init_ · line 185
Quoted 8 lines
samples/bpf/syncookie_kern.c +1
-1@@ -178,7 +179,7 @@ static __always_inline bool cookie_init_ break; if (opcode == TCPOPT_NOP) { ++ptr; - continue; + goto skip_nop; } if (ptr + 1 >= end)
I tried this as well, with latest llvm, and got the following errors in ~30 seconds:
......
536: (79) r2 = *(u64 *)(r10 -96)
537: R0=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R1=pkt(id=9,off=499,r=518,umax_value=60,var_off=(0x0; 0x3c)) R2=pkt_end(id=0,off=0,imm=0) R3=pkt(id=27,off=14,r=0,umin_value=20,umax_value=120,var_off=(0x0; 0x7c),s32_min_value=0,s32_max_value=124,u32_max_value=124) R4=invP3 R5=inv1 R6=ctx(id=0,off=0,imm=0) R7=pkt(id=9,off=519,r=518,umax_va^C [[email protected] ~/work/bpf-next/samples/bpf] tail -f log
550: (55) if r0 != 0x4 goto pc+4
The sequence of 8193 jumps is too complex.
verification time 30631375 usec
stack depth 160
processed 330595 insns (limit 1000000) max_states_per_insn 4 total_states 20377 peak_states 100 mark_read 37
With llvm12, I got the similar verification error:
The sequence of 8193 jumps is too complex.
processed 330592 insns (limit 1000000) max_states_per_insn 4 total_states 20378 peak_states 101 mark_read 37
Could you check again with your experiment which takes 3 hours to fail? What is the verification failure log?
Quoted 6 lines
--cut--
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/* Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
[...]
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 38 lines · Yonghong Song
Quoted 35 lines · Maxim Mikityanskiy
Quoted 33 lines · Yonghong Song
Quoted 21 lines · Maxim Mikityanskiy
Quoted 11 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Lorenz Bauer
Quoted 2 lines
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
After trying to put this code into BPF (replacing the underlying
ktime_get_nswithktime_get_mono_fast_ns), I experienced issues with passing the verifier.In addition to comparing ptr to end, I had to add checks that compare ptr to
data_end, because the verifier can't deduce that end <=data_end. More branches will add a certain slowdown (not measured).A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is 10. With more iterations, I have the following error:
BPF program is too large. Processed 1000001 insn
Quoted 7 lines
processed 1000001 insns (limit 1000000)
max_states_per_insn29total_states35489peak_states596mark_read45I assume that
BPF_COMPLEXITY_LIMIT_INSNS(1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using
bpf_load_hdr_optand calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal?
Maxim, this may expose a verifier bug. Do you have a reproducer I can access? I would like to debug this to see what is the root case. Thanks!
Quoted 8 lines
Quoted 3 lines
Thanks, I appreciate your help in debugging it. The reproducer is based on the modified XDP program from patch 10 in this series. You'll need to apply at least patches 6, 7, 8 from this series to get new BPF helpers needed for the XDP program (tell me if that's a problem, I can try to remove usage of new helpers, but it will affect the program length and may produce different results in the verifier).
See the C code of the program that passes the verifier (compiled with clang version 12.0.0-1ubuntu1) in the bottom of this email. If you increase the loop boundary from 10 to at least 11 in
cookie_init_timestamp_raw(), it fails the verifier after a few minutes.I tried to reproduce with latest llvm (llvm-project repo), loop boundary 10 is okay and 11 exceeds the 1M complexity limit. For 10, the number of verified instructions is 563626 (more than 0.5M) so it is totally possible that one more iteration just blows past the limit.
So, does it mean that the verifying complexity grows exponentially with increasing the number of loop iterations (options parsed)?
Is it a good enough reason to keep this code as a BPF helper, rather than trying to fit it into the BPF program?
Quoted 36 lines
Quoted 14 lines
If you apply this tiny change, it fails the verifier after about 3 hours:
samples/bpf/syncookie_kern.c +1
-0@@ -167,6 +167,7 @@ static __always_inline bool cookie_init_ for (i = 0; i < 10; i++) { u8 opcode, opsize; +skip_nop: if (ptr >= end) break; if (ptr + 1 > data_end)samples/bpf/syncookie_kern.c static __always_inline bool cookie_init_ · line 185
Quoted 8 lines
samples/bpf/syncookie_kern.c +1
-1@@ -178,7 +179,7 @@ static __always_inline bool cookie_init_ break; if (opcode == TCPOPT_NOP) { ++ptr; - continue; + goto skip_nop; } if (ptr + 1 >= end)I tried this as well, with latest llvm, and got the following errors in ~30 seconds:
......
536: (79) r2 = *(u64 *)(r10 -96)
537: R0=inv(id=0,umax_value=255,var_off=(0x0;0xff)) R1=pkt(id=9,off=499,r=518,umax_value=60,var_off=(0x0;0x3c)) R2=pkt_end(id=0,off=0,imm=0) R3=pkt(id=27,off=14,r=0,umin_value=20,umax_value=120,var_off=(0x0; 0x7c),s32_min_value=0,s32_max_value=124,u32_max_value=124) R4=invP3 R5=inv1 R6=ctx(id=0,off=0,imm=0) R7=pkt(id=9,off=519,r=518,umax_va^C [[email protected] ~/work/bpf-next/samples/bpf] tail -f log
550: (55) if r0 !=0x4goto pc+4
The sequence of 8193 jumps is too complex.
verification time 30631375 usec
stack depth 160
processed 330595 insns (limit 1000000)max_states_per_insn4total_states20377peak_states100mark_read37
Quoted 7 lines
With llvm12, I got the similar verification error:
The sequence of 8193 jumps is too complex.
processed 330592 insns (limit 1000000)max_states_per_insn4total_states20378peak_states101mark_read37Could you check again with your experiment which takes 3 hours to fail? What is the verification failure log?
The log is similar:
...
; if (opsize == TCPOLEN_WINDOW) {
460: (55) if r8 != 0x3 goto pc+31
R0_w=pkt(id=28132,off=4037,r=0,umin_value=20,umax_value=2610,var_off=(0x0; 0x3ffff),s32_min_value=0,s32_max_value=262143,u32_max_value=262143) R1=inv0 R2=pkt(id=27,off=14,r=0,umin_value=20,umax_value=120,var_off=(0x0; 0x7c),s32_min_value=0,s32_max_value=124,u32_max_value=124) R3_w=inv3 R4_w=inv9 R5=inv0 R6=ctx(id=0,off=0,imm=0) R7_w=pkt(id=44,off=4047,r=4039,umin_value=18,umax_value=2355,var_off=(0x0; 0x1ffff),s32_min_value=0,s32_max_value=131071,u32_max_value=131071) R8_w=invP3 R9=inv0 R10=fp0 fp-16=????mmmm fp-24=00000000 fp-64=????mmmm fp-72=mmmmmmmm fp-80=mmmmmmmm fp-88=pkt fp-96=pkt_end fp-104=pkt fp-112=pkt fp-120=inv20 fp-128=mmmmmmmm fp-136_w=inv14 fp-144=pkt ; if (ptr + TCPOLEN_WINDOW > data_end)
461: (bf) r3 = r7
462: (07) r3 += -7
; if (ptr + TCPOLEN_WINDOW > data_end)
463: (79) r8 = *(u64 *)(r10 -96)
464: (2d) if r3 > r8 goto pc+56
The sequence of 8193 jumps is too complex.
processed 414429 insns (limit 1000000) max_states_per_insn 4 total_states 8097 peak_states 97 mark_read 49
libbpf: -- END LOG --
libbpf: failed to load program 'syncookie_xdp'
libbpf: failed to load object '.../samples/bpf/syncookie_kern.o'
Error: bpf_prog_load: Unknown error 4007
real 189m49.659s user 0m0.012s sys 189m26.322s
Ubuntu clang version 12.0.0-1ubuntu1
I wonder why it takes only 30 seconds for you. As I understand, the expectation is less than 1 second anyway, but the difference between 30 seconds and 3 hours is huge. Maybe some kernel config options matter (KASAN?)
Is it expected that increasing the loop length linearly increases the verifying complexity exponentially? Is there any mitigation?
Thanks,
Max
Quoted 7 lines
Quoted 6 lines
--cut--
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/* Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */[...]
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 41 lines · Maxim Mikityanskiy
Quoted 40 lines · Yonghong Song
Quoted 37 lines · Maxim Mikityanskiy
Quoted 26 lines · Yonghong Song
Quoted 23 lines · Maxim Mikityanskiy
Quoted 11 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Lorenz Bauer
Quoted 2 lines
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
After trying to put this code into BPF (replacing the underlying
ktime_get_nswithktime_get_mono_fast_ns), I experienced issues with passing the verifier.In addition to comparing ptr to end, I had to add checks that compare ptr to
data_end, because the verifier can't deduce that end <=data_end. More branches will add a certain slowdown (not measured).A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is 10. With more iterations, I have the following error:
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000)
max_states_per_insn29total_states35489peak_states596mark_read45Quoted 7 lines
Quoted 5 lines
I assume that
BPF_COMPLEXITY_LIMIT_INSNS(1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using
bpf_load_hdr_optand calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal?
Maxim, this may expose a verifier bug. Do you have a reproducer I can access? I would like to debug this to see what is the root case. Thanks!
Thanks, I appreciate your help in debugging it. The reproducer is based on the modified XDP program from patch 10 in this series. You'll need to apply at least patches 6, 7, 8 from this series to get new BPF helpers needed for the XDP program (tell me if that's a problem, I can try to remove usage of new helpers, but it will affect the program length and may produce different results in the verifier).
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
See the C code of the program that passes the verifier (compiled with clang version 12.0.0-1ubuntu1) in the bottom of this email. If you increase the loop boundary from 10 to at least 11 in
cookie_init_timestamp_raw(), it fails the verifier after a few minutes.I tried to reproduce with latest llvm (llvm-project repo), loop boundary 10 is okay and 11 exceeds the 1M complexity limit. For 10, the number of verified instructions is 563626 (more than 0.5M) so it is totally possible that one more iteration just blows past the limit.
So, does it mean that the verifying complexity grows exponentially with increasing the number of loop iterations (options parsed)?
Depending on verification time pruning results, it is possible slightly increase number of branches could result quite some (2x, 4x, etc.) of to-be-verified dynamic instructions.
Quoted 2 lines
Is it a good enough reason to keep this code as a BPF helper, rather than trying to fit it into the BPF program?
Another option is to use global function, which is verified separately from the main bpf program.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
If you apply this tiny change, it fails the verifier after about 3 hours:
[...]
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 51 lines · Yonghong Song
Quoted 48 lines · Maxim Mikityanskiy
Quoted 46 lines · Yonghong Song
Quoted 31 lines · Maxim Mikityanskiy
Quoted 30 lines · Yonghong Song
Quoted 27 lines · Maxim Mikityanskiy
Quoted 11 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Lorenz Bauer
Quoted 2 lines
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
After trying to put this code into BPF (replacing the underlying
ktime_get_nswithktime_get_mono_fast_ns), I experienced issues with passing the verifier.In addition to comparing ptr to end, I had to add checks that compare ptr to
data_end, because the verifier can't deduce that end <=data_end. More branches will add a certain slowdown (not measured).A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is 10. With more iterations, I have the following error:
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000)
max_states_per_insn29total_states35489peak_states596mark_read45I assume that
BPF_COMPLEXITY_LIMIT_INSNS(1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using
bpf_load_hdr_optand calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?Quoted 7 lines
Quoted 3 lines
Quoted 1 line
One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal?
Maxim, this may expose a verifier bug. Do you have a reproducer I can access? I would like to debug this to see what is the root case. Thanks!
Thanks, I appreciate your help in debugging it. The reproducer is based on the modified XDP program from patch 10 in this series. You'll need to apply at least patches 6, 7, 8 from this series to get new BPF helpers needed for the XDP program (tell me if that's a problem, I can try to remove usage of new helpers, but it will affect the program length and may produce different results in the verifier).
See the C code of the program that passes the verifier (compiled with clang version 12.0.0-1ubuntu1) in the bottom of this email. If you increase the loop boundary from 10 to at least 11 in
cookie_init_timestamp_raw(), it fails the verifier after a few minutes.I tried to reproduce with latest llvm (llvm-project repo), loop boundary 10 is okay and 11 exceeds the 1M complexity limit. For 10, the number of verified instructions is 563626 (more than 0.5M) so it is totally possible that one more iteration just blows past the limit.
Quoted 4 lines
Quoted 1 line
So, does it mean that the verifying complexity grows exponentially with increasing the number of loop iterations (options parsed)?
Depending on verification time pruning results, it is possible slightly increase number of branches could result quite some (2x, 4x, etc.) of to-be-verified dynamic instructions.
Is it at least theoretically possible to make this coefficient below 2x? I.e. write a loop, so that adding another iteration will not double the number of verified instructions, but will have a smaller increase?
If that's not possible, then it looks like BPF can't have loops bigger than ~19 iterations (2^20 > 1M), and this function is not implementable in BPF.
Quoted 5 lines
Quoted 2 lines
Is it a good enough reason to keep this code as a BPF helper, rather than trying to fit it into the BPF program?
Another option is to use global function, which is verified separately from the main bpf program.
Simply removing __always_inline didn't change anything. Do I need to make any other changes? Will it make sense to call a global function in a loop, i.e. will it increase chances to pass the verifier?
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
If you apply this tiny change, it fails the verifier after about 3 hours:
[...]
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 52 lines · Maxim Mikityanskiy
Quoted 51 lines · Yonghong Song
Quoted 48 lines · Maxim Mikityanskiy
Quoted 46 lines · Yonghong Song
Quoted 31 lines · Maxim Mikityanskiy
Quoted 30 lines · Yonghong Song
Quoted 27 lines · Maxim Mikityanskiy
Quoted 11 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Lorenz Bauer
Quoted 2 lines
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
After trying to put this code into BPF (replacing the underlying
ktime_get_nswithktime_get_mono_fast_ns), I experienced issues with passing the verifier.In addition to comparing ptr to end, I had to add checks that compare ptr to
data_end, because the verifier can't deduce that end <=data_end. More branches will add a certain slowdown (not measured).A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is 10. With more iterations, I have the following error:
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000)
max_states_per_insn29total_states35489peak_states596mark_read45I assume that
BPF_COMPLEXITY_LIMIT_INSNS(1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using
bpf_load_hdr_optand calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?Quoted 7 lines
Quoted 3 lines
Quoted 1 line
One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal?
Maxim, this may expose a verifier bug. Do you have a reproducer I can access? I would like to debug this to see what is the root case. Thanks!
Thanks, I appreciate your help in debugging it. The reproducer is based on the modified XDP program from patch 10 in this series. You'll need to apply at least patches 6, 7, 8 from this series to get new BPF helpers needed for the XDP program (tell me if that's a problem, I can try to remove usage of new helpers, but it will affect the program length and may produce different results in the verifier).
See the C code of the program that passes the verifier (compiled with clang version 12.0.0-1ubuntu1) in the bottom of this email. If you increase the loop boundary from 10 to at least 11 in
cookie_init_timestamp_raw(), it fails the verifier after a few minutes.I tried to reproduce with latest llvm (llvm-project repo), loop boundary 10 is okay and 11 exceeds the 1M complexity limit. For 10, the number of verified instructions is 563626 (more than 0.5M) so it is totally possible that one more iteration just blows past the limit.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
So, does it mean that the verifying complexity grows exponentially with increasing the number of loop iterations (options parsed)?
Depending on verification time pruning results, it is possible slightly increase number of branches could result quite some (2x, 4x, etc.) of to-be-verified dynamic instructions.
Is it at least theoretically possible to make this coefficient below 2x? I.e. write a loop, so that adding another iteration will not double the number of verified instructions, but will have a smaller increase?
If that's not possible, then it looks like BPF can't have loops bigger than ~19 iterations (2^20 > 1M), and this function is not implementable in BPF.
This is the worst case. As I mentioned pruning plays a huge role in verification. Effective pruning can add little increase of dynamic instructions say from 19 iterations to 20 iterations. But we have to look at verifier log to find out whether pruning is less effective or
something else... Based on my experience, in most cases, pruning is
quite effective. But occasionally it is not... You can look atverifier.c file to roughly understand how pruning work.
Not sure whether in this case it is due to less effective pruning or inherently we just have to go through all these dynamic instructions for verification.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Is it a good enough reason to keep this code as a BPF helper, rather than trying to fit it into the BPF program?
Another option is to use global function, which is verified separately from the main bpf program.
Simply removing
__always_inlinedidn't change anything. Do I need to make any other changes? Will it make sense to call a global function in a loop, i.e. will it increase chances to pass the verifier?
global function cannot be static function. You can try either global function inside the loop or global function containing the loop. It probably more effective to put loops inside the global function. You have to do some experiments to see which one is better.
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
If you apply this tiny change, it fails the verifier after about 3 hours:
[...]
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 79 lines · Yonghong Song
Quoted 52 lines · Maxim Mikityanskiy
Quoted 51 lines · Yonghong Song
Quoted 48 lines · Maxim Mikityanskiy
Quoted 46 lines · Yonghong Song
Quoted 31 lines · Maxim Mikityanskiy
Quoted 30 lines · Yonghong Song
Quoted 27 lines · Maxim Mikityanskiy
Quoted 11 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Lorenz Bauer
Quoted 2 lines
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
After trying to put this code into BPF (replacing the underlying
ktime_get_nswithktime_get_mono_fast_ns), I experienced issues with passing the verifier.In addition to comparing ptr to end, I had to add checks that compare ptr to
data_end, because the verifier can't deduce that end <=data_end. More branches will add a certain slowdown (not measured).A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is 10. With more iterations, I have the following error:
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000)
max_states_per_insn29total_states35489peak_states596mark_read45I assume that
BPF_COMPLEXITY_LIMIT_INSNS(1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using
bpf_load_hdr_optand calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?Quoted 7 lines
Quoted 3 lines
Quoted 1 line
One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal?
Maxim, this may expose a verifier bug. Do you have a reproducer I can access? I would like to debug this to see what is the root case. Thanks!
Thanks, I appreciate your help in debugging it. The reproducer is based on the modified XDP program from patch 10 in this series. You'll need to apply at least patches 6, 7, 8 from this series to get new BPF helpers needed for the XDP program (tell me if that's a problem, I can try to remove usage of new helpers, but it will affect the program length and may produce different results in the verifier).
See the C code of the program that passes the verifier (compiled with clang version 12.0.0-1ubuntu1) in the bottom of this email. If you increase the loop boundary from 10 to at least 11 in
cookie_init_timestamp_raw(), it fails the verifier after a few minutes.I tried to reproduce with latest llvm (llvm-project repo), loop boundary 10 is okay and 11 exceeds the 1M complexity limit. For 10, the number of verified instructions is 563626 (more than 0.5M) so it is totally possible that one more iteration just blows past the limit.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
So, does it mean that the verifying complexity grows exponentially with increasing the number of loop iterations (options parsed)?
Depending on verification time pruning results, it is possible slightly increase number of branches could result quite some (2x, 4x, etc.) of to-be-verified dynamic instructions.
Is it at least theoretically possible to make this coefficient below 2x? I.e. write a loop, so that adding another iteration will not double the number of verified instructions, but will have a smaller increase?
If that's not possible, then it looks like BPF can't have loops bigger than ~19 iterations (2^20 > 1M), and this function is not implementable in BPF.
This is the worst case. As I mentioned pruning plays a huge role in verification. Effective pruning can add little increase of dynamic instructions say from 19 iterations to 20 iterations. But we have to look at verifier log to find out whether pruning is less effective or
something else... Based on my experience, in most cases, pruning is
quite effective. But occasionally it is not... You can look atverifier.cfile to roughly understand how pruning work.Not sure whether in this case it is due to less effective pruning or inherently we just have to go through all these dynamic instructions for verification.
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
Is it a good enough reason to keep this code as a BPF helper, rather than trying to fit it into the BPF program?
Another option is to use global function, which is verified separately from the main bpf program.
Quoted 7 lines
Quoted 1 line
Simply removing
__always_inlinedidn't change anything. Do I need to make any other changes? Will it make sense to call a global function in a loop, i.e. will it increase chances to pass the verifier?global function cannot be static function. You can try either global function inside the loop or global function containing the loop. It probably more effective to put loops inside the global function. You have to do some experiments to see which one is better.
Sorry for a probably noob question, but how can I pass data_end to a global function? I'm getting this error:
Validating cookie_init_timestamp_raw() func#1...
arg#4 reference type('UNKNOWN ') size cannot be determined: -22
processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
When I removed data_end, I got another one:
; opcode = ptr[0];
969: (71) r8 = *(u8 *)(r0 +0)
R0=mem(id=0,ref_obj_id=0,off=20,imm=0)
R1=mem(id=0,ref_obj_id=0,off=0,umin_value=4,umax_value=60,var_off=(0x0; 0x3f),s32_min_value=0,s32_max_value=63,u32_max_value=63)
R2=invP0 R3=invP0 R4=mem_or_null(id=6,ref_obj_id=0,off=0,imm=0)
R5=invP0 R6=mem_or_null(id=5,ref_obj_id=0,off=0,imm=0) R7=mem(id=0,ref_obj_id=0,off=0,imm=0) R10=fp0 fp -8=00000000 fp-16=invP15
invalid access to memory, mem_size=20 off=20 size=1
R0 min value is outside of the allowed memory range
processed 20 insns (limit 1000000) max_states_per_insn 0 total_states 2 peak_states 2 mark_read 1
It looks like pointers to the context aren't supported:
https://www.spinics.net/lists/bpf/msg34907.html
Quoted 1 line
test_global_func11- check that CTX pointer cannot be passed
What is the standard way to pass packet data to a global function?
Thanks,
Max
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
If you apply this tiny change, it fails the verifier after about 3 hours:
[...]
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 107 lines · Maxim Mikityanskiy
Quoted 79 lines · Yonghong Song
Quoted 52 lines · Maxim Mikityanskiy
Quoted 51 lines · Yonghong Song
Quoted 48 lines · Maxim Mikityanskiy
Quoted 46 lines · Yonghong Song
Quoted 31 lines · Maxim Mikityanskiy
Quoted 30 lines · Yonghong Song
Quoted 27 lines · Maxim Mikityanskiy
Quoted 11 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Lorenz Bauer
Quoted 2 lines
+bool
cookie_init_timestamp_raw(struct tcphdr *th, __be32 *tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that means you can't implement it in BPF?
I was about to reply with some other comments but upon closer inspection I ended up at the same conclusion: this helper doesn't seem to be needed at all?
After trying to put this code into BPF (replacing the underlying
ktime_get_nswithktime_get_mono_fast_ns), I experienced issues with passing the verifier.In addition to comparing ptr to end, I had to add checks that compare ptr to
data_end, because the verifier can't deduce that end <=data_end. More branches will add a certain slowdown (not measured).A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is 10. With more iterations, I have the following error:
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000)
max_states_per_insn29total_states35489peak_states596mark_read45I assume that
BPF_COMPLEXITY_LIMIT_INSNS(1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using
bpf_load_hdr_optand calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?Quoted 7 lines
Quoted 3 lines
Quoted 1 line
One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal?
Maxim, this may expose a verifier bug. Do you have a reproducer I can access? I would like to debug this to see what is the root case. Thanks!
Thanks, I appreciate your help in debugging it. The reproducer is based on the modified XDP program from patch 10 in this series. You'll need to apply at least patches 6, 7, 8 from this series to get new BPF helpers needed for the XDP program (tell me if that's a problem, I can try to remove usage of new helpers, but it will affect the program length and may produce different results in the verifier).
See the C code of the program that passes the verifier (compiled with clang version 12.0.0-1ubuntu1) in the bottom of this email. If you increase the loop boundary from 10 to at least 11 in
cookie_init_timestamp_raw(), it fails the verifier after a few minutes.I tried to reproduce with latest llvm (llvm-project repo), loop boundary 10 is okay and 11 exceeds the 1M complexity limit. For 10, the number of verified instructions is 563626 (more than 0.5M) so it is totally possible that one more iteration just blows past the limit.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
So, does it mean that the verifying complexity grows exponentially with increasing the number of loop iterations (options parsed)?
Depending on verification time pruning results, it is possible slightly increase number of branches could result quite some (2x, 4x, etc.) of to-be-verified dynamic instructions.
Is it at least theoretically possible to make this coefficient below 2x? I.e. write a loop, so that adding another iteration will not double the number of verified instructions, but will have a smaller increase?
If that's not possible, then it looks like BPF can't have loops bigger than ~19 iterations (2^20 > 1M), and this function is not implementable in BPF.
This is the worst case. As I mentioned pruning plays a huge role in verification. Effective pruning can add little increase of dynamic instructions say from 19 iterations to 20 iterations. But we have to look at verifier log to find out whether pruning is less effective or
something else... Based on my experience, in most cases, pruning is
quite effective. But occasionally it is not... You can look atverifier.cfile to roughly understand how pruning work.Not sure whether in this case it is due to less effective pruning or inherently we just have to go through all these dynamic instructions for verification.
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
Is it a good enough reason to keep this code as a BPF helper, rather than trying to fit it into the BPF program?
Another option is to use global function, which is verified separately from the main bpf program.
Quoted 7 lines
Quoted 1 line
Simply removing
__always_inlinedidn't change anything. Do I need to make any other changes? Will it make sense to call a global function in a loop, i.e. will it increase chances to pass the verifier?global function cannot be static function. You can try either global function inside the loop or global function containing the loop. It probably more effective to put loops inside the global function. You have to do some experiments to see which one is better.
Sorry for a probably noob question, but how can I pass
data_endto a global function? I'm getting this error:Validating
cookie_init_timestamp_raw()func#1...
arg#4 reference type('UNKNOWN ') size cannot be determined: -22
processed 0 insns (limit 1000000)max_states_per_insn0total_states0peak_states0mark_read0When I removed
data_end, I got another one:; opcode = ptr[0];
969: (71) r8 = *(u8 *)(r0 +0)
R0=mem(id=0,ref_obj_id=0,off=20,imm=0)
R1=mem(id=0,ref_obj_id=0,off=0,umin_value=4,umax_value=60,var_off=(0x0; 0x3f),s32_min_value=0,s32_max_value=63,u32_max_value=63)
R2=invP0 R3=invP0 R4=mem_or_null(id=6,ref_obj_id=0,off=0,imm=0)
R5=invP0 R6=mem_or_null(id=5,ref_obj_id=0,off=0,imm=0) R7=mem(id=0,ref_obj_id=0,off=0,imm=0) R10=fp0 fp -8=00000000 fp-16=invP15
invalid access to memory, mem_size=20 off=20 size=1
R0 min value is outside of the allowed memory range
processed 20 insns (limit 1000000)max_states_per_insn0total_states2peak_states2mark_read1
Quoted 7 lines
It looks like pointers to the context aren't supported:
https://www.spinics.net/lists/bpf/msg34907.html
Quoted 1 line
test_global_func11- check that CTX pointer cannot be passedWhat is the standard way to pass packet data to a global function?
Since global function is separately verified, you need to pass the 'ctx' to the global function and do the 'data_end' check again in the global function. This will incur some packet re-parsing overhead similar to
tail calls.
Quoted 12 lines
Thanks,
MaxQuoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
If you apply this tiny change, it fails the verifier after about 3 hours:
[...]
Re: [PATCH bpf-next 09/10] bpf: Add a helper to issue timestamp cookies in XDP
Quoted 182 lines · Yonghong Song
Quoted 178 lines · Maxim Mikityanskiy
Quoted 143 lines · Yonghong Song
Quoted 107 lines · Maxim Mikityanskiy
Quoted 98 lines · Yonghong Song
Quoted 95 lines · Maxim Mikityanskiy
Quoted 93 lines · Yonghong Song
Quoted 78 lines · Maxim Mikityanskiy
Quoted 65 lines · Yonghong Song
Quoted 53 lines · Maxim Mikityanskiy
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 6 lines · Lorenz Bauer
Quoted 2 lines
+bool cookie_init_timestamp_raw(struct tcphdr *th, __be32
*tsval, __be32 *tsecr)I'm probably missing context, Is there something in this function that
means you can't implement it in BPF?Quoted 5 lines
I was about to reply with some other comments but upon closer inspection
I ended up at the same conclusion: this helper doesn't seem to be needed
at all?After trying to put this code into BPF (replacing the underlying
ktime_get_nswithktime_get_mono_fast_ns), I experienced issues with passing the verifier.In addition to comparing ptr to end, I had to add checks that compare ptr to
data_end, because the verifier can't deduce that end <=data_end. More branches will add a certain slowdown (not measured).A more serious issue is the overall program complexity. Even though the loop over the TCP options has an upper bound, and the pointer advances by at least one byte every iteration, I had to limit the total number of iterations artificially. The maximum number of iterations that makes the verifier happy is
10. With more iterations, I have the following error:BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000)
max_states_per_insn29total_states35489peak_states596mark_read45I assume that
BPF_COMPLEXITY_LIMIT_INSNS(1 million) is the accumulated amount of instructions that the verifier can process in all branches, is that right? It doesn't look realistic that my program can run 1 million instructions in a single run, but it might be that if you take all possible flows and add up the instructions from these flows, it will exceed 1 million.The limitation of maximum 10 TCP options might be not enough, given that valid packets are permitted to include more than 10 NOPs. An alternative of using
bpf_load_hdr_optand calling it three times doesn't look good either, because it will be about three times slower than going over the options once. So maybe having a helper for that is better than trying to fit it into BPF?Quoted 8 lines
One more interesting fact is the time that it takes for the verifier to check my program. If it's limited to 10 iterations, it does it pretty fast, but if I try to increase the number to 11 iterations, it takes several minutes for the verifier to reach 1 million instructions and print the error then. I also tried grouping the NOPs in an inner loop to count only 10 real options, and the verifier has been running for a few hours without any response. Is it normal?
Quoted 3 lines
Maxim, this may expose a verifier bug. Do you have a reproducer I can access? I would like to debug this to see what is the root case. Thanks!
Thanks, I appreciate your help in debugging it. The reproducer is based on the modified XDP program from patch 10 in this series. You'll need to apply at least patches 6, 7, 8 from this series to get new BPF helpers needed for the XDP program (tell me if that's a problem, I can try to remove usage of new helpers, but it will affect the program length and may produce different results in the verifier).
Quoted 5 lines
See the C code of the program that passes the verifier (compiled with clang version 12.0.0-1ubuntu1) in the bottom of this email. If you increase the loop boundary from 10 to at least 11 in
cookie_init_timestamp_raw(), it fails the verifier after a few minutes.I tried to reproduce with latest llvm (llvm-project repo), loop boundary 10 is okay and 11 exceeds the 1M complexity limit. For 10,
the number of verified instructions is 563626 (more than 0.5M) so it is
totally possible that one more iteration just blows past the limit.Quoted 7 lines
Quoted 2 lines
So, does it mean that the verifying complexity grows exponentially with increasing the number of loop iterations (options parsed)?
Depending on verification time pruning results, it is possible slightly increase number of branches could result quite some (2x, 4x, etc.) of
to-be-verified dynamic instructions.Quoted 8 lines
Is it at least theoretically possible to make this coefficient below 2x? I.e. write a loop, so that adding another iteration will not double the number of verified instructions, but will have a smaller increase?
If that's not possible, then it looks like BPF can't have loops bigger than ~19 iterations (2^20 > 1M), and this function is not implementable in BPF.
This is the worst case. As I mentioned pruning plays a huge role in verification. Effective pruning can add little increase of dynamic instructions say from 19 iterations to 20 iterations. But we have to look at verifier log to find out whether pruning is less effective or something else... Based on my experience, in most cases, pruning is quite effective. But occasionally it is not... You can look at
verifier.cfile to roughly understand how pruning work.Not sure whether in this case it is due to less effective pruning or inherently we just have to go through all these dynamic instructions for verification.
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
Is it a good enough reason to keep this code as a BPF helper, rather than trying to fit it into the BPF program?
Another option is to use global function, which is verified separately from the main bpf program.
Simply removing
__always_inlinedidn't change anything. Do I need to make any other changes? Will it make sense to call a global function in a loop, i.e. will it increase chances to pass the verifier?Quoted 5 lines
global function cannot be static function. You can try either global function inside the loop or global function containing the loop. It probably more effective to put loops inside the global function. You have to do some experiments to see which one is better.
Sorry for a probably noob question, but how can I pass
data_endto a global function? I'm getting this error:Validating
cookie_init_timestamp_raw()func#1...
arg#4 reference type('UNKNOWN ') size cannot be determined: -22 processed 0 insns (limit 1000000)max_states_per_insn0total_states0peak_states0mark_read0When I removed
data_end, I got another one:; opcode = ptr[0];
969: (71) r8 = *(u8 *)(r0 +0)
R0=mem(id=0,ref_obj_id=0,off=20,imm=0)
R1=mem(id=0,ref_obj_id=0,off=0,umin_value=4,umax_value=60,var_off=(0x0; 0x3f),s32_min_value=0,s32_max_value=63,u32_max_value=63)
R2=invP0 R3=invP0 R4=mem_or_null(id=6,ref_obj_id=0,off=0,imm=0)
R5=invP0 R6=mem_or_null(id=5,ref_obj_id=0,off=0,imm=0) R7=mem(id=0,ref_obj_id=0,off=0,imm=0) R10=fp0 fp
-8=00000000 fp-16=invP15
invalid access to memory, mem_size=20 off=20 size=1
R0 min value is outside of the allowed memory range processed 20 insns (limit 1000000)max_states_per_insn0total_states2peak_states2mark_read1It looks like pointers to the context aren't supported:
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
test_global_func11- check that CTX pointer cannot be passedWhat is the standard way to pass packet data to a global function?
Since global function is separately verified, you need to pass the 'ctx' to the global function and do the '
data_end' check again in the global function. This will incur some packet re-parsing overhead similar to tail calls.
Now that the bpf_loop() helper landed, it's another option for doing repeated work. Please see [0].
[0] https://patchwork.kernel.org/project/netdevbpf/list/?series=587497&state=*
Quoted 14 lines
Quoted 14 lines
Thanks,
MaxQuoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
If you apply this tiny change, it fails the verifier after about 3 hours:
[...]
PATCH 10/10 bpf: Add sample for raw syncookie helpers
In reply to Maxim MikityanskiyThis commit adds a sample for the new BPF helpers: bpf_ct_lookup_tcp, bpf_tcp_raw_gen_syncookie and bpf_tcp_raw_check_syncookie.
samples/bpf/syncookie_kern.c is a BPF program that generates SYN cookies on allowed TCP ports and sends SYNACKs to clients, accelerating synproxy iptables module.
samples/bpf/syncookie_user.c is a userspace control application that allows to configure the following options in runtime: list of allowed ports, MSS, window scale, TTL.
samples/bpf/syncookie_test.sh is a script that demonstrates the setup of synproxy with XDP acceleration.
--- samples/bpf/.gitignore | 1 + samples/bpf/Makefile | 3 + samples/bpf/syncookie_kern.c | 591 ++++++++++++++++++++++++++++++++++ samples/bpf/syncookie_test.sh | 55 ++++ samples/bpf/syncookie_user.c | 388 ++++++++++++++++++++++ 5 files changed, 1038 insertions(+) create mode 100644 samples/bpf/syncookie_kern.c create mode 100755 samples/bpf/syncookie_test.sh create mode 100644 samples/bpf/syncookie_user.c
samples/bpf/.gitignore +1 -0
@@ -61,3 +61,4 @@ iperf.* /vmlinux.h /bpftool/ /libbpf/ +syncookie
samples/bpf/Makefile +3 -0
@@ -53,6 +53,7 @@ tprogs-y += task_fd_query tprogs-y += xdp_sample_pkts tprogs-y += ibumad tprogs-y += hbm +tprogs-y += syncookie tprogs-y += xdp_redirect_cpu tprogs-y += xdp_redirect_map_multi
@@ -118,6 +119,7 @@ task_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS) xdp_sample_pkts-objs := xdp_sample_pkts_user.o ibumad-objs := ibumad_user.o hbm-objs := hbm.o $(CGROUP_HELPERS) +syncookie-objs := syncookie_user.o xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o $(XDP_SAMPLE) xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o $(XDP_SAMPLE)
@@ -181,6 +183,7 @@ always-y += ibumad_kern.o always-y += hbm_out_kern.o always-y += hbm_edt_kern.o always-y += xdpsock_kern.o +always-y += syncookie_kern.o ifeq ($(ARCH), arm) # Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
samples/bpf/syncookie_kern.c new file +591 -0
591 lines not on this page · raw message
samples/bpf/syncookie_test.sh new file +55 -0
55 lines not on this page · raw message
samples/bpf/syncookie_user.c new file +388 -0
388 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH bpf-next 10/10] bpf: Add sample for raw syncookie helpers
Hi, just one comment related to the discussion on patch 7.
On Tue, Oct 19, 2021 at 7:49 AM Maxim Mikityanskiy <[email protected]> wrote:
<snip>
samples/bpf/syncookie_kern.c line 540
Quoted 12 lines
samples/bpf/syncookie_kern.c new file +12
-0+ + value =0 ;// Flags. + ct =bpf_ct_lookup_tcp (ctx, &tup, tup_size,BPF_F_CURRENT_NETNS , &value); +if (ct) { +unsigned long status = ct->status; + +bpf_ct_release (ct); +if (status &IPS_CONFIRMED_BIT ) +return XDP_PASS ; + }else if (value != -ENOENT ) { +return XDP_ABORTED ; + }
Is this the only reason that you wish to expose conntrack lookup functions to the API?
You should be able to find out whether the TCP session is established by doing a TCP socket lookup and checking sk->state.
Re: [PATCH bpf-next 10/10] bpf: Add sample for raw syncookie helpers
Quoted 22 lines · Joe Stringer
Hi, just one comment related to the discussion on patch 7.
On Tue, Oct 19, 2021 at 7:49 AM Maxim Mikityanskiy <[email protected]> wrote:
<snip>
samples/bpf/syncookie_kern.c line 540
Quoted 12 lines
samples/bpf/syncookie_kern.c new file +12
-0+ + value =0 ;// Flags. + ct =bpf_ct_lookup_tcp (ctx, &tup, tup_size,BPF_F_CURRENT_NETNS , &value); +if (ct) { +unsigned long status = ct->status; + +bpf_ct_release (ct); +if (status &IPS_CONFIRMED_BIT ) +return XDP_PASS ; + }else if (value != -ENOENT ) { +return XDP_ABORTED ; + }Is this the only reason that you wish to expose conntrack lookup functions to the API?
Quoted 2 lines
You should be able to find out whether the TCP session is established by doing a TCP socket lookup and checking
sk->state.
It's not possible to lookup a socket, because there is no socket. The traffic is forwarded through the firewall machine that runs synproxy and this XDP program.
Re: [PATCH bpf-next 10/10] bpf: Add sample for raw syncookie helpers
In reply to Maxim MikityanskiyQuoted 16 lines · Maxim Mikityanskiy
This commit adds a sample for the new BPF helpers:
bpf_ct_lookup_tcp,bpf_tcp_raw_gen_syncookieandbpf_tcp_raw_check_syncookie.
samples/bpf/syncookie_kern.cis a BPF program that generates SYN cookies on allowed TCP ports and sends SYNACKs to clients, accelerating synproxy iptables module.
samples/bpf/syncookie_user.cis a userspace control application that allows to configure the following options in runtime: list of allowed ports, MSS, window scale, TTL.
samples/bpf/syncookie_test.shis a script that demonstrates the setup of synproxy with XDP acceleration.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>
Quoted 10 lines
--- samples/bpf/.gitignore | 1 + samples/bpf/Makefile | 3 + samples/bpf/syncookie_kern.c | 591 ++++++++++++++++++++++++++++++++++ samples/bpf/syncookie_test.sh | 55 ++++ samples/bpf/syncookie_user.c | 388 ++++++++++++++++++++++ 5 files changed, 1038 insertions(+) create mode 100644 samples/bpf/syncookie_kern.c create mode 100755 samples/bpf/syncookie_test.sh create mode 100644 samples/bpf/syncookie_user.c
Tests should be in selftests/bpf.
Samples are for samples only.
Quoted 1 line
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
Isn't it deprecated?LICENSES/deprecated/Linux-OpenIB
samples/bpf/syncookie_kern.c line 114
Quoted 1 line
samples/bpf/syncookie_kern.c new file +1
-0+// Don't combine additions to avoid 32-bit overflow.
c++ style comment?
did you run checkpatch?
Re: [PATCH bpf-next 10/10] bpf: Add sample for raw syncookie helpers
Quoted 28 lines · Alexei Starovoitov
Quoted 23 lines · Maxim Mikityanskiy
This commit adds a sample for the new BPF helpers:
bpf_ct_lookup_tcp,bpf_tcp_raw_gen_syncookieandbpf_tcp_raw_check_syncookie.
samples/bpf/syncookie_kern.cis a BPF program that generates SYN cookies on allowed TCP ports and sends SYNACKs to clients, accelerating synproxy iptables module.
samples/bpf/syncookie_user.cis a userspace control application that allows to configure the following options in runtime: list of allowed ports, MSS, window scale, TTL.
samples/bpf/syncookie_test.shis a script that demonstrates the setup of synproxy with XDP acceleration.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>--- samples/bpf/.gitignore | 1 + samples/bpf/Makefile | 3 + samples/bpf/syncookie_kern.c | 591 ++++++++++++++++++++++++++++++++++ samples/bpf/syncookie_test.sh | 55 ++++ samples/bpf/syncookie_user.c | 388 ++++++++++++++++++++++ 5 files changed, 1038 insertions(+)Quoted 3 lines
create mode 100644
samples/bpf/syncookie_kern.ccreate mode 100755samples/bpf/syncookie_test.shcreate mode 100644samples/bpf/syncookie_user.c
Quoted 2 lines
Tests should be in selftests/bpf.
Samples are for samples only.
It's not a test, please don't be confused by the name of syncookie_test.sh - it's more like a demo script.
syncookie_user.c and syncookie_kern.c are 100% a sample, they show how to use the new helpers and are themselves a more or less feature-complete solution to protect from SYN flood. syncookie_test.sh should probably be named syncookie_demo.sh, it demonstrates how to bring pieces together.
These files aren't aimed to be a unit test for the new helpers, their purpose is to show the usage.
Quoted 5 lines
Quoted 2 lines
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
Isn't it deprecated?
LICENSES/deprecated/Linux-OpenIB
Honestly, I had no idea, I just used our template. I'll ask whoever is responsible for the license.
If it's deprecated, what should be used instead?
Quoted 5 lines
samples/bpf/syncookie_kern.c line 114
Quoted 2 lines
samples/bpf/syncookie_kern.c new file +1
-0+// Don't combine additions to avoid 32-bit overflow. c++ style comment?
did you run checkpatch?
Sure I did, and it doesn't complain on such comments. If such comments are a problem, please tell me, but I also saw them in other BPF samples.
Thanks for reviewing!
Re: [PATCH bpf-next 10/10] bpf: Add sample for raw syncookie helpers
Quoted 36 lines · Maxim Mikityanskiy
Quoted 28 lines · Alexei Starovoitov
Quoted 23 lines · Maxim Mikityanskiy
This commit adds a sample for the new BPF helpers:
bpf_ct_lookup_tcp,bpf_tcp_raw_gen_syncookieandbpf_tcp_raw_check_syncookie.
samples/bpf/syncookie_kern.cis a BPF program that generates SYN cookies on allowed TCP ports and sends SYNACKs to clients, accelerating synproxy iptables module.
samples/bpf/syncookie_user.cis a userspace control application that allows to configure the following options in runtime: list of allowed ports, MSS, window scale, TTL.
samples/bpf/syncookie_test.shis a script that demonstrates the setup of synproxy with XDP acceleration.Signed-off-by Maxim Mikityanskiy <[email protected]>Reviewed-by Tariq Toukan <[email protected]>--- samples/bpf/.gitignore | 1 + samples/bpf/Makefile | 3 + samples/bpf/syncookie_kern.c | 591 ++++++++++++++++++++++++++++++++++ samples/bpf/syncookie_test.sh | 55 ++++ samples/bpf/syncookie_user.c | 388 ++++++++++++++++++++++ 5 files changed, 1038 insertions(+)Quoted 3 lines
create mode 100644
samples/bpf/syncookie_kern.ccreate mode 100755samples/bpf/syncookie_test.shcreate mode 100644samples/bpf/syncookie_user.cQuoted 2 lines
Tests should be in selftests/bpf.
Samples are for samples only.It's not a test, please don't be confused by the name of
syncookie_test.sh- it's more like a demo script.
Quoted 8 lines
syncookie_user.candsyncookie_kern.care 100% a sample, they show how to use the new helpers and are themselves a more or less feature-complete solution to protect from SYN flood.syncookie_test.shshould probably be namedsyncookie_demo.sh, it demonstrates how to bring pieces together.These files aren't aimed to be a unit test for the new helpers, their purpose is to show the usage.
Please convert it to a selftest.
Sooner or later we will convert all samples/bpf into tests and delete that dir.