selftests/bpf: test_progs: remove global fail/success counts
[muen/linux.git] / tools / testing / selftests / bpf / test_progs.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <time.h>
10 #include <signal.h>
11
12 #include <linux/types.h>
13 typedef __u16 __sum16;
14 #include <arpa/inet.h>
15 #include <linux/if_ether.h>
16 #include <linux/if_packet.h>
17 #include <linux/ip.h>
18 #include <linux/ipv6.h>
19 #include <linux/tcp.h>
20 #include <linux/filter.h>
21 #include <linux/perf_event.h>
22 #include <linux/unistd.h>
23
24 #include <sys/ioctl.h>
25 #include <sys/wait.h>
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <fcntl.h>
29 #include <pthread.h>
30 #include <linux/bpf.h>
31 #include <linux/err.h>
32 #include <bpf/bpf.h>
33 #include <bpf/libbpf.h>
34
35 #include "test_iptunnel_common.h"
36 #include "bpf_util.h"
37 #include "bpf_endian.h"
38 #include "trace_helpers.h"
39 #include "flow_dissector_load.h"
40
41 struct test_selector {
42         const char *name;
43         bool *num_set;
44         int num_set_len;
45 };
46
47 struct test_env {
48         struct test_selector test_selector;
49         struct test_selector subtest_selector;
50         bool verifier_stats;
51         bool verbose;
52         bool very_verbose;
53
54         bool jit_enabled;
55
56         struct prog_test_def *test;
57         FILE *stdout;
58         FILE *stderr;
59         char *log_buf;
60         size_t log_cnt;
61
62         int succ_cnt; /* successful tests */
63         int sub_succ_cnt; /* successful sub-tests */
64         int fail_cnt; /* total failed tests + sub-tests */
65         int skip_cnt; /* skipped tests */
66 };
67
68 extern struct test_env env;
69
70 extern void test__force_log();
71 extern bool test__start_subtest(const char *name);
72 extern void test__skip(void);
73 extern void test__fail(void);
74
75 #define MAGIC_BYTES 123
76
77 /* ipv4 test vector */
78 struct ipv4_packet {
79         struct ethhdr eth;
80         struct iphdr iph;
81         struct tcphdr tcp;
82 } __packed;
83 extern struct ipv4_packet pkt_v4;
84
85 /* ipv6 test vector */
86 struct ipv6_packet {
87         struct ethhdr eth;
88         struct ipv6hdr iph;
89         struct tcphdr tcp;
90 } __packed;
91 extern struct ipv6_packet pkt_v6;
92
93 #define _CHECK(condition, tag, duration, format...) ({                  \
94         int __ret = !!(condition);                                      \
95         if (__ret) {                                                    \
96                 test__fail();                                           \
97                 printf("%s:FAIL:%s ", __func__, tag);                   \
98                 printf(format);                                         \
99         } else {                                                        \
100                 printf("%s:PASS:%s %d nsec\n",                          \
101                        __func__, tag, duration);                        \
102         }                                                               \
103         __ret;                                                          \
104 })
105
106 #define CHECK_FAIL(condition) ({                                        \
107         int __ret = !!(condition);                                      \
108         if (__ret) {                                                    \
109                 test__fail();                                           \
110                 printf("%s:FAIL:%d ", __func__, __LINE__);              \
111         }                                                               \
112         __ret;                                                          \
113 })
114
115 #define CHECK(condition, tag, format...) \
116         _CHECK(condition, tag, duration, format)
117 #define CHECK_ATTR(condition, tag, format...) \
118         _CHECK(condition, tag, tattr.duration, format)
119
120 #define MAGIC_VAL 0x1234
121 #define NUM_ITER 100000
122 #define VIP_NUM 5
123
124 static inline __u64 ptr_to_u64(const void *ptr)
125 {
126         return (__u64) (unsigned long) ptr;
127 }
128
129 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
130 int compare_map_keys(int map1_fd, int map2_fd);
131 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
132 int extract_build_id(char *build_id, size_t size);
133 void *spin_lock_thread(void *arg);
134
135 #ifdef __x86_64__
136 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
137 #elif defined(__s390x__)
138 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
139 #else
140 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
141 #endif