selftests/bpf: add testcases for fsession cookie

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>
This commit is contained in:
Menglong Dong
2026-01-24 14:20:07 +08:00
committed by Alexei Starovoitov
parent a5533a6eaa
commit 8909b3fb23
2 changed files with 100 additions and 0 deletions

View File

@@ -77,6 +77,38 @@ static void test_fsession_reattach(void)
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();
}

View 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;
}