Skip to content

Commit e41074d

Browse files
Alexei Starovoitovborkmann
authored andcommitted
selftest/bpf: Simple test for fentry/fexit
Add simple test for fentry and fexit programs around eth_type_trans. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrii Nakryiko <andriin@fb.com> Acked-by: Song Liu <songliubraving@fb.com> Link: https://lore.kernel.org/bpf/20191114185720.1641606-8-ast@kernel.org
1 parent b8c54ea commit e41074d

2 files changed

Lines changed: 88 additions & 3 deletions

File tree

‎tools/testing/selftests/bpf/prog_tests/kfree_skb.c‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ void test_kfree_skb(void)
6060
.file = "./kfree_skb.o",
6161
};
6262

63+
struct bpf_link *link = NULL, *link_fentry = NULL, *link_fexit = NULL;
64+
struct bpf_map *perf_buf_map, *global_data;
65+
struct bpf_program *prog, *fentry, *fexit;
6366
struct bpf_object *obj, *obj2 = NULL;
6467
struct perf_buffer_opts pb_opts = {};
6568
struct perf_buffer *pb = NULL;
66-
struct bpf_link *link = NULL;
67-
struct bpf_map *perf_buf_map;
68-
struct bpf_program *prog;
6969
int err, kfree_skb_fd;
7070
bool passed = false;
7171
__u32 duration = 0;
72+
const int zero = 0;
73+
bool test_ok[2];
7274

7375
err = bpf_prog_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS,
7476
&obj, &tattr.prog_fd);
@@ -82,9 +84,28 @@ void test_kfree_skb(void)
8284
prog = bpf_object__find_program_by_title(obj2, "tp_btf/kfree_skb");
8385
if (CHECK(!prog, "find_prog", "prog kfree_skb not found\n"))
8486
goto close_prog;
87+
fentry = bpf_object__find_program_by_title(obj2, "fentry/eth_type_trans");
88+
if (CHECK(!fentry, "find_prog", "prog eth_type_trans not found\n"))
89+
goto close_prog;
90+
fexit = bpf_object__find_program_by_title(obj2, "fexit/eth_type_trans");
91+
if (CHECK(!fexit, "find_prog", "prog eth_type_trans not found\n"))
92+
goto close_prog;
93+
94+
global_data = bpf_object__find_map_by_name(obj2, "kfree_sk.bss");
95+
if (CHECK(!global_data, "find global data", "not found\n"))
96+
goto close_prog;
97+
8598
link = bpf_program__attach_raw_tracepoint(prog, NULL);
8699
if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n", PTR_ERR(link)))
87100
goto close_prog;
101+
link_fentry = bpf_program__attach_trace(fentry);
102+
if (CHECK(IS_ERR(link_fentry), "attach fentry", "err %ld\n",
103+
PTR_ERR(link_fentry)))
104+
goto close_prog;
105+
link_fexit = bpf_program__attach_trace(fexit);
106+
if (CHECK(IS_ERR(link_fexit), "attach fexit", "err %ld\n",
107+
PTR_ERR(link_fexit)))
108+
goto close_prog;
88109

89110
perf_buf_map = bpf_object__find_map_by_name(obj2, "perf_buf_map");
90111
if (CHECK(!perf_buf_map, "find_perf_buf_map", "not found\n"))
@@ -108,14 +129,26 @@ void test_kfree_skb(void)
108129
err = perf_buffer__poll(pb, 100);
109130
if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
110131
goto close_prog;
132+
111133
/* make sure kfree_skb program was triggered
112134
* and it sent expected skb into ring buffer
113135
*/
114136
CHECK_FAIL(!passed);
137+
138+
err = bpf_map_lookup_elem(bpf_map__fd(global_data), &zero, test_ok);
139+
if (CHECK(err, "get_result",
140+
"failed to get output data: %d\n", err))
141+
goto close_prog;
142+
143+
CHECK_FAIL(!test_ok[0] || !test_ok[1]);
115144
close_prog:
116145
perf_buffer__free(pb);
117146
if (!IS_ERR_OR_NULL(link))
118147
bpf_link__destroy(link);
148+
if (!IS_ERR_OR_NULL(link_fentry))
149+
bpf_link__destroy(link_fentry);
150+
if (!IS_ERR_OR_NULL(link_fexit))
151+
bpf_link__destroy(link_fexit);
119152
bpf_object__close(obj);
120153
bpf_object__close(obj2);
121154
}

‎tools/testing/selftests/bpf/progs/kfree_skb.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22
// Copyright (c) 2019 Facebook
33
#include <linux/bpf.h>
4+
#include <stdbool.h>
45
#include "bpf_helpers.h"
56
#include "bpf_endian.h"
67

@@ -116,3 +117,54 @@ int trace_kfree_skb(struct trace_kfree_skb *ctx)
116117
&meta, sizeof(meta));
117118
return 0;
118119
}
120+
121+
static volatile struct {
122+
bool fentry_test_ok;
123+
bool fexit_test_ok;
124+
} result;
125+
126+
struct eth_type_trans_args {
127+
struct sk_buff *skb;
128+
struct net_device *dev;
129+
unsigned short protocol; /* return value available to fexit progs */
130+
};
131+
132+
SEC("fentry/eth_type_trans")
133+
int fentry_eth_type_trans(struct eth_type_trans_args *ctx)
134+
{
135+
struct sk_buff *skb = ctx->skb;
136+
struct net_device *dev = ctx->dev;
137+
int len, ifindex;
138+
139+
__builtin_preserve_access_index(({
140+
len = skb->len;
141+
ifindex = dev->ifindex;
142+
}));
143+
144+
/* fentry sees full packet including L2 header */
145+
if (len != 74 || ifindex != 1)
146+
return 0;
147+
result.fentry_test_ok = true;
148+
return 0;
149+
}
150+
151+
SEC("fexit/eth_type_trans")
152+
int fexit_eth_type_trans(struct eth_type_trans_args *ctx)
153+
{
154+
struct sk_buff *skb = ctx->skb;
155+
struct net_device *dev = ctx->dev;
156+
int len, ifindex;
157+
158+
__builtin_preserve_access_index(({
159+
len = skb->len;
160+
ifindex = dev->ifindex;
161+
}));
162+
163+
/* fexit sees packet without L2 header that eth_type_trans should have
164+
* consumed.
165+
*/
166+
if (len != 60 || ctx->protocol != bpf_htons(0x86dd) || ifindex != 1)
167+
return 0;
168+
result.fexit_test_ok = true;
169+
return 0;
170+
}

0 commit comments

Comments
 (0)