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>
13typedef __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 <fcntl.h>
29#include <pthread.h>
30#include <linux/bpf.h>
31#include <linux/err.h>
32#include <bpf/bpf.h>
33#include <bpf/libbpf.h>
34
35#include "test_iptunnel_common.h"
36#include "bpf_util.h"
37#include <bpf/bpf_endian.h>
38#include "trace_helpers.h"
39#include "testing_helpers.h"
40#include "flow_dissector_load.h"
41
42enum verbosity {
43	VERBOSE_NONE,
44	VERBOSE_NORMAL,
45	VERBOSE_VERY,
46	VERBOSE_SUPER,
47};
48
49struct str_set {
50	const char **strs;
51	int cnt;
52};
53
54struct test_selector {
55	struct str_set whitelist;
56	struct str_set blacklist;
57	bool *num_set;
58	int num_set_len;
59};
60
61struct test_env {
62	struct test_selector test_selector;
63	struct test_selector subtest_selector;
64	bool verifier_stats;
65	enum verbosity verbosity;
66
67	bool jit_enabled;
68	bool get_test_cnt;
69	bool list_test_names;
70
71	struct prog_test_def *test;
72	FILE *stdout;
73	FILE *stderr;
74	char *log_buf;
75	size_t log_cnt;
76	int nr_cpus;
77
78	int succ_cnt; /* successful tests */
79	int sub_succ_cnt; /* successful sub-tests */
80	int fail_cnt; /* total failed tests + sub-tests */
81	int skip_cnt; /* skipped tests */
82
83	int saved_netns_fd;
84};
85
86extern struct test_env env;
87
88extern void test__force_log();
89extern bool test__start_subtest(const char *name);
90extern void test__skip(void);
91extern void test__fail(void);
92extern int test__join_cgroup(const char *path);
93
94#define PRINT_FAIL(format...)                                                  \
95	({                                                                     \
96		test__fail();                                                  \
97		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
98		fprintf(stdout, ##format);                                     \
99	})
100
101#define _CHECK(condition, tag, duration, format...) ({			\
102	int __ret = !!(condition);					\
103	int __save_errno = errno;					\
104	if (__ret) {							\
105		test__fail();						\
106		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
107		fprintf(stdout, ##format);				\
108	} else {							\
109		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
110		       __func__, tag, duration);			\
111	}								\
112	errno = __save_errno;						\
113	__ret;								\
114})
115
116#define CHECK_FAIL(condition) ({					\
117	int __ret = !!(condition);					\
118	int __save_errno = errno;					\
119	if (__ret) {							\
120		test__fail();						\
121		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
122	}								\
123	errno = __save_errno;						\
124	__ret;								\
125})
126
127#define CHECK(condition, tag, format...) \
128	_CHECK(condition, tag, duration, format)
129#define CHECK_ATTR(condition, tag, format...) \
130	_CHECK(condition, tag, tattr.duration, format)
131
132#define ASSERT_EQ(actual, expected, name) ({				\
133	static int duration = 0;					\
134	typeof(actual) ___act = (actual);				\
135	typeof(expected) ___exp = (expected);				\
136	bool ___ok = ___act == ___exp;					\
137	CHECK(!___ok, (name),						\
138	      "unexpected %s: actual %lld != expected %lld\n",		\
139	      (name), (long long)(___act), (long long)(___exp));	\
140	___ok;								\
141})
142
143#define ASSERT_STREQ(actual, expected, name) ({				\
144	static int duration = 0;					\
145	const char *___act = actual;					\
146	const char *___exp = expected;					\
147	bool ___ok = strcmp(___act, ___exp) == 0;			\
148	CHECK(!___ok, (name),						\
149	      "unexpected %s: actual '%s' != expected '%s'\n",		\
150	      (name), ___act, ___exp);					\
151	___ok;								\
152})
153
154#define ASSERT_OK(res, name) ({						\
155	static int duration = 0;					\
156	long long ___res = (res);					\
157	bool ___ok = ___res == 0;					\
158	CHECK(!___ok, (name), "unexpected error: %lld\n", ___res);	\
159	___ok;								\
160})
161
162#define ASSERT_ERR(res, name) ({					\
163	static int duration = 0;					\
164	long long ___res = (res);					\
165	bool ___ok = ___res < 0;					\
166	CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);	\
167	___ok;								\
168})
169
170#define ASSERT_NULL(ptr, name) ({					\
171	static int duration = 0;					\
172	const void *___res = (ptr);					\
173	bool ___ok = !___res;						\
174	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
175	___ok;								\
176})
177
178#define ASSERT_OK_PTR(ptr, name) ({					\
179	static int duration = 0;					\
180	const void *___res = (ptr);					\
181	bool ___ok = !IS_ERR_OR_NULL(___res);				\
182	CHECK(!___ok, (name),						\
183	      "unexpected error: %ld\n", PTR_ERR(___res));		\
184	___ok;								\
185})
186
187#define ASSERT_ERR_PTR(ptr, name) ({					\
188	static int duration = 0;					\
189	const void *___res = (ptr);					\
190	bool ___ok = IS_ERR(___res)					\
191	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
192	___ok;								\
193})
194
195static inline __u64 ptr_to_u64(const void *ptr)
196{
197	return (__u64) (unsigned long) ptr;
198}
199
200static inline void *u64_to_ptr(__u64 ptr)
201{
202	return (void *) (unsigned long) ptr;
203}
204
205int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
206int compare_map_keys(int map1_fd, int map2_fd);
207int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
208int extract_build_id(char *build_id, size_t size);
209
210#ifdef __x86_64__
211#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
212#elif defined(__s390x__)
213#define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
214#else
215#define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
216#endif
217