xref: /linux/tools/testing/selftests/bpf/prog_tests/get_smp_processor_id.c (revision 5a8cd539ac19f7a68e68e1d25ef9ca2ff55b8500)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <test_progs.h>
4 #include "bpf/libbpf_internal.h"
5 #include "get_smp_processor_id.skel.h"
6 
test_get_smp_processor_id(void)7 void test_get_smp_processor_id(void)
8 {
9 	LIBBPF_OPTS(bpf_test_run_opts, opts,
10 		    .flags = BPF_F_TEST_RUN_ON_CPU,
11 		    .cpu = 0,
12 	);
13 	struct get_smp_processor_id *skel;
14 	int prog_fd, err, online_cpu_nr, i;
15 	bool *online = NULL;
16 
17 	err = parse_cpu_mask_file("/sys/devices/system/cpu/online",
18 				  &online, &online_cpu_nr);
19 	if (!ASSERT_OK(err, "parse_cpu_mask_file"))
20 		return;
21 
22 	skel = get_smp_processor_id__open_and_load();
23 	if (!ASSERT_OK_PTR(skel, "get_smp_processor_id__open_and_load"))
24 		goto cleanup;
25 
26 	prog_fd = bpf_program__fd(skel->progs.call_bpf_get_smp_processor_id);
27 
28 	for (i = 0; i < online_cpu_nr; i++) {
29 		if (!online[i])
30 			continue;
31 
32 		opts.cpu = i;
33 		skel->bss->cpu_nr_result = -1;
34 
35 		err = bpf_prog_test_run_opts(prog_fd, &opts);
36 		if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
37 			goto cleanup;
38 
39 		ASSERT_EQ(skel->bss->cpu_nr_result, opts.cpu, "cpu_nr_result");
40 	}
41 
42 cleanup:
43 	free(online);
44 	get_smp_processor_id__destroy(skel);
45 }
46