xref: /linux/tools/testing/selftests/bpf/testing_helpers.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (C) 2019 Netronome Systems, Inc. */
3 /* Copyright (C) 2020 Facebook, Inc. */
4 #include <ctype.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <bpf/bpf.h>
9 #include <bpf/libbpf.h>
10 #include "test_progs.h"
11 #include "testing_helpers.h"
12 #include <linux/membarrier.h>
13 
14 int parse_num_list(const char *s, bool **num_set, int *num_set_len)
15 {
16 	int i, set_len = 0, new_len, num, start = 0, end = -1;
17 	bool *set = NULL, *tmp, parsing_end = false;
18 	char *next;
19 
20 	while (s[0]) {
21 		errno = 0;
22 		num = strtol(s, &next, 10);
23 		if (errno)
24 			return -errno;
25 
26 		if (parsing_end)
27 			end = num;
28 		else
29 			start = num;
30 
31 		if (!parsing_end && *next == '-') {
32 			s = next + 1;
33 			parsing_end = true;
34 			continue;
35 		} else if (*next == ',') {
36 			parsing_end = false;
37 			s = next + 1;
38 			end = num;
39 		} else if (*next == '\0') {
40 			parsing_end = false;
41 			s = next;
42 			end = num;
43 		} else {
44 			return -EINVAL;
45 		}
46 
47 		if (start > end)
48 			return -EINVAL;
49 
50 		if (end + 1 > set_len) {
51 			new_len = end + 1;
52 			tmp = realloc(set, new_len);
53 			if (!tmp) {
54 				free(set);
55 				return -ENOMEM;
56 			}
57 			for (i = set_len; i < start; i++)
58 				tmp[i] = false;
59 			set = tmp;
60 			set_len = new_len;
61 		}
62 		for (i = start; i <= end; i++)
63 			set[i] = true;
64 	}
65 
66 	if (!set || parsing_end)
67 		return -EINVAL;
68 
69 	*num_set = set;
70 	*num_set_len = set_len;
71 
72 	return 0;
73 }
74 
75 static int do_insert_test(struct test_filter_set *set,
76 			  char *test_str,
77 			  char *subtest_str)
78 {
79 	struct test_filter *tmp, *test;
80 	char **ctmp;
81 	int i;
82 
83 	for (i = 0; i < set->cnt; i++) {
84 		test = &set->tests[i];
85 
86 		if (strcmp(test_str, test->name) == 0) {
87 			free(test_str);
88 			goto subtest;
89 		}
90 	}
91 
92 	tmp = realloc(set->tests, sizeof(*test) * (set->cnt + 1));
93 	if (!tmp)
94 		return -ENOMEM;
95 
96 	set->tests = tmp;
97 	test = &set->tests[set->cnt];
98 
99 	test->name = test_str;
100 	test->subtests = NULL;
101 	test->subtest_cnt = 0;
102 
103 	set->cnt++;
104 
105 subtest:
106 	if (!subtest_str)
107 		return 0;
108 
109 	for (i = 0; i < test->subtest_cnt; i++) {
110 		if (strcmp(subtest_str, test->subtests[i]) == 0) {
111 			free(subtest_str);
112 			return 0;
113 		}
114 	}
115 
116 	ctmp = realloc(test->subtests,
117 		       sizeof(*test->subtests) * (test->subtest_cnt + 1));
118 	if (!ctmp)
119 		return -ENOMEM;
120 
121 	test->subtests = ctmp;
122 	test->subtests[test->subtest_cnt] = subtest_str;
123 
124 	test->subtest_cnt++;
125 
126 	return 0;
127 }
128 
129 static int insert_test(struct test_filter_set *set,
130 		       char *test_spec,
131 		       bool is_glob_pattern)
132 {
133 	char *pattern, *subtest_str, *ext_test_str, *ext_subtest_str = NULL;
134 	int glob_chars = 0;
135 
136 	if (is_glob_pattern) {
137 		pattern = "%s";
138 	} else {
139 		pattern = "*%s*";
140 		glob_chars = 2;
141 	}
142 
143 	subtest_str = strchr(test_spec, '/');
144 	if (subtest_str) {
145 		*subtest_str = '\0';
146 		subtest_str += 1;
147 	}
148 
149 	ext_test_str = malloc(strlen(test_spec) + glob_chars + 1);
150 	if (!ext_test_str)
151 		goto err;
152 
153 	sprintf(ext_test_str, pattern, test_spec);
154 
155 	if (subtest_str) {
156 		ext_subtest_str = malloc(strlen(subtest_str) + glob_chars + 1);
157 		if (!ext_subtest_str)
158 			goto err;
159 
160 		sprintf(ext_subtest_str, pattern, subtest_str);
161 	}
162 
163 	return do_insert_test(set, ext_test_str, ext_subtest_str);
164 
165 err:
166 	free(ext_test_str);
167 	free(ext_subtest_str);
168 
169 	return -ENOMEM;
170 }
171 
172 int parse_test_list_file(const char *path,
173 			 struct test_filter_set *set,
174 			 bool is_glob_pattern)
175 {
176 	char *buf = NULL, *capture_start, *capture_end, *scan_end;
177 	size_t buflen = 0;
178 	int err = 0;
179 	FILE *f;
180 
181 	f = fopen(path, "r");
182 	if (!f) {
183 		err = -errno;
184 		fprintf(stderr, "Failed to open '%s': %d\n", path, err);
185 		return err;
186 	}
187 
188 	while (getline(&buf, &buflen, f) != -1) {
189 		capture_start = buf;
190 
191 		while (isspace(*capture_start))
192 			++capture_start;
193 
194 		capture_end = capture_start;
195 		scan_end = capture_start;
196 
197 		while (*scan_end && *scan_end != '#') {
198 			if (!isspace(*scan_end))
199 				capture_end = scan_end;
200 
201 			++scan_end;
202 		}
203 
204 		if (capture_end == capture_start)
205 			continue;
206 
207 		*(++capture_end) = '\0';
208 
209 		err = insert_test(set, capture_start, is_glob_pattern);
210 		if (err)
211 			break;
212 	}
213 
214 	fclose(f);
215 	return err;
216 }
217 
218 int parse_test_list(const char *s,
219 		    struct test_filter_set *set,
220 		    bool is_glob_pattern)
221 {
222 	char *input, *state = NULL, *test_spec;
223 	int err = 0;
224 
225 	input = strdup(s);
226 	if (!input)
227 		return -ENOMEM;
228 
229 	while ((test_spec = strtok_r(state ? NULL : input, ",", &state))) {
230 		err = insert_test(set, test_spec, is_glob_pattern);
231 		if (err)
232 			break;
233 	}
234 
235 	free(input);
236 	return err;
237 }
238 
239 __u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info)
240 {
241 	__u32 info_len = sizeof(*info);
242 	int err;
243 
244 	memset(info, 0, sizeof(*info));
245 	err = bpf_link_get_info_by_fd(bpf_link__fd(link), info, &info_len);
246 	if (err) {
247 		printf("failed to get link info: %d\n", -errno);
248 		return 0;
249 	}
250 	return info->prog_id;
251 }
252 
253 int extra_prog_load_log_flags = 0;
254 
255 int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
256 		       struct bpf_object **pobj, int *prog_fd)
257 {
258 	LIBBPF_OPTS(bpf_object_open_opts, opts,
259 		.kernel_log_level = extra_prog_load_log_flags,
260 	);
261 	struct bpf_object *obj;
262 	struct bpf_program *prog;
263 	__u32 flags;
264 	int err;
265 
266 	obj = bpf_object__open_file(file, &opts);
267 	if (!obj)
268 		return -errno;
269 
270 	prog = bpf_object__next_program(obj, NULL);
271 	if (!prog) {
272 		err = -ENOENT;
273 		goto err_out;
274 	}
275 
276 	if (type != BPF_PROG_TYPE_UNSPEC && bpf_program__type(prog) != type)
277 		bpf_program__set_type(prog, type);
278 
279 	flags = bpf_program__flags(prog) | BPF_F_TEST_RND_HI32 | BPF_F_TEST_REG_INVARIANTS;
280 	bpf_program__set_flags(prog, flags);
281 
282 	err = bpf_object__load(obj);
283 	if (err)
284 		goto err_out;
285 
286 	*pobj = obj;
287 	*prog_fd = bpf_program__fd(prog);
288 
289 	return 0;
290 err_out:
291 	bpf_object__close(obj);
292 	return err;
293 }
294 
295 int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
296 			  size_t insns_cnt, const char *license,
297 			  __u32 kern_version, char *log_buf,
298 			  size_t log_buf_sz)
299 {
300 	LIBBPF_OPTS(bpf_prog_load_opts, opts,
301 		.kern_version = kern_version,
302 		.prog_flags = BPF_F_TEST_RND_HI32 | BPF_F_TEST_REG_INVARIANTS,
303 		.log_level = extra_prog_load_log_flags,
304 		.log_buf = log_buf,
305 		.log_size = log_buf_sz,
306 	);
307 
308 	return bpf_prog_load(type, NULL, license, insns, insns_cnt, &opts);
309 }
310 
311 __u64 read_perf_max_sample_freq(void)
312 {
313 	__u64 sample_freq = 5000; /* fallback to 5000 on error */
314 	FILE *f;
315 
316 	f = fopen("/proc/sys/kernel/perf_event_max_sample_rate", "r");
317 	if (f == NULL) {
318 		printf("Failed to open /proc/sys/kernel/perf_event_max_sample_rate: err %d\n"
319 		       "return default value: 5000\n", -errno);
320 		return sample_freq;
321 	}
322 	if (fscanf(f, "%llu", &sample_freq) != 1) {
323 		printf("Failed to parse /proc/sys/kernel/perf_event_max_sample_rate: err %d\n"
324 		       "return default value: 5000\n", -errno);
325 	}
326 
327 	fclose(f);
328 	return sample_freq;
329 }
330 
331 static int finit_module(int fd, const char *param_values, int flags)
332 {
333 	return syscall(__NR_finit_module, fd, param_values, flags);
334 }
335 
336 static int delete_module(const char *name, int flags)
337 {
338 	return syscall(__NR_delete_module, name, flags);
339 }
340 
341 int unload_bpf_testmod(bool verbose)
342 {
343 	if (kern_sync_rcu())
344 		fprintf(stdout, "Failed to trigger kernel-side RCU sync!\n");
345 	if (delete_module("bpf_testmod", 0)) {
346 		if (errno == ENOENT) {
347 			if (verbose)
348 				fprintf(stdout, "bpf_testmod.ko is already unloaded.\n");
349 			return -1;
350 		}
351 		fprintf(stdout, "Failed to unload bpf_testmod.ko from kernel: %d\n", -errno);
352 		return -1;
353 	}
354 	if (verbose)
355 		fprintf(stdout, "Successfully unloaded bpf_testmod.ko.\n");
356 	return 0;
357 }
358 
359 int load_bpf_testmod(bool verbose)
360 {
361 	int fd;
362 
363 	if (verbose)
364 		fprintf(stdout, "Loading bpf_testmod.ko...\n");
365 
366 	fd = open("bpf_testmod.ko", O_RDONLY);
367 	if (fd < 0) {
368 		fprintf(stdout, "Can't find bpf_testmod.ko kernel module: %d\n", -errno);
369 		return -ENOENT;
370 	}
371 	if (finit_module(fd, "", 0)) {
372 		fprintf(stdout, "Failed to load bpf_testmod.ko into the kernel: %d\n", -errno);
373 		close(fd);
374 		return -EINVAL;
375 	}
376 	close(fd);
377 
378 	if (verbose)
379 		fprintf(stdout, "Successfully loaded bpf_testmod.ko.\n");
380 	return 0;
381 }
382 
383 /*
384  * Trigger synchronize_rcu() in kernel.
385  */
386 int kern_sync_rcu(void)
387 {
388 	return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);
389 }
390