Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[sfrench/cifs-2.6.git] / samples / bpf / test_tunnel_bpf.sh
1 #!/bin/bash
2 # In Namespace 0 (at_ns0) using native tunnel
3 # Overlay IP: 10.1.1.100
4 # local 192.16.1.100 remote 192.16.1.200
5 # veth0 IP: 172.16.1.100, tunnel dev <type>00
6
7 # Out of Namespace using BPF set/get on lwtunnel
8 # Overlay IP: 10.1.1.200
9 # local 172.16.1.200 remote 172.16.1.100
10 # veth1 IP: 172.16.1.200, tunnel dev <type>11
11
12 function config_device {
13         ip netns add at_ns0
14         ip link add veth0 type veth peer name veth1
15         ip link set veth0 netns at_ns0
16         ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
17         ip netns exec at_ns0 ip link set dev veth0 up
18         ip link set dev veth1 up mtu 1500
19         ip addr add dev veth1 172.16.1.200/24
20 }
21
22 function add_gre_tunnel {
23         # in namespace
24         ip netns exec at_ns0 \
25                 ip link add dev $DEV_NS type $TYPE key 2 local 172.16.1.100 remote 172.16.1.200
26         ip netns exec at_ns0 ip link set dev $DEV_NS up
27         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
28
29         # out of namespace
30         ip link add dev $DEV type $TYPE key 2 external
31         ip link set dev $DEV up
32         ip addr add dev $DEV 10.1.1.200/24
33 }
34
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
48 function add_vxlan_tunnel {
49         # Set static ARP entry here because iptables set-mark works
50         # on L3 packet, as a result not applying to ARP packets,
51         # causing errors at get_tunnel_{key/opt}.
52
53         # in namespace
54         ip netns exec at_ns0 \
55                 ip link add dev $DEV_NS type $TYPE id 2 dstport 4789 gbp remote 172.16.1.200
56         ip netns exec at_ns0 ip link set dev $DEV_NS address 52:54:00:d9:01:00 up
57         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
58         ip netns exec at_ns0 arp -s 10.1.1.200 52:54:00:d9:02:00
59         ip netns exec at_ns0 iptables -A OUTPUT -j MARK --set-mark 0x800FF
60
61         # out of namespace
62         ip link add dev $DEV type $TYPE external gbp dstport 4789
63         ip link set dev $DEV address 52:54:00:d9:02:00 up
64         ip addr add dev $DEV 10.1.1.200/24
65         arp -s 10.1.1.100 52:54:00:d9:01:00
66 }
67
68 function add_geneve_tunnel {
69         # in namespace
70         ip netns exec at_ns0 \
71                 ip link add dev $DEV_NS type $TYPE id 2 dstport 6081 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 dstport 6081 external
77         ip link set dev $DEV up
78         ip addr add dev $DEV 10.1.1.200/24
79 }
80
81 function add_ipip_tunnel {
82         # in namespace
83         ip netns exec at_ns0 \
84                 ip link add dev $DEV_NS type $TYPE local 172.16.1.100 remote 172.16.1.200
85         ip netns exec at_ns0 ip link set dev $DEV_NS up
86         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
87
88         # out of namespace
89         ip link add dev $DEV type $TYPE external
90         ip link set dev $DEV up
91         ip addr add dev $DEV 10.1.1.200/24
92 }
93
94 function attach_bpf {
95         DEV=$1
96         SET_TUNNEL=$2
97         GET_TUNNEL=$3
98         tc qdisc add dev $DEV clsact
99         tc filter add dev $DEV egress bpf da obj tcbpf2_kern.o sec $SET_TUNNEL
100         tc filter add dev $DEV ingress bpf da obj tcbpf2_kern.o sec $GET_TUNNEL
101 }
102
103 function test_gre {
104         TYPE=gretap
105         DEV_NS=gretap00
106         DEV=gretap11
107         config_device
108         add_gre_tunnel
109         attach_bpf $DEV gre_set_tunnel gre_get_tunnel
110         ping -c 1 10.1.1.100
111         ip netns exec at_ns0 ping -c 1 10.1.1.200
112         cleanup
113 }
114
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
127 function test_vxlan {
128         TYPE=vxlan
129         DEV_NS=vxlan00
130         DEV=vxlan11
131         config_device
132         add_vxlan_tunnel
133         attach_bpf $DEV vxlan_set_tunnel vxlan_get_tunnel
134         ping -c 1 10.1.1.100
135         ip netns exec at_ns0 ping -c 1 10.1.1.200
136         cleanup
137 }
138
139 function test_geneve {
140         TYPE=geneve
141         DEV_NS=geneve00
142         DEV=geneve11
143         config_device
144         add_geneve_tunnel
145         attach_bpf $DEV geneve_set_tunnel geneve_get_tunnel
146         ping -c 1 10.1.1.100
147         ip netns exec at_ns0 ping -c 1 10.1.1.200
148         cleanup
149 }
150
151 function test_ipip {
152         TYPE=ipip
153         DEV_NS=ipip00
154         DEV=ipip11
155         config_device
156         tcpdump -nei veth1 &
157         cat /sys/kernel/debug/tracing/trace_pipe &
158         add_ipip_tunnel
159         ethtool -K veth1 gso off gro off rx off tx off
160         ip link set dev veth1 mtu 1500
161         attach_bpf $DEV ipip_set_tunnel ipip_get_tunnel
162         ping -c 1 10.1.1.100
163         ip netns exec at_ns0 ping -c 1 10.1.1.200
164         ip netns exec at_ns0 iperf -sD -p 5200 > /dev/null
165         sleep 0.2
166         iperf -c 10.1.1.100 -n 5k -p 5200
167         cleanup
168 }
169
170 function cleanup {
171         set +ex
172         pkill iperf
173         ip netns delete at_ns0
174         ip link del veth1
175         ip link del ipip11
176         ip link del gretap11
177         ip link del vxlan11
178         ip link del geneve11
179         ip link del erspan11
180         pkill tcpdump
181         pkill cat
182         set -ex
183 }
184
185 trap cleanup 0 2 3 6 9
186 cleanup
187 echo "Testing GRE tunnel..."
188 test_gre
189 echo "Testing ERSPAN tunnel..."
190 test_erspan
191 echo "Testing VXLAN tunnel..."
192 test_vxlan
193 echo "Testing GENEVE tunnel..."
194 test_geneve
195 echo "Testing IPIP tunnel..."
196 test_ipip
197 echo "*** PASS ***"