13f306588SStanislav Fomichev /* SPDX-License-Identifier: GPL-2.0 */
2*537c3f66SAndrii Nakryiko #ifndef __TEST_PROGS_H
3*537c3f66SAndrii Nakryiko #define __TEST_PROGS_H
4*537c3f66SAndrii 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;
1170ff97e56SAndrii Nakryiko 
1183a516a0aSAndrii Nakryiko 	int succ_cnt; /* successful tests */
1193a516a0aSAndrii Nakryiko 	int sub_succ_cnt; /* successful sub-tests */
1203a516a0aSAndrii Nakryiko 	int fail_cnt; /* total failed tests + sub-tests */
121cd9c21d7SStanislav Fomichev 	int skip_cnt; /* skipped tests */
122811d7e37SMartin KaFai Lau 
123811d7e37SMartin KaFai Lau 	int saved_netns_fd;
12491b2c0afSYucong Sun 	int workers; /* number of worker process */
12591b2c0afSYucong Sun 	int worker_id; /* id number of current worker, main process is -1 */
12691b2c0afSYucong Sun 	pid_t *worker_pids; /* array of worker pids */
12791b2c0afSYucong Sun 	int *worker_socks; /* array of worker socks */
12891b2c0afSYucong Sun 	int *worker_current_test; /* array of current running test for each worker */
12991b2c0afSYucong Sun };
13091b2c0afSYucong Sun 
13191b2c0afSYucong Sun #define MAX_LOG_TRUNK_SIZE 8192
13209252259SMykola Lysenko #define MAX_SUBTEST_NAME 1024
13391b2c0afSYucong Sun enum msg_type {
13491b2c0afSYucong Sun 	MSG_DO_TEST = 0,
13591b2c0afSYucong Sun 	MSG_TEST_DONE = 1,
13691b2c0afSYucong Sun 	MSG_TEST_LOG = 2,
13709252259SMykola Lysenko 	MSG_SUBTEST_DONE = 3,
13891b2c0afSYucong Sun 	MSG_EXIT = 255,
13991b2c0afSYucong Sun };
14091b2c0afSYucong Sun struct msg {
14191b2c0afSYucong Sun 	enum msg_type type;
14291b2c0afSYucong Sun 	union {
14391b2c0afSYucong Sun 		struct {
14409252259SMykola Lysenko 			int num;
14591b2c0afSYucong Sun 		} do_test;
14691b2c0afSYucong Sun 		struct {
14709252259SMykola Lysenko 			int num;
14891b2c0afSYucong Sun 			int sub_succ_cnt;
14991b2c0afSYucong Sun 			int error_cnt;
15091b2c0afSYucong Sun 			int skip_cnt;
15191b2c0afSYucong Sun 			bool have_log;
15209252259SMykola Lysenko 			int subtest_num;
15391b2c0afSYucong Sun 		} test_done;
15491b2c0afSYucong Sun 		struct {
15591b2c0afSYucong Sun 			char log_buf[MAX_LOG_TRUNK_SIZE + 1];
15691b2c0afSYucong Sun 			bool is_last;
15791b2c0afSYucong Sun 		} test_log;
15809252259SMykola Lysenko 		struct {
15909252259SMykola Lysenko 			int num;
16009252259SMykola Lysenko 			char name[MAX_SUBTEST_NAME + 1];
16109252259SMykola Lysenko 			int error_cnt;
16209252259SMykola Lysenko 			bool skipped;
1632dc323b1SMykola Lysenko 			bool filtered;
16409252259SMykola Lysenko 			bool have_log;
16509252259SMykola Lysenko 		} subtest_done;
16691b2c0afSYucong Sun 	};
1670ff97e56SAndrii Nakryiko };
1680ff97e56SAndrii Nakryiko 
1690ff97e56SAndrii Nakryiko extern struct test_env env;
1700ff97e56SAndrii Nakryiko 
1712324257dSMykola Lysenko void test__force_log(void);
1722324257dSMykola Lysenko bool test__start_subtest(const char *name);
1732324257dSMykola Lysenko void test__end_subtest(void);
1742324257dSMykola Lysenko void test__skip(void);
1752324257dSMykola Lysenko void test__fail(void);
1762324257dSMykola Lysenko int test__join_cgroup(const char *path);
1773f306588SStanislav Fomichev 
17823458901SLorenz Bauer #define PRINT_FAIL(format...)                                                  \
17923458901SLorenz Bauer 	({                                                                     \
18023458901SLorenz Bauer 		test__fail();                                                  \
18123458901SLorenz Bauer 		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
18223458901SLorenz Bauer 		fprintf(stdout, ##format);                                     \
18323458901SLorenz Bauer 	})
18423458901SLorenz Bauer 
1853f306588SStanislav Fomichev #define _CHECK(condition, tag, duration, format...) ({			\
1863f306588SStanislav Fomichev 	int __ret = !!(condition);					\
187478bee0dSAndrey Ignatov 	int __save_errno = errno;					\
1883f306588SStanislav Fomichev 	if (__ret) {							\
189d38835b7SStanislav Fomichev 		test__fail();						\
1903e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
1913e2671fbSAndrii Nakryiko 		fprintf(stdout, ##format);				\
1923f306588SStanislav Fomichev 	} else {							\
1933e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
1940ff97e56SAndrii Nakryiko 		       __func__, tag, duration);			\
1953f306588SStanislav Fomichev 	}								\
196478bee0dSAndrey Ignatov 	errno = __save_errno;						\
1973f306588SStanislav Fomichev 	__ret;								\
1983f306588SStanislav Fomichev })
1993f306588SStanislav Fomichev 
200d38835b7SStanislav Fomichev #define CHECK_FAIL(condition) ({					\
201d38835b7SStanislav Fomichev 	int __ret = !!(condition);					\
202478bee0dSAndrey Ignatov 	int __save_errno = errno;					\
203d38835b7SStanislav Fomichev 	if (__ret) {							\
204d38835b7SStanislav Fomichev 		test__fail();						\
2053e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
206d38835b7SStanislav Fomichev 	}								\
207478bee0dSAndrey Ignatov 	errno = __save_errno;						\
208d38835b7SStanislav Fomichev 	__ret;								\
209d38835b7SStanislav Fomichev })
210d38835b7SStanislav Fomichev 
2113f306588SStanislav Fomichev #define CHECK(condition, tag, format...) \
2123f306588SStanislav Fomichev 	_CHECK(condition, tag, duration, format)
2133f306588SStanislav Fomichev #define CHECK_ATTR(condition, tag, format...) \
2143f306588SStanislav Fomichev 	_CHECK(condition, tag, tattr.duration, format)
2153f306588SStanislav Fomichev 
216*537c3f66SAndrii Nakryiko #define ASSERT_FAIL(fmt, args...) ({					\
217*537c3f66SAndrii Nakryiko 	static int duration = 0;					\
218*537c3f66SAndrii Nakryiko 	CHECK(false, "", fmt"\n", ##args);				\
219*537c3f66SAndrii Nakryiko 	false;								\
220*537c3f66SAndrii Nakryiko })
221*537c3f66SAndrii Nakryiko 
2227a2fa70aSAndrii Nakryiko #define ASSERT_TRUE(actual, name) ({					\
2237a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2247a2fa70aSAndrii Nakryiko 	bool ___ok = (actual);						\
2257a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name));	\
2267a2fa70aSAndrii Nakryiko 	___ok;								\
2277a2fa70aSAndrii Nakryiko })
2287a2fa70aSAndrii Nakryiko 
2297a2fa70aSAndrii Nakryiko #define ASSERT_FALSE(actual, name) ({					\
2307a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2317a2fa70aSAndrii Nakryiko 	bool ___ok = !(actual);						\
2327a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name));	\
2337a2fa70aSAndrii Nakryiko 	___ok;								\
2347a2fa70aSAndrii Nakryiko })
2357a2fa70aSAndrii Nakryiko 
23622ba3635SAndrii Nakryiko #define ASSERT_EQ(actual, expected, name) ({				\
23722ba3635SAndrii Nakryiko 	static int duration = 0;					\
23822ba3635SAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
23922ba3635SAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
24022ba3635SAndrii Nakryiko 	bool ___ok = ___act == ___exp;					\
24122ba3635SAndrii Nakryiko 	CHECK(!___ok, (name),						\
24222ba3635SAndrii Nakryiko 	      "unexpected %s: actual %lld != expected %lld\n",		\
24322ba3635SAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
24422ba3635SAndrii Nakryiko 	___ok;								\
24522ba3635SAndrii Nakryiko })
24622ba3635SAndrii Nakryiko 
247197389daSAndrii Nakryiko #define ASSERT_NEQ(actual, expected, name) ({				\
248197389daSAndrii Nakryiko 	static int duration = 0;					\
249197389daSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
250197389daSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
251197389daSAndrii Nakryiko 	bool ___ok = ___act != ___exp;					\
252197389daSAndrii Nakryiko 	CHECK(!___ok, (name),						\
253197389daSAndrii Nakryiko 	      "unexpected %s: actual %lld == expected %lld\n",		\
254197389daSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
255197389daSAndrii Nakryiko 	___ok;								\
256197389daSAndrii Nakryiko })
257197389daSAndrii Nakryiko 
2589de7f0fdSYonghong Song #define ASSERT_LT(actual, expected, name) ({				\
2599de7f0fdSYonghong Song 	static int duration = 0;					\
2609de7f0fdSYonghong Song 	typeof(actual) ___act = (actual);				\
2619de7f0fdSYonghong Song 	typeof(expected) ___exp = (expected);				\
2629de7f0fdSYonghong Song 	bool ___ok = ___act < ___exp;					\
2639de7f0fdSYonghong Song 	CHECK(!___ok, (name),						\
2649de7f0fdSYonghong Song 	      "unexpected %s: actual %lld >= expected %lld\n",		\
2659de7f0fdSYonghong Song 	      (name), (long long)(___act), (long long)(___exp));	\
2669de7f0fdSYonghong Song 	___ok;								\
2679de7f0fdSYonghong Song })
2689de7f0fdSYonghong Song 
2697a2fa70aSAndrii Nakryiko #define ASSERT_LE(actual, expected, name) ({				\
2707a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2717a2fa70aSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
2727a2fa70aSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
2737a2fa70aSAndrii Nakryiko 	bool ___ok = ___act <= ___exp;					\
2747a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name),						\
2757a2fa70aSAndrii Nakryiko 	      "unexpected %s: actual %lld > expected %lld\n",		\
2767a2fa70aSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
2777a2fa70aSAndrii Nakryiko 	___ok;								\
2787a2fa70aSAndrii Nakryiko })
2797a2fa70aSAndrii Nakryiko 
2807a2fa70aSAndrii Nakryiko #define ASSERT_GT(actual, expected, name) ({				\
2817a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2827a2fa70aSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
2837a2fa70aSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
2847a2fa70aSAndrii Nakryiko 	bool ___ok = ___act > ___exp;					\
2857a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name),						\
2867a2fa70aSAndrii Nakryiko 	      "unexpected %s: actual %lld <= expected %lld\n",		\
2877a2fa70aSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
2887a2fa70aSAndrii Nakryiko 	___ok;								\
2897a2fa70aSAndrii Nakryiko })
2907a2fa70aSAndrii Nakryiko 
2917a2fa70aSAndrii Nakryiko #define ASSERT_GE(actual, expected, name) ({				\
2927a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2937a2fa70aSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
2947a2fa70aSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
2957a2fa70aSAndrii Nakryiko 	bool ___ok = ___act >= ___exp;					\
2967a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name),						\
2977a2fa70aSAndrii Nakryiko 	      "unexpected %s: actual %lld < expected %lld\n",		\
2987a2fa70aSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
2997a2fa70aSAndrii Nakryiko 	___ok;								\
3007a2fa70aSAndrii Nakryiko })
3017a2fa70aSAndrii Nakryiko 
30222ba3635SAndrii Nakryiko #define ASSERT_STREQ(actual, expected, name) ({				\
30322ba3635SAndrii Nakryiko 	static int duration = 0;					\
30422ba3635SAndrii Nakryiko 	const char *___act = actual;					\
30522ba3635SAndrii Nakryiko 	const char *___exp = expected;					\
30622ba3635SAndrii Nakryiko 	bool ___ok = strcmp(___act, ___exp) == 0;			\
30722ba3635SAndrii Nakryiko 	CHECK(!___ok, (name),						\
30822ba3635SAndrii Nakryiko 	      "unexpected %s: actual '%s' != expected '%s'\n",		\
30922ba3635SAndrii Nakryiko 	      (name), ___act, ___exp);					\
31022ba3635SAndrii Nakryiko 	___ok;								\
31122ba3635SAndrii Nakryiko })
31222ba3635SAndrii Nakryiko 
31317283337SAlan Maguire #define ASSERT_STRNEQ(actual, expected, len, name) ({			\
31417283337SAlan Maguire 	static int duration = 0;					\
31517283337SAlan Maguire 	const char *___act = actual;					\
31617283337SAlan Maguire 	const char *___exp = expected;					\
31717283337SAlan Maguire 	int ___len = len;						\
31817283337SAlan Maguire 	bool ___ok = strncmp(___act, ___exp, ___len) == 0;		\
31917283337SAlan Maguire 	CHECK(!___ok, (name),						\
32017283337SAlan Maguire 	      "unexpected %s: actual '%.*s' != expected '%.*s'\n",	\
32117283337SAlan Maguire 	      (name), ___len, ___act, ___len, ___exp);			\
32217283337SAlan Maguire 	___ok;								\
32317283337SAlan Maguire })
32417283337SAlan Maguire 
325ea4128ebSAndrii Nakryiko #define ASSERT_HAS_SUBSTR(str, substr, name) ({				\
326ea4128ebSAndrii Nakryiko 	static int duration = 0;					\
327ea4128ebSAndrii Nakryiko 	const char *___str = str;					\
328ea4128ebSAndrii Nakryiko 	const char *___substr = substr;					\
329ea4128ebSAndrii Nakryiko 	bool ___ok = strstr(___str, ___substr) != NULL;			\
330ea4128ebSAndrii Nakryiko 	CHECK(!___ok, (name),						\
331ea4128ebSAndrii Nakryiko 	      "unexpected %s: '%s' is not a substring of '%s'\n",	\
332ea4128ebSAndrii Nakryiko 	      (name), ___substr, ___str);				\
333ea4128ebSAndrii Nakryiko 	___ok;								\
334ea4128ebSAndrii Nakryiko })
335ea4128ebSAndrii Nakryiko 
33622ba3635SAndrii Nakryiko #define ASSERT_OK(res, name) ({						\
33722ba3635SAndrii Nakryiko 	static int duration = 0;					\
33822ba3635SAndrii Nakryiko 	long long ___res = (res);					\
33922ba3635SAndrii Nakryiko 	bool ___ok = ___res == 0;					\
3407a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n",	\
3417a2fa70aSAndrii Nakryiko 	      ___res, errno);						\
34222ba3635SAndrii Nakryiko 	___ok;								\
34322ba3635SAndrii Nakryiko })
34422ba3635SAndrii Nakryiko 
34522ba3635SAndrii Nakryiko #define ASSERT_ERR(res, name) ({					\
34622ba3635SAndrii Nakryiko 	static int duration = 0;					\
34722ba3635SAndrii Nakryiko 	long long ___res = (res);					\
34822ba3635SAndrii Nakryiko 	bool ___ok = ___res < 0;					\
34922ba3635SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);	\
35022ba3635SAndrii Nakryiko 	___ok;								\
35122ba3635SAndrii Nakryiko })
35222ba3635SAndrii Nakryiko 
35322ba3635SAndrii Nakryiko #define ASSERT_NULL(ptr, name) ({					\
35422ba3635SAndrii Nakryiko 	static int duration = 0;					\
35522ba3635SAndrii Nakryiko 	const void *___res = (ptr);					\
35622ba3635SAndrii Nakryiko 	bool ___ok = !___res;						\
35722ba3635SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
35822ba3635SAndrii Nakryiko 	___ok;								\
35922ba3635SAndrii Nakryiko })
36022ba3635SAndrii Nakryiko 
36122ba3635SAndrii Nakryiko #define ASSERT_OK_PTR(ptr, name) ({					\
36222ba3635SAndrii Nakryiko 	static int duration = 0;					\
36322ba3635SAndrii Nakryiko 	const void *___res = (ptr);					\
364bad2e478SAndrii Nakryiko 	int ___err = libbpf_get_error(___res);				\
365bad2e478SAndrii Nakryiko 	bool ___ok = ___err == 0;					\
366bad2e478SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected error: %d\n", ___err);	\
36722ba3635SAndrii Nakryiko 	___ok;								\
36822ba3635SAndrii Nakryiko })
36922ba3635SAndrii Nakryiko 
37022ba3635SAndrii Nakryiko #define ASSERT_ERR_PTR(ptr, name) ({					\
37122ba3635SAndrii Nakryiko 	static int duration = 0;					\
37222ba3635SAndrii Nakryiko 	const void *___res = (ptr);					\
373bad2e478SAndrii Nakryiko 	int ___err = libbpf_get_error(___res);				\
374bad2e478SAndrii Nakryiko 	bool ___ok = ___err != 0;					\
37522ba3635SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
37622ba3635SAndrii Nakryiko 	___ok;								\
37722ba3635SAndrii Nakryiko })
37822ba3635SAndrii Nakryiko 
379886225bbSStanislav Fomichev static inline __u64 ptr_to_u64(const void *ptr)
380886225bbSStanislav Fomichev {
381886225bbSStanislav Fomichev 	return (__u64) (unsigned long) ptr;
382886225bbSStanislav Fomichev }
383886225bbSStanislav Fomichev 
3849028bbccSAndrii Nakryiko static inline void *u64_to_ptr(__u64 ptr)
3859028bbccSAndrii Nakryiko {
3869028bbccSAndrii Nakryiko 	return (void *) (unsigned long) ptr;
3879028bbccSAndrii Nakryiko }
3889028bbccSAndrii Nakryiko 
3893f306588SStanislav Fomichev int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
390615741d8SStanislav Fomichev int compare_map_keys(int map1_fd, int map2_fd);
391615741d8SStanislav Fomichev int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
392615741d8SStanislav Fomichev int extract_build_id(char *build_id, size_t size);
393635599baSAndrii Nakryiko int kern_sync_rcu(void);
394025bd7c7SSong Liu int trigger_module_test_read(int read_sz);
395025bd7c7SSong Liu int trigger_module_test_write(int write_sz);
396e42921c3SYiFei Zhu int write_sysctl(const char *sysctl, const char *value);
3971cb59a60SIlya Leoshkevich 
3981cb59a60SIlya Leoshkevich #ifdef __x86_64__
3991cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
4001cb59a60SIlya Leoshkevich #elif defined(__s390x__)
4011cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
402d2987617SIlya Leoshkevich #elif defined(__aarch64__)
403d2987617SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
4041cb59a60SIlya Leoshkevich #else
4051cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
4061cb59a60SIlya Leoshkevich #endif
407fa7f17d0SHou Tao 
408fa7f17d0SHou Tao #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
409*537c3f66SAndrii Nakryiko 
410*537c3f66SAndrii Nakryiko struct test_loader {
411*537c3f66SAndrii Nakryiko 	char *log_buf;
412*537c3f66SAndrii Nakryiko 	size_t log_buf_sz;
413*537c3f66SAndrii Nakryiko 
414*537c3f66SAndrii Nakryiko 	struct bpf_object *obj;
415*537c3f66SAndrii Nakryiko };
416*537c3f66SAndrii Nakryiko 
417*537c3f66SAndrii Nakryiko typedef const void *(*skel_elf_bytes_fn)(size_t *sz);
418*537c3f66SAndrii Nakryiko 
419*537c3f66SAndrii Nakryiko extern void test_loader__run_subtests(struct test_loader *tester,
420*537c3f66SAndrii Nakryiko 				      const char *skel_name,
421*537c3f66SAndrii Nakryiko 				      skel_elf_bytes_fn elf_bytes_factory);
422*537c3f66SAndrii Nakryiko 
423*537c3f66SAndrii Nakryiko extern void test_loader_fini(struct test_loader *tester);
424*537c3f66SAndrii Nakryiko 
425*537c3f66SAndrii Nakryiko #define RUN_TESTS(skel) ({						       \
426*537c3f66SAndrii Nakryiko 	struct test_loader tester = {};					       \
427*537c3f66SAndrii Nakryiko 									       \
428*537c3f66SAndrii Nakryiko 	test_loader__run_subtests(&tester, #skel, skel##__elf_bytes);	       \
429*537c3f66SAndrii Nakryiko 	test_loader_fini(&tester);					       \
430*537c3f66SAndrii Nakryiko })
431*537c3f66SAndrii Nakryiko 
432*537c3f66SAndrii Nakryiko #endif /* __TEST_PROGS_H */
433