bpf: add BPF_LWT_ENCAP_IP option to bpf_lwt_push_encap
Subsystems (7)
PATCH 1/2 bpf: add BPF_LWT_ENCAP_IP option to bpf_lwt_push_encap
This patch enables BPF programs (specifically, of LWT_XMIT type) to add IP encapsulation headers to packets (e.g. IP/GRE, GUE, IPIP).
This is useful when thousands of different short-lived flows should be encapped, each with different and dynamically determined destination. Although lwtunnels can be used in some of these scenarios, the ability to dynamically generate encap headers adds more flexibility, e.g. when routing depends on the state of the host (reflected in global bpf maps).
A future patch will enable IPv6 encapping (and IPv4/IPv6 cross-routing).
Tested: see the second patch in the series.
--- include/net/lwtunnel.h | 2 ++ include/uapi/linux/bpf.h | 7 ++++- net/core/filter.c | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-)
include/net/lwtunnel.h +2 -0
@@ -16,6 +16,8 @@#define LWTUNNEL_STATE_INPUT_REDIRECT BIT(1)#define LWTUNNEL_STATE_XMIT_REDIRECT BIT(2) +#define LWTUNNEL_MAX_ENCAP_HSIZE 80 +enum {LWTUNNEL_XMIT_DONE ,LWTUNNEL_XMIT_CONTINUE ,
include/uapi/linux/bpf.h +6 -1
@@ -1998,6 +1998,10 @@ union bpf_attr {* Only works if *skb* contains an IPv6 packet. Insert a * Segment Routing Header (**struct ipv6_sr_hdr**) inside * the IPv6 header. +* **BPF_LWT_ENCAP_IP** +* IP encapsulation (GRE/GUE/IPIP/etc). The outer header +* must be IPv4, followed by zero, one, or more additional +* headers. * * A call to this helper is susceptible to change the underlaying * packet buffer. Therefore, at load time, all checks on pointers
@@ -2444,7 +2448,8 @@ enum bpf_hdr_start_off {/* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */ enum bpf_lwt_encap_mode {BPF_LWT_ENCAP_SEG6 , -BPF_LWT_ENCAP_SEG6_INLINE +BPF_LWT_ENCAP_SEG6_INLINE , +BPF_LWT_ENCAP_IP , };/* user accessible mirror of in-kernel sk_buff.
net/core/filter.c +58 -0
76 lines not on this page · raw message
Signature · 1 line
2.20.0.rc0.387.gc7a69e6b6c-goog
PATCH 2/2 selftests/bpf: add test_lwt_ip_encap selftest
This patch adds a sample/selftest that covers BPF_LWT_ENCAP_IP option added in the first patch in the series.
--- tools/testing/selftests/bpf/Makefile | 5 +- .../testing/selftests/bpf/test_lwt_ip_encap.c | 65 ++++++++++ .../selftests/bpf/test_lwt_ip_encap.sh | 114 ++++++++++++++++++ 3 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_lwt_ip_encap.c create mode 100755 tools/testing/selftests/bpf/test_lwt_ip_encap.sh
tools/testing/selftests/bpf/Makefile +3 -2
@@ -39,7 +39,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \ test_skb_cgroup_id_kern.o bpf_flow.o netcnt_prog.o \ test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o \ - xdp_dummy.o test_map_in_map.o + xdp_dummy.o test_map_in_map.o test_lwt_ip_encap.o # Order correspond to 'make run_tests' order TEST_PROGS := test_kmod.sh \
@@ -53,7 +53,8 @@ TEST_PROGS := test_kmod.sh \ test_lirc_mode2.sh \ test_skb_cgroup_id.sh \ test_flow_dissector.sh \ - test_xdp_vlan.sh + test_xdp_vlan.sh \ + test_lwt_ip_encap.sh TEST_PROGS_EXTENDED := with_addr.sh
tools/testing/selftests/bpf/test_lwt_ip_encap.c new file +65 -0
65 lines not on this page · raw message
tools/testing/selftests/bpf/test_lwt_ip_encap.sh new file +114 -0
114 lines not on this page · raw message
Signature · 1 line
2.20.0.rc0.387.gc7a69e6b6c-goog
Quoted 22 lines · Peter Oskolkov
net/core/filter.c +14
-0@@ -4793,6 +4793,60 @@ static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len }#endif /* CONFIG_IPV6_SEG6_BPF */ +static int bpf_push_ip_encap (struct sk_buff *skb,void *hdr,u32 len) +{ +struct dst_entry *dst; +struct rtable *rt; +struct iphdr *iph; +struct net *net; +int err; + +if (skb->protocol !=htons (ETH_P_IP )) +return -EINVAL ;/* ETH_P_IPV6 not yet supported */ + + iph = (struct iphdr *)hdr; + +if (unlikely (len <sizeof (struct iphdr ) || len >LWTUNNEL_MAX_ENCAP_HSIZE ))
net/core/filter.c static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len · line 4819
Quoted 10 lines
net/core/filter.c +10
-0+return -EINVAL ; +if (unlikely (iph->version !=4 || iph->ihl *4 > len)) +return -EINVAL ; + +if (skb->sk) + net =sock_net (skb->sk); +else { + net =dev_net (skb_dst (skb)->dev); + } + rt =ip_route_output (net, iph->daddr,0 ,0 ,0 );
That is a very limited use case. e.g., oif = 0 means you are not considering any kind of policy routing (e.g., VRF).
Quoted 29 lines · David Ahern
Quoted 27 lines · Peter Oskolkov
net/core/filter.c +19
-0@@ -4793,6 +4793,60 @@ static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len }#endif /* CONFIG_IPV6_SEG6_BPF */ +static int bpf_push_ip_encap (struct sk_buff *skb,void *hdr,u32 len) +{ +struct dst_entry *dst; +struct rtable *rt; +struct iphdr *iph; +struct net *net; +int err; + +if (skb->protocol !=htons (ETH_P_IP )) +return -EINVAL ;/* ETH_P_IPV6 not yet supported */ + + iph = (struct iphdr *)hdr; + +if (unlikely (len <sizeof (struct iphdr ) || len >LWTUNNEL_MAX_ENCAP_HSIZE )) +return -EINVAL ; +if (unlikely (iph->version !=4 || iph->ihl *4 > len)) +return -EINVAL ; + +if (skb->sk)
Quoted 8 lines
net/core/filter.c static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len · line 4819
Quoted 5 lines
net/core/filter.c +5
-0+ net =sock_net (skb->sk); + else { + net =dev_net (skb_dst (skb)->dev); + } + rt =ip_route_output (net, iph->daddr,0 ,0 ,0 );That is a very limited use case. e.g., oif = 0 means you are not considering any kind of policy routing (e.g., VRF).
Hi David! Could you be a bit more specific re: what you would like to see here? Thanks!
Quoted 39 lines · Peter Oskolkov
Quoted 29 lines · David Ahern
Quoted 27 lines · Peter Oskolkov
net/core/filter.c +19
-0@@ -4793,6 +4793,60 @@ static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len }#endif /* CONFIG_IPV6_SEG6_BPF */ +static int bpf_push_ip_encap (struct sk_buff *skb,void *hdr,u32 len) +{ +struct dst_entry *dst; +struct rtable *rt; +struct iphdr *iph; +struct net *net; +int err; + +if (skb->protocol !=htons (ETH_P_IP )) +return -EINVAL ;/* ETH_P_IPV6 not yet supported */ + + iph = (struct iphdr *)hdr; + +if (unlikely (len <sizeof (struct iphdr ) || len >LWTUNNEL_MAX_ENCAP_HSIZE )) +return -EINVAL ; +if (unlikely (iph->version !=4 || iph->ihl *4 > len)) +return -EINVAL ; + +if (skb->sk)Quoted 8 lines
net/core/filter.c static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len · line 4819
Quoted 5 lines
net/core/filter.c +5
-0+ net =sock_net (skb->sk); + else { + net =dev_net (skb_dst (skb)->dev); + } + rt =ip_route_output (net, iph->daddr,0 ,0 ,0 );That is a very limited use case. e.g., oif = 0 means you are not considering any kind of policy routing (e.g., VRF).
Quoted 3 lines
Hi David! Could you be a bit more specific re: what you would like to see here? Thanks!
Is the encap happening on ingress or egress? Seems like the current code does not assume either direction for lwt (BPF_PROG_TYPE_LWT_IN vs BPF_PROG_TYPE_LWT_OUT), yet your change does - output only. Basically, you should be filling in a flow struct and doing a proper lookup.
When the potentially custom encap header is pushed on, seems to me skb marks should still be honored for the route lookup. If not, you should handle that in the API.
Quoted 1 line
From there
skb->devat a minimum should be used as either iif (ingress)
or oif (egress).
The iph is already set so you have quick access to the tos.
Also, this should implement IPv6 as well before going in.
Thanks, David! This is for egress only, so I'll add an appropriate check. I'll also address your other comments/concerns in a v2 version of this patchset.
Quoted 60 lines · David Ahern
Quoted 39 lines · Peter Oskolkov
Quoted 29 lines · David Ahern
Quoted 27 lines · Peter Oskolkov
net/core/filter.c +19
-0@@ -4793,6 +4793,60 @@ static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len }#endif /* CONFIG_IPV6_SEG6_BPF */ +static int bpf_push_ip_encap (struct sk_buff *skb,void *hdr,u32 len) +{ +struct dst_entry *dst; +struct rtable *rt; +struct iphdr *iph; +struct net *net; +int err; + +if (skb->protocol !=htons (ETH_P_IP )) +return -EINVAL ;/* ETH_P_IPV6 not yet supported */ + + iph = (struct iphdr *)hdr; + +if (unlikely (len <sizeof (struct iphdr ) || len >LWTUNNEL_MAX_ENCAP_HSIZE )) +return -EINVAL ; +if (unlikely (iph->version !=4 || iph->ihl *4 > len)) +return -EINVAL ; + +if (skb->sk)Quoted 8 lines
net/core/filter.c static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len · line 4819
Quoted 5 lines
net/core/filter.c +5
-0+ net =sock_net (skb->sk); + else { + net =dev_net (skb_dst (skb)->dev); + } + rt =ip_route_output (net, iph->daddr,0 ,0 ,0 );That is a very limited use case. e.g., oif = 0 means you are not considering any kind of policy routing (e.g., VRF).
Quoted 3 lines
Hi David! Could you be a bit more specific re: what you would like to see here? Thanks!
Is the encap happening on ingress or egress? Seems like the current code does not assume either direction for lwt (
BPF_PROG_TYPE_LWT_INvsBPF_PROG_TYPE_LWT_OUT), yet your change does - output only. Basically, you should be filling in a flow struct and doing a proper lookup.When the potentially custom encap header is pushed on, seems to me skb marks should still be honored for the route lookup. If not, you should handle that in the API.
From there
skb->devat a minimum should be used as either iif (ingress) or oif (egress).The iph is already set so you have quick access to the tos.
Also, this should implement IPv6 as well before going in.
Quoted 3 lines · Peter Oskolkov
Thanks, David! This is for egress only, so I'll add an appropriate check. I'll also address your other comments/concerns in a v2 version of this patchset.
Why are you limiting this to egress only?
Quoted 7 lines · David Ahern
Quoted 3 lines · Peter Oskolkov
Thanks, David! This is for egress only, so I'll add an appropriate check. I'll also address your other comments/concerns in a v2 version of this patchset.
Why are you limiting this to egress only?
I'm focusing on egress because this is a use case that we have and understand well, and would like to have a solution for sooner rather than later.
Without understanding why anybody would want to do lwt-bpf encap on ingress, I don't know, for example, what a good test would look like; but I'd be happy to look into a specific ingress use case if you have one.
Quoted 9 lines · Peter Oskolkov
Quoted 7 lines · David Ahern
Quoted 3 lines · Peter Oskolkov
Thanks, David! This is for egress only, so I'll add an appropriate check. I'll also address your other comments/concerns in a v2 version of this patchset.
Why are you limiting this to egress only?
Quoted 9 lines
I'm focusing on egress because this is a use case that we have and understand well, and would like to have a solution for sooner rather than later.
Without understanding why anybody would want to do lwt-bpf encap on ingress, I don't know, for example, what a good test would look like; but I'd be happy to look into a specific ingress use case if you have one.
We can not have proliferation of helpers for a lot of one off use cases. A little thought now makes this helper useful for more than just your 1 use case. And, IPv6 parity should be a minimal requirement for helpers.
Based on your route lookup I presume your use case is capturing certain local traffic, pushing a custom header and sending that packet else where. The same could be done on the ingress path.
Our case is a bit different - it is more like using the SRH header in IPv6 to route packets via a non-default intermediate hop. But I see your point - I'll expand the patchset to cover IPv6 and the ingress path.
Quoted 28 lines · David Ahern
Quoted 9 lines · Peter Oskolkov
Quoted 7 lines · David Ahern
Quoted 3 lines · Peter Oskolkov
Thanks, David! This is for egress only, so I'll add an appropriate check. I'll also address your other comments/concerns in a v2 version of this patchset.
Why are you limiting this to egress only?
Quoted 9 lines
I'm focusing on egress because this is a use case that we have and understand well, and would like to have a solution for sooner rather than later.
Without understanding why anybody would want to do lwt-bpf encap on ingress, I don't know, for example, what a good test would look like; but I'd be happy to look into a specific ingress use case if you have one.
We can not have proliferation of helpers for a lot of one off use cases. A little thought now makes this helper useful for more than just your 1 use case. And, IPv6 parity should be a minimal requirement for helpers.
Based on your route lookup I presume your use case is capturing certain local traffic, pushing a custom header and sending that packet else where. The same could be done on the ingress path.