1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
10 struct rdonly_map_subtest
{
11 const char *subtest_name
;
12 const char *prog_name
;
17 void test_rdonly_maps(void)
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 },
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
;
32 obj
= bpf_object__open_file(file
, NULL
);
33 if (CHECK(IS_ERR(obj
), "obj_open", "err %ld\n", PTR_ERR(obj
)))
36 err
= bpf_object__load(obj
);
37 if (CHECK(err
, "obj_load", "err %d errno %d\n", err
, errno
))
40 bss_map
= bpf_object__find_map_by_name(obj
, "test_rdo.bss");
41 if (CHECK(!bss_map
, "find_bss_map", "failed\n"))
44 for (i
= 0; i
< ARRAY_SIZE(subtests
); i
++) {
45 const struct rdonly_map_subtest
*t
= &subtests
[i
];
47 if (!test__start_subtest(t
->subtest_name
))
50 prog
= bpf_object__find_program_by_name(obj
, t
->prog_name
);
51 if (CHECK(!prog
, "find_prog", "prog '%s' not found\n",
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
))
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
))) {
70 bpf_link__destroy(link
);
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
))
76 if (CHECK(bss
.did_run
== 0, "check_run",
77 "prog '%s' didn't run?\n", t
->prog_name
))
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
))
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
))
90 bpf_link__destroy(link
);
91 bpf_object__close(obj
);