bpf: add BPF_LWT_ENCAP_IP option to bpf_lwt_push_encap
Subsystems (7)
v3 cover bpf: add BPF_LWT_ENCAP_IP option to bpf_lwt_push_encap
This patchset implements BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap BPF helper. It enables BPF programs (specifically, BPF_PROG_TYPE_LWT_IN and BPF_PROG_TYPE_LWT_XMIT prog types) 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).
V2 changes: Added flowi-based route lookup, IPv6 encapping, and encapping on ingress.
V3 changes: incorporated David Ahern's suggestions:
- added l3mdev check/oif (patch 2)
- sync bpf.h from include/uapi into tools/include/uapi
- selftest tweaks
Peter Oskolkov (4):
bpf: add plumbing for BPF_LWT_ENCAP_IP in bpf_lwt_push_encap
bpf: implement BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap
bpf: sync <kdir>/<uapi>/bpf.h with tools/<uapi>/bpf.h
selftests: bpf: add test_lwt_ip_encap selftest
include/net/lwtunnel.h | 3 + include/uapi/linux/bpf.h | 23 +- net/core/filter.c | 47 ++- net/core/lwt_bpf.c | 147 +++++++++ tools/include/uapi/linux/bpf.h | 23 +- tools/testing/selftests/bpf/Makefile | 5 +- .../testing/selftests/bpf/test_lwt_ip_encap.c | 84 +++++ .../selftests/bpf/test_lwt_ip_encap.sh | 311 ++++++++++++++++++ 8 files changed, 632 insertions(+), 11 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
Signature · 1 line
2.20.1.495.gaa96b0ce6b-goog
PATCH v3 1/4 bpf: add plumbing for BPF_LWT_ENCAP_IP in bpf_lwt_push_encap
This patch adds all needed plumbing in preparation to allowing bpf programs to do IP encapping via bpf_lwt_push_encap. Actual implementation is added in the next patch in the patchset.
Of note:
- bpf_lwt_push_encap can now be called from BPF_PROG_TYPE_LWT_XMIT prog types in addition to BPF_PROG_TYPE_LWT_IN;
- as route lookups are different for ingress vs egress, the single external bpf_lwt_push_encap BPF helper is routed internally to either bpf_lwt_in_push_encap or bpf_lwt_xmit_push_encap BPF_CALLs, depending on prog type.
--- include/uapi/linux/bpf.h | 23 ++++++++++++++++++-- net/core/filter.c | 46 +++++++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 7 deletions(-)
include/uapi/linux/bpf.h +21 -2
@@ -2015,6 +2015,16 @@ 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 or IPv6, followed by zero or more +* additional headers, up to LWT_BPF_MAX_HEADROOM total +* bytes in all prepended headers. +* +* BPF_LWT_ENCAP_SEG6*** types can be called by bpf programs of +* type BPF_PROG_TYPE_LWT_IN; BPF_LWT_ENCAP_IP type can be called +* by bpf programs of types BPF_PROG_TYPE_LWT_IN and +* BPF_PROG_TYPE_LWT_XMIT. * * A call to this helper is susceptible to change the underlaying * packet buffer. Therefore, at load time, all checks on pointers
@@ -2495,7 +2505,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 , };#define __bpf_md_ptr (type, name) \
@@ -2583,7 +2594,15 @@ enum bpf_ret_code {BPF_DROP =2 ,/* 3-6 reserved */ BPF_REDIRECT =7 , - /* >127 are reserved for prog type specific return codes */ +/* >127 are reserved for prog type specific return codes. +* +* BPF_LWT_REROUTE: used by BPF_PROG_TYPE_LWT_IN and +* BPF_PROG_TYPE_LWT_XMIT to indicate that skb's dst +* has changed and appropriate dst_input() or dst_output() +* action has to be taken (this is an L3 redirect, as +* opposed to L2 redirect represented by BPF_REDIRECT above). +*/ + BPF_LWT_REROUTE = 128, }; struct bpf_sock {
net/core/filter.c +41 -5
82 lines not on this page · raw message
Signature · 1 line
2.20.1.495.gaa96b0ce6b-goog
PATCH v3 2/4 bpf: implement BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap
In reply to Peter OskolkovThis patch implements BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap BPF helper. It enables BPF programs (specifically, BPF_PROG_TYPE_LWT_IN and BPF_PROG_TYPE_LWT_XMIT prog types) 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).
--- include/net/lwtunnel.h | 3 + net/core/filter.c | 3 +- net/core/lwt_bpf.c | 148 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 1 deletion(-)
include/net/lwtunnel.h +3 -0
@@ -126,6 +126,8 @@ int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b);int lwtunnel_output (struct net *net,struct sock *sk,struct sk_buff *skb);int lwtunnel_input (struct sk_buff *skb);int lwtunnel_xmit (struct sk_buff *skb); +int bpf_lwt_push_ip_encap (struct sk_buff *skb,void *hdr,u32 len, +bool ingress);static inline void lwtunnel_set_redirect (struct dst_entry *dst) {
@@ -138,6 +140,7 @@ static inline void lwtunnel_set_redirect(struct dst_entry *dst) dst->input = lwtunnel_input; } } +#else static inline void lwtstate_free (struct lwtunnel_state *lws)
net/core/filter.c +2 -1
@@ -73,6 +73,7 @@#include <linux/seg6_local.h> #include <net/seg6.h> #include <net/seg6_local.h> +#include <net/lwtunnel.h> /** * sk_filter_trim_cap - run a packet through a socket filter
@@ -4796,7 +4797,7 @@ static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 lenstatic int bpf_push_ip_encap (struct sk_buff *skb,void *hdr,u32 len,bool ingress) { -return -EINVAL ;/* Implemented in the next patch. */ +return bpf_lwt_push_ip_encap (skb, hdr, len, ingress); } BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
net/core/lwt_bpf.c +148 -0
178 lines not on this page · raw message
Signature · 1 line
2.20.1.495.gaa96b0ce6b-goog
PATCH v3 3/4 bpf: sync <kdir>/<uapi>/bpf.h with tools/<uapi>/bpf.h
In reply to Peter OskolkovThis patch copies changes in bpf.h done by a previous patch in this patchset from the kernel uapi include dir into tools uapi include dir.
--- tools/include/uapi/linux/bpf.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
tools/include/uapi/linux/bpf.h +21 -2
@@ -2015,6 +2015,16 @@ 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 or IPv6, followed by zero or more +* additional headers, up to LWT_BPF_MAX_HEADROOM total +* bytes in all prepended headers. +* +* BPF_LWT_ENCAP_SEG6*** types can be called by bpf programs of +* type BPF_PROG_TYPE_LWT_IN; BPF_LWT_ENCAP_IP type can be called +* by bpf programs of types BPF_PROG_TYPE_LWT_IN and +* BPF_PROG_TYPE_LWT_XMIT. * * A call to this helper is susceptible to change the underlaying * packet buffer. Therefore, at load time, all checks on pointers
@@ -2495,7 +2505,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 , };#define __bpf_md_ptr (type, name) \
@@ -2583,7 +2594,15 @@ enum bpf_ret_code {BPF_DROP =2 ,/* 3-6 reserved */ BPF_REDIRECT =7 , - /* >127 are reserved for prog type specific return codes */ +/* >127 are reserved for prog type specific return codes. +* +* BPF_LWT_REROUTE: used by BPF_PROG_TYPE_LWT_IN and +* BPF_PROG_TYPE_LWT_XMIT to indicate that skb's dst +* has changed and appropriate dst_input() or dst_output() +* action has to be taken (this is an L3 redirect, as +* opposed to L2 redirect represented by BPF_REDIRECT above). +*/ + BPF_LWT_REROUTE = 128, }; struct bpf_sock {
Signature · 1 line
2.20.1.495.gaa96b0ce6b-goog
PATCH v3 4/4 selftests: bpf: add test_lwt_ip_encap selftest
In reply to Peter OskolkovThis patch adds a bpf self-test to cover BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap.
Covered:
- encapping in LWT_IN and LWT_XMIT
- IPv4 and IPv6
--- tools/testing/selftests/bpf/Makefile | 5 +- .../testing/selftests/bpf/test_lwt_ip_encap.c | 84 +++++ .../selftests/bpf/test_lwt_ip_encap.sh | 311 ++++++++++++++++++ 3 files changed, 398 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
@@ -35,7 +35,7 @@ BPF_OBJ_FILES = \ sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \ 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_xdp_vlan.o \ - xdp_dummy.o test_map_in_map.o + xdp_dummy.o test_map_in_map.o test_lwt_ip_encap.o # Objects are built with default compilation flags and with sub-register # code-gen enabled.
@@ -73,7 +73,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 \ tcp_client.py \
tools/testing/selftests/bpf/test_lwt_ip_encap.c new file +84 -0
84 lines not on this page · raw message
tools/testing/selftests/bpf/test_lwt_ip_encap.sh new file +311 -0
311 lines not on this page · raw message
Signature · 1 line
2.20.1.495.gaa96b0ce6b-goog
Re: [PATCH bpf-next v3 1/4] bpf: add plumbing for BPF_LWT_ENCAP_IP in bpf_lwt_push_encap
In reply to Peter OskolkovOn 1/28/19 6:12 PM, Peter Oskolkov wrote
Quoted 5 lines
include/uapi/linux/bpf.h +0
-1@@ -2583,7 +2594,15 @@ enum bpf_ret_code {BPF_DROP =2 ,/* 3-6 reserved */ BPF_REDIRECT = 7, -/* >127 are reserved for prog type specific return codes */
include/uapi/linux/bpf.h enum bpf_ret_code { · line 2606
Quoted 10 lines
include/uapi/linux/bpf.h +9
-0+/* >127 are reserved for prog type specific return codes. +* +* BPF_LWT_REROUTE: used by BPF_PROG_TYPE_LWT_IN and +* BPF_PROG_TYPE_LWT_XMIT to indicate that skb's dst +* has changed and appropriate dst_input() or dst_output() +* action has to be taken (this is an L3 redirect, as +* opposed to L2 redirect represented by BPF_REDIRECT above). +*/ + BPF_LWT_REROUTE = 128, };
What happens if a program pushes a new header onto the skb and does not return BPF_LWT_REROUTE?
Might be better to move the route lookup and dst swap to run_lwt_bpf and only do it if the program returns BPF_LWT_REROUTE. That allows calling bpf_push_ip_encap without requiring a route lookup. That might be fine as long as their is not a protocol mismatch (ipv4 packet gets an ipv6 header or vice versa). But then, I think you have the mismatch problem now if the program does not return BPF_LWT_REROUTE.
Re: [PATCH bpf-next v3 1/4] bpf: add plumbing for BPF_LWT_ENCAP_IP in bpf_lwt_push_encap
Quoted 21 lines · David Ahern
On 1/28/19 6:12 PM, Peter Oskolkov wrote
Quoted 5 lines
include/uapi/linux/bpf.h +0
-1@@ -2583,7 +2594,15 @@ enum bpf_ret_code {BPF_DROP =2 ,/* 3-6 reserved */ BPF_REDIRECT = 7, -/* >127 are reserved for prog type specific return codes */ include/uapi/linux/bpf.h enum bpf_ret_code { · line 2606
Quoted 10 lines
include/uapi/linux/bpf.h +9
-0+/* >127 are reserved for prog type specific return codes. +* +* BPF_LWT_REROUTE: used by BPF_PROG_TYPE_LWT_IN and +* BPF_PROG_TYPE_LWT_XMIT to indicate that skb's dst +* has changed and appropriate dst_input() or dst_output() +* action has to be taken (this is an L3 redirect, as +* opposed to L2 redirect represented by BPF_REDIRECT above). +*/ + BPF_LWT_REROUTE = 128, };What happens if a program pushes a new header onto the skb and does not return
BPF_LWT_REROUTE?
Quoted 6 lines
Might be better to move the route lookup and dst swap to
run_lwt_bpfand only do it if the program returnsBPF_LWT_REROUTE. That allows callingbpf_push_ip_encapwithout requiring a route lookup. That might be fine as long as their is not a protocol mismatch (ipv4 packet gets an ipv6 header or vice versa). But then, I think you have the mismatch problem now if the program does not returnBPF_LWT_REROUTE.
Makes sense - thanks for the suggestion I'll send a v4 later today.
Thanks,
Peter