1 // SPDX-License-Identifier: GPL-2.0
2 #define KBUILD_MODNAME "foo"
3 #include <uapi/linux/bpf.h>
5 #include <linux/if_ether.h>
7 #include <linux/ipv6.h>
8 #include <bpf/bpf_helpers.h>
11 __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
12 __uint(key_size, sizeof(int));
13 __uint(value_size, sizeof(int));
14 __uint(max_entries, 32);
15 } forward_map_general SEC(".maps");
18 __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
19 __uint(key_size, sizeof(int));
20 __uint(value_size, sizeof(struct bpf_devmap_val));
21 __uint(max_entries, 32);
22 } forward_map_native SEC(".maps");
25 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
28 __uint(max_entries, 1);
31 /* map to store egress interfaces mac addresses, set the
32 * max_entries to 1 and extend it in user sapce prog.
35 __uint(type, BPF_MAP_TYPE_ARRAY);
37 __type(value, __be64);
38 __uint(max_entries, 1);
39 } mac_map SEC(".maps");
41 static int xdp_redirect_map(struct xdp_md *ctx, void *forward_map)
46 /* count packet in global counter */
47 value = bpf_map_lookup_elem(&rxcnt, &key);
51 return bpf_redirect_map(forward_map, key,
52 BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
55 SEC("xdp_redirect_general")
56 int xdp_redirect_map_general(struct xdp_md *ctx)
58 return xdp_redirect_map(ctx, &forward_map_general);
61 SEC("xdp_redirect_native")
62 int xdp_redirect_map_native(struct xdp_md *ctx)
64 return xdp_redirect_map(ctx, &forward_map_native);
67 SEC("xdp_devmap/map_prog")
68 int xdp_devmap_prog(struct xdp_md *ctx)
70 void *data_end = (void *)(long)ctx->data_end;
71 void *data = (void *)(long)ctx->data;
72 u32 key = ctx->egress_ifindex;
73 struct ethhdr *eth = data;
77 nh_off = sizeof(*eth);
78 if (data + nh_off > data_end)
81 mac = bpf_map_lookup_elem(&mac_map, &key);
83 __builtin_memcpy(eth->h_source, mac, ETH_ALEN);
88 char _license[] SEC("license") = "GPL";