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@xxxxxxxxx> --- .../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