Back to home page

OSCL-LXR

 
 

    


0001 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
0002  *
0003  * This program is free software; you can redistribute it and/or
0004  * modify it under the terms of version 2 of the GNU General Public
0005  * License as published by the Free Software Foundation.
0006  *
0007  * This program is distributed in the hope that it will be useful, but
0008  * WITHOUT ANY WARRANTY; without even the implied warranty of
0009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0010  * General Public License for more details.
0011  */
0012 #define KBUILD_MODNAME "foo"
0013 
0014 #include "vmlinux.h"
0015 #include "xdp_sample.bpf.h"
0016 #include "xdp_sample_shared.h"
0017 
0018 /* The 2nd xdp prog on egress does not support skb mode, so we define two
0019  * maps, tx_port_general and tx_port_native.
0020  */
0021 struct {
0022     __uint(type, BPF_MAP_TYPE_DEVMAP);
0023     __uint(key_size, sizeof(int));
0024     __uint(value_size, sizeof(int));
0025     __uint(max_entries, 1);
0026 } tx_port_general SEC(".maps");
0027 
0028 struct {
0029     __uint(type, BPF_MAP_TYPE_DEVMAP);
0030     __uint(key_size, sizeof(int));
0031     __uint(value_size, sizeof(struct bpf_devmap_val));
0032     __uint(max_entries, 1);
0033 } tx_port_native SEC(".maps");
0034 
0035 /* store egress interface mac address */
0036 const volatile __u8 tx_mac_addr[ETH_ALEN];
0037 
0038 static __always_inline int xdp_redirect_map(struct xdp_md *ctx, void *redirect_map)
0039 {
0040     void *data_end = (void *)(long)ctx->data_end;
0041     void *data = (void *)(long)ctx->data;
0042     u32 key = bpf_get_smp_processor_id();
0043     struct ethhdr *eth = data;
0044     struct datarec *rec;
0045     u64 nh_off;
0046 
0047     nh_off = sizeof(*eth);
0048     if (data + nh_off > data_end)
0049         return XDP_DROP;
0050 
0051     rec = bpf_map_lookup_elem(&rx_cnt, &key);
0052     if (!rec)
0053         return XDP_PASS;
0054     NO_TEAR_INC(rec->processed);
0055     swap_src_dst_mac(data);
0056     return bpf_redirect_map(redirect_map, 0, 0);
0057 }
0058 
0059 SEC("xdp")
0060 int xdp_redirect_map_general(struct xdp_md *ctx)
0061 {
0062     return xdp_redirect_map(ctx, &tx_port_general);
0063 }
0064 
0065 SEC("xdp")
0066 int xdp_redirect_map_native(struct xdp_md *ctx)
0067 {
0068     return xdp_redirect_map(ctx, &tx_port_native);
0069 }
0070 
0071 SEC("xdp/devmap")
0072 int xdp_redirect_map_egress(struct xdp_md *ctx)
0073 {
0074     void *data_end = (void *)(long)ctx->data_end;
0075     void *data = (void *)(long)ctx->data;
0076     u8 *mac_addr = (u8 *) tx_mac_addr;
0077     struct ethhdr *eth = data;
0078     u64 nh_off;
0079 
0080     nh_off = sizeof(*eth);
0081     if (data + nh_off > data_end)
0082         return XDP_DROP;
0083 
0084     barrier_var(mac_addr); /* prevent optimizing out memcpy */
0085     __builtin_memcpy(eth->h_source, mac_addr, ETH_ALEN);
0086 
0087     return XDP_PASS;
0088 }
0089 
0090 /* Redirect require an XDP bpf_prog loaded on the TX device */
0091 SEC("xdp")
0092 int xdp_redirect_dummy_prog(struct xdp_md *ctx)
0093 {
0094     return XDP_PASS;
0095 }
0096 
0097 char _license[] SEC("license") = "GPL";