xref: /openbmc/linux/tools/testing/selftests/bpf/test_progs.h (revision e42921c3c346b1b49068af3f3881322081e1dddd)
13f306588SStanislav Fomichev /* SPDX-License-Identifier: GPL-2.0 */
23f306588SStanislav Fomichev #include <stdio.h>
33f306588SStanislav Fomichev #include <unistd.h>
43f306588SStanislav Fomichev #include <errno.h>
53f306588SStanislav Fomichev #include <string.h>
63f306588SStanislav Fomichev #include <assert.h>
73f306588SStanislav Fomichev #include <stdlib.h>
83f306588SStanislav Fomichev #include <stdarg.h>
93f306588SStanislav Fomichev #include <time.h>
103f306588SStanislav Fomichev #include <signal.h>
113f306588SStanislav Fomichev 
123f306588SStanislav Fomichev #include <linux/types.h>
133f306588SStanislav Fomichev typedef __u16 __sum16;
143f306588SStanislav Fomichev #include <arpa/inet.h>
153f306588SStanislav Fomichev #include <linux/if_ether.h>
163f306588SStanislav Fomichev #include <linux/if_packet.h>
173f306588SStanislav Fomichev #include <linux/ip.h>
183f306588SStanislav Fomichev #include <linux/ipv6.h>
193f306588SStanislav Fomichev #include <linux/filter.h>
203f306588SStanislav Fomichev #include <linux/perf_event.h>
219a365e67SStanislav Fomichev #include <linux/socket.h>
223f306588SStanislav Fomichev #include <linux/unistd.h>
233f306588SStanislav Fomichev 
243f306588SStanislav Fomichev #include <sys/ioctl.h>
253f306588SStanislav Fomichev #include <sys/wait.h>
263f306588SStanislav Fomichev #include <sys/types.h>
273f306588SStanislav Fomichev #include <sys/time.h>
28f4fd706fSGeliang Tang #include <sys/param.h>
293f306588SStanislav Fomichev #include <fcntl.h>
303f306588SStanislav Fomichev #include <pthread.h>
313f306588SStanislav Fomichev #include <linux/bpf.h>
323f306588SStanislav Fomichev #include <linux/err.h>
333f306588SStanislav Fomichev #include <bpf/bpf.h>
343f306588SStanislav Fomichev #include <bpf/libbpf.h>
353f306588SStanislav Fomichev 
363f306588SStanislav Fomichev #include "test_iptunnel_common.h"
373f306588SStanislav Fomichev #include "bpf_util.h"
383e689141SToke Høiland-Jørgensen #include <bpf/bpf_endian.h>
393f306588SStanislav Fomichev #include "trace_helpers.h"
40cd49291cSAndrii Nakryiko #include "testing_helpers.h"
413f306588SStanislav Fomichev 
42a8fdaad5SAndrii Nakryiko enum verbosity {
43a8fdaad5SAndrii Nakryiko 	VERBOSE_NONE,
44a8fdaad5SAndrii Nakryiko 	VERBOSE_NORMAL,
45a8fdaad5SAndrii Nakryiko 	VERBOSE_VERY,
46a8fdaad5SAndrii Nakryiko 	VERBOSE_SUPER,
47a8fdaad5SAndrii Nakryiko };
48a8fdaad5SAndrii Nakryiko 
4961ddff37SMykola Lysenko struct test_filter {
5061ddff37SMykola Lysenko 	char *name;
5161ddff37SMykola Lysenko 	char **subtests;
5261ddff37SMykola Lysenko 	int subtest_cnt;
5361ddff37SMykola Lysenko };
5461ddff37SMykola Lysenko 
5561ddff37SMykola Lysenko struct test_filter_set {
5661ddff37SMykola Lysenko 	struct test_filter *tests;
57b65053cdSAndrii Nakryiko 	int cnt;
58b65053cdSAndrii Nakryiko };
59b65053cdSAndrii Nakryiko 
603a516a0aSAndrii Nakryiko struct test_selector {
6161ddff37SMykola Lysenko 	struct test_filter_set whitelist;
6261ddff37SMykola Lysenko 	struct test_filter_set blacklist;
633a516a0aSAndrii Nakryiko 	bool *num_set;
643a516a0aSAndrii Nakryiko 	int num_set_len;
653a516a0aSAndrii Nakryiko };
663a516a0aSAndrii Nakryiko 
6709252259SMykola Lysenko struct subtest_state {
6809252259SMykola Lysenko 	char *name;
6909252259SMykola Lysenko 	size_t log_cnt;
7009252259SMykola Lysenko 	char *log_buf;
7109252259SMykola Lysenko 	int error_cnt;
7209252259SMykola Lysenko 	bool skipped;
732dc323b1SMykola Lysenko 	bool filtered;
7409252259SMykola Lysenko 
7509252259SMykola Lysenko 	FILE *stdout;
7609252259SMykola Lysenko };
7709252259SMykola Lysenko 
782324257dSMykola Lysenko struct test_state {
792324257dSMykola Lysenko 	bool tested;
802324257dSMykola Lysenko 	bool force_log;
812324257dSMykola Lysenko 
822324257dSMykola Lysenko 	int error_cnt;
832324257dSMykola Lysenko 	int skip_cnt;
842324257dSMykola Lysenko 	int sub_succ_cnt;
852324257dSMykola Lysenko 
8609252259SMykola Lysenko 	struct subtest_state *subtest_states;
872324257dSMykola Lysenko 	int subtest_num;
882324257dSMykola Lysenko 
892324257dSMykola Lysenko 	size_t log_cnt;
902324257dSMykola Lysenko 	char *log_buf;
9109252259SMykola Lysenko 
9209252259SMykola Lysenko 	FILE *stdout;
932324257dSMykola Lysenko };
942324257dSMykola Lysenko 
950ff97e56SAndrii Nakryiko struct test_env {
963a516a0aSAndrii Nakryiko 	struct test_selector test_selector;
973a516a0aSAndrii Nakryiko 	struct test_selector subtest_selector;
980ff97e56SAndrii Nakryiko 	bool verifier_stats;
9991b2c0afSYucong Sun 	bool debug;
100a8fdaad5SAndrii Nakryiko 	enum verbosity verbosity;
1010ff97e56SAndrii Nakryiko 
1020ff97e56SAndrii Nakryiko 	bool jit_enabled;
1039f7fa225SAndrii Nakryiko 	bool has_testmod;
104643e7233SJesper Dangaard Brouer 	bool get_test_cnt;
105c1f1f365SJesper Dangaard Brouer 	bool list_test_names;
1060ff97e56SAndrii Nakryiko 
1072324257dSMykola Lysenko 	struct prog_test_def *test; /* current running test */
10809252259SMykola Lysenko 	struct test_state *test_state; /* current running test state */
10909252259SMykola Lysenko 	struct subtest_state *subtest_state; /* current running subtest state */
11091b2c0afSYucong Sun 
111946152b3SStanislav Fomichev 	FILE *stdout;
112946152b3SStanislav Fomichev 	FILE *stderr;
113fd27b183SAndrii Nakryiko 	int nr_cpus;
1140ff97e56SAndrii Nakryiko 
1153a516a0aSAndrii Nakryiko 	int succ_cnt; /* successful tests */
1163a516a0aSAndrii Nakryiko 	int sub_succ_cnt; /* successful sub-tests */
1173a516a0aSAndrii Nakryiko 	int fail_cnt; /* total failed tests + sub-tests */
118cd9c21d7SStanislav Fomichev 	int skip_cnt; /* skipped tests */
119811d7e37SMartin KaFai Lau 
120811d7e37SMartin KaFai Lau 	int saved_netns_fd;
12191b2c0afSYucong Sun 	int workers; /* number of worker process */
12291b2c0afSYucong Sun 	int worker_id; /* id number of current worker, main process is -1 */
12391b2c0afSYucong Sun 	pid_t *worker_pids; /* array of worker pids */
12491b2c0afSYucong Sun 	int *worker_socks; /* array of worker socks */
12591b2c0afSYucong Sun 	int *worker_current_test; /* array of current running test for each worker */
12691b2c0afSYucong Sun };
12791b2c0afSYucong Sun 
12891b2c0afSYucong Sun #define MAX_LOG_TRUNK_SIZE 8192
12909252259SMykola Lysenko #define MAX_SUBTEST_NAME 1024
13091b2c0afSYucong Sun enum msg_type {
13191b2c0afSYucong Sun 	MSG_DO_TEST = 0,
13291b2c0afSYucong Sun 	MSG_TEST_DONE = 1,
13391b2c0afSYucong Sun 	MSG_TEST_LOG = 2,
13409252259SMykola Lysenko 	MSG_SUBTEST_DONE = 3,
13591b2c0afSYucong Sun 	MSG_EXIT = 255,
13691b2c0afSYucong Sun };
13791b2c0afSYucong Sun struct msg {
13891b2c0afSYucong Sun 	enum msg_type type;
13991b2c0afSYucong Sun 	union {
14091b2c0afSYucong Sun 		struct {
14109252259SMykola Lysenko 			int num;
14291b2c0afSYucong Sun 		} do_test;
14391b2c0afSYucong Sun 		struct {
14409252259SMykola Lysenko 			int num;
14591b2c0afSYucong Sun 			int sub_succ_cnt;
14691b2c0afSYucong Sun 			int error_cnt;
14791b2c0afSYucong Sun 			int skip_cnt;
14891b2c0afSYucong Sun 			bool have_log;
14909252259SMykola Lysenko 			int subtest_num;
15091b2c0afSYucong Sun 		} test_done;
15191b2c0afSYucong Sun 		struct {
15291b2c0afSYucong Sun 			char log_buf[MAX_LOG_TRUNK_SIZE + 1];
15391b2c0afSYucong Sun 			bool is_last;
15491b2c0afSYucong Sun 		} test_log;
15509252259SMykola Lysenko 		struct {
15609252259SMykola Lysenko 			int num;
15709252259SMykola Lysenko 			char name[MAX_SUBTEST_NAME + 1];
15809252259SMykola Lysenko 			int error_cnt;
15909252259SMykola Lysenko 			bool skipped;
1602dc323b1SMykola Lysenko 			bool filtered;
16109252259SMykola Lysenko 			bool have_log;
16209252259SMykola Lysenko 		} subtest_done;
16391b2c0afSYucong Sun 	};
1640ff97e56SAndrii Nakryiko };
1650ff97e56SAndrii Nakryiko 
1660ff97e56SAndrii Nakryiko extern struct test_env env;
1670ff97e56SAndrii Nakryiko 
1682324257dSMykola Lysenko void test__force_log(void);
1692324257dSMykola Lysenko bool test__start_subtest(const char *name);
1702324257dSMykola Lysenko void test__end_subtest(void);
1712324257dSMykola Lysenko void test__skip(void);
1722324257dSMykola Lysenko void test__fail(void);
1732324257dSMykola Lysenko int test__join_cgroup(const char *path);
1743f306588SStanislav Fomichev 
17523458901SLorenz Bauer #define PRINT_FAIL(format...)                                                  \
17623458901SLorenz Bauer 	({                                                                     \
17723458901SLorenz Bauer 		test__fail();                                                  \
17823458901SLorenz Bauer 		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
17923458901SLorenz Bauer 		fprintf(stdout, ##format);                                     \
18023458901SLorenz Bauer 	})
18123458901SLorenz Bauer 
1823f306588SStanislav Fomichev #define _CHECK(condition, tag, duration, format...) ({			\
1833f306588SStanislav Fomichev 	int __ret = !!(condition);					\
184478bee0dSAndrey Ignatov 	int __save_errno = errno;					\
1853f306588SStanislav Fomichev 	if (__ret) {							\
186d38835b7SStanislav Fomichev 		test__fail();						\
1873e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
1883e2671fbSAndrii Nakryiko 		fprintf(stdout, ##format);				\
1893f306588SStanislav Fomichev 	} else {							\
1903e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
1910ff97e56SAndrii Nakryiko 		       __func__, tag, duration);			\
1923f306588SStanislav Fomichev 	}								\
193478bee0dSAndrey Ignatov 	errno = __save_errno;						\
1943f306588SStanislav Fomichev 	__ret;								\
1953f306588SStanislav Fomichev })
1963f306588SStanislav Fomichev 
197d38835b7SStanislav Fomichev #define CHECK_FAIL(condition) ({					\
198d38835b7SStanislav Fomichev 	int __ret = !!(condition);					\
199478bee0dSAndrey Ignatov 	int __save_errno = errno;					\
200d38835b7SStanislav Fomichev 	if (__ret) {							\
201d38835b7SStanislav Fomichev 		test__fail();						\
2023e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
203d38835b7SStanislav Fomichev 	}								\
204478bee0dSAndrey Ignatov 	errno = __save_errno;						\
205d38835b7SStanislav Fomichev 	__ret;								\
206d38835b7SStanislav Fomichev })
207d38835b7SStanislav Fomichev 
2083f306588SStanislav Fomichev #define CHECK(condition, tag, format...) \
2093f306588SStanislav Fomichev 	_CHECK(condition, tag, duration, format)
2103f306588SStanislav Fomichev #define CHECK_ATTR(condition, tag, format...) \
2113f306588SStanislav Fomichev 	_CHECK(condition, tag, tattr.duration, format)
2123f306588SStanislav Fomichev 
2137a2fa70aSAndrii Nakryiko #define ASSERT_TRUE(actual, name) ({					\
2147a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2157a2fa70aSAndrii Nakryiko 	bool ___ok = (actual);						\
2167a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name));	\
2177a2fa70aSAndrii Nakryiko 	___ok;								\
2187a2fa70aSAndrii Nakryiko })
2197a2fa70aSAndrii Nakryiko 
2207a2fa70aSAndrii Nakryiko #define ASSERT_FALSE(actual, name) ({					\
2217a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2227a2fa70aSAndrii Nakryiko 	bool ___ok = !(actual);						\
2237a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name));	\
2247a2fa70aSAndrii Nakryiko 	___ok;								\
2257a2fa70aSAndrii Nakryiko })
2267a2fa70aSAndrii Nakryiko 
22722ba3635SAndrii Nakryiko #define ASSERT_EQ(actual, expected, name) ({				\
22822ba3635SAndrii Nakryiko 	static int duration = 0;					\
22922ba3635SAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
23022ba3635SAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
23122ba3635SAndrii Nakryiko 	bool ___ok = ___act == ___exp;					\
23222ba3635SAndrii Nakryiko 	CHECK(!___ok, (name),						\
23322ba3635SAndrii Nakryiko 	      "unexpected %s: actual %lld != expected %lld\n",		\
23422ba3635SAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
23522ba3635SAndrii Nakryiko 	___ok;								\
23622ba3635SAndrii Nakryiko })
23722ba3635SAndrii Nakryiko 
238197389daSAndrii Nakryiko #define ASSERT_NEQ(actual, expected, name) ({				\
239197389daSAndrii Nakryiko 	static int duration = 0;					\
240197389daSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
241197389daSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
242197389daSAndrii Nakryiko 	bool ___ok = ___act != ___exp;					\
243197389daSAndrii Nakryiko 	CHECK(!___ok, (name),						\
244197389daSAndrii Nakryiko 	      "unexpected %s: actual %lld == expected %lld\n",		\
245197389daSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
246197389daSAndrii Nakryiko 	___ok;								\
247197389daSAndrii Nakryiko })
248197389daSAndrii Nakryiko 
2499de7f0fdSYonghong Song #define ASSERT_LT(actual, expected, name) ({				\
2509de7f0fdSYonghong Song 	static int duration = 0;					\
2519de7f0fdSYonghong Song 	typeof(actual) ___act = (actual);				\
2529de7f0fdSYonghong Song 	typeof(expected) ___exp = (expected);				\
2539de7f0fdSYonghong Song 	bool ___ok = ___act < ___exp;					\
2549de7f0fdSYonghong Song 	CHECK(!___ok, (name),						\
2559de7f0fdSYonghong Song 	      "unexpected %s: actual %lld >= expected %lld\n",		\
2569de7f0fdSYonghong Song 	      (name), (long long)(___act), (long long)(___exp));	\
2579de7f0fdSYonghong Song 	___ok;								\
2589de7f0fdSYonghong Song })
2599de7f0fdSYonghong Song 
2607a2fa70aSAndrii Nakryiko #define ASSERT_LE(actual, expected, name) ({				\
2617a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2627a2fa70aSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
2637a2fa70aSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
2647a2fa70aSAndrii Nakryiko 	bool ___ok = ___act <= ___exp;					\
2657a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name),						\
2667a2fa70aSAndrii Nakryiko 	      "unexpected %s: actual %lld > expected %lld\n",		\
2677a2fa70aSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
2687a2fa70aSAndrii Nakryiko 	___ok;								\
2697a2fa70aSAndrii Nakryiko })
2707a2fa70aSAndrii Nakryiko 
2717a2fa70aSAndrii Nakryiko #define ASSERT_GT(actual, expected, name) ({				\
2727a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2737a2fa70aSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
2747a2fa70aSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
2757a2fa70aSAndrii Nakryiko 	bool ___ok = ___act > ___exp;					\
2767a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name),						\
2777a2fa70aSAndrii Nakryiko 	      "unexpected %s: actual %lld <= expected %lld\n",		\
2787a2fa70aSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
2797a2fa70aSAndrii Nakryiko 	___ok;								\
2807a2fa70aSAndrii Nakryiko })
2817a2fa70aSAndrii Nakryiko 
2827a2fa70aSAndrii Nakryiko #define ASSERT_GE(actual, expected, name) ({				\
2837a2fa70aSAndrii Nakryiko 	static int duration = 0;					\
2847a2fa70aSAndrii Nakryiko 	typeof(actual) ___act = (actual);				\
2857a2fa70aSAndrii Nakryiko 	typeof(expected) ___exp = (expected);				\
2867a2fa70aSAndrii Nakryiko 	bool ___ok = ___act >= ___exp;					\
2877a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name),						\
2887a2fa70aSAndrii Nakryiko 	      "unexpected %s: actual %lld < expected %lld\n",		\
2897a2fa70aSAndrii Nakryiko 	      (name), (long long)(___act), (long long)(___exp));	\
2907a2fa70aSAndrii Nakryiko 	___ok;								\
2917a2fa70aSAndrii Nakryiko })
2927a2fa70aSAndrii Nakryiko 
29322ba3635SAndrii Nakryiko #define ASSERT_STREQ(actual, expected, name) ({				\
29422ba3635SAndrii Nakryiko 	static int duration = 0;					\
29522ba3635SAndrii Nakryiko 	const char *___act = actual;					\
29622ba3635SAndrii Nakryiko 	const char *___exp = expected;					\
29722ba3635SAndrii Nakryiko 	bool ___ok = strcmp(___act, ___exp) == 0;			\
29822ba3635SAndrii Nakryiko 	CHECK(!___ok, (name),						\
29922ba3635SAndrii Nakryiko 	      "unexpected %s: actual '%s' != expected '%s'\n",		\
30022ba3635SAndrii Nakryiko 	      (name), ___act, ___exp);					\
30122ba3635SAndrii Nakryiko 	___ok;								\
30222ba3635SAndrii Nakryiko })
30322ba3635SAndrii Nakryiko 
30417283337SAlan Maguire #define ASSERT_STRNEQ(actual, expected, len, name) ({			\
30517283337SAlan Maguire 	static int duration = 0;					\
30617283337SAlan Maguire 	const char *___act = actual;					\
30717283337SAlan Maguire 	const char *___exp = expected;					\
30817283337SAlan Maguire 	int ___len = len;						\
30917283337SAlan Maguire 	bool ___ok = strncmp(___act, ___exp, ___len) == 0;		\
31017283337SAlan Maguire 	CHECK(!___ok, (name),						\
31117283337SAlan Maguire 	      "unexpected %s: actual '%.*s' != expected '%.*s'\n",	\
31217283337SAlan Maguire 	      (name), ___len, ___act, ___len, ___exp);			\
31317283337SAlan Maguire 	___ok;								\
31417283337SAlan Maguire })
31517283337SAlan Maguire 
316ea4128ebSAndrii Nakryiko #define ASSERT_HAS_SUBSTR(str, substr, name) ({				\
317ea4128ebSAndrii Nakryiko 	static int duration = 0;					\
318ea4128ebSAndrii Nakryiko 	const char *___str = str;					\
319ea4128ebSAndrii Nakryiko 	const char *___substr = substr;					\
320ea4128ebSAndrii Nakryiko 	bool ___ok = strstr(___str, ___substr) != NULL;			\
321ea4128ebSAndrii Nakryiko 	CHECK(!___ok, (name),						\
322ea4128ebSAndrii Nakryiko 	      "unexpected %s: '%s' is not a substring of '%s'\n",	\
323ea4128ebSAndrii Nakryiko 	      (name), ___substr, ___str);				\
324ea4128ebSAndrii Nakryiko 	___ok;								\
325ea4128ebSAndrii Nakryiko })
326ea4128ebSAndrii Nakryiko 
32722ba3635SAndrii Nakryiko #define ASSERT_OK(res, name) ({						\
32822ba3635SAndrii Nakryiko 	static int duration = 0;					\
32922ba3635SAndrii Nakryiko 	long long ___res = (res);					\
33022ba3635SAndrii Nakryiko 	bool ___ok = ___res == 0;					\
3317a2fa70aSAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n",	\
3327a2fa70aSAndrii Nakryiko 	      ___res, errno);						\
33322ba3635SAndrii Nakryiko 	___ok;								\
33422ba3635SAndrii Nakryiko })
33522ba3635SAndrii Nakryiko 
33622ba3635SAndrii Nakryiko #define ASSERT_ERR(res, name) ({					\
33722ba3635SAndrii Nakryiko 	static int duration = 0;					\
33822ba3635SAndrii Nakryiko 	long long ___res = (res);					\
33922ba3635SAndrii Nakryiko 	bool ___ok = ___res < 0;					\
34022ba3635SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);	\
34122ba3635SAndrii Nakryiko 	___ok;								\
34222ba3635SAndrii Nakryiko })
34322ba3635SAndrii Nakryiko 
34422ba3635SAndrii Nakryiko #define ASSERT_NULL(ptr, name) ({					\
34522ba3635SAndrii Nakryiko 	static int duration = 0;					\
34622ba3635SAndrii Nakryiko 	const void *___res = (ptr);					\
34722ba3635SAndrii Nakryiko 	bool ___ok = !___res;						\
34822ba3635SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
34922ba3635SAndrii Nakryiko 	___ok;								\
35022ba3635SAndrii Nakryiko })
35122ba3635SAndrii Nakryiko 
35222ba3635SAndrii Nakryiko #define ASSERT_OK_PTR(ptr, name) ({					\
35322ba3635SAndrii Nakryiko 	static int duration = 0;					\
35422ba3635SAndrii Nakryiko 	const void *___res = (ptr);					\
355bad2e478SAndrii Nakryiko 	int ___err = libbpf_get_error(___res);				\
356bad2e478SAndrii Nakryiko 	bool ___ok = ___err == 0;					\
357bad2e478SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected error: %d\n", ___err);	\
35822ba3635SAndrii Nakryiko 	___ok;								\
35922ba3635SAndrii Nakryiko })
36022ba3635SAndrii Nakryiko 
36122ba3635SAndrii Nakryiko #define ASSERT_ERR_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;					\
36622ba3635SAndrii Nakryiko 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
36722ba3635SAndrii Nakryiko 	___ok;								\
36822ba3635SAndrii Nakryiko })
36922ba3635SAndrii Nakryiko 
370886225bbSStanislav Fomichev static inline __u64 ptr_to_u64(const void *ptr)
371886225bbSStanislav Fomichev {
372886225bbSStanislav Fomichev 	return (__u64) (unsigned long) ptr;
373886225bbSStanislav Fomichev }
374886225bbSStanislav Fomichev 
3759028bbccSAndrii Nakryiko static inline void *u64_to_ptr(__u64 ptr)
3769028bbccSAndrii Nakryiko {
3779028bbccSAndrii Nakryiko 	return (void *) (unsigned long) ptr;
3789028bbccSAndrii Nakryiko }
3799028bbccSAndrii Nakryiko 
3803f306588SStanislav Fomichev int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
381615741d8SStanislav Fomichev int compare_map_keys(int map1_fd, int map2_fd);
382615741d8SStanislav Fomichev int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
383615741d8SStanislav Fomichev int extract_build_id(char *build_id, size_t size);
384635599baSAndrii Nakryiko int kern_sync_rcu(void);
385025bd7c7SSong Liu int trigger_module_test_read(int read_sz);
386025bd7c7SSong Liu int trigger_module_test_write(int write_sz);
387*e42921c3SYiFei Zhu int write_sysctl(const char *sysctl, const char *value);
3881cb59a60SIlya Leoshkevich 
3891cb59a60SIlya Leoshkevich #ifdef __x86_64__
3901cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
3911cb59a60SIlya Leoshkevich #elif defined(__s390x__)
3921cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
393d2987617SIlya Leoshkevich #elif defined(__aarch64__)
394d2987617SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
3951cb59a60SIlya Leoshkevich #else
3961cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
3971cb59a60SIlya Leoshkevich #endif
398fa7f17d0SHou Tao 
399fa7f17d0SHou Tao #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
400