mirror of
https://github.com/BobTheBlinker/android_kernel_motorola_sm6375.git
synced 2026-10-28 01:23:00 -04:00
BACKPORT: 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. Change-Id: I418e7a0142b7f7e9f44febf5b1d6bdd5519ef5ca Signed-off-by: Eelco Chaudron <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Toke Høiland-Jørgensen <[email protected]> Link: https://lore.kernel.org/bpf/158348514556.2239.11050972434793741444.stgit@xdp-tutorial
This commit is contained in:
parent
fe3fbc1044
commit
cc217594c6
7 changed files with 144 additions and 2 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
#include "bpf_helpers.h"
|
||||
#include "bpf_trace_helpers.h"
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
|
||||
struct net_device {
|
||||
/* Structure does not need to contain all entries,
|
||||
* as "preserve_access_index" will use BTF to fix this...
|
||||
|
|
@ -27,10 +29,32 @@ struct xdp_buff {
|
|||
struct xdp_rxq_info *rxq;
|
||||
} __attribute__((preserve_access_index));
|
||||
|
||||
struct meta {
|
||||
int ifindex;
|
||||
int pkt_len;
|
||||
};
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
|
||||
__uint(key_size, sizeof(int));
|
||||
__uint(value_size, sizeof(int));
|
||||
} perf_buf_map SEC(".maps");
|
||||
|
||||
__u64 test_result_fentry = 0;
|
||||
SEC("fentry/FUNC")
|
||||
int BPF_PROG(trace_on_entry, struct xdp_buff *xdp)
|
||||
{
|
||||
struct meta meta;
|
||||
void *data_end = (void *)(long)xdp->data_end;
|
||||
void *data = (void *)(long)xdp->data;
|
||||
|
||||
meta.ifindex = xdp->rxq->dev->ifindex;
|
||||
meta.pkt_len = data_end - data;
|
||||
bpf_xdp_output(xdp, &perf_buf_map,
|
||||
((__u64) meta.pkt_len << 32) |
|
||||
BPF_F_CURRENT_CPU,
|
||||
&meta, sizeof(meta));
|
||||
|
||||
test_result_fentry = xdp->rxq->dev->ifindex;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue