Test the fsession when it is used together with fentry, fexit.

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

diff --git a/tools/testing/selftests/bpf/prog_tests/fsession_test.c 
b/tools/testing/selftests/bpf/prog_tests/fsession_test.c
index 549b6fdd6167..5529e4236b7c 100644
--- a/tools/testing/selftests/bpf/prog_tests/fsession_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/fsession_test.c
@@ -3,6 +3,7 @@
 #include <test_progs.h>
 #include "fsession_test.skel.h"
 #include "fsession_cookie.skel.h"
+#include "fsession_mixed.skel.h"
 
 static int check_result(struct fsession_test *skel)
 {
@@ -114,6 +115,35 @@ static void test_fsession_cookie(void)
        fsession_cookie__destroy(skel);
 }
 
+static void test_fsession_mixed(void)
+{
+       LIBBPF_OPTS(bpf_test_run_opts, topts);
+       struct fsession_mixed *skel = NULL;
+       int err, prog_fd;
+
+       skel = fsession_mixed__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "fsession_mixed__open_and_load"))
+               goto cleanup;
+
+       err = fsession_mixed__attach(skel);
+       if (!ASSERT_OK(err, "fsession_mixed_attach"))
+               goto cleanup;
+
+       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_mixed__destroy(skel);
+}
+
 void test_fsession_test(void)
 {
 #if !defined(__x86_64__)
@@ -126,4 +156,6 @@ void test_fsession_test(void)
                test_fsession_reattach();
        if (test__start_subtest("fsession_cookie"))
                test_fsession_cookie();
+       if (test__start_subtest("fsession_mixed"))
+               test_fsession_mixed();
 }
diff --git a/tools/testing/selftests/bpf/progs/fsession_mixed.c 
b/tools/testing/selftests/bpf/progs/fsession_mixed.c
new file mode 100644
index 000000000000..226e8ca438d4
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/fsession_mixed.c
@@ -0,0 +1,45 @@
+// 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_result = 0;
+__u64 test1_exit_result = 0;
+
+SEC("fsession/bpf_fentry_test5")
+int BPF_PROG(test1, __u64 a, void *b, short c, int d, __u64 e, int ret)
+{
+       __u64 *cookie = bpf_fsession_cookie(ctx);
+
+       if (!bpf_tracing_is_exit(ctx)) {
+               test1_entry_result = a == 11 && b == (void *)12 && c == 13 && d 
== 14 &&
+                       e == 15 && ret == 0;
+               *cookie = 0x123456ULL;
+               return 0;
+       }
+
+       test1_exit_result = a == 11 && b == (void *)12 && c == 13 && d == 14 &&
+               e == 15 && ret == 65 && *cookie == 0x123456ULL;
+       return 0;
+}
+
+__u64 test2_result = 0;
+SEC("fexit/bpf_fentry_test5")
+int BPF_PROG(test2, __u64 a, void *b, short c, int d, __u64 e, int ret)
+{
+       test2_result = a == 11 && b == (void *)12 && c == 13 && d == 14 &&
+               e == 15 && ret == 65;
+       return 0;
+}
+
+__u64 test3_result = 0;
+SEC("fentry/bpf_fentry_test5")
+int BPF_PROG(test3, __u64 a, void *b, short c, int d, __u64 e)
+{
+       test3_result = a == 11 && b == (void *)12 && c == 13 && d == 14 &&
+               e == 15;
+       return 0;
+}
-- 
2.51.1.dirty


Reply via email to