bpf: Add bpf_xdp_output() helper
Introduce new helper that reuses existing xdp perf_event output implementation, but can be called from raw_tracepoint programs that receive 'struct xdp_buff *' as a tracepoint argument. Signed-off-by: Eelco Chaudron <echaudro@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Toke Høiland-Jørgensen <toke@redhat.com> Link: https://lore.kernel.org/bpf/158348514556.2239.11050972434793741444.stgit@xdp-tutorial
This commit is contained in:

committed by
Alexei Starovoitov

parent
4823b7210b
commit
d831ee84bf
@@ -4,17 +4,51 @@
|
||||
#include "test_xdp.skel.h"
|
||||
#include "test_xdp_bpf2bpf.skel.h"
|
||||
|
||||
struct meta {
|
||||
int ifindex;
|
||||
int pkt_len;
|
||||
};
|
||||
|
||||
static void on_sample(void *ctx, int cpu, void *data, __u32 size)
|
||||
{
|
||||
int duration = 0;
|
||||
struct meta *meta = (struct meta *)data;
|
||||
struct ipv4_packet *trace_pkt_v4 = data + sizeof(*meta);
|
||||
|
||||
if (CHECK(size < sizeof(pkt_v4) + sizeof(*meta),
|
||||
"check_size", "size %u < %zu\n",
|
||||
size, sizeof(pkt_v4) + sizeof(*meta)))
|
||||
return;
|
||||
|
||||
if (CHECK(meta->ifindex != if_nametoindex("lo"), "check_meta_ifindex",
|
||||
"meta->ifindex = %d\n", meta->ifindex))
|
||||
return;
|
||||
|
||||
if (CHECK(meta->pkt_len != sizeof(pkt_v4), "check_meta_pkt_len",
|
||||
"meta->pkt_len = %zd\n", sizeof(pkt_v4)))
|
||||
return;
|
||||
|
||||
if (CHECK(memcmp(trace_pkt_v4, &pkt_v4, sizeof(pkt_v4)),
|
||||
"check_packet_content", "content not the same\n"))
|
||||
return;
|
||||
|
||||
*(bool *)ctx = true;
|
||||
}
|
||||
|
||||
void test_xdp_bpf2bpf(void)
|
||||
{
|
||||
__u32 duration = 0, retval, size;
|
||||
char buf[128];
|
||||
int err, pkt_fd, map_fd;
|
||||
bool passed = false;
|
||||
struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
|
||||
struct iptnl_info value4 = {.family = AF_INET};
|
||||
struct test_xdp *pkt_skel = NULL;
|
||||
struct test_xdp_bpf2bpf *ftrace_skel = NULL;
|
||||
struct vip key4 = {.protocol = 6, .family = AF_INET};
|
||||
struct bpf_program *prog;
|
||||
struct perf_buffer *pb = NULL;
|
||||
struct perf_buffer_opts pb_opts = {};
|
||||
|
||||
/* Load XDP program to introspect */
|
||||
pkt_skel = test_xdp__open_and_load();
|
||||
@@ -50,6 +84,14 @@ void test_xdp_bpf2bpf(void)
|
||||
if (CHECK(err, "ftrace_attach", "ftrace attach failed: %d\n", err))
|
||||
goto out;
|
||||
|
||||
/* Set up perf buffer */
|
||||
pb_opts.sample_cb = on_sample;
|
||||
pb_opts.ctx = &passed;
|
||||
pb = perf_buffer__new(bpf_map__fd(ftrace_skel->maps.perf_buf_map),
|
||||
1, &pb_opts);
|
||||
if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb)))
|
||||
goto out;
|
||||
|
||||
/* Run test program */
|
||||
err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4),
|
||||
buf, &size, &retval, &duration);
|
||||
@@ -60,6 +102,15 @@ void test_xdp_bpf2bpf(void)
|
||||
err, errno, retval, size))
|
||||
goto out;
|
||||
|
||||
/* Make sure bpf_xdp_output() was triggered and it sent the expected
|
||||
* data to the perf ring buffer.
|
||||
*/
|
||||
err = perf_buffer__poll(pb, 100);
|
||||
if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
|
||||
goto out;
|
||||
|
||||
CHECK_FAIL(!passed);
|
||||
|
||||
/* Verify test results */
|
||||
if (CHECK(ftrace_skel->bss->test_result_fentry != if_nametoindex("lo"),
|
||||
"result", "fentry failed err %llu\n",
|
||||
@@ -70,6 +121,8 @@ void test_xdp_bpf2bpf(void)
|
||||
"fexit failed err %llu\n", ftrace_skel->bss->test_result_fexit);
|
||||
|
||||
out:
|
||||
if (pb)
|
||||
perf_buffer__free(pb);
|
||||
test_xdp__destroy(pkt_skel);
|
||||
test_xdp_bpf2bpf__destroy(ftrace_skel);
|
||||
}
|
||||
|
Reference in New Issue
Block a user