]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - samples/bpf/xdp_redirect_map_multi.bpf.c
Merge tag 'docs-5.15-2' of git://git.lwn.net/linux
[mirror_ubuntu-jammy-kernel.git] / samples / bpf / xdp_redirect_map_multi.bpf.c
1 // SPDX-License-Identifier: GPL-2.0
2 #define KBUILD_MODNAME "foo"
3
4 #include "vmlinux.h"
5 #include "xdp_sample.bpf.h"
6 #include "xdp_sample_shared.h"
7
8 enum {
9 BPF_F_BROADCAST = (1ULL << 3),
10 BPF_F_EXCLUDE_INGRESS = (1ULL << 4),
11 };
12
13 struct {
14 __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
15 __uint(key_size, sizeof(int));
16 __uint(value_size, sizeof(int));
17 __uint(max_entries, 32);
18 } forward_map_general SEC(".maps");
19
20 struct {
21 __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
22 __uint(key_size, sizeof(int));
23 __uint(value_size, sizeof(struct bpf_devmap_val));
24 __uint(max_entries, 32);
25 } forward_map_native SEC(".maps");
26
27 /* map to store egress interfaces mac addresses */
28 struct {
29 __uint(type, BPF_MAP_TYPE_HASH);
30 __type(key, u32);
31 __type(value, __be64);
32 __uint(max_entries, 32);
33 } mac_map SEC(".maps");
34
35 static int xdp_redirect_map(struct xdp_md *ctx, void *forward_map)
36 {
37 u32 key = bpf_get_smp_processor_id();
38 struct datarec *rec;
39
40 rec = bpf_map_lookup_elem(&rx_cnt, &key);
41 if (!rec)
42 return XDP_PASS;
43 NO_TEAR_INC(rec->processed);
44
45 return bpf_redirect_map(forward_map, 0,
46 BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
47 }
48
49 SEC("xdp")
50 int xdp_redirect_map_general(struct xdp_md *ctx)
51 {
52 return xdp_redirect_map(ctx, &forward_map_general);
53 }
54
55 SEC("xdp")
56 int xdp_redirect_map_native(struct xdp_md *ctx)
57 {
58 return xdp_redirect_map(ctx, &forward_map_native);
59 }
60
61 SEC("xdp_devmap/egress")
62 int xdp_devmap_prog(struct xdp_md *ctx)
63 {
64 void *data_end = (void *)(long)ctx->data_end;
65 void *data = (void *)(long)ctx->data;
66 u32 key = ctx->egress_ifindex;
67 struct ethhdr *eth = data;
68 __be64 *mac;
69 u64 nh_off;
70
71 nh_off = sizeof(*eth);
72 if (data + nh_off > data_end)
73 return XDP_DROP;
74
75 mac = bpf_map_lookup_elem(&mac_map, &key);
76 if (mac)
77 __builtin_memcpy(eth->h_source, mac, ETH_ALEN);
78
79 return XDP_PASS;
80 }
81
82 char _license[] SEC("license") = "GPL";