Skip to content

Commit a1c8270

Browse files
4astdavem330
authored andcommitted
samples/bpf: extend test_tunnel_bpf.sh with IPIP test
extend existing tests for vxlan, geneve, gre to include IPIP tunnel. It tests both traditional tunnel configuration and dynamic via bpf helpers. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8d79266 commit a1c8270

File tree

2 files changed

+106
-8
lines changed

2 files changed

+106
-8
lines changed

samples/bpf/tcbpf2_kern.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* Copyright (c) 2016 VMware
2+
* Copyright (c) 2016 Facebook
23
*
34
* This program is free software; you can redistribute it and/or
45
* modify it under the terms of version 2 of the GNU General Public
@@ -188,4 +189,61 @@ int _geneve_get_tunnel(struct __sk_buff *skb)
188189
return TC_ACT_OK;
189190
}
190191

192+
SEC("ipip_set_tunnel")
193+
int _ipip_set_tunnel(struct __sk_buff *skb)
194+
{
195+
struct bpf_tunnel_key key = {};
196+
void *data = (void *)(long)skb->data;
197+
struct iphdr *iph = data;
198+
struct tcphdr *tcp = data + sizeof(*iph);
199+
void *data_end = (void *)(long)skb->data_end;
200+
int ret;
201+
202+
/* single length check */
203+
if (data + sizeof(*iph) + sizeof(*tcp) > data_end) {
204+
ERROR(1);
205+
return TC_ACT_SHOT;
206+
}
207+
208+
key.tunnel_ttl = 64;
209+
if (iph->protocol == IPPROTO_ICMP) {
210+
key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
211+
} else {
212+
if (iph->protocol != IPPROTO_TCP || iph->ihl != 5)
213+
return TC_ACT_SHOT;
214+
215+
if (tcp->dest == htons(5200))
216+
key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
217+
else if (tcp->dest == htons(5201))
218+
key.remote_ipv4 = 0xac100165; /* 172.16.1.101 */
219+
else
220+
return TC_ACT_SHOT;
221+
}
222+
223+
ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0);
224+
if (ret < 0) {
225+
ERROR(ret);
226+
return TC_ACT_SHOT;
227+
}
228+
229+
return TC_ACT_OK;
230+
}
231+
232+
SEC("ipip_get_tunnel")
233+
int _ipip_get_tunnel(struct __sk_buff *skb)
234+
{
235+
int ret;
236+
struct bpf_tunnel_key key;
237+
char fmt[] = "remote ip 0x%x\n";
238+
239+
ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
240+
if (ret < 0) {
241+
ERROR(ret);
242+
return TC_ACT_SHOT;
243+
}
244+
245+
bpf_trace_printk(fmt, sizeof(fmt), key.remote_ipv4);
246+
return TC_ACT_OK;
247+
}
248+
191249
char _license[] SEC("license") = "GPL";

samples/bpf/test_tunnel_bpf.sh

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99
# local 172.16.1.200 remote 172.16.1.100
1010
# veth1 IP: 172.16.1.200, tunnel dev <type>11
1111

12-
set -e
13-
1412
function config_device {
1513
ip netns add at_ns0
1614
ip link add veth0 type veth peer name veth1
1715
ip link set veth0 netns at_ns0
1816
ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
1917
ip netns exec at_ns0 ip link set dev veth0 up
20-
ip link set dev veth1 up
18+
ip link set dev veth1 up mtu 1500
2119
ip addr add dev veth1 172.16.1.200/24
2220
}
2321

@@ -67,6 +65,19 @@ function add_geneve_tunnel {
6765
ip addr add dev $DEV 10.1.1.200/24
6866
}
6967

68+
function add_ipip_tunnel {
69+
# in namespace
70+
ip netns exec at_ns0 \
71+
ip link add dev $DEV_NS type $TYPE local 172.16.1.100 remote 172.16.1.200
72+
ip netns exec at_ns0 ip link set dev $DEV_NS up
73+
ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
74+
75+
# out of namespace
76+
ip link add dev $DEV type $TYPE external
77+
ip link set dev $DEV up
78+
ip addr add dev $DEV 10.1.1.200/24
79+
}
80+
7081
function attach_bpf {
7182
DEV=$1
7283
SET_TUNNEL=$2
@@ -85,6 +96,7 @@ function test_gre {
8596
attach_bpf $DEV gre_set_tunnel gre_get_tunnel
8697
ping -c 1 10.1.1.100
8798
ip netns exec at_ns0 ping -c 1 10.1.1.200
99+
cleanup
88100
}
89101

90102
function test_vxlan {
@@ -96,6 +108,7 @@ function test_vxlan {
96108
attach_bpf $DEV vxlan_set_tunnel vxlan_get_tunnel
97109
ping -c 1 10.1.1.100
98110
ip netns exec at_ns0 ping -c 1 10.1.1.200
111+
cleanup
99112
}
100113

101114
function test_geneve {
@@ -107,21 +120,48 @@ function test_geneve {
107120
attach_bpf $DEV geneve_set_tunnel geneve_get_tunnel
108121
ping -c 1 10.1.1.100
109122
ip netns exec at_ns0 ping -c 1 10.1.1.200
123+
cleanup
124+
}
125+
126+
function test_ipip {
127+
TYPE=ipip
128+
DEV_NS=ipip00
129+
DEV=ipip11
130+
config_device
131+
tcpdump -nei veth1 &
132+
cat /sys/kernel/debug/tracing/trace_pipe &
133+
add_ipip_tunnel
134+
ethtool -K veth1 gso off gro off rx off tx off
135+
ip link set dev veth1 mtu 1500
136+
attach_bpf $DEV ipip_set_tunnel ipip_get_tunnel
137+
ping -c 1 10.1.1.100
138+
ip netns exec at_ns0 ping -c 1 10.1.1.200
139+
ip netns exec at_ns0 iperf -sD -p 5200 > /dev/null
140+
sleep 0.2
141+
iperf -c 10.1.1.100 -n 5k -p 5200
142+
cleanup
110143
}
111144

112145
function cleanup {
146+
set +ex
147+
pkill iperf
113148
ip netns delete at_ns0
114149
ip link del veth1
115-
ip link del $DEV
150+
ip link del ipip11
151+
ip link del gretap11
152+
ip link del geneve11
153+
pkill tcpdump
154+
pkill cat
155+
set -ex
116156
}
117157

158+
cleanup
118159
echo "Testing GRE tunnel..."
119160
test_gre
120-
cleanup
121161
echo "Testing VXLAN tunnel..."
122162
test_vxlan
123-
cleanup
124163
echo "Testing GENEVE tunnel..."
125164
test_geneve
126-
cleanup
127-
echo "Success"
165+
echo "Testing IPIP tunnel..."
166+
test_ipip
167+
echo "*** PASS ***"

0 commit comments

Comments
 (0)