Skip to content

Commit ef88f89

Browse files
williamtudavem330
authored andcommitted
samples/bpf: extend test_tunnel_bpf.sh with ERSPAN
Extend existing tests for vxlan, gre, geneve, ipip to include ERSPAN tunnel. Signed-off-by: William Tu <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1a66a83 commit ef88f89

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

samples/bpf/tcbpf2_kern.c

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <uapi/linux/pkt_cls.h>
1818
#include <net/ipv6.h>
1919
#include "bpf_helpers.h"
20+
#include "bpf_endian.h"
2021

2122
#define _htonl __builtin_bswap32
2223
#define ERROR(ret) do {\
@@ -38,6 +39,10 @@ struct vxlan_metadata {
3839
u32 gbp;
3940
};
4041

42+
struct erspan_metadata {
43+
__be32 index;
44+
};
45+
4146
SEC("gre_set_tunnel")
4247
int _gre_set_tunnel(struct __sk_buff *skb)
4348
{
@@ -76,6 +81,63 @@ int _gre_get_tunnel(struct __sk_buff *skb)
7681
return TC_ACT_OK;
7782
}
7883

84+
SEC("erspan_set_tunnel")
85+
int _erspan_set_tunnel(struct __sk_buff *skb)
86+
{
87+
struct bpf_tunnel_key key;
88+
struct erspan_metadata md;
89+
int ret;
90+
91+
__builtin_memset(&key, 0x0, sizeof(key));
92+
key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
93+
key.tunnel_id = 2;
94+
key.tunnel_tos = 0;
95+
key.tunnel_ttl = 64;
96+
97+
ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
98+
if (ret < 0) {
99+
ERROR(ret);
100+
return TC_ACT_SHOT;
101+
}
102+
103+
md.index = htonl(123);
104+
ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
105+
if (ret < 0) {
106+
ERROR(ret);
107+
return TC_ACT_SHOT;
108+
}
109+
110+
return TC_ACT_OK;
111+
}
112+
113+
SEC("erspan_get_tunnel")
114+
int _erspan_get_tunnel(struct __sk_buff *skb)
115+
{
116+
char fmt[] = "key %d remote ip 0x%x erspan index 0x%x\n";
117+
struct bpf_tunnel_key key;
118+
struct erspan_metadata md;
119+
u32 index;
120+
int ret;
121+
122+
ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
123+
if (ret < 0) {
124+
ERROR(ret);
125+
return TC_ACT_SHOT;
126+
}
127+
128+
ret = bpf_skb_get_tunnel_opt(skb, &md, sizeof(md));
129+
if (ret < 0) {
130+
ERROR(ret);
131+
return TC_ACT_SHOT;
132+
}
133+
134+
index = bpf_ntohl(md.index);
135+
bpf_trace_printk(fmt, sizeof(fmt),
136+
key.tunnel_id, key.remote_ipv4, index);
137+
138+
return TC_ACT_OK;
139+
}
140+
79141
SEC("vxlan_set_tunnel")
80142
int _vxlan_set_tunnel(struct __sk_buff *skb)
81143
{
@@ -378,5 +440,4 @@ int _ip6ip6_get_tunnel(struct __sk_buff *skb)
378440
return TC_ACT_OK;
379441
}
380442

381-
382443
char _license[] SEC("license") = "GPL";

samples/bpf/test_tunnel_bpf.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ function add_gre_tunnel {
3232
ip addr add dev $DEV 10.1.1.200/24
3333
}
3434

35+
function add_erspan_tunnel {
36+
# in namespace
37+
ip netns exec at_ns0 \
38+
ip link add dev $DEV_NS type $TYPE seq key 2 local 172.16.1.100 remote 172.16.1.200 erspan 123
39+
ip netns exec at_ns0 ip link set dev $DEV_NS up
40+
ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
41+
42+
# out of namespace
43+
ip link add dev $DEV type $TYPE external
44+
ip link set dev $DEV up
45+
ip addr add dev $DEV 10.1.1.200/24
46+
}
47+
3548
function add_vxlan_tunnel {
3649
# Set static ARP entry here because iptables set-mark works
3750
# on L3 packet, as a result not applying to ARP packets,
@@ -99,6 +112,18 @@ function test_gre {
99112
cleanup
100113
}
101114

115+
function test_erspan {
116+
TYPE=erspan
117+
DEV_NS=erspan00
118+
DEV=erspan11
119+
config_device
120+
add_erspan_tunnel
121+
attach_bpf $DEV erspan_set_tunnel erspan_get_tunnel
122+
ping -c 1 10.1.1.100
123+
ip netns exec at_ns0 ping -c 1 10.1.1.200
124+
cleanup
125+
}
126+
102127
function test_vxlan {
103128
TYPE=vxlan
104129
DEV_NS=vxlan00
@@ -151,14 +176,18 @@ function cleanup {
151176
ip link del gretap11
152177
ip link del vxlan11
153178
ip link del geneve11
179+
ip link del erspan11
154180
pkill tcpdump
155181
pkill cat
156182
set -ex
157183
}
158184

185+
trap cleanup 0 2 3 6 9
159186
cleanup
160187
echo "Testing GRE tunnel..."
161188
test_gre
189+
echo "Testing ERSPAN tunnel..."
190+
test_erspan
162191
echo "Testing VXLAN tunnel..."
163192
test_vxlan
164193
echo "Testing GENEVE tunnel..."

0 commit comments

Comments
 (0)