Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0
0002  * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
0003  *
0004  *  Example howto extract XDP RX-queue info
0005  */
0006 #include <uapi/linux/bpf.h>
0007 #include <uapi/linux/if_ether.h>
0008 #include <uapi/linux/in.h>
0009 #include <bpf/bpf_helpers.h>
0010 
0011 /* Config setup from with userspace
0012  *
0013  * User-side setup ifindex in config_map, to verify that
0014  * ctx->ingress_ifindex is correct (against configured ifindex)
0015  */
0016 struct config {
0017     __u32 action;
0018     int ifindex;
0019     __u32 options;
0020 };
0021 enum cfg_options_flags {
0022     NO_TOUCH = 0x0U,
0023     READ_MEM = 0x1U,
0024     SWAP_MAC = 0x2U,
0025 };
0026 
0027 struct {
0028     __uint(type, BPF_MAP_TYPE_ARRAY);
0029     __type(key, int);
0030     __type(value, struct config);
0031     __uint(max_entries, 1);
0032 } config_map SEC(".maps");
0033 
0034 /* Common stats data record (shared with userspace) */
0035 struct datarec {
0036     __u64 processed;
0037     __u64 issue;
0038 };
0039 
0040 struct {
0041     __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
0042     __type(key, u32);
0043     __type(value, struct datarec);
0044     __uint(max_entries, 1);
0045 } stats_global_map SEC(".maps");
0046 
0047 #define MAX_RXQs 64
0048 
0049 /* Stats per rx_queue_index (per CPU) */
0050 struct {
0051     __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
0052     __type(key, u32);
0053     __type(value, struct datarec);
0054     __uint(max_entries, MAX_RXQs + 1);
0055 } rx_queue_index_map SEC(".maps");
0056 
0057 static __always_inline
0058 void swap_src_dst_mac(void *data)
0059 {
0060     unsigned short *p = data;
0061     unsigned short dst[3];
0062 
0063     dst[0] = p[0];
0064     dst[1] = p[1];
0065     dst[2] = p[2];
0066     p[0] = p[3];
0067     p[1] = p[4];
0068     p[2] = p[5];
0069     p[3] = dst[0];
0070     p[4] = dst[1];
0071     p[5] = dst[2];
0072 }
0073 
0074 SEC("xdp_prog0")
0075 int  xdp_prognum0(struct xdp_md *ctx)
0076 {
0077     void *data_end = (void *)(long)ctx->data_end;
0078     void *data     = (void *)(long)ctx->data;
0079     struct datarec *rec, *rxq_rec;
0080     int ingress_ifindex;
0081     struct config *config;
0082     u32 key = 0;
0083 
0084     /* Global stats record */
0085     rec = bpf_map_lookup_elem(&stats_global_map, &key);
0086     if (!rec)
0087         return XDP_ABORTED;
0088     rec->processed++;
0089 
0090     /* Accessing ctx->ingress_ifindex, cause BPF to rewrite BPF
0091      * instructions inside kernel to access xdp_rxq->dev->ifindex
0092      */
0093     ingress_ifindex = ctx->ingress_ifindex;
0094 
0095     config = bpf_map_lookup_elem(&config_map, &key);
0096     if (!config)
0097         return XDP_ABORTED;
0098 
0099     /* Simple test: check ctx provided ifindex is as expected */
0100     if (ingress_ifindex != config->ifindex) {
0101         /* count this error case */
0102         rec->issue++;
0103         return XDP_ABORTED;
0104     }
0105 
0106     /* Update stats per rx_queue_index. Handle if rx_queue_index
0107      * is larger than stats map can contain info for.
0108      */
0109     key = ctx->rx_queue_index;
0110     if (key >= MAX_RXQs)
0111         key = MAX_RXQs;
0112     rxq_rec = bpf_map_lookup_elem(&rx_queue_index_map, &key);
0113     if (!rxq_rec)
0114         return XDP_ABORTED;
0115     rxq_rec->processed++;
0116     if (key == MAX_RXQs)
0117         rxq_rec->issue++;
0118 
0119     /* Default: Don't touch packet data, only count packets */
0120     if (unlikely(config->options & (READ_MEM|SWAP_MAC))) {
0121         struct ethhdr *eth = data;
0122 
0123         if (eth + 1 > data_end)
0124             return XDP_ABORTED;
0125 
0126         /* Avoid compiler removing this: Drop non 802.3 Ethertypes */
0127         if (ntohs(eth->h_proto) < ETH_P_802_3_MIN)
0128             return XDP_ABORTED;
0129 
0130         /* XDP_TX requires changing MAC-addrs, else HW may drop.
0131          * Can also be enabled with --swapmac (for test purposes)
0132          */
0133         if (unlikely(config->options & SWAP_MAC))
0134             swap_src_dst_mac(data);
0135     }
0136 
0137     return config->action;
0138 }
0139 
0140 char _license[] SEC("license") = "GPL";