1 // SPDX-License-Identifier: GPL-2.0
2 #define KBUILD_MODNAME "foo"
5 #include "xdp_sample.bpf.h"
6 #include "xdp_sample_shared.h"
9 BPF_F_BROADCAST = (1ULL << 3),
10 BPF_F_EXCLUDE_INGRESS = (1ULL << 4),
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");
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");
27 /* map to store egress interfaces mac addresses */
29 __uint(type, BPF_MAP_TYPE_HASH);
31 __type(value, __be64);
32 __uint(max_entries, 32);
33 } mac_map SEC(".maps");
35 static int xdp_redirect_map(struct xdp_md *ctx, void *forward_map)
37 u32 key = bpf_get_smp_processor_id();
40 rec = bpf_map_lookup_elem(&rx_cnt, &key);
43 NO_TEAR_INC(rec->processed);
45 return bpf_redirect_map(forward_map, 0,
46 BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
50 int xdp_redirect_map_general(struct xdp_md *ctx)
52 return xdp_redirect_map(ctx, &forward_map_general);
56 int xdp_redirect_map_native(struct xdp_md *ctx)
58 return xdp_redirect_map(ctx, &forward_map_native);
61 SEC("xdp_devmap/egress")
62 int xdp_devmap_prog(struct xdp_md *ctx)
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;
71 nh_off = sizeof(*eth);
72 if (data + nh_off > data_end)
75 mac = bpf_map_lookup_elem(&mac_map, &key);
77 __builtin_memcpy(eth->h_source, mac, ETH_ALEN);
82 char _license[] SEC("license") = "GPL";