Skip to content

Commit 83e4b88

Browse files
chaudronAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Add a test for attaching a bpf fentry/fexit trace to an XDP program
Add a test that will attach a FENTRY and FEXIT program to the XDP test program. It will also verify data from the XDP context on FENTRY and verifies the return code on exit. Signed-off-by: Eelco Chaudron <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/157909410480.47481.11202505690938004673.stgit@xdp-tutorial
1 parent 9173cac commit 83e4b88

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include <net/if.h>
4+
#include "test_xdp.skel.h"
5+
#include "test_xdp_bpf2bpf.skel.h"
6+
7+
void test_xdp_bpf2bpf(void)
8+
{
9+
__u32 duration = 0, retval, size;
10+
char buf[128];
11+
int err, pkt_fd, map_fd;
12+
struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
13+
struct iptnl_info value4 = {.family = AF_INET};
14+
struct test_xdp *pkt_skel = NULL;
15+
struct test_xdp_bpf2bpf *ftrace_skel = NULL;
16+
struct vip key4 = {.protocol = 6, .family = AF_INET};
17+
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
18+
19+
/* Load XDP program to introspect */
20+
pkt_skel = test_xdp__open_and_load();
21+
if (CHECK(!pkt_skel, "pkt_skel_load", "test_xdp skeleton failed\n"))
22+
return;
23+
24+
pkt_fd = bpf_program__fd(pkt_skel->progs._xdp_tx_iptunnel);
25+
26+
map_fd = bpf_map__fd(pkt_skel->maps.vip2tnl);
27+
bpf_map_update_elem(map_fd, &key4, &value4, 0);
28+
29+
/* Load trace program */
30+
opts.attach_prog_fd = pkt_fd,
31+
ftrace_skel = test_xdp_bpf2bpf__open_opts(&opts);
32+
if (CHECK(!ftrace_skel, "__open", "ftrace skeleton failed\n"))
33+
goto out;
34+
35+
err = test_xdp_bpf2bpf__load(ftrace_skel);
36+
if (CHECK(err, "__load", "ftrace skeleton failed\n"))
37+
goto out;
38+
39+
err = test_xdp_bpf2bpf__attach(ftrace_skel);
40+
if (CHECK(err, "ftrace_attach", "ftrace attach failed: %d\n", err))
41+
goto out;
42+
43+
/* Run test program */
44+
err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4),
45+
buf, &size, &retval, &duration);
46+
47+
if (CHECK(err || retval != XDP_TX || size != 74 ||
48+
iph->protocol != IPPROTO_IPIP, "ipv4",
49+
"err %d errno %d retval %d size %d\n",
50+
err, errno, retval, size))
51+
goto out;
52+
53+
/* Verify test results */
54+
if (CHECK(ftrace_skel->bss->test_result_fentry != if_nametoindex("lo"),
55+
"result", "fentry failed err %llu\n",
56+
ftrace_skel->bss->test_result_fentry))
57+
goto out;
58+
59+
CHECK(ftrace_skel->bss->test_result_fexit != XDP_TX, "result",
60+
"fexit failed err %llu\n", ftrace_skel->bss->test_result_fexit);
61+
62+
out:
63+
test_xdp__destroy(pkt_skel);
64+
test_xdp_bpf2bpf__destroy(ftrace_skel);
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
#include "bpf_helpers.h"
4+
#include "bpf_trace_helpers.h"
5+
6+
struct net_device {
7+
/* Structure does not need to contain all entries,
8+
* as "preserve_access_index" will use BTF to fix this...
9+
*/
10+
int ifindex;
11+
} __attribute__((preserve_access_index));
12+
13+
struct xdp_rxq_info {
14+
/* Structure does not need to contain all entries,
15+
* as "preserve_access_index" will use BTF to fix this...
16+
*/
17+
struct net_device *dev;
18+
__u32 queue_index;
19+
} __attribute__((preserve_access_index));
20+
21+
struct xdp_buff {
22+
void *data;
23+
void *data_end;
24+
void *data_meta;
25+
void *data_hard_start;
26+
unsigned long handle;
27+
struct xdp_rxq_info *rxq;
28+
} __attribute__((preserve_access_index));
29+
30+
__u64 test_result_fentry = 0;
31+
SEC("fentry/_xdp_tx_iptunnel")
32+
int BPF_PROG(trace_on_entry, struct xdp_buff *xdp)
33+
{
34+
test_result_fentry = xdp->rxq->dev->ifindex;
35+
return 0;
36+
}
37+
38+
__u64 test_result_fexit = 0;
39+
SEC("fexit/_xdp_tx_iptunnel")
40+
int BPF_PROG(trace_on_exit, struct xdp_buff *xdp, int ret)
41+
{
42+
test_result_fexit = ret;
43+
return 0;
44+
}

0 commit comments

Comments
 (0)