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