Merge tag 'xfs-5.20-merge-8' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-2.6-microblaze.git] / samples / bpf / xdp_redirect_map.bpf.c
1 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
2  *
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.
6  *
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.
11  */
12 #define KBUILD_MODNAME "foo"
13
14 #include "vmlinux.h"
15 #include "xdp_sample.bpf.h"
16 #include "xdp_sample_shared.h"
17
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.
20  */
21 struct {
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");
27
28 struct {
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");
34
35 /* store egress interface mac address */
36 const volatile __u8 tx_mac_addr[ETH_ALEN];
37
38 static __always_inline int xdp_redirect_map(struct xdp_md *ctx, void *redirect_map)
39 {
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;
44         struct datarec *rec;
45         u64 nh_off;
46
47         nh_off = sizeof(*eth);
48         if (data + nh_off > data_end)
49                 return XDP_DROP;
50
51         rec = bpf_map_lookup_elem(&rx_cnt, &key);
52         if (!rec)
53                 return XDP_PASS;
54         NO_TEAR_INC(rec->processed);
55         swap_src_dst_mac(data);
56         return bpf_redirect_map(redirect_map, 0, 0);
57 }
58
59 SEC("xdp")
60 int xdp_redirect_map_general(struct xdp_md *ctx)
61 {
62         return xdp_redirect_map(ctx, &tx_port_general);
63 }
64
65 SEC("xdp")
66 int xdp_redirect_map_native(struct xdp_md *ctx)
67 {
68         return xdp_redirect_map(ctx, &tx_port_native);
69 }
70
71 SEC("xdp/devmap")
72 int xdp_redirect_map_egress(struct xdp_md *ctx)
73 {
74         void *data_end = (void *)(long)ctx->data_end;
75         void *data = (void *)(long)ctx->data;
76         u8 *mac_addr = (u8 *) tx_mac_addr;
77         struct ethhdr *eth = data;
78         u64 nh_off;
79
80         nh_off = sizeof(*eth);
81         if (data + nh_off > data_end)
82                 return XDP_DROP;
83
84         barrier_var(mac_addr); /* prevent optimizing out memcpy */
85         __builtin_memcpy(eth->h_source, mac_addr, ETH_ALEN);
86
87         return XDP_PASS;
88 }
89
90 /* Redirect require an XDP bpf_prog loaded on the TX device */
91 SEC("xdp")
92 int xdp_redirect_dummy_prog(struct xdp_md *ctx)
93 {
94         return XDP_PASS;
95 }
96
97 char _license[] SEC("license") = "GPL";