libbpf: add netfilter link attach helper
Subsystems (6)
v3 cover libbpf: add netfilter link attach helper
v3: address comments from Andrii:
- prune verbose error message in 1/2
- use bpf_link_create internally in 1/2
- use subtests in patch 2/2
When initial netfilter bpf program type support got added one suggestion was to extend libbpf with a helper to ease attachment of nf programs to the hook locations.
Add such a helper and a demo test case that attaches a dummy program to various combinations.
I tested that the selftest fails when changing the expected outcome (i.e., set 'success' when it should fail and v.v.).
Florian Westphal (2):
tools: libbpf: add netfilter link attach helper selftests/bpf: Add bpf_program__attach_netfilter helper test
tools/lib/bpf/bpf.c | 6 ++ tools/lib/bpf/bpf.h | 6 ++ tools/lib/bpf/libbpf.c | 42 +++++++++ tools/lib/bpf/libbpf.h | 15 ++++ tools/lib/bpf/libbpf.map | 1 + .../bpf/prog_tests/netfilter_link_attach.c | 88 +++++++++++++++++++ .../bpf/progs/test_netfilter_link_attach.c | 14 +++ 7 files changed, 172 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/netfilter_link_attach.c create mode 100644 tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c
Signature · 1 line
2.39.3
PATCH v3 1/2 tools: libbpf: add netfilter link attach helper
Add new api function: bpf_program__attach_netfilter.
It takes a bpf program (netfilter type), and a pointer to a option struct that contains the desired attachment (protocol family, priority, hook location, ...).
It returns a pointer to a 'bpf_link' structure or NULL on error.
Next patch adds new netfilter_basic test that uses this function to attach a program to a few pf/hook/priority combinations.
v2: change name and use bpf_link_create.
--- tools/lib/bpf/bpf.c | 6 ++++++ tools/lib/bpf/bpf.h | 6 ++++++ tools/lib/bpf/libbpf.c | 42 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 15 ++++++++++++++ tools/lib/bpf/libbpf.map | 1 + 5 files changed, 70 insertions(+)
tools/lib/bpf/bpf.c +6 -0
@@ -741,6 +741,12 @@ int bpf_link_create(int prog_fd, int target_fd,if (!OPTS_ZEROED (opts, tracing))return libbpf_err (-EINVAL );break ; +case BPF_NETFILTER : + attr.link_create.netfilter.pf =OPTS_GET (opts, netfilter.pf,0 ); + attr.link_create.netfilter.hooknum =OPTS_GET (opts, netfilter.hooknum,0 ); + attr.link_create.netfilter.priority =OPTS_GET (opts, netfilter.priority,0 ); + attr.link_create.netfilter.flags =OPTS_GET (opts, netfilter.flags,0 ); +break ;default :if (!OPTS_ZEROED (opts, flags))return libbpf_err (-EINVAL );
tools/lib/bpf/bpf.h +6 -0
@@ -349,6 +349,12 @@ struct bpf_link_create_opts {struct {__u64 cookie; } tracing; +struct { +__u32 pf; +__u32 hooknum; +__s32 priority; +__u32 flags; + } netfilter; }; size_t :0; };
tools/lib/bpf/libbpf.c +42 -0
48 lines not on this page · raw message
tools/lib/bpf/libbpf.h +15 -0
@@ -718,6 +718,21 @@ LIBBPF_API struct bpf_link * bpf_program__attach_freplace(const struct bpf_program *prog, int target_fd, const char *attach_func_name); +struct bpf_netfilter_opts { +/* size of this struct, for forward/backward compatibility */ +size_t sz; + +__u32 pf; +__u32 hooknum; +__s32 priority; +__u32 flags; +}; +#define bpf_netfilter_opts__last_field flags + +LIBBPF_API struct bpf_link * +bpf_program__attach_netfilter (const struct bpf_program *prog, +const struct bpf_netfilter_opts *opts); +struct bpf_map ; LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
tools/lib/bpf/libbpf.map +1 -0
@@ -395,4 +395,5 @@ LIBBPF_1.2.0 { LIBBPF_1.3.0 { global: bpf_obj_pin_opts; + bpf_program__attach_netfilter; } LIBBPF_1.2.0;
Signature · 1 line
2.39.3
PATCH v3 2/2 selftests/bpf: Add bpf_program__attach_netfilter helper test
In reply to Florian WestphalCall bpf_program__attach_netfilter() with different protocol/hook/priority combinations.
Test fails if supposedly-illegal attachments work (e.g., bogus protocol family, illegal priority and so on) or if a should-work attachment fails. Expected output:
./test_progs -t netfilter_link_attach
#145/1 netfilter_link_attach/netfilter link attach 0:OK #145/2 netfilter_link_attach/netfilter link attach 1:OK #145/3 netfilter_link_attach/netfilter link attach 2:OK #145/4 netfilter_link_attach/netfilter link attach 3:OK #145/5 netfilter_link_attach/netfilter link attach 4:OK #145/6 netfilter_link_attach/netfilter link attach 5:OK #145/7 netfilter_link_attach/netfilter link attach 6:OK #145/8 netfilter_link_attach/netfilter link attach 7:OK #145/9 netfilter_link_attach/netfilter link attach 8:OK #145 netfilter_link_attach:OK
--- .../bpf/prog_tests/netfilter_link_attach.c | 88 +++++++++++++++++++ .../bpf/progs/test_netfilter_link_attach.c | 14 +++ 2 files changed, 102 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/netfilter_link_attach.c create mode 100644 tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c
tools/testing/selftests/bpf/prog_tests/netfilter_link_attach.c new file +88 -0
@@ -0,0 +1,88 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <netinet/in.h> +#include <linux/netfilter.h> + +#include "test_progs.h" +#include "test_netfilter_link_attach.skel.h" + +struct nf_link_test { +__u32 pf; +__u32 hooknum; +__s32 priority; +__u32 flags; + +bool expect_success; +}; + +struct nf_link_test nf_hook_link_tests[] = { + { }, + { .pf =NFPROTO_NUMPROTO , }, + { .pf =NFPROTO_IPV4 , .hooknum =42 , }, + { .pf =NFPROTO_IPV4 , .priority =INT_MIN }, + { .pf =NFPROTO_IPV4 , .priority =INT_MAX }, + { .pf =NFPROTO_IPV4 , .flags =UINT_MAX }, + + { .pf =NFPROTO_INET , .priority =1 , }, + + { .pf =NFPROTO_IPV4 , .priority =-10000 , .expect_success = true }, + { .pf =NFPROTO_IPV6 , .priority =10001 , .expect_success = true }, +}; + +void test_netfilter_link_attach (void ) +{ +struct test_netfilter_link_attach *skel; +struct bpf_program *prog; +LIBBPF_OPTS (bpf_netfilter_opts, opts); +int i; + + skel =test_netfilter_link_attach__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_netfilter_link_attach__open_and_load" )) + goto out; + + prog = skel->progs.nf_link_attach_test; +if (!ASSERT_OK_PTR (prog,"attach program" )) + goto out; + +for (i =0 ; i <ARRAY_SIZE (nf_hook_link_tests); i++) { +struct bpf_link *link; +char name[128 ]; + +snprintf (name,sizeof (name),"netfilter link attach %u" , i); + +if (!test__start_subtest (name)) +continue ; + +#define X (opts, m, i) opts.m = nf_hook_link_tests[(i)].m +X (opts, pf, i); +X (opts, hooknum, i); +X (opts, priority, i); +X (opts, flags, i); +#undef X + link =bpf_program__attach_netfilter (prog, &opts); +if (nf_hook_link_tests[i].expect_success) { +struct bpf_link *link2; + +if (!ASSERT_OK_PTR (link,"program attach successful" )) +continue ; + + link2 =bpf_program__attach_netfilter (prog, &opts); +ASSERT_ERR_PTR (link2,"attach program with same pf/hook/priority" ); + +if (!ASSERT_OK (bpf_link__destroy (link),"link destroy" )) +break ; + + link2 =bpf_program__attach_netfilter (prog, &opts); +if (!ASSERT_OK_PTR (link2,"program reattach successful" )) +continue ; +if (!ASSERT_OK (bpf_link__destroy (link2),"link destroy" )) +break ; + }else { +ASSERT_ERR_PTR (link,"program load failure" ); + } + } + +out: +test_netfilter_link_attach__destroy (skel); +} +
tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c new file +14 -0
@@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> + +#define NF_ACCEPT 1 + +SEC("netfilter") +int nf_link_attach_test (struct bpf_nf_ctx *ctx) +{ +return NF_ACCEPT ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.39.3
Re: [PATCH bpf-next v3 2/2] selftests/bpf: Add bpf_program__attach_netfilter helper test
Hi Florian,
Quoted 77 lines · Florian Westphal
Call
bpf_program__attach_netfilter()with different protocol/hook/priority combinations.Test fails if supposedly-illegal attachments work (e.g., bogus protocol family, illegal priority and so on) or if a should-work attachment fails. Expected output:
./test_progs -t
netfilter_link_attach#145/1 netfilter_link_attach/netfilter link attach 0:OK #145/2 netfilter_link_attach/netfilter link attach 1:OK #145/3 netfilter_link_attach/netfilter link attach 2:OK #145/4 netfilter_link_attach/netfilter link attach 3:OK #145/5 netfilter_link_attach/netfilter link attach 4:OK #145/6 netfilter_link_attach/netfilter link attach 5:OK #145/7 netfilter_link_attach/netfilter link attach 6:OK #145/8 netfilter_link_attach/netfilter link attach 7:OK #145/9 netfilter_link_attach/netfilter link attach 8:OK #145 netfilter_link_attach:OKSigned-off-by Florian Westphal <[email protected]>--- .../bpf/prog_tests/netfilter_link_attach.c | 88 +++++++++++++++++++ .../bpf/progs/test_netfilter_link_attach.c | 14 +++ 2 files changed, 102 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/netfilter_link_attach.c create mode 100644 tools/testing/selftests/bpf/progs/test_netfilter_link_attach.ctools/testing/selftests/bpf/prog_tests/netfilter_link_attach.c new file +44
-0@@ -0,0 +1,88 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <netinet/in.h> +#include <linux/netfilter.h> + +#include "test_progs.h" +#include "test_netfilter_link_attach.skel.h" + +struct nf_link_test { +__u32 pf; +__u32 hooknum; +__s32 priority; +__u32 flags; + +bool expect_success; +}; + +struct nf_link_test nf_hook_link_tests[] = { + { }, + { .pf =NFPROTO_NUMPROTO , }, + { .pf =NFPROTO_IPV4 , .hooknum =42 , }, + { .pf =NFPROTO_IPV4 , .priority =INT_MIN }, + { .pf =NFPROTO_IPV4 , .priority =INT_MAX }, + { .pf =NFPROTO_IPV4 , .flags =UINT_MAX }, + + { .pf =NFPROTO_INET , .priority =1 , }, + + { .pf =NFPROTO_IPV4 , .priority =-10000 , .expect_success = true }, + { .pf =NFPROTO_IPV6 , .priority =10001 , .expect_success = true }, +}; + +void test_netfilter_link_attach (void ) +{ +struct test_netfilter_link_attach *skel; +struct bpf_program *prog; +LIBBPF_OPTS (bpf_netfilter_opts, opts); +int i; + + skel =test_netfilter_link_attach__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_netfilter_link_attach__open_and_load" )) + goto out; + + prog = skel->progs.nf_link_attach_test; +if (!ASSERT_OK_PTR (prog,"attach program" ))
tools/testing/selftests/bpf/prog_tests/netfilter_link_attach.c line 54
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/netfilter_link_attach.c new file +10
-0+ goto out; + +for (i =0 ; i <ARRAY_SIZE (nf_hook_link_tests); i++) { +struct bpf_link *link; +char name[128 ]; + +snprintf (name,sizeof (name),"netfilter link attach %u" , i); + +if (!test__start_subtest (name)) +continue ;
Nit: naming by index makes it a little annoying to debug failures. How about adding a name field to struct nf_link_test?
[...]
Thanks,
Daniel