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 <netinet/tcp.h>
20 #include <linux/filter.h>
21 #include <linux/perf_event.h>
22 #include <linux/socket.h>
23 #include <linux/unistd.h>
24 
25 #include <sys/ioctl.h>
26 #include <sys/wait.h>
27 #include <sys/types.h>
28 #include <sys/time.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 #include "flow_dissector_load.h"
42 
43 enum verbosity {
44 	VERBOSE_NONE,
45 	VERBOSE_NORMAL,
46 	VERBOSE_VERY,
47 	VERBOSE_SUPER,
48 };
49 
50 struct str_set {
51 	const char **strs;
52 	int cnt;
53 };
54 
55 struct test_selector {
56 	struct str_set whitelist;
57 	struct str_set blacklist;
58 	bool *num_set;
59 	int num_set_len;
60 };
61 
62 struct test_env {
63 	struct test_selector test_selector;
64 	struct test_selector subtest_selector;
65 	bool verifier_stats;
66 	enum verbosity verbosity;
67 
68 	bool jit_enabled;
69 
70 	struct prog_test_def *test;
71 	FILE *stdout;
72 	FILE *stderr;
73 	char *log_buf;
74 	size_t log_cnt;
75 	int nr_cpus;
76 
77 	int succ_cnt; /* successful tests */
78 	int sub_succ_cnt; /* successful sub-tests */
79 	int fail_cnt; /* total failed tests + sub-tests */
80 	int skip_cnt; /* skipped tests */
81 };
82 
83 extern struct test_env env;
84 
85 extern void test__force_log();
86 extern bool test__start_subtest(const char *name);
87 extern void test__skip(void);
88 extern void test__fail(void);
89 extern int test__join_cgroup(const char *path);
90 
91 #define PRINT_FAIL(format...)                                                  \
92 	({                                                                     \
93 		test__fail();                                                  \
94 		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
95 		fprintf(stdout, ##format);                                     \
96 	})
97 
98 #define _CHECK(condition, tag, duration, format...) ({			\
99 	int __ret = !!(condition);					\
100 	int __save_errno = errno;					\
101 	if (__ret) {							\
102 		test__fail();						\
103 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
104 		fprintf(stdout, ##format);				\
105 	} else {							\
106 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
107 		       __func__, tag, duration);			\
108 	}								\
109 	errno = __save_errno;						\
110 	__ret;								\
111 })
112 
113 #define CHECK_FAIL(condition) ({					\
114 	int __ret = !!(condition);					\
115 	int __save_errno = errno;					\
116 	if (__ret) {							\
117 		test__fail();						\
118 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
119 	}								\
120 	errno = __save_errno;						\
121 	__ret;								\
122 })
123 
124 #define CHECK(condition, tag, format...) \
125 	_CHECK(condition, tag, duration, format)
126 #define CHECK_ATTR(condition, tag, format...) \
127 	_CHECK(condition, tag, tattr.duration, format)
128 
129 static inline __u64 ptr_to_u64(const void *ptr)
130 {
131 	return (__u64) (unsigned long) ptr;
132 }
133 
134 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
135 int compare_map_keys(int map1_fd, int map2_fd);
136 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
137 int extract_build_id(char *build_id, size_t size);
138 
139 #ifdef __x86_64__
140 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
141 #elif defined(__s390x__)
142 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
143 #else
144 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
145 #endif
146