Test the session cookie for fsession. Multiple fsession BPF progs is
attached to bpf_fentry_test1() and session cookie is read and write in
this testcase.

Signed-off-by: Menglong Dong <[email protected]>
---
 .../selftests/bpf/prog_tests/fsession_test.c  | 34 +++++++++++++
 .../selftests/bpf/progs/fsession_cookie.c     | 49 +++++++++++++++++++
 2 files changed, 83 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/fsession_cookie.c

diff --git a/tools/testing/selftests/bpf/prog_tests/fsession_test.c 
b/tools/testing/selftests/bpf/prog_tests/fsession_test.c
index d70bdb683691..549b6fdd6167 100644
--- a/tools/testing/selftests/bpf/prog_tests/fsession_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/fsession_test.c
@@ -2,6 +2,7 @@
 /* Copyright (c) 2025 ChinaTelecom */
 #include <test_progs.h>
 #include "fsession_test.skel.h"
+#include "fsession_cookie.skel.h"
 
 static int check_result(struct fsession_test *skel)
 {
@@ -82,6 +83,37 @@ static void test_fsession_reattach(void)
        fsession_test__destroy(skel);
 }
 
+static void test_fsession_cookie(void)
+{
+       LIBBPF_OPTS(bpf_test_run_opts, topts);
+       struct fsession_cookie *skel = NULL;
+       int err, prog_fd;
+
+       skel = fsession_cookie__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "fsession_cookie__open_and_load"))
+               goto cleanup;
+
+       err = fsession_cookie__attach(skel);
+       if (!ASSERT_OK(err, "fsession_cookie_attach"))
+               goto cleanup;
+
+       /* Trigger target once */
+       prog_fd = bpf_program__fd(skel->progs.test1);
+       err = bpf_prog_test_run_opts(prog_fd, &topts);
+       if (!ASSERT_OK(err, "test_run_opts err"))
+               goto cleanup;
+       if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
+               goto cleanup;
+
+       for (int i = 0; i < sizeof(*skel->bss) / sizeof(__u64); i++) {
+               if (!ASSERT_EQ(((__u64 *)skel->bss)[i], 1, "test_result"))
+                       goto cleanup;
+       }
+
+cleanup:
+       fsession_cookie__destroy(skel);
+}
+
 void test_fsession_test(void)
 {
 #if !defined(__x86_64__)
@@ -92,4 +124,6 @@ void test_fsession_test(void)
                test_fsession_basic();
        if (test__start_subtest("fsession_reattach"))
                test_fsession_reattach();
+       if (test__start_subtest("fsession_cookie"))
+               test_fsession_cookie();
 }
diff --git a/tools/testing/selftests/bpf/progs/fsession_cookie.c 
b/tools/testing/selftests/bpf/progs/fsession_cookie.c
new file mode 100644
index 000000000000..93a120fb62e2
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/fsession_cookie.c
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 ChinaTelecom */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+__u64 test1_entry_ok = 0;
+__u64 test1_exit_ok = 0;
+
+SEC("fsession/bpf_fentry_test1")
+int BPF_PROG(test1, int a)
+{
+       __u64 *cookie = bpf_fsession_cookie(ctx);
+
+       if (!bpf_tracing_is_exit(ctx)) {
+               if (cookie) {
+                       *cookie = 0xAAAABBBBCCCCDDDDull;
+                       test1_entry_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
+               }
+               return 0;
+       }
+
+       if (cookie)
+               test1_exit_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
+       return 0;
+}
+
+__u64 test2_entry_ok = 0;
+__u64 test2_exit_ok = 0;
+
+SEC("fsession/bpf_fentry_test1")
+int BPF_PROG(test2, int a)
+{
+       __u64 *cookie = bpf_fsession_cookie(ctx);
+
+       if (!bpf_tracing_is_exit(ctx)) {
+               if (cookie) {
+                       *cookie = 0x1111222233334444ull;
+                       test2_entry_ok = *cookie == 0x1111222233334444ull;
+               }
+               return 0;
+       }
+
+       if (cookie)
+               test2_exit_ok = *cookie == 0x1111222233334444ull;
+       return 0;
+}
-- 
2.51.1.dirty


Reply via email to