Skip to content

Commit 9c33ca4

Browse files
williamtudavem330
authored andcommitted
sample/bpf: fix erspan metadata
The commit c69de58 ("net: erspan: use bitfield instead of mask and offset") changes the erspan header to use bitfield, and commit d350a82 ("net: erspan: create erspan metadata uapi header") creates a uapi header file. The above two commit breaks the current erspan test. This patch fixes it by adapting the above two changes. Fixes: ac80c2a ("samples/bpf: add erspan v2 sample code") Fixes: ef88f89 ("samples/bpf: extend test_tunnel_bpf.sh with ERSPAN") Signed-off-by: William Tu <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 39f57f6 commit 9c33ca4

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

samples/bpf/tcbpf2_kern.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <uapi/linux/tcp.h>
1616
#include <uapi/linux/filter.h>
1717
#include <uapi/linux/pkt_cls.h>
18+
#include <uapi/linux/erspan.h>
1819
#include <net/ipv6.h>
1920
#include "bpf_helpers.h"
2021
#include "bpf_endian.h"
@@ -35,24 +36,10 @@ struct geneve_opt {
3536
u8 opt_data[8]; /* hard-coded to 8 byte */
3637
};
3738

38-
struct erspan_md2 {
39-
__be32 timestamp;
40-
__be16 sgt;
41-
__be16 flags;
42-
};
43-
4439
struct vxlan_metadata {
4540
u32 gbp;
4641
};
4742

48-
struct erspan_metadata {
49-
union {
50-
__be32 index;
51-
struct erspan_md2 md2;
52-
} u;
53-
int version;
54-
};
55-
5643
SEC("gre_set_tunnel")
5744
int _gre_set_tunnel(struct __sk_buff *skb)
5845
{
@@ -156,13 +143,15 @@ int _erspan_set_tunnel(struct __sk_buff *skb)
156143
__builtin_memset(&md, 0, sizeof(md));
157144
#ifdef ERSPAN_V1
158145
md.version = 1;
159-
md.u.index = htonl(123);
146+
md.u.index = bpf_htonl(123);
160147
#else
161148
u8 direction = 1;
162-
u16 hwid = 7;
149+
u8 hwid = 7;
163150

164151
md.version = 2;
165-
md.u.md2.flags = htons((direction << 3) | (hwid << 4));
152+
md.u.md2.dir = direction;
153+
md.u.md2.hwid = hwid & 0xf;
154+
md.u.md2.hwid_upper = (hwid >> 4) & 0x3;
166155
#endif
167156

168157
ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
@@ -207,9 +196,9 @@ int _erspan_get_tunnel(struct __sk_buff *skb)
207196
char fmt2[] = "\tdirection %d hwid %x timestamp %u\n";
208197

209198
bpf_trace_printk(fmt2, sizeof(fmt2),
210-
(ntohs(md.u.md2.flags) >> 3) & 0x1,
211-
(ntohs(md.u.md2.flags) >> 4) & 0x3f,
212-
bpf_ntohl(md.u.md2.timestamp));
199+
md.u.md2.dir,
200+
(md.u.md2.hwid_upper << 4) + md.u.md2.hwid,
201+
bpf_ntohl(md.u.md2.timestamp));
213202
#endif
214203

215204
return TC_ACT_OK;
@@ -242,10 +231,12 @@ int _ip4ip6erspan_set_tunnel(struct __sk_buff *skb)
242231
md.version = 1;
243232
#else
244233
u8 direction = 0;
245-
u16 hwid = 17;
234+
u8 hwid = 17;
246235

247236
md.version = 2;
248-
md.u.md2.flags = htons((direction << 3) | (hwid << 4));
237+
md.u.md2.dir = direction;
238+
md.u.md2.hwid = hwid & 0xf;
239+
md.u.md2.hwid_upper = (hwid >> 4) & 0x3;
249240
#endif
250241

251242
ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
@@ -290,9 +281,9 @@ int _ip4ip6erspan_get_tunnel(struct __sk_buff *skb)
290281
char fmt2[] = "\tdirection %d hwid %x timestamp %u\n";
291282

292283
bpf_trace_printk(fmt2, sizeof(fmt2),
293-
(ntohs(md.u.md2.flags) >> 3) & 0x1,
294-
(ntohs(md.u.md2.flags) >> 4) & 0x3f,
295-
bpf_ntohl(md.u.md2.timestamp));
284+
md.u.md2.dir,
285+
(md.u.md2.hwid_upper << 4) + md.u.md2.hwid,
286+
bpf_ntohl(md.u.md2.timestamp));
296287
#endif
297288

298289
return TC_ACT_OK;

samples/bpf/test_tunnel_bpf.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function add_erspan_tunnel {
6868
ip netns exec at_ns0 \
6969
ip link add dev $DEV_NS type $TYPE seq key 2 \
7070
local 172.16.1.100 remote 172.16.1.200 \
71-
erspan_ver 2 erspan_dir 1 erspan_hwid 3
71+
erspan_ver 2 erspan_dir egress erspan_hwid 3
7272
fi
7373
ip netns exec at_ns0 ip link set dev $DEV_NS up
7474
ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
@@ -97,7 +97,7 @@ function add_ip6erspan_tunnel {
9797
ip netns exec at_ns0 \
9898
ip link add dev $DEV_NS type $TYPE seq key 2 \
9999
local ::11 remote ::22 \
100-
erspan_ver 2 erspan_dir 1 erspan_hwid 7
100+
erspan_ver 2 erspan_dir egress erspan_hwid 7
101101
fi
102102
ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
103103
ip netns exec at_ns0 ip link set dev $DEV_NS up

0 commit comments

Comments
 (0)