1 /* SPDX-License-Identifier: GPL-2.0
2 * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
4 * Example howto extract XDP RX-queue info
6 #include <uapi/linux/bpf.h>
7 #include "bpf_helpers.h"
9 /* Config setup from with userspace
11 * User-side setup ifindex in config_map, to verify that
12 * ctx->ingress_ifindex is correct (against configured ifindex)
18 struct bpf_map_def
SEC("maps") config_map
= {
19 .type
= BPF_MAP_TYPE_ARRAY
,
20 .key_size
= sizeof(int),
21 .value_size
= sizeof(struct config
),
25 /* Common stats data record (shared with userspace) */
31 struct bpf_map_def
SEC("maps") stats_global_map
= {
32 .type
= BPF_MAP_TYPE_PERCPU_ARRAY
,
33 .key_size
= sizeof(u32
),
34 .value_size
= sizeof(struct datarec
),
40 /* Stats per rx_queue_index (per CPU) */
41 struct bpf_map_def
SEC("maps") rx_queue_index_map
= {
42 .type
= BPF_MAP_TYPE_PERCPU_ARRAY
,
43 .key_size
= sizeof(u32
),
44 .value_size
= sizeof(struct datarec
),
45 .max_entries
= MAX_RXQs
+ 1,
49 int xdp_prognum0(struct xdp_md
*ctx
)
51 void *data_end
= (void *)(long)ctx
->data_end
;
52 void *data
= (void *)(long)ctx
->data
;
53 struct datarec
*rec
, *rxq_rec
;
55 struct config
*config
;
58 /* Global stats record */
59 rec
= bpf_map_lookup_elem(&stats_global_map
, &key
);
64 /* Accessing ctx->ingress_ifindex, cause BPF to rewrite BPF
65 * instructions inside kernel to access xdp_rxq->dev->ifindex
67 ingress_ifindex
= ctx
->ingress_ifindex
;
69 config
= bpf_map_lookup_elem(&config_map
, &key
);
73 /* Simple test: check ctx provided ifindex is as expected */
74 if (ingress_ifindex
!= config
->ifindex
) {
75 /* count this error case */
80 /* Update stats per rx_queue_index. Handle if rx_queue_index
81 * is larger than stats map can contain info for.
83 key
= ctx
->rx_queue_index
;
86 rxq_rec
= bpf_map_lookup_elem(&rx_queue_index_map
, &key
);
93 return config
->action
;
96 char _license
[] SEC("license") = "GPL";