Skip to content

Commit ac80c2a

Browse files
williamtudavem330
authored andcommitted
samples/bpf: add erspan v2 sample code
Extend the existing tests for ipv4 ipv6 erspan version 2. Signed-off-by: William Tu <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 94d7d8f commit ac80c2a

File tree

2 files changed

+96
-19
lines changed

2 files changed

+96
-19
lines changed

samples/bpf/tcbpf2_kern.c

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,22 @@ struct geneve_opt {
3535
u8 opt_data[8]; /* hard-coded to 8 byte */
3636
};
3737

38+
struct erspan_md2 {
39+
__be32 timestamp;
40+
__be16 sgt;
41+
__be16 flags;
42+
};
43+
3844
struct vxlan_metadata {
3945
u32 gbp;
4046
};
4147

4248
struct erspan_metadata {
43-
__be32 index;
49+
union {
50+
__be32 index;
51+
struct erspan_md2 md2;
52+
} u;
53+
int version;
4454
};
4555

4656
SEC("gre_set_tunnel")
@@ -143,7 +153,18 @@ int _erspan_set_tunnel(struct __sk_buff *skb)
143153
return TC_ACT_SHOT;
144154
}
145155

146-
md.index = htonl(123);
156+
__builtin_memset(&md, 0, sizeof(md));
157+
#ifdef ERSPAN_V1
158+
md.version = 1;
159+
md.u.index = htonl(123);
160+
#else
161+
u8 direction = 1;
162+
u16 hwid = 7;
163+
164+
md.version = 2;
165+
md.u.md2.flags = htons((direction << 3) | (hwid << 4));
166+
#endif
167+
147168
ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
148169
if (ret < 0) {
149170
ERROR(ret);
@@ -156,7 +177,7 @@ int _erspan_set_tunnel(struct __sk_buff *skb)
156177
SEC("erspan_get_tunnel")
157178
int _erspan_get_tunnel(struct __sk_buff *skb)
158179
{
159-
char fmt[] = "key %d remote ip 0x%x erspan index 0x%x\n";
180+
char fmt[] = "key %d remote ip 0x%x erspan version %d\n";
160181
struct bpf_tunnel_key key;
161182
struct erspan_metadata md;
162183
u32 index;
@@ -174,9 +195,22 @@ int _erspan_get_tunnel(struct __sk_buff *skb)
174195
return TC_ACT_SHOT;
175196
}
176197

177-
index = bpf_ntohl(md.index);
178198
bpf_trace_printk(fmt, sizeof(fmt),
179-
key.tunnel_id, key.remote_ipv4, index);
199+
key.tunnel_id, key.remote_ipv4, md.version);
200+
201+
#ifdef ERSPAN_V1
202+
char fmt2[] = "\tindex %x\n";
203+
204+
index = bpf_ntohl(md.u.index);
205+
bpf_trace_printk(fmt2, sizeof(fmt2), index);
206+
#else
207+
char fmt2[] = "\tdirection %d hwid %x timestamp %u\n";
208+
209+
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));
213+
#endif
180214

181215
return TC_ACT_OK;
182216
}
@@ -201,7 +235,19 @@ int _ip4ip6erspan_set_tunnel(struct __sk_buff *skb)
201235
return TC_ACT_SHOT;
202236
}
203237

204-
md.index = htonl(123);
238+
__builtin_memset(&md, 0, sizeof(md));
239+
240+
#ifdef ERSPAN_V1
241+
md.u.index = htonl(123);
242+
md.version = 1;
243+
#else
244+
u8 direction = 0;
245+
u16 hwid = 17;
246+
247+
md.version = 2;
248+
md.u.md2.flags = htons((direction << 3) | (hwid << 4));
249+
#endif
250+
205251
ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
206252
if (ret < 0) {
207253
ERROR(ret);
@@ -214,7 +260,7 @@ int _ip4ip6erspan_set_tunnel(struct __sk_buff *skb)
214260
SEC("ip4ip6erspan_get_tunnel")
215261
int _ip4ip6erspan_get_tunnel(struct __sk_buff *skb)
216262
{
217-
char fmt[] = "key %d remote ip6 ::%x erspan index 0x%x\n";
263+
char fmt[] = "ip6erspan get key %d remote ip6 ::%x erspan version %d\n";
218264
struct bpf_tunnel_key key;
219265
struct erspan_metadata md;
220266
u32 index;
@@ -232,9 +278,22 @@ int _ip4ip6erspan_get_tunnel(struct __sk_buff *skb)
232278
return TC_ACT_SHOT;
233279
}
234280

235-
index = bpf_ntohl(md.index);
236281
bpf_trace_printk(fmt, sizeof(fmt),
237-
key.tunnel_id, key.remote_ipv6[0], index);
282+
key.tunnel_id, key.remote_ipv4, md.version);
283+
284+
#ifdef ERSPAN_V1
285+
char fmt2[] = "\tindex %x\n";
286+
287+
index = bpf_ntohl(md.u.index);
288+
bpf_trace_printk(fmt2, sizeof(fmt2), index);
289+
#else
290+
char fmt2[] = "\tdirection %d hwid %x timestamp %u\n";
291+
292+
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));
296+
#endif
238297

239298
return TC_ACT_OK;
240299
}

samples/bpf/test_tunnel_bpf.sh

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,17 @@ function add_ip6gretap_tunnel {
5959

6060
function add_erspan_tunnel {
6161
# in namespace
62-
ip netns exec at_ns0 \
63-
ip link add dev $DEV_NS type $TYPE seq key 2 local 172.16.1.100 remote 172.16.1.200 erspan 123
62+
if [ "$1" == "v1" ]; then
63+
ip netns exec at_ns0 \
64+
ip link add dev $DEV_NS type $TYPE seq key 2 \
65+
local 172.16.1.100 remote 172.16.1.200 \
66+
erspan_ver 1 erspan 123
67+
else
68+
ip netns exec at_ns0 \
69+
ip link add dev $DEV_NS type $TYPE seq key 2 \
70+
local 172.16.1.100 remote 172.16.1.200 \
71+
erspan_ver 2 erspan_dir 1 erspan_hwid 3
72+
fi
6473
ip netns exec at_ns0 ip link set dev $DEV_NS up
6574
ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
6675

@@ -79,10 +88,17 @@ function add_ip6erspan_tunnel {
7988
ip link set dev veth1 up
8089

8190
# in namespace
82-
ip netns exec at_ns0 \
83-
ip link add dev $DEV_NS type $TYPE seq key 2 erspan 123 \
84-
local ::11 remote ::22
85-
91+
if [ "$1" == "v1" ]; then
92+
ip netns exec at_ns0 \
93+
ip link add dev $DEV_NS type $TYPE seq key 2 \
94+
local ::11 remote ::22 \
95+
erspan_ver 1 erspan 123
96+
else
97+
ip netns exec at_ns0 \
98+
ip link add dev $DEV_NS type $TYPE seq key 2 \
99+
local ::11 remote ::22 \
100+
erspan_ver 2 erspan_dir 1 erspan_hwid 7
101+
fi
86102
ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
87103
ip netns exec at_ns0 ip link set dev $DEV_NS up
88104

@@ -199,7 +215,7 @@ function test_erspan {
199215
DEV_NS=erspan00
200216
DEV=erspan11
201217
config_device
202-
add_erspan_tunnel
218+
add_erspan_tunnel $1
203219
attach_bpf $DEV erspan_set_tunnel erspan_get_tunnel
204220
ping -c 1 10.1.1.100
205221
ip netns exec at_ns0 ping -c 1 10.1.1.200
@@ -211,7 +227,7 @@ function test_ip6erspan {
211227
DEV_NS=ip6erspan00
212228
DEV=ip6erspan11
213229
config_device
214-
add_ip6erspan_tunnel
230+
add_ip6erspan_tunnel $1
215231
attach_bpf $DEV ip4ip6erspan_set_tunnel ip4ip6erspan_get_tunnel
216232
ping6 -c 3 ::11
217233
ip netns exec at_ns0 ping -c 1 10.1.1.200
@@ -288,9 +304,11 @@ test_ip6gre
288304
echo "Testing IP6GRETAP tunnel..."
289305
test_ip6gretap
290306
echo "Testing ERSPAN tunnel..."
291-
test_erspan
307+
test_erspan v1
308+
test_erspan v2
292309
echo "Testing IP6ERSPAN tunnel..."
293-
test_ip6erspan
310+
test_ip6erspan v1
311+
test_ip6erspan v2
294312
echo "Testing VXLAN tunnel..."
295313
test_vxlan
296314
echo "Testing GENEVE tunnel..."

0 commit comments

Comments
 (0)