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