|  | 
|  | 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