1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bpf.h>
3 #include <bpf/bpf_tracing.h>
4 #include <bpf/bpf_helpers.h>
5 
6 char _license[] SEC("license") = "GPL";
7 
8 struct net_device {
9 	/* Structure does not need to contain all entries,
10 	 * as "preserve_access_index" will use BTF to fix this...
11 	 */
12 	int ifindex;
13 } __attribute__((preserve_access_index));
14 
15 struct xdp_rxq_info {
16 	/* Structure does not need to contain all entries,
17 	 * as "preserve_access_index" will use BTF to fix this...
18 	 */
19 	struct net_device *dev;
20 	__u32 queue_index;
21 } __attribute__((preserve_access_index));
22 
23 struct xdp_buff {
24 	void *data;
25 	void *data_end;
26 	void *data_meta;
27 	void *data_hard_start;
28 	unsigned long handle;
29 	struct xdp_rxq_info *rxq;
30 } __attribute__((preserve_access_index));
31 
32 struct meta {
33 	int ifindex;
34 	int pkt_len;
35 };
36 
37 struct {
38 	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
39 	__uint(key_size, sizeof(int));
40 	__uint(value_size, sizeof(int));
41 } perf_buf_map SEC(".maps");
42 
43 __u64 test_result_fentry = 0;
44 SEC("fentry/FUNC")
45 int BPF_PROG(trace_on_entry, struct xdp_buff *xdp)
46 {
47 	struct meta meta;
48 	void *data_end = (void *)(long)xdp->data_end;
49 	void *data = (void *)(long)xdp->data;
50 
51 	meta.ifindex = xdp->rxq->dev->ifindex;
52 	meta.pkt_len = data_end - data;
53 	bpf_xdp_output(xdp, &perf_buf_map,
54 		       ((__u64) meta.pkt_len << 32) |
55 		       BPF_F_CURRENT_CPU,
56 		       &meta, sizeof(meta));
57 
58 	test_result_fentry = xdp->rxq->dev->ifindex;
59 	return 0;
60 }
61 
62 __u64 test_result_fexit = 0;
63 SEC("fexit/FUNC")
64 int BPF_PROG(trace_on_exit, struct xdp_buff *xdp, int ret)
65 {
66 	test_result_fexit = ret;
67 	return 0;
68 }
69