|
6 | 6 | * modify it under the terms of version 2 of the GNU General Public
|
7 | 7 | * License as published by the Free Software Foundation.
|
8 | 8 | */
|
9 |
| -#include <stddef.h> |
10 |
| -#include <string.h> |
11 |
| -#include <arpa/inet.h> |
12 |
| -#include <linux/bpf.h> |
13 |
| -#include <linux/if_ether.h> |
14 |
| -#include <linux/if_packet.h> |
15 |
| -#include <linux/if_tunnel.h> |
16 |
| -#include <linux/ip.h> |
17 |
| -#include <linux/ipv6.h> |
18 |
| -#include <linux/icmp.h> |
19 |
| -#include <linux/types.h> |
20 |
| -#include <linux/socket.h> |
21 |
| -#include <linux/pkt_cls.h> |
22 |
| -#include <linux/erspan.h> |
23 |
| -#include <linux/udp.h> |
| 9 | +#include "vmlinux.h" |
| 10 | +#include <bpf/bpf_core_read.h> |
24 | 11 | #include <bpf/bpf_helpers.h>
|
25 | 12 | #include <bpf/bpf_endian.h>
|
| 13 | +#include "bpf_kfuncs.h" |
| 14 | +#include "bpf_tracing_net.h" |
26 | 15 |
|
27 | 16 | #define log_err(__ret) bpf_printk("ERROR line:%d ret:%d\n", __LINE__, __ret)
|
28 | 17 |
|
29 |
| -#define VXLAN_UDP_PORT 4789 |
| 18 | +#define VXLAN_UDP_PORT 4789 |
| 19 | +#define ETH_P_IP 0x0800 |
| 20 | +#define PACKET_HOST 0 |
| 21 | +#define TUNNEL_CSUM bpf_htons(0x01) |
| 22 | +#define TUNNEL_KEY bpf_htons(0x04) |
30 | 23 |
|
31 | 24 | /* Only IPv4 address assigned to veth1.
|
32 | 25 | * 172.16.1.200
|
33 | 26 | */
|
34 | 27 | #define ASSIGNED_ADDR_VETH1 0xac1001c8
|
35 | 28 |
|
36 |
| -struct geneve_opt { |
37 |
| - __be16 opt_class; |
38 |
| - __u8 type; |
39 |
| - __u8 length:5; |
40 |
| - __u8 r3:1; |
41 |
| - __u8 r2:1; |
42 |
| - __u8 r1:1; |
43 |
| - __u8 opt_data[8]; /* hard-coded to 8 byte */ |
44 |
| -}; |
45 |
| - |
46 |
| -struct vxlanhdr { |
47 |
| - __be32 vx_flags; |
48 |
| - __be32 vx_vni; |
49 |
| -} __attribute__((packed)); |
50 |
| - |
51 |
| -struct vxlan_metadata { |
52 |
| - __u32 gbp; |
53 |
| -}; |
54 |
| - |
55 |
| -struct bpf_fou_encap { |
56 |
| - __be16 sport; |
57 |
| - __be16 dport; |
58 |
| -}; |
59 |
| - |
60 |
| -enum bpf_fou_encap_type { |
61 |
| - FOU_BPF_ENCAP_FOU, |
62 |
| - FOU_BPF_ENCAP_GUE, |
63 |
| -}; |
64 |
| - |
65 | 29 | int bpf_skb_set_fou_encap(struct __sk_buff *skb_ctx,
|
66 | 30 | struct bpf_fou_encap *encap, int type) __ksym;
|
67 | 31 | int bpf_skb_get_fou_encap(struct __sk_buff *skb_ctx,
|
@@ -205,9 +169,9 @@ int erspan_set_tunnel(struct __sk_buff *skb)
|
205 | 169 | __u8 hwid = 7;
|
206 | 170 |
|
207 | 171 | md.version = 2;
|
208 |
| - md.u.md2.dir = direction; |
209 |
| - md.u.md2.hwid = hwid & 0xf; |
210 |
| - md.u.md2.hwid_upper = (hwid >> 4) & 0x3; |
| 172 | + BPF_CORE_WRITE_BITFIELD(&md.u.md2, dir, direction); |
| 173 | + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid, (hwid & 0xf)); |
| 174 | + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid_upper, (hwid >> 4) & 0x3); |
211 | 175 | #endif
|
212 | 176 |
|
213 | 177 | ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
|
@@ -246,8 +210,9 @@ int erspan_get_tunnel(struct __sk_buff *skb)
|
246 | 210 | bpf_printk("\tindex %x\n", index);
|
247 | 211 | #else
|
248 | 212 | bpf_printk("\tdirection %d hwid %x timestamp %u\n",
|
249 |
| - md.u.md2.dir, |
250 |
| - (md.u.md2.hwid_upper << 4) + md.u.md2.hwid, |
| 213 | + BPF_CORE_READ_BITFIELD(&md.u.md2, dir), |
| 214 | + (BPF_CORE_READ_BITFIELD(&md.u.md2, hwid_upper) << 4) + |
| 215 | + BPF_CORE_READ_BITFIELD(&md.u.md2, hwid), |
251 | 216 | bpf_ntohl(md.u.md2.timestamp));
|
252 | 217 | #endif
|
253 | 218 |
|
@@ -284,9 +249,9 @@ int ip4ip6erspan_set_tunnel(struct __sk_buff *skb)
|
284 | 249 | __u8 hwid = 17;
|
285 | 250 |
|
286 | 251 | md.version = 2;
|
287 |
| - md.u.md2.dir = direction; |
288 |
| - md.u.md2.hwid = hwid & 0xf; |
289 |
| - md.u.md2.hwid_upper = (hwid >> 4) & 0x3; |
| 252 | + BPF_CORE_WRITE_BITFIELD(&md.u.md2, dir, direction); |
| 253 | + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid, (hwid & 0xf)); |
| 254 | + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid_upper, (hwid >> 4) & 0x3); |
290 | 255 | #endif
|
291 | 256 |
|
292 | 257 | ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
|
@@ -326,8 +291,9 @@ int ip4ip6erspan_get_tunnel(struct __sk_buff *skb)
|
326 | 291 | bpf_printk("\tindex %x\n", index);
|
327 | 292 | #else
|
328 | 293 | bpf_printk("\tdirection %d hwid %x timestamp %u\n",
|
329 |
| - md.u.md2.dir, |
330 |
| - (md.u.md2.hwid_upper << 4) + md.u.md2.hwid, |
| 294 | + BPF_CORE_READ_BITFIELD(&md.u.md2, dir), |
| 295 | + (BPF_CORE_READ_BITFIELD(&md.u.md2, hwid_upper) << 4) + |
| 296 | + BPF_CORE_READ_BITFIELD(&md.u.md2, hwid), |
331 | 297 | bpf_ntohl(md.u.md2.timestamp));
|
332 | 298 | #endif
|
333 | 299 |
|
|
0 commit comments