Linux Audio

Check our new training course

Embedded Linux training

Mar 31-Apr 8, 2025
Register
Loading...
Note: File does not exist in v6.13.7.
 1// SPDX-License-Identifier: GPL-2.0
 2#include <linux/ptrace.h>
 3#include <linux/version.h>
 4#include <uapi/linux/bpf.h>
 5#include "bpf_helpers.h"
 6
 7#define SAMPLE_SIZE 64ul
 8#define MAX_CPUS 128
 9
10struct bpf_map_def SEC("maps") my_map = {
11	.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
12	.key_size = sizeof(int),
13	.value_size = sizeof(u32),
14	.max_entries = MAX_CPUS,
15};
16
17SEC("xdp_sample")
18int xdp_sample_prog(struct xdp_md *ctx)
19{
20	void *data_end = (void *)(long)ctx->data_end;
21	void *data = (void *)(long)ctx->data;
22
23	/* Metadata will be in the perf event before the packet data. */
24	struct S {
25		u16 cookie;
26		u16 pkt_len;
27	} __packed metadata;
28
29	if (data < data_end) {
30		/* The XDP perf_event_output handler will use the upper 32 bits
31		 * of the flags argument as a number of bytes to include of the
32		 * packet payload in the event data. If the size is too big, the
33		 * call to bpf_perf_event_output will fail and return -EFAULT.
34		 *
35		 * See bpf_xdp_event_output in net/core/filter.c.
36		 *
37		 * The BPF_F_CURRENT_CPU flag means that the event output fd
38		 * will be indexed by the CPU number in the event map.
39		 */
40		u64 flags = BPF_F_CURRENT_CPU;
41		u16 sample_size;
42		int ret;
43
44		metadata.cookie = 0xdead;
45		metadata.pkt_len = (u16)(data_end - data);
46		sample_size = min(metadata.pkt_len, SAMPLE_SIZE);
47		flags |= (u64)sample_size << 32;
48
49		ret = bpf_perf_event_output(ctx, &my_map, flags,
50					    &metadata, sizeof(metadata));
51		if (ret)
52			bpf_printk("perf_event_output failed: %d\n", ret);
53	}
54
55	return XDP_PASS;
56}
57
58char _license[] SEC("license") = "GPL";
59u32 _version SEC("version") = LINUX_VERSION_CODE;