xref: /linux/tools/testing/selftests/bpf/test_progs.h (revision 156010ed9c2ac1e9df6c11b1f688cf8a6e0152e6)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __TEST_PROGS_H
3 #define __TEST_PROGS_H
4 
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <time.h>
13 #include <signal.h>
14 
15 #include <linux/types.h>
16 typedef __u16 __sum16;
17 #include <arpa/inet.h>
18 #include <linux/if_ether.h>
19 #include <linux/if_packet.h>
20 #include <linux/ip.h>
21 #include <linux/ipv6.h>
22 #include <linux/filter.h>
23 #include <linux/perf_event.h>
24 #include <linux/socket.h>
25 #include <linux/unistd.h>
26 
27 #include <sys/ioctl.h>
28 #include <sys/wait.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/param.h>
32 #include <fcntl.h>
33 #include <pthread.h>
34 #include <linux/bpf.h>
35 #include <linux/err.h>
36 #include <bpf/bpf.h>
37 #include <bpf/libbpf.h>
38 
39 #include "test_iptunnel_common.h"
40 #include "bpf_util.h"
41 #include <bpf/bpf_endian.h>
42 #include "trace_helpers.h"
43 #include "testing_helpers.h"
44 
45 enum verbosity {
46 	VERBOSE_NONE,
47 	VERBOSE_NORMAL,
48 	VERBOSE_VERY,
49 	VERBOSE_SUPER,
50 };
51 
52 struct test_filter {
53 	char *name;
54 	char **subtests;
55 	int subtest_cnt;
56 };
57 
58 struct test_filter_set {
59 	struct test_filter *tests;
60 	int cnt;
61 };
62 
63 struct test_selector {
64 	struct test_filter_set whitelist;
65 	struct test_filter_set blacklist;
66 	bool *num_set;
67 	int num_set_len;
68 };
69 
70 struct subtest_state {
71 	char *name;
72 	size_t log_cnt;
73 	char *log_buf;
74 	int error_cnt;
75 	bool skipped;
76 	bool filtered;
77 
78 	FILE *stdout;
79 };
80 
81 struct test_state {
82 	bool tested;
83 	bool force_log;
84 
85 	int error_cnt;
86 	int skip_cnt;
87 	int sub_succ_cnt;
88 
89 	struct subtest_state *subtest_states;
90 	int subtest_num;
91 
92 	size_t log_cnt;
93 	char *log_buf;
94 
95 	FILE *stdout;
96 };
97 
98 struct test_env {
99 	struct test_selector test_selector;
100 	struct test_selector subtest_selector;
101 	bool verifier_stats;
102 	bool debug;
103 	enum verbosity verbosity;
104 
105 	bool jit_enabled;
106 	bool has_testmod;
107 	bool get_test_cnt;
108 	bool list_test_names;
109 
110 	struct prog_test_def *test; /* current running test */
111 	struct test_state *test_state; /* current running test state */
112 	struct subtest_state *subtest_state; /* current running subtest state */
113 
114 	FILE *stdout;
115 	FILE *stderr;
116 	int nr_cpus;
117 
118 	int succ_cnt; /* successful tests */
119 	int sub_succ_cnt; /* successful sub-tests */
120 	int fail_cnt; /* total failed tests + sub-tests */
121 	int skip_cnt; /* skipped tests */
122 
123 	int saved_netns_fd;
124 	int workers; /* number of worker process */
125 	int worker_id; /* id number of current worker, main process is -1 */
126 	pid_t *worker_pids; /* array of worker pids */
127 	int *worker_socks; /* array of worker socks */
128 	int *worker_current_test; /* array of current running test for each worker */
129 };
130 
131 #define MAX_LOG_TRUNK_SIZE 8192
132 #define MAX_SUBTEST_NAME 1024
133 enum msg_type {
134 	MSG_DO_TEST = 0,
135 	MSG_TEST_DONE = 1,
136 	MSG_TEST_LOG = 2,
137 	MSG_SUBTEST_DONE = 3,
138 	MSG_EXIT = 255,
139 };
140 struct msg {
141 	enum msg_type type;
142 	union {
143 		struct {
144 			int num;
145 		} do_test;
146 		struct {
147 			int num;
148 			int sub_succ_cnt;
149 			int error_cnt;
150 			int skip_cnt;
151 			bool have_log;
152 			int subtest_num;
153 		} test_done;
154 		struct {
155 			char log_buf[MAX_LOG_TRUNK_SIZE + 1];
156 			bool is_last;
157 		} test_log;
158 		struct {
159 			int num;
160 			char name[MAX_SUBTEST_NAME + 1];
161 			int error_cnt;
162 			bool skipped;
163 			bool filtered;
164 			bool have_log;
165 		} subtest_done;
166 	};
167 };
168 
169 extern struct test_env env;
170 
171 void test__force_log(void);
172 bool test__start_subtest(const char *name);
173 void test__end_subtest(void);
174 void test__skip(void);
175 void test__fail(void);
176 int test__join_cgroup(const char *path);
177 
178 #define PRINT_FAIL(format...)                                                  \
179 	({                                                                     \
180 		test__fail();                                                  \
181 		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
182 		fprintf(stdout, ##format);                                     \
183 	})
184 
185 #define _CHECK(condition, tag, duration, format...) ({			\
186 	int __ret = !!(condition);					\
187 	int __save_errno = errno;					\
188 	if (__ret) {							\
189 		test__fail();						\
190 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
191 		fprintf(stdout, ##format);				\
192 	} else {							\
193 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
194 		       __func__, tag, duration);			\
195 	}								\
196 	errno = __save_errno;						\
197 	__ret;								\
198 })
199 
200 #define CHECK_FAIL(condition) ({					\
201 	int __ret = !!(condition);					\
202 	int __save_errno = errno;					\
203 	if (__ret) {							\
204 		test__fail();						\
205 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
206 	}								\
207 	errno = __save_errno;						\
208 	__ret;								\
209 })
210 
211 #define CHECK(condition, tag, format...) \
212 	_CHECK(condition, tag, duration, format)
213 #define CHECK_ATTR(condition, tag, format...) \
214 	_CHECK(condition, tag, tattr.duration, format)
215 
216 #define ASSERT_FAIL(fmt, args...) ({					\
217 	static int duration = 0;					\
218 	CHECK(false, "", fmt"\n", ##args);				\
219 	false;								\
220 })
221 
222 #define ASSERT_TRUE(actual, name) ({					\
223 	static int duration = 0;					\
224 	bool ___ok = (actual);						\
225 	CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name));	\
226 	___ok;								\
227 })
228 
229 #define ASSERT_FALSE(actual, name) ({					\
230 	static int duration = 0;					\
231 	bool ___ok = !(actual);						\
232 	CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name));	\
233 	___ok;								\
234 })
235 
236 #define ASSERT_EQ(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_NEQ(actual, expected, name) ({				\
248 	static int duration = 0;					\
249 	typeof(actual) ___act = (actual);				\
250 	typeof(expected) ___exp = (expected);				\
251 	bool ___ok = ___act != ___exp;					\
252 	CHECK(!___ok, (name),						\
253 	      "unexpected %s: actual %lld == expected %lld\n",		\
254 	      (name), (long long)(___act), (long long)(___exp));	\
255 	___ok;								\
256 })
257 
258 #define ASSERT_LT(actual, expected, name) ({				\
259 	static int duration = 0;					\
260 	typeof(actual) ___act = (actual);				\
261 	typeof(expected) ___exp = (expected);				\
262 	bool ___ok = ___act < ___exp;					\
263 	CHECK(!___ok, (name),						\
264 	      "unexpected %s: actual %lld >= expected %lld\n",		\
265 	      (name), (long long)(___act), (long long)(___exp));	\
266 	___ok;								\
267 })
268 
269 #define ASSERT_LE(actual, expected, name) ({				\
270 	static int duration = 0;					\
271 	typeof(actual) ___act = (actual);				\
272 	typeof(expected) ___exp = (expected);				\
273 	bool ___ok = ___act <= ___exp;					\
274 	CHECK(!___ok, (name),						\
275 	      "unexpected %s: actual %lld > expected %lld\n",		\
276 	      (name), (long long)(___act), (long long)(___exp));	\
277 	___ok;								\
278 })
279 
280 #define ASSERT_GT(actual, expected, name) ({				\
281 	static int duration = 0;					\
282 	typeof(actual) ___act = (actual);				\
283 	typeof(expected) ___exp = (expected);				\
284 	bool ___ok = ___act > ___exp;					\
285 	CHECK(!___ok, (name),						\
286 	      "unexpected %s: actual %lld <= expected %lld\n",		\
287 	      (name), (long long)(___act), (long long)(___exp));	\
288 	___ok;								\
289 })
290 
291 #define ASSERT_GE(actual, expected, name) ({				\
292 	static int duration = 0;					\
293 	typeof(actual) ___act = (actual);				\
294 	typeof(expected) ___exp = (expected);				\
295 	bool ___ok = ___act >= ___exp;					\
296 	CHECK(!___ok, (name),						\
297 	      "unexpected %s: actual %lld < expected %lld\n",		\
298 	      (name), (long long)(___act), (long long)(___exp));	\
299 	___ok;								\
300 })
301 
302 #define ASSERT_STREQ(actual, expected, name) ({				\
303 	static int duration = 0;					\
304 	const char *___act = actual;					\
305 	const char *___exp = expected;					\
306 	bool ___ok = strcmp(___act, ___exp) == 0;			\
307 	CHECK(!___ok, (name),						\
308 	      "unexpected %s: actual '%s' != expected '%s'\n",		\
309 	      (name), ___act, ___exp);					\
310 	___ok;								\
311 })
312 
313 #define ASSERT_STRNEQ(actual, expected, len, name) ({			\
314 	static int duration = 0;					\
315 	const char *___act = actual;					\
316 	const char *___exp = expected;					\
317 	int ___len = len;						\
318 	bool ___ok = strncmp(___act, ___exp, ___len) == 0;		\
319 	CHECK(!___ok, (name),						\
320 	      "unexpected %s: actual '%.*s' != expected '%.*s'\n",	\
321 	      (name), ___len, ___act, ___len, ___exp);			\
322 	___ok;								\
323 })
324 
325 #define ASSERT_HAS_SUBSTR(str, substr, name) ({				\
326 	static int duration = 0;					\
327 	const char *___str = str;					\
328 	const char *___substr = substr;					\
329 	bool ___ok = strstr(___str, ___substr) != NULL;			\
330 	CHECK(!___ok, (name),						\
331 	      "unexpected %s: '%s' is not a substring of '%s'\n",	\
332 	      (name), ___substr, ___str);				\
333 	___ok;								\
334 })
335 
336 #define ASSERT_OK(res, name) ({						\
337 	static int duration = 0;					\
338 	long long ___res = (res);					\
339 	bool ___ok = ___res == 0;					\
340 	CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n",	\
341 	      ___res, errno);						\
342 	___ok;								\
343 })
344 
345 #define ASSERT_ERR(res, name) ({					\
346 	static int duration = 0;					\
347 	long long ___res = (res);					\
348 	bool ___ok = ___res < 0;					\
349 	CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);	\
350 	___ok;								\
351 })
352 
353 #define ASSERT_NULL(ptr, name) ({					\
354 	static int duration = 0;					\
355 	const void *___res = (ptr);					\
356 	bool ___ok = !___res;						\
357 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
358 	___ok;								\
359 })
360 
361 #define ASSERT_OK_PTR(ptr, name) ({					\
362 	static int duration = 0;					\
363 	const void *___res = (ptr);					\
364 	int ___err = libbpf_get_error(___res);				\
365 	bool ___ok = ___err == 0;					\
366 	CHECK(!___ok, (name), "unexpected error: %d\n", ___err);	\
367 	___ok;								\
368 })
369 
370 #define ASSERT_ERR_PTR(ptr, name) ({					\
371 	static int duration = 0;					\
372 	const void *___res = (ptr);					\
373 	int ___err = libbpf_get_error(___res);				\
374 	bool ___ok = ___err != 0;					\
375 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
376 	___ok;								\
377 })
378 
379 static inline __u64 ptr_to_u64(const void *ptr)
380 {
381 	return (__u64) (unsigned long) ptr;
382 }
383 
384 static inline void *u64_to_ptr(__u64 ptr)
385 {
386 	return (void *) (unsigned long) ptr;
387 }
388 
389 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
390 int compare_map_keys(int map1_fd, int map2_fd);
391 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
392 int extract_build_id(char *build_id, size_t size);
393 int kern_sync_rcu(void);
394 int trigger_module_test_read(int read_sz);
395 int trigger_module_test_write(int write_sz);
396 int write_sysctl(const char *sysctl, const char *value);
397 
398 #ifdef __x86_64__
399 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
400 #elif defined(__s390x__)
401 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
402 #elif defined(__aarch64__)
403 #define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
404 #else
405 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
406 #endif
407 
408 #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
409 
410 struct test_loader {
411 	char *log_buf;
412 	size_t log_buf_sz;
413 
414 	struct bpf_object *obj;
415 };
416 
417 typedef const void *(*skel_elf_bytes_fn)(size_t *sz);
418 
419 extern void test_loader__run_subtests(struct test_loader *tester,
420 				      const char *skel_name,
421 				      skel_elf_bytes_fn elf_bytes_factory);
422 
423 extern void test_loader_fini(struct test_loader *tester);
424 
425 #define RUN_TESTS(skel) ({						       \
426 	struct test_loader tester = {};					       \
427 									       \
428 	test_loader__run_subtests(&tester, #skel, skel##__elf_bytes);	       \
429 	test_loader_fini(&tester);					       \
430 })
431 
432 #endif /* __TEST_PROGS_H */
433