xref: /linux/tools/testing/selftests/bpf/progs/fsession_test.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
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_session_is_return(ctx);
16 
17 	if (!is_exit) {
18 		test1_entry_result = a == 1 && ret == 0;
19 		return 0;
20 	}
21 
22 	test1_exit_result = a == 1 && ret == 2;
23 	return 0;
24 }
25 
26 __u64 test2_entry_result = 0;
27 __u64 test2_exit_result = 0;
28 
29 SEC("fsession/bpf_fentry_test3")
30 int BPF_PROG(test2, char a, int b, __u64 c, int ret)
31 {
32 	bool is_exit = bpf_session_is_return(ctx);
33 
34 	if (!is_exit) {
35 		test2_entry_result = a == 4 && b == 5 && c == 6 && ret == 0;
36 		return 0;
37 	}
38 
39 	test2_exit_result = a == 4 && b == 5 && c == 6 && ret == 15;
40 	return 0;
41 }
42 
43 __u64 test3_entry_result = 0;
44 __u64 test3_exit_result = 0;
45 
46 SEC("fsession/bpf_fentry_test4")
47 int BPF_PROG(test3, void *a, char b, int c, __u64 d, int ret)
48 {
49 	bool is_exit = bpf_session_is_return(ctx);
50 
51 	if (!is_exit) {
52 		test3_entry_result = a == (void *)7 && b == 8 && c == 9 && d == 10 && ret == 0;
53 		return 0;
54 	}
55 
56 	test3_exit_result = a == (void *)7 && b == 8 && c == 9 && d == 10 && ret == 34;
57 	return 0;
58 }
59 
60 __u64 test4_entry_result = 0;
61 __u64 test4_exit_result = 0;
62 
63 SEC("fsession/bpf_fentry_test5")
64 int BPF_PROG(test4, __u64 a, void *b, short c, int d, __u64 e, int ret)
65 {
66 	bool is_exit = bpf_session_is_return(ctx);
67 
68 	if (!is_exit) {
69 		test4_entry_result = a == 11 && b == (void *)12 && c == 13 && d == 14 &&
70 			e == 15 && ret == 0;
71 		return 0;
72 	}
73 
74 	test4_exit_result = a == 11 && b == (void *)12 && c == 13 && d == 14 &&
75 		e == 15 && ret == 65;
76 	return 0;
77 }
78 
79 __u64 test5_entry_result = 0;
80 __u64 test5_exit_result = 0;
81 
82 SEC("fsession/bpf_fentry_test7")
83 int BPF_PROG(test5, struct bpf_fentry_test_t *arg, int ret)
84 {
85 	bool is_exit = bpf_session_is_return(ctx);
86 
87 	if (!is_exit) {
88 		if (!arg)
89 			test5_entry_result = ret == 0;
90 		return 0;
91 	}
92 
93 	if (!arg)
94 		test5_exit_result = 1;
95 	return 0;
96 }
97 
98 __u64 test6_entry_result = 0;
99 __u64 test6_exit_result = 0;
100 SEC("fsession/bpf_fentry_test1")
101 int BPF_PROG(test6, int a)
102 {
103 	__u64 addr = bpf_get_func_ip(ctx);
104 
105 	if (bpf_session_is_return(ctx))
106 		test6_exit_result = (const void *) addr == &bpf_fentry_test1;
107 	else
108 		test6_entry_result = (const void *) addr == &bpf_fentry_test1;
109 	return 0;
110 }
111 
112 __u64 test7_entry_ok = 0;
113 __u64 test7_exit_ok = 0;
114 SEC("fsession/bpf_fentry_test1")
115 int BPF_PROG(test7, int a)
116 {
117 	volatile __u64 *cookie = bpf_session_cookie(ctx);
118 
119 	if (!bpf_session_is_return(ctx)) {
120 		*cookie = 0xAAAABBBBCCCCDDDDull;
121 		test7_entry_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
122 		return 0;
123 	}
124 
125 	test7_exit_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
126 	return 0;
127 }
128 
129 __u64 test8_entry_ok = 0;
130 __u64 test8_exit_ok = 0;
131 
132 SEC("fsession/bpf_fentry_test1")
133 int BPF_PROG(test8, int a)
134 {
135 	volatile __u64 *cookie = bpf_session_cookie(ctx);
136 
137 	if (!bpf_session_is_return(ctx)) {
138 		*cookie = 0x1111222233334444ull;
139 		test8_entry_ok = *cookie == 0x1111222233334444ull;
140 		return 0;
141 	}
142 
143 	test8_exit_ok = *cookie == 0x1111222233334444ull;
144 	return 0;
145 }
146 
147 __u64 test9_entry_result = 0;
148 __u64 test9_exit_result = 0;
149 
150 SEC("fsession/bpf_fentry_test1")
151 int BPF_PROG(test9, int a, int ret)
152 {
153 	__u64 *cookie = bpf_session_cookie(ctx);
154 
155 	if (!bpf_session_is_return(ctx)) {
156 		test9_entry_result = a == 1 && ret == 0;
157 		*cookie = 0x123456ULL;
158 		return 0;
159 	}
160 
161 	test9_exit_result = a == 1 && ret == 2 && *cookie == 0x123456ULL;
162 	return 0;
163 }
164 
165 __u64 test10_result = 0;
166 SEC("fexit/bpf_fentry_test1")
167 int BPF_PROG(test10, int a, int ret)
168 {
169 	test10_result = a == 1 && ret == 2;
170 	return 0;
171 }
172 
173 __u64 test11_result = 0;
174 SEC("fentry/bpf_fentry_test1")
175 int BPF_PROG(test11, int a)
176 {
177 	test11_result = a == 1;
178 	return 0;
179 }
180