From: Yonghong Song <yonghong.song@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
kernel-team@fb.com, Martin KaFai Lau <martin.lau@kernel.org>,
Tejun Heo <tj@kernel.org>
Subject: [PATCH bpf-next v4 10/10] selftests/bpf: Add tests for bpf_prog_call()
Date: Thu, 10 Oct 2024 10:56:44 -0700 [thread overview]
Message-ID: <20241010175644.1900546-1-yonghong.song@linux.dev> (raw)
In-Reply-To: <20241010175552.1895980-1-yonghong.song@linux.dev>
Add two subtests for nested bpf_prog_call(). One is recursion in main prog,
and the other is recursion in callback func.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
.../selftests/bpf/prog_tests/prog_call.c | 78 ++++++++++++++++
tools/testing/selftests/bpf/progs/prog_call.c | 92 +++++++++++++++++++
2 files changed, 170 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/prog_call.c
create mode 100644 tools/testing/selftests/bpf/progs/prog_call.c
diff --git a/tools/testing/selftests/bpf/prog_tests/prog_call.c b/tools/testing/selftests/bpf/prog_tests/prog_call.c
new file mode 100644
index 000000000000..573c67c9af12
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/prog_call.c
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "prog_call.skel.h"
+
+static void test_nest_prog_call(int prog_index)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .data_in = &pkt_v4,
+ .data_size_in = sizeof(pkt_v4),
+ );
+ int err, idx = 0, prog_fd, map_fd;
+ struct prog_call *skel;
+ struct bpf_program *prog;
+
+ skel = prog_call__open();
+ if (!ASSERT_OK_PTR(skel, "prog_call__open"))
+ return;
+
+ switch (prog_index) {
+ case 0:
+ prog = skel->progs.entry_no_subprog;
+ break;
+ case 1:
+ prog = skel->progs.entry_subprog;
+ break;
+ case 2:
+ prog = skel->progs.entry_callback;
+ break;
+ }
+
+ bpf_program__set_autoload(prog, true);
+
+ err = prog_call__load(skel);
+ if (!ASSERT_OK(err, "prog_call__load"))
+ return;
+
+ map_fd = bpf_map__fd(skel->maps.jmp_table);
+ prog_fd = bpf_program__fd(prog);
+ /* maximum recursion level 4 */
+ err = bpf_map_update_elem(map_fd, &idx, &prog_fd, 0);
+ if (!ASSERT_OK(err, "bpf_map_update_elem"))
+ goto out;
+
+ err = bpf_prog_test_run_opts(prog_fd, &topts);
+ ASSERT_OK(err, "test_run");
+ ASSERT_EQ(skel->bss->vali, 4, "i");
+ ASSERT_EQ(skel->bss->valj, 6, "j");
+out:
+ prog_call__destroy(skel);
+}
+
+static void test_prog_call_with_tailcall(void)
+{
+ struct prog_call *skel;
+ int err;
+
+ skel = prog_call__open();
+ if (!ASSERT_OK_PTR(skel, "prog_call__open"))
+ return;
+
+ bpf_program__set_autoload(skel->progs.entry_tail_call, true);
+ err = prog_call__load(skel);
+ if (!ASSERT_ERR(err, "prog_call__load"))
+ prog_call__destroy(skel);
+}
+
+void test_prog_call(void)
+{
+ if (test__start_subtest("single_main_prog"))
+ test_nest_prog_call(0);
+ if (test__start_subtest("sub_prog"))
+ test_nest_prog_call(1);
+ if (test__start_subtest("callback_fn"))
+ test_nest_prog_call(2);
+ if (test__start_subtest("with_tailcall"))
+ test_prog_call_with_tailcall();
+}
diff --git a/tools/testing/selftests/bpf/progs/prog_call.c b/tools/testing/selftests/bpf/progs/prog_call.c
new file mode 100644
index 000000000000..c494cfcf653b
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/prog_call.c
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 3);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+struct callback_ctx {
+ struct __sk_buff *skb;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, __u64);
+} arraymap SEC(".maps");
+
+int vali, valj;
+
+int glb;
+__noinline static void subprog2(volatile int *a)
+{
+ glb = a[20] + a[10];
+}
+
+__noinline static void subprog1(struct __sk_buff *skb)
+{
+ volatile int a[100] = {};
+
+ a[10] = vali;
+ subprog2(a);
+ vali++;
+ bpf_prog_call(skb, (struct bpf_map *)&jmp_table, 0);
+ valj += a[10];
+}
+
+SEC("?tc")
+int entry_no_subprog(struct __sk_buff *skb)
+{
+ volatile int a[100] = {};
+
+ a[10] = vali;
+ subprog2(a);
+ vali++;
+ bpf_prog_call(skb, (struct bpf_map *)&jmp_table, 0);
+ valj += a[10];
+ return 0;
+}
+
+SEC("?tc")
+int entry_subprog(struct __sk_buff *skb)
+{
+ subprog1(skb);
+ return 0;
+}
+
+static __u64
+check_array_elem(struct bpf_map *map, __u32 *key, __u64 *val,
+ struct callback_ctx *data)
+{
+ subprog1(data->skb);
+ return 0;
+}
+
+SEC("?tc")
+int entry_callback(struct __sk_buff *skb)
+{
+ struct callback_ctx data;
+
+ data.skb = skb;
+ bpf_for_each_map_elem(&arraymap, check_array_elem, &data, 0);
+ return 0;
+}
+
+SEC("?tc")
+int entry_tail_call(struct __sk_buff *skb)
+{
+ struct callback_ctx data;
+
+ bpf_tail_call_static(skb, &jmp_table, 0);
+
+ data.skb = skb;
+ bpf_for_each_map_elem(&arraymap, check_array_elem, &data, 0);
+ return 0;
+}
+
+char __license[] SEC("license") = "GPL";
--
2.43.5
next prev parent reply other threads:[~2024-10-10 17:56 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-10 17:55 [PATCH bpf-next v4 00/10] bpf: Support private stack for bpf progs Yonghong Song
2024-10-10 17:55 ` [PATCH bpf-next v4 01/10] bpf: Allow each subprog having stack size of 512 bytes Yonghong Song
2024-10-10 17:56 ` [PATCH bpf-next v4 02/10] bpf: Mark each subprog with proper private stack modes Yonghong Song
2024-10-10 17:56 ` [PATCH bpf-next v4 03/10] bpf, x86: Refactor func emit_prologue Yonghong Song
2024-10-10 17:56 ` [PATCH bpf-next v4 04/10] bpf, x86: Create a helper for certain "reg <op>= imm" operations Yonghong Song
2024-10-10 17:56 ` [PATCH bpf-next v4 05/10] bpf, x86: Add jit support for private stack Yonghong Song
2024-10-10 17:56 ` [PATCH bpf-next v4 06/10] selftests/bpf: Add private stack tests Yonghong Song
2024-10-10 17:56 ` [PATCH bpf-next v4 07/10] bpf: Support calling non-tailcall bpf prog Yonghong Song
2024-10-10 20:28 ` Alexei Starovoitov
2024-10-11 4:12 ` Yonghong Song
2024-10-15 21:18 ` Tejun Heo
2024-10-15 21:35 ` Alexei Starovoitov
2024-10-10 17:56 ` [PATCH bpf-next v4 08/10] bpf, x86: Create two helpers for some arith operations Yonghong Song
2024-10-10 20:21 ` Alexei Starovoitov
2024-10-11 4:16 ` Yonghong Song
2024-10-10 17:56 ` [PATCH bpf-next v4 09/10] bpf, x86: Jit support for nested bpf_prog_call Yonghong Song
2024-10-10 20:53 ` Alexei Starovoitov
2024-10-11 4:20 ` Yonghong Song
2024-10-11 4:29 ` Alexei Starovoitov
2024-10-11 15:38 ` Yonghong Song
2024-10-11 15:40 ` Alexei Starovoitov
2024-10-11 16:14 ` Yonghong Song
2024-10-10 17:56 ` Yonghong Song [this message]
2024-10-15 21:28 ` [PATCH bpf-next v4 00/10] bpf: Support private stack for bpf progs Tejun Heo
2024-10-15 21:39 ` Alexei Starovoitov
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=20241010175644.1900546-1-yonghong.song@linux.dev \
--to=yonghong.song@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
--cc=tj@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.