Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/bpf.h>
0003 #include <bpf/bpf_tracing.h>
0004 #include <bpf/bpf_helpers.h>
0005 
0006 char _license[] SEC("license") = "GPL";
0007 
0008 struct net_device {
0009     /* Structure does not need to contain all entries,
0010      * as "preserve_access_index" will use BTF to fix this...
0011      */
0012     int ifindex;
0013 } __attribute__((preserve_access_index));
0014 
0015 struct xdp_rxq_info {
0016     /* Structure does not need to contain all entries,
0017      * as "preserve_access_index" will use BTF to fix this...
0018      */
0019     struct net_device *dev;
0020     __u32 queue_index;
0021 } __attribute__((preserve_access_index));
0022 
0023 struct xdp_buff {
0024     void *data;
0025     void *data_end;
0026     void *data_meta;
0027     void *data_hard_start;
0028     unsigned long handle;
0029     struct xdp_rxq_info *rxq;
0030 } __attribute__((preserve_access_index));
0031 
0032 struct meta {
0033     int ifindex;
0034     int pkt_len;
0035 };
0036 
0037 struct {
0038     __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
0039     __type(key, int);
0040     __type(value, int);
0041 } perf_buf_map SEC(".maps");
0042 
0043 __u64 test_result_fentry = 0;
0044 SEC("fentry/FUNC")
0045 int BPF_PROG(trace_on_entry, struct xdp_buff *xdp)
0046 {
0047     struct meta meta;
0048     void *data_end = (void *)(long)xdp->data_end;
0049     void *data = (void *)(long)xdp->data;
0050 
0051     meta.ifindex = xdp->rxq->dev->ifindex;
0052     meta.pkt_len = bpf_xdp_get_buff_len((struct xdp_md *)xdp);
0053     bpf_xdp_output(xdp, &perf_buf_map,
0054                ((__u64) meta.pkt_len << 32) |
0055                BPF_F_CURRENT_CPU,
0056                &meta, sizeof(meta));
0057 
0058     test_result_fentry = xdp->rxq->dev->ifindex;
0059     return 0;
0060 }
0061 
0062 __u64 test_result_fexit = 0;
0063 SEC("fexit/FUNC")
0064 int BPF_PROG(trace_on_exit, struct xdp_buff *xdp, int ret)
0065 {
0066     test_result_fexit = ret;
0067     return 0;
0068 }