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 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 (!ASSERT_OK_PTR(obj, "obj_open")) 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, ".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 (!ASSERT_OK_PTR(link, "attach_prog")) 62 goto cleanup; 63 64 /* trigger probe */ 65 usleep(1); 66 67 bpf_link__destroy(link); 68 link = NULL; 69 70 err = bpf_map_lookup_elem(bpf_map__fd(bss_map), &zero, &bss); 71 if (CHECK(err, "get_bss", "failed to get bss data: %d\n", err)) 72 goto cleanup; 73 if (CHECK(bss.did_run == 0, "check_run", 74 "prog '%s' didn't run?\n", t->prog_name)) 75 goto cleanup; 76 if (CHECK(bss.iters != t->exp_iters, "check_iters", 77 "prog '%s' iters: %d, expected: %d\n", 78 t->prog_name, bss.iters, t->exp_iters)) 79 goto cleanup; 80 if (CHECK(bss.sum != t->exp_sum, "check_sum", 81 "prog '%s' sum: %d, expected: %d\n", 82 t->prog_name, bss.sum, t->exp_sum)) 83 goto cleanup; 84 } 85 86 cleanup: 87 bpf_link__destroy(link); 88 bpf_object__close(obj); 89 } 90