From: Yonghong Song <yhs@fb.com>
To: <bpf@vger.kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>, <kernel-team@fb.com>,
Martin KaFai Lau <martin.lau@kernel.org>,
Namhyung Kim <namhyung@kernel.org>
Subject: [RFC PATCH bpf-next 3/3] bpf: Add bpf_get_kern_btf_id() tests
Date: Mon, 14 Nov 2022 08:23:45 -0800 [thread overview]
Message-ID: <20221114162345.625666-1-yhs@fb.com> (raw)
In-Reply-To: <20221114162328.622665-1-yhs@fb.com>
Two tests are added. One is from John Fastabend ({1]) which tests
tracing style access for xdp program from the kernel ctx.
Another is a tc test to test both kernel ctx tracing style access
and explicit non-ctx type cast.
[1] https://lore.kernel.org/bpf/20221109215242.1279993-1-john.fastabend@gmail.com/
Signed-off-by: Yonghong Song <yhs@fb.com>
---
.../bpf/prog_tests/get_kern_btf_id.c | 81 +++++++++++++++++++
.../selftests/bpf/progs/get_kern_btf_id.c | 44 ++++++++++
2 files changed, 125 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c
create mode 100644 tools/testing/selftests/bpf/progs/get_kern_btf_id.c
diff --git a/tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c b/tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c
new file mode 100644
index 000000000000..205631916564
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "get_kern_btf_id.skel.h"
+
+static void test_xdp(void)
+{
+ struct get_kern_btf_id *skel;
+ int err, prog_fd;
+ char buf[128];
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .data_in = &pkt_v4,
+ .data_size_in = sizeof(pkt_v4),
+ .data_out = buf,
+ .data_size_out = sizeof(buf),
+ .repeat = 1,
+ );
+
+ skel = get_kern_btf_id__open();
+ if (!ASSERT_OK_PTR(skel, "skel_open"))
+ return;
+
+ bpf_program__set_autoload(skel->progs.md_xdp, true);
+ err = get_kern_btf_id__load(skel);
+ if (!ASSERT_OK(err, "skel_load"))
+ goto out;
+
+ prog_fd = bpf_program__fd(skel->progs.md_xdp);
+ err = bpf_prog_test_run_opts(prog_fd, &topts);
+ ASSERT_OK(err, "test_run");
+ ASSERT_EQ(topts.retval, XDP_PASS, "xdp test_run retval");
+
+ ASSERT_EQ(skel->bss->ifindex, 1, "xdp_md ifindex");
+ ASSERT_EQ(skel->bss->ifindex, skel->bss->ingress_ifindex, "xdp_md ingress_ifindex");
+ ASSERT_STREQ(skel->bss->name, "lo", "xdp_md name");
+ ASSERT_NEQ(skel->bss->inum, 0, "xdp_md inum");
+
+out:
+ get_kern_btf_id__destroy(skel);
+}
+
+static void test_tc(void)
+{
+ struct get_kern_btf_id *skel;
+ int err, prog_fd;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .data_in = &pkt_v4,
+ .data_size_in = sizeof(pkt_v4),
+ .repeat = 1,
+ );
+
+ skel = get_kern_btf_id__open();
+ if (!ASSERT_OK_PTR(skel, "skel_open"))
+ return;
+
+ bpf_program__set_autoload(skel->progs.md_skb, true);
+ err = get_kern_btf_id__load(skel);
+ if (!ASSERT_OK(err, "skel_load"))
+ goto out;
+
+ prog_fd = bpf_program__fd(skel->progs.md_skb);
+ err = bpf_prog_test_run_opts(prog_fd, &topts);
+ ASSERT_OK(err, "test_run");
+ ASSERT_EQ(topts.retval, 0, "tc test_run retval");
+
+ ASSERT_EQ(skel->bss->meta_len, 0, "skb meta_len");
+ ASSERT_EQ(skel->bss->frag0_len, 0, "skb frag0_len");
+
+out:
+ get_kern_btf_id__destroy(skel);
+}
+
+void test_get_kern_btf_id(void)
+{
+ if (test__start_subtest("xdp"))
+ test_xdp();
+ if (test__start_subtest("tc"))
+ test_tc();
+}
diff --git a/tools/testing/selftests/bpf/progs/get_kern_btf_id.c b/tools/testing/selftests/bpf/progs/get_kern_btf_id.c
new file mode 100644
index 000000000000..b530c7c52ff3
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/get_kern_btf_id.c
@@ -0,0 +1,44 @@
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_core_read.h>
+
+#define IFNAMSIZ 16
+
+int ifindex, ingress_ifindex;
+char name[IFNAMSIZ];
+unsigned int inum;
+int meta_len, frag0_len;
+
+extern void *bpf_get_kern_btf_id(void *, __u32) __ksym;
+
+SEC("?xdp")
+int md_xdp(struct xdp_md *ctx)
+{
+ struct xdp_buff *kctx = bpf_get_kern_btf_id(ctx, 0);
+ struct net_device *dev;
+
+ dev = kctx->rxq->dev;
+ ifindex = dev->ifindex;
+ inum = dev->nd_net.net->ns.inum;
+ __builtin_memcpy(name, dev->name, IFNAMSIZ);
+ ingress_ifindex = ctx->ingress_ifindex;
+ return XDP_PASS;
+}
+
+SEC("?tc")
+int md_skb(struct __sk_buff *skb)
+{
+ struct sk_buff *kskb = bpf_get_kern_btf_id(skb, 0);
+ struct skb_shared_info *shared_info;
+
+ /* Simulate the following kernel macro:
+ * #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
+ */
+ shared_info = bpf_get_kern_btf_id(kskb->head + kskb->end,
+ bpf_core_type_id_kernel(struct skb_shared_info));
+ meta_len = shared_info->meta_len;
+ frag0_len = shared_info->frag_list->len;
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.30.2
next prev parent reply other threads:[~2022-11-14 16:24 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-14 16:23 [RFC PATCH bpf-next 0/3] bpf: Implement bpf_get_kern_btf_id() kfunc Yonghong Song
2022-11-14 16:23 ` [RFC PATCH bpf-next 1/3] bpf: Add support for kfunc set with generic btf_ids Yonghong Song
2022-11-14 16:23 ` [RFC PATCH bpf-next 2/3] bpf: Implement bpf_get_kern_btf_id() kfunc Yonghong Song
2022-11-15 19:43 ` Alexei Starovoitov
2022-11-15 20:05 ` Kumar Kartikeya Dwivedi
2022-11-15 20:26 ` Yonghong Song
2022-11-17 18:24 ` Kumar Kartikeya Dwivedi
2022-11-17 22:52 ` Yonghong Song
2022-11-17 23:01 ` Kumar Kartikeya Dwivedi
2022-11-17 23:13 ` Yonghong Song
2022-11-14 16:23 ` Yonghong Song [this message]
2022-11-15 16:30 ` [RFC PATCH bpf-next 0/3] " Toke Høiland-Jørgensen
2022-11-15 19:53 ` Yonghong Song
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=20221114162345.625666-1-yhs@fb.com \
--to=yhs@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
--cc=namhyung@kernel.org \
/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