diff mbox series

[bpf-next,2/2] selftests/bpf: Add bpf_program__attach_netfilter_opts helper test

Message ID 20230525110100.8212-3-fw@strlen.de (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series libbpf: add netfilter link attach helper | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers warning 13 maintainers not CCed: daniel@iogearbox.net yhs@fb.com kpsingh@kernel.org martin.lau@linux.dev john.fastabend@gmail.com song@kernel.org sdf@google.com andrii@kernel.org shuah@kernel.org mykolal@fb.com linux-kselftest@vger.kernel.org jolsa@kernel.org haoluo@google.com
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 81 exceeds 80 columns WARNING: line length of 83 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: line length of 88 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on s390x with gcc

Commit Message

Florian Westphal May 25, 2023, 11:01 a.m. UTC
Call bpf_program__attach_netfilter_opts() 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.

Signed-off-by: Florian Westphal <fw@strlen.de>
---
 .../bpf/prog_tests/netfilter_basic.c          | 87 +++++++++++++++++++
 .../bpf/progs/test_netfilter_link_attach.c    | 14 +++
 2 files changed, 101 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/netfilter_basic.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c

Comments

Andrii Nakryiko May 25, 2023, 9:05 p.m. UTC | #1
On Thu, May 25, 2023 at 4:01 AM Florian Westphal <fw@strlen.de> wrote:
>
> Call bpf_program__attach_netfilter_opts() 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.
>
> Signed-off-by: Florian Westphal <fw@strlen.de>
> ---
>  .../bpf/prog_tests/netfilter_basic.c          | 87 +++++++++++++++++++
>  .../bpf/progs/test_netfilter_link_attach.c    | 14 +++
>  2 files changed, 101 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/netfilter_basic.c
>  create mode 100644 tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/netfilter_basic.c b/tools/testing/selftests/bpf/prog_tests/netfilter_basic.c
> new file mode 100644
> index 000000000000..a64b5feaaca4
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/netfilter_basic.c
> @@ -0,0 +1,87 @@
> +// 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_hook_options {
> +       __u32 pf;
> +       __u32 hooknum;
> +       __s32 priority;
> +       __u32 flags;
> +
> +       bool expect_success;
> +};
> +
> +struct nf_hook_options nf_hook_attach_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 },
> +};
> +
> +static void __test_netfilter_link_attach(struct bpf_program *prog)
> +{
> +       LIBBPF_OPTS(bpf_netfilter_opts, opts);
> +       int i;
> +
> +       for (i = 0; i < ARRAY_SIZE(nf_hook_attach_tests); i++) {
> +               struct bpf_link *link;
> +
> +#define X(opts, m, i)  opts.m = nf_hook_attach_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_opts(prog, &opts);
> +               if (nf_hook_attach_tests[i].expect_success) {
> +                       struct bpf_link *link2;
> +
> +                       if (!ASSERT_OK_PTR(link, "program attach successful"))
> +                               continue;
> +
> +                       link2 = bpf_program__attach_netfilter_opts(prog, &opts);
> +                       ASSERT_NULL(link2, "attach program with same pf/hook/priority");

we have ASSERT_ERR_PTR(), which semantically is a bit more explicit,
let's use it here and below for !expect_success case

> +
> +                       if (!ASSERT_EQ(bpf_link__destroy(link), 0, "link destroy"))

ASSERT_OK()

> +                               break;
> +
> +                       link2 = bpf_program__attach_netfilter_opts(prog, &opts);
> +                       if (!ASSERT_OK_PTR(link2, "program reattach successful"))
> +                               continue;
> +                       if (!ASSERT_EQ(bpf_link__destroy(link2), 0, "link destroy"))

same, ASSERT_OK()

> +                               break;
> +               } else {
> +                       ASSERT_NULL(link, "program load failure");
> +               }
> +       }
> +}
> +
> +static void test_netfilter_link_attach(void)
> +{
> +       struct test_netfilter_link_attach *skel;
> +
> +       skel = test_netfilter_link_attach__open_and_load();
> +       if (!ASSERT_OK_PTR(skel, "test_netfilter_link_attach__open_and_load"))
> +               goto out;
> +
> +       __test_netfilter_link_attach(skel->progs.nf_link_attach_test);

nit: I'd just inline that function here instead of having
double-underscored helper function

> +out:
> +       test_netfilter_link_attach__destroy(skel);
> +}
> +
> +void test_netfilter_basic(void)
> +{
> +       if (test__start_subtest("netfilter link attach"))
> +               test_netfilter_link_attach();

Do you plan to add more subtests? If not, then this should be just a
test. Single subtest per test doesn't make much sense. Alternatively
(and perhaps better) is to treat each combination in
nf_hook_attach_tests as its own subtest.

> +}
> diff --git a/tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c b/tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c
> new file mode 100644
> index 000000000000..03a475160abe
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c
> @@ -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";
> --
> 2.39.3
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/netfilter_basic.c b/tools/testing/selftests/bpf/prog_tests/netfilter_basic.c
new file mode 100644
index 000000000000..a64b5feaaca4
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/netfilter_basic.c
@@ -0,0 +1,87 @@ 
+// 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_hook_options {
+	__u32 pf;
+	__u32 hooknum;
+	__s32 priority;
+	__u32 flags;
+
+	bool expect_success;
+};
+
+struct nf_hook_options nf_hook_attach_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 },
+};
+
+static void __test_netfilter_link_attach(struct bpf_program *prog)
+{
+	LIBBPF_OPTS(bpf_netfilter_opts, opts);
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(nf_hook_attach_tests); i++) {
+		struct bpf_link *link;
+
+#define X(opts, m, i)	opts.m = nf_hook_attach_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_opts(prog, &opts);
+		if (nf_hook_attach_tests[i].expect_success) {
+			struct bpf_link *link2;
+
+			if (!ASSERT_OK_PTR(link, "program attach successful"))
+				continue;
+
+			link2 = bpf_program__attach_netfilter_opts(prog, &opts);
+			ASSERT_NULL(link2, "attach program with same pf/hook/priority");
+
+			if (!ASSERT_EQ(bpf_link__destroy(link), 0, "link destroy"))
+				break;
+
+			link2 = bpf_program__attach_netfilter_opts(prog, &opts);
+			if (!ASSERT_OK_PTR(link2, "program reattach successful"))
+				continue;
+			if (!ASSERT_EQ(bpf_link__destroy(link2), 0, "link destroy"))
+				break;
+		} else {
+			ASSERT_NULL(link, "program load failure");
+		}
+	}
+}
+
+static void test_netfilter_link_attach(void)
+{
+	struct test_netfilter_link_attach *skel;
+
+	skel = test_netfilter_link_attach__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_netfilter_link_attach__open_and_load"))
+		goto out;
+
+	__test_netfilter_link_attach(skel->progs.nf_link_attach_test);
+out:
+	test_netfilter_link_attach__destroy(skel);
+}
+
+void test_netfilter_basic(void)
+{
+	if (test__start_subtest("netfilter link attach"))
+		test_netfilter_link_attach();
+}
diff --git a/tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c b/tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c
new file mode 100644
index 000000000000..03a475160abe
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c
@@ -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";