13f306588SStanislav Fomichev /* SPDX-License-Identifier: GPL-2.0 */
2537c3f66SAndrii Nakryiko #ifndef __TEST_PROGS_H
3537c3f66SAndrii Nakryiko #define __TEST_PROGS_H
4537c3f66SAndrii Nakryiko 
53f306588SStanislav Fomichev #include <stdio.h>
63f306588SStanislav Fomichev #include <unistd.h>
73f306588SStanislav Fomichev #include <errno.h>
83f306588SStanislav Fomichev #include <string.h>
93f306588SStanislav Fomichev #include <assert.h>
103f306588SStanislav Fomichev #include <stdlib.h>
113f306588SStanislav Fomichev #include <stdarg.h>
123f306588SStanislav Fomichev #include <time.h>
133f306588SStanislav Fomichev #include <signal.h>
143f306588SStanislav Fomichev 
153f306588SStanislav Fomichev #include <linux/types.h>
163f306588SStanislav Fomichev typedef __u16 __sum16;
173f306588SStanislav Fomichev #include <arpa/inet.h>
183f306588SStanislav Fomichev #include <linux/if_ether.h>
193f306588SStanislav Fomichev #include <linux/if_packet.h>
203f306588SStanislav Fomichev #include <linux/ip.h>
213f306588SStanislav Fomichev #include <linux/ipv6.h>
223f306588SStanislav Fomichev #include <linux/filter.h>
233f306588SStanislav Fomichev #include <linux/perf_event.h>
249a365e67SStanislav Fomichev #include <linux/socket.h>
253f306588SStanislav Fomichev #include <linux/unistd.h>
263f306588SStanislav Fomichev 
273f306588SStanislav Fomichev #include <sys/ioctl.h>
283f306588SStanislav Fomichev #include <sys/wait.h>
293f306588SStanislav Fomichev #include <sys/types.h>
303f306588SStanislav Fomichev #include <sys/time.h>
31f4fd706fSGeliang Tang #include <sys/param.h>
323f306588SStanislav Fomichev #include <fcntl.h>
333f306588SStanislav Fomichev #include <pthread.h>
343f306588SStanislav Fomichev #include <linux/bpf.h>
353f306588SStanislav Fomichev #include <linux/err.h>
363f306588SStanislav Fomichev #include <bpf/bpf.h>
373f306588SStanislav Fomichev #include <bpf/libbpf.h>
383f306588SStanislav Fomichev 
393f306588SStanislav Fomichev #include "test_iptunnel_common.h"
403f306588SStanislav Fomichev #include "bpf_util.h"
413e689141SToke Høiland-Jørgensen #include <bpf/bpf_endian.h>
423f306588SStanislav Fomichev #include "trace_helpers.h"
43cd49291cSAndrii Nakryiko #include "testing_helpers.h"
443f306588SStanislav Fomichev 
45a8fdaad5SAndrii Nakryiko enum verbosity {
46a8fdaad5SAndrii Nakryiko 	VERBOSE_NONE,
47a8fdaad5SAndrii Nakryiko 	VERBOSE_NORMAL,
48a8fdaad5SAndrii Nakryiko 	VERBOSE_VERY,
49a8fdaad5SAndrii Nakryiko 	VERBOSE_SUPER,
50a8fdaad5SAndrii Nakryiko };
51a8fdaad5SAndrii Nakryiko 
5261ddff37SMykola Lysenko struct test_filter {
5361ddff37SMykola Lysenko 	char *name;
5461ddff37SMykola Lysenko 	char **subtests;
5561ddff37SMykola Lysenko 	int subtest_cnt;
5661ddff37SMykola Lysenko };
5761ddff37SMykola Lysenko 
5861ddff37SMykola Lysenko struct test_filter_set {
5961ddff37SMykola Lysenko 	struct test_filter *tests;
60b65053cdSAndrii Nakryiko 	int cnt;
61b65053cdSAndrii Nakryiko };
62b65053cdSAndrii Nakryiko 
633a516a0aSAndrii Nakryiko struct test_selector {
6461ddff37SMykola Lysenko 	struct test_filter_set whitelist;
6561ddff37SMykola Lysenko 	struct test_filter_set blacklist;
663a516a0aSAndrii Nakryiko 	bool *num_set;
673a516a0aSAndrii Nakryiko 	int num_set_len;
683a516a0aSAndrii Nakryiko };
693a516a0aSAndrii Nakryiko 
7009252259SMykola Lysenko struct subtest_state {
7109252259SMykola Lysenko 	char *name;
7209252259SMykola Lysenko 	size_t log_cnt;
7309252259SMykola Lysenko 	char *log_buf;
7409252259SMykola Lysenko 	int error_cnt;
7509252259SMykola Lysenko 	bool skipped;
762dc323b1SMykola Lysenko 	bool filtered;
7709252259SMykola Lysenko 
7809252259SMykola Lysenko 	FILE *stdout;
7909252259SMykola Lysenko };
8009252259SMykola Lysenko 
812324257dSMykola Lysenko struct test_state {
822324257dSMykola Lysenko 	bool tested;
832324257dSMykola Lysenko 	bool force_log;
842324257dSMykola Lysenko 
852324257dSMykola Lysenko 	int error_cnt;
862324257dSMykola Lysenko 	int skip_cnt;
872324257dSMykola Lysenko 	int sub_succ_cnt;
882324257dSMykola Lysenko 
8909252259SMykola Lysenko 	struct subtest_state *subtest_states;
902324257dSMykola Lysenko 	int subtest_num;
912324257dSMykola Lysenko 
922324257dSMykola Lysenko 	size_t log_cnt;
932324257dSMykola Lysenko 	char *log_buf;
9409252259SMykola Lysenko 
9509252259SMykola Lysenko 	FILE *stdout;
962324257dSMykola Lysenko };
972324257dSMykola Lysenko 
980ff97e56SAndrii Nakryiko struct test_env {
993a516a0aSAndrii Nakryiko 	struct test_selector test_selector;
1003a516a0aSAndrii Nakryiko 	struct test_selector subtest_selector;
1010ff97e56SAndrii Nakryiko 	bool verifier_stats;
10291b2c0afSYucong Sun 	bool debug;
103a8fdaad5SAndrii Nakryiko 	enum verbosity verbosity;
1040ff97e56SAndrii Nakryiko 
1050ff97e56SAndrii Nakryiko 	bool jit_enabled;
1069f7fa225SAndrii Nakryiko 	bool has_testmod;
107643e7233SJesper Dangaard Brouer 	bool get_test_cnt;
108c1f1f365SJesper Dangaard Brouer 	bool list_test_names;
1090ff97e56SAndrii Nakryiko 
1102324257dSMykola Lysenko 	struct prog_test_def *test; /* current running test */
11109252259SMykola Lysenko 	struct test_state *test_state; /* current running test state */
11209252259SMykola Lysenko 	struct subtest_state *subtest_state; /* current running subtest state */
11391b2c0afSYucong Sun 
114946152b3SStanislav Fomichev 	FILE *stdout;
115946152b3SStanislav Fomichev 	FILE *stderr;
116fd27b183SAndrii Nakryiko 	int nr_cpus;
1172be7aa76SManu Bretelle 	FILE *json;
1180ff97e56SAndrii Nakryiko 
1193a516a0aSAndrii Nakryiko 	int succ_cnt; /* successful tests */
1203a516a0aSAndrii Nakryiko 	int sub_succ_cnt; /* successful sub-tests */
1213a516a0aSAndrii Nakryiko 	int fail_cnt; /* total failed tests + sub-tests */
122cd9c21d7SStanislav Fomichev 	int skip_cnt; /* skipped tests */
123811d7e37SMartin KaFai Lau 
124811d7e37SMartin KaFai Lau 	int saved_netns_fd;
12591b2c0afSYucong Sun 	int workers; /* number of worker process */
12691b2c0afSYucong Sun 	int worker_id; /* id number of current worker, main process is -1 */
12791b2c0afSYucong Sun 	pid_t *worker_pids; /* array of worker pids */
12891b2c0afSYucong Sun 	int *worker_socks; /* array of worker socks */
12991b2c0afSYucong Sun 	int *worker_current_test; /* array of current running test for each worker */
13091b2c0afSYucong Sun };
13191b2c0afSYucong Sun 
13291b2c0afSYucong Sun #define MAX_LOG_TRUNK_SIZE 8192
13309252259SMykola Lysenko #define MAX_SUBTEST_NAME 1024
13491b2c0afSYucong Sun enum msg_type {
13591b2c0afSYucong Sun 	MSG_DO_TEST = 0,
13691b2c0afSYucong Sun 	MSG_TEST_DONE = 1,
13791b2c0afSYucong Sun 	MSG_TEST_LOG = 2,
13809252259SMykola Lysenko 	MSG_SUBTEST_DONE = 3,
13991b2c0afSYucong Sun 	MSG_EXIT = 255,
14091b2c0afSYucong Sun };
14191b2c0afSYucong Sun struct msg {
14291b2c0afSYucong Sun 	enum msg_type type;
14391b2c0afSYucong Sun 	union {
14491b2c0afSYucong Sun 		struct {
14509252259SMykola Lysenko 			int num;
14691b2c0afSYucong Sun 		} do_test;
14791b2c0afSYucong Sun 		struct {
14809252259SMykola Lysenko 			int num;
14991b2c0afSYucong Sun 			int sub_succ_cnt;
15091b2c0afSYucong Sun 			int error_cnt;
15191b2c0afSYucong Sun 			int skip_cnt;
15291b2c0afSYucong Sun 			bool have_log;
15309252259SMykola Lysenko 			int subtest_num;
15491b2c0afSYucong Sun 		} test_done;
15591b2c0afSYucong Sun 		struct {
15691b2c0afSYucong Sun 			char log_buf[MAX_LOG_TRUNK_SIZE + 1];
15791b2c0afSYucong Sun 			bool is_last;
15891b2c0afSYucong Sun 		} test_log;
15909252259SMykola Lysenko 		struct {
16009252259SMykola Lysenko 			int num;
16109252259SMykola Lysenko 			char name[MAX_SUBTEST_NAME + 1];
16209252259SMykola Lysenko 			int error_cnt;
16309252259SMykola Lysenko 			bool skipped;
1642dc323b1SMykola Lysenko 			bool filtered;
16509252259SMykola Lysenko 			bool have_log;
16609252259SMykola Lysenko 		} subtest_done;
16791b2c0afSYucong Sun 	};
1680ff97e56SAndrii Nakryiko };
1690ff97e56SAndrii Nakryiko 
1700ff97e56SAndrii Nakryiko extern struct test_env env;
1710ff97e56SAndrii Nakryiko 
1722324257dSMykola Lysenko void test__force_log(void);
1732324257dSMykola Lysenko bool test__start_subtest(const char *name);
1742324257dSMykola Lysenko void test__end_subtest(void);
1752324257dSMykola Lysenko void test__skip(void);
1762324257dSMykola Lysenko void test__fail(void);
1772324257dSMykola Lysenko int test__join_cgroup(const char *path);
1783f306588SStanislav Fomichev 
17923458901SLorenz Bauer #define PRINT_FAIL(format...)                                                  \
18023458901SLorenz Bauer 	({                                                                     \
18123458901SLorenz Bauer 		test__fail();                                                  \
18223458901SLorenz Bauer 		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
18323458901SLorenz Bauer 		fprintf(stdout, ##format);                                     \
18423458901SLorenz Bauer 	})
18523458901SLorenz Bauer 
1863f306588SStanislav Fomichev #define _CHECK(condition, tag, duration, format...) ({			\
1873f306588SStanislav Fomichev 	int __ret = !!(condition);					\
188478bee0dSAndrey Ignatov 	int __save_errno = errno;					\
1893f306588SStanislav Fomichev 	if (__ret) {							\
190d38835b7SStanislav Fomichev 		test__fail();						\
1913e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
1923e2671fbSAndrii Nakryiko 		fprintf(stdout, ##format);				\
1933f306588SStanislav Fomichev 	} else {							\
1943e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
1950ff97e56SAndrii Nakryiko 		       __func__, tag, duration);			\
1963f306588SStanislav Fomichev 	}								\
197478bee0dSAndrey Ignatov 	errno = __save_errno;						\
1983f306588SStanislav Fomichev 	__ret;								\
1993f306588SStanislav Fomichev })
2003f306588SStanislav Fomichev 
201d38835b7SStanislav Fomichev #define CHECK_FAIL(condition) ({					\
202d38835b7SStanislav Fomichev 	int __ret = !!(condition);					\
203478bee0dSAndrey Ignatov 	int __save_errno = errno;					\
204d38835b7SStanislav Fomichev 	if (__ret) {							\
205d38835b7SStanislav Fomichev 		test__fail();						\
2063e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
207d38835b7SStanislav Fomichev 	}								\
208478bee0dSAndrey Ignatov 	errno = __save_errno;						\
209d38835b7SStanislav Fomichev 	__ret;								\
210d38835b7SStanislav Fomichev })
211d38835b7SStanislav Fomichev 
2123f306588SStanislav Fomichev #define CHECK(condition, tag, format...) \
2133f306588SStanislav Fomichev 	_CHECK(condition, tag, duration, format)
2143f306588SStanislav Fomichev #define CHECK_ATTR(condition, tag, format...) \
2153f306588SStanislav Fomichev 	_CHECK(condition, tag, tattr.duration, format)
2163f306588SStanislav Fomichev 
217537c3f66SAndrii Nakryiko #define ASSERT_FAIL(fmt, args...) ({					\
218537c3f66SAndrii Nakryiko 	static int duration = 0;					\
219537c3f66SAndrii Nakryiko 	CHECK(false, "", fmt"\n", ##args);				\
220537c3f66SAndrii Nakryiko 	false;								\
221537c3f66SAndrii Nakryiko })
222537c3f66SAndrii Nakryiko 
2237a2fa70aSAndrii Nakryiko #define ASSERT_TRUE(actual, name) ({					\
2247a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2257a2fa70aSAndrii Nakryiko 	bool ___ok = (actual);						\
2267a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name));	\
2277a2fa70aSAndrii Nakryiko 	___ok;								\
2287a2fa70aSAndrii Nakryiko })
2297a2fa70aSAndrii Nakryiko 
2307a2fa70aSAndrii Nakryiko #define ASSERT_FALSE(actual, name) ({					\
2317a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2327a2fa70aSAndrii Nakryiko 	bool ___ok = !(actual);						\
2337a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name));	\
2347a2fa70aSAndrii Nakryiko 	___ok;								\
2357a2fa70aSAndrii Nakryiko })
2367a2fa70aSAndrii Nakryiko 
23722ba3635SAndrii Nakryiko #define ASSERT_EQ(actual, expected, name) ({				\
23822ba3635SAndrii Nakryiko 	static int duration = 0;					\
23922ba3635SAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
24022ba3635SAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
24122ba3635SAndrii Nakryiko 	bool ___ok = ___act == ___exp;					\
24222ba3635SAndrii Nakryiko 	CHECK(!___ok, (name),						\
24322ba3635SAndrii Nakryiko 	      "unexpected %s: actual %lld != expected %lld\n",		\
24422ba3635SAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
24522ba3635SAndrii Nakryiko 	___ok;								\
24622ba3635SAndrii Nakryiko })
24722ba3635SAndrii Nakryiko 
248197389daSAndrii Nakryiko #define ASSERT_NEQ(actual, expected, name) ({				\
249197389daSAndrii Nakryiko 	static int duration = 0;					\
250197389daSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
251197389daSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
252197389daSAndrii Nakryiko 	bool ___ok = ___act != ___exp;					\
253197389daSAndrii Nakryiko 	CHECK(!___ok, (name),						\
254197389daSAndrii Nakryiko 	      "unexpected %s: actual %lld == expected %lld\n",		\
255197389daSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
256197389daSAndrii Nakryiko 	___ok;								\
257197389daSAndrii Nakryiko })
258197389daSAndrii Nakryiko 
2599de7f0fdSYonghong Song #define ASSERT_LT(actual, expected, name) ({				\
2609de7f0fdSYonghong Song 	static int duration = 0;					\
2619de7f0fdSYonghong Song 	typeof(actual) ___act = (actual);				\
2629de7f0fdSYonghong Song 	typeof(expected) ___exp = (expected);				\
2639de7f0fdSYonghong Song 	bool ___ok = ___act < ___exp;					\
2649de7f0fdSYonghong Song 	CHECK(!___ok, (name),						\
2659de7f0fdSYonghong Song 	      "unexpected %s: actual %lld >= expected %lld\n",		\
2669de7f0fdSYonghong Song 	      (name), (long long)(___act), (long long)(___exp));	\
2679de7f0fdSYonghong Song 	___ok;								\
2689de7f0fdSYonghong Song })
2699de7f0fdSYonghong Song 
2707a2fa70aSAndrii Nakryiko #define ASSERT_LE(actual, expected, name) ({				\
2717a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2727a2fa70aSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
2737a2fa70aSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
2747a2fa70aSAndrii Nakryiko 	bool ___ok = ___act <= ___exp;					\
2757a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name),						\
2767a2fa70aSAndrii Nakryiko 	      "unexpected %s: actual %lld > expected %lld\n",		\
2777a2fa70aSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
2787a2fa70aSAndrii Nakryiko 	___ok;								\
2797a2fa70aSAndrii Nakryiko })
2807a2fa70aSAndrii Nakryiko 
2817a2fa70aSAndrii Nakryiko #define ASSERT_GT(actual, expected, name) ({				\
2827a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2837a2fa70aSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
2847a2fa70aSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
2857a2fa70aSAndrii Nakryiko 	bool ___ok = ___act > ___exp;					\
2867a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name),						\
2877a2fa70aSAndrii Nakryiko 	      "unexpected %s: actual %lld <= expected %lld\n",		\
2887a2fa70aSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
2897a2fa70aSAndrii Nakryiko 	___ok;								\
2907a2fa70aSAndrii Nakryiko })
2917a2fa70aSAndrii Nakryiko 
2927a2fa70aSAndrii Nakryiko #define ASSERT_GE(actual, expected, name) ({				\
2937a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2947a2fa70aSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
2957a2fa70aSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
2967a2fa70aSAndrii Nakryiko 	bool ___ok = ___act >= ___exp;					\
2977a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name),						\
2987a2fa70aSAndrii Nakryiko 	      "unexpected %s: actual %lld < expected %lld\n",		\
2997a2fa70aSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
3007a2fa70aSAndrii Nakryiko 	___ok;								\
3017a2fa70aSAndrii Nakryiko })
3027a2fa70aSAndrii Nakryiko 
30322ba3635SAndrii Nakryiko #define ASSERT_STREQ(actual, expected, name) ({				\
30422ba3635SAndrii Nakryiko 	static int duration = 0;					\
30522ba3635SAndrii Nakryiko 	const char *___act = actual;					\
30622ba3635SAndrii Nakryiko 	const char *___exp = expected;					\
30722ba3635SAndrii Nakryiko 	bool ___ok = strcmp(___act, ___exp) == 0;			\
30822ba3635SAndrii Nakryiko 	CHECK(!___ok, (name),						\
30922ba3635SAndrii Nakryiko 	      "unexpected %s: actual '%s' != expected '%s'\n",		\
31022ba3635SAndrii Nakryiko 	      (name), ___act, ___exp);					\
31122ba3635SAndrii Nakryiko 	___ok;								\
31222ba3635SAndrii Nakryiko })
31322ba3635SAndrii Nakryiko 
31417283337SAlan Maguire #define ASSERT_STRNEQ(actual, expected, len, name) ({			\
31517283337SAlan Maguire 	static int duration = 0;					\
31617283337SAlan Maguire 	const char *___act = actual;					\
31717283337SAlan Maguire 	const char *___exp = expected;					\
31817283337SAlan Maguire 	int ___len = len;						\
31917283337SAlan Maguire 	bool ___ok = strncmp(___act, ___exp, ___len) == 0;		\
32017283337SAlan Maguire 	CHECK(!___ok, (name),						\
32117283337SAlan Maguire 	      "unexpected %s: actual '%.*s' != expected '%.*s'\n",	\
32217283337SAlan Maguire 	      (name), ___len, ___act, ___len, ___exp);			\
32317283337SAlan Maguire 	___ok;								\
32417283337SAlan Maguire })
32517283337SAlan Maguire 
326ea4128ebSAndrii Nakryiko #define ASSERT_HAS_SUBSTR(str, substr, name) ({				\
327ea4128ebSAndrii Nakryiko 	static int duration = 0;					\
328ea4128ebSAndrii Nakryiko 	const char *___str = str;					\
329ea4128ebSAndrii Nakryiko 	const char *___substr = substr;					\
330ea4128ebSAndrii Nakryiko 	bool ___ok = strstr(___str, ___substr) != NULL;			\
331ea4128ebSAndrii Nakryiko 	CHECK(!___ok, (name),						\
332ea4128ebSAndrii Nakryiko 	      "unexpected %s: '%s' is not a substring of '%s'\n",	\
333ea4128ebSAndrii Nakryiko 	      (name), ___substr, ___str);				\
334ea4128ebSAndrii Nakryiko 	___ok;								\
335ea4128ebSAndrii Nakryiko })
336ea4128ebSAndrii Nakryiko 
33722ba3635SAndrii Nakryiko #define ASSERT_OK(res, name) ({						\
33822ba3635SAndrii Nakryiko 	static int duration = 0;					\
33922ba3635SAndrii Nakryiko 	long long ___res = (res);					\
34022ba3635SAndrii Nakryiko 	bool ___ok = ___res == 0;					\
3417a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n",	\
3427a2fa70aSAndrii Nakryiko 	      ___res, errno);						\
34322ba3635SAndrii Nakryiko 	___ok;								\
34422ba3635SAndrii Nakryiko })
34522ba3635SAndrii Nakryiko 
34622ba3635SAndrii Nakryiko #define ASSERT_ERR(res, name) ({					\
34722ba3635SAndrii Nakryiko 	static int duration = 0;					\
34822ba3635SAndrii Nakryiko 	long long ___res = (res);					\
34922ba3635SAndrii Nakryiko 	bool ___ok = ___res < 0;					\
35022ba3635SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);	\
35122ba3635SAndrii Nakryiko 	___ok;								\
35222ba3635SAndrii Nakryiko })
35322ba3635SAndrii Nakryiko 
35422ba3635SAndrii Nakryiko #define ASSERT_NULL(ptr, name) ({					\
35522ba3635SAndrii Nakryiko 	static int duration = 0;					\
35622ba3635SAndrii Nakryiko 	const void *___res = (ptr);					\
35722ba3635SAndrii Nakryiko 	bool ___ok = !___res;						\
35822ba3635SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
35922ba3635SAndrii Nakryiko 	___ok;								\
36022ba3635SAndrii Nakryiko })
36122ba3635SAndrii Nakryiko 
36222ba3635SAndrii Nakryiko #define ASSERT_OK_PTR(ptr, name) ({					\
36322ba3635SAndrii Nakryiko 	static int duration = 0;					\
36422ba3635SAndrii Nakryiko 	const void *___res = (ptr);					\
365bad2e478SAndrii Nakryiko 	int ___err = libbpf_get_error(___res);				\
366bad2e478SAndrii Nakryiko 	bool ___ok = ___err == 0;					\
367bad2e478SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected error: %d\n", ___err);	\
36822ba3635SAndrii Nakryiko 	___ok;								\
36922ba3635SAndrii Nakryiko })
37022ba3635SAndrii Nakryiko 
37122ba3635SAndrii Nakryiko #define ASSERT_ERR_PTR(ptr, name) ({					\
37222ba3635SAndrii Nakryiko 	static int duration = 0;					\
37322ba3635SAndrii Nakryiko 	const void *___res = (ptr);					\
374bad2e478SAndrii Nakryiko 	int ___err = libbpf_get_error(___res);				\
375bad2e478SAndrii Nakryiko 	bool ___ok = ___err != 0;					\
37622ba3635SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
37722ba3635SAndrii Nakryiko 	___ok;								\
37822ba3635SAndrii Nakryiko })
37922ba3635SAndrii Nakryiko 
380b61987d3SHangbin Liu #define SYS(goto_label, fmt, ...)					\
381b61987d3SHangbin Liu 	({								\
382b61987d3SHangbin Liu 		char cmd[1024];						\
383b61987d3SHangbin Liu 		snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);		\
384b61987d3SHangbin Liu 		if (!ASSERT_OK(system(cmd), cmd))			\
385b61987d3SHangbin Liu 			goto goto_label;				\
386b61987d3SHangbin Liu 	})
387b61987d3SHangbin Liu 
388b61987d3SHangbin Liu #define SYS_NOFAIL(fmt, ...)						\
389b61987d3SHangbin Liu 	({								\
390b61987d3SHangbin Liu 		char cmd[1024];						\
391b61987d3SHangbin Liu 		snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);		\
392b61987d3SHangbin Liu 		system(cmd);						\
393b61987d3SHangbin Liu 	})
394b61987d3SHangbin Liu 
ptr_to_u64(const void * ptr)395886225bbSStanislav Fomichev static inline __u64 ptr_to_u64(const void *ptr)
396886225bbSStanislav Fomichev {
397886225bbSStanislav Fomichev 	return (__u64) (unsigned long) ptr;
398886225bbSStanislav Fomichev }
399886225bbSStanislav Fomichev 
u64_to_ptr(__u64 ptr)4009028bbccSAndrii Nakryiko static inline void *u64_to_ptr(__u64 ptr)
4019028bbccSAndrii Nakryiko {
4029028bbccSAndrii Nakryiko 	return (void *) (unsigned long) ptr;
4039028bbccSAndrii Nakryiko }
4049028bbccSAndrii Nakryiko 
4053f306588SStanislav Fomichev int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
406615741d8SStanislav Fomichev int compare_map_keys(int map1_fd, int map2_fd);
407615741d8SStanislav Fomichev int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
408025bd7c7SSong Liu int trigger_module_test_read(int read_sz);
409025bd7c7SSong Liu int trigger_module_test_write(int write_sz);
410e42921c3SYiFei Zhu int write_sysctl(const char *sysctl, const char *value);
4118fb9fb2fSIlya Leoshkevich int get_bpf_max_tramp_links_from(struct btf *btf);
4128fb9fb2fSIlya Leoshkevich int get_bpf_max_tramp_links(void);
4131cb59a60SIlya Leoshkevich 
4141cb59a60SIlya Leoshkevich #ifdef __x86_64__
4151cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
4161cb59a60SIlya Leoshkevich #elif defined(__s390x__)
4171cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
418d2987617SIlya Leoshkevich #elif defined(__aarch64__)
419d2987617SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
420*f84debaeSBjörn Töpel #elif defined(__riscv)
421*f84debaeSBjörn Töpel #define SYS_NANOSLEEP_KPROBE_NAME "__riscv_sys_nanosleep"
4221cb59a60SIlya Leoshkevich #else
4231cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
4241cb59a60SIlya Leoshkevich #endif
425fa7f17d0SHou Tao 
426fa7f17d0SHou Tao #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
427537c3f66SAndrii Nakryiko 
4285b22f4d1SEduard Zingerman typedef int (*pre_execution_cb)(struct bpf_object *obj);
4295b22f4d1SEduard Zingerman 
430537c3f66SAndrii Nakryiko struct test_loader {
431537c3f66SAndrii Nakryiko 	char *log_buf;
432537c3f66SAndrii Nakryiko 	size_t log_buf_sz;
43335cbf7f9SAndrii Nakryiko 	size_t next_match_pos;
4345b22f4d1SEduard Zingerman 	pre_execution_cb pre_execution_cb;
435537c3f66SAndrii Nakryiko 
436537c3f66SAndrii Nakryiko 	struct bpf_object *obj;
437537c3f66SAndrii Nakryiko };
438537c3f66SAndrii Nakryiko 
test_loader__set_pre_execution_cb(struct test_loader * tester,pre_execution_cb cb)4395b22f4d1SEduard Zingerman static inline void test_loader__set_pre_execution_cb(struct test_loader *tester,
4405b22f4d1SEduard Zingerman 						     pre_execution_cb cb)
4415b22f4d1SEduard Zingerman {
4425b22f4d1SEduard Zingerman 	tester->pre_execution_cb = cb;
4435b22f4d1SEduard Zingerman }
4445b22f4d1SEduard Zingerman 
445537c3f66SAndrii Nakryiko typedef const void *(*skel_elf_bytes_fn)(size_t *sz);
446537c3f66SAndrii Nakryiko 
447537c3f66SAndrii Nakryiko extern void test_loader__run_subtests(struct test_loader *tester,
448537c3f66SAndrii Nakryiko 				      const char *skel_name,
449537c3f66SAndrii Nakryiko 				      skel_elf_bytes_fn elf_bytes_factory);
450537c3f66SAndrii Nakryiko 
451537c3f66SAndrii Nakryiko extern void test_loader_fini(struct test_loader *tester);
452537c3f66SAndrii Nakryiko 
453537c3f66SAndrii Nakryiko #define RUN_TESTS(skel) ({						       \
454537c3f66SAndrii Nakryiko 	struct test_loader tester = {};					       \
455537c3f66SAndrii Nakryiko 									       \
456537c3f66SAndrii Nakryiko 	test_loader__run_subtests(&tester, #skel, skel##__elf_bytes);	       \
457537c3f66SAndrii Nakryiko 	test_loader_fini(&tester);					       \
458537c3f66SAndrii Nakryiko })
459537c3f66SAndrii Nakryiko 
460537c3f66SAndrii Nakryiko #endif /* __TEST_PROGS_H */
461