1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3
4 struct bss {
5 unsigned did_run;
6 unsigned iters;
7 unsigned sum;
8 };
9
10 struct rdonly_map_subtest {
11 const char *subtest_name;
12 const char *prog_name;
13 unsigned exp_iters;
14 unsigned exp_sum;
15 };
16
test_rdonly_maps(void)17 void test_rdonly_maps(void)
18 {
19 const char *file = "test_rdonly_maps.o";
20 struct rdonly_map_subtest subtests[] = {
21 { "skip loop", "skip_loop", 0, 0 },
22 { "part loop", "part_loop", 3, 2 + 3 + 4 },
23 { "full loop", "full_loop", 4, 2 + 3 + 4 + 5 },
24 };
25 int i, err, zero = 0, duration = 0;
26 struct bpf_link *link = NULL;
27 struct bpf_program *prog;
28 struct bpf_map *bss_map;
29 struct bpf_object *obj;
30 struct bss bss;
31
32 obj = bpf_object__open_file(file, NULL);
33 if (CHECK(IS_ERR(obj), "obj_open", "err %ld\n", PTR_ERR(obj)))
34 return;
35
36 err = bpf_object__load(obj);
37 if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno))
38 goto cleanup;
39
40 bss_map = bpf_object__find_map_by_name(obj, "test_rdo.bss");
41 if (CHECK(!bss_map, "find_bss_map", "failed\n"))
42 goto cleanup;
43
44 for (i = 0; i < ARRAY_SIZE(subtests); i++) {
45 const struct rdonly_map_subtest *t = &subtests[i];
46
47 if (!test__start_subtest(t->subtest_name))
48 continue;
49
50 prog = bpf_object__find_program_by_name(obj, t->prog_name);
51 if (CHECK(!prog, "find_prog", "prog '%s' not found\n",
52 t->prog_name))
53 goto cleanup;
54
55 memset(&bss, 0, sizeof(bss));
56 err = bpf_map_update_elem(bpf_map__fd(bss_map), &zero, &bss, 0);
57 if (CHECK(err, "set_bss", "failed to set bss data: %d\n", err))
58 goto cleanup;
59
60 link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
61 if (CHECK(IS_ERR(link), "attach_prog", "prog '%s', err %ld\n",
62 t->prog_name, PTR_ERR(link))) {
63 link = NULL;
64 goto cleanup;
65 }
66
67 /* trigger probe */
68 usleep(1);
69
70 bpf_link__destroy(link);
71 link = NULL;
72
73 err = bpf_map_lookup_elem(bpf_map__fd(bss_map), &zero, &bss);
74 if (CHECK(err, "get_bss", "failed to get bss data: %d\n", err))
75 goto cleanup;
76 if (CHECK(bss.did_run == 0, "check_run",
77 "prog '%s' didn't run?\n", t->prog_name))
78 goto cleanup;
79 if (CHECK(bss.iters != t->exp_iters, "check_iters",
80 "prog '%s' iters: %d, expected: %d\n",
81 t->prog_name, bss.iters, t->exp_iters))
82 goto cleanup;
83 if (CHECK(bss.sum != t->exp_sum, "check_sum",
84 "prog '%s' sum: %d, expected: %d\n",
85 t->prog_name, bss.sum, t->exp_sum))
86 goto cleanup;
87 }
88
89 cleanup:
90 bpf_link__destroy(link);
91 bpf_object__close(obj);
92 }
93