1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3 
4 #include <test_progs.h>
5 #include "cpumask_failure.skel.h"
6 #include "cpumask_success.skel.h"
7 
8 static const char * const cpumask_success_testcases[] = {
9 	"test_alloc_free_cpumask",
10 	"test_set_clear_cpu",
11 	"test_setall_clear_cpu",
12 	"test_first_firstzero_cpu",
13 	"test_firstand_nocpu",
14 	"test_test_and_set_clear",
15 	"test_and_or_xor",
16 	"test_intersects_subset",
17 	"test_copy_any_anyand",
18 	"test_insert_leave",
19 	"test_insert_remove_release",
20 	"test_global_mask_rcu",
21 };
22 
verify_success(const char * prog_name)23 static void verify_success(const char *prog_name)
24 {
25 	struct cpumask_success *skel;
26 	struct bpf_program *prog;
27 	struct bpf_link *link = NULL;
28 	pid_t child_pid;
29 	int status;
30 
31 	skel = cpumask_success__open();
32 	if (!ASSERT_OK_PTR(skel, "cpumask_success__open"))
33 		return;
34 
35 	skel->bss->pid = getpid();
36 	skel->bss->nr_cpus = libbpf_num_possible_cpus();
37 
38 	cpumask_success__load(skel);
39 	if (!ASSERT_OK_PTR(skel, "cpumask_success__load"))
40 		goto cleanup;
41 
42 	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
43 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
44 		goto cleanup;
45 
46 	link = bpf_program__attach(prog);
47 	if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
48 		goto cleanup;
49 
50 	child_pid = fork();
51 	if (!ASSERT_GT(child_pid, -1, "child_pid"))
52 		goto cleanup;
53 	if (child_pid == 0)
54 		_exit(0);
55 	waitpid(child_pid, &status, 0);
56 	ASSERT_OK(skel->bss->err, "post_wait_err");
57 
58 cleanup:
59 	bpf_link__destroy(link);
60 	cpumask_success__destroy(skel);
61 }
62 
test_cpumask(void)63 void test_cpumask(void)
64 {
65 	int i;
66 
67 	for (i = 0; i < ARRAY_SIZE(cpumask_success_testcases); i++) {
68 		if (!test__start_subtest(cpumask_success_testcases[i]))
69 			continue;
70 
71 		verify_success(cpumask_success_testcases[i]);
72 	}
73 
74 	RUN_TESTS(cpumask_success);
75 	RUN_TESTS(cpumask_failure);
76 }
77