1 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 #define KBUILD_MODNAME "foo"
13 #include <uapi/linux/bpf.h>
15 #include <linux/if_ether.h>
16 #include <linux/if_packet.h>
17 #include <linux/if_vlan.h>
19 #include <linux/ipv6.h>
20 #include <bpf/bpf_helpers.h>
22 /* The 2nd xdp prog on egress does not support skb mode, so we define two
23 * maps, tx_port_general and tx_port_native.
26 __uint(type, BPF_MAP_TYPE_DEVMAP);
27 __uint(key_size, sizeof(int));
28 __uint(value_size, sizeof(int));
29 __uint(max_entries, 100);
30 } tx_port_general SEC(".maps");
33 __uint(type, BPF_MAP_TYPE_DEVMAP);
34 __uint(key_size, sizeof(int));
35 __uint(value_size, sizeof(struct bpf_devmap_val));
36 __uint(max_entries, 100);
37 } tx_port_native SEC(".maps");
39 /* Count RX packets, as XDP bpf_prog doesn't get direct TX-success
40 * feedback. Redirect TX errors can be caught via a tracepoint.
43 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
46 __uint(max_entries, 1);
49 /* map to store egress interface mac address */
51 __uint(type, BPF_MAP_TYPE_ARRAY);
53 __type(value, __be64);
54 __uint(max_entries, 1);
55 } tx_mac SEC(".maps");
57 static void swap_src_dst_mac(void *data)
59 unsigned short *p = data;
60 unsigned short dst[3];
73 static __always_inline int xdp_redirect_map(struct xdp_md *ctx, void *redirect_map)
75 void *data_end = (void *)(long)ctx->data_end;
76 void *data = (void *)(long)ctx->data;
77 struct ethhdr *eth = data;
84 nh_off = sizeof(*eth);
85 if (data + nh_off > data_end)
88 /* constant virtual port */
91 /* count packet in global counter */
92 value = bpf_map_lookup_elem(&rxcnt, &key);
96 swap_src_dst_mac(data);
98 /* send packet out physical port */
99 return bpf_redirect_map(redirect_map, vport, 0);
102 SEC("xdp_redirect_general")
103 int xdp_redirect_map_general(struct xdp_md *ctx)
105 return xdp_redirect_map(ctx, &tx_port_general);
108 SEC("xdp_redirect_native")
109 int xdp_redirect_map_native(struct xdp_md *ctx)
111 return xdp_redirect_map(ctx, &tx_port_native);
114 SEC("xdp_devmap/map_prog")
115 int xdp_redirect_map_egress(struct xdp_md *ctx)
117 void *data_end = (void *)(long)ctx->data_end;
118 void *data = (void *)(long)ctx->data;
119 struct ethhdr *eth = data;
124 nh_off = sizeof(*eth);
125 if (data + nh_off > data_end)
128 mac = bpf_map_lookup_elem(&tx_mac, &key);
130 __builtin_memcpy(eth->h_source, mac, ETH_ALEN);
135 /* Redirect require an XDP bpf_prog loaded on the TX device */
136 SEC("xdp_redirect_dummy")
137 int xdp_redirect_dummy_prog(struct xdp_md *ctx)
142 char _license[] SEC("license") = "GPL";