xref: /openbmc/linux/tools/testing/selftests/bpf/test_progs.h (revision 3e2671fb9a95d2b46990832466383ec8384d88a3)
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>
199a365e67SStanislav Fomichev #include <netinet/tcp.h>
203f306588SStanislav Fomichev #include <linux/filter.h>
213f306588SStanislav Fomichev #include <linux/perf_event.h>
229a365e67SStanislav Fomichev #include <linux/socket.h>
233f306588SStanislav Fomichev #include <linux/unistd.h>
243f306588SStanislav Fomichev 
253f306588SStanislav Fomichev #include <sys/ioctl.h>
263f306588SStanislav Fomichev #include <sys/wait.h>
273f306588SStanislav Fomichev #include <sys/types.h>
283f306588SStanislav Fomichev #include <sys/time.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"
403f306588SStanislav Fomichev #include "flow_dissector_load.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 
49b65053cdSAndrii Nakryiko struct str_set {
50b65053cdSAndrii Nakryiko 	const char **strs;
51b65053cdSAndrii Nakryiko 	int cnt;
52b65053cdSAndrii Nakryiko };
53b65053cdSAndrii Nakryiko 
543a516a0aSAndrii Nakryiko struct test_selector {
55b65053cdSAndrii Nakryiko 	struct str_set whitelist;
56b65053cdSAndrii Nakryiko 	struct str_set blacklist;
573a516a0aSAndrii Nakryiko 	bool *num_set;
583a516a0aSAndrii Nakryiko 	int num_set_len;
593a516a0aSAndrii Nakryiko };
603a516a0aSAndrii Nakryiko 
610ff97e56SAndrii Nakryiko struct test_env {
623a516a0aSAndrii Nakryiko 	struct test_selector test_selector;
633a516a0aSAndrii Nakryiko 	struct test_selector subtest_selector;
640ff97e56SAndrii Nakryiko 	bool verifier_stats;
65a8fdaad5SAndrii Nakryiko 	enum verbosity verbosity;
660ff97e56SAndrii Nakryiko 
670ff97e56SAndrii Nakryiko 	bool jit_enabled;
680ff97e56SAndrii Nakryiko 
690ff97e56SAndrii Nakryiko 	struct prog_test_def *test;
70946152b3SStanislav Fomichev 	FILE *stdout;
71946152b3SStanislav Fomichev 	FILE *stderr;
720ff97e56SAndrii Nakryiko 	char *log_buf;
730ff97e56SAndrii Nakryiko 	size_t log_cnt;
740ff97e56SAndrii Nakryiko 
753a516a0aSAndrii Nakryiko 	int succ_cnt; /* successful tests */
763a516a0aSAndrii Nakryiko 	int sub_succ_cnt; /* successful sub-tests */
773a516a0aSAndrii Nakryiko 	int fail_cnt; /* total failed tests + sub-tests */
78cd9c21d7SStanislav Fomichev 	int skip_cnt; /* skipped tests */
790ff97e56SAndrii Nakryiko };
800ff97e56SAndrii Nakryiko 
810ff97e56SAndrii Nakryiko extern struct test_env env;
820ff97e56SAndrii Nakryiko 
830ff97e56SAndrii Nakryiko extern void test__force_log();
843a516a0aSAndrii Nakryiko extern bool test__start_subtest(const char *name);
85cd9c21d7SStanislav Fomichev extern void test__skip(void);
86d38835b7SStanislav Fomichev extern void test__fail(void);
8788dadc63SStanislav Fomichev extern int test__join_cgroup(const char *path);
883f306588SStanislav Fomichev 
893f306588SStanislav Fomichev #define MAGIC_BYTES 123
903f306588SStanislav Fomichev 
913f306588SStanislav Fomichev /* ipv4 test vector */
923f306588SStanislav Fomichev struct ipv4_packet {
933f306588SStanislav Fomichev 	struct ethhdr eth;
943f306588SStanislav Fomichev 	struct iphdr iph;
953f306588SStanislav Fomichev 	struct tcphdr tcp;
963f306588SStanislav Fomichev } __packed;
973f306588SStanislav Fomichev extern struct ipv4_packet pkt_v4;
983f306588SStanislav Fomichev 
993f306588SStanislav Fomichev /* ipv6 test vector */
1003f306588SStanislav Fomichev struct ipv6_packet {
1013f306588SStanislav Fomichev 	struct ethhdr eth;
1023f306588SStanislav Fomichev 	struct ipv6hdr iph;
1033f306588SStanislav Fomichev 	struct tcphdr tcp;
1043f306588SStanislav Fomichev } __packed;
1053f306588SStanislav Fomichev extern struct ipv6_packet pkt_v6;
1063f306588SStanislav Fomichev 
1073f306588SStanislav Fomichev #define _CHECK(condition, tag, duration, format...) ({			\
1083f306588SStanislav Fomichev 	int __ret = !!(condition);					\
109478bee0dSAndrey Ignatov 	int __save_errno = errno;					\
1103f306588SStanislav Fomichev 	if (__ret) {							\
111d38835b7SStanislav Fomichev 		test__fail();						\
112*3e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
113*3e2671fbSAndrii Nakryiko 		fprintf(stdout, ##format);				\
1143f306588SStanislav Fomichev 	} else {							\
115*3e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
1160ff97e56SAndrii Nakryiko 		       __func__, tag, duration);			\
1173f306588SStanislav Fomichev 	}								\
118478bee0dSAndrey Ignatov 	errno = __save_errno;						\
1193f306588SStanislav Fomichev 	__ret;								\
1203f306588SStanislav Fomichev })
1213f306588SStanislav Fomichev 
122d38835b7SStanislav Fomichev #define CHECK_FAIL(condition) ({					\
123d38835b7SStanislav Fomichev 	int __ret = !!(condition);					\
124478bee0dSAndrey Ignatov 	int __save_errno = errno;					\
125d38835b7SStanislav Fomichev 	if (__ret) {							\
126d38835b7SStanislav Fomichev 		test__fail();						\
127*3e2671fbSAndrii Nakryiko 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
128d38835b7SStanislav Fomichev 	}								\
129478bee0dSAndrey Ignatov 	errno = __save_errno;						\
130d38835b7SStanislav Fomichev 	__ret;								\
131d38835b7SStanislav Fomichev })
132d38835b7SStanislav Fomichev 
1333f306588SStanislav Fomichev #define CHECK(condition, tag, format...) \
1343f306588SStanislav Fomichev 	_CHECK(condition, tag, duration, format)
1353f306588SStanislav Fomichev #define CHECK_ATTR(condition, tag, format...) \
1363f306588SStanislav Fomichev 	_CHECK(condition, tag, tattr.duration, format)
1373f306588SStanislav Fomichev 
138eaf90740SStanislav Fomichev #define MAGIC_VAL 0x1234
139eaf90740SStanislav Fomichev #define NUM_ITER 100000
140eaf90740SStanislav Fomichev #define VIP_NUM 5
141eaf90740SStanislav Fomichev 
142886225bbSStanislav Fomichev static inline __u64 ptr_to_u64(const void *ptr)
143886225bbSStanislav Fomichev {
144886225bbSStanislav Fomichev 	return (__u64) (unsigned long) ptr;
145886225bbSStanislav Fomichev }
146886225bbSStanislav Fomichev 
1473f306588SStanislav Fomichev int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
148615741d8SStanislav Fomichev int compare_map_keys(int map1_fd, int map2_fd);
149615741d8SStanislav Fomichev int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
150615741d8SStanislav Fomichev int extract_build_id(char *build_id, size_t size);
151271a6337SStanislav Fomichev void *spin_lock_thread(void *arg);
1521cb59a60SIlya Leoshkevich 
1531cb59a60SIlya Leoshkevich #ifdef __x86_64__
1541cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
1551cb59a60SIlya Leoshkevich #elif defined(__s390x__)
1561cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
1571cb59a60SIlya Leoshkevich #else
1581cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
1591cb59a60SIlya Leoshkevich #endif
160