13f306588SStanislav Fomichev /* SPDX-License-Identifier: GPL-2.0 */
23f306588SStanislav Fomichev #include <stdio.h>
33f306588SStanislav Fomichev #include <unistd.h>
43f306588SStanislav Fomichev #include <errno.h>
53f306588SStanislav Fomichev #include <string.h>
63f306588SStanislav Fomichev #include <assert.h>
73f306588SStanislav Fomichev #include <stdlib.h>
83f306588SStanislav Fomichev #include <stdarg.h>
93f306588SStanislav Fomichev #include <time.h>
103f306588SStanislav Fomichev #include <signal.h>
113f306588SStanislav Fomichev 
123f306588SStanislav Fomichev #include <linux/types.h>
133f306588SStanislav Fomichev typedef __u16 __sum16;
143f306588SStanislav Fomichev #include <arpa/inet.h>
153f306588SStanislav Fomichev #include <linux/if_ether.h>
163f306588SStanislav Fomichev #include <linux/if_packet.h>
173f306588SStanislav Fomichev #include <linux/ip.h>
183f306588SStanislav Fomichev #include <linux/ipv6.h>
199a365e67SStanislav Fomichev #include <netinet/tcp.h>
203f306588SStanislav Fomichev #include <linux/filter.h>
213f306588SStanislav Fomichev #include <linux/perf_event.h>
229a365e67SStanislav Fomichev #include <linux/socket.h>
233f306588SStanislav Fomichev #include <linux/unistd.h>
243f306588SStanislav Fomichev 
253f306588SStanislav Fomichev #include <sys/ioctl.h>
263f306588SStanislav Fomichev #include <sys/wait.h>
273f306588SStanislav Fomichev #include <sys/types.h>
283f306588SStanislav Fomichev #include <sys/time.h>
293f306588SStanislav Fomichev #include <fcntl.h>
303f306588SStanislav Fomichev #include <pthread.h>
313f306588SStanislav Fomichev #include <linux/bpf.h>
323f306588SStanislav Fomichev #include <linux/err.h>
333f306588SStanislav Fomichev #include <bpf/bpf.h>
343f306588SStanislav Fomichev #include <bpf/libbpf.h>
353f306588SStanislav Fomichev 
363f306588SStanislav Fomichev #include "test_iptunnel_common.h"
373f306588SStanislav Fomichev #include "bpf_util.h"
383e689141SToke Høiland-Jørgensen #include <bpf/bpf_endian.h>
393f306588SStanislav Fomichev #include "trace_helpers.h"
40cd49291cSAndrii Nakryiko #include "testing_helpers.h"
413f306588SStanislav Fomichev #include "flow_dissector_load.h"
423f306588SStanislav Fomichev 
43a8fdaad5SAndrii Nakryiko enum verbosity {
44a8fdaad5SAndrii Nakryiko 	VERBOSE_NONE,
45a8fdaad5SAndrii Nakryiko 	VERBOSE_NORMAL,
46a8fdaad5SAndrii Nakryiko 	VERBOSE_VERY,
47a8fdaad5SAndrii Nakryiko 	VERBOSE_SUPER,
48a8fdaad5SAndrii Nakryiko };
49a8fdaad5SAndrii Nakryiko 
50b65053cdSAndrii Nakryiko struct str_set {
51b65053cdSAndrii Nakryiko 	const char **strs;
52b65053cdSAndrii Nakryiko 	int cnt;
53b65053cdSAndrii Nakryiko };
54b65053cdSAndrii Nakryiko 
553a516a0aSAndrii Nakryiko struct test_selector {
56b65053cdSAndrii Nakryiko 	struct str_set whitelist;
57b65053cdSAndrii Nakryiko 	struct str_set blacklist;
583a516a0aSAndrii Nakryiko 	bool *num_set;
593a516a0aSAndrii Nakryiko 	int num_set_len;
603a516a0aSAndrii Nakryiko };
613a516a0aSAndrii Nakryiko 
620ff97e56SAndrii Nakryiko struct test_env {
633a516a0aSAndrii Nakryiko 	struct test_selector test_selector;
643a516a0aSAndrii Nakryiko 	struct test_selector subtest_selector;
650ff97e56SAndrii Nakryiko 	bool verifier_stats;
66a8fdaad5SAndrii Nakryiko 	enum verbosity verbosity;
670ff97e56SAndrii Nakryiko 
680ff97e56SAndrii Nakryiko 	bool jit_enabled;
690ff97e56SAndrii Nakryiko 
700ff97e56SAndrii Nakryiko 	struct prog_test_def *test;
71946152b3SStanislav Fomichev 	FILE *stdout;
72946152b3SStanislav Fomichev 	FILE *stderr;
730ff97e56SAndrii Nakryiko 	char *log_buf;
740ff97e56SAndrii Nakryiko 	size_t log_cnt;
75fd27b183SAndrii Nakryiko 	int nr_cpus;
760ff97e56SAndrii Nakryiko 
773a516a0aSAndrii Nakryiko 	int succ_cnt; /* successful tests */
783a516a0aSAndrii Nakryiko 	int sub_succ_cnt; /* successful sub-tests */
793a516a0aSAndrii Nakryiko 	int fail_cnt; /* total failed tests + sub-tests */
80cd9c21d7SStanislav Fomichev 	int skip_cnt; /* skipped tests */
810ff97e56SAndrii Nakryiko };
820ff97e56SAndrii Nakryiko 
830ff97e56SAndrii Nakryiko extern struct test_env env;
840ff97e56SAndrii Nakryiko 
850ff97e56SAndrii Nakryiko extern void test__force_log();
863a516a0aSAndrii Nakryiko extern bool test__start_subtest(const char *name);
87cd9c21d7SStanislav Fomichev extern void test__skip(void);
88d38835b7SStanislav Fomichev extern void test__fail(void);
8988dadc63SStanislav Fomichev extern int test__join_cgroup(const char *path);
903f306588SStanislav Fomichev 
9123458901SLorenz Bauer #define PRINT_FAIL(format...)                                                  \
9223458901SLorenz Bauer 	({                                                                     \
9323458901SLorenz Bauer 		test__fail();                                                  \
9423458901SLorenz Bauer 		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
9523458901SLorenz Bauer 		fprintf(stdout, ##format);                                     \
9623458901SLorenz Bauer 	})
9723458901SLorenz Bauer 
983f306588SStanislav Fomichev #define _CHECK(condition, tag, duration, format...) ({			\
993f306588SStanislav Fomichev 	int __ret = !!(condition);					\
100478bee0dSAndrey Ignatov 	int __save_errno = errno;					\
1013f306588SStanislav Fomichev 	if (__ret) {							\
102d38835b7SStanislav Fomichev 		test__fail();						\
1033e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
1043e2671fbSAndrii Nakryiko 		fprintf(stdout, ##format);				\
1053f306588SStanislav Fomichev 	} else {							\
1063e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
1070ff97e56SAndrii Nakryiko 		       __func__, tag, duration);			\
1083f306588SStanislav Fomichev 	}								\
109478bee0dSAndrey Ignatov 	errno = __save_errno;						\
1103f306588SStanislav Fomichev 	__ret;								\
1113f306588SStanislav Fomichev })
1123f306588SStanislav Fomichev 
113d38835b7SStanislav Fomichev #define CHECK_FAIL(condition) ({					\
114d38835b7SStanislav Fomichev 	int __ret = !!(condition);					\
115478bee0dSAndrey Ignatov 	int __save_errno = errno;					\
116d38835b7SStanislav Fomichev 	if (__ret) {							\
117d38835b7SStanislav Fomichev 		test__fail();						\
1183e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
119d38835b7SStanislav Fomichev 	}								\
120478bee0dSAndrey Ignatov 	errno = __save_errno;						\
121d38835b7SStanislav Fomichev 	__ret;								\
122d38835b7SStanislav Fomichev })
123d38835b7SStanislav Fomichev 
1243f306588SStanislav Fomichev #define CHECK(condition, tag, format...) \
1253f306588SStanislav Fomichev 	_CHECK(condition, tag, duration, format)
1263f306588SStanislav Fomichev #define CHECK_ATTR(condition, tag, format...) \
1273f306588SStanislav Fomichev 	_CHECK(condition, tag, tattr.duration, format)
1283f306588SStanislav Fomichev 
129886225bbSStanislav Fomichev static inline __u64 ptr_to_u64(const void *ptr)
130886225bbSStanislav Fomichev {
131886225bbSStanislav Fomichev 	return (__u64) (unsigned long) ptr;
132886225bbSStanislav Fomichev }
133886225bbSStanislav Fomichev 
1343f306588SStanislav Fomichev int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
135615741d8SStanislav Fomichev int compare_map_keys(int map1_fd, int map2_fd);
136615741d8SStanislav Fomichev int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
137615741d8SStanislav Fomichev int extract_build_id(char *build_id, size_t size);
1381cb59a60SIlya Leoshkevich 
1391cb59a60SIlya Leoshkevich #ifdef __x86_64__
1401cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
1411cb59a60SIlya Leoshkevich #elif defined(__s390x__)
1421cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
1431cb59a60SIlya Leoshkevich #else
1441cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
1451cb59a60SIlya Leoshkevich #endif
146