xref: /linux/tools/testing/selftests/bpf/test_progs.h (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <time.h>
10 #include <signal.h>
11 
12 #include <linux/types.h>
13 typedef __u16 __sum16;
14 #include <arpa/inet.h>
15 #include <linux/if_ether.h>
16 #include <linux/if_packet.h>
17 #include <linux/ip.h>
18 #include <linux/ipv6.h>
19 #include <linux/filter.h>
20 #include <linux/perf_event.h>
21 #include <linux/socket.h>
22 #include <linux/unistd.h>
23 
24 #include <sys/ioctl.h>
25 #include <sys/wait.h>
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <fcntl.h>
29 #include <pthread.h>
30 #include <linux/bpf.h>
31 #include <linux/err.h>
32 #include <bpf/bpf.h>
33 #include <bpf/libbpf.h>
34 
35 #include "test_iptunnel_common.h"
36 #include "bpf_util.h"
37 #include <bpf/bpf_endian.h>
38 #include "trace_helpers.h"
39 #include "testing_helpers.h"
40 #include "flow_dissector_load.h"
41 
42 enum verbosity {
43 	VERBOSE_NONE,
44 	VERBOSE_NORMAL,
45 	VERBOSE_VERY,
46 	VERBOSE_SUPER,
47 };
48 
49 struct str_set {
50 	const char **strs;
51 	int cnt;
52 };
53 
54 struct test_selector {
55 	struct str_set whitelist;
56 	struct str_set blacklist;
57 	bool *num_set;
58 	int num_set_len;
59 };
60 
61 struct test_env {
62 	struct test_selector test_selector;
63 	struct test_selector subtest_selector;
64 	bool verifier_stats;
65 	bool debug;
66 	enum verbosity verbosity;
67 
68 	bool jit_enabled;
69 	bool has_testmod;
70 	bool get_test_cnt;
71 	bool list_test_names;
72 
73 	struct prog_test_def *test; /* current running tests */
74 
75 	FILE *stdout;
76 	FILE *stderr;
77 	char *log_buf;
78 	size_t log_cnt;
79 	int nr_cpus;
80 
81 	int succ_cnt; /* successful tests */
82 	int sub_succ_cnt; /* successful sub-tests */
83 	int fail_cnt; /* total failed tests + sub-tests */
84 	int skip_cnt; /* skipped tests */
85 
86 	int saved_netns_fd;
87 	int workers; /* number of worker process */
88 	int worker_id; /* id number of current worker, main process is -1 */
89 	pid_t *worker_pids; /* array of worker pids */
90 	int *worker_socks; /* array of worker socks */
91 	int *worker_current_test; /* array of current running test for each worker */
92 };
93 
94 #define MAX_LOG_TRUNK_SIZE 8192
95 enum msg_type {
96 	MSG_DO_TEST = 0,
97 	MSG_TEST_DONE = 1,
98 	MSG_TEST_LOG = 2,
99 	MSG_EXIT = 255,
100 };
101 struct msg {
102 	enum msg_type type;
103 	union {
104 		struct {
105 			int test_num;
106 		} do_test;
107 		struct {
108 			int test_num;
109 			int sub_succ_cnt;
110 			int error_cnt;
111 			int skip_cnt;
112 			bool have_log;
113 		} test_done;
114 		struct {
115 			char log_buf[MAX_LOG_TRUNK_SIZE + 1];
116 			bool is_last;
117 		} test_log;
118 	};
119 };
120 
121 extern struct test_env env;
122 
123 extern void test__force_log();
124 extern bool test__start_subtest(const char *name);
125 extern void test__skip(void);
126 extern void test__fail(void);
127 extern int test__join_cgroup(const char *path);
128 
129 #define PRINT_FAIL(format...)                                                  \
130 	({                                                                     \
131 		test__fail();                                                  \
132 		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
133 		fprintf(stdout, ##format);                                     \
134 	})
135 
136 #define _CHECK(condition, tag, duration, format...) ({			\
137 	int __ret = !!(condition);					\
138 	int __save_errno = errno;					\
139 	if (__ret) {							\
140 		test__fail();						\
141 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
142 		fprintf(stdout, ##format);				\
143 	} else {							\
144 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
145 		       __func__, tag, duration);			\
146 	}								\
147 	errno = __save_errno;						\
148 	__ret;								\
149 })
150 
151 #define CHECK_FAIL(condition) ({					\
152 	int __ret = !!(condition);					\
153 	int __save_errno = errno;					\
154 	if (__ret) {							\
155 		test__fail();						\
156 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
157 	}								\
158 	errno = __save_errno;						\
159 	__ret;								\
160 })
161 
162 #define CHECK(condition, tag, format...) \
163 	_CHECK(condition, tag, duration, format)
164 #define CHECK_ATTR(condition, tag, format...) \
165 	_CHECK(condition, tag, tattr.duration, format)
166 
167 #define ASSERT_TRUE(actual, name) ({					\
168 	static int duration = 0;					\
169 	bool ___ok = (actual);						\
170 	CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name));	\
171 	___ok;								\
172 })
173 
174 #define ASSERT_FALSE(actual, name) ({					\
175 	static int duration = 0;					\
176 	bool ___ok = !(actual);						\
177 	CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name));	\
178 	___ok;								\
179 })
180 
181 #define ASSERT_EQ(actual, expected, name) ({				\
182 	static int duration = 0;					\
183 	typeof(actual) ___act = (actual);				\
184 	typeof(expected) ___exp = (expected);				\
185 	bool ___ok = ___act == ___exp;					\
186 	CHECK(!___ok, (name),						\
187 	      "unexpected %s: actual %lld != expected %lld\n",		\
188 	      (name), (long long)(___act), (long long)(___exp));	\
189 	___ok;								\
190 })
191 
192 #define ASSERT_NEQ(actual, expected, name) ({				\
193 	static int duration = 0;					\
194 	typeof(actual) ___act = (actual);				\
195 	typeof(expected) ___exp = (expected);				\
196 	bool ___ok = ___act != ___exp;					\
197 	CHECK(!___ok, (name),						\
198 	      "unexpected %s: actual %lld == expected %lld\n",		\
199 	      (name), (long long)(___act), (long long)(___exp));	\
200 	___ok;								\
201 })
202 
203 #define ASSERT_LT(actual, expected, name) ({				\
204 	static int duration = 0;					\
205 	typeof(actual) ___act = (actual);				\
206 	typeof(expected) ___exp = (expected);				\
207 	bool ___ok = ___act < ___exp;					\
208 	CHECK(!___ok, (name),						\
209 	      "unexpected %s: actual %lld >= expected %lld\n",		\
210 	      (name), (long long)(___act), (long long)(___exp));	\
211 	___ok;								\
212 })
213 
214 #define ASSERT_LE(actual, expected, name) ({				\
215 	static int duration = 0;					\
216 	typeof(actual) ___act = (actual);				\
217 	typeof(expected) ___exp = (expected);				\
218 	bool ___ok = ___act <= ___exp;					\
219 	CHECK(!___ok, (name),						\
220 	      "unexpected %s: actual %lld > expected %lld\n",		\
221 	      (name), (long long)(___act), (long long)(___exp));	\
222 	___ok;								\
223 })
224 
225 #define ASSERT_GT(actual, expected, name) ({				\
226 	static int duration = 0;					\
227 	typeof(actual) ___act = (actual);				\
228 	typeof(expected) ___exp = (expected);				\
229 	bool ___ok = ___act > ___exp;					\
230 	CHECK(!___ok, (name),						\
231 	      "unexpected %s: actual %lld <= expected %lld\n",		\
232 	      (name), (long long)(___act), (long long)(___exp));	\
233 	___ok;								\
234 })
235 
236 #define ASSERT_GE(actual, expected, name) ({				\
237 	static int duration = 0;					\
238 	typeof(actual) ___act = (actual);				\
239 	typeof(expected) ___exp = (expected);				\
240 	bool ___ok = ___act >= ___exp;					\
241 	CHECK(!___ok, (name),						\
242 	      "unexpected %s: actual %lld < expected %lld\n",		\
243 	      (name), (long long)(___act), (long long)(___exp));	\
244 	___ok;								\
245 })
246 
247 #define ASSERT_STREQ(actual, expected, name) ({				\
248 	static int duration = 0;					\
249 	const char *___act = actual;					\
250 	const char *___exp = expected;					\
251 	bool ___ok = strcmp(___act, ___exp) == 0;			\
252 	CHECK(!___ok, (name),						\
253 	      "unexpected %s: actual '%s' != expected '%s'\n",		\
254 	      (name), ___act, ___exp);					\
255 	___ok;								\
256 })
257 
258 #define ASSERT_STRNEQ(actual, expected, len, name) ({			\
259 	static int duration = 0;					\
260 	const char *___act = actual;					\
261 	const char *___exp = expected;					\
262 	int ___len = len;						\
263 	bool ___ok = strncmp(___act, ___exp, ___len) == 0;		\
264 	CHECK(!___ok, (name),						\
265 	      "unexpected %s: actual '%.*s' != expected '%.*s'\n",	\
266 	      (name), ___len, ___act, ___len, ___exp);			\
267 	___ok;								\
268 })
269 
270 #define ASSERT_OK(res, name) ({						\
271 	static int duration = 0;					\
272 	long long ___res = (res);					\
273 	bool ___ok = ___res == 0;					\
274 	CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n",	\
275 	      ___res, errno);						\
276 	___ok;								\
277 })
278 
279 #define ASSERT_ERR(res, name) ({					\
280 	static int duration = 0;					\
281 	long long ___res = (res);					\
282 	bool ___ok = ___res < 0;					\
283 	CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);	\
284 	___ok;								\
285 })
286 
287 #define ASSERT_NULL(ptr, name) ({					\
288 	static int duration = 0;					\
289 	const void *___res = (ptr);					\
290 	bool ___ok = !___res;						\
291 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
292 	___ok;								\
293 })
294 
295 #define ASSERT_OK_PTR(ptr, name) ({					\
296 	static int duration = 0;					\
297 	const void *___res = (ptr);					\
298 	int ___err = libbpf_get_error(___res);				\
299 	bool ___ok = ___err == 0;					\
300 	CHECK(!___ok, (name), "unexpected error: %d\n", ___err);	\
301 	___ok;								\
302 })
303 
304 #define ASSERT_ERR_PTR(ptr, name) ({					\
305 	static int duration = 0;					\
306 	const void *___res = (ptr);					\
307 	int ___err = libbpf_get_error(___res);				\
308 	bool ___ok = ___err != 0;					\
309 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
310 	___ok;								\
311 })
312 
313 static inline __u64 ptr_to_u64(const void *ptr)
314 {
315 	return (__u64) (unsigned long) ptr;
316 }
317 
318 static inline void *u64_to_ptr(__u64 ptr)
319 {
320 	return (void *) (unsigned long) ptr;
321 }
322 
323 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
324 int compare_map_keys(int map1_fd, int map2_fd);
325 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
326 int extract_build_id(char *build_id, size_t size);
327 int kern_sync_rcu(void);
328 int trigger_module_test_read(int read_sz);
329 int trigger_module_test_write(int write_sz);
330 
331 #ifdef __x86_64__
332 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
333 #elif defined(__s390x__)
334 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
335 #else
336 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
337 #endif
338 
339 #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
340