1 // SPDX-License-Identifier: GPL-2.0
2 #define KBUILD_MODNAME "foo"
5 #include <linux/if_ether.h>
6 #include <linux/if_packet.h>
8 #include <linux/ipv6.h>
10 #include <linux/bpf.h>
11 #include <bpf/bpf_helpers.h>
12 #include <bpf/bpf_endian.h>
14 /* One map use devmap, another one use devmap_hash for testing */
16 __uint(type, BPF_MAP_TYPE_DEVMAP);
17 __uint(key_size, sizeof(int));
18 __uint(value_size, sizeof(int));
19 __uint(max_entries, 1024);
20 } map_all SEC(".maps");
23 __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
24 __uint(key_size, sizeof(int));
25 __uint(value_size, sizeof(struct bpf_devmap_val));
26 __uint(max_entries, 128);
27 } map_egress SEC(".maps");
29 /* map to store egress interfaces mac addresses */
31 __uint(type, BPF_MAP_TYPE_HASH);
33 __type(value, __be64);
34 __uint(max_entries, 128);
35 } mac_map SEC(".maps");
38 int xdp_redirect_map_multi_prog(struct xdp_md *ctx)
40 void *data_end = (void *)(long)ctx->data_end;
41 void *data = (void *)(long)ctx->data;
42 int if_index = ctx->ingress_ifindex;
43 struct ethhdr *eth = data;
47 nh_off = sizeof(*eth);
48 if (data + nh_off > data_end)
51 h_proto = eth->h_proto;
53 /* Using IPv4 for (BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS) testing */
54 if (h_proto == bpf_htons(ETH_P_IP))
55 return bpf_redirect_map(&map_all, 0,
56 BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
57 /* Using IPv6 for none flag testing */
58 else if (h_proto == bpf_htons(ETH_P_IPV6))
59 return bpf_redirect_map(&map_all, if_index, 0);
60 /* All others for BPF_F_BROADCAST testing */
62 return bpf_redirect_map(&map_all, 0, BPF_F_BROADCAST);
65 /* The following 2 progs are for 2nd devmap prog testing */
67 int xdp_redirect_map_all_prog(struct xdp_md *ctx)
69 return bpf_redirect_map(&map_egress, 0,
70 BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
74 int xdp_devmap_prog(struct xdp_md *ctx)
76 void *data_end = (void *)(long)ctx->data_end;
77 void *data = (void *)(long)ctx->data;
78 __u32 key = ctx->egress_ifindex;
79 struct ethhdr *eth = data;
83 nh_off = sizeof(*eth);
84 if (data + nh_off > data_end)
87 mac = bpf_map_lookup_elem(&mac_map, &key);
89 __builtin_memcpy(eth->h_source, mac, ETH_ALEN);
94 char _license[] SEC("license") = "GPL";