Attach a struct_ops map with a cookie and test that struct_ops programs can read the cookie using bpf_get_attach_cookie(). Signed-off-by: Amery Hung <ameryhung@xxxxxxxxx> --- .../bpf/prog_tests/test_struct_ops_cookie.c | 42 ++++++++++++++++ .../selftests/bpf/progs/struct_ops_cookie.c | 48 +++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 1 + 3 files changed, 91 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_cookie.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_cookie.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_cookie.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_cookie.c new file mode 100644 index 000000000000..36179785b173 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_cookie.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> + +#include "struct_ops_cookie.skel.h" + +static void test_struct_ops_cookie_basic(void) +{ + LIBBPF_OPTS(bpf_struct_ops_opts, attach_opts); + LIBBPF_OPTS(bpf_test_run_opts, run_opts); + struct struct_ops_cookie *skel; + struct bpf_link *link; + int err, prog_fd; + + skel = struct_ops_cookie__open_and_load(); + if (!ASSERT_OK_PTR(skel, "struct_ops_cookie__open_and_load")) + return; + + attach_opts.cookie = 0x12345678; + link = bpf_map__attach_struct_ops_opts(skel->maps.testmod_cookie, &attach_opts); + if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops_opts")) + goto out; + + prog_fd = bpf_program__fd(skel->progs.trigger_test_1); + err = bpf_prog_test_run_opts(prog_fd, &run_opts); + ASSERT_OK(err, "bpf_prog_test_run_opts"); + ASSERT_EQ(skel->bss->cookie_test_1, 0x12345678, "cookie_1_value"); + + prog_fd = bpf_program__fd(skel->progs.trigger_test_2); + err = bpf_prog_test_run_opts(prog_fd, &run_opts); + ASSERT_OK(err, "bpf_prog_test_run_opts"); + ASSERT_EQ(skel->bss->cookie_test_2, 0x12345678, "cookie_2_value"); + +out: + bpf_link__destroy(link); + struct_ops_cookie__destroy(skel); +} + +void serial_test_struct_ops_cookie(void) +{ + if (test__start_subtest("struct_ops_cookie_basic")) + test_struct_ops_cookie_basic(); +} diff --git a/tools/testing/selftests/bpf/progs/struct_ops_cookie.c b/tools/testing/selftests/bpf/progs/struct_ops_cookie.c new file mode 100644 index 000000000000..1033939fbc1e --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_cookie.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> +#include "../test_kmods/bpf_testmod.h" + +char _license[] SEC("license") = "GPL"; + +__u64 cookie_test_1 = 0; +__u64 cookie_test_2 = 0; + +void bpf_testmod_ops3_call_test_1(void) __ksym; +void bpf_testmod_ops3_call_test_2(void) __ksym; + +SEC("struct_ops/test_cookie_1") +int BPF_PROG(test_cookie_1) +{ + cookie_test_1 = bpf_get_attach_cookie(ctx); + return 0; +} + +SEC("struct_ops/test_cookie_2") +int BPF_PROG(test_cookie_2) +{ + cookie_test_2 = bpf_get_attach_cookie(ctx); + return 0; +} + +SEC("syscall") +int trigger_test_1(void *ctx) +{ + bpf_testmod_ops3_call_test_1(); + return 0; +} + +SEC("syscall") +int trigger_test_2(void *ctx) +{ + bpf_testmod_ops3_call_test_2(); + return 0; +} + +/* Struct ops map that will be attached with a cookie */ +SEC(".struct_ops.link") +struct bpf_testmod_ops3 testmod_cookie = { + .test_1 = (void *)test_cookie_1, + .test_2 = (void *)test_cookie_2, +}; diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c index e9e918cdf31f..d273b327a1c0 100644 --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c @@ -1139,6 +1139,7 @@ static const struct bpf_verifier_ops bpf_testmod_verifier_ops = { }; static const struct bpf_verifier_ops bpf_testmod_verifier_ops3 = { + .get_func_proto = bpf_base_func_proto, .is_valid_access = bpf_testmod_ops_is_valid_access, }; -- 2.47.1