net: netfilter: Make ct zone opts configurable for bpf ct helpers
Subsystems (6)
PATCH v3 1/2 net: netfilter: Make ct zone opts configurable for bpf ct helpers
Add ct zone id and direction to bpf_ct_opts so that arbitrary ct zones can be used for xdp/tc bpf ct helper functions bpf_{xdp,skb}_ct_alloc and bpf_{xdp,skb}_ct_lookup.
---
v2 -> v3:
- Remove whitespace changes
- Add reserved padding options
- If ct_zone_id is set when opts__sz isn't 16 return -EINVAL
- Remove ct_zone_flags, not used by nf_conntrack_alloc or nf_conntrack_find_get
- Update comments to reflect opts__sz can be 16 or 12
v1 -> v2:
- Make ct zone flags/dir configurable
--- net/netfilter/nf_conntrack_bpf.c | 68 ++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 13 deletions(-)
net/netfilter/nf_conntrack_bpf.c +55 -13
@@ -32,7 +32,9 @@* -EINVAL - Passed NULL for bpf_tuple pointer * -EINVAL - opts->reserved is not 0 * -EINVAL - netns_id is less than -1 -* -EINVAL - opts__sz isn't NF_BPF_CT_OPTS_SZ (12) +* -EINVAL - opts__sz isn't NF_BPF_CT_OPTS_SZ (16) or 12 +* -EINVAL - opts->ct_zone_id set when +opts__sz isn't NF_BPF_CT_OPTS_SZ (16) * -EPROTO - l4proto isn't one of IPPROTO_TCP or IPPROTO_UDP * -ENONET - No network namespace found for netns_id * -ENOENT - Conntrack lookup could not find entry for tuple
@@ -42,6 +44,8 @@* Values: * IPPROTO_TCP, IPPROTO_UDP * @dir: - connection tracking tuple direction. +* @ct_zone_id - connection tracking zone id. +* @ct_zone_dir - connection tracking zone direction. * @reserved - Reserved member, will be reused for more options in future * Values: * 0
@@ -51,11 +55,13 @@ struct bpf_ct_opts {s32 error;u8 l4proto;u8 dir; -u8 reserved[2 ]; +u16 ct_zone_id; +u8 ct_zone_dir; +u8 reserved[3 ]; };enum { -NF_BPF_CT_OPTS_SZ =12 , +NF_BPF_CT_OPTS_SZ =16 , }; static int bpf_nf_ct_tuple_parse(struct bpf_sock_tuple *bpf_tuple,
@@ -104,12 +110,21 @@ __bpf_nf_ct_alloc_entry(struct net *net, struct bpf_sock_tuple *bpf_tuple, u32 timeout) {struct nf_conntrack_tuple otuple, rtuple; +struct nf_conntrack_zone ct_zone;struct nf_conn *ct;int err; -if (!opts || !bpf_tuple || opts->reserved[0 ] || opts->reserved[1 ] || - opts_len !=NF_BPF_CT_OPTS_SZ ) +if (!opts || !bpf_tuple)return ERR_PTR (-EINVAL ); +if (!(opts_len ==NF_BPF_CT_OPTS_SZ || opts_len ==12 )) +return ERR_PTR (-EINVAL ); +if (opts_len ==NF_BPF_CT_OPTS_SZ ) { +if (opts->reserved[0 ] || opts->reserved[1 ] || opts->reserved[2 ]) +return ERR_PTR (-EINVAL ); + }else { +if (opts->ct_zone_id) +return ERR_PTR (-EINVAL ); + }if (unlikely (opts->netns_id <BPF_F_CURRENT_NETNS ))return ERR_PTR (-EINVAL );
@@ -130,7 +145,16 @@ __bpf_nf_ct_alloc_entry(struct net *net, struct bpf_sock_tuple *bpf_tuple,return ERR_PTR (-ENONET ); } - ct =nf_conntrack_alloc (net, &nf_ct_zone_dflt, &otuple, &rtuple, +if (opts_len ==NF_BPF_CT_OPTS_SZ ) { +if (opts->ct_zone_dir ==0 ) + opts->ct_zone_dir =NF_CT_DEFAULT_ZONE_DIR ; +nf_ct_zone_init (&ct_zone, + opts->ct_zone_id, opts->ct_zone_dir,0 ); + }else { + ct_zone = nf_ct_zone_dflt; + } + + ct =nf_conntrack_alloc (net, &ct_zone, &otuple, &rtuple,GFP_ATOMIC );if (IS_ERR (ct)) goto out;
@@ -152,12 +176,21 @@ static struct nf_conn *__bpf_nf_ct_lookup(struct net *net, {struct nf_conntrack_tuple_hash *hash;struct nf_conntrack_tuple tuple; +struct nf_conntrack_zone ct_zone;struct nf_conn *ct;int err; -if (!opts || !bpf_tuple || opts->reserved[0 ] || opts->reserved[1 ] || - opts_len !=NF_BPF_CT_OPTS_SZ ) +if (!opts || !bpf_tuple)return ERR_PTR (-EINVAL ); +if (!(opts_len ==NF_BPF_CT_OPTS_SZ || opts_len ==12 )) +return ERR_PTR (-EINVAL ); +if (opts_len ==NF_BPF_CT_OPTS_SZ ) { +if (opts->reserved[0 ] || opts->reserved[1 ] || opts->reserved[2 ]) +return ERR_PTR (-EINVAL ); + }else { +if (opts->ct_zone_id) +return ERR_PTR (-EINVAL ); + }if (unlikely (opts->l4proto !=IPPROTO_TCP && opts->l4proto !=IPPROTO_UDP ))return ERR_PTR (-EPROTO );if (unlikely (opts->netns_id <BPF_F_CURRENT_NETNS ))
@@ -174,7 +207,16 @@ static struct nf_conn *__bpf_nf_ct_lookup(struct net *net,return ERR_PTR (-ENONET ); } - hash =nf_conntrack_find_get (net, &nf_ct_zone_dflt, &tuple); +if (opts_len ==NF_BPF_CT_OPTS_SZ ) { +if (opts->ct_zone_dir ==0 ) + opts->ct_zone_dir =NF_CT_DEFAULT_ZONE_DIR ; +nf_ct_zone_init (&ct_zone, + opts->ct_zone_id, opts->ct_zone_dir,0 ); + }else { + ct_zone = nf_ct_zone_dflt; + } + + hash =nf_conntrack_find_get (net, &ct_zone, &tuple);if (opts->netns_id >=0 )put_net (net);if (!hash)
@@ -245,7 +287,7 @@ __bpf_kfunc_start_defs();* @opts - Additional options for allocation (documented above) * Cannot be NULL * @opts__sz - Length of the bpf_ct_opts structure -* Must be NF_BPF_CT_OPTS_SZ (12) +* Must be NF_BPF_CT_OPTS_SZ (16) or 12 */ __bpf_kfunc struct nf_conn___init * bpf_xdp_ct_alloc(struct xdp_md *xdp_ctx, struct bpf_sock_tuple *bpf_tuple,
@@ -279,7 +321,7 @@ bpf_xdp_ct_alloc(struct xdp_md *xdp_ctx, struct bpf_sock_tuple *bpf_tuple,* @opts - Additional options for lookup (documented above) * Cannot be NULL * @opts__sz - Length of the bpf_ct_opts structure -* Must be NF_BPF_CT_OPTS_SZ (12) +* Must be NF_BPF_CT_OPTS_SZ (16) or 12 */ __bpf_kfunc struct nf_conn * bpf_xdp_ct_lookup(struct xdp_md *xdp_ctx, struct bpf_sock_tuple *bpf_tuple,
@@ -312,7 +354,7 @@ bpf_xdp_ct_lookup(struct xdp_md *xdp_ctx, struct bpf_sock_tuple *bpf_tuple,* @opts - Additional options for allocation (documented above) * Cannot be NULL * @opts__sz - Length of the bpf_ct_opts structure -* Must be NF_BPF_CT_OPTS_SZ (12) +* Must be NF_BPF_CT_OPTS_SZ (16) or 12 */ __bpf_kfunc struct nf_conn___init * bpf_skb_ct_alloc(struct __sk_buff *skb_ctx, struct bpf_sock_tuple *bpf_tuple,
@@ -347,7 +389,7 @@ bpf_skb_ct_alloc(struct __sk_buff *skb_ctx, struct bpf_sock_tuple *bpf_tuple,* @opts - Additional options for lookup (documented above) * Cannot be NULL * @opts__sz - Length of the bpf_ct_opts structure -* Must be NF_BPF_CT_OPTS_SZ (12) +* Must be NF_BPF_CT_OPTS_SZ (16) or 12 */ __bpf_kfunc struct nf_conn * bpf_skb_ct_lookup(struct __sk_buff *skb_ctx, struct bpf_sock_tuple *bpf_tuple,
Signature · 1 line
2.34.1
PATCH v3 2/2 selftests/bpf: Update tests for new ct zone opts for nf_conntrack kfuncs
Add test for allocating and looking up ct entry in a non-default ct zone with kfuncs bpf_{xdp,skb}_ct_alloc and bpf_{xdp,skb}_ct_lookup.
Add negative tests for looking up ct entry in a different ct zone to where it was allocated and with a different direction.
---
v2 -> v3:
- Test both old and new bpf_ct_opts struct definitions
- Restore test for reserved options
- Add test for ct_zone_dir
v1 -> v2:
- Separate test changes into different patch
- Add test for allocating/looking up entry in non-default ct zone
--- tools/testing/selftests/bpf/config | 1 + .../testing/selftests/bpf/prog_tests/bpf_nf.c | 6 + .../testing/selftests/bpf/progs/test_bpf_nf.c | 117 ++++++++++++++++-- 3 files changed, 114 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/config +1 -0
@@ -80,6 +80,7 @@ CONFIG_NETFILTER_XT_TARGET_CT=y CONFIG_NETKIT=y CONFIG_NF_CONNTRACK=y CONFIG_NF_CONNTRACK_MARK=y +CONFIG_NF_CONNTRACK_ZONES=y CONFIG_NF_DEFRAG_IPV4=y CONFIG_NF_DEFRAG_IPV6=y CONFIG_NF_NAT=y
tools/testing/selftests/bpf/prog_tests/bpf_nf.c +6 -0
@@ -122,6 +122,12 @@ static void test_bpf_nf_ct(int mode)ASSERT_EQ (skel->bss->test_exist_lookup_mark,43 ,"Test existing connection lookup ctmark" );ASSERT_EQ (skel->data->test_snat_addr,0 ,"Test for source natting" );ASSERT_EQ (skel->data->test_dnat_addr,0 ,"Test for destination natting" ); +ASSERT_EQ (skel->data->test_ct_zone_id_alloc_entry,0 ,"Test for alloc new entry in specified ct zone" ); +ASSERT_EQ (skel->data->test_ct_zone_id_insert_entry,0 ,"Test for insert new entry in specified ct zone" ); +ASSERT_EQ (skel->data->test_ct_zone_id_succ_lookup,0 ,"Test for successful lookup in specified ct_zone" ); +ASSERT_EQ (skel->bss->test_ct_zone_dir_enoent_lookup, -ENOENT ,"Test ENOENT for lookup with wrong ct zone dir" ); +ASSERT_EQ (skel->bss->test_ct_zone_id_enoent_lookup, -ENOENT ,"Test ENOENT for lookup in wrong ct zone" ); + end:if (client_fd !=-1 )close (client_fd);
tools/testing/selftests/bpf/progs/test_bpf_nf.c +107 -10
157 lines not on this page · raw message
Signature · 1 line
2.34.1
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v3 2/2] selftests/bpf: Update tests for new ct zone opts for nf_conntrack kfuncs
Quoted 6 lines · Brad Cowie
tools/testing/selftests/bpf/progs/test_bpf_nf.c +0
-2@@ -84,16 +102,6 @@ nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32,else test_einval_bpf_tuple = opts_def.error; - opts_def.reserved[0 ] =1 ; - ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
tools/testing/selftests/bpf/progs/test_bpf_nf.c nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32, · line 106
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_bpf_nf.c +0
-8- sizeof(opts_def)); - opts_def.reserved[0 ] =0 ; - opts_def.l4proto =IPPROTO_TCP ; -if (ct) -bpf_ct_release (ct); -else - test_einval_reserved = opts_def.error; - opts_def.netns_id =-2 ; ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
This non-zero reserved[0] test is still valid and useful. How about create a new test_einval_reserved_new for testing the new struct?
pw-bot: cr
[ Sorry for the delay. I have some backlog. ].