Skip to content

Commit 02b4e12

Browse files
danobiAlexei Starovoitov
authored andcommitted
bpf: selftests: test_tunnel: Use vmlinux.h declarations
vmlinux.h declarations are more ergnomic, especially when working with kfuncs. The uapi headers are often incomplete for kfunc definitions. This commit also switches bitfield accesses to use CO-RE helpers. Switching to vmlinux.h definitions makes the verifier very unhappy with raw bitfield accesses. The error is: ; md.u.md2.dir = direction; 33: (69) r1 = *(u16 *)(r2 +11) misaligned stack access off (0x0; 0x0)+-64+11 size 2 Fix by using CO-RE-aware bitfield reads and writes. Co-developed-by: Antony Antony <[email protected]> Signed-off-by: Antony Antony <[email protected]> Signed-off-by: Daniel Xu <[email protected]> Link: https://lore.kernel.org/r/884bde1d9a351d126a3923886b945ea6b1b0776b.1702593901.git.dxu@dxuuu.xyz Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 77a7a82 commit 02b4e12

File tree

2 files changed

+22
-55
lines changed

2 files changed

+22
-55
lines changed

tools/testing/selftests/bpf/progs/bpf_tracing_net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define IPV6_AUTOFLOWLABEL 70
2727

2828
#define TC_ACT_UNSPEC (-1)
29+
#define TC_ACT_OK 0
2930
#define TC_ACT_SHOT 2
3031

3132
#define SOL_TCP 6

tools/testing/selftests/bpf/progs/test_tunnel_kern.c

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,26 @@
66
* modify it under the terms of version 2 of the GNU General Public
77
* License as published by the Free Software Foundation.
88
*/
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>
2411
#include <bpf/bpf_helpers.h>
2512
#include <bpf/bpf_endian.h>
13+
#include "bpf_kfuncs.h"
14+
#include "bpf_tracing_net.h"
2615

2716
#define log_err(__ret) bpf_printk("ERROR line:%d ret:%d\n", __LINE__, __ret)
2817

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)
3023

3124
/* Only IPv4 address assigned to veth1.
3225
* 172.16.1.200
3326
*/
3427
#define ASSIGNED_ADDR_VETH1 0xac1001c8
3528

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-
6529
int bpf_skb_set_fou_encap(struct __sk_buff *skb_ctx,
6630
struct bpf_fou_encap *encap, int type) __ksym;
6731
int bpf_skb_get_fou_encap(struct __sk_buff *skb_ctx,
@@ -205,9 +169,9 @@ int erspan_set_tunnel(struct __sk_buff *skb)
205169
__u8 hwid = 7;
206170

207171
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);
211175
#endif
212176

213177
ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
@@ -246,8 +210,9 @@ int erspan_get_tunnel(struct __sk_buff *skb)
246210
bpf_printk("\tindex %x\n", index);
247211
#else
248212
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),
251216
bpf_ntohl(md.u.md2.timestamp));
252217
#endif
253218

@@ -284,9 +249,9 @@ int ip4ip6erspan_set_tunnel(struct __sk_buff *skb)
284249
__u8 hwid = 17;
285250

286251
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);
290255
#endif
291256

292257
ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
@@ -326,8 +291,9 @@ int ip4ip6erspan_get_tunnel(struct __sk_buff *skb)
326291
bpf_printk("\tindex %x\n", index);
327292
#else
328293
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),
331297
bpf_ntohl(md.u.md2.timestamp));
332298
#endif
333299

0 commit comments

Comments
 (0)