mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2025-01-10 15:54:39 +08:00
01af3bf067
Add APIs to get BPF program function name, as opposed to bpf_program__title(), which returns BPF program function's section name. Function name has a benefit of being a valid C identifier and uniquely identifies a specific BPF program, while section name can be duplicated across multiple independent BPF programs. Add also bpf_object__find_program_by_name(), similar to bpf_object__find_program_by_title(), to facilitate looking up BPF programs by their C function names. Convert one of selftests to new API for look up. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Martin KaFai Lau <kafai@fb.com> Link: https://lore.kernel.org/bpf/20191214014341.3442258-9-andriin@fb.com
93 lines
2.4 KiB
C
93 lines
2.4 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <test_progs.h>
|
|
|
|
struct bss {
|
|
unsigned did_run;
|
|
unsigned iters;
|
|
unsigned sum;
|
|
};
|
|
|
|
struct rdonly_map_subtest {
|
|
const char *subtest_name;
|
|
const char *prog_name;
|
|
unsigned exp_iters;
|
|
unsigned exp_sum;
|
|
};
|
|
|
|
void test_rdonly_maps(void)
|
|
{
|
|
const char *file = "test_rdonly_maps.o";
|
|
struct rdonly_map_subtest subtests[] = {
|
|
{ "skip loop", "skip_loop", 0, 0 },
|
|
{ "part loop", "part_loop", 3, 2 + 3 + 4 },
|
|
{ "full loop", "full_loop", 4, 2 + 3 + 4 + 5 },
|
|
};
|
|
int i, err, zero = 0, duration = 0;
|
|
struct bpf_link *link = NULL;
|
|
struct bpf_program *prog;
|
|
struct bpf_map *bss_map;
|
|
struct bpf_object *obj;
|
|
struct bss bss;
|
|
|
|
obj = bpf_object__open_file(file, NULL);
|
|
if (CHECK(IS_ERR(obj), "obj_open", "err %ld\n", PTR_ERR(obj)))
|
|
return;
|
|
|
|
err = bpf_object__load(obj);
|
|
if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno))
|
|
goto cleanup;
|
|
|
|
bss_map = bpf_object__find_map_by_name(obj, "test_rdo.bss");
|
|
if (CHECK(!bss_map, "find_bss_map", "failed\n"))
|
|
goto cleanup;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(subtests); i++) {
|
|
const struct rdonly_map_subtest *t = &subtests[i];
|
|
|
|
if (!test__start_subtest(t->subtest_name))
|
|
continue;
|
|
|
|
prog = bpf_object__find_program_by_name(obj, t->prog_name);
|
|
if (CHECK(!prog, "find_prog", "prog '%s' not found\n",
|
|
t->prog_name))
|
|
goto cleanup;
|
|
|
|
memset(&bss, 0, sizeof(bss));
|
|
err = bpf_map_update_elem(bpf_map__fd(bss_map), &zero, &bss, 0);
|
|
if (CHECK(err, "set_bss", "failed to set bss data: %d\n", err))
|
|
goto cleanup;
|
|
|
|
link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
|
|
if (CHECK(IS_ERR(link), "attach_prog", "prog '%s', err %ld\n",
|
|
t->prog_name, PTR_ERR(link))) {
|
|
link = NULL;
|
|
goto cleanup;
|
|
}
|
|
|
|
/* trigger probe */
|
|
usleep(1);
|
|
|
|
bpf_link__destroy(link);
|
|
link = NULL;
|
|
|
|
err = bpf_map_lookup_elem(bpf_map__fd(bss_map), &zero, &bss);
|
|
if (CHECK(err, "get_bss", "failed to get bss data: %d\n", err))
|
|
goto cleanup;
|
|
if (CHECK(bss.did_run == 0, "check_run",
|
|
"prog '%s' didn't run?\n", t->prog_name))
|
|
goto cleanup;
|
|
if (CHECK(bss.iters != t->exp_iters, "check_iters",
|
|
"prog '%s' iters: %d, expected: %d\n",
|
|
t->prog_name, bss.iters, t->exp_iters))
|
|
goto cleanup;
|
|
if (CHECK(bss.sum != t->exp_sum, "check_sum",
|
|
"prog '%s' sum: %d, expected: %d\n",
|
|
t->prog_name, bss.sum, t->exp_sum))
|
|
goto cleanup;
|
|
}
|
|
|
|
cleanup:
|
|
bpf_link__destroy(link);
|
|
bpf_object__close(obj);
|
|
}
|