Skip to content

Commit 9bbe631

Browse files
image-dragonKernel Patches Daemon
authored andcommitted
selftests/bpf: add session cookie testcase for fsession
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]>
1 parent 19d17ba commit 9bbe631

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

tools/testing/selftests/bpf/prog_tests/fsession_test.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Copyright (c) 2025 ChinaTelecom */
33
#include <test_progs.h>
44
#include "fsession_test.skel.h"
5+
#include "fsession_cookie.skel.h"
56

67
static int check_result(struct fsession_test *skel)
78
{
@@ -82,6 +83,37 @@ static void test_fsession_reattach(void)
8283
fsession_test__destroy(skel);
8384
}
8485

86+
static void test_fsession_cookie(void)
87+
{
88+
LIBBPF_OPTS(bpf_test_run_opts, topts);
89+
struct fsession_cookie *skel = NULL;
90+
int err, prog_fd;
91+
92+
skel = fsession_cookie__open_and_load();
93+
if (!ASSERT_OK_PTR(skel, "fsession_cookie__open_and_load"))
94+
goto cleanup;
95+
96+
err = fsession_cookie__attach(skel);
97+
if (!ASSERT_OK(err, "fsession_cookie_attach"))
98+
goto cleanup;
99+
100+
/* Trigger target once */
101+
prog_fd = bpf_program__fd(skel->progs.test1);
102+
err = bpf_prog_test_run_opts(prog_fd, &topts);
103+
if (!ASSERT_OK(err, "test_run_opts err"))
104+
goto cleanup;
105+
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
106+
goto cleanup;
107+
108+
for (int i = 0; i < sizeof(*skel->bss) / sizeof(__u64); i++) {
109+
if (!ASSERT_EQ(((__u64 *)skel->bss)[i], 1, "test_result"))
110+
goto cleanup;
111+
}
112+
113+
cleanup:
114+
fsession_cookie__destroy(skel);
115+
}
116+
85117
void test_fsession_test(void)
86118
{
87119
#if !defined(__x86_64__)
@@ -92,4 +124,6 @@ void test_fsession_test(void)
92124
test_fsession_basic();
93125
if (test__start_subtest("fsession_reattach"))
94126
test_fsession_reattach();
127+
if (test__start_subtest("fsession_cookie"))
128+
test_fsession_cookie();
95129
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 ChinaTelecom */
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
__u64 test1_entry_ok = 0;
10+
__u64 test1_exit_ok = 0;
11+
12+
SEC("fsession/bpf_fentry_test1")
13+
int BPF_PROG(test1, int a)
14+
{
15+
__u64 *cookie = bpf_fsession_cookie(ctx);
16+
17+
if (!bpf_tracing_is_exit(ctx)) {
18+
if (cookie) {
19+
*cookie = 0xAAAABBBBCCCCDDDDull;
20+
test1_entry_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
21+
}
22+
return 0;
23+
}
24+
25+
if (cookie)
26+
test1_exit_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
27+
return 0;
28+
}
29+
30+
__u64 test2_entry_ok = 0;
31+
__u64 test2_exit_ok = 0;
32+
33+
SEC("fsession/bpf_fentry_test1")
34+
int BPF_PROG(test2, int a)
35+
{
36+
__u64 *cookie = bpf_fsession_cookie(ctx);
37+
38+
if (!bpf_tracing_is_exit(ctx)) {
39+
if (cookie) {
40+
*cookie = 0x1111222233334444ull;
41+
test2_entry_ok = *cookie == 0x1111222233334444ull;
42+
}
43+
return 0;
44+
}
45+
46+
if (cookie)
47+
test2_exit_ok = *cookie == 0x1111222233334444ull;
48+
return 0;
49+
}

0 commit comments

Comments
 (0)