kvm: x86: IA32_ARCH_CAPABILITIES is always supported
[linux-2.6-microblaze.git] / samples / bpf / test_tunnel_bpf.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # In Namespace 0 (at_ns0) using native tunnel
4 # Overlay IP: 10.1.1.100
5 # local 192.16.1.100 remote 192.16.1.200
6 # veth0 IP: 172.16.1.100, tunnel dev <type>00
7
8 # Out of Namespace using BPF set/get on lwtunnel
9 # Overlay IP: 10.1.1.200
10 # local 172.16.1.200 remote 172.16.1.100
11 # veth1 IP: 172.16.1.200, tunnel dev <type>11
12
13 function config_device {
14         ip netns add at_ns0
15         ip link add veth0 type veth peer name veth1
16         ip link set veth0 netns at_ns0
17         ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
18         ip netns exec at_ns0 ip link set dev veth0 up
19         ip link set dev veth1 up mtu 1500
20         ip addr add dev veth1 172.16.1.200/24
21 }
22
23 function add_gre_tunnel {
24         # in namespace
25         ip netns exec at_ns0 \
26         ip link add dev $DEV_NS type $TYPE seq key 2 \
27                 local 172.16.1.100 remote 172.16.1.200
28         ip netns exec at_ns0 ip link set dev $DEV_NS up
29         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
30
31         # out of namespace
32         ip link add dev $DEV type $TYPE key 2 external
33         ip link set dev $DEV up
34         ip addr add dev $DEV 10.1.1.200/24
35 }
36
37 function add_ip6gretap_tunnel {
38
39         # assign ipv6 address
40         ip netns exec at_ns0 ip addr add ::11/96 dev veth0
41         ip netns exec at_ns0 ip link set dev veth0 up
42         ip addr add dev veth1 ::22/96
43         ip link set dev veth1 up
44
45         # in namespace
46         ip netns exec at_ns0 \
47                 ip link add dev $DEV_NS type $TYPE seq flowlabel 0xbcdef key 2 \
48                 local ::11 remote ::22
49
50         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
51         ip netns exec at_ns0 ip addr add dev $DEV_NS fc80::100/96
52         ip netns exec at_ns0 ip link set dev $DEV_NS up
53
54         # out of namespace
55         ip link add dev $DEV type $TYPE external
56         ip addr add dev $DEV 10.1.1.200/24
57         ip addr add dev $DEV fc80::200/24
58         ip link set dev $DEV up
59 }
60
61 function add_erspan_tunnel {
62         # in namespace
63         if [ "$1" == "v1" ]; then
64                 ip netns exec at_ns0 \
65                 ip link add dev $DEV_NS type $TYPE seq key 2 \
66                 local 172.16.1.100 remote 172.16.1.200 \
67                 erspan_ver 1 erspan 123
68         else
69                 ip netns exec at_ns0 \
70                 ip link add dev $DEV_NS type $TYPE seq key 2 \
71                 local 172.16.1.100 remote 172.16.1.200 \
72                 erspan_ver 2 erspan_dir egress erspan_hwid 3
73         fi
74         ip netns exec at_ns0 ip link set dev $DEV_NS up
75         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
76
77         # out of namespace
78         ip link add dev $DEV type $TYPE external
79         ip link set dev $DEV up
80         ip addr add dev $DEV 10.1.1.200/24
81 }
82
83 function add_ip6erspan_tunnel {
84
85         # assign ipv6 address
86         ip netns exec at_ns0 ip addr add ::11/96 dev veth0
87         ip netns exec at_ns0 ip link set dev veth0 up
88         ip addr add dev veth1 ::22/96
89         ip link set dev veth1 up
90
91         # in namespace
92         if [ "$1" == "v1" ]; then
93                 ip netns exec at_ns0 \
94                 ip link add dev $DEV_NS type $TYPE seq key 2 \
95                 local ::11 remote ::22 \
96                 erspan_ver 1 erspan 123
97         else
98                 ip netns exec at_ns0 \
99                 ip link add dev $DEV_NS type $TYPE seq key 2 \
100                 local ::11 remote ::22 \
101                 erspan_ver 2 erspan_dir egress erspan_hwid 7
102         fi
103         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
104         ip netns exec at_ns0 ip link set dev $DEV_NS up
105
106         # out of namespace
107         ip link add dev $DEV type $TYPE external
108         ip addr add dev $DEV 10.1.1.200/24
109         ip link set dev $DEV up
110 }
111
112 function add_vxlan_tunnel {
113         # Set static ARP entry here because iptables set-mark works
114         # on L3 packet, as a result not applying to ARP packets,
115         # causing errors at get_tunnel_{key/opt}.
116
117         # in namespace
118         ip netns exec at_ns0 \
119                 ip link add dev $DEV_NS type $TYPE id 2 dstport 4789 gbp remote 172.16.1.200
120         ip netns exec at_ns0 ip link set dev $DEV_NS address 52:54:00:d9:01:00 up
121         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
122         ip netns exec at_ns0 arp -s 10.1.1.200 52:54:00:d9:02:00
123         ip netns exec at_ns0 iptables -A OUTPUT -j MARK --set-mark 0x800FF
124
125         # out of namespace
126         ip link add dev $DEV type $TYPE external gbp dstport 4789
127         ip link set dev $DEV address 52:54:00:d9:02:00 up
128         ip addr add dev $DEV 10.1.1.200/24
129         arp -s 10.1.1.100 52:54:00:d9:01:00
130 }
131
132 function add_geneve_tunnel {
133         # in namespace
134         ip netns exec at_ns0 \
135                 ip link add dev $DEV_NS type $TYPE id 2 dstport 6081 remote 172.16.1.200
136         ip netns exec at_ns0 ip link set dev $DEV_NS up
137         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
138
139         # out of namespace
140         ip link add dev $DEV type $TYPE dstport 6081 external
141         ip link set dev $DEV up
142         ip addr add dev $DEV 10.1.1.200/24
143 }
144
145 function add_ipip_tunnel {
146         # in namespace
147         ip netns exec at_ns0 \
148                 ip link add dev $DEV_NS type $TYPE local 172.16.1.100 remote 172.16.1.200
149         ip netns exec at_ns0 ip link set dev $DEV_NS up
150         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
151
152         # out of namespace
153         ip link add dev $DEV type $TYPE external
154         ip link set dev $DEV up
155         ip addr add dev $DEV 10.1.1.200/24
156 }
157
158 function attach_bpf {
159         DEV=$1
160         SET_TUNNEL=$2
161         GET_TUNNEL=$3
162         tc qdisc add dev $DEV clsact
163         tc filter add dev $DEV egress bpf da obj tcbpf2_kern.o sec $SET_TUNNEL
164         tc filter add dev $DEV ingress bpf da obj tcbpf2_kern.o sec $GET_TUNNEL
165 }
166
167 function test_gre {
168         TYPE=gretap
169         DEV_NS=gretap00
170         DEV=gretap11
171         config_device
172         add_gre_tunnel
173         attach_bpf $DEV gre_set_tunnel gre_get_tunnel
174         ping -c 1 10.1.1.100
175         ip netns exec at_ns0 ping -c 1 10.1.1.200
176         cleanup
177 }
178
179 function test_ip6gre {
180         TYPE=ip6gre
181         DEV_NS=ip6gre00
182         DEV=ip6gre11
183         config_device
184         # reuse the ip6gretap function
185         add_ip6gretap_tunnel
186         attach_bpf $DEV ip6gretap_set_tunnel ip6gretap_get_tunnel
187         # underlay
188         ping6 -c 4 ::11
189         # overlay: ipv4 over ipv6
190         ip netns exec at_ns0 ping -c 1 10.1.1.200
191         ping -c 1 10.1.1.100
192         # overlay: ipv6 over ipv6
193         ip netns exec at_ns0 ping6 -c 1 fc80::200
194         cleanup
195 }
196
197 function test_ip6gretap {
198         TYPE=ip6gretap
199         DEV_NS=ip6gretap00
200         DEV=ip6gretap11
201         config_device
202         add_ip6gretap_tunnel
203         attach_bpf $DEV ip6gretap_set_tunnel ip6gretap_get_tunnel
204         # underlay
205         ping6 -c 4 ::11
206         # overlay: ipv4 over ipv6
207         ip netns exec at_ns0 ping -i .2 -c 1 10.1.1.200
208         ping -c 1 10.1.1.100
209         # overlay: ipv6 over ipv6
210         ip netns exec at_ns0 ping6 -c 1 fc80::200
211         cleanup
212 }
213
214 function test_erspan {
215         TYPE=erspan
216         DEV_NS=erspan00
217         DEV=erspan11
218         config_device
219         add_erspan_tunnel $1
220         attach_bpf $DEV erspan_set_tunnel erspan_get_tunnel
221         ping -c 1 10.1.1.100
222         ip netns exec at_ns0 ping -c 1 10.1.1.200
223         cleanup
224 }
225
226 function test_ip6erspan {
227         TYPE=ip6erspan
228         DEV_NS=ip6erspan00
229         DEV=ip6erspan11
230         config_device
231         add_ip6erspan_tunnel $1
232         attach_bpf $DEV ip4ip6erspan_set_tunnel ip4ip6erspan_get_tunnel
233         ping6 -c 3 ::11
234         ip netns exec at_ns0 ping -c 1 10.1.1.200
235         cleanup
236 }
237
238 function test_vxlan {
239         TYPE=vxlan
240         DEV_NS=vxlan00
241         DEV=vxlan11
242         config_device
243         add_vxlan_tunnel
244         attach_bpf $DEV vxlan_set_tunnel vxlan_get_tunnel
245         ping -c 1 10.1.1.100
246         ip netns exec at_ns0 ping -c 1 10.1.1.200
247         cleanup
248 }
249
250 function test_geneve {
251         TYPE=geneve
252         DEV_NS=geneve00
253         DEV=geneve11
254         config_device
255         add_geneve_tunnel
256         attach_bpf $DEV geneve_set_tunnel geneve_get_tunnel
257         ping -c 1 10.1.1.100
258         ip netns exec at_ns0 ping -c 1 10.1.1.200
259         cleanup
260 }
261
262 function test_ipip {
263         TYPE=ipip
264         DEV_NS=ipip00
265         DEV=ipip11
266         config_device
267         tcpdump -nei veth1 &
268         cat /sys/kernel/debug/tracing/trace_pipe &
269         add_ipip_tunnel
270         ethtool -K veth1 gso off gro off rx off tx off
271         ip link set dev veth1 mtu 1500
272         attach_bpf $DEV ipip_set_tunnel ipip_get_tunnel
273         ping -c 1 10.1.1.100
274         ip netns exec at_ns0 ping -c 1 10.1.1.200
275         ip netns exec at_ns0 iperf -sD -p 5200 > /dev/null
276         sleep 0.2
277         iperf -c 10.1.1.100 -n 5k -p 5200
278         cleanup
279 }
280
281 function cleanup {
282         set +ex
283         pkill iperf
284         ip netns delete at_ns0
285         ip link del veth1
286         ip link del ipip11
287         ip link del gretap11
288         ip link del ip6gre11
289         ip link del ip6gretap11
290         ip link del vxlan11
291         ip link del geneve11
292         ip link del erspan11
293         ip link del ip6erspan11
294         pkill tcpdump
295         pkill cat
296         set -ex
297 }
298
299 trap cleanup 0 2 3 6 9
300 cleanup
301 echo "Testing GRE tunnel..."
302 test_gre
303 echo "Testing IP6GRE tunnel..."
304 test_ip6gre
305 echo "Testing IP6GRETAP tunnel..."
306 test_ip6gretap
307 echo "Testing ERSPAN tunnel..."
308 test_erspan v1
309 test_erspan v2
310 echo "Testing IP6ERSPAN tunnel..."
311 test_ip6erspan v1
312 test_ip6erspan v2
313 echo "Testing VXLAN tunnel..."
314 test_vxlan
315 echo "Testing GENEVE tunnel..."
316 test_geneve
317 echo "Testing IPIP tunnel..."
318 test_ipip
319 echo "*** PASS ***"