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 # +---------------------------+
144 
145 h1_create()
146 {
147 	simple_if_init $h1 192.0.2.1/28 2001:db8:1::1/64
148 	ip route add vrf v$h1 192.0.2.16/28 via 192.0.2.2
149 }
150 
151 h1_destroy()
152 {
153 	ip route del vrf v$h1 192.0.2.16/28 via 192.0.2.2
154 	simple_if_fini $h1 192.0.2.1/28
155 }
156 
157 h2_create()
158 {
159 	simple_if_init $h2 192.0.2.18/28
160 	ip route add vrf v$h2 192.0.2.0/28 via 192.0.2.17
161 }
162 
163 h2_destroy()
164 {
165 	ip route del vrf v$h2 192.0.2.0/28 via 192.0.2.17
166 	simple_if_fini $h2 192.0.2.18/28
167 }
168 
169 sw1_flat_create()
170 {
171 	local type=$1; shift
172 	local ol1=$1; shift
173 	local ul1=$1; shift
174 
175 	ip link set dev $ol1 up
176         __addr_add_del $ol1 add "192.0.2.2/28"
177 
178 	ip link set dev $ul1 up
179 	vlan_create $ul1 111 "" 192.0.2.129/28
180 
181 	tunnel_create g1a $type 192.0.2.65 192.0.2.66 tos inherit "$@"
182 	ip link set dev g1a up
183         __addr_add_del g1a add "192.0.2.65/32"
184 
185 	ip route add 192.0.2.66/32 via 192.0.2.130
186 
187 	ip route add 192.0.2.16/28 nexthop dev g1a
188 }
189 
190 sw1_flat_destroy()
191 {
192 	local ol1=$1; shift
193 	local ul1=$1; shift
194 
195 	ip route del 192.0.2.16/28
196 
197 	ip route del 192.0.2.66/32 via 192.0.2.130
198 	__simple_if_fini g1a 192.0.2.65/32
199 	tunnel_destroy g1a
200 
201 	vlan_destroy $ul1 111
202 	__simple_if_fini $ul1
203 	__simple_if_fini $ol1 192.0.2.2/28
204 }
205 
206 sw2_flat_create()
207 {
208 	local type=$1; shift
209 	local ol2=$1; shift
210 	local ul2=$1; shift
211 
212 	simple_if_init $ol2 192.0.2.17/28
213 	__simple_if_init $ul2 v$ol2
214 	vlan_create $ul2 111 v$ol2 192.0.2.130/28
215 
216 	tunnel_create g2a $type 192.0.2.66 192.0.2.65 tos inherit dev v$ol2 \
217 		"$@"
218 	__simple_if_init g2a v$ol2 192.0.2.66/32
219 
220 	ip route add vrf v$ol2 192.0.2.65/32 via 192.0.2.129
221 	ip route add vrf v$ol2 192.0.2.0/28 nexthop dev g2a
222 }
223 
224 sw2_flat_destroy()
225 {
226 	local ol2=$1; shift
227 	local ul2=$1; shift
228 
229 	ip route del vrf v$ol2 192.0.2.0/28
230 
231 	ip route del vrf v$ol2 192.0.2.65/32 via 192.0.2.129
232 	__simple_if_fini g2a 192.0.2.66/32
233 	tunnel_destroy g2a
234 
235 	vlan_destroy $ul2 111
236 	__simple_if_fini $ul2
237 	simple_if_fini $ol2 192.0.2.17/28
238 }
239 
240 sw1_hierarchical_create()
241 {
242 	local type=$1; shift
243 	local ol1=$1; shift
244 	local ul1=$1; shift
245 
246 	simple_if_init $ol1 192.0.2.2/28
247 	simple_if_init $ul1
248 	ip link add name dummy1 type dummy
249 	__simple_if_init dummy1 v$ul1 192.0.2.65/32
250 
251 	vlan_create $ul1 111 v$ul1 192.0.2.129/28
252 	tunnel_create g1a $type 192.0.2.65 192.0.2.66 tos inherit dev dummy1 \
253 		"$@"
254 	ip link set dev g1a master v$ol1
255 
256 	ip route add vrf v$ul1 192.0.2.66/32 via 192.0.2.130
257 	ip route add vrf v$ol1 192.0.2.16/28 nexthop dev g1a
258 }
259 
260 sw1_hierarchical_destroy()
261 {
262 	local ol1=$1; shift
263 	local ul1=$1; shift
264 
265 	ip route del vrf v$ol1 192.0.2.16/28
266 	ip route del vrf v$ul1 192.0.2.66/32
267 
268 	tunnel_destroy g1a
269 	vlan_destroy $ul1 111
270 
271 	__simple_if_fini dummy1 192.0.2.65/32
272 	ip link del dev dummy1
273 
274 	simple_if_fini $ul1
275 	simple_if_fini $ol1 192.0.2.2/28
276 }
277 
278 sw2_hierarchical_create()
279 {
280 	local type=$1; shift
281 	local ol2=$1; shift
282 	local ul2=$1; shift
283 
284 	simple_if_init $ol2 192.0.2.17/28
285 	simple_if_init $ul2
286 
287 	ip link add name dummy2 type dummy
288 	__simple_if_init dummy2 v$ul2 192.0.2.66/32
289 
290 	vlan_create $ul2 111 v$ul2 192.0.2.130/28
291 	tunnel_create g2a $type 192.0.2.66 192.0.2.65 tos inherit dev dummy2 \
292 		"$@"
293 	ip link set dev g2a master v$ol2
294 
295 	ip route add vrf v$ul2 192.0.2.65/32 via 192.0.2.129
296 	ip route add vrf v$ol2 192.0.2.0/28 nexthop dev g2a
297 }
298 
299 sw2_hierarchical_destroy()
300 {
301 	local ol2=$1; shift
302 	local ul2=$1; shift
303 
304 	ip route del vrf v$ol2 192.0.2.0/28
305 	ip route del vrf v$ul2 192.0.2.65/32
306 
307 	tunnel_destroy g2a
308 	vlan_destroy $ul2 111
309 
310 	__simple_if_fini dummy2 192.0.2.66/32
311 	ip link del dev dummy2
312 
313 	simple_if_fini $ul2
314 	simple_if_fini $ol2 192.0.2.17/28
315 }
316 
317 topo_mtu_change()
318 {
319 	local mtu=$1
320 
321 	ip link set mtu $mtu dev $h1
322 	ip link set mtu $mtu dev $ol1
323 	ip link set mtu $mtu dev g1a
324 	ip link set mtu $mtu dev $ul1
325 	ip link set mtu $mtu dev $ul1.111
326 	ip link set mtu $mtu dev $h2
327 	ip link set mtu $mtu dev $ol2
328 	ip link set mtu $mtu dev g2a
329 	ip link set mtu $mtu dev $ul2
330 	ip link set mtu $mtu dev $ul2.111
331 }
332 
333 test_mtu_change()
334 {
335 	local encap=$1; shift
336 
337 	RET=0
338 
339 	ping_do $h1 192.0.2.18 "-s 1800	-w 3"
340 	check_fail $? "ping $encap should not pass with size 1800"
341 
342 	RET=0
343 
344 	topo_mtu_change	2000
345 	ping_do	$h1 192.0.2.18 "-s 1800	-w 3"
346 	check_err $?
347 	log_test "ping $encap packet size 1800 after MTU change"
348 }
349