This source file includes following definitions.
- swap_src_dst_mac
- SEC
- SEC
1
2
3
4
5
6
7
8
9
10
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>
20 #include "bpf_helpers.h"
21
22 struct {
23 __uint(type, BPF_MAP_TYPE_DEVMAP);
24 __uint(key_size, sizeof(int));
25 __uint(value_size, sizeof(int));
26 __uint(max_entries, 100);
27 } tx_port SEC(".maps");
28
29
30
31
32 struct {
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");
38
39 static 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
55 SEC("xdp_redirect_map")
56 int xdp_redirect_map_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 vport, port = 0, m = 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
72 vport = 0;
73
74
75 value = bpf_map_lookup_elem(&rxcnt, &key);
76 if (value)
77 *value += 1;
78
79 swap_src_dst_mac(data);
80
81
82 return bpf_redirect_map(&tx_port, vport, 0);
83 }
84
85
86 SEC("xdp_redirect_dummy")
87 int xdp_redirect_dummy_prog(struct xdp_md *ctx)
88 {
89 return XDP_PASS;
90 }
91
92 char _license[] SEC("license") = "GPL";