selftests/bpf: revamp test_progs to allow more control
[platform/kernel/linux-rpi.git] / tools / testing / selftests / bpf / test_progs.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3  */
4 #include "test_progs.h"
5 #include "bpf_rlimit.h"
6 #include <argp.h>
7
8 int error_cnt, pass_cnt;
9 bool jit_enabled;
10 bool verifier_stats = false;
11
12 struct ipv4_packet pkt_v4 = {
13         .eth.h_proto = __bpf_constant_htons(ETH_P_IP),
14         .iph.ihl = 5,
15         .iph.protocol = IPPROTO_TCP,
16         .iph.tot_len = __bpf_constant_htons(MAGIC_BYTES),
17         .tcp.urg_ptr = 123,
18         .tcp.doff = 5,
19 };
20
21 struct ipv6_packet pkt_v6 = {
22         .eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
23         .iph.nexthdr = IPPROTO_TCP,
24         .iph.payload_len = __bpf_constant_htons(MAGIC_BYTES),
25         .tcp.urg_ptr = 123,
26         .tcp.doff = 5,
27 };
28
29 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
30 {
31         struct bpf_map *map;
32
33         map = bpf_object__find_map_by_name(obj, name);
34         if (!map) {
35                 printf("%s:FAIL:map '%s' not found\n", test, name);
36                 error_cnt++;
37                 return -1;
38         }
39         return bpf_map__fd(map);
40 }
41
42 static bool is_jit_enabled(void)
43 {
44         const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable";
45         bool enabled = false;
46         int sysctl_fd;
47
48         sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
49         if (sysctl_fd != -1) {
50                 char tmpc;
51
52                 if (read(sysctl_fd, &tmpc, sizeof(tmpc)) == 1)
53                         enabled = (tmpc != '0');
54                 close(sysctl_fd);
55         }
56
57         return enabled;
58 }
59
60 int compare_map_keys(int map1_fd, int map2_fd)
61 {
62         __u32 key, next_key;
63         char val_buf[PERF_MAX_STACK_DEPTH *
64                      sizeof(struct bpf_stack_build_id)];
65         int err;
66
67         err = bpf_map_get_next_key(map1_fd, NULL, &key);
68         if (err)
69                 return err;
70         err = bpf_map_lookup_elem(map2_fd, &key, val_buf);
71         if (err)
72                 return err;
73
74         while (bpf_map_get_next_key(map1_fd, &key, &next_key) == 0) {
75                 err = bpf_map_lookup_elem(map2_fd, &next_key, val_buf);
76                 if (err)
77                         return err;
78
79                 key = next_key;
80         }
81         if (errno != ENOENT)
82                 return -1;
83
84         return 0;
85 }
86
87 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
88 {
89         __u32 key, next_key, *cur_key_p, *next_key_p;
90         char *val_buf1, *val_buf2;
91         int i, err = 0;
92
93         val_buf1 = malloc(stack_trace_len);
94         val_buf2 = malloc(stack_trace_len);
95         cur_key_p = NULL;
96         next_key_p = &key;
97         while (bpf_map_get_next_key(smap_fd, cur_key_p, next_key_p) == 0) {
98                 err = bpf_map_lookup_elem(smap_fd, next_key_p, val_buf1);
99                 if (err)
100                         goto out;
101                 err = bpf_map_lookup_elem(amap_fd, next_key_p, val_buf2);
102                 if (err)
103                         goto out;
104                 for (i = 0; i < stack_trace_len; i++) {
105                         if (val_buf1[i] != val_buf2[i]) {
106                                 err = -1;
107                                 goto out;
108                         }
109                 }
110                 key = *next_key_p;
111                 cur_key_p = &key;
112                 next_key_p = &next_key;
113         }
114         if (errno != ENOENT)
115                 err = -1;
116
117 out:
118         free(val_buf1);
119         free(val_buf2);
120         return err;
121 }
122
123 int extract_build_id(char *build_id, size_t size)
124 {
125         FILE *fp;
126         char *line = NULL;
127         size_t len = 0;
128
129         fp = popen("readelf -n ./urandom_read | grep 'Build ID'", "r");
130         if (fp == NULL)
131                 return -1;
132
133         if (getline(&line, &len, fp) == -1)
134                 goto err;
135         fclose(fp);
136
137         if (len > size)
138                 len = size;
139         memcpy(build_id, line, len);
140         build_id[len] = '\0';
141         return 0;
142 err:
143         fclose(fp);
144         return -1;
145 }
146
147 void *spin_lock_thread(void *arg)
148 {
149         __u32 duration, retval;
150         int err, prog_fd = *(u32 *) arg;
151
152         err = bpf_prog_test_run(prog_fd, 10000, &pkt_v4, sizeof(pkt_v4),
153                                 NULL, NULL, &retval, &duration);
154         CHECK(err || retval, "",
155               "err %d errno %d retval %d duration %d\n",
156               err, errno, retval, duration);
157         pthread_exit(arg);
158 }
159
160 /* extern declarations for test funcs */
161 #define DEFINE_TEST(name) extern void test_##name();
162 #include <prog_tests/tests.h>
163 #undef DEFINE_TEST
164
165 struct prog_test_def {
166         const char *test_name;
167         void (*run_test)(void);
168 };
169
170 static struct prog_test_def prog_test_defs[] = {
171 #define DEFINE_TEST(name) {           \
172         .test_name = #name,           \
173         .run_test = &test_##name,   \
174 },
175 #include <prog_tests/tests.h>
176 #undef DEFINE_TEST
177 };
178
179 const char *argp_program_version = "test_progs 0.1";
180 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
181 const char argp_program_doc[] = "BPF selftests test runner";
182
183 enum ARG_KEYS {
184         ARG_VERIFIER_STATS = 's',
185 };
186         
187 static const struct argp_option opts[] = {
188         { "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
189           "Output verifier statistics", },
190         {},
191 };
192
193 struct test_env {
194         bool verifier_stats;
195 };
196
197 static struct test_env env = {};
198
199 static error_t parse_arg(int key, char *arg, struct argp_state *state)
200 {
201         struct test_env *env = state->input;
202
203         switch (key) {
204         case ARG_VERIFIER_STATS:
205                 env->verifier_stats = true;
206                 break;
207         case ARGP_KEY_ARG:
208                 argp_usage(state);
209                 break;
210         case ARGP_KEY_END:
211                 break;
212         default:
213                 return ARGP_ERR_UNKNOWN;
214         }
215         return 0;
216 }
217
218
219 int main(int argc, char **argv)
220 {
221         static const struct argp argp = {
222                 .options = opts,
223                 .parser = parse_arg,
224                 .doc = argp_program_doc,
225         };
226         const struct prog_test_def *def;
227         int err, i;
228
229         err = argp_parse(&argp, argc, argv, 0, NULL, &env);
230         if (err)
231                 return err;
232
233         srand(time(NULL));
234
235         jit_enabled = is_jit_enabled();
236
237         verifier_stats = env.verifier_stats;
238
239         for (i = 0; i < ARRAY_SIZE(prog_test_defs); i++) {
240                 def = &prog_test_defs[i];
241                 def->run_test();
242         }
243
244         printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
245         return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
246 }