Commit 928ca75e authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

selftests/bpf: switch tests to new bpf_object__open_{file, mem}() APIs

Verify new bpf_object__open_mem() and bpf_object__open_file() APIs work
as expected by switching test_attach_probe test to use embedded BPF
object and bpf_object__open_mem() and test_reference_tracking to
bpf_object__open_file().
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent c9e4c301
...@@ -160,7 +160,7 @@ $(OUTPUT)/test_queue_map.o: test_queue_stack_map.h ...@@ -160,7 +160,7 @@ $(OUTPUT)/test_queue_map.o: test_queue_stack_map.h
$(OUTPUT)/test_stack_map.o: test_queue_stack_map.h $(OUTPUT)/test_stack_map.o: test_queue_stack_map.h
$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h $(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
$(OUTPUT)/test_progs.o: flow_dissector_load.h $(OUTPUT)/test_progs.o: flow_dissector_load.h $(OUTPUT)/test_attach_probe.o
BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris) BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF) BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
......
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include <test_progs.h> #include <test_progs.h>
#define EMBED_FILE(NAME, PATH) \
asm ( \
" .pushsection \".rodata\", \"a\", @progbits \n" \
" .global "#NAME"_data \n" \
#NAME"_data: \n" \
" .incbin \"" PATH "\" \n" \
#NAME"_data_end: \n" \
" .global "#NAME"_size \n" \
" .type "#NAME"_size, @object \n" \
" .size "#NAME"_size, 4 \n" \
" .align 4, \n" \
#NAME"_size: \n" \
" .int "#NAME"_data_end - "#NAME"_data \n" \
" .popsection \n" \
); \
extern char NAME##_data[]; \
extern int NAME##_size;
ssize_t get_base_addr() { ssize_t get_base_addr() {
size_t start; size_t start;
char buf[256]; char buf[256];
...@@ -21,6 +39,8 @@ ssize_t get_base_addr() { ...@@ -21,6 +39,8 @@ ssize_t get_base_addr() {
return -EINVAL; return -EINVAL;
} }
EMBED_FILE(probe, "test_attach_probe.o");
void test_attach_probe(void) void test_attach_probe(void)
{ {
const char *kprobe_name = "kprobe/sys_nanosleep"; const char *kprobe_name = "kprobe/sys_nanosleep";
...@@ -29,11 +49,15 @@ void test_attach_probe(void) ...@@ -29,11 +49,15 @@ void test_attach_probe(void)
const char *uretprobe_name = "uretprobe/trigger_func"; const char *uretprobe_name = "uretprobe/trigger_func";
const int kprobe_idx = 0, kretprobe_idx = 1; const int kprobe_idx = 0, kretprobe_idx = 1;
const int uprobe_idx = 2, uretprobe_idx = 3; const int uprobe_idx = 2, uretprobe_idx = 3;
const char *file = "./test_attach_probe.o"; const char *obj_name = "attach_probe";
LIBBPF_OPTS(bpf_object_open_opts, open_opts,
.object_name = obj_name,
.relaxed_maps = true,
);
struct bpf_program *kprobe_prog, *kretprobe_prog; struct bpf_program *kprobe_prog, *kretprobe_prog;
struct bpf_program *uprobe_prog, *uretprobe_prog; struct bpf_program *uprobe_prog, *uretprobe_prog;
struct bpf_object *obj; struct bpf_object *obj;
int err, prog_fd, duration = 0, res; int err, duration = 0, res;
struct bpf_link *kprobe_link = NULL; struct bpf_link *kprobe_link = NULL;
struct bpf_link *kretprobe_link = NULL; struct bpf_link *kretprobe_link = NULL;
struct bpf_link *uprobe_link = NULL; struct bpf_link *uprobe_link = NULL;
...@@ -48,11 +72,16 @@ void test_attach_probe(void) ...@@ -48,11 +72,16 @@ void test_attach_probe(void)
return; return;
uprobe_offset = (size_t)&get_base_addr - base_addr; uprobe_offset = (size_t)&get_base_addr - base_addr;
/* load programs */ /* open object */
err = bpf_prog_load(file, BPF_PROG_TYPE_KPROBE, &obj, &prog_fd); obj = bpf_object__open_mem(probe_data, probe_size, &open_opts);
if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno)) if (CHECK(IS_ERR(obj), "obj_open_mem", "err %ld\n", PTR_ERR(obj)))
return; return;
if (CHECK(strcmp(bpf_object__name(obj), obj_name), "obj_name",
"wrong obj name '%s', expected '%s'\n",
bpf_object__name(obj), obj_name))
goto cleanup;
kprobe_prog = bpf_object__find_program_by_title(obj, kprobe_name); kprobe_prog = bpf_object__find_program_by_title(obj, kprobe_name);
if (CHECK(!kprobe_prog, "find_probe", if (CHECK(!kprobe_prog, "find_probe",
"prog '%s' not found\n", kprobe_name)) "prog '%s' not found\n", kprobe_name))
...@@ -70,6 +99,16 @@ void test_attach_probe(void) ...@@ -70,6 +99,16 @@ void test_attach_probe(void)
"prog '%s' not found\n", uretprobe_name)) "prog '%s' not found\n", uretprobe_name))
goto cleanup; goto cleanup;
bpf_program__set_kprobe(kprobe_prog);
bpf_program__set_kprobe(kretprobe_prog);
bpf_program__set_kprobe(uprobe_prog);
bpf_program__set_kprobe(uretprobe_prog);
/* create maps && load programs */
err = bpf_object__load(obj);
if (CHECK(err, "obj_load", "err %d\n", err))
goto cleanup;
/* load maps */ /* load maps */
results_map_fd = bpf_find_map(__func__, obj, "results_map"); results_map_fd = bpf_find_map(__func__, obj, "results_map");
if (CHECK(results_map_fd < 0, "find_results_map", if (CHECK(results_map_fd < 0, "find_results_map",
......
...@@ -3,16 +3,26 @@ ...@@ -3,16 +3,26 @@
void test_reference_tracking(void) void test_reference_tracking(void)
{ {
const char *file = "./test_sk_lookup_kern.o"; const char *file = "test_sk_lookup_kern.o";
const char *obj_name = "ref_track";
LIBBPF_OPTS(bpf_object_open_opts, open_opts,
.object_name = obj_name,
.relaxed_maps = true,
);
struct bpf_object *obj; struct bpf_object *obj;
struct bpf_program *prog; struct bpf_program *prog;
__u32 duration = 0; __u32 duration = 0;
int err = 0; int err = 0;
obj = bpf_object__open(file); obj = bpf_object__open_file(file, &open_opts);
if (CHECK_FAIL(IS_ERR(obj))) if (CHECK_FAIL(IS_ERR(obj)))
return; return;
if (CHECK(strcmp(bpf_object__name(obj), obj_name), "obj_name",
"wrong obj name '%s', expected '%s'\n",
bpf_object__name(obj), obj_name))
goto cleanup;
bpf_object__for_each_program(prog, obj) { bpf_object__for_each_program(prog, obj) {
const char *title; const char *title;
...@@ -35,5 +45,7 @@ void test_reference_tracking(void) ...@@ -35,5 +45,7 @@ void test_reference_tracking(void)
} }
CHECK(err, title, "\n"); CHECK(err, title, "\n");
} }
cleanup:
bpf_object__close(obj); bpf_object__close(obj);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment