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 __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
10 __uint(key_size, sizeof(int));
11 __uint(value_size, sizeof(int));
12 __uint(max_entries, 32);
13 } forward_map_general SEC(".maps");
16 __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
17 __uint(key_size, sizeof(int));
18 __uint(value_size, sizeof(struct bpf_devmap_val));
19 __uint(max_entries, 32);
20 } forward_map_native SEC(".maps");
22 /* map to store egress interfaces mac addresses */
24 __uint(type, BPF_MAP_TYPE_HASH);
26 __type(value, __be64);
27 __uint(max_entries, 32);
28 } mac_map SEC(".maps");
30 static int xdp_redirect_map(struct xdp_md *ctx, void *forward_map)
32 u32 key = bpf_get_smp_processor_id();
35 rec = bpf_map_lookup_elem(&rx_cnt, &key);
38 NO_TEAR_INC(rec->processed);
40 return bpf_redirect_map(forward_map, 0,
41 BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
45 int xdp_redirect_map_general(struct xdp_md *ctx)
47 return xdp_redirect_map(ctx, &forward_map_general);
51 int xdp_redirect_map_native(struct xdp_md *ctx)
53 return xdp_redirect_map(ctx, &forward_map_native);
57 int xdp_devmap_prog(struct xdp_md *ctx)
59 void *data_end = (void *)(long)ctx->data_end;
60 void *data = (void *)(long)ctx->data;
61 u32 key = ctx->egress_ifindex;
62 struct ethhdr *eth = data;
66 nh_off = sizeof(*eth);
67 if (data + nh_off > data_end)
70 mac = bpf_map_lookup_elem(&mac_map, &key);
72 __builtin_memcpy(eth->h_source, mac, ETH_ALEN);
77 char _license[] SEC("license") = "GPL";