]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: add testcases for fsession cookie
authorMenglong Dong <menglong8.dong@gmail.com>
Sat, 24 Jan 2026 06:20:07 +0000 (14:20 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 25 Jan 2026 02:49:36 +0000 (18:49 -0800)
Test session cookie for fsession. Multiple fsession BPF progs is attached
to bpf_fentry_test1() and session cookie is read and write in the
testcase.

bpf_get_func_ip() will influence the layout of the session cookies, so we
test the cookie in two case: with and without bpf_get_func_ip().

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
Link: https://lore.kernel.org/r/20260124062008.8657-13-dongml2@chinatelecom.cn
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/fsession_test.c
tools/testing/selftests/bpf/progs/fsession_test.c

index 75bb42942b67fec5dc8f5a8e71ab98e888238852..0c4b428e1cee98cd6e4174fba54c88ec59bd488f 100644 (file)
@@ -77,6 +77,38 @@ cleanup:
        fsession_test__destroy(skel);
 }
 
+static void test_fsession_cookie(void)
+{
+       struct fsession_test *skel = NULL;
+       int err;
+
+       skel = fsession_test__open();
+       if (!ASSERT_OK_PTR(skel, "fsession_test__open"))
+               goto cleanup;
+
+       /*
+        * The test_fsession_basic() will test the session cookie with
+        * bpf_get_func_ip() case, so we need only check
+        * the cookie without bpf_get_func_ip() case here
+        */
+       bpf_program__set_autoload(skel->progs.test6, false);
+
+       err = fsession_test__load(skel);
+       if (!ASSERT_OK(err, "fsession_test__load"))
+               goto cleanup;
+
+       err = fsession_test__attach(skel);
+       if (!ASSERT_OK(err, "fsession_attach"))
+               goto cleanup;
+
+       skel->bss->test6_entry_result = 1;
+       skel->bss->test6_exit_result = 1;
+
+       check_result(skel);
+cleanup:
+       fsession_test__destroy(skel);
+}
+
 void test_fsession_test(void)
 {
 #if !defined(__x86_64__)
@@ -87,4 +119,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();
 }
index 0e1b66b2dddcd7b86e7b3089bfa0f7f1b11dd71e..211332bdcccb104440053411952b01b0d9a6509e 100644 (file)
@@ -95,3 +95,69 @@ int BPF_PROG(test5, struct bpf_fentry_test_t *arg, int ret)
        return 0;
 }
 
+__u64 test6_entry_result = 0;
+__u64 test6_exit_result = 0;
+SEC("fsession/bpf_fentry_test1")
+int BPF_PROG(test6, int a)
+{
+       __u64 addr = bpf_get_func_ip(ctx);
+
+       if (bpf_session_is_return(ctx))
+               test6_exit_result = (const void *) addr == &bpf_fentry_test1;
+       else
+               test6_entry_result = (const void *) addr == &bpf_fentry_test1;
+       return 0;
+}
+
+__u64 test7_entry_ok = 0;
+__u64 test7_exit_ok = 0;
+SEC("fsession/bpf_fentry_test1")
+int BPF_PROG(test7, int a)
+{
+       volatile __u64 *cookie = bpf_session_cookie(ctx);
+
+       if (!bpf_session_is_return(ctx)) {
+               *cookie = 0xAAAABBBBCCCCDDDDull;
+               test7_entry_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
+               return 0;
+       }
+
+       test7_exit_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
+       return 0;
+}
+
+__u64 test8_entry_ok = 0;
+__u64 test8_exit_ok = 0;
+
+SEC("fsession/bpf_fentry_test1")
+int BPF_PROG(test8, int a)
+{
+       volatile __u64 *cookie = bpf_session_cookie(ctx);
+
+       if (!bpf_session_is_return(ctx)) {
+               *cookie = 0x1111222233334444ull;
+               test8_entry_ok = *cookie == 0x1111222233334444ull;
+               return 0;
+       }
+
+       test8_exit_ok = *cookie == 0x1111222233334444ull;
+       return 0;
+}
+
+__u64 test9_entry_result = 0;
+__u64 test9_exit_result = 0;
+
+SEC("fsession/bpf_fentry_test1")
+int BPF_PROG(test9, int a, int ret)
+{
+       __u64 *cookie = bpf_session_cookie(ctx);
+
+       if (!bpf_session_is_return(ctx)) {
+               test9_entry_result = a == 1 && ret == 0;
+               *cookie = 0x123456ULL;
+               return 0;
+       }
+
+       test9_exit_result = a == 1 && ret == 2 && *cookie == 0x123456ULL;
+       return 0;
+}