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 	bool has_testmod;
70 	bool get_test_cnt;
71 	bool list_test_names;
72 
73 	struct prog_test_def *test;
74 	FILE *stdout;
75 	FILE *stderr;
76 	char *log_buf;
77 	size_t log_cnt;
78 	int nr_cpus;
79 
80 	int succ_cnt; /* successful tests */
81 	int sub_succ_cnt; /* successful sub-tests */
82 	int fail_cnt; /* total failed tests + sub-tests */
83 	int skip_cnt; /* skipped tests */
84 
85 	int saved_netns_fd;
86 };
87 
88 extern struct test_env env;
89 
90 extern void test__force_log();
91 extern bool test__start_subtest(const char *name);
92 extern void test__skip(void);
93 extern void test__fail(void);
94 extern int test__join_cgroup(const char *path);
95 
96 #define PRINT_FAIL(format...)                                                  \
97 	({                                                                     \
98 		test__fail();                                                  \
99 		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
100 		fprintf(stdout, ##format);                                     \
101 	})
102 
103 #define _CHECK(condition, tag, duration, format...) ({			\
104 	int __ret = !!(condition);					\
105 	int __save_errno = errno;					\
106 	if (__ret) {							\
107 		test__fail();						\
108 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
109 		fprintf(stdout, ##format);				\
110 	} else {							\
111 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
112 		       __func__, tag, duration);			\
113 	}								\
114 	errno = __save_errno;						\
115 	__ret;								\
116 })
117 
118 #define CHECK_FAIL(condition) ({					\
119 	int __ret = !!(condition);					\
120 	int __save_errno = errno;					\
121 	if (__ret) {							\
122 		test__fail();						\
123 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
124 	}								\
125 	errno = __save_errno;						\
126 	__ret;								\
127 })
128 
129 #define CHECK(condition, tag, format...) \
130 	_CHECK(condition, tag, duration, format)
131 #define CHECK_ATTR(condition, tag, format...) \
132 	_CHECK(condition, tag, tattr.duration, format)
133 
134 #define ASSERT_EQ(actual, expected, name) ({				\
135 	static int duration = 0;					\
136 	typeof(actual) ___act = (actual);				\
137 	typeof(expected) ___exp = (expected);				\
138 	bool ___ok = ___act == ___exp;					\
139 	CHECK(!___ok, (name),						\
140 	      "unexpected %s: actual %lld != expected %lld\n",		\
141 	      (name), (long long)(___act), (long long)(___exp));	\
142 	___ok;								\
143 })
144 
145 #define ASSERT_NEQ(actual, expected, name) ({				\
146 	static int duration = 0;					\
147 	typeof(actual) ___act = (actual);				\
148 	typeof(expected) ___exp = (expected);				\
149 	bool ___ok = ___act != ___exp;					\
150 	CHECK(!___ok, (name),						\
151 	      "unexpected %s: actual %lld == expected %lld\n",		\
152 	      (name), (long long)(___act), (long long)(___exp));	\
153 	___ok;								\
154 })
155 
156 #define ASSERT_STREQ(actual, expected, name) ({				\
157 	static int duration = 0;					\
158 	const char *___act = actual;					\
159 	const char *___exp = expected;					\
160 	bool ___ok = strcmp(___act, ___exp) == 0;			\
161 	CHECK(!___ok, (name),						\
162 	      "unexpected %s: actual '%s' != expected '%s'\n",		\
163 	      (name), ___act, ___exp);					\
164 	___ok;								\
165 })
166 
167 #define ASSERT_OK(res, name) ({						\
168 	static int duration = 0;					\
169 	long long ___res = (res);					\
170 	bool ___ok = ___res == 0;					\
171 	CHECK(!___ok, (name), "unexpected error: %lld\n", ___res);	\
172 	___ok;								\
173 })
174 
175 #define ASSERT_ERR(res, name) ({					\
176 	static int duration = 0;					\
177 	long long ___res = (res);					\
178 	bool ___ok = ___res < 0;					\
179 	CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);	\
180 	___ok;								\
181 })
182 
183 #define ASSERT_NULL(ptr, name) ({					\
184 	static int duration = 0;					\
185 	const void *___res = (ptr);					\
186 	bool ___ok = !___res;						\
187 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
188 	___ok;								\
189 })
190 
191 #define ASSERT_OK_PTR(ptr, name) ({					\
192 	static int duration = 0;					\
193 	const void *___res = (ptr);					\
194 	bool ___ok = !IS_ERR_OR_NULL(___res);				\
195 	CHECK(!___ok, (name),						\
196 	      "unexpected error: %ld\n", PTR_ERR(___res));		\
197 	___ok;								\
198 })
199 
200 #define ASSERT_ERR_PTR(ptr, name) ({					\
201 	static int duration = 0;					\
202 	const void *___res = (ptr);					\
203 	bool ___ok = IS_ERR(___res)					\
204 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
205 	___ok;								\
206 })
207 
208 static inline __u64 ptr_to_u64(const void *ptr)
209 {
210 	return (__u64) (unsigned long) ptr;
211 }
212 
213 static inline void *u64_to_ptr(__u64 ptr)
214 {
215 	return (void *) (unsigned long) ptr;
216 }
217 
218 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
219 int compare_map_keys(int map1_fd, int map2_fd);
220 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
221 int extract_build_id(char *build_id, size_t size);
222 
223 #ifdef __x86_64__
224 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
225 #elif defined(__s390x__)
226 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
227 #else
228 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
229 #endif
230