.. | .. |
---|
1 | 1 | // SPDX-License-Identifier: GPL-2.0 |
---|
2 | | -#define KBUILD_MODNAME "foo" |
---|
3 | | -#include <uapi/linux/bpf.h> |
---|
4 | | -#include "bpf_helpers.h" |
---|
5 | | - |
---|
| 2 | +#include <linux/bpf.h> |
---|
| 3 | +#include <bpf/bpf_helpers.h> |
---|
6 | 4 | #include "xdpsock.h" |
---|
7 | 5 | |
---|
8 | | -struct bpf_map_def SEC("maps") qidconf_map = { |
---|
9 | | - .type = BPF_MAP_TYPE_ARRAY, |
---|
10 | | - .key_size = sizeof(int), |
---|
11 | | - .value_size = sizeof(int), |
---|
12 | | - .max_entries = 1, |
---|
13 | | -}; |
---|
| 6 | +/* This XDP program is only needed for the XDP_SHARED_UMEM mode. |
---|
| 7 | + * If you do not use this mode, libbpf can supply an XDP program for you. |
---|
| 8 | + */ |
---|
14 | 9 | |
---|
15 | | -struct bpf_map_def SEC("maps") xsks_map = { |
---|
16 | | - .type = BPF_MAP_TYPE_XSKMAP, |
---|
17 | | - .key_size = sizeof(int), |
---|
18 | | - .value_size = sizeof(int), |
---|
19 | | - .max_entries = 4, |
---|
20 | | -}; |
---|
| 10 | +struct { |
---|
| 11 | + __uint(type, BPF_MAP_TYPE_XSKMAP); |
---|
| 12 | + __uint(max_entries, MAX_SOCKS); |
---|
| 13 | + __uint(key_size, sizeof(int)); |
---|
| 14 | + __uint(value_size, sizeof(int)); |
---|
| 15 | +} xsks_map SEC(".maps"); |
---|
21 | 16 | |
---|
22 | | -struct bpf_map_def SEC("maps") rr_map = { |
---|
23 | | - .type = BPF_MAP_TYPE_PERCPU_ARRAY, |
---|
24 | | - .key_size = sizeof(int), |
---|
25 | | - .value_size = sizeof(unsigned int), |
---|
26 | | - .max_entries = 1, |
---|
27 | | -}; |
---|
| 17 | +static unsigned int rr; |
---|
28 | 18 | |
---|
29 | | -SEC("xdp_sock") |
---|
30 | | -int xdp_sock_prog(struct xdp_md *ctx) |
---|
| 19 | +SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx) |
---|
31 | 20 | { |
---|
32 | | - int *qidconf, key = 0, idx; |
---|
33 | | - unsigned int *rr; |
---|
| 21 | + rr = (rr + 1) & (MAX_SOCKS - 1); |
---|
34 | 22 | |
---|
35 | | - qidconf = bpf_map_lookup_elem(&qidconf_map, &key); |
---|
36 | | - if (!qidconf) |
---|
37 | | - return XDP_ABORTED; |
---|
38 | | - |
---|
39 | | - if (*qidconf != ctx->rx_queue_index) |
---|
40 | | - return XDP_PASS; |
---|
41 | | - |
---|
42 | | -#if RR_LB /* NB! RR_LB is configured in xdpsock.h */ |
---|
43 | | - rr = bpf_map_lookup_elem(&rr_map, &key); |
---|
44 | | - if (!rr) |
---|
45 | | - return XDP_ABORTED; |
---|
46 | | - |
---|
47 | | - *rr = (*rr + 1) & (MAX_SOCKS - 1); |
---|
48 | | - idx = *rr; |
---|
49 | | -#else |
---|
50 | | - idx = 0; |
---|
51 | | -#endif |
---|
52 | | - |
---|
53 | | - return bpf_redirect_map(&xsks_map, idx, 0); |
---|
| 23 | + return bpf_redirect_map(&xsks_map, rr, XDP_DROP); |
---|
54 | 24 | } |
---|
55 | | - |
---|
56 | | -char _license[] SEC("license") = "GPL"; |
---|