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_helpers.h"
22 struct bpf_map_def
SEC("maps") tx_port
= {
23 .type
= BPF_MAP_TYPE_DEVMAP
,
24 .key_size
= sizeof(int),
25 .value_size
= sizeof(int),
29 /* Count RX packets, as XDP bpf_prog doesn't get direct TX-success
30 * feedback. Redirect TX errors can be caught via a tracepoint.
32 struct bpf_map_def
SEC("maps") rxcnt
= {
33 .type
= BPF_MAP_TYPE_PERCPU_ARRAY
,
34 .key_size
= sizeof(u32
),
35 .value_size
= sizeof(long),
39 static void swap_src_dst_mac(void *data
)
41 unsigned short *p
= data
;
42 unsigned short dst
[3];
55 SEC("xdp_redirect_map")
56 int xdp_redirect_map_prog(struct xdp_md
*ctx
)
58 void *data_end
= (void *)(long)ctx
->data_end
;
59 void *data
= (void *)(long)ctx
->data
;
60 struct ethhdr
*eth
= data
;
62 int vport
, port
= 0, m
= 0;
67 nh_off
= sizeof(*eth
);
68 if (data
+ nh_off
> data_end
)
71 /* constant virtual port */
74 /* count packet in global counter */
75 value
= bpf_map_lookup_elem(&rxcnt
, &key
);
79 swap_src_dst_mac(data
);
81 /* send packet out physical port */
82 return bpf_redirect_map(&tx_port
, vport
, 0);
85 /* Redirect require an XDP bpf_prog loaded on the TX device */
86 SEC("xdp_redirect_dummy")
87 int xdp_redirect_dummy_prog(struct xdp_md
*ctx
)
92 char _license
[] SEC("license") = "GPL";