selftests/bpf: switch tests to new bpf_object__open_{file, mem}() APIs
authorAndrii Nakryiko <andriin@fb.com>
Fri, 4 Oct 2019 22:40:37 +0000 (15:40 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 6 Oct 2019 01:09:48 +0000 (18:09 -0700)
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: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/Makefile
tools/testing/selftests/bpf/prog_tests/attach_probe.c
tools/testing/selftests/bpf/prog_tests/reference_tracking.c

index 6889c19a628c238a90da948d5733056039998fbd..294d7472dad7a4d758ee14e5674ff396e4f32bfb 100644 (file)
@@ -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)/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_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
index 5ecc267d98b06b5289d39d01b85630a7145347b1..4f50d32c4abb5cedb57bb642930bb68840d3da4a 100644 (file)
@@ -1,6 +1,24 @@
 // SPDX-License-Identifier: GPL-2.0
 #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() {
        size_t start;
        char buf[256];
@@ -21,6 +39,8 @@ ssize_t get_base_addr() {
        return -EINVAL;
 }
 
+EMBED_FILE(probe, "test_attach_probe.o");
+
 void test_attach_probe(void)
 {
        const char *kprobe_name = "kprobe/sys_nanosleep";
@@ -29,11 +49,15 @@ void test_attach_probe(void)
        const char *uretprobe_name = "uretprobe/trigger_func";
        const int kprobe_idx = 0, kretprobe_idx = 1;
        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 *uprobe_prog, *uretprobe_prog;
        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 *kretprobe_link = NULL;
        struct bpf_link *uprobe_link = NULL;
@@ -48,11 +72,16 @@ void test_attach_probe(void)
                return;
        uprobe_offset = (size_t)&get_base_addr - base_addr;
 
-       /* load programs */
-       err = bpf_prog_load(file, BPF_PROG_TYPE_KPROBE, &obj, &prog_fd);
-       if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno))
+       /* open object */
+       obj = bpf_object__open_mem(probe_data, probe_size, &open_opts);
+       if (CHECK(IS_ERR(obj), "obj_open_mem", "err %ld\n", PTR_ERR(obj)))
                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);
        if (CHECK(!kprobe_prog, "find_probe",
                  "prog '%s' not found\n", kprobe_name))
@@ -70,6 +99,16 @@ void test_attach_probe(void)
                  "prog '%s' not found\n", uretprobe_name))
                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 */
        results_map_fd = bpf_find_map(__func__, obj, "results_map");
        if (CHECK(results_map_fd < 0, "find_results_map",
index 5c78e2b5a917421d0f8c2d21a91584b3514379a7..86cee820d4d3ab82123bf9f6d32d27ac807baea2 100644 (file)
@@ -3,16 +3,26 @@
 
 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_program *prog;
        __u32 duration = 0;
        int err = 0;
 
-       obj = bpf_object__open(file);
+       obj = bpf_object__open_file(file, &open_opts);
        if (CHECK_FAIL(IS_ERR(obj)))
                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) {
                const char *title;
 
@@ -35,5 +45,7 @@ void test_reference_tracking(void)
                }
                CHECK(err, title, "\n");
        }
+
+cleanup:
        bpf_object__close(obj);
 }