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"
15 #include "xdp_sample.bpf.h"
16 #include "xdp_sample_shared.h"
18 /* The 2nd xdp prog on egress does not support skb mode, so we define two
19 * maps, tx_port_general and tx_port_native.
22 __uint(type, BPF_MAP_TYPE_DEVMAP);
23 __uint(key_size, sizeof(int));
24 __uint(value_size, sizeof(int));
25 __uint(max_entries, 1);
26 } tx_port_general SEC(".maps");
29 __uint(type, BPF_MAP_TYPE_DEVMAP);
30 __uint(key_size, sizeof(int));
31 __uint(value_size, sizeof(struct bpf_devmap_val));
32 __uint(max_entries, 1);
33 } tx_port_native SEC(".maps");
35 /* store egress interface mac address */
36 const volatile char tx_mac_addr[ETH_ALEN];
38 static __always_inline int xdp_redirect_map(struct xdp_md *ctx, void *redirect_map)
40 void *data_end = (void *)(long)ctx->data_end;
41 void *data = (void *)(long)ctx->data;
42 u32 key = bpf_get_smp_processor_id();
43 struct ethhdr *eth = data;
47 nh_off = sizeof(*eth);
48 if (data + nh_off > data_end)
51 rec = bpf_map_lookup_elem(&rx_cnt, &key);
54 NO_TEAR_INC(rec->processed);
55 swap_src_dst_mac(data);
56 return bpf_redirect_map(redirect_map, 0, 0);
60 int xdp_redirect_map_general(struct xdp_md *ctx)
62 return xdp_redirect_map(ctx, &tx_port_general);
66 int xdp_redirect_map_native(struct xdp_md *ctx)
68 return xdp_redirect_map(ctx, &tx_port_native);
71 SEC("xdp_devmap/egress")
72 int xdp_redirect_map_egress(struct xdp_md *ctx)
74 void *data_end = (void *)(long)ctx->data_end;
75 void *data = (void *)(long)ctx->data;
76 struct ethhdr *eth = data;
79 nh_off = sizeof(*eth);
80 if (data + nh_off > data_end)
83 __builtin_memcpy(eth->h_source, (const char *)tx_mac_addr, ETH_ALEN);
88 /* Redirect require an XDP bpf_prog loaded on the TX device */
90 int xdp_redirect_dummy_prog(struct xdp_md *ctx)
95 char _license[] SEC("license") = "GPL";