blob: d26ec3aa215e215538b26e23e1d43db73c36849c [file] [log] [blame]
John Fastabend832622e2017-07-17 09:27:28 -07001/* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
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#include <uapi/linux/bpf.h>
14#include <linux/in.h>
15#include <linux/if_ether.h>
16#include <linux/if_packet.h>
17#include <linux/if_vlan.h>
18#include <linux/ip.h>
19#include <linux/ipv6.h>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010020#include <bpf/bpf_helpers.h>
John Fastabend832622e2017-07-17 09:27:28 -070021
Daniel T. Lee451d1dc2019-11-07 09:51:53 +090022struct {
23 __uint(type, BPF_MAP_TYPE_ARRAY);
24 __type(key, int);
25 __type(value, int);
26 __uint(max_entries, 1);
27} tx_port SEC(".maps");
John Fastabend832622e2017-07-17 09:27:28 -070028
Jesper Dangaard Brouer306da4e2017-08-29 16:38:06 +020029/* Count RX packets, as XDP bpf_prog doesn't get direct TX-success
30 * feedback. Redirect TX errors can be caught via a tracepoint.
31 */
Daniel T. Lee451d1dc2019-11-07 09:51:53 +090032struct {
33 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
34 __type(key, u32);
35 __type(value, long);
36 __uint(max_entries, 1);
37} rxcnt SEC(".maps");
John Fastabend832622e2017-07-17 09:27:28 -070038
John Fastabend832622e2017-07-17 09:27:28 -070039static void swap_src_dst_mac(void *data)
40{
41 unsigned short *p = data;
42 unsigned short dst[3];
43
44 dst[0] = p[0];
45 dst[1] = p[1];
46 dst[2] = p[2];
47 p[0] = p[3];
48 p[1] = p[4];
49 p[2] = p[5];
50 p[3] = dst[0];
51 p[4] = dst[1];
52 p[5] = dst[2];
53}
54
55SEC("xdp_redirect")
56int xdp_redirect_prog(struct xdp_md *ctx)
57{
58 void *data_end = (void *)(long)ctx->data_end;
59 void *data = (void *)(long)ctx->data;
60 struct ethhdr *eth = data;
61 int rc = XDP_DROP;
62 int *ifindex, port = 0;
63 long *value;
64 u32 key = 0;
65 u64 nh_off;
66
67 nh_off = sizeof(*eth);
68 if (data + nh_off > data_end)
69 return rc;
70
71 ifindex = bpf_map_lookup_elem(&tx_port, &port);
72 if (!ifindex)
73 return rc;
74
75 value = bpf_map_lookup_elem(&rxcnt, &key);
76 if (value)
77 *value += 1;
78
79 swap_src_dst_mac(data);
80 return bpf_redirect(*ifindex, 0);
81}
82
Jesper Dangaard Brouer306da4e2017-08-29 16:38:06 +020083/* Redirect require an XDP bpf_prog loaded on the TX device */
84SEC("xdp_redirect_dummy")
Tariq Toukan3edcf182017-08-31 14:16:39 +030085int xdp_redirect_dummy_prog(struct xdp_md *ctx)
Jesper Dangaard Brouer306da4e2017-08-29 16:38:06 +020086{
87 return XDP_PASS;
88}
89
John Fastabend832622e2017-07-17 09:27:28 -070090char _license[] SEC("license") = "GPL";