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