Skip to content

Commit 7153a21

Browse files
tohojokernel-patches-bot
authored andcommitted
From: Jiri Olsa <jolsa@kernel.org>
Adding test that setup following program: SEC("classifier/test_pkt_md_access") int test_pkt_md_access(struct __sk_buff *skb) with its extension: SEC("freplace/test_pkt_md_access") int test_pkt_md_access_new(struct __sk_buff *skb) and tracing that extension with: SEC("fentry/test_pkt_md_access_new") int BPF_PROG(fentry, struct sk_buff *skb) The test verifies that the tracing program can dereference skb argument properly. Signed-off-by: Jiri Olsa <jolsa@kernel.org> --- tools/testing/selftests/bpf/prog_tests/trace_ext.c | 93 ++++++++++++++++++++ tools/testing/selftests/bpf/progs/test_trace_ext.c | 18 ++++ .../selftests/bpf/progs/test_trace_ext_tracing.c | 25 +++++ 3 files changed, 136 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/trace_ext.c create mode 100644 tools/testing/selftests/bpf/progs/test_trace_ext.c create mode 100644 tools/testing/selftests/bpf/progs/test_trace_ext_tracing.c
1 parent 0d82c39 commit 7153a21

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#define _GNU_SOURCE
4+
#include <test_progs.h>
5+
#include <network_helpers.h>
6+
#include <sys/stat.h>
7+
#include <linux/sched.h>
8+
#include <sys/syscall.h>
9+
10+
#include "test_trace_ext.skel.h"
11+
#include "test_trace_ext_tracing.skel.h"
12+
13+
static __u32 duration;
14+
15+
void test_trace_ext(void)
16+
{
17+
struct test_trace_ext_tracing *skel_trace = NULL;
18+
struct test_trace_ext_tracing__bss *bss_trace;
19+
const char *file = "./test_pkt_md_access.o";
20+
struct test_trace_ext *skel_ext = NULL;
21+
struct test_trace_ext__bss *bss_ext;
22+
int err, prog_fd, ext_fd;
23+
struct bpf_object *obj;
24+
char buf[100];
25+
__u32 retval;
26+
__u64 len;
27+
28+
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
29+
if (CHECK_FAIL(err))
30+
return;
31+
32+
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
33+
.attach_prog_fd = prog_fd,
34+
);
35+
36+
skel_ext = test_trace_ext__open_opts(&opts);
37+
if (CHECK(!skel_ext, "setup", "freplace/test_pkt_md_access open failed\n"))
38+
goto cleanup;
39+
40+
err = test_trace_ext__load(skel_ext);
41+
if (CHECK(err, "setup", "freplace/test_pkt_md_access load failed\n")) {
42+
libbpf_strerror(err, buf, sizeof(buf));
43+
fprintf(stderr, "%s\n", buf);
44+
goto cleanup;
45+
}
46+
47+
err = test_trace_ext__attach(skel_ext);
48+
if (CHECK(err, "setup", "freplace/test_pkt_md_access attach failed: %d\n", err))
49+
goto cleanup;
50+
51+
ext_fd = bpf_program__fd(skel_ext->progs.test_pkt_md_access_new);
52+
53+
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts_trace,
54+
.attach_prog_fd = ext_fd,
55+
);
56+
57+
skel_trace = test_trace_ext_tracing__open_opts(&opts_trace);
58+
if (CHECK(!skel_trace, "setup", "tracing/test_pkt_md_access_new open failed\n"))
59+
goto cleanup;
60+
61+
err = test_trace_ext_tracing__load(skel_trace);
62+
if (CHECK(err, "setup", "tracing/test_pkt_md_access_new load failed\n")) {
63+
libbpf_strerror(err, buf, sizeof(buf));
64+
fprintf(stderr, "%s\n", buf);
65+
goto cleanup;
66+
}
67+
68+
err = test_trace_ext_tracing__attach(skel_trace);
69+
if (CHECK(err, "setup", "tracing/test_pkt_md_access_new attach failed: %d\n", err))
70+
goto cleanup;
71+
72+
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
73+
NULL, NULL, &retval, &duration);
74+
CHECK(err || retval, "",
75+
"err %d errno %d retval %d duration %d\n",
76+
err, errno, retval, duration);
77+
78+
bss_ext = skel_ext->bss;
79+
bss_trace = skel_trace->bss;
80+
81+
len = bss_ext->ext_called;
82+
83+
CHECK(bss_ext->ext_called == 0,
84+
"check", "failed to trigger freplace/test_pkt_md_access\n");
85+
CHECK(bss_trace->fentry_called != len,
86+
"check", "failed to trigger fentry/test_pkt_md_access_new\n");
87+
CHECK(bss_trace->fexit_called != len,
88+
"check", "failed to trigger fexit/test_pkt_md_access_new\n");
89+
90+
cleanup:
91+
test_trace_ext__destroy(skel_ext);
92+
bpf_object__close(obj);
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (c) 2019 Facebook
3+
#include <linux/bpf.h>
4+
#include <stdbool.h>
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_endian.h>
7+
#include <bpf/bpf_tracing.h>
8+
9+
volatile __u64 ext_called = 0;
10+
11+
SEC("freplace/test_pkt_md_access")
12+
int test_pkt_md_access_new(struct __sk_buff *skb)
13+
{
14+
ext_called = skb->len;
15+
return 0;
16+
}
17+
18+
char _license[] SEC("license") = "GPL";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
7+
volatile __u64 fentry_called = 0;
8+
9+
SEC("fentry/test_pkt_md_access_new")
10+
int BPF_PROG(fentry, struct sk_buff *skb)
11+
{
12+
fentry_called = skb->len;
13+
return 0;
14+
}
15+
16+
volatile __u64 fexit_called = 0;
17+
18+
SEC("fexit/test_pkt_md_access_new")
19+
int BPF_PROG(fexit, struct sk_buff *skb)
20+
{
21+
fexit_called = skb->len;
22+
return 0;
23+
}
24+
25+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)