xref: /openbmc/linux/tools/testing/selftests/bpf/test_progs.h (revision a8fdaad5cfd250b9effcec942b3bf7bc5a6c8b17)
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"
383f306588SStanislav Fomichev #include "bpf_endian.h"
393f306588SStanislav Fomichev #include "trace_helpers.h"
403f306588SStanislav Fomichev #include "flow_dissector_load.h"
413f306588SStanislav Fomichev 
42*a8fdaad5SAndrii Nakryiko enum verbosity {
43*a8fdaad5SAndrii Nakryiko 	VERBOSE_NONE,
44*a8fdaad5SAndrii Nakryiko 	VERBOSE_NORMAL,
45*a8fdaad5SAndrii Nakryiko 	VERBOSE_VERY,
46*a8fdaad5SAndrii Nakryiko 	VERBOSE_SUPER,
47*a8fdaad5SAndrii Nakryiko };
48*a8fdaad5SAndrii Nakryiko 
493a516a0aSAndrii Nakryiko struct test_selector {
503a516a0aSAndrii Nakryiko 	const char *name;
513a516a0aSAndrii Nakryiko 	bool *num_set;
523a516a0aSAndrii Nakryiko 	int num_set_len;
533a516a0aSAndrii Nakryiko };
543a516a0aSAndrii Nakryiko 
550ff97e56SAndrii Nakryiko struct test_env {
563a516a0aSAndrii Nakryiko 	struct test_selector test_selector;
573a516a0aSAndrii Nakryiko 	struct test_selector subtest_selector;
580ff97e56SAndrii Nakryiko 	bool verifier_stats;
59*a8fdaad5SAndrii Nakryiko 	enum verbosity verbosity;
600ff97e56SAndrii Nakryiko 
610ff97e56SAndrii Nakryiko 	bool jit_enabled;
620ff97e56SAndrii Nakryiko 
630ff97e56SAndrii Nakryiko 	struct prog_test_def *test;
64946152b3SStanislav Fomichev 	FILE *stdout;
65946152b3SStanislav Fomichev 	FILE *stderr;
660ff97e56SAndrii Nakryiko 	char *log_buf;
670ff97e56SAndrii Nakryiko 	size_t log_cnt;
680ff97e56SAndrii Nakryiko 
693a516a0aSAndrii Nakryiko 	int succ_cnt; /* successful tests */
703a516a0aSAndrii Nakryiko 	int sub_succ_cnt; /* successful sub-tests */
713a516a0aSAndrii Nakryiko 	int fail_cnt; /* total failed tests + sub-tests */
72cd9c21d7SStanislav Fomichev 	int skip_cnt; /* skipped tests */
730ff97e56SAndrii Nakryiko };
740ff97e56SAndrii Nakryiko 
750ff97e56SAndrii Nakryiko extern struct test_env env;
760ff97e56SAndrii Nakryiko 
770ff97e56SAndrii Nakryiko extern void test__force_log();
783a516a0aSAndrii Nakryiko extern bool test__start_subtest(const char *name);
79cd9c21d7SStanislav Fomichev extern void test__skip(void);
80d38835b7SStanislav Fomichev extern void test__fail(void);
8188dadc63SStanislav Fomichev extern int test__join_cgroup(const char *path);
823f306588SStanislav Fomichev 
833f306588SStanislav Fomichev #define MAGIC_BYTES 123
843f306588SStanislav Fomichev 
853f306588SStanislav Fomichev /* ipv4 test vector */
863f306588SStanislav Fomichev struct ipv4_packet {
873f306588SStanislav Fomichev 	struct ethhdr eth;
883f306588SStanislav Fomichev 	struct iphdr iph;
893f306588SStanislav Fomichev 	struct tcphdr tcp;
903f306588SStanislav Fomichev } __packed;
913f306588SStanislav Fomichev extern struct ipv4_packet pkt_v4;
923f306588SStanislav Fomichev 
933f306588SStanislav Fomichev /* ipv6 test vector */
943f306588SStanislav Fomichev struct ipv6_packet {
953f306588SStanislav Fomichev 	struct ethhdr eth;
963f306588SStanislav Fomichev 	struct ipv6hdr iph;
973f306588SStanislav Fomichev 	struct tcphdr tcp;
983f306588SStanislav Fomichev } __packed;
993f306588SStanislav Fomichev extern struct ipv6_packet pkt_v6;
1003f306588SStanislav Fomichev 
1013f306588SStanislav Fomichev #define _CHECK(condition, tag, duration, format...) ({			\
1023f306588SStanislav Fomichev 	int __ret = !!(condition);					\
1033f306588SStanislav Fomichev 	if (__ret) {							\
104d38835b7SStanislav Fomichev 		test__fail();						\
10566bd2ec1SStanislav Fomichev 		printf("%s:FAIL:%s ", __func__, tag);			\
10666bd2ec1SStanislav Fomichev 		printf(format);						\
1073f306588SStanislav Fomichev 	} else {							\
10866bd2ec1SStanislav Fomichev 		printf("%s:PASS:%s %d nsec\n",				\
1090ff97e56SAndrii Nakryiko 		       __func__, tag, duration);			\
1103f306588SStanislav Fomichev 	}								\
1113f306588SStanislav Fomichev 	__ret;								\
1123f306588SStanislav Fomichev })
1133f306588SStanislav Fomichev 
114d38835b7SStanislav Fomichev #define CHECK_FAIL(condition) ({					\
115d38835b7SStanislav Fomichev 	int __ret = !!(condition);					\
116d38835b7SStanislav Fomichev 	if (__ret) {							\
117d38835b7SStanislav Fomichev 		test__fail();						\
118ac915762SStanislav Fomichev 		printf("%s:FAIL:%d\n", __func__, __LINE__);		\
119d38835b7SStanislav Fomichev 	}								\
120d38835b7SStanislav Fomichev 	__ret;								\
121d38835b7SStanislav Fomichev })
122d38835b7SStanislav Fomichev 
1233f306588SStanislav Fomichev #define CHECK(condition, tag, format...) \
1243f306588SStanislav Fomichev 	_CHECK(condition, tag, duration, format)
1253f306588SStanislav Fomichev #define CHECK_ATTR(condition, tag, format...) \
1263f306588SStanislav Fomichev 	_CHECK(condition, tag, tattr.duration, format)
1273f306588SStanislav Fomichev 
128eaf90740SStanislav Fomichev #define MAGIC_VAL 0x1234
129eaf90740SStanislav Fomichev #define NUM_ITER 100000
130eaf90740SStanislav Fomichev #define VIP_NUM 5
131eaf90740SStanislav Fomichev 
132886225bbSStanislav Fomichev static inline __u64 ptr_to_u64(const void *ptr)
133886225bbSStanislav Fomichev {
134886225bbSStanislav Fomichev 	return (__u64) (unsigned long) ptr;
135886225bbSStanislav Fomichev }
136886225bbSStanislav Fomichev 
1373f306588SStanislav Fomichev int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
138615741d8SStanislav Fomichev int compare_map_keys(int map1_fd, int map2_fd);
139615741d8SStanislav Fomichev int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
140615741d8SStanislav Fomichev int extract_build_id(char *build_id, size_t size);
141271a6337SStanislav Fomichev void *spin_lock_thread(void *arg);
1421cb59a60SIlya Leoshkevich 
1431cb59a60SIlya Leoshkevich #ifdef __x86_64__
1441cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
1451cb59a60SIlya Leoshkevich #elif defined(__s390x__)
1461cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
1471cb59a60SIlya Leoshkevich #else
1481cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
1491cb59a60SIlya Leoshkevich #endif
150