From: Florian Westphal <fw@strlen.de>
To: <bpf@vger.kernel.org>
Cc: andrii.nakryiko@gmail.com, ast@kernel.org,
<netdev@vger.kernel.org>,
dxu@dxuuu.xyz, qde@naccy.de, Florian Westphal <fw@strlen.de>
Subject: [PATCH bpf-next 2/2] selftests/bpf: Add bpf_program__attach_netfilter_opts helper test
Date: Thu, 25 May 2023 13:01:00 +0200 [thread overview]
Message-ID: <20230525110100.8212-3-fw@strlen.de> (raw)
In-Reply-To: <20230525110100.8212-1-fw@strlen.de>
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");
+
+ 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";
--
2.39.3
next prev parent reply other threads:[~2023-05-25 11:01 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-25 11:00 [PATCH bpf-next 0/2] libbpf: add netfilter link attach helper Florian Westphal
2023-05-25 11:00 ` [PATCH bpf-next 1/2] tools: " Florian Westphal
2023-05-25 21:01 ` Andrii Nakryiko
2023-05-26 12:11 ` [PATCH bpf] bpf: netfilter: add BPF_NETFILTER bpf_attach_type Florian Westphal
2023-05-30 17:10 ` Andrii Nakryiko
2023-05-25 11:01 ` Florian Westphal [this message]
2023-05-25 21:05 ` [PATCH bpf-next 2/2] selftests/bpf: Add bpf_program__attach_netfilter_opts helper test Andrii Nakryiko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230525110100.8212-3-fw@strlen.de \
--to=fw@strlen.de \
--cc=andrii.nakryiko@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=dxu@dxuuu.xyz \
--cc=netdev@vger.kernel.org \
--cc=qde@naccy.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).