Commit 1bdb3427 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Daniel Borkmann

selftests/bpf: convert existing tracepoint tests to new APIs

Convert some existing tests that attach to tracepoints to use
bpf_program__attach_tracepoint API instead.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 1e8611bb
...@@ -4,11 +4,13 @@ ...@@ -4,11 +4,13 @@
void test_stacktrace_build_id(void) void test_stacktrace_build_id(void)
{ {
int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd; int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
const char *prog_name = "tracepoint/random/urandom_read";
const char *file = "./test_stacktrace_build_id.o"; const char *file = "./test_stacktrace_build_id.o";
int bytes, efd, err, pmu_fd, prog_fd, stack_trace_len; int err, prog_fd, stack_trace_len;
struct perf_event_attr attr = {};
__u32 key, previous_key, val, duration = 0; __u32 key, previous_key, val, duration = 0;
struct bpf_program *prog;
struct bpf_object *obj; struct bpf_object *obj;
struct bpf_link *link = NULL;
char buf[256]; char buf[256];
int i, j; int i, j;
struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH]; struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH];
...@@ -18,44 +20,16 @@ void test_stacktrace_build_id(void) ...@@ -18,44 +20,16 @@ void test_stacktrace_build_id(void)
retry: retry:
err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
goto out; return;
/* Get the ID for the sched/sched_switch tracepoint */
snprintf(buf, sizeof(buf),
"/sys/kernel/debug/tracing/events/random/urandom_read/id");
efd = open(buf, O_RDONLY, 0);
if (CHECK(efd < 0, "open", "err %d errno %d\n", efd, errno))
goto close_prog;
bytes = read(efd, buf, sizeof(buf)); prog = bpf_object__find_program_by_title(obj, prog_name);
close(efd); if (CHECK(!prog, "find_prog", "prog '%s' not found\n", prog_name))
if (CHECK(bytes <= 0 || bytes >= sizeof(buf),
"read", "bytes %d errno %d\n", bytes, errno))
goto close_prog; goto close_prog;
/* Open the perf event and attach bpf progrram */ link = bpf_program__attach_tracepoint(prog, "random", "urandom_read");
attr.config = strtol(buf, NULL, 0); if (CHECK(IS_ERR(link), "attach_tp", "err %ld\n", PTR_ERR(link)))
attr.type = PERF_TYPE_TRACEPOINT;
attr.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN;
attr.sample_period = 1;
attr.wakeup_events = 1;
pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
0 /* cpu 0 */, -1 /* group id */,
0 /* flags */);
if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
pmu_fd, errno))
goto close_prog; goto close_prog;
err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
if (CHECK(err, "perf_event_ioc_enable", "err %d errno %d\n",
err, errno))
goto close_pmu;
err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd);
if (CHECK(err, "perf_event_ioc_set_bpf", "err %d errno %d\n",
err, errno))
goto disable_pmu;
/* find map fds */ /* find map fds */
control_map_fd = bpf_find_map(__func__, obj, "control_map"); control_map_fd = bpf_find_map(__func__, obj, "control_map");
if (CHECK(control_map_fd < 0, "bpf_find_map control_map", if (CHECK(control_map_fd < 0, "bpf_find_map control_map",
...@@ -133,8 +107,7 @@ void test_stacktrace_build_id(void) ...@@ -133,8 +107,7 @@ void test_stacktrace_build_id(void)
* try it one more time. * try it one more time.
*/ */
if (build_id_matches < 1 && retry--) { if (build_id_matches < 1 && retry--) {
ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE); bpf_link__destroy(link);
close(pmu_fd);
bpf_object__close(obj); bpf_object__close(obj);
printf("%s:WARN:Didn't find expected build ID from the map, retrying\n", printf("%s:WARN:Didn't find expected build ID from the map, retrying\n",
__func__); __func__);
...@@ -152,14 +125,8 @@ void test_stacktrace_build_id(void) ...@@ -152,14 +125,8 @@ void test_stacktrace_build_id(void)
"err %d errno %d\n", err, errno); "err %d errno %d\n", err, errno);
disable_pmu: disable_pmu:
ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE); bpf_link__destroy(link);
close_pmu:
close(pmu_fd);
close_prog: close_prog:
bpf_object__close(obj); bpf_object__close(obj);
out:
return;
} }
...@@ -4,50 +4,26 @@ ...@@ -4,50 +4,26 @@
void test_stacktrace_map(void) void test_stacktrace_map(void)
{ {
int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd; int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
const char *prog_name = "tracepoint/sched/sched_switch";
int err, prog_fd, stack_trace_len;
const char *file = "./test_stacktrace_map.o"; const char *file = "./test_stacktrace_map.o";
int bytes, efd, err, pmu_fd, prog_fd, stack_trace_len;
struct perf_event_attr attr = {};
__u32 key, val, duration = 0; __u32 key, val, duration = 0;
struct bpf_program *prog;
struct bpf_object *obj; struct bpf_object *obj;
char buf[256]; struct bpf_link *link;
err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
return; return;
/* Get the ID for the sched/sched_switch tracepoint */ prog = bpf_object__find_program_by_title(obj, prog_name);
snprintf(buf, sizeof(buf), if (CHECK(!prog, "find_prog", "prog '%s' not found\n", prog_name))
"/sys/kernel/debug/tracing/events/sched/sched_switch/id");
efd = open(buf, O_RDONLY, 0);
if (CHECK(efd < 0, "open", "err %d errno %d\n", efd, errno))
goto close_prog; goto close_prog;
bytes = read(efd, buf, sizeof(buf)); link = bpf_program__attach_tracepoint(prog, "sched", "sched_switch");
close(efd); if (CHECK(IS_ERR(link), "attach_tp", "err %ld\n", PTR_ERR(link)))
if (bytes <= 0 || bytes >= sizeof(buf))
goto close_prog; goto close_prog;
/* Open the perf event and attach bpf progrram */
attr.config = strtol(buf, NULL, 0);
attr.type = PERF_TYPE_TRACEPOINT;
attr.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN;
attr.sample_period = 1;
attr.wakeup_events = 1;
pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
0 /* cpu 0 */, -1 /* group id */,
0 /* flags */);
if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
pmu_fd, errno))
goto close_prog;
err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
if (err)
goto disable_pmu;
err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd);
if (err)
goto disable_pmu;
/* find map fds */ /* find map fds */
control_map_fd = bpf_find_map(__func__, obj, "control_map"); control_map_fd = bpf_find_map(__func__, obj, "control_map");
if (control_map_fd < 0) if (control_map_fd < 0)
...@@ -96,8 +72,7 @@ void test_stacktrace_map(void) ...@@ -96,8 +72,7 @@ void test_stacktrace_map(void)
disable_pmu: disable_pmu:
error_cnt++; error_cnt++;
disable_pmu_noerr: disable_pmu_noerr:
ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE); bpf_link__destroy(link);
close(pmu_fd);
close_prog: close_prog:
bpf_object__close(obj); bpf_object__close(obj);
} }
...@@ -3,18 +3,25 @@ ...@@ -3,18 +3,25 @@
void test_stacktrace_map_raw_tp(void) void test_stacktrace_map_raw_tp(void)
{ {
const char *prog_name = "tracepoint/sched/sched_switch";
int control_map_fd, stackid_hmap_fd, stackmap_fd; int control_map_fd, stackid_hmap_fd, stackmap_fd;
const char *file = "./test_stacktrace_map.o"; const char *file = "./test_stacktrace_map.o";
int efd, err, prog_fd;
__u32 key, val, duration = 0; __u32 key, val, duration = 0;
int err, prog_fd;
struct bpf_program *prog;
struct bpf_object *obj; struct bpf_object *obj;
struct bpf_link *link = NULL;
err = bpf_prog_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno))
return; return;
efd = bpf_raw_tracepoint_open("sched_switch", prog_fd); prog = bpf_object__find_program_by_title(obj, prog_name);
if (CHECK(efd < 0, "raw_tp_open", "err %d errno %d\n", efd, errno)) if (CHECK(!prog, "find_prog", "prog '%s' not found\n", prog_name))
goto close_prog;
link = bpf_program__attach_raw_tracepoint(prog, "sched_switch");
if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n", PTR_ERR(link)))
goto close_prog; goto close_prog;
/* find map fds */ /* find map fds */
...@@ -55,5 +62,7 @@ void test_stacktrace_map_raw_tp(void) ...@@ -55,5 +62,7 @@ void test_stacktrace_map_raw_tp(void)
close_prog: close_prog:
error_cnt++; error_cnt++;
close_prog_noerr: close_prog_noerr:
if (!IS_ERR_OR_NULL(link))
bpf_link__destroy(link);
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