Skip to content

Commit d8e329d

Browse files
image-dragonKernel Patches Daemon
authored andcommitted
selftests/bpf: add testcases for tracing session
Add testcases for BPF_TRACE_SESSION. The function arguments and return value are tested both in the entry and exit. And the kfunc bpf_tracing_is_exit() is also tested. As the layout of the stack changed for fsession, so we also test bpf_get_func_ip() for it. Session cookie for fsession is also tested. Multiple fsession BPF progs is attached to bpf_fentry_test1() and session cookie is read and write in the testcase. Signed-off-by: Menglong Dong <[email protected]>
1 parent 98a7136 commit d8e329d

File tree

2 files changed

+325
-0
lines changed

2 files changed

+325
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 ChinaTelecom */
3+
#include <test_progs.h>
4+
#include "fsession_test.skel.h"
5+
6+
static int check_result(struct fsession_test *skel)
7+
{
8+
LIBBPF_OPTS(bpf_test_run_opts, topts);
9+
int err, prog_fd;
10+
11+
/* Trigger test function calls */
12+
prog_fd = bpf_program__fd(skel->progs.test1);
13+
err = bpf_prog_test_run_opts(prog_fd, &topts);
14+
if (!ASSERT_OK(err, "test_run_opts err"))
15+
return err;
16+
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
17+
return topts.retval;
18+
19+
for (int i = 0; i < sizeof(*skel->bss) / sizeof(__u64); i++) {
20+
if (!ASSERT_EQ(((__u64 *)skel->bss)[i], 1, "test_result"))
21+
return -EINVAL;
22+
}
23+
24+
/* some fields go to the "data" sections, not "bss" */
25+
for (int i = 0; i < sizeof(*skel->data) / sizeof(__u64); i++) {
26+
if (!ASSERT_EQ(((__u64 *)skel->data)[i], 1, "test_result"))
27+
return -EINVAL;
28+
}
29+
return 0;
30+
}
31+
32+
static void test_fsession_basic(void)
33+
{
34+
struct fsession_test *skel = NULL;
35+
int err;
36+
37+
skel = fsession_test__open_and_load();
38+
if (!ASSERT_OK_PTR(skel, "fsession_test__open_and_load"))
39+
goto cleanup;
40+
41+
err = fsession_test__attach(skel);
42+
if (!ASSERT_OK(err, "fsession_attach"))
43+
goto cleanup;
44+
45+
check_result(skel);
46+
cleanup:
47+
fsession_test__destroy(skel);
48+
}
49+
50+
static void test_fsession_reattach(void)
51+
{
52+
struct fsession_test *skel = NULL;
53+
int err;
54+
55+
skel = fsession_test__open_and_load();
56+
if (!ASSERT_OK_PTR(skel, "fsession_test__open_and_load"))
57+
goto cleanup;
58+
59+
/* First attach */
60+
err = fsession_test__attach(skel);
61+
if (!ASSERT_OK(err, "fsession_first_attach"))
62+
goto cleanup;
63+
64+
if (check_result(skel))
65+
goto cleanup;
66+
67+
/* Detach */
68+
fsession_test__detach(skel);
69+
70+
/* Reset counters */
71+
memset(skel->bss, 0, sizeof(*skel->bss));
72+
73+
/* Second attach */
74+
err = fsession_test__attach(skel);
75+
if (!ASSERT_OK(err, "fsession_second_attach"))
76+
goto cleanup;
77+
78+
if (check_result(skel))
79+
goto cleanup;
80+
81+
cleanup:
82+
fsession_test__destroy(skel);
83+
}
84+
85+
void test_fsession_test(void)
86+
{
87+
#if !defined(__x86_64__)
88+
test__skip();
89+
return;
90+
#endif
91+
if (test__start_subtest("fsession_basic"))
92+
test_fsession_basic();
93+
if (test__start_subtest("fsession_reattach"))
94+
test_fsession_reattach();
95+
}
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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_result = 0;
10+
__u64 test1_exit_result = 0;
11+
12+
SEC("fsession/bpf_fentry_test1")
13+
int BPF_PROG(test1, int a, int ret)
14+
{
15+
bool is_exit = bpf_tracing_is_exit(ctx);
16+
17+
if (!is_exit) {
18+
/* This is entry */
19+
test1_entry_result = a == 1 && ret == 0;
20+
/* Return 0 to allow exit to be called */
21+
return 0;
22+
}
23+
24+
/* This is exit */
25+
test1_exit_result = a == 1 && ret == 2;
26+
return 0;
27+
}
28+
29+
__u64 test2_entry_result = 0;
30+
__u64 test2_exit_result = 1;
31+
32+
SEC("fsession/bpf_fentry_test2")
33+
int BPF_PROG(test2, int a, __u64 b, int ret)
34+
{
35+
bool is_exit = bpf_tracing_is_exit(ctx);
36+
37+
if (!is_exit) {
38+
/* This is entry */
39+
test2_entry_result = a == 2 && b == 3 && ret == 0;
40+
/* Return non-zero value to block exit call */
41+
return 1;
42+
}
43+
44+
/* This is exit - should not be called due to blocking */
45+
test2_exit_result = 0;
46+
return 0;
47+
}
48+
49+
__u64 test3_entry_result = 0;
50+
__u64 test3_exit_result = 0;
51+
52+
SEC("fsession/bpf_fentry_test3")
53+
int BPF_PROG(test3, char a, int b, __u64 c, int ret)
54+
{
55+
bool is_exit = bpf_tracing_is_exit(ctx);
56+
57+
if (!is_exit) {
58+
test3_entry_result = a == 4 && b == 5 && c == 6 && ret == 0;
59+
return 0;
60+
}
61+
62+
test3_exit_result = a == 4 && b == 5 && c == 6 && ret == 15;
63+
return 0;
64+
}
65+
66+
__u64 test4_entry_result = 0;
67+
__u64 test4_exit_result = 0;
68+
69+
SEC("fsession/bpf_fentry_test4")
70+
int BPF_PROG(test4, void *a, char b, int c, __u64 d, int ret)
71+
{
72+
bool is_exit = bpf_tracing_is_exit(ctx);
73+
74+
if (!is_exit) {
75+
test4_entry_result = a == (void *)7 && b == 8 && c == 9 && d == 10 && ret == 0;
76+
return 0;
77+
}
78+
79+
test4_exit_result = a == (void *)7 && b == 8 && c == 9 && d == 10 && ret == 34;
80+
return 0;
81+
}
82+
83+
__u64 test5_entry_result = 0;
84+
__u64 test5_exit_result = 0;
85+
86+
SEC("fsession/bpf_fentry_test5")
87+
int BPF_PROG(test5, __u64 a, void *b, short c, int d, __u64 e, int ret)
88+
{
89+
bool is_exit = bpf_tracing_is_exit(ctx);
90+
91+
if (!is_exit) {
92+
test5_entry_result = a == 11 && b == (void *)12 && c == 13 && d == 14 &&
93+
e == 15 && ret == 0;
94+
return 0;
95+
}
96+
97+
test5_exit_result = a == 11 && b == (void *)12 && c == 13 && d == 14 &&
98+
e == 15 && ret == 65;
99+
return 0;
100+
}
101+
102+
__u64 test6_entry_result = 0;
103+
__u64 test6_exit_result = 1;
104+
105+
SEC("fsession/bpf_fentry_test6")
106+
int BPF_PROG(test6, __u64 a, void *b, short c, int d, void *e, __u64 f, int ret)
107+
{
108+
bool is_exit = bpf_tracing_is_exit(ctx);
109+
110+
if (!is_exit) {
111+
test6_entry_result = a == 16 && b == (void *)17 && c == 18 && d == 19 &&
112+
e == (void *)20 && f == 21 && ret == 0;
113+
return 1;
114+
}
115+
116+
test6_exit_result = 0;
117+
return 0;
118+
}
119+
120+
__u64 test7_entry_result = 0;
121+
__u64 test7_exit_result = 0;
122+
123+
SEC("fsession/bpf_fentry_test7")
124+
int BPF_PROG(test7, struct bpf_fentry_test_t *arg, int ret)
125+
{
126+
bool is_exit = bpf_tracing_is_exit(ctx);
127+
128+
if (!is_exit) {
129+
if (!arg)
130+
test7_entry_result = ret == 0;
131+
return 0;
132+
}
133+
134+
if (!arg)
135+
test7_exit_result = 1;
136+
return 0;
137+
}
138+
139+
__u64 test8_entry_result = 0;
140+
__u64 test8_exit_result = 1;
141+
/*
142+
* test1, test8 and test9 hook the same target to verify the "ret" is always
143+
* 0 in the entry.
144+
*/
145+
SEC("fsession/bpf_fentry_test1")
146+
int BPF_PROG(test8, int a, int ret)
147+
{
148+
bool is_exit = bpf_tracing_is_exit(ctx);
149+
150+
if (!is_exit) {
151+
test8_entry_result = a == 1 && ret == 0;
152+
return -21;
153+
}
154+
155+
/* This is exit */
156+
test8_exit_result = 0;
157+
return 0;
158+
}
159+
160+
__u64 test9_entry_result = 0;
161+
__u64 test9_exit_result = 1;
162+
163+
SEC("fsession/bpf_fentry_test1")
164+
int BPF_PROG(test9, int a, int ret)
165+
{
166+
bool is_exit = bpf_tracing_is_exit(ctx);
167+
168+
if (!is_exit) {
169+
test9_entry_result = a == 1 && ret == 0;
170+
return -22;
171+
}
172+
173+
test9_exit_result = 0;
174+
return 0;
175+
}
176+
177+
__u64 test10_entry_result = 0;
178+
__u64 test10_exit_result = 0;
179+
SEC("fsession/bpf_fentry_test1")
180+
int BPF_PROG(test10, int a)
181+
{
182+
__u64 addr = bpf_get_func_ip(ctx);
183+
184+
if (bpf_tracing_is_exit(ctx))
185+
test10_exit_result = (const void *) addr == &bpf_fentry_test1;
186+
else
187+
test10_entry_result = (const void *) addr == &bpf_fentry_test1;
188+
return 0;
189+
}
190+
191+
__u64 test11_entry_ok = 0;
192+
__u64 test11_exit_ok = 0;
193+
SEC("fsession/bpf_fentry_test1")
194+
int BPF_PROG(test11, int a)
195+
{
196+
__u64 *cookie = bpf_fsession_cookie(ctx);
197+
198+
if (!bpf_tracing_is_exit(ctx)) {
199+
if (cookie) {
200+
*cookie = 0xAAAABBBBCCCCDDDDull;
201+
test11_entry_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
202+
}
203+
return 0;
204+
}
205+
206+
if (cookie)
207+
test11_exit_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
208+
return 0;
209+
}
210+
211+
__u64 test12_entry_ok = 0;
212+
__u64 test12_exit_ok = 0;
213+
214+
SEC("fsession/bpf_fentry_test1")
215+
int BPF_PROG(test12, int a)
216+
{
217+
__u64 *cookie = bpf_fsession_cookie(ctx);
218+
219+
if (!bpf_tracing_is_exit(ctx)) {
220+
if (cookie) {
221+
*cookie = 0x1111222233334444ull;
222+
test12_entry_ok = *cookie == 0x1111222233334444ull;
223+
}
224+
return 0;
225+
}
226+
227+
if (cookie)
228+
test12_exit_ok = *cookie == 0x1111222233334444ull;
229+
return 0;
230+
}

0 commit comments

Comments
 (0)