selftests/net: change shebang to bash to support "source"
[platform/kernel/linux-starfive.git] / tools / testing / selftests / net / pmtu.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 #
4 # Check that route PMTU values match expectations, and that initial device MTU
5 # values are assigned correctly
6 #
7 # Tests currently implemented:
8 #
9 # - pmtu_ipv4
10 #       Set up two namespaces, A and B, with two paths between them over routers
11 #       R1 and R2 (also implemented with namespaces), with different MTUs:
12 #
13 #         segment a_r1    segment b_r1          a_r1: 2000
14 #       .--------------R1--------------.        b_r1: 1400
15 #       A                               B       a_r2: 2000
16 #       '--------------R2--------------'        b_r2: 1500
17 #         segment a_r2    segment b_r2
18 #
19 #       Check that PMTU exceptions with the correct PMTU are created. Then
20 #       decrease and increase the MTU of the local link for one of the paths,
21 #       A to R1, checking that route exception PMTU changes accordingly over
22 #       this path. Also check that locked exceptions are created when an ICMP
23 #       message advertising a PMTU smaller than net.ipv4.route.min_pmtu is
24 #       received
25 #
26 # - pmtu_ipv6
27 #       Same as pmtu_ipv4, except for locked PMTU tests, using IPv6
28 #
29 # - pmtu_ipv4_dscp_icmp_exception
30 #       Set up the same network topology as pmtu_ipv4, but use non-default
31 #       routing table in A. A fib-rule is used to jump to this routing table
32 #       based on DSCP. Send ICMPv4 packets with the expected DSCP value and
33 #       verify that ECN doesn't interfere with the creation of PMTU exceptions.
34 #
35 # - pmtu_ipv4_dscp_udp_exception
36 #       Same as pmtu_ipv4_dscp_icmp_exception, but use UDP instead of ICMP.
37 #
38 # - pmtu_ipv4_vxlan4_exception
39 #       Set up the same network topology as pmtu_ipv4, create a VXLAN tunnel
40 #       over IPv4 between A and B, routed via R1. On the link between R1 and B,
41 #       set a MTU lower than the VXLAN MTU and the MTU on the link between A and
42 #       R1. Send IPv4 packets, exceeding the MTU between R1 and B, over VXLAN
43 #       from A to B and check that the PMTU exception is created with the right
44 #       value on A
45 #
46 # - pmtu_ipv6_vxlan4_exception
47 #       Same as pmtu_ipv4_vxlan4_exception, but send IPv6 packets from A to B
48 #
49 # - pmtu_ipv4_vxlan6_exception
50 #       Same as pmtu_ipv4_vxlan4_exception, but use IPv6 transport from A to B
51 #
52 # - pmtu_ipv6_vxlan6_exception
53 #       Same as pmtu_ipv4_vxlan6_exception, but send IPv6 packets from A to B
54 #
55 # - pmtu_ipv4_geneve4_exception
56 #       Same as pmtu_ipv4_vxlan4_exception, but using a GENEVE tunnel instead of
57 #       VXLAN
58 #
59 # - pmtu_ipv6_geneve4_exception
60 #       Same as pmtu_ipv6_vxlan4_exception, but using a GENEVE tunnel instead of
61 #       VXLAN
62 #
63 # - pmtu_ipv4_geneve6_exception
64 #       Same as pmtu_ipv4_vxlan6_exception, but using a GENEVE tunnel instead of
65 #       VXLAN
66 #
67 # - pmtu_ipv6_geneve6_exception
68 #       Same as pmtu_ipv6_vxlan6_exception, but using a GENEVE tunnel instead of
69 #       VXLAN
70 #
71 # - pmtu_ipv{4,6}_br_vxlan{4,6}_exception
72 #       Set up three namespaces, A, B, and C, with routing between A and B over
73 #       R1. R2 is unused in these tests. A has a veth connection to C, and is
74 #       connected to B via a VXLAN endpoint, which is directly bridged to C.
75 #       MTU on the B-R1 link is lower than other MTUs.
76 #
77 #       Check that both C and A are able to communicate with B over the VXLAN
78 #       tunnel, and that PMTU exceptions with the correct values are created.
79 #
80 #                         segment a_r1    segment b_r1            b_r1: 4000
81 #                       .--------------R1--------------.    everything
82 #          C---veth     A                               B         else: 5000
83 #               ' bridge                                |
84 #                   '---- - - - - - VXLAN - - - - - - - '
85 #
86 # - pmtu_ipv{4,6}_br_geneve{4,6}_exception
87 #       Same as pmtu_ipv{4,6}_br_vxlan{4,6}_exception, with a GENEVE tunnel
88 #       instead.
89 #
90 # - pmtu_ipv{4,6}_ovs_vxlan{4,6}_exception
91 #       Set up two namespaces, B, and C, with routing between the init namespace
92 #       and B over R1. A and R2 are unused in these tests. The init namespace
93 #       has a veth connection to C, and is connected to B via a VXLAN endpoint,
94 #       which is handled by Open vSwitch and bridged to C. MTU on the B-R1 link
95 #       is lower than other MTUs.
96 #
97 #       Check that C is able to communicate with B over the VXLAN tunnel, and
98 #       that PMTU exceptions with the correct values are created.
99 #
100 #                         segment a_r1    segment b_r1            b_r1: 4000
101 #                       .--------------R1--------------.    everything
102 #          C---veth    init                             B         else: 5000
103 #               '- ovs                                  |
104 #                   '---- - - - - - VXLAN - - - - - - - '
105 #
106 # - pmtu_ipv{4,6}_ovs_geneve{4,6}_exception
107 #       Same as pmtu_ipv{4,6}_ovs_vxlan{4,6}_exception, with a GENEVE tunnel
108 #       instead.
109 #
110 # - pmtu_ipv{4,6}_fou{4,6}_exception
111 #       Same as pmtu_ipv4_vxlan4, but using a direct IPv4/IPv6 encapsulation
112 #       (FoU) over IPv4/IPv6, instead of VXLAN
113 #
114 # - pmtu_ipv{4,6}_fou{4,6}_exception
115 #       Same as pmtu_ipv4_vxlan4, but using a generic UDP IPv4/IPv6
116 #       encapsulation (GUE) over IPv4/IPv6, instead of VXLAN
117 #
118 # - pmtu_ipv{4,6}_ipv{4,6}_exception
119 #       Same as pmtu_ipv4_vxlan4, but using a IPv4/IPv6 tunnel over IPv4/IPv6,
120 #       instead of VXLAN
121 #
122 # - pmtu_vti4_exception
123 #       Set up vti tunnel on top of veth, with xfrm states and policies, in two
124 #       namespaces with matching endpoints. Check that route exception is not
125 #       created if link layer MTU is not exceeded, then exceed it and check that
126 #       exception is created with the expected PMTU. The approach described
127 #       below for IPv6 doesn't apply here, because, on IPv4, administrative MTU
128 #       changes alone won't affect PMTU
129 #
130 # - pmtu_vti4_udp_exception
131 #       Same as pmtu_vti4_exception, but using ESP-in-UDP
132 #
133 # - pmtu_vti4_udp_routed_exception
134 #       Set up vti tunnel on top of veth connected through routing namespace and
135 #       add xfrm states and policies with ESP-in-UDP encapsulation. Check that
136 #       route exception is not created if link layer MTU is not exceeded, then
137 #       lower MTU on second part of routed environment and check that exception
138 #       is created with the expected PMTU.
139 #
140 # - pmtu_vti6_exception
141 #       Set up vti6 tunnel on top of veth, with xfrm states and policies, in two
142 #       namespaces with matching endpoints. Check that route exception is
143 #       created by exceeding link layer MTU with ping to other endpoint. Then
144 #       decrease and increase MTU of tunnel, checking that route exception PMTU
145 #       changes accordingly
146 #
147 # - pmtu_vti6_udp_exception
148 #       Same as pmtu_vti6_exception, but using ESP-in-UDP
149 #
150 # - pmtu_vti6_udp_routed_exception
151 #       Same as pmtu_vti6_udp_routed_exception but with routing between vti
152 #       endpoints
153 #
154 # - pmtu_vti4_default_mtu
155 #       Set up vti4 tunnel on top of veth, in two namespaces with matching
156 #       endpoints. Check that MTU assigned to vti interface is the MTU of the
157 #       lower layer (veth) minus additional lower layer headers (zero, for veth)
158 #       minus IPv4 header length
159 #
160 # - pmtu_vti6_default_mtu
161 #       Same as above, for IPv6
162 #
163 # - pmtu_vti4_link_add_mtu
164 #       Set up vti4 interface passing MTU value at link creation, check MTU is
165 #       configured, and that link is not created with invalid MTU values
166 #
167 # - pmtu_vti6_link_add_mtu
168 #       Same as above, for IPv6
169 #
170 # - pmtu_vti6_link_change_mtu
171 #       Set up two dummy interfaces with different MTUs, create a vti6 tunnel
172 #       and check that configured MTU is used on link creation and changes, and
173 #       that MTU is properly calculated instead when MTU is not configured from
174 #       userspace
175 #
176 # - cleanup_ipv4_exception
177 #       Similar to pmtu_ipv4_vxlan4_exception, but explicitly generate PMTU
178 #       exceptions on multiple CPUs and check that the veth device tear-down
179 #       happens in a timely manner
180 #
181 # - cleanup_ipv6_exception
182 #       Same as above, but use IPv6 transport from A to B
183 #
184 # - list_flush_ipv4_exception
185 #       Using the same topology as in pmtu_ipv4, create exceptions, and check
186 #       they are shown when listing exception caches, gone after flushing them
187 #
188 # - list_flush_ipv6_exception
189 #       Using the same topology as in pmtu_ipv6, create exceptions, and check
190 #       they are shown when listing exception caches, gone after flushing them
191 #
192 # - pmtu_ipv4_route_change
193 #       Use the same topology as in pmtu_ipv4, but issue a route replacement
194 #       command and delete the corresponding device afterward. This tests for
195 #       proper cleanup of the PMTU exceptions by the route replacement path.
196 #       Device unregistration should complete successfully
197 #
198 # - pmtu_ipv6_route_change
199 #       Same as above but with IPv6
200
201 source lib.sh
202
203 PAUSE_ON_FAIL=no
204 VERBOSE=0
205 TRACING=0
206
207 # Some systems don't have a ping6 binary anymore
208 which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
209
210 #               Name                          Description                  re-run with nh
211 tests="
212         pmtu_ipv4_exception             ipv4: PMTU exceptions                   1
213         pmtu_ipv6_exception             ipv6: PMTU exceptions                   1
214         pmtu_ipv4_dscp_icmp_exception   ICMPv4 with DSCP and ECN: PMTU exceptions       1
215         pmtu_ipv4_dscp_udp_exception    UDPv4 with DSCP and ECN: PMTU exceptions        1
216         pmtu_ipv4_vxlan4_exception      IPv4 over vxlan4: PMTU exceptions       1
217         pmtu_ipv6_vxlan4_exception      IPv6 over vxlan4: PMTU exceptions       1
218         pmtu_ipv4_vxlan6_exception      IPv4 over vxlan6: PMTU exceptions       1
219         pmtu_ipv6_vxlan6_exception      IPv6 over vxlan6: PMTU exceptions       1
220         pmtu_ipv4_geneve4_exception     IPv4 over geneve4: PMTU exceptions      1
221         pmtu_ipv6_geneve4_exception     IPv6 over geneve4: PMTU exceptions      1
222         pmtu_ipv4_geneve6_exception     IPv4 over geneve6: PMTU exceptions      1
223         pmtu_ipv6_geneve6_exception     IPv6 over geneve6: PMTU exceptions      1
224         pmtu_ipv4_br_vxlan4_exception   IPv4, bridged vxlan4: PMTU exceptions   1
225         pmtu_ipv6_br_vxlan4_exception   IPv6, bridged vxlan4: PMTU exceptions   1
226         pmtu_ipv4_br_vxlan6_exception   IPv4, bridged vxlan6: PMTU exceptions   1
227         pmtu_ipv6_br_vxlan6_exception   IPv6, bridged vxlan6: PMTU exceptions   1
228         pmtu_ipv4_br_geneve4_exception  IPv4, bridged geneve4: PMTU exceptions  1
229         pmtu_ipv6_br_geneve4_exception  IPv6, bridged geneve4: PMTU exceptions  1
230         pmtu_ipv4_br_geneve6_exception  IPv4, bridged geneve6: PMTU exceptions  1
231         pmtu_ipv6_br_geneve6_exception  IPv6, bridged geneve6: PMTU exceptions  1
232         pmtu_ipv4_ovs_vxlan4_exception  IPv4, OVS vxlan4: PMTU exceptions       1
233         pmtu_ipv6_ovs_vxlan4_exception  IPv6, OVS vxlan4: PMTU exceptions       1
234         pmtu_ipv4_ovs_vxlan6_exception  IPv4, OVS vxlan6: PMTU exceptions       1
235         pmtu_ipv6_ovs_vxlan6_exception  IPv6, OVS vxlan6: PMTU exceptions       1
236         pmtu_ipv4_ovs_geneve4_exception IPv4, OVS geneve4: PMTU exceptions      1
237         pmtu_ipv6_ovs_geneve4_exception IPv6, OVS geneve4: PMTU exceptions      1
238         pmtu_ipv4_ovs_geneve6_exception IPv4, OVS geneve6: PMTU exceptions      1
239         pmtu_ipv6_ovs_geneve6_exception IPv6, OVS geneve6: PMTU exceptions      1
240         pmtu_ipv4_fou4_exception        IPv4 over fou4: PMTU exceptions         1
241         pmtu_ipv6_fou4_exception        IPv6 over fou4: PMTU exceptions         1
242         pmtu_ipv4_fou6_exception        IPv4 over fou6: PMTU exceptions         1
243         pmtu_ipv6_fou6_exception        IPv6 over fou6: PMTU exceptions         1
244         pmtu_ipv4_gue4_exception        IPv4 over gue4: PMTU exceptions         1
245         pmtu_ipv6_gue4_exception        IPv6 over gue4: PMTU exceptions         1
246         pmtu_ipv4_gue6_exception        IPv4 over gue6: PMTU exceptions         1
247         pmtu_ipv6_gue6_exception        IPv6 over gue6: PMTU exceptions         1
248         pmtu_ipv4_ipv4_exception        IPv4 over IPv4: PMTU exceptions         1
249         pmtu_ipv6_ipv4_exception        IPv6 over IPv4: PMTU exceptions         1
250         pmtu_ipv4_ipv6_exception        IPv4 over IPv6: PMTU exceptions         1
251         pmtu_ipv6_ipv6_exception        IPv6 over IPv6: PMTU exceptions         1
252         pmtu_vti6_exception             vti6: PMTU exceptions                   0
253         pmtu_vti4_exception             vti4: PMTU exceptions                   0
254         pmtu_vti6_udp_exception         vti6: PMTU exceptions (ESP-in-UDP)      0
255         pmtu_vti4_udp_exception         vti4: PMTU exceptions (ESP-in-UDP)      0
256         pmtu_vti6_udp_routed_exception  vti6: PMTU exceptions, routed (ESP-in-UDP)      0
257         pmtu_vti4_udp_routed_exception  vti4: PMTU exceptions, routed (ESP-in-UDP)      0
258         pmtu_vti4_default_mtu           vti4: default MTU assignment            0
259         pmtu_vti6_default_mtu           vti6: default MTU assignment            0
260         pmtu_vti4_link_add_mtu          vti4: MTU setting on link creation      0
261         pmtu_vti6_link_add_mtu          vti6: MTU setting on link creation      0
262         pmtu_vti6_link_change_mtu       vti6: MTU changes on link changes       0
263         cleanup_ipv4_exception          ipv4: cleanup of cached exceptions      1
264         cleanup_ipv6_exception          ipv6: cleanup of cached exceptions      1
265         list_flush_ipv4_exception       ipv4: list and flush cached exceptions  1
266         list_flush_ipv6_exception       ipv6: list and flush cached exceptions  1
267         pmtu_ipv4_route_change          ipv4: PMTU exception w/route replace    1
268         pmtu_ipv6_route_change          ipv6: PMTU exception w/route replace    1"
269
270 # Addressing and routing for tests with routers: four network segments, with
271 # index SEGMENT between 1 and 4, a common prefix (PREFIX4 or PREFIX6) and an
272 # identifier ID, which is 1 for hosts (A and B), 2 for routers (R1 and R2).
273 # Addresses are:
274 # - IPv4: PREFIX4.SEGMENT.ID (/24)
275 # - IPv6: PREFIX6:SEGMENT::ID (/64)
276 prefix4="10.0"
277 prefix6="fc00"
278 a_r1=1
279 a_r2=2
280 b_r1=3
281 b_r2=4
282 #       ns      peer    segment
283 routing_addrs="
284         A       R1      ${a_r1}
285         A       R2      ${a_r2}
286         B       R1      ${b_r1}
287         B       R2      ${b_r2}
288 "
289 # Traffic from A to B goes through R1 by default, and through R2, if destined to
290 # B's address on the b_r2 segment.
291 # Traffic from B to A goes through R1.
292 #       ns      destination             gateway
293 routes="
294         A       default                 ${prefix4}.${a_r1}.2
295         A       ${prefix4}.${b_r2}.1    ${prefix4}.${a_r2}.2
296         B       default                 ${prefix4}.${b_r1}.2
297
298         A       default                 ${prefix6}:${a_r1}::2
299         A       ${prefix6}:${b_r2}::1   ${prefix6}:${a_r2}::2
300         B       default                 ${prefix6}:${b_r1}::2
301 "
302 USE_NH="no"
303 #       ns      family  nh id      destination          gateway
304 nexthops="
305         A       4       41      ${prefix4}.${a_r1}.2    veth_A-R1
306         A       4       42      ${prefix4}.${a_r2}.2    veth_A-R2
307         B       4       41      ${prefix4}.${b_r1}.2    veth_B-R1
308
309         A       6       61      ${prefix6}:${a_r1}::2   veth_A-R1
310         A       6       62      ${prefix6}:${a_r2}::2   veth_A-R2
311         B       6       61      ${prefix6}:${b_r1}::2   veth_B-R1
312 "
313
314 # nexthop id correlates to id in nexthops config above
315 #       ns    family    prefix                  nh id
316 routes_nh="
317         A       4       default                 41
318         A       4       ${prefix4}.${b_r2}.1    42
319         B       4       default                 41
320
321         A       6       default                 61
322         A       6       ${prefix6}:${b_r2}::1   62
323         B       6       default                 61
324 "
325
326 policy_mark=0x04
327 rt_table=main
328
329 veth4_a_addr="192.168.1.1"
330 veth4_b_addr="192.168.1.2"
331 veth4_c_addr="192.168.2.10"
332 veth4_mask="24"
333 veth6_a_addr="fd00:1::a"
334 veth6_b_addr="fd00:1::b"
335 veth6_c_addr="fd00:2::c"
336 veth6_mask="64"
337
338 tunnel4_a_addr="192.168.2.1"
339 tunnel4_b_addr="192.168.2.2"
340 tunnel4_mask="24"
341 tunnel6_a_addr="fd00:2::a"
342 tunnel6_b_addr="fd00:2::b"
343 tunnel6_mask="64"
344
345 dummy6_0_prefix="fc00:1000::"
346 dummy6_1_prefix="fc00:1001::"
347 dummy6_mask="64"
348
349 err_buf=
350 tcpdump_pids=
351 nettest_pids=
352 socat_pids=
353 tmpoutfile=
354
355 err() {
356         err_buf="${err_buf}${1}
357 "
358 }
359
360 err_flush() {
361         echo -n "${err_buf}"
362         err_buf=
363 }
364
365 run_cmd() {
366         cmd="$*"
367
368         if [ "$VERBOSE" = "1" ]; then
369                 printf "    COMMAND: $cmd\n"
370         fi
371
372         out="$($cmd 2>&1)"
373         rc=$?
374         if [ "$VERBOSE" = "1" -a -n "$out" ]; then
375                 echo "    $out"
376                 echo
377         fi
378
379         return $rc
380 }
381
382 run_cmd_bg() {
383         cmd="$*"
384
385         if [ "$VERBOSE" = "1" ]; then
386                 printf "    COMMAND: %s &\n" "${cmd}"
387         fi
388
389         $cmd 2>&1 &
390 }
391
392 # Find the auto-generated name for this namespace
393 nsname() {
394         eval echo \$NS_$1
395 }
396
397 setup_fou_or_gue() {
398         outer="${1}"
399         inner="${2}"
400         encap="${3}"
401
402         if [ "${outer}" = "4" ]; then
403                 modprobe fou || return $ksft_skip
404                 a_addr="${prefix4}.${a_r1}.1"
405                 b_addr="${prefix4}.${b_r1}.1"
406                 if [ "${inner}" = "4" ]; then
407                         type="ipip"
408                         ipproto="4"
409                 else
410                         type="sit"
411                         ipproto="41"
412                 fi
413         else
414                 modprobe fou6 || return $ksft_skip
415                 a_addr="${prefix6}:${a_r1}::1"
416                 b_addr="${prefix6}:${b_r1}::1"
417                 if [ "${inner}" = "4" ]; then
418                         type="ip6tnl"
419                         mode="mode ipip6"
420                         ipproto="4 -6"
421                 else
422                         type="ip6tnl"
423                         mode="mode ip6ip6"
424                         ipproto="41 -6"
425                 fi
426         fi
427
428         run_cmd ${ns_a} ip fou add port 5555 ipproto ${ipproto} || return $ksft_skip
429         run_cmd ${ns_a} ip link add ${encap}_a type ${type} ${mode} local ${a_addr} remote ${b_addr} encap ${encap} encap-sport auto encap-dport 5556 || return $ksft_skip
430
431         run_cmd ${ns_b} ip fou add port 5556 ipproto ${ipproto}
432         run_cmd ${ns_b} ip link add ${encap}_b type ${type} ${mode} local ${b_addr} remote ${a_addr} encap ${encap} encap-sport auto encap-dport 5555
433
434         if [ "${inner}" = "4" ]; then
435                 run_cmd ${ns_a} ip addr add ${tunnel4_a_addr}/${tunnel4_mask} dev ${encap}_a
436                 run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ${encap}_b
437         else
438                 run_cmd ${ns_a} ip addr add ${tunnel6_a_addr}/${tunnel6_mask} dev ${encap}_a
439                 run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ${encap}_b
440         fi
441
442         run_cmd ${ns_a} ip link set ${encap}_a up
443         run_cmd ${ns_b} ip link set ${encap}_b up
444 }
445
446 setup_fou44() {
447         setup_fou_or_gue 4 4 fou
448 }
449
450 setup_fou46() {
451         setup_fou_or_gue 4 6 fou
452 }
453
454 setup_fou64() {
455         setup_fou_or_gue 6 4 fou
456 }
457
458 setup_fou66() {
459         setup_fou_or_gue 6 6 fou
460 }
461
462 setup_gue44() {
463         setup_fou_or_gue 4 4 gue
464 }
465
466 setup_gue46() {
467         setup_fou_or_gue 4 6 gue
468 }
469
470 setup_gue64() {
471         setup_fou_or_gue 6 4 gue
472 }
473
474 setup_gue66() {
475         setup_fou_or_gue 6 6 gue
476 }
477
478 setup_ipvX_over_ipvY() {
479         inner=${1}
480         outer=${2}
481
482         if [ "${outer}" -eq 4 ]; then
483                 a_addr="${prefix4}.${a_r1}.1"
484                 b_addr="${prefix4}.${b_r1}.1"
485                 if [ "${inner}" -eq 4 ]; then
486                         type="ipip"
487                         mode="ipip"
488                 else
489                         type="sit"
490                         mode="ip6ip"
491                 fi
492         else
493                 a_addr="${prefix6}:${a_r1}::1"
494                 b_addr="${prefix6}:${b_r1}::1"
495                 type="ip6tnl"
496                 if [ "${inner}" -eq 4 ]; then
497                         mode="ipip6"
498                 else
499                         mode="ip6ip6"
500                 fi
501         fi
502
503         run_cmd ${ns_a} ip link add ip_a type ${type} local ${a_addr} remote ${b_addr} mode ${mode} || return $ksft_skip
504         run_cmd ${ns_b} ip link add ip_b type ${type} local ${b_addr} remote ${a_addr} mode ${mode}
505
506         run_cmd ${ns_a} ip link set ip_a up
507         run_cmd ${ns_b} ip link set ip_b up
508
509         if [ "${inner}" = "4" ]; then
510                 run_cmd ${ns_a} ip addr add ${tunnel4_a_addr}/${tunnel4_mask} dev ip_a
511                 run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ip_b
512         else
513                 run_cmd ${ns_a} ip addr add ${tunnel6_a_addr}/${tunnel6_mask} dev ip_a
514                 run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ip_b
515         fi
516 }
517
518 setup_ip4ip4() {
519         setup_ipvX_over_ipvY 4 4
520 }
521
522 setup_ip6ip4() {
523         setup_ipvX_over_ipvY 6 4
524 }
525
526 setup_ip4ip6() {
527         setup_ipvX_over_ipvY 4 6
528 }
529
530 setup_ip6ip6() {
531         setup_ipvX_over_ipvY 6 6
532 }
533
534 setup_namespaces() {
535         setup_ns NS_A NS_B NS_C NS_R1 NS_R2
536         for n in ${NS_A} ${NS_B} ${NS_C} ${NS_R1} ${NS_R2}; do
537                 # Disable DAD, so that we don't have to wait to use the
538                 # configured IPv6 addresses
539                 ip netns exec ${n} sysctl -q net/ipv6/conf/default/accept_dad=0
540         done
541         ns_a="ip netns exec ${NS_A}"
542         ns_b="ip netns exec ${NS_B}"
543         ns_c="ip netns exec ${NS_C}"
544         ns_r1="ip netns exec ${NS_R1}"
545         ns_r2="ip netns exec ${NS_R2}"
546 }
547
548 setup_veth() {
549         run_cmd ${ns_a} ip link add veth_a type veth peer name veth_b || return 1
550         run_cmd ${ns_a} ip link set veth_b netns ${NS_B}
551
552         run_cmd ${ns_a} ip addr add ${veth4_a_addr}/${veth4_mask} dev veth_a
553         run_cmd ${ns_b} ip addr add ${veth4_b_addr}/${veth4_mask} dev veth_b
554
555         run_cmd ${ns_a} ip addr add ${veth6_a_addr}/${veth6_mask} dev veth_a
556         run_cmd ${ns_b} ip addr add ${veth6_b_addr}/${veth6_mask} dev veth_b
557
558         run_cmd ${ns_a} ip link set veth_a up
559         run_cmd ${ns_b} ip link set veth_b up
560 }
561
562 setup_vti() {
563         proto=${1}
564         veth_a_addr="${2}"
565         veth_b_addr="${3}"
566         vti_a_addr="${4}"
567         vti_b_addr="${5}"
568         vti_mask=${6}
569
570         [ ${proto} -eq 6 ] && vti_type="vti6" || vti_type="vti"
571
572         run_cmd ${ns_a} ip link add vti${proto}_a type ${vti_type} local ${veth_a_addr} remote ${veth_b_addr} key 10 || return 1
573         run_cmd ${ns_b} ip link add vti${proto}_b type ${vti_type} local ${veth_b_addr} remote ${veth_a_addr} key 10
574
575         run_cmd ${ns_a} ip addr add ${vti_a_addr}/${vti_mask} dev vti${proto}_a
576         run_cmd ${ns_b} ip addr add ${vti_b_addr}/${vti_mask} dev vti${proto}_b
577
578         run_cmd ${ns_a} ip link set vti${proto}_a up
579         run_cmd ${ns_b} ip link set vti${proto}_b up
580 }
581
582 setup_vti4() {
583         setup_vti 4 ${veth4_a_addr} ${veth4_b_addr} ${tunnel4_a_addr} ${tunnel4_b_addr} ${tunnel4_mask}
584 }
585
586 setup_vti6() {
587         setup_vti 6 ${veth6_a_addr} ${veth6_b_addr} ${tunnel6_a_addr} ${tunnel6_b_addr} ${tunnel6_mask}
588 }
589
590 setup_vti4routed() {
591         setup_vti 4 ${prefix4}.${a_r1}.1 ${prefix4}.${b_r1}.1 ${tunnel4_a_addr} ${tunnel4_b_addr} ${tunnel4_mask}
592 }
593
594 setup_vti6routed() {
595         setup_vti 6 ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 ${tunnel6_a_addr} ${tunnel6_b_addr} ${tunnel6_mask}
596 }
597
598 setup_vxlan_or_geneve() {
599         type="${1}"
600         a_addr="${2}"
601         b_addr="${3}"
602         opts="${4}"
603         br_if_a="${5}"
604
605         if [ "${type}" = "vxlan" ]; then
606                 opts="${opts} ttl 64 dstport 4789"
607                 opts_a="local ${a_addr}"
608                 opts_b="local ${b_addr}"
609         else
610                 opts_a=""
611                 opts_b=""
612         fi
613
614         run_cmd ${ns_a} ip link add ${type}_a type ${type} id 1 ${opts_a} remote ${b_addr} ${opts} || return 1
615         run_cmd ${ns_b} ip link add ${type}_b type ${type} id 1 ${opts_b} remote ${a_addr} ${opts}
616
617         if [ -n "${br_if_a}" ]; then
618                 run_cmd ${ns_a} ip addr add ${tunnel4_a_addr}/${tunnel4_mask} dev ${br_if_a}
619                 run_cmd ${ns_a} ip addr add ${tunnel6_a_addr}/${tunnel6_mask} dev ${br_if_a}
620                 run_cmd ${ns_a} ip link set ${type}_a master ${br_if_a}
621         else
622                 run_cmd ${ns_a} ip addr add ${tunnel4_a_addr}/${tunnel4_mask} dev ${type}_a
623                 run_cmd ${ns_a} ip addr add ${tunnel6_a_addr}/${tunnel6_mask} dev ${type}_a
624         fi
625
626         run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ${type}_b
627         run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ${type}_b
628
629         run_cmd ${ns_a} ip link set ${type}_a up
630         run_cmd ${ns_b} ip link set ${type}_b up
631 }
632
633 setup_geneve4() {
634         setup_vxlan_or_geneve geneve ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1  "df set"
635 }
636
637 setup_vxlan4() {
638         setup_vxlan_or_geneve vxlan  ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1  "df set"
639 }
640
641 setup_geneve6() {
642         setup_vxlan_or_geneve geneve ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 ""
643 }
644
645 setup_vxlan6() {
646         setup_vxlan_or_geneve vxlan  ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 ""
647 }
648
649 setup_bridged_geneve4() {
650         setup_vxlan_or_geneve geneve ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1  "df set" "br0"
651 }
652
653 setup_bridged_vxlan4() {
654         setup_vxlan_or_geneve vxlan  ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1  "df set" "br0"
655 }
656
657 setup_bridged_geneve6() {
658         setup_vxlan_or_geneve geneve ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 "" "br0"
659 }
660
661 setup_bridged_vxlan6() {
662         setup_vxlan_or_geneve vxlan  ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 "" "br0"
663 }
664
665 setup_xfrm() {
666         proto=${1}
667         veth_a_addr="${2}"
668         veth_b_addr="${3}"
669         encap=${4}
670
671         run_cmd ${ns_a} ip -${proto} xfrm state add src ${veth_a_addr} dst ${veth_b_addr} spi 0x1000 proto esp aead 'rfc4106(gcm(aes))' 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel ${encap} || return 1
672         run_cmd ${ns_a} ip -${proto} xfrm state add src ${veth_b_addr} dst ${veth_a_addr} spi 0x1001 proto esp aead 'rfc4106(gcm(aes))' 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel ${encap}
673         run_cmd ${ns_a} ip -${proto} xfrm policy add dir out mark 10 tmpl src ${veth_a_addr} dst ${veth_b_addr} proto esp mode tunnel
674         run_cmd ${ns_a} ip -${proto} xfrm policy add dir in mark 10 tmpl src ${veth_b_addr} dst ${veth_a_addr} proto esp mode tunnel
675
676         run_cmd ${ns_b} ip -${proto} xfrm state add src ${veth_a_addr} dst ${veth_b_addr} spi 0x1000 proto esp aead 'rfc4106(gcm(aes))' 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel ${encap}
677         run_cmd ${ns_b} ip -${proto} xfrm state add src ${veth_b_addr} dst ${veth_a_addr} spi 0x1001 proto esp aead 'rfc4106(gcm(aes))' 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel ${encap}
678         run_cmd ${ns_b} ip -${proto} xfrm policy add dir out mark 10 tmpl src ${veth_b_addr} dst ${veth_a_addr} proto esp mode tunnel
679         run_cmd ${ns_b} ip -${proto} xfrm policy add dir in mark 10 tmpl src ${veth_a_addr} dst ${veth_b_addr} proto esp mode tunnel
680 }
681
682 setup_nettest_xfrm() {
683         if ! which nettest >/dev/null; then
684                 PATH=$PWD:$PATH
685                 if ! which nettest >/dev/null; then
686                         echo "'nettest' command not found; skipping tests"
687                         return 1
688                 fi
689         fi
690
691         [ ${1} -eq 6 ] && proto="-6" || proto=""
692         port=${2}
693
694         run_cmd_bg "${ns_a}" nettest "${proto}" -q -D -s -x -p "${port}" -t 5
695         nettest_pids="${nettest_pids} $!"
696
697         run_cmd_bg "${ns_b}" nettest "${proto}" -q -D -s -x -p "${port}" -t 5
698         nettest_pids="${nettest_pids} $!"
699 }
700
701 setup_xfrm4() {
702         setup_xfrm 4 ${veth4_a_addr} ${veth4_b_addr}
703 }
704
705 setup_xfrm6() {
706         setup_xfrm 6 ${veth6_a_addr} ${veth6_b_addr}
707 }
708
709 setup_xfrm4udp() {
710         setup_xfrm 4 ${veth4_a_addr} ${veth4_b_addr} "encap espinudp 4500 4500 0.0.0.0" && \
711                 setup_nettest_xfrm 4 4500
712 }
713
714 setup_xfrm6udp() {
715         setup_xfrm 6 ${veth6_a_addr} ${veth6_b_addr} "encap espinudp 4500 4500 0.0.0.0" && \
716                 setup_nettest_xfrm 6 4500
717 }
718
719 setup_xfrm4udprouted() {
720         setup_xfrm 4 ${prefix4}.${a_r1}.1 ${prefix4}.${b_r1}.1 "encap espinudp 4500 4500 0.0.0.0" && \
721                 setup_nettest_xfrm 4 4500
722 }
723
724 setup_xfrm6udprouted() {
725         setup_xfrm 6 ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 "encap espinudp 4500 4500 0.0.0.0" && \
726                 setup_nettest_xfrm 6 4500
727 }
728
729 setup_routing_old() {
730         for i in ${routes}; do
731                 [ "${ns}" = "" ]        && ns="${i}"            && continue
732                 [ "${addr}" = "" ]      && addr="${i}"          && continue
733                 [ "${gw}" = "" ]        && gw="${i}"
734
735                 ns_name="$(nsname ${ns})"
736
737                 ip -n "${ns_name}" route add "${addr}" table "${rt_table}" via "${gw}"
738
739                 ns=""; addr=""; gw=""
740         done
741 }
742
743 setup_routing_new() {
744         for i in ${nexthops}; do
745                 [ "${ns}" = "" ]        && ns="${i}"            && continue
746                 [ "${fam}" = "" ]       && fam="${i}"           && continue
747                 [ "${nhid}" = "" ]      && nhid="${i}"          && continue
748                 [ "${gw}" = "" ]        && gw="${i}"            && continue
749                 [ "${dev}" = "" ]       && dev="${i}"
750
751                 ns_name="$(nsname ${ns})"
752
753                 ip -n ${ns_name} -${fam} nexthop add id ${nhid} via ${gw} dev ${dev}
754
755                 ns=""; fam=""; nhid=""; gw=""; dev=""
756
757         done
758
759         for i in ${routes_nh}; do
760                 [ "${ns}" = "" ]        && ns="${i}"            && continue
761                 [ "${fam}" = "" ]       && fam="${i}"           && continue
762                 [ "${addr}" = "" ]      && addr="${i}"          && continue
763                 [ "${nhid}" = "" ]      && nhid="${i}"
764
765                 ns_name="$(nsname ${ns})"
766
767                 ip -n "${ns_name}" -"${fam}" route add "${addr}" table "${rt_table}" nhid "${nhid}"
768
769                 ns=""; fam=""; addr=""; nhid=""
770         done
771 }
772
773 setup_routing() {
774         for i in ${NS_R1} ${NS_R2}; do
775                 ip netns exec ${i} sysctl -q net/ipv4/ip_forward=1
776                 ip netns exec ${i} sysctl -q net/ipv6/conf/all/forwarding=1
777         done
778
779         for i in ${routing_addrs}; do
780                 [ "${ns}" = "" ]        && ns="${i}"            && continue
781                 [ "${peer}" = "" ]      && peer="${i}"          && continue
782                 [ "${segment}" = "" ]   && segment="${i}"
783
784                 ns_name="$(nsname ${ns})"
785                 peer_name="$(nsname ${peer})"
786                 if="veth_${ns}-${peer}"
787                 ifpeer="veth_${peer}-${ns}"
788
789                 # Create veth links
790                 ip link add ${if} up netns ${ns_name} type veth peer name ${ifpeer} netns ${peer_name} || return 1
791                 ip -n ${peer_name} link set dev ${ifpeer} up
792
793                 # Add addresses
794                 ip -n ${ns_name}   addr add ${prefix4}.${segment}.1/24  dev ${if}
795                 ip -n ${ns_name}   addr add ${prefix6}:${segment}::1/64 dev ${if}
796
797                 ip -n ${peer_name} addr add ${prefix4}.${segment}.2/24  dev ${ifpeer}
798                 ip -n ${peer_name} addr add ${prefix6}:${segment}::2/64 dev ${ifpeer}
799
800                 ns=""; peer=""; segment=""
801         done
802
803         if [ "$USE_NH" = "yes" ]; then
804                 setup_routing_new
805         else
806                 setup_routing_old
807         fi
808
809         return 0
810 }
811
812 setup_policy_routing() {
813         setup_routing
814
815         ip -netns "${NS_A}" -4 rule add dsfield "${policy_mark}" \
816                 table "${rt_table}"
817
818         # Set the IPv4 Don't Fragment bit with tc, since socat doesn't seem to
819         # have an option do to it.
820         tc -netns "${NS_A}" qdisc replace dev veth_A-R1 root prio
821         tc -netns "${NS_A}" qdisc replace dev veth_A-R2 root prio
822         tc -netns "${NS_A}" filter add dev veth_A-R1                      \
823                 protocol ipv4 flower ip_proto udp                         \
824                 action pedit ex munge ip df set 0x40 pipe csum ip and udp
825         tc -netns "${NS_A}" filter add dev veth_A-R2                      \
826                 protocol ipv4 flower ip_proto udp                         \
827                 action pedit ex munge ip df set 0x40 pipe csum ip and udp
828 }
829
830 setup_bridge() {
831         run_cmd ${ns_a} ip link add br0 type bridge || return $ksft_skip
832         run_cmd ${ns_a} ip link set br0 up
833
834         run_cmd ${ns_c} ip link add veth_C-A type veth peer name veth_A-C
835         run_cmd ${ns_c} ip link set veth_A-C netns ${NS_A}
836
837         run_cmd ${ns_a} ip link set veth_A-C up
838         run_cmd ${ns_c} ip link set veth_C-A up
839         run_cmd ${ns_c} ip addr add ${veth4_c_addr}/${veth4_mask} dev veth_C-A
840         run_cmd ${ns_c} ip addr add ${veth6_c_addr}/${veth6_mask} dev veth_C-A
841         run_cmd ${ns_a} ip link set veth_A-C master br0
842 }
843
844 setup_ovs_vxlan_or_geneve() {
845         type="${1}"
846         a_addr="${2}"
847         b_addr="${3}"
848
849         if [ "${type}" = "vxlan" ]; then
850                 opts="${opts} ttl 64 dstport 4789"
851                 opts_b="local ${b_addr}"
852         fi
853
854         run_cmd ovs-vsctl add-port ovs_br0 ${type}_a -- \
855                 set interface ${type}_a type=${type} \
856                 options:remote_ip=${b_addr} options:key=1 options:csum=true || return 1
857
858         run_cmd ${ns_b} ip link add ${type}_b type ${type} id 1 ${opts_b} remote ${a_addr} ${opts} || return 1
859
860         run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ${type}_b
861         run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ${type}_b
862
863         run_cmd ${ns_b} ip link set ${type}_b up
864 }
865
866 setup_ovs_geneve4() {
867         setup_ovs_vxlan_or_geneve geneve ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1
868 }
869
870 setup_ovs_vxlan4() {
871         setup_ovs_vxlan_or_geneve vxlan  ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1
872 }
873
874 setup_ovs_geneve6() {
875         setup_ovs_vxlan_or_geneve geneve ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1
876 }
877
878 setup_ovs_vxlan6() {
879         setup_ovs_vxlan_or_geneve vxlan  ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1
880 }
881
882 setup_ovs_bridge() {
883         run_cmd ovs-vsctl add-br ovs_br0 || return $ksft_skip
884         run_cmd ip link set ovs_br0 up
885
886         run_cmd ${ns_c} ip link add veth_C-A type veth peer name veth_A-C
887         run_cmd ${ns_c} ip link set veth_A-C netns 1
888
889         run_cmd         ip link set veth_A-C up
890         run_cmd ${ns_c} ip link set veth_C-A up
891         run_cmd ${ns_c} ip addr add ${veth4_c_addr}/${veth4_mask} dev veth_C-A
892         run_cmd ${ns_c} ip addr add ${veth6_c_addr}/${veth6_mask} dev veth_C-A
893         run_cmd ovs-vsctl add-port ovs_br0 veth_A-C
894
895         # Move veth_A-R1 to init
896         run_cmd ${ns_a} ip link set veth_A-R1 netns 1
897         run_cmd ip addr add ${prefix4}.${a_r1}.1/${veth4_mask} dev veth_A-R1
898         run_cmd ip addr add ${prefix6}:${a_r1}::1/${veth6_mask} dev veth_A-R1
899         run_cmd ip link set veth_A-R1 up
900         run_cmd ip route add ${prefix4}.${b_r1}.1 via ${prefix4}.${a_r1}.2
901         run_cmd ip route add ${prefix6}:${b_r1}::1 via ${prefix6}:${a_r1}::2
902 }
903
904 setup() {
905         [ "$(id -u)" -ne 0 ] && echo "  need to run as root" && return $ksft_skip
906
907         for arg do
908                 eval setup_${arg} || { echo "  ${arg} not supported"; return 1; }
909         done
910 }
911
912 trace() {
913         [ $TRACING -eq 0 ] && return
914
915         for arg do
916                 [ "${ns_cmd}" = "" ] && ns_cmd="${arg}" && continue
917                 ${ns_cmd} tcpdump --immediate-mode -s 0 -i "${arg}" -w "${name}_${arg}.pcap" 2> /dev/null &
918                 tcpdump_pids="${tcpdump_pids} $!"
919                 ns_cmd=
920         done
921         sleep 1
922 }
923
924 cleanup() {
925         for pid in ${tcpdump_pids}; do
926                 kill ${pid}
927         done
928         tcpdump_pids=
929
930         for pid in ${nettest_pids}; do
931                 kill ${pid}
932         done
933         nettest_pids=
934
935         for pid in ${socat_pids}; do
936                 kill "${pid}"
937         done
938         socat_pids=
939
940         cleanup_all_ns
941
942         ip link del veth_A-C                    2>/dev/null
943         ip link del veth_A-R1                   2>/dev/null
944         ovs-vsctl --if-exists del-port vxlan_a  2>/dev/null
945         ovs-vsctl --if-exists del-br ovs_br0    2>/dev/null
946         rm -f "$tmpoutfile"
947 }
948
949 mtu() {
950         ns_cmd="${1}"
951         dev="${2}"
952         mtu="${3}"
953
954         ${ns_cmd} ip link set dev ${dev} mtu ${mtu}
955 }
956
957 mtu_parse() {
958         input="${1}"
959
960         next=0
961         for i in ${input}; do
962                 [ ${next} -eq 1 -a "${i}" = "lock" ] && next=2 && continue
963                 [ ${next} -eq 1 ] && echo "${i}" && return
964                 [ ${next} -eq 2 ] && echo "lock ${i}" && return
965                 [ "${i}" = "mtu" ] && next=1
966         done
967 }
968
969 link_get() {
970         ns_cmd="${1}"
971         name="${2}"
972
973         ${ns_cmd} ip link show dev "${name}"
974 }
975
976 link_get_mtu() {
977         ns_cmd="${1}"
978         name="${2}"
979
980         mtu_parse "$(link_get "${ns_cmd}" ${name})"
981 }
982
983 route_get_dst_exception() {
984         ns_cmd="${1}"
985         dst="${2}"
986         dsfield="${3}"
987
988         if [ -z "${dsfield}" ]; then
989                 dsfield=0
990         fi
991
992         ${ns_cmd} ip route get "${dst}" dsfield "${dsfield}"
993 }
994
995 route_get_dst_pmtu_from_exception() {
996         ns_cmd="${1}"
997         dst="${2}"
998         dsfield="${3}"
999
1000         mtu_parse "$(route_get_dst_exception "${ns_cmd}" "${dst}" "${dsfield}")"
1001 }
1002
1003 check_pmtu_value() {
1004         expected="${1}"
1005         value="${2}"
1006         event="${3}"
1007
1008         [ "${expected}" = "any" ] && [ -n "${value}" ] && return 0
1009         [ "${value}" = "${expected}" ] && return 0
1010         [ -z "${value}" ] &&    err "  PMTU exception wasn't created after ${event}" && return 1
1011         [ -z "${expected}" ] && err "  PMTU exception shouldn't exist after ${event}" && return 1
1012         err "  found PMTU exception with incorrect MTU ${value}, expected ${expected}, after ${event}"
1013         return 1
1014 }
1015
1016 test_pmtu_ipvX() {
1017         family=${1}
1018
1019         setup namespaces routing || return $ksft_skip
1020         trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
1021               "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
1022               "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
1023               "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
1024
1025         if [ ${family} -eq 4 ]; then
1026                 ping=ping
1027                 dst1="${prefix4}.${b_r1}.1"
1028                 dst2="${prefix4}.${b_r2}.1"
1029         else
1030                 ping=${ping6}
1031                 dst1="${prefix6}:${b_r1}::1"
1032                 dst2="${prefix6}:${b_r2}::1"
1033         fi
1034
1035         # Set up initial MTU values
1036         mtu "${ns_a}"  veth_A-R1 2000
1037         mtu "${ns_r1}" veth_R1-A 2000
1038         mtu "${ns_r1}" veth_R1-B 1400
1039         mtu "${ns_b}"  veth_B-R1 1400
1040
1041         mtu "${ns_a}"  veth_A-R2 2000
1042         mtu "${ns_r2}" veth_R2-A 2000
1043         mtu "${ns_r2}" veth_R2-B 1500
1044         mtu "${ns_b}"  veth_B-R2 1500
1045
1046         # Create route exceptions
1047         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst1}
1048         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst2}
1049
1050         # Check that exceptions have been created with the correct PMTU
1051         pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
1052         check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
1053         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1054         check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
1055
1056         # Decrease local MTU below PMTU, check for PMTU decrease in route exception
1057         mtu "${ns_a}"  veth_A-R1 1300
1058         mtu "${ns_r1}" veth_R1-A 1300
1059         pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
1060         check_pmtu_value "1300" "${pmtu_1}" "decreasing local MTU" || return 1
1061         # Second exception shouldn't be modified
1062         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1063         check_pmtu_value "1500" "${pmtu_2}" "changing local MTU on a link not on this path" || return 1
1064
1065         # Increase MTU, check for PMTU increase in route exception
1066         mtu "${ns_a}"  veth_A-R1 1700
1067         mtu "${ns_r1}" veth_R1-A 1700
1068         pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
1069         check_pmtu_value "1700" "${pmtu_1}" "increasing local MTU" || return 1
1070         # Second exception shouldn't be modified
1071         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1072         check_pmtu_value "1500" "${pmtu_2}" "changing local MTU on a link not on this path" || return 1
1073
1074         # Skip PMTU locking tests for IPv6
1075         [ $family -eq 6 ] && return 0
1076
1077         # Decrease remote MTU on path via R2, get new exception
1078         mtu "${ns_r2}" veth_R2-B 400
1079         mtu "${ns_b}"  veth_B-R2 400
1080         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1400 ${dst2}
1081         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1082         check_pmtu_value "lock 552" "${pmtu_2}" "exceeding MTU, with MTU < min_pmtu" || return 1
1083
1084         # Decrease local MTU below PMTU
1085         mtu "${ns_a}"  veth_A-R2 500
1086         mtu "${ns_r2}" veth_R2-A 500
1087         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1088         check_pmtu_value "500" "${pmtu_2}" "decreasing local MTU" || return 1
1089
1090         # Increase local MTU
1091         mtu "${ns_a}"  veth_A-R2 1500
1092         mtu "${ns_r2}" veth_R2-A 1500
1093         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1094         check_pmtu_value "1500" "${pmtu_2}" "increasing local MTU" || return 1
1095
1096         # Get new exception
1097         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1400 ${dst2}
1098         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1099         check_pmtu_value "lock 552" "${pmtu_2}" "exceeding MTU, with MTU < min_pmtu" || return 1
1100 }
1101
1102 test_pmtu_ipv4_exception() {
1103         test_pmtu_ipvX 4
1104 }
1105
1106 test_pmtu_ipv6_exception() {
1107         test_pmtu_ipvX 6
1108 }
1109
1110 test_pmtu_ipv4_dscp_icmp_exception() {
1111         rt_table=100
1112
1113         setup namespaces policy_routing || return $ksft_skip
1114         trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
1115               "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
1116               "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
1117               "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
1118
1119         # Set up initial MTU values
1120         mtu "${ns_a}"  veth_A-R1 2000
1121         mtu "${ns_r1}" veth_R1-A 2000
1122         mtu "${ns_r1}" veth_R1-B 1400
1123         mtu "${ns_b}"  veth_B-R1 1400
1124
1125         mtu "${ns_a}"  veth_A-R2 2000
1126         mtu "${ns_r2}" veth_R2-A 2000
1127         mtu "${ns_r2}" veth_R2-B 1500
1128         mtu "${ns_b}"  veth_B-R2 1500
1129
1130         len=$((2000 - 20 - 8)) # Fills MTU of veth_A-R1
1131
1132         dst1="${prefix4}.${b_r1}.1"
1133         dst2="${prefix4}.${b_r2}.1"
1134
1135         # Create route exceptions
1136         dsfield=${policy_mark} # No ECN bit set (Not-ECT)
1137         run_cmd "${ns_a}" ping -q -M want -Q "${dsfield}" -c 1 -w 1 -s "${len}" "${dst1}"
1138
1139         dsfield=$(printf "%#x" $((policy_mark + 0x02))) # ECN=2 (ECT(0))
1140         run_cmd "${ns_a}" ping -q -M want -Q "${dsfield}" -c 1 -w 1 -s "${len}" "${dst2}"
1141
1142         # Check that exceptions have been created with the correct PMTU
1143         pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst1}" "${policy_mark}")"
1144         check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
1145
1146         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst2}" "${policy_mark}")"
1147         check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
1148 }
1149
1150 test_pmtu_ipv4_dscp_udp_exception() {
1151         rt_table=100
1152
1153         if ! which socat > /dev/null 2>&1; then
1154                 echo "'socat' command not found; skipping tests"
1155                 return $ksft_skip
1156         fi
1157
1158         setup namespaces policy_routing || return $ksft_skip
1159         trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
1160               "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
1161               "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
1162               "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
1163
1164         # Set up initial MTU values
1165         mtu "${ns_a}"  veth_A-R1 2000
1166         mtu "${ns_r1}" veth_R1-A 2000
1167         mtu "${ns_r1}" veth_R1-B 1400
1168         mtu "${ns_b}"  veth_B-R1 1400
1169
1170         mtu "${ns_a}"  veth_A-R2 2000
1171         mtu "${ns_r2}" veth_R2-A 2000
1172         mtu "${ns_r2}" veth_R2-B 1500
1173         mtu "${ns_b}"  veth_B-R2 1500
1174
1175         len=$((2000 - 20 - 8)) # Fills MTU of veth_A-R1
1176
1177         dst1="${prefix4}.${b_r1}.1"
1178         dst2="${prefix4}.${b_r2}.1"
1179
1180         # Create route exceptions
1181         run_cmd_bg "${ns_b}" socat UDP-LISTEN:50000 OPEN:/dev/null,wronly=1
1182         socat_pids="${socat_pids} $!"
1183
1184         dsfield=${policy_mark} # No ECN bit set (Not-ECT)
1185         run_cmd "${ns_a}" socat OPEN:/dev/zero,rdonly=1,readbytes="${len}" \
1186                 UDP:"${dst1}":50000,tos="${dsfield}"
1187
1188         dsfield=$(printf "%#x" $((policy_mark + 0x02))) # ECN=2 (ECT(0))
1189         run_cmd "${ns_a}" socat OPEN:/dev/zero,rdonly=1,readbytes="${len}" \
1190                 UDP:"${dst2}":50000,tos="${dsfield}"
1191
1192         # Check that exceptions have been created with the correct PMTU
1193         pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst1}" "${policy_mark}")"
1194         check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
1195         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst2}" "${policy_mark}")"
1196         check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
1197 }
1198
1199 test_pmtu_ipvX_over_vxlanY_or_geneveY_exception() {
1200         type=${1}
1201         family=${2}
1202         outer_family=${3}
1203         ll_mtu=4000
1204
1205         if [ ${outer_family} -eq 4 ]; then
1206                 setup namespaces routing ${type}4 || return $ksft_skip
1207                 #                      IPv4 header   UDP header   VXLAN/GENEVE header   Ethernet header
1208                 exp_mtu=$((${ll_mtu} - 20          - 8          - 8                   - 14))
1209         else
1210                 setup namespaces routing ${type}6 || return $ksft_skip
1211                 #                      IPv6 header   UDP header   VXLAN/GENEVE header   Ethernet header
1212                 exp_mtu=$((${ll_mtu} - 40          - 8          - 8                   - 14))
1213         fi
1214
1215         trace "${ns_a}" ${type}_a    "${ns_b}"  ${type}_b \
1216               "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1217               "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1218
1219         if [ ${family} -eq 4 ]; then
1220                 ping=ping
1221                 dst=${tunnel4_b_addr}
1222         else
1223                 ping=${ping6}
1224                 dst=${tunnel6_b_addr}
1225         fi
1226
1227         # Create route exception by exceeding link layer MTU
1228         mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1229         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1230         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1231         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1232
1233         mtu "${ns_a}" ${type}_a $((${ll_mtu} + 1000))
1234         mtu "${ns_b}" ${type}_b $((${ll_mtu} + 1000))
1235         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
1236
1237         # Check that exception was created
1238         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1239         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ${type} interface"
1240 }
1241
1242 test_pmtu_ipv4_vxlan4_exception() {
1243         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  4 4
1244 }
1245
1246 test_pmtu_ipv6_vxlan4_exception() {
1247         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  6 4
1248 }
1249
1250 test_pmtu_ipv4_geneve4_exception() {
1251         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 4 4
1252 }
1253
1254 test_pmtu_ipv6_geneve4_exception() {
1255         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 6 4
1256 }
1257
1258 test_pmtu_ipv4_vxlan6_exception() {
1259         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  4 6
1260 }
1261
1262 test_pmtu_ipv6_vxlan6_exception() {
1263         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  6 6
1264 }
1265
1266 test_pmtu_ipv4_geneve6_exception() {
1267         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 4 6
1268 }
1269
1270 test_pmtu_ipv6_geneve6_exception() {
1271         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 6 6
1272 }
1273
1274 test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception() {
1275         type=${1}
1276         family=${2}
1277         outer_family=${3}
1278         ll_mtu=4000
1279
1280         if [ ${outer_family} -eq 4 ]; then
1281                 setup namespaces routing bridge bridged_${type}4 || return $ksft_skip
1282                 #                      IPv4 header   UDP header   VXLAN/GENEVE header   Ethernet header
1283                 exp_mtu=$((${ll_mtu} - 20          - 8          - 8                   - 14))
1284         else
1285                 setup namespaces routing bridge bridged_${type}6 || return $ksft_skip
1286                 #                      IPv6 header   UDP header   VXLAN/GENEVE header   Ethernet header
1287                 exp_mtu=$((${ll_mtu} - 40          - 8          - 8                   - 14))
1288         fi
1289
1290         trace "${ns_a}" ${type}_a    "${ns_b}"  ${type}_b \
1291               "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1292               "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B \
1293               "${ns_a}" br0          "${ns_a}"  veth-A-C  \
1294               "${ns_c}" veth_C-A
1295
1296         if [ ${family} -eq 4 ]; then
1297                 ping=ping
1298                 dst=${tunnel4_b_addr}
1299         else
1300                 ping=${ping6}
1301                 dst=${tunnel6_b_addr}
1302         fi
1303
1304         # Create route exception by exceeding link layer MTU
1305         mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1306         mtu "${ns_a}"  br0       $((${ll_mtu} + 1000))
1307         mtu "${ns_a}"  veth_A-C  $((${ll_mtu} + 1000))
1308         mtu "${ns_c}"  veth_C-A  $((${ll_mtu} + 1000))
1309         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1310         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1311         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1312
1313         mtu "${ns_a}" ${type}_a $((${ll_mtu} + 1000))
1314         mtu "${ns_b}" ${type}_b $((${ll_mtu} + 1000))
1315
1316         run_cmd ${ns_c} ${ping} -q -M want -i 0.1 -c 10 -s $((${ll_mtu} + 500)) ${dst} || return 1
1317         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1  -s $((${ll_mtu} + 500)) ${dst} || return 1
1318
1319         # Check that exceptions were created
1320         pmtu="$(route_get_dst_pmtu_from_exception "${ns_c}" ${dst})"
1321         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on bridged ${type} interface"
1322         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1323         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on locally bridged ${type} interface"
1324
1325         tmpoutfile=$(mktemp)
1326
1327         # Flush Exceptions, retry with TCP
1328         run_cmd ${ns_a} ip route flush cached ${dst}
1329         run_cmd ${ns_b} ip route flush cached ${dst}
1330         run_cmd ${ns_c} ip route flush cached ${dst}
1331
1332         for target in "${ns_a}" "${ns_c}" ; do
1333                 if [ ${family} -eq 4 ]; then
1334                         TCPDST=TCP:${dst}:50000
1335                 else
1336                         TCPDST="TCP:[${dst}]:50000"
1337                 fi
1338                 ${ns_b} socat -T 3 -u -6 TCP-LISTEN:50000 STDOUT > $tmpoutfile &
1339
1340                 sleep 1
1341
1342                 dd if=/dev/zero status=none bs=1M count=1 | ${target} socat -T 3 -u STDIN $TCPDST,connect-timeout=3
1343
1344                 size=$(du -sb $tmpoutfile)
1345                 size=${size%%/tmp/*}
1346
1347                 [ $size -ne 1048576 ] && err "File size $size mismatches exepcted value in locally bridged vxlan test" && return 1
1348         done
1349
1350         rm -f "$tmpoutfile"
1351
1352         # Check that exceptions were created
1353         pmtu="$(route_get_dst_pmtu_from_exception "${ns_c}" ${dst})"
1354         check_pmtu_value ${exp_mtu} "${pmtu}" "tcp: exceeding link layer MTU on bridged ${type} interface"
1355         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1356         check_pmtu_value ${exp_mtu} "${pmtu}" "tcp exceeding link layer MTU on locally bridged ${type} interface"
1357 }
1358
1359 test_pmtu_ipv4_br_vxlan4_exception() {
1360         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  4 4
1361 }
1362
1363 test_pmtu_ipv6_br_vxlan4_exception() {
1364         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  6 4
1365 }
1366
1367 test_pmtu_ipv4_br_geneve4_exception() {
1368         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 4 4
1369 }
1370
1371 test_pmtu_ipv6_br_geneve4_exception() {
1372         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 6 4
1373 }
1374
1375 test_pmtu_ipv4_br_vxlan6_exception() {
1376         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  4 6
1377 }
1378
1379 test_pmtu_ipv6_br_vxlan6_exception() {
1380         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  6 6
1381 }
1382
1383 test_pmtu_ipv4_br_geneve6_exception() {
1384         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 4 6
1385 }
1386
1387 test_pmtu_ipv6_br_geneve6_exception() {
1388         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 6 6
1389 }
1390
1391 test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception() {
1392         type=${1}
1393         family=${2}
1394         outer_family=${3}
1395         ll_mtu=4000
1396
1397         if [ ${outer_family} -eq 4 ]; then
1398                 setup namespaces routing ovs_bridge ovs_${type}4 || return $ksft_skip
1399                 #                      IPv4 header   UDP header   VXLAN/GENEVE header   Ethernet header
1400                 exp_mtu=$((${ll_mtu} - 20          - 8          - 8                   - 14))
1401         else
1402                 setup namespaces routing ovs_bridge ovs_${type}6 || return $ksft_skip
1403                 #                      IPv6 header   UDP header   VXLAN/GENEVE header   Ethernet header
1404                 exp_mtu=$((${ll_mtu} - 40          - 8          - 8                   - 14))
1405         fi
1406
1407         if [ "${type}" = "vxlan" ]; then
1408                 tun_a="vxlan_sys_4789"
1409         elif [ "${type}" = "geneve" ]; then
1410                 tun_a="genev_sys_6081"
1411         fi
1412
1413         trace ""        "${tun_a}"  "${ns_b}"  ${type}_b \
1414               ""        veth_A-R1   "${ns_r1}" veth_R1-A \
1415               "${ns_b}" veth_B-R1   "${ns_r1}" veth_R1-B \
1416               ""        ovs_br0     ""         veth-A-C  \
1417               "${ns_c}" veth_C-A
1418
1419         if [ ${family} -eq 4 ]; then
1420                 ping=ping
1421                 dst=${tunnel4_b_addr}
1422         else
1423                 ping=${ping6}
1424                 dst=${tunnel6_b_addr}
1425         fi
1426
1427         # Create route exception by exceeding link layer MTU
1428         mtu ""         veth_A-R1 $((${ll_mtu} + 1000))
1429         mtu ""         ovs_br0   $((${ll_mtu} + 1000))
1430         mtu ""         veth_A-C  $((${ll_mtu} + 1000))
1431         mtu "${ns_c}"  veth_C-A  $((${ll_mtu} + 1000))
1432         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1433         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1434         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1435
1436         mtu ""        ${tun_a}  $((${ll_mtu} + 1000))
1437         mtu "${ns_b}" ${type}_b $((${ll_mtu} + 1000))
1438
1439         run_cmd ${ns_c} ${ping} -q -M want -i 0.1 -c 20 -s $((${ll_mtu} + 500)) ${dst} || return 1
1440
1441         # Check that exceptions were created
1442         pmtu="$(route_get_dst_pmtu_from_exception "${ns_c}" ${dst})"
1443         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on Open vSwitch ${type} interface"
1444 }
1445
1446 test_pmtu_ipv4_ovs_vxlan4_exception() {
1447         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  4 4
1448 }
1449
1450 test_pmtu_ipv6_ovs_vxlan4_exception() {
1451         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  6 4
1452 }
1453
1454 test_pmtu_ipv4_ovs_geneve4_exception() {
1455         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 4 4
1456 }
1457
1458 test_pmtu_ipv6_ovs_geneve4_exception() {
1459         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 6 4
1460 }
1461
1462 test_pmtu_ipv4_ovs_vxlan6_exception() {
1463         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  4 6
1464 }
1465
1466 test_pmtu_ipv6_ovs_vxlan6_exception() {
1467         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  6 6
1468 }
1469
1470 test_pmtu_ipv4_ovs_geneve6_exception() {
1471         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 4 6
1472 }
1473
1474 test_pmtu_ipv6_ovs_geneve6_exception() {
1475         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 6 6
1476 }
1477
1478 test_pmtu_ipvX_over_fouY_or_gueY() {
1479         inner_family=${1}
1480         outer_family=${2}
1481         encap=${3}
1482         ll_mtu=4000
1483
1484         setup namespaces routing ${encap}${outer_family}${inner_family} || return $ksft_skip
1485         trace "${ns_a}" ${encap}_a   "${ns_b}"  ${encap}_b \
1486               "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1487               "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1488
1489         if [ ${inner_family} -eq 4 ]; then
1490                 ping=ping
1491                 dst=${tunnel4_b_addr}
1492         else
1493                 ping=${ping6}
1494                 dst=${tunnel6_b_addr}
1495         fi
1496
1497         if [ "${encap}" = "gue" ]; then
1498                 encap_overhead=4
1499         else
1500                 encap_overhead=0
1501         fi
1502
1503         if [ ${outer_family} -eq 4 ]; then
1504                 #                      IPv4 header   UDP header
1505                 exp_mtu=$((${ll_mtu} - 20          - 8         - ${encap_overhead}))
1506         else
1507                 #                      IPv6 header   Option 4   UDP header
1508                 exp_mtu=$((${ll_mtu} - 40          - 8        - 8       - ${encap_overhead}))
1509         fi
1510
1511         # Create route exception by exceeding link layer MTU
1512         mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1513         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1514         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1515         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1516
1517         mtu "${ns_a}" ${encap}_a $((${ll_mtu} + 1000))
1518         mtu "${ns_b}" ${encap}_b $((${ll_mtu} + 1000))
1519         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
1520
1521         # Check that exception was created
1522         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1523         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ${encap} interface"
1524 }
1525
1526 test_pmtu_ipv4_fou4_exception() {
1527         test_pmtu_ipvX_over_fouY_or_gueY 4 4 fou
1528 }
1529
1530 test_pmtu_ipv6_fou4_exception() {
1531         test_pmtu_ipvX_over_fouY_or_gueY 6 4 fou
1532 }
1533
1534 test_pmtu_ipv4_fou6_exception() {
1535         test_pmtu_ipvX_over_fouY_or_gueY 4 6 fou
1536 }
1537
1538 test_pmtu_ipv6_fou6_exception() {
1539         test_pmtu_ipvX_over_fouY_or_gueY 6 6 fou
1540 }
1541
1542 test_pmtu_ipv4_gue4_exception() {
1543         test_pmtu_ipvX_over_fouY_or_gueY 4 4 gue
1544 }
1545
1546 test_pmtu_ipv6_gue4_exception() {
1547         test_pmtu_ipvX_over_fouY_or_gueY 6 4 gue
1548 }
1549
1550 test_pmtu_ipv4_gue6_exception() {
1551         test_pmtu_ipvX_over_fouY_or_gueY 4 6 gue
1552 }
1553
1554 test_pmtu_ipv6_gue6_exception() {
1555         test_pmtu_ipvX_over_fouY_or_gueY 6 6 gue
1556 }
1557
1558 test_pmtu_ipvX_over_ipvY_exception() {
1559         inner=${1}
1560         outer=${2}
1561         ll_mtu=4000
1562
1563         setup namespaces routing ip${inner}ip${outer} || return $ksft_skip
1564
1565         trace "${ns_a}" ip_a         "${ns_b}"  ip_b  \
1566               "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1567               "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1568
1569         if [ ${inner} -eq 4 ]; then
1570                 ping=ping
1571                 dst=${tunnel4_b_addr}
1572         else
1573                 ping=${ping6}
1574                 dst=${tunnel6_b_addr}
1575         fi
1576
1577         if [ ${outer} -eq 4 ]; then
1578                 #                      IPv4 header
1579                 exp_mtu=$((${ll_mtu} - 20))
1580         else
1581                 #                      IPv6 header   Option 4
1582                 exp_mtu=$((${ll_mtu} - 40          - 8))
1583         fi
1584
1585         # Create route exception by exceeding link layer MTU
1586         mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1587         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1588         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1589         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1590
1591         mtu "${ns_a}" ip_a $((${ll_mtu} + 1000)) || return
1592         mtu "${ns_b}" ip_b $((${ll_mtu} + 1000)) || return
1593         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
1594
1595         # Check that exception was created
1596         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1597         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ip${inner}ip${outer} interface"
1598 }
1599
1600 test_pmtu_ipv4_ipv4_exception() {
1601         test_pmtu_ipvX_over_ipvY_exception 4 4
1602 }
1603
1604 test_pmtu_ipv6_ipv4_exception() {
1605         test_pmtu_ipvX_over_ipvY_exception 6 4
1606 }
1607
1608 test_pmtu_ipv4_ipv6_exception() {
1609         test_pmtu_ipvX_over_ipvY_exception 4 6
1610 }
1611
1612 test_pmtu_ipv6_ipv6_exception() {
1613         test_pmtu_ipvX_over_ipvY_exception 6 6
1614 }
1615
1616 test_pmtu_vti4_exception() {
1617         setup namespaces veth vti4 xfrm4 || return $ksft_skip
1618         trace "${ns_a}" veth_a    "${ns_b}" veth_b \
1619               "${ns_a}" vti4_a    "${ns_b}" vti4_b
1620
1621         veth_mtu=1500
1622         vti_mtu=$((veth_mtu - 20))
1623
1624         #                                SPI   SN   IV  ICV   pad length   next header
1625         esp_payload_rfc4106=$((vti_mtu - 4   - 4  - 8 - 16  - 1          - 1))
1626         ping_payload=$((esp_payload_rfc4106 - 28))
1627
1628         mtu "${ns_a}" veth_a ${veth_mtu}
1629         mtu "${ns_b}" veth_b ${veth_mtu}
1630         mtu "${ns_a}" vti4_a ${vti_mtu}
1631         mtu "${ns_b}" vti4_b ${vti_mtu}
1632
1633         # Send DF packet without exceeding link layer MTU, check that no
1634         # exception is created
1635         run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s ${ping_payload} ${tunnel4_b_addr}
1636         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1637         check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
1638
1639         # Now exceed link layer MTU by one byte, check that exception is created
1640         # with the right PMTU value
1641         run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((ping_payload + 1)) ${tunnel4_b_addr}
1642         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1643         check_pmtu_value "${esp_payload_rfc4106}" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106 + 1)))"
1644 }
1645
1646 test_pmtu_vti6_exception() {
1647         setup namespaces veth vti6 xfrm6 || return $ksft_skip
1648         trace "${ns_a}" veth_a    "${ns_b}" veth_b \
1649               "${ns_a}" vti6_a    "${ns_b}" vti6_b
1650         fail=0
1651
1652         # Create route exception by exceeding link layer MTU
1653         mtu "${ns_a}" veth_a 4000
1654         mtu "${ns_b}" veth_b 4000
1655         mtu "${ns_a}" vti6_a 5000
1656         mtu "${ns_b}" vti6_b 5000
1657         run_cmd ${ns_a} ${ping6} -q -i 0.1 -w 1 -s 60000 ${tunnel6_b_addr}
1658
1659         # Check that exception was created
1660         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1661         check_pmtu_value any "${pmtu}" "creating tunnel exceeding link layer MTU" || return 1
1662
1663         # Decrease tunnel MTU, check for PMTU decrease in route exception
1664         mtu "${ns_a}" vti6_a 3000
1665         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1666         check_pmtu_value "3000" "${pmtu}" "decreasing tunnel MTU" || fail=1
1667
1668         # Increase tunnel MTU, check for PMTU increase in route exception
1669         mtu "${ns_a}" vti6_a 9000
1670         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1671         check_pmtu_value "9000" "${pmtu}" "increasing tunnel MTU" || fail=1
1672
1673         return ${fail}
1674 }
1675
1676 test_pmtu_vti4_udp_exception() {
1677         setup namespaces veth vti4 xfrm4udp || return $ksft_skip
1678         trace "${ns_a}" veth_a    "${ns_b}" veth_b \
1679               "${ns_a}" vti4_a    "${ns_b}" vti4_b
1680
1681         veth_mtu=1500
1682         vti_mtu=$((veth_mtu - 20))
1683
1684         #                                UDP   SPI   SN   IV  ICV   pad length   next header
1685         esp_payload_rfc4106=$((vti_mtu - 8   - 4   - 4  - 8 - 16  - 1          - 1))
1686         ping_payload=$((esp_payload_rfc4106 - 28))
1687
1688         mtu "${ns_a}" veth_a ${veth_mtu}
1689         mtu "${ns_b}" veth_b ${veth_mtu}
1690         mtu "${ns_a}" vti4_a ${vti_mtu}
1691         mtu "${ns_b}" vti4_b ${vti_mtu}
1692
1693         # Send DF packet without exceeding link layer MTU, check that no
1694         # exception is created
1695         run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s ${ping_payload} ${tunnel4_b_addr}
1696         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1697         check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
1698
1699         # Now exceed link layer MTU by one byte, check that exception is created
1700         # with the right PMTU value
1701         run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((ping_payload + 1)) ${tunnel4_b_addr}
1702         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1703         check_pmtu_value "${esp_payload_rfc4106}" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106 + 1)))"
1704 }
1705
1706 test_pmtu_vti6_udp_exception() {
1707         setup namespaces veth vti6 xfrm6udp || return $ksft_skip
1708         trace "${ns_a}" veth_a    "${ns_b}" veth_b \
1709               "${ns_a}" vti6_a    "${ns_b}" vti6_b
1710         fail=0
1711
1712         # Create route exception by exceeding link layer MTU
1713         mtu "${ns_a}" veth_a 4000
1714         mtu "${ns_b}" veth_b 4000
1715         mtu "${ns_a}" vti6_a 5000
1716         mtu "${ns_b}" vti6_b 5000
1717         run_cmd ${ns_a} ${ping6} -q -i 0.1 -w 1 -s 60000 ${tunnel6_b_addr}
1718
1719         # Check that exception was created
1720         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1721         check_pmtu_value any "${pmtu}" "creating tunnel exceeding link layer MTU" || return 1
1722
1723         # Decrease tunnel MTU, check for PMTU decrease in route exception
1724         mtu "${ns_a}" vti6_a 3000
1725         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1726         check_pmtu_value "3000" "${pmtu}" "decreasing tunnel MTU" || fail=1
1727
1728         # Increase tunnel MTU, check for PMTU increase in route exception
1729         mtu "${ns_a}" vti6_a 9000
1730         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1731         check_pmtu_value "9000" "${pmtu}" "increasing tunnel MTU" || fail=1
1732
1733         return ${fail}
1734 }
1735
1736 test_pmtu_vti4_udp_routed_exception() {
1737         setup namespaces routing vti4routed xfrm4udprouted || return $ksft_skip
1738         trace "${ns_a}" veth_A-R1    "${ns_b}" veth_B-R1 \
1739               "${ns_a}" vti4_a       "${ns_b}" vti4_b
1740
1741         veth_mtu=1500
1742         vti_mtu=$((veth_mtu - 20))
1743
1744         #                                UDP   SPI   SN   IV  ICV   pad length   next header
1745         esp_payload_rfc4106=$((vti_mtu - 8   - 4   - 4  - 8 - 16  - 1          - 1))
1746         ping_payload=$((esp_payload_rfc4106 - 28))
1747
1748         mtu "${ns_a}"  veth_A-R1 ${veth_mtu}
1749         mtu "${ns_r1}" veth_R1-A ${veth_mtu}
1750         mtu "${ns_b}"  veth_B-R1 ${veth_mtu}
1751         mtu "${ns_r1}" veth_R1-B ${veth_mtu}
1752
1753         mtu "${ns_a}" vti4_a ${vti_mtu}
1754         mtu "${ns_b}" vti4_b ${vti_mtu}
1755
1756         # Send DF packet without exceeding link layer MTU, check that no
1757         # exception is created
1758         run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s ${ping_payload} ${tunnel4_b_addr}
1759         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1760         check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
1761
1762         # Now decrease link layer MTU by 8 bytes on R1, check that exception is created
1763         # with the right PMTU value
1764         mtu "${ns_r1}" veth_R1-B $((veth_mtu - 8))
1765         run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((ping_payload)) ${tunnel4_b_addr}
1766         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1767         check_pmtu_value "$((esp_payload_rfc4106 - 8))" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106)))"
1768 }
1769
1770 test_pmtu_vti6_udp_routed_exception() {
1771         setup namespaces routing vti6routed xfrm6udprouted || return $ksft_skip
1772         trace "${ns_a}" veth_A-R1    "${ns_b}" veth_B-R1 \
1773               "${ns_a}" vti6_a       "${ns_b}" vti6_b
1774
1775         veth_mtu=1500
1776         vti_mtu=$((veth_mtu - 40))
1777
1778         #                                UDP   SPI   SN   IV  ICV   pad length   next header
1779         esp_payload_rfc4106=$((vti_mtu - 8   - 4   - 4  - 8 - 16  - 1          - 1))
1780         ping_payload=$((esp_payload_rfc4106 - 48))
1781
1782         mtu "${ns_a}"  veth_A-R1 ${veth_mtu}
1783         mtu "${ns_r1}" veth_R1-A ${veth_mtu}
1784         mtu "${ns_b}"  veth_B-R1 ${veth_mtu}
1785         mtu "${ns_r1}" veth_R1-B ${veth_mtu}
1786
1787         # mtu "${ns_a}" vti6_a ${vti_mtu}
1788         # mtu "${ns_b}" vti6_b ${vti_mtu}
1789
1790         run_cmd ${ns_a} ${ping6} -q -M want -i 0.1 -w 1 -s ${ping_payload} ${tunnel6_b_addr}
1791
1792         # Check that exception was not created
1793         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1794         check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
1795
1796         # Now decrease link layer MTU by 8 bytes on R1, check that exception is created
1797         # with the right PMTU value
1798         mtu "${ns_r1}" veth_R1-B $((veth_mtu - 8))
1799         run_cmd ${ns_a} ${ping6} -q -M want -i 0.1 -w 1 -s $((ping_payload)) ${tunnel6_b_addr}
1800         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1801         check_pmtu_value "$((esp_payload_rfc4106 - 8))" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106)))"
1802
1803 }
1804
1805 test_pmtu_vti4_default_mtu() {
1806         setup namespaces veth vti4 || return $ksft_skip
1807
1808         # Check that MTU of vti device is MTU of veth minus IPv4 header length
1809         veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
1810         vti4_mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1811         if [ $((veth_mtu - vti4_mtu)) -ne 20 ]; then
1812                 err "  vti MTU ${vti4_mtu} is not veth MTU ${veth_mtu} minus IPv4 header length"
1813                 return 1
1814         fi
1815 }
1816
1817 test_pmtu_vti6_default_mtu() {
1818         setup namespaces veth vti6 || return $ksft_skip
1819
1820         # Check that MTU of vti device is MTU of veth minus IPv6 header length
1821         veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
1822         vti6_mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1823         if [ $((veth_mtu - vti6_mtu)) -ne 40 ]; then
1824                 err "  vti MTU ${vti6_mtu} is not veth MTU ${veth_mtu} minus IPv6 header length"
1825                 return 1
1826         fi
1827 }
1828
1829 test_pmtu_vti4_link_add_mtu() {
1830         setup namespaces || return $ksft_skip
1831
1832         run_cmd ${ns_a} ip link add vti4_a type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1833         [ $? -ne 0 ] && err "  vti not supported" && return $ksft_skip
1834         run_cmd ${ns_a} ip link del vti4_a
1835
1836         fail=0
1837
1838         min=68
1839         max=$((65535 - 20))
1840         # Check invalid values first
1841         for v in $((min - 1)) $((max + 1)); do
1842                 run_cmd ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1843                 # This can fail, or MTU can be adjusted to a proper value
1844                 [ $? -ne 0 ] && continue
1845                 mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1846                 if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
1847                         err "  vti tunnel created with invalid MTU ${mtu}"
1848                         fail=1
1849                 fi
1850                 run_cmd ${ns_a} ip link del vti4_a
1851         done
1852
1853         # Now check valid values
1854         for v in ${min} 1300 ${max}; do
1855                 run_cmd ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1856                 mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1857                 run_cmd ${ns_a} ip link del vti4_a
1858                 if [ "${mtu}" != "${v}" ]; then
1859                         err "  vti MTU ${mtu} doesn't match configured value ${v}"
1860                         fail=1
1861                 fi
1862         done
1863
1864         return ${fail}
1865 }
1866
1867 test_pmtu_vti6_link_add_mtu() {
1868         setup namespaces || return $ksft_skip
1869
1870         run_cmd ${ns_a} ip link add vti6_a type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1871         [ $? -ne 0 ] && err "  vti6 not supported" && return $ksft_skip
1872         run_cmd ${ns_a} ip link del vti6_a
1873
1874         fail=0
1875
1876         min=68                  # vti6 can carry IPv4 packets too
1877         max=$((65535 - 40))
1878         # Check invalid values first
1879         for v in $((min - 1)) $((max + 1)); do
1880                 run_cmd ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1881                 # This can fail, or MTU can be adjusted to a proper value
1882                 [ $? -ne 0 ] && continue
1883                 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1884                 if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
1885                         err "  vti6 tunnel created with invalid MTU ${v}"
1886                         fail=1
1887                 fi
1888                 run_cmd ${ns_a} ip link del vti6_a
1889         done
1890
1891         # Now check valid values
1892         for v in 68 1280 1300 $((65535 - 40)); do
1893                 run_cmd ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1894                 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1895                 run_cmd ${ns_a} ip link del vti6_a
1896                 if [ "${mtu}" != "${v}" ]; then
1897                         err "  vti6 MTU ${mtu} doesn't match configured value ${v}"
1898                         fail=1
1899                 fi
1900         done
1901
1902         return ${fail}
1903 }
1904
1905 test_pmtu_vti6_link_change_mtu() {
1906         setup namespaces || return $ksft_skip
1907
1908         run_cmd ${ns_a} ip link add dummy0 mtu 1500 type dummy
1909         [ $? -ne 0 ] && err "  dummy not supported" && return $ksft_skip
1910         run_cmd ${ns_a} ip link add dummy1 mtu 3000 type dummy
1911         run_cmd ${ns_a} ip link set dummy0 up
1912         run_cmd ${ns_a} ip link set dummy1 up
1913
1914         run_cmd ${ns_a} ip addr add ${dummy6_0_prefix}1/${dummy6_mask} dev dummy0
1915         run_cmd ${ns_a} ip addr add ${dummy6_1_prefix}1/${dummy6_mask} dev dummy1
1916
1917         fail=0
1918
1919         # Create vti6 interface bound to device, passing MTU, check it
1920         run_cmd ${ns_a} ip link add vti6_a mtu 1300 type vti6 remote ${dummy6_0_prefix}2 local ${dummy6_0_prefix}1
1921         mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1922         if [ ${mtu} -ne 1300 ]; then
1923                 err "  vti6 MTU ${mtu} doesn't match configured value 1300"
1924                 fail=1
1925         fi
1926
1927         # Move to another device with different MTU, without passing MTU, check
1928         # MTU is adjusted
1929         run_cmd ${ns_a} ip link set vti6_a type vti6 remote ${dummy6_1_prefix}2 local ${dummy6_1_prefix}1
1930         mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1931         if [ ${mtu} -ne $((3000 - 40)) ]; then
1932                 err "  vti MTU ${mtu} is not dummy MTU 3000 minus IPv6 header length"
1933                 fail=1
1934         fi
1935
1936         # Move it back, passing MTU, check MTU is not overridden
1937         run_cmd ${ns_a} ip link set vti6_a mtu 1280 type vti6 remote ${dummy6_0_prefix}2 local ${dummy6_0_prefix}1
1938         mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1939         if [ ${mtu} -ne 1280 ]; then
1940                 err "  vti6 MTU ${mtu} doesn't match configured value 1280"
1941                 fail=1
1942         fi
1943
1944         return ${fail}
1945 }
1946
1947 check_command() {
1948         cmd=${1}
1949
1950         if ! which ${cmd} > /dev/null 2>&1; then
1951                 err "  missing required command: '${cmd}'"
1952                 return 1
1953         fi
1954         return 0
1955 }
1956
1957 test_cleanup_vxlanX_exception() {
1958         outer="${1}"
1959         encap="vxlan"
1960         ll_mtu=4000
1961
1962         check_command taskset || return $ksft_skip
1963         cpu_list=$(grep -m 2 processor /proc/cpuinfo | cut -d ' ' -f 2)
1964
1965         setup namespaces routing ${encap}${outer} || return $ksft_skip
1966         trace "${ns_a}" ${encap}_a   "${ns_b}"  ${encap}_b \
1967               "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1968               "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1969
1970         # Create route exception by exceeding link layer MTU
1971         mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1972         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1973         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1974         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1975
1976         mtu "${ns_a}" ${encap}_a $((${ll_mtu} + 1000))
1977         mtu "${ns_b}" ${encap}_b $((${ll_mtu} + 1000))
1978
1979         # Fill exception cache for multiple CPUs (2)
1980         # we can always use inner IPv4 for that
1981         for cpu in ${cpu_list}; do
1982                 run_cmd taskset --cpu-list ${cpu} ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${tunnel4_b_addr}
1983         done
1984
1985         ${ns_a} ip link del dev veth_A-R1 &
1986         iplink_pid=$!
1987         sleep 1
1988         if [ "$(cat /proc/${iplink_pid}/cmdline 2>/dev/null | tr -d '\0')" = "iplinkdeldevveth_A-R1" ]; then
1989                 err "  can't delete veth device in a timely manner, PMTU dst likely leaked"
1990                 return 1
1991         fi
1992 }
1993
1994 test_cleanup_ipv6_exception() {
1995         test_cleanup_vxlanX_exception 6
1996 }
1997
1998 test_cleanup_ipv4_exception() {
1999         test_cleanup_vxlanX_exception 4
2000 }
2001
2002 run_test() {
2003         (
2004         tname="$1"
2005         tdesc="$2"
2006
2007         unset IFS
2008
2009         # Since cleanup() relies on variables modified by this subshell, it
2010         # has to run in this context.
2011         trap cleanup EXIT
2012
2013         if [ "$VERBOSE" = "1" ]; then
2014                 printf "\n##########################################################################\n\n"
2015         fi
2016
2017         eval test_${tname}
2018         ret=$?
2019
2020         if [ $ret -eq 0 ]; then
2021                 printf "TEST: %-60s  [ OK ]\n" "${tdesc}"
2022         elif [ $ret -eq 1 ]; then
2023                 printf "TEST: %-60s  [FAIL]\n" "${tdesc}"
2024                 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
2025                         echo
2026                         echo "Pausing. Hit enter to continue"
2027                         read a
2028                 fi
2029                 err_flush
2030                 exit 1
2031         elif [ $ret -eq $ksft_skip ]; then
2032                 printf "TEST: %-60s  [SKIP]\n" "${tdesc}"
2033                 err_flush
2034         fi
2035
2036         return $ret
2037         )
2038         ret=$?
2039         case $ret in
2040                 0)
2041                         all_skipped=false
2042                         [ $exitcode -eq $ksft_skip ] && exitcode=0
2043                 ;;
2044                 $ksft_skip)
2045                         [ $all_skipped = true ] && exitcode=$ksft_skip
2046                 ;;
2047                 *)
2048                         all_skipped=false
2049                         exitcode=1
2050                 ;;
2051         esac
2052
2053         return $ret
2054 }
2055
2056 run_test_nh() {
2057         tname="$1"
2058         tdesc="$2"
2059
2060         USE_NH=yes
2061         run_test "${tname}" "${tdesc} - nexthop objects"
2062         USE_NH=no
2063 }
2064
2065 test_list_flush_ipv4_exception() {
2066         setup namespaces routing || return $ksft_skip
2067         trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
2068               "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
2069               "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
2070               "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
2071
2072         dst_prefix1="${prefix4}.${b_r1}."
2073         dst2="${prefix4}.${b_r2}.1"
2074
2075         # Set up initial MTU values
2076         mtu "${ns_a}"  veth_A-R1 2000
2077         mtu "${ns_r1}" veth_R1-A 2000
2078         mtu "${ns_r1}" veth_R1-B 1500
2079         mtu "${ns_b}"  veth_B-R1 1500
2080
2081         mtu "${ns_a}"  veth_A-R2 2000
2082         mtu "${ns_r2}" veth_R2-A 2000
2083         mtu "${ns_r2}" veth_R2-B 1500
2084         mtu "${ns_b}"  veth_B-R2 1500
2085
2086         fail=0
2087
2088         # Add 100 addresses for veth endpoint on B reached by default A route
2089         for i in $(seq 100 199); do
2090                 run_cmd ${ns_b} ip addr add "${dst_prefix1}${i}" dev veth_B-R1
2091         done
2092
2093         # Create 100 cached route exceptions for path via R1, one via R2. Note
2094         # that with IPv4 we need to actually cause a route lookup that matches
2095         # the exception caused by ICMP, in order to actually have a cached
2096         # route, so we need to ping each destination twice
2097         for i in $(seq 100 199); do
2098                 run_cmd ${ns_a} ping -q -M want -i 0.1 -c 2 -s 1800 "${dst_prefix1}${i}"
2099         done
2100         run_cmd ${ns_a} ping -q -M want -i 0.1 -c 2 -s 1800 "${dst2}"
2101
2102         if [ "$(${ns_a} ip -oneline route list cache | wc -l)" -ne 101 ]; then
2103                 err "  can't list cached exceptions"
2104                 fail=1
2105         fi
2106
2107         run_cmd ${ns_a} ip route flush cache
2108         pmtu1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst_prefix}1)"
2109         pmtu2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst_prefix}2)"
2110         if [ -n "${pmtu1}" ] || [ -n "${pmtu2}" ] || \
2111            [ -n "$(${ns_a} ip route list cache)" ]; then
2112                 err "  can't flush cached exceptions"
2113                 fail=1
2114         fi
2115
2116         return ${fail}
2117 }
2118
2119 test_list_flush_ipv6_exception() {
2120         setup namespaces routing || return $ksft_skip
2121         trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
2122               "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
2123               "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
2124               "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
2125
2126         dst_prefix1="${prefix6}:${b_r1}::"
2127         dst2="${prefix6}:${b_r2}::1"
2128
2129         # Set up initial MTU values
2130         mtu "${ns_a}"  veth_A-R1 2000
2131         mtu "${ns_r1}" veth_R1-A 2000
2132         mtu "${ns_r1}" veth_R1-B 1500
2133         mtu "${ns_b}"  veth_B-R1 1500
2134
2135         mtu "${ns_a}"  veth_A-R2 2000
2136         mtu "${ns_r2}" veth_R2-A 2000
2137         mtu "${ns_r2}" veth_R2-B 1500
2138         mtu "${ns_b}"  veth_B-R2 1500
2139
2140         fail=0
2141
2142         # Add 100 addresses for veth endpoint on B reached by default A route
2143         for i in $(seq 100 199); do
2144                 run_cmd ${ns_b} ip addr add "${dst_prefix1}${i}" dev veth_B-R1
2145         done
2146
2147         # Create 100 cached route exceptions for path via R1, one via R2
2148         for i in $(seq 100 199); do
2149                 run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s 1800 "${dst_prefix1}${i}"
2150         done
2151         run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s 1800 "${dst2}"
2152         if [ "$(${ns_a} ip -oneline -6 route list cache | wc -l)" -ne 101 ]; then
2153                 err "  can't list cached exceptions"
2154                 fail=1
2155         fi
2156
2157         run_cmd ${ns_a} ip -6 route flush cache
2158         pmtu1="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst_prefix1}100")"
2159         pmtu2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
2160         if [ -n "${pmtu1}" ] || [ -n "${pmtu2}" ] || \
2161            [ -n "$(${ns_a} ip -6 route list cache)" ]; then
2162                 err "  can't flush cached exceptions"
2163                 fail=1
2164         fi
2165
2166         return ${fail}
2167 }
2168
2169 test_pmtu_ipvX_route_change() {
2170         family=${1}
2171
2172         setup namespaces routing || return 2
2173         trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
2174               "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
2175               "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
2176               "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
2177
2178         if [ ${family} -eq 4 ]; then
2179                 ping=ping
2180                 dst1="${prefix4}.${b_r1}.1"
2181                 dst2="${prefix4}.${b_r2}.1"
2182                 gw="${prefix4}.${a_r1}.2"
2183         else
2184                 ping=${ping6}
2185                 dst1="${prefix6}:${b_r1}::1"
2186                 dst2="${prefix6}:${b_r2}::1"
2187                 gw="${prefix6}:${a_r1}::2"
2188         fi
2189
2190         # Set up initial MTU values
2191         mtu "${ns_a}"  veth_A-R1 2000
2192         mtu "${ns_r1}" veth_R1-A 2000
2193         mtu "${ns_r1}" veth_R1-B 1400
2194         mtu "${ns_b}"  veth_B-R1 1400
2195
2196         mtu "${ns_a}"  veth_A-R2 2000
2197         mtu "${ns_r2}" veth_R2-A 2000
2198         mtu "${ns_r2}" veth_R2-B 1500
2199         mtu "${ns_b}"  veth_B-R2 1500
2200
2201         # Create route exceptions
2202         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst1}
2203         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst2}
2204
2205         # Check that exceptions have been created with the correct PMTU
2206         pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
2207         check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
2208         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
2209         check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
2210
2211         # Replace the route from A to R1
2212         run_cmd ${ns_a} ip route change default via ${gw}
2213
2214         # Delete the device in A
2215         run_cmd ${ns_a} ip link del "veth_A-R1"
2216 }
2217
2218 test_pmtu_ipv4_route_change() {
2219         test_pmtu_ipvX_route_change 4
2220 }
2221
2222 test_pmtu_ipv6_route_change() {
2223         test_pmtu_ipvX_route_change 6
2224 }
2225
2226 usage() {
2227         echo
2228         echo "$0 [OPTIONS] [TEST]..."
2229         echo "If no TEST argument is given, all tests will be run."
2230         echo
2231         echo "Options"
2232         echo "  --trace: capture traffic to TEST_INTERFACE.pcap"
2233         echo
2234         echo "Available tests${tests}"
2235         exit 1
2236 }
2237
2238 ################################################################################
2239 #
2240 exitcode=0
2241 desc=0
2242 all_skipped=true
2243
2244 while getopts :ptv o
2245 do
2246         case $o in
2247         p) PAUSE_ON_FAIL=yes;;
2248         v) VERBOSE=1;;
2249         t) if which tcpdump > /dev/null 2>&1; then
2250                 TRACING=1
2251            else
2252                 echo "=== tcpdump not available, tracing disabled"
2253            fi
2254            ;;
2255         *) usage;;
2256         esac
2257 done
2258 shift $(($OPTIND-1))
2259
2260 IFS="   
2261 "
2262
2263 for arg do
2264         # Check first that all requested tests are available before running any
2265         command -v > /dev/null "test_${arg}" || { echo "=== Test ${arg} not found"; usage; }
2266 done
2267
2268 trap cleanup EXIT
2269
2270 # start clean
2271 cleanup
2272
2273 HAVE_NH=no
2274 ip nexthop ls >/dev/null 2>&1
2275 [ $? -eq 0 ] && HAVE_NH=yes
2276
2277 name=""
2278 desc=""
2279 rerun_nh=0
2280 for t in ${tests}; do
2281         [ "${name}" = "" ]      && name="${t}"  && continue
2282         [ "${desc}" = "" ]      && desc="${t}"  && continue
2283
2284         if [ "${HAVE_NH}" = "yes" ]; then
2285                 rerun_nh="${t}"
2286         fi
2287
2288         run_this=1
2289         for arg do
2290                 [ "${arg}" != "${arg#--*}" ] && continue
2291                 [ "${arg}" = "${name}" ] && run_this=1 && break
2292                 run_this=0
2293         done
2294         if [ $run_this -eq 1 ]; then
2295                 run_test "${name}" "${desc}"
2296                 # if test was skipped no need to retry with nexthop objects
2297                 [ $? -eq $ksft_skip ] && rerun_nh=0
2298
2299                 if [ "${rerun_nh}" = "1" ]; then
2300                         run_test_nh "${name}" "${desc}"
2301                 fi
2302         fi
2303         name=""
2304         desc=""
2305         rerun_nh=0
2306 done
2307
2308 exit ${exitcode}