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