1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Handles creation and destruction of IP-in-IP or GRE tunnels over the given
5# topology. Supports both flat and hierarchical models.
6#
7# Flat Model:
8# Overlay and underlay share the same VRF.
9# SW1 uses default VRF so tunnel has no bound dev.
10# SW2 uses non-default VRF tunnel has a bound dev.
11# +-------------------------+
12# | H1                      |
13# |               $h1 +     |
14# |      192.0.2.1/28 |     |
15# +-------------------|-----+
16#                     |
17# +-------------------|-----+
18# | SW1               |     |
19# |              $ol1 +     |
20# |      192.0.2.2/28       |
21# |                         |
22# |  + g1a (gre)            |
23# |    loc=192.0.2.65       |
24# |    rem=192.0.2.66 --.   |
25# |    tos=inherit      |   |
26# |  .------------------'   |
27# |  |                      |
28# |  v                      |
29# |  + $ul1.111 (vlan)      |
30# |  | 192.0.2.129/28       |
31# |   \                     |
32# |    \_______             |
33# |            |            |
34# |VRF default + $ul1       |
35# +------------|------------+
36#              |
37# +------------|------------+
38# | SW2        + $ul2       |
39# |     _______|            |
40# |    /                    |
41# |   /                     |
42# |  + $ul2.111 (vlan)      |
43# |  ^ 192.0.2.130/28       |
44# |  |                      |
45# |  |                      |
46# |  '------------------.   |
47# |  + g2a (gre)        |   |
48# |    loc=192.0.2.66   |   |
49# |    rem=192.0.2.65 --'   |
50# |    tos=inherit          |
51# |                         |
52# |              $ol2 +     |
53# |     192.0.2.17/28 |     |
54# | VRF v$ol2         |     |
55# +-------------------|-----+
56#                     |
57# +-------------------|-----+
58# | H2                |     |
59# |               $h2 +     |
60# |     192.0.2.18/28       |
61# +-------------------------+
62#
63# Hierarchical model:
64# The tunnel is bound to a device in a different VRF
65#
66# +---------------------------+
67# | H1                        |
68# |               $h1 +       |
69# |      192.0.2.1/28 |       |
70# +-------------------|-------+
71#                     |
72# +-------------------|-------+
73# | SW1               |       |
74# | +-----------------|-----+ |
75# | |            $ol1 +     | |
76# | |     192.0.2.2/28      | |
77# | |                       | |
78# | |    + g1a (gre)        | |
79# | |    rem=192.0.2.66     | |
80# | |    tos=inherit        | |
81# | |    loc=192.0.2.65     | |
82# | |           ^           | |
83# | | VRF v$ol1 |           | |
84# | +-----------|-----------+ |
85# |             |             |
86# | +-----------|-----------+ |
87# | | VRF v$ul1 |           | |
88# | |           |           | |
89# | |           |           | |
90# | |           v           | |
91# | |    dummy1 +           | |
92# | |   192.0.2.65          | |
93# | |   .-------'           | |
94# | |   |                   | |
95# | |   v                   | |
96# | |   + $ul1.111 (vlan)   | |
97# | |   | 192.0.2.129/28    | |
98# | |   \                   | |
99# | |    \_____             | |
100# | |          |            | |
101# | |          + $ul1       | |
102# | +----------|------------+ |
103# +------------|--------------+
104#              |
105# +------------|--------------+
106# | SW2        |              |
107# | +----------|------------+ |
108# | |          + $ul2       | |
109# | |     _____|            | |
110# | |    /                  | |
111# | |   /                   | |
112# | |   | $ul2.111 (vlan)   | |
113# | |   + 192.0.2.130/28    | |
114# | |   ^                   | |
115# | |   |                   | |
116# | |   '-------.           | |
117# | |    dummy2 +           | |
118# | |    192.0.2.66         | |
119# | |           ^           | |
120# | |           |           | |
121# | |           |           | |
122# | | VRF v$ul2 |           | |
123# | +-----------|-----------+ |
124# |             |             |
125# | +-----------|-----------+ |
126# | | VRF v$ol2 |           | |
127# | |           |           | |
128# | |           v           | |
129# | |  g2a (gre)+           | |
130# | |  loc=192.0.2.66       | |
131# | |  rem=192.0.2.65       | |
132# | |  tos=inherit          | |
133# | |                       | |
134# | |            $ol2 +     | |
135# | |   192.0.2.17/28 |     | |
136# | +-----------------|-----+ |
137# +-------------------|-------+
138#                     |
139# +-------------------|-------+
140# | H2                |       |
141# |               $h2 +       |
142# |     192.0.2.18/28         |
143# +---------------------------+
144source lib.sh
145
146h1_create()
147{
148	simple_if_init $h1 192.0.2.1/28 2001:db8:1::1/64
149	ip route add vrf v$h1 192.0.2.16/28 via 192.0.2.2
150}
151
152h1_destroy()
153{
154	ip route del vrf v$h1 192.0.2.16/28 via 192.0.2.2
155	simple_if_fini $h1 192.0.2.1/28
156}
157
158h2_create()
159{
160	simple_if_init $h2 192.0.2.18/28
161	ip route add vrf v$h2 192.0.2.0/28 via 192.0.2.17
162}
163
164h2_destroy()
165{
166	ip route del vrf v$h2 192.0.2.0/28 via 192.0.2.17
167	simple_if_fini $h2 192.0.2.18/28
168}
169
170sw1_flat_create()
171{
172	local type=$1; shift
173	local ol1=$1; shift
174	local ul1=$1; shift
175
176	ip link set dev $ol1 up
177        __addr_add_del $ol1 add "192.0.2.2/28"
178
179	ip link set dev $ul1 up
180	vlan_create $ul1 111 "" 192.0.2.129/28
181
182	tunnel_create g1a $type 192.0.2.65 192.0.2.66 tos inherit "$@"
183	ip link set dev g1a up
184        __addr_add_del g1a add "192.0.2.65/32"
185
186	ip route add 192.0.2.66/32 via 192.0.2.130
187
188	ip route add 192.0.2.16/28 nexthop dev g1a
189}
190
191sw1_flat_destroy()
192{
193	local ol1=$1; shift
194	local ul1=$1; shift
195
196	ip route del 192.0.2.16/28
197
198	ip route del 192.0.2.66/32 via 192.0.2.130
199	__simple_if_fini g1a 192.0.2.65/32
200	tunnel_destroy g1a
201
202	vlan_destroy $ul1 111
203	__simple_if_fini $ul1
204	__simple_if_fini $ol1 192.0.2.2/28
205}
206
207sw2_flat_create()
208{
209	local type=$1; shift
210	local ol2=$1; shift
211	local ul2=$1; shift
212
213	simple_if_init $ol2 192.0.2.17/28
214	__simple_if_init $ul2 v$ol2
215	vlan_create $ul2 111 v$ol2 192.0.2.130/28
216
217	tunnel_create g2a $type 192.0.2.66 192.0.2.65 tos inherit dev v$ol2 \
218		"$@"
219	__simple_if_init g2a v$ol2 192.0.2.66/32
220
221	ip route add vrf v$ol2 192.0.2.65/32 via 192.0.2.129
222	ip route add vrf v$ol2 192.0.2.0/28 nexthop dev g2a
223}
224
225sw2_flat_destroy()
226{
227	local ol2=$1; shift
228	local ul2=$1; shift
229
230	ip route del vrf v$ol2 192.0.2.0/28
231
232	ip route del vrf v$ol2 192.0.2.65/32 via 192.0.2.129
233	__simple_if_fini g2a 192.0.2.66/32
234	tunnel_destroy g2a
235
236	vlan_destroy $ul2 111
237	__simple_if_fini $ul2
238	simple_if_fini $ol2 192.0.2.17/28
239}
240
241sw1_hierarchical_create()
242{
243	local type=$1; shift
244	local ol1=$1; shift
245	local ul1=$1; shift
246
247	simple_if_init $ol1 192.0.2.2/28
248	simple_if_init $ul1
249	ip link add name dummy1 type dummy
250	__simple_if_init dummy1 v$ul1 192.0.2.65/32
251
252	vlan_create $ul1 111 v$ul1 192.0.2.129/28
253	tunnel_create g1a $type 192.0.2.65 192.0.2.66 tos inherit dev dummy1 \
254		"$@"
255	ip link set dev g1a master v$ol1
256
257	ip route add vrf v$ul1 192.0.2.66/32 via 192.0.2.130
258	ip route add vrf v$ol1 192.0.2.16/28 nexthop dev g1a
259}
260
261sw1_hierarchical_destroy()
262{
263	local ol1=$1; shift
264	local ul1=$1; shift
265
266	ip route del vrf v$ol1 192.0.2.16/28
267	ip route del vrf v$ul1 192.0.2.66/32
268
269	tunnel_destroy g1a
270	vlan_destroy $ul1 111
271
272	__simple_if_fini dummy1 192.0.2.65/32
273	ip link del dev dummy1
274
275	simple_if_fini $ul1
276	simple_if_fini $ol1 192.0.2.2/28
277}
278
279sw2_hierarchical_create()
280{
281	local type=$1; shift
282	local ol2=$1; shift
283	local ul2=$1; shift
284
285	simple_if_init $ol2 192.0.2.17/28
286	simple_if_init $ul2
287
288	ip link add name dummy2 type dummy
289	__simple_if_init dummy2 v$ul2 192.0.2.66/32
290
291	vlan_create $ul2 111 v$ul2 192.0.2.130/28
292	tunnel_create g2a $type 192.0.2.66 192.0.2.65 tos inherit dev dummy2 \
293		"$@"
294	ip link set dev g2a master v$ol2
295
296	ip route add vrf v$ul2 192.0.2.65/32 via 192.0.2.129
297	ip route add vrf v$ol2 192.0.2.0/28 nexthop dev g2a
298}
299
300sw2_hierarchical_destroy()
301{
302	local ol2=$1; shift
303	local ul2=$1; shift
304
305	ip route del vrf v$ol2 192.0.2.0/28
306	ip route del vrf v$ul2 192.0.2.65/32
307
308	tunnel_destroy g2a
309	vlan_destroy $ul2 111
310
311	__simple_if_fini dummy2 192.0.2.66/32
312	ip link del dev dummy2
313
314	simple_if_fini $ul2
315	simple_if_fini $ol2 192.0.2.17/28
316}
317
318topo_mtu_change()
319{
320	local mtu=$1
321
322	ip link set mtu $mtu dev $h1
323	ip link set mtu $mtu dev $ol1
324	ip link set mtu $mtu dev g1a
325	ip link set mtu $mtu dev $ul1
326	ip link set mtu $mtu dev $ul1.111
327	ip link set mtu $mtu dev $h2
328	ip link set mtu $mtu dev $ol2
329	ip link set mtu $mtu dev g2a
330	ip link set mtu $mtu dev $ul2
331	ip link set mtu $mtu dev $ul2.111
332}
333
334test_mtu_change()
335{
336	local encap=$1; shift
337
338	RET=0
339
340	ping_do $h1 192.0.2.18 "-s 1800	-w 3"
341	check_fail $? "ping $encap should not pass with size 1800"
342
343	RET=0
344
345	topo_mtu_change	2000
346	ping_do	$h1 192.0.2.18 "-s 1800	-w 3"
347	check_err $?
348	log_test "ping $encap packet size 1800 after MTU change"
349}
350