xref: /linux/tools/testing/selftests/net/pmtu.sh (revision 9410645520e9b820069761f3450ef6661418e279)
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	check_gen_prog "nettest"
685
686	[ ${1} -eq 6 ] && proto="-6" || proto=""
687	port=${2}
688
689	run_cmd_bg "${ns_a}" nettest "${proto}" -q -D -s -x -p "${port}" -t 5
690	nettest_pids="${nettest_pids} $!"
691
692	run_cmd_bg "${ns_b}" nettest "${proto}" -q -D -s -x -p "${port}" -t 5
693	nettest_pids="${nettest_pids} $!"
694}
695
696setup_xfrm4() {
697	setup_xfrm 4 ${veth4_a_addr} ${veth4_b_addr}
698}
699
700setup_xfrm6() {
701	setup_xfrm 6 ${veth6_a_addr} ${veth6_b_addr}
702}
703
704setup_xfrm4udp() {
705	setup_xfrm 4 ${veth4_a_addr} ${veth4_b_addr} "encap espinudp 4500 4500 0.0.0.0" && \
706		setup_nettest_xfrm 4 4500
707}
708
709setup_xfrm6udp() {
710	setup_xfrm 6 ${veth6_a_addr} ${veth6_b_addr} "encap espinudp 4500 4500 0.0.0.0" && \
711		setup_nettest_xfrm 6 4500
712}
713
714setup_xfrm4udprouted() {
715	setup_xfrm 4 ${prefix4}.${a_r1}.1 ${prefix4}.${b_r1}.1 "encap espinudp 4500 4500 0.0.0.0" && \
716		setup_nettest_xfrm 4 4500
717}
718
719setup_xfrm6udprouted() {
720	setup_xfrm 6 ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 "encap espinudp 4500 4500 0.0.0.0" && \
721		setup_nettest_xfrm 6 4500
722}
723
724setup_routing_old() {
725	for i in ${routes}; do
726		[ "${ns}" = "" ]	&& ns="${i}"		&& continue
727		[ "${addr}" = "" ]	&& addr="${i}"		&& continue
728		[ "${gw}" = "" ]	&& gw="${i}"
729
730		ns_name="$(nsname ${ns})"
731
732		ip -n "${ns_name}" route add "${addr}" table "${rt_table}" via "${gw}"
733
734		ns=""; addr=""; gw=""
735	done
736}
737
738setup_routing_new() {
739	for i in ${nexthops}; do
740		[ "${ns}" = "" ]	&& ns="${i}"		&& continue
741		[ "${fam}" = "" ]	&& fam="${i}"		&& continue
742		[ "${nhid}" = "" ]	&& nhid="${i}"		&& continue
743		[ "${gw}" = "" ]	&& gw="${i}"		&& continue
744		[ "${dev}" = "" ]	&& dev="${i}"
745
746		ns_name="$(nsname ${ns})"
747
748		ip -n ${ns_name} -${fam} nexthop add id ${nhid} via ${gw} dev ${dev}
749
750		ns=""; fam=""; nhid=""; gw=""; dev=""
751
752	done
753
754	for i in ${routes_nh}; do
755		[ "${ns}" = "" ]	&& ns="${i}"		&& continue
756		[ "${fam}" = "" ]	&& fam="${i}"		&& continue
757		[ "${addr}" = "" ]	&& addr="${i}"		&& continue
758		[ "${nhid}" = "" ]	&& nhid="${i}"
759
760		ns_name="$(nsname ${ns})"
761
762		ip -n "${ns_name}" -"${fam}" route add "${addr}" table "${rt_table}" nhid "${nhid}"
763
764		ns=""; fam=""; addr=""; nhid=""
765	done
766}
767
768setup_routing() {
769	for i in ${NS_R1} ${NS_R2}; do
770		ip netns exec ${i} sysctl -q net/ipv4/ip_forward=1
771		ip netns exec ${i} sysctl -q net/ipv6/conf/all/forwarding=1
772	done
773
774	for i in ${routing_addrs}; do
775		[ "${ns}" = "" ]	&& ns="${i}"		&& continue
776		[ "${peer}" = "" ]	&& peer="${i}"		&& continue
777		[ "${segment}" = "" ]	&& segment="${i}"
778
779		ns_name="$(nsname ${ns})"
780		peer_name="$(nsname ${peer})"
781		if="veth_${ns}-${peer}"
782		ifpeer="veth_${peer}-${ns}"
783
784		# Create veth links
785		ip link add ${if} up netns ${ns_name} type veth peer name ${ifpeer} netns ${peer_name} || return 1
786		ip -n ${peer_name} link set dev ${ifpeer} up
787
788		# Add addresses
789		ip -n ${ns_name}   addr add ${prefix4}.${segment}.1/24  dev ${if}
790		ip -n ${ns_name}   addr add ${prefix6}:${segment}::1/64 dev ${if}
791
792		ip -n ${peer_name} addr add ${prefix4}.${segment}.2/24  dev ${ifpeer}
793		ip -n ${peer_name} addr add ${prefix6}:${segment}::2/64 dev ${ifpeer}
794
795		ns=""; peer=""; segment=""
796	done
797
798	if [ "$USE_NH" = "yes" ]; then
799		setup_routing_new
800	else
801		setup_routing_old
802	fi
803
804	return 0
805}
806
807setup_policy_routing() {
808	setup_routing
809
810	ip -netns "${NS_A}" -4 rule add dsfield "${policy_mark}" \
811		table "${rt_table}"
812
813	# Set the IPv4 Don't Fragment bit with tc, since socat doesn't seem to
814	# have an option do to it.
815	tc -netns "${NS_A}" qdisc replace dev veth_A-R1 root prio
816	tc -netns "${NS_A}" qdisc replace dev veth_A-R2 root prio
817	tc -netns "${NS_A}" filter add dev veth_A-R1                      \
818		protocol ipv4 flower ip_proto udp                         \
819		action pedit ex munge ip df set 0x40 pipe csum ip and udp
820	tc -netns "${NS_A}" filter add dev veth_A-R2                      \
821		protocol ipv4 flower ip_proto udp                         \
822		action pedit ex munge ip df set 0x40 pipe csum ip and udp
823}
824
825setup_bridge() {
826	run_cmd ${ns_a} ip link add br0 type bridge || return $ksft_skip
827	run_cmd ${ns_a} ip link set br0 up
828
829	run_cmd ${ns_c} ip link add veth_C-A type veth peer name veth_A-C
830	run_cmd ${ns_c} ip link set veth_A-C netns ${NS_A}
831
832	run_cmd ${ns_a} ip link set veth_A-C up
833	run_cmd ${ns_c} ip link set veth_C-A up
834	run_cmd ${ns_c} ip addr add ${veth4_c_addr}/${veth4_mask} dev veth_C-A
835	run_cmd ${ns_c} ip addr add ${veth6_c_addr}/${veth6_mask} dev veth_C-A
836	run_cmd ${ns_a} ip link set veth_A-C master br0
837}
838
839setup_ovs_via_internal_utility() {
840	type="${1}"
841	a_addr="${2}"
842	b_addr="${3}"
843	dport="${4}"
844
845	run_cmd python3 ./openvswitch/ovs-dpctl.py add-if ovs_br0 ${type}_a -t ${type} || return 1
846
847	ports=$(python3 ./openvswitch/ovs-dpctl.py show)
848	br0_port=$(echo "$ports" | grep -E "\sovs_br0" | sed -e 's@port @@' | cut -d: -f1 | xargs)
849	type_a_port=$(echo "$ports" | grep ${type}_a | sed -e 's@port @@' | cut -d: -f1 | xargs)
850	veth_a_port=$(echo "$ports" | grep veth_A | sed -e 's@port @@' | cut -d: -f1 | xargs)
851
852	v4_a_tun="${prefix4}.${a_r1}.1"
853	v4_b_tun="${prefix4}.${b_r1}.1"
854
855	v6_a_tun="${prefix6}:${a_r1}::1"
856	v6_b_tun="${prefix6}:${b_r1}::1"
857
858	if [ "${v4_a_tun}" = "${a_addr}" ]; then
859		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
860		    "recirc_id(0),in_port(${veth_a_port}),eth(),eth_type(0x0800),ipv4()" \
861		    "set(tunnel(tun_id=1,dst=${v4_b_tun},ttl=64,tp_dst=${dport},flags(df|csum))),${type_a_port}"
862		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
863		    "recirc_id(0),in_port(${veth_a_port}),eth(),eth_type(0x86dd),ipv6()" \
864		    "set(tunnel(tun_id=1,dst=${v4_b_tun},ttl=64,tp_dst=${dport},flags(df|csum))),${type_a_port}"
865		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
866		    "recirc_id(0),tunnel(tun_id=1,src=${v4_b_tun},dst=${v4_a_tun}),in_port(${type_a_port}),eth(),eth_type(0x0800),ipv4()" \
867		    "${veth_a_port}"
868		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
869		    "recirc_id(0),tunnel(tun_id=1,src=${v4_b_tun},dst=${v4_a_tun}),in_port(${type_a_port}),eth(),eth_type(0x86dd),ipv6()" \
870		    "${veth_a_port}"
871		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
872		    "recirc_id(0),tunnel(tun_id=1,src=${v4_b_tun},dst=${v4_a_tun}),in_port(${type_a_port}),eth(),eth_type(0x0806),arp()" \
873		    "${veth_a_port}"
874		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
875		    "recirc_id(0),in_port(${veth_a_port}),eth(),eth_type(0x0806),arp(sip=${veth4_c_addr},tip=${tunnel4_b_addr})" \
876		    "set(tunnel(tun_id=1,dst=${v4_b_tun},ttl=64,tp_dst=${dport},flags(df|csum))),${type_a_port}"
877	else
878		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
879		    "recirc_id(0),in_port(${veth_a_port}),eth(),eth_type(0x0800),ipv4()" \
880		    "set(tunnel(tun_id=1,ipv6_dst=${v6_b_tun},ttl=64,tp_dst=${dport},flags(df|csum))),${type_a_port}"
881		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
882		    "recirc_id(0),in_port(${veth_a_port}),eth(),eth_type(0x86dd),ipv6()" \
883		    "set(tunnel(tun_id=1,ipv6_dst=${v6_b_tun},ttl=64,tp_dst=${dport},flags(df|csum))),${type_a_port}"
884		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
885		    "recirc_id(0),tunnel(tun_id=1,ipv6_src=${v6_b_tun},ipv6_dst=${v6_a_tun}),in_port(${type_a_port}),eth(),eth_type(0x0800),ipv4()" \
886		    "${veth_a_port}"
887		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
888		    "recirc_id(0),tunnel(tun_id=1,ipv6_src=${v6_b_tun},ipv6_dst=${v6_a_tun}),in_port(${type_a_port}),eth(),eth_type(0x86dd),ipv6()" \
889		    "${veth_a_port}"
890		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
891		    "recirc_id(0),tunnel(tun_id=1,ipv6_src=${v6_b_tun},ipv6_dst=${v6_a_tun}),in_port(${type_a_port}),eth(),eth_type(0x0806),arp()" \
892		    "${veth_a_port}"
893		run_cmd python3 ./openvswitch/ovs-dpctl.py add-flow ovs_br0 \
894		    "recirc_id(0),in_port(${veth_a_port}),eth(),eth_type(0x0806),arp(sip=${veth4_c_addr},tip=${tunnel4_b_addr})" \
895		    "set(tunnel(tun_id=1,ipv6_dst=${v6_b_tun},ttl=64,tp_dst=${dport},flags(df|csum))),${type_a_port}"
896	fi
897}
898
899setup_ovs_via_vswitchd() {
900	type="${1}"
901	b_addr="${2}"
902
903	run_cmd ovs-vsctl add-port ovs_br0 ${type}_a -- \
904		set interface ${type}_a type=${type} \
905		options:remote_ip=${b_addr} options:key=1 options:csum=true || return 1
906}
907
908setup_ovs_vxlan_or_geneve() {
909	type="${1}"
910	a_addr="${2}"
911	b_addr="${3}"
912	dport="6081"
913
914	if [ "${type}" = "vxlan" ]; then
915		dport="4789"
916		opts="${opts} ttl 64 dstport 4789"
917		opts_b="local ${b_addr}"
918	fi
919
920	setup_ovs_via_internal_utility "${type}" "${a_addr}" "${b_addr}" \
921				       "${dport}" || \
922	    setup_ovs_via_vswitchd "${type}" "${b_addr}" || return 1
923
924	run_cmd ${ns_b} ip link add ${type}_b type ${type} id 1 ${opts_b} remote ${a_addr} ${opts} || return 1
925
926	run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ${type}_b
927	run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ${type}_b
928
929	run_cmd ip link set ${type}_a up
930	run_cmd ${ns_b} ip link set ${type}_b up
931}
932
933setup_ovs_geneve4() {
934	setup_ovs_vxlan_or_geneve geneve ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1
935}
936
937setup_ovs_vxlan4() {
938	setup_ovs_vxlan_or_geneve vxlan  ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1
939}
940
941setup_ovs_geneve6() {
942	setup_ovs_vxlan_or_geneve geneve ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1
943}
944
945setup_ovs_vxlan6() {
946	setup_ovs_vxlan_or_geneve vxlan  ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1
947}
948
949setup_ovs_br_internal() {
950	run_cmd python3 ./openvswitch/ovs-dpctl.py add-dp ovs_br0 || \
951		return 1
952}
953
954setup_ovs_br_vswitchd() {
955	run_cmd ovs-vsctl add-br ovs_br0 || return 1
956}
957
958setup_ovs_add_if() {
959	ifname="${1}"
960	run_cmd python3 ./openvswitch/ovs-dpctl.py add-if ovs_br0 \
961		"${ifname}" || \
962		run_cmd ovs-vsctl add-port ovs_br0 "${ifname}"
963}
964
965setup_ovs_bridge() {
966	setup_ovs_br_internal || setup_ovs_br_vswitchd || return $ksft_skip
967	run_cmd ip link set ovs_br0 up
968
969	run_cmd ${ns_c} ip link add veth_C-A type veth peer name veth_A-C
970	run_cmd ${ns_c} ip link set veth_A-C netns 1
971
972	run_cmd         ip link set veth_A-C up
973	run_cmd ${ns_c} ip link set veth_C-A up
974	run_cmd ${ns_c} ip addr add ${veth4_c_addr}/${veth4_mask} dev veth_C-A
975	run_cmd ${ns_c} ip addr add ${veth6_c_addr}/${veth6_mask} dev veth_C-A
976	setup_ovs_add_if veth_A-C
977
978	# Move veth_A-R1 to init
979	run_cmd ${ns_a} ip link set veth_A-R1 netns 1
980	run_cmd ip addr add ${prefix4}.${a_r1}.1/${veth4_mask} dev veth_A-R1
981	run_cmd ip addr add ${prefix6}:${a_r1}::1/${veth6_mask} dev veth_A-R1
982	run_cmd ip link set veth_A-R1 up
983	run_cmd ip route add ${prefix4}.${b_r1}.1 via ${prefix4}.${a_r1}.2
984	run_cmd ip route add ${prefix6}:${b_r1}::1 via ${prefix6}:${a_r1}::2
985}
986
987setup() {
988	[ "$(id -u)" -ne 0 ] && echo "  need to run as root" && return $ksft_skip
989
990	for arg do
991		eval setup_${arg} || { echo "  ${arg} not supported"; return 1; }
992	done
993}
994
995trace() {
996	[ $TRACING -eq 0 ] && return
997
998	for arg do
999		[ "${ns_cmd}" = "" ] && ns_cmd="${arg}" && continue
1000		${ns_cmd} tcpdump --immediate-mode -s 0 -i "${arg}" -w "${name}_${arg}.pcap" 2> /dev/null &
1001		tcpdump_pids="${tcpdump_pids} $!"
1002		ns_cmd=
1003	done
1004	sleep 1
1005}
1006
1007cleanup_del_ovs_internal() {
1008	# squelch the output of the del-if commands since it can be wordy
1009	python3 ./openvswitch/ovs-dpctl.py del-if ovs_br0 -d true vxlan_a	>/dev/null	2>&1
1010	python3 ./openvswitch/ovs-dpctl.py del-if ovs_br0 -d true geneve_a	>/dev/null	2>&1
1011	python3 ./openvswitch/ovs-dpctl.py del-dp ovs_br0			>/dev/null	2>&1
1012}
1013
1014cleanup_del_ovs_vswitchd() {
1015	ovs-vsctl --if-exists del-port vxlan_a	2>/dev/null
1016	ovs-vsctl --if-exists del-br ovs_br0	2>/dev/null
1017}
1018
1019cleanup() {
1020	for pid in ${tcpdump_pids}; do
1021		kill ${pid}
1022	done
1023	tcpdump_pids=
1024
1025	for pid in ${nettest_pids}; do
1026		kill ${pid}
1027	done
1028	nettest_pids=
1029
1030	for pid in ${socat_pids}; do
1031		kill "${pid}"
1032	done
1033	socat_pids=
1034
1035	cleanup_all_ns
1036
1037	ip link del veth_A-C		2>/dev/null
1038	ip link del veth_A-R1		2>/dev/null
1039	cleanup_del_ovs_internal
1040	cleanup_del_ovs_vswitchd
1041	rm -f "$tmpoutfile"
1042}
1043
1044mtu() {
1045	ns_cmd="${1}"
1046	dev="${2}"
1047	mtu="${3}"
1048
1049	${ns_cmd} ip link set dev ${dev} mtu ${mtu}
1050}
1051
1052mtu_parse() {
1053	input="${1}"
1054
1055	next=0
1056	for i in ${input}; do
1057		[ ${next} -eq 1 -a "${i}" = "lock" ] && next=2 && continue
1058		[ ${next} -eq 1 ] && echo "${i}" && return
1059		[ ${next} -eq 2 ] && echo "lock ${i}" && return
1060		[ "${i}" = "mtu" ] && next=1
1061	done
1062}
1063
1064link_get() {
1065	ns_cmd="${1}"
1066	name="${2}"
1067
1068	${ns_cmd} ip link show dev "${name}"
1069}
1070
1071link_get_mtu() {
1072	ns_cmd="${1}"
1073	name="${2}"
1074
1075	mtu_parse "$(link_get "${ns_cmd}" ${name})"
1076}
1077
1078route_get_dst_exception() {
1079	ns_cmd="${1}"
1080	dst="${2}"
1081	dsfield="${3}"
1082
1083	if [ -z "${dsfield}" ]; then
1084		dsfield=0
1085	fi
1086
1087	${ns_cmd} ip route get "${dst}" dsfield "${dsfield}"
1088}
1089
1090route_get_dst_pmtu_from_exception() {
1091	ns_cmd="${1}"
1092	dst="${2}"
1093	dsfield="${3}"
1094
1095	mtu_parse "$(route_get_dst_exception "${ns_cmd}" "${dst}" "${dsfield}")"
1096}
1097
1098check_pmtu_value() {
1099	expected="${1}"
1100	value="${2}"
1101	event="${3}"
1102
1103	[ "${expected}" = "any" ] && [ -n "${value}" ] && return 0
1104	[ "${value}" = "${expected}" ] && return 0
1105	[ -z "${value}" ] &&    err "  PMTU exception wasn't created after ${event}" && return 1
1106	[ -z "${expected}" ] && err "  PMTU exception shouldn't exist after ${event}" && return 1
1107	err "  found PMTU exception with incorrect MTU ${value}, expected ${expected}, after ${event}"
1108	return 1
1109}
1110
1111test_pmtu_ipvX() {
1112	family=${1}
1113
1114	setup namespaces 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	if [ ${family} -eq 4 ]; then
1121		ping=ping
1122		dst1="${prefix4}.${b_r1}.1"
1123		dst2="${prefix4}.${b_r2}.1"
1124	else
1125		ping=${ping6}
1126		dst1="${prefix6}:${b_r1}::1"
1127		dst2="${prefix6}:${b_r2}::1"
1128	fi
1129
1130	# Set up initial MTU values
1131	mtu "${ns_a}"  veth_A-R1 2000
1132	mtu "${ns_r1}" veth_R1-A 2000
1133	mtu "${ns_r1}" veth_R1-B 1400
1134	mtu "${ns_b}"  veth_B-R1 1400
1135
1136	mtu "${ns_a}"  veth_A-R2 2000
1137	mtu "${ns_r2}" veth_R2-A 2000
1138	mtu "${ns_r2}" veth_R2-B 1500
1139	mtu "${ns_b}"  veth_B-R2 1500
1140
1141	# Create route exceptions
1142	run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst1}
1143	run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst2}
1144
1145	# Check that exceptions have been created with the correct PMTU
1146	pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
1147	check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
1148	pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1149	check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
1150
1151	# Decrease local MTU below PMTU, check for PMTU decrease in route exception
1152	mtu "${ns_a}"  veth_A-R1 1300
1153	mtu "${ns_r1}" veth_R1-A 1300
1154	pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
1155	check_pmtu_value "1300" "${pmtu_1}" "decreasing local MTU" || return 1
1156	# Second exception shouldn't be modified
1157	pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1158	check_pmtu_value "1500" "${pmtu_2}" "changing local MTU on a link not on this path" || return 1
1159
1160	# Increase MTU, check for PMTU increase in route exception
1161	mtu "${ns_a}"  veth_A-R1 1700
1162	mtu "${ns_r1}" veth_R1-A 1700
1163	pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
1164	check_pmtu_value "1700" "${pmtu_1}" "increasing local MTU" || return 1
1165	# Second exception shouldn't be modified
1166	pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1167	check_pmtu_value "1500" "${pmtu_2}" "changing local MTU on a link not on this path" || return 1
1168
1169	# Skip PMTU locking tests for IPv6
1170	[ $family -eq 6 ] && return 0
1171
1172	# Decrease remote MTU on path via R2, get new exception
1173	mtu "${ns_r2}" veth_R2-B 400
1174	mtu "${ns_b}"  veth_B-R2 400
1175	run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1400 ${dst2}
1176	pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1177	check_pmtu_value "lock 552" "${pmtu_2}" "exceeding MTU, with MTU < min_pmtu" || return 1
1178
1179	# Decrease local MTU below PMTU
1180	mtu "${ns_a}"  veth_A-R2 500
1181	mtu "${ns_r2}" veth_R2-A 500
1182	pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1183	check_pmtu_value "500" "${pmtu_2}" "decreasing local MTU" || return 1
1184
1185	# Increase local MTU
1186	mtu "${ns_a}"  veth_A-R2 1500
1187	mtu "${ns_r2}" veth_R2-A 1500
1188	pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1189	check_pmtu_value "1500" "${pmtu_2}" "increasing local MTU" || return 1
1190
1191	# Get new exception
1192	run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1400 ${dst2}
1193	pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1194	check_pmtu_value "lock 552" "${pmtu_2}" "exceeding MTU, with MTU < min_pmtu" || return 1
1195}
1196
1197test_pmtu_ipv4_exception() {
1198	test_pmtu_ipvX 4
1199}
1200
1201test_pmtu_ipv6_exception() {
1202	test_pmtu_ipvX 6
1203}
1204
1205test_pmtu_ipv4_dscp_icmp_exception() {
1206	rt_table=100
1207
1208	setup namespaces policy_routing || return $ksft_skip
1209	trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
1210	      "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
1211	      "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
1212	      "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
1213
1214	# Set up initial MTU values
1215	mtu "${ns_a}"  veth_A-R1 2000
1216	mtu "${ns_r1}" veth_R1-A 2000
1217	mtu "${ns_r1}" veth_R1-B 1400
1218	mtu "${ns_b}"  veth_B-R1 1400
1219
1220	mtu "${ns_a}"  veth_A-R2 2000
1221	mtu "${ns_r2}" veth_R2-A 2000
1222	mtu "${ns_r2}" veth_R2-B 1500
1223	mtu "${ns_b}"  veth_B-R2 1500
1224
1225	len=$((2000 - 20 - 8)) # Fills MTU of veth_A-R1
1226
1227	dst1="${prefix4}.${b_r1}.1"
1228	dst2="${prefix4}.${b_r2}.1"
1229
1230	# Create route exceptions
1231	dsfield=${policy_mark} # No ECN bit set (Not-ECT)
1232	run_cmd "${ns_a}" ping -q -M want -Q "${dsfield}" -c 1 -w 1 -s "${len}" "${dst1}"
1233
1234	dsfield=$(printf "%#x" $((policy_mark + 0x02))) # ECN=2 (ECT(0))
1235	run_cmd "${ns_a}" ping -q -M want -Q "${dsfield}" -c 1 -w 1 -s "${len}" "${dst2}"
1236
1237	# Check that exceptions have been created with the correct PMTU
1238	pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst1}" "${policy_mark}")"
1239	check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
1240
1241	pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst2}" "${policy_mark}")"
1242	check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
1243}
1244
1245test_pmtu_ipv4_dscp_udp_exception() {
1246	rt_table=100
1247
1248	if ! which socat > /dev/null 2>&1; then
1249		echo "'socat' command not found; skipping tests"
1250		return $ksft_skip
1251	fi
1252
1253	setup namespaces policy_routing || return $ksft_skip
1254	trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
1255	      "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
1256	      "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
1257	      "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
1258
1259	# Set up initial MTU values
1260	mtu "${ns_a}"  veth_A-R1 2000
1261	mtu "${ns_r1}" veth_R1-A 2000
1262	mtu "${ns_r1}" veth_R1-B 1400
1263	mtu "${ns_b}"  veth_B-R1 1400
1264
1265	mtu "${ns_a}"  veth_A-R2 2000
1266	mtu "${ns_r2}" veth_R2-A 2000
1267	mtu "${ns_r2}" veth_R2-B 1500
1268	mtu "${ns_b}"  veth_B-R2 1500
1269
1270	len=$((2000 - 20 - 8)) # Fills MTU of veth_A-R1
1271
1272	dst1="${prefix4}.${b_r1}.1"
1273	dst2="${prefix4}.${b_r2}.1"
1274
1275	# Create route exceptions
1276	run_cmd_bg "${ns_b}" socat UDP-LISTEN:50000 OPEN:/dev/null,wronly=1
1277	socat_pids="${socat_pids} $!"
1278
1279	dsfield=${policy_mark} # No ECN bit set (Not-ECT)
1280	run_cmd "${ns_a}" socat OPEN:/dev/zero,rdonly=1,readbytes="${len}" \
1281		UDP:"${dst1}":50000,tos="${dsfield}"
1282
1283	dsfield=$(printf "%#x" $((policy_mark + 0x02))) # ECN=2 (ECT(0))
1284	run_cmd "${ns_a}" socat OPEN:/dev/zero,rdonly=1,readbytes="${len}" \
1285		UDP:"${dst2}":50000,tos="${dsfield}"
1286
1287	# Check that exceptions have been created with the correct PMTU
1288	pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst1}" "${policy_mark}")"
1289	check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
1290	pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst2}" "${policy_mark}")"
1291	check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
1292}
1293
1294test_pmtu_ipvX_over_vxlanY_or_geneveY_exception() {
1295	type=${1}
1296	family=${2}
1297	outer_family=${3}
1298	ll_mtu=4000
1299
1300	if [ ${outer_family} -eq 4 ]; then
1301		setup namespaces routing ${type}4 || return $ksft_skip
1302		#                      IPv4 header   UDP header   VXLAN/GENEVE header   Ethernet header
1303		exp_mtu=$((${ll_mtu} - 20          - 8          - 8                   - 14))
1304	else
1305		setup namespaces routing ${type}6 || return $ksft_skip
1306		#                      IPv6 header   UDP header   VXLAN/GENEVE header   Ethernet header
1307		exp_mtu=$((${ll_mtu} - 40          - 8          - 8                   - 14))
1308	fi
1309
1310	trace "${ns_a}" ${type}_a    "${ns_b}"  ${type}_b \
1311	      "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1312	      "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1313
1314	if [ ${family} -eq 4 ]; then
1315		ping=ping
1316		dst=${tunnel4_b_addr}
1317	else
1318		ping=${ping6}
1319		dst=${tunnel6_b_addr}
1320	fi
1321
1322	# Create route exception by exceeding link layer MTU
1323	mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1324	mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1325	mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1326	mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1327
1328	mtu "${ns_a}" ${type}_a $((${ll_mtu} + 1000))
1329	mtu "${ns_b}" ${type}_b $((${ll_mtu} + 1000))
1330	run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
1331
1332	# Check that exception was created
1333	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1334	check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ${type} interface"
1335}
1336
1337test_pmtu_ipv4_vxlan4_exception() {
1338	test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  4 4
1339}
1340
1341test_pmtu_ipv6_vxlan4_exception() {
1342	test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  6 4
1343}
1344
1345test_pmtu_ipv4_geneve4_exception() {
1346	test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 4 4
1347}
1348
1349test_pmtu_ipv6_geneve4_exception() {
1350	test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 6 4
1351}
1352
1353test_pmtu_ipv4_vxlan6_exception() {
1354	test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  4 6
1355}
1356
1357test_pmtu_ipv6_vxlan6_exception() {
1358	test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  6 6
1359}
1360
1361test_pmtu_ipv4_geneve6_exception() {
1362	test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 4 6
1363}
1364
1365test_pmtu_ipv6_geneve6_exception() {
1366	test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 6 6
1367}
1368
1369test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception() {
1370	type=${1}
1371	family=${2}
1372	outer_family=${3}
1373	ll_mtu=4000
1374
1375	if [ ${outer_family} -eq 4 ]; then
1376		setup namespaces routing bridge bridged_${type}4 || return $ksft_skip
1377		#                      IPv4 header   UDP header   VXLAN/GENEVE header   Ethernet header
1378		exp_mtu=$((${ll_mtu} - 20          - 8          - 8                   - 14))
1379	else
1380		setup namespaces routing bridge bridged_${type}6 || return $ksft_skip
1381		#                      IPv6 header   UDP header   VXLAN/GENEVE header   Ethernet header
1382		exp_mtu=$((${ll_mtu} - 40          - 8          - 8                   - 14))
1383	fi
1384
1385	trace "${ns_a}" ${type}_a    "${ns_b}"  ${type}_b \
1386	      "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1387	      "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B \
1388	      "${ns_a}" br0          "${ns_a}"  veth-A-C  \
1389	      "${ns_c}" veth_C-A
1390
1391	if [ ${family} -eq 4 ]; then
1392		ping=ping
1393		dst=${tunnel4_b_addr}
1394	else
1395		ping=${ping6}
1396		dst=${tunnel6_b_addr}
1397	fi
1398
1399	# Create route exception by exceeding link layer MTU
1400	mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1401	mtu "${ns_a}"  br0       $((${ll_mtu} + 1000))
1402	mtu "${ns_a}"  veth_A-C  $((${ll_mtu} + 1000))
1403	mtu "${ns_c}"  veth_C-A  $((${ll_mtu} + 1000))
1404	mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1405	mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1406	mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1407
1408	mtu "${ns_a}" ${type}_a $((${ll_mtu} + 1000))
1409	mtu "${ns_b}" ${type}_b $((${ll_mtu} + 1000))
1410
1411	run_cmd ${ns_c} ${ping} -q -M want -i 0.1 -c 10 -s $((${ll_mtu} + 500)) ${dst} || return 1
1412	run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1  -s $((${ll_mtu} + 500)) ${dst} || return 1
1413
1414	# Check that exceptions were created
1415	pmtu="$(route_get_dst_pmtu_from_exception "${ns_c}" ${dst})"
1416	check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on bridged ${type} interface"
1417	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1418	check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on locally bridged ${type} interface"
1419
1420	tmpoutfile=$(mktemp)
1421
1422	# Flush Exceptions, retry with TCP
1423	run_cmd ${ns_a} ip route flush cached ${dst}
1424	run_cmd ${ns_b} ip route flush cached ${dst}
1425	run_cmd ${ns_c} ip route flush cached ${dst}
1426
1427	for target in "${ns_a}" "${ns_c}" ; do
1428		if [ ${family} -eq 4 ]; then
1429			TCPDST=TCP:${dst}:50000
1430		else
1431			TCPDST="TCP:[${dst}]:50000"
1432		fi
1433		${ns_b} socat -T 3 -u -6 TCP-LISTEN:50000,reuseaddr STDOUT > $tmpoutfile &
1434		local socat_pid=$!
1435
1436		wait_local_port_listen ${NS_B} 50000 tcp
1437
1438		dd if=/dev/zero status=none bs=1M count=1 | ${target} socat -T 3 -u STDIN $TCPDST,connect-timeout=3
1439
1440		wait ${socat_pid}
1441		size=$(du -sb $tmpoutfile)
1442		size=${size%%/tmp/*}
1443
1444		[ $size -ne 1048576 ] && err "File size $size mismatches expected value in locally bridged vxlan test" && return 1
1445	done
1446
1447	rm -f "$tmpoutfile"
1448
1449	# Check that exceptions were created
1450	pmtu="$(route_get_dst_pmtu_from_exception "${ns_c}" ${dst})"
1451	check_pmtu_value ${exp_mtu} "${pmtu}" "tcp: exceeding link layer MTU on bridged ${type} interface"
1452	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1453	check_pmtu_value ${exp_mtu} "${pmtu}" "tcp exceeding link layer MTU on locally bridged ${type} interface"
1454}
1455
1456test_pmtu_ipv4_br_vxlan4_exception() {
1457	test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  4 4
1458}
1459
1460test_pmtu_ipv6_br_vxlan4_exception() {
1461	test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  6 4
1462}
1463
1464test_pmtu_ipv4_br_geneve4_exception() {
1465	test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 4 4
1466}
1467
1468test_pmtu_ipv6_br_geneve4_exception() {
1469	test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 6 4
1470}
1471
1472test_pmtu_ipv4_br_vxlan6_exception() {
1473	test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  4 6
1474}
1475
1476test_pmtu_ipv6_br_vxlan6_exception() {
1477	test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  6 6
1478}
1479
1480test_pmtu_ipv4_br_geneve6_exception() {
1481	test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 4 6
1482}
1483
1484test_pmtu_ipv6_br_geneve6_exception() {
1485	test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 6 6
1486}
1487
1488test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception() {
1489	type=${1}
1490	family=${2}
1491	outer_family=${3}
1492	ll_mtu=4000
1493
1494	if [ "${type}" = "vxlan" ]; then
1495		tun_a="vxlan_sys_4789"
1496	elif [ "${type}" = "geneve" ]; then
1497		tun_a="genev_sys_6081"
1498	fi
1499
1500	if [ ${outer_family} -eq 4 ]; then
1501		setup namespaces routing ovs_bridge ovs_${type}4 || return $ksft_skip
1502		#                      IPv4 header   UDP header   VXLAN/GENEVE header   Ethernet header
1503		exp_mtu=$((${ll_mtu} - 20          - 8          - 8                   - 14))
1504	else
1505		setup namespaces routing ovs_bridge ovs_${type}6 || return $ksft_skip
1506		#                      IPv6 header   UDP header   VXLAN/GENEVE header   Ethernet header
1507		exp_mtu=$((${ll_mtu} - 40          - 8          - 8                   - 14))
1508	fi
1509
1510	trace ""        ${type}_a    "${ns_b}"  ${type}_b \
1511	      ""        veth_A-R1    "${ns_r1}" veth_R1-A \
1512	      "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B \
1513	      ""        ovs_br0      ""         veth-A_C  \
1514	      "${ns_c}" veth_C-A     ""         "${tun_a}"
1515
1516	if [ ${family} -eq 4 ]; then
1517		ping=ping
1518		dst=${tunnel4_b_addr}
1519	else
1520		ping=${ping6}
1521		dst=${tunnel6_b_addr}
1522	fi
1523
1524	# Create route exception by exceeding link layer MTU
1525	mtu ""         veth_A-R1 $((${ll_mtu} + 1000))
1526	mtu ""         ovs_br0   $((${ll_mtu} + 1000))
1527	mtu ""         veth_A-C  $((${ll_mtu} + 1000))
1528	mtu "${ns_c}"  veth_C-A  $((${ll_mtu} + 1000))
1529	mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1530	mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1531	mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1532
1533	mtu ""        ${tun_a}  $((${ll_mtu} + 1000)) 2>/dev/null || \
1534		mtu ""        ${type}_a  $((${ll_mtu} + 1000)) 2>/dev/null
1535	mtu "${ns_b}" ${type}_b  $((${ll_mtu} + 1000))
1536
1537	run_cmd ${ns_c} ${ping} -q -M want -i 0.1 -c 20 -s $((${ll_mtu} + 500)) ${dst} || return 1
1538
1539	# Check that exceptions were created
1540	pmtu="$(route_get_dst_pmtu_from_exception "${ns_c}" ${dst})"
1541	check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on Open vSwitch ${type} interface"
1542}
1543
1544test_pmtu_ipv4_ovs_vxlan4_exception() {
1545	test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  4 4
1546}
1547
1548test_pmtu_ipv6_ovs_vxlan4_exception() {
1549	test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  6 4
1550}
1551
1552test_pmtu_ipv4_ovs_geneve4_exception() {
1553	test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 4 4
1554}
1555
1556test_pmtu_ipv6_ovs_geneve4_exception() {
1557	test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 6 4
1558}
1559
1560test_pmtu_ipv4_ovs_vxlan6_exception() {
1561	test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  4 6
1562}
1563
1564test_pmtu_ipv6_ovs_vxlan6_exception() {
1565	test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  6 6
1566}
1567
1568test_pmtu_ipv4_ovs_geneve6_exception() {
1569	test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 4 6
1570}
1571
1572test_pmtu_ipv6_ovs_geneve6_exception() {
1573	test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 6 6
1574}
1575
1576test_pmtu_ipvX_over_fouY_or_gueY() {
1577	inner_family=${1}
1578	outer_family=${2}
1579	encap=${3}
1580	ll_mtu=4000
1581
1582	setup namespaces routing ${encap}${outer_family}${inner_family} || return $ksft_skip
1583	trace "${ns_a}" ${encap}_a   "${ns_b}"  ${encap}_b \
1584	      "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1585	      "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1586
1587	if [ ${inner_family} -eq 4 ]; then
1588		ping=ping
1589		dst=${tunnel4_b_addr}
1590	else
1591		ping=${ping6}
1592		dst=${tunnel6_b_addr}
1593	fi
1594
1595	if [ "${encap}" = "gue" ]; then
1596		encap_overhead=4
1597	else
1598		encap_overhead=0
1599	fi
1600
1601	if [ ${outer_family} -eq 4 ]; then
1602		#                      IPv4 header   UDP header
1603		exp_mtu=$((${ll_mtu} - 20          - 8         - ${encap_overhead}))
1604	else
1605		#                      IPv6 header   Option 4   UDP header
1606		exp_mtu=$((${ll_mtu} - 40          - 8        - 8       - ${encap_overhead}))
1607	fi
1608
1609	# Create route exception by exceeding link layer MTU
1610	mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1611	mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1612	mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1613	mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1614
1615	mtu "${ns_a}" ${encap}_a $((${ll_mtu} + 1000))
1616	mtu "${ns_b}" ${encap}_b $((${ll_mtu} + 1000))
1617	run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
1618
1619	# Check that exception was created
1620	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1621	check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ${encap} interface"
1622}
1623
1624test_pmtu_ipv4_fou4_exception() {
1625	test_pmtu_ipvX_over_fouY_or_gueY 4 4 fou
1626}
1627
1628test_pmtu_ipv6_fou4_exception() {
1629	test_pmtu_ipvX_over_fouY_or_gueY 6 4 fou
1630}
1631
1632test_pmtu_ipv4_fou6_exception() {
1633	test_pmtu_ipvX_over_fouY_or_gueY 4 6 fou
1634}
1635
1636test_pmtu_ipv6_fou6_exception() {
1637	test_pmtu_ipvX_over_fouY_or_gueY 6 6 fou
1638}
1639
1640test_pmtu_ipv4_gue4_exception() {
1641	test_pmtu_ipvX_over_fouY_or_gueY 4 4 gue
1642}
1643
1644test_pmtu_ipv6_gue4_exception() {
1645	test_pmtu_ipvX_over_fouY_or_gueY 6 4 gue
1646}
1647
1648test_pmtu_ipv4_gue6_exception() {
1649	test_pmtu_ipvX_over_fouY_or_gueY 4 6 gue
1650}
1651
1652test_pmtu_ipv6_gue6_exception() {
1653	test_pmtu_ipvX_over_fouY_or_gueY 6 6 gue
1654}
1655
1656test_pmtu_ipvX_over_ipvY_exception() {
1657	inner=${1}
1658	outer=${2}
1659	ll_mtu=4000
1660
1661	setup namespaces routing ip${inner}ip${outer} || return $ksft_skip
1662
1663	trace "${ns_a}" ip_a         "${ns_b}"  ip_b  \
1664	      "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1665	      "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1666
1667	if [ ${inner} -eq 4 ]; then
1668		ping=ping
1669		dst=${tunnel4_b_addr}
1670	else
1671		ping=${ping6}
1672		dst=${tunnel6_b_addr}
1673	fi
1674
1675	if [ ${outer} -eq 4 ]; then
1676		#                      IPv4 header
1677		exp_mtu=$((${ll_mtu} - 20))
1678	else
1679		#                      IPv6 header   Option 4
1680		exp_mtu=$((${ll_mtu} - 40          - 8))
1681	fi
1682
1683	# Create route exception by exceeding link layer MTU
1684	mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1685	mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1686	mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1687	mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1688
1689	mtu "${ns_a}" ip_a $((${ll_mtu} + 1000)) || return
1690	mtu "${ns_b}" ip_b $((${ll_mtu} + 1000)) || return
1691	run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
1692
1693	# Check that exception was created
1694	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1695	check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ip${inner}ip${outer} interface"
1696}
1697
1698test_pmtu_ipv4_ipv4_exception() {
1699	test_pmtu_ipvX_over_ipvY_exception 4 4
1700}
1701
1702test_pmtu_ipv6_ipv4_exception() {
1703	test_pmtu_ipvX_over_ipvY_exception 6 4
1704}
1705
1706test_pmtu_ipv4_ipv6_exception() {
1707	test_pmtu_ipvX_over_ipvY_exception 4 6
1708}
1709
1710test_pmtu_ipv6_ipv6_exception() {
1711	test_pmtu_ipvX_over_ipvY_exception 6 6
1712}
1713
1714test_pmtu_vti4_exception() {
1715	setup namespaces veth vti4 xfrm4 || return $ksft_skip
1716	trace "${ns_a}" veth_a    "${ns_b}" veth_b \
1717	      "${ns_a}" vti4_a    "${ns_b}" vti4_b
1718
1719	veth_mtu=1500
1720	vti_mtu=$((veth_mtu - 20))
1721
1722	#                                SPI   SN   IV  ICV   pad length   next header
1723	esp_payload_rfc4106=$((vti_mtu - 4   - 4  - 8 - 16  - 1          - 1))
1724	ping_payload=$((esp_payload_rfc4106 - 28))
1725
1726	mtu "${ns_a}" veth_a ${veth_mtu}
1727	mtu "${ns_b}" veth_b ${veth_mtu}
1728	mtu "${ns_a}" vti4_a ${vti_mtu}
1729	mtu "${ns_b}" vti4_b ${vti_mtu}
1730
1731	# Send DF packet without exceeding link layer MTU, check that no
1732	# exception is created
1733	run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s ${ping_payload} ${tunnel4_b_addr}
1734	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1735	check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
1736
1737	# Now exceed link layer MTU by one byte, check that exception is created
1738	# with the right PMTU value
1739	run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((ping_payload + 1)) ${tunnel4_b_addr}
1740	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1741	check_pmtu_value "${esp_payload_rfc4106}" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106 + 1)))"
1742}
1743
1744test_pmtu_vti6_exception() {
1745	setup namespaces veth vti6 xfrm6 || return $ksft_skip
1746	trace "${ns_a}" veth_a    "${ns_b}" veth_b \
1747	      "${ns_a}" vti6_a    "${ns_b}" vti6_b
1748	fail=0
1749
1750	# Create route exception by exceeding link layer MTU
1751	mtu "${ns_a}" veth_a 4000
1752	mtu "${ns_b}" veth_b 4000
1753	mtu "${ns_a}" vti6_a 5000
1754	mtu "${ns_b}" vti6_b 5000
1755	run_cmd ${ns_a} ${ping6} -q -i 0.1 -w 1 -s 60000 ${tunnel6_b_addr}
1756
1757	# Check that exception was created
1758	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1759	check_pmtu_value any "${pmtu}" "creating tunnel exceeding link layer MTU" || return 1
1760
1761	# Decrease tunnel MTU, check for PMTU decrease in route exception
1762	mtu "${ns_a}" vti6_a 3000
1763	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1764	check_pmtu_value "3000" "${pmtu}" "decreasing tunnel MTU" || fail=1
1765
1766	# Increase tunnel MTU, check for PMTU increase in route exception
1767	mtu "${ns_a}" vti6_a 9000
1768	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1769	check_pmtu_value "9000" "${pmtu}" "increasing tunnel MTU" || fail=1
1770
1771	return ${fail}
1772}
1773
1774test_pmtu_vti4_udp_exception() {
1775	setup namespaces veth vti4 xfrm4udp || return $ksft_skip
1776	trace "${ns_a}" veth_a    "${ns_b}" veth_b \
1777	      "${ns_a}" vti4_a    "${ns_b}" vti4_b
1778
1779	veth_mtu=1500
1780	vti_mtu=$((veth_mtu - 20))
1781
1782	#                                UDP   SPI   SN   IV  ICV   pad length   next header
1783	esp_payload_rfc4106=$((vti_mtu - 8   - 4   - 4  - 8 - 16  - 1          - 1))
1784	ping_payload=$((esp_payload_rfc4106 - 28))
1785
1786	mtu "${ns_a}" veth_a ${veth_mtu}
1787	mtu "${ns_b}" veth_b ${veth_mtu}
1788	mtu "${ns_a}" vti4_a ${vti_mtu}
1789	mtu "${ns_b}" vti4_b ${vti_mtu}
1790
1791	# Send DF packet without exceeding link layer MTU, check that no
1792	# exception is created
1793	run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s ${ping_payload} ${tunnel4_b_addr}
1794	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1795	check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
1796
1797	# Now exceed link layer MTU by one byte, check that exception is created
1798	# with the right PMTU value
1799	run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((ping_payload + 1)) ${tunnel4_b_addr}
1800	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1801	check_pmtu_value "${esp_payload_rfc4106}" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106 + 1)))"
1802}
1803
1804test_pmtu_vti6_udp_exception() {
1805	setup namespaces veth vti6 xfrm6udp || return $ksft_skip
1806	trace "${ns_a}" veth_a    "${ns_b}" veth_b \
1807	      "${ns_a}" vti6_a    "${ns_b}" vti6_b
1808	fail=0
1809
1810	# Create route exception by exceeding link layer MTU
1811	mtu "${ns_a}" veth_a 4000
1812	mtu "${ns_b}" veth_b 4000
1813	mtu "${ns_a}" vti6_a 5000
1814	mtu "${ns_b}" vti6_b 5000
1815	run_cmd ${ns_a} ${ping6} -q -i 0.1 -w 1 -s 60000 ${tunnel6_b_addr}
1816
1817	# Check that exception was created
1818	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1819	check_pmtu_value any "${pmtu}" "creating tunnel exceeding link layer MTU" || return 1
1820
1821	# Decrease tunnel MTU, check for PMTU decrease in route exception
1822	mtu "${ns_a}" vti6_a 3000
1823	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1824	check_pmtu_value "3000" "${pmtu}" "decreasing tunnel MTU" || fail=1
1825
1826	# Increase tunnel MTU, check for PMTU increase in route exception
1827	mtu "${ns_a}" vti6_a 9000
1828	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1829	check_pmtu_value "9000" "${pmtu}" "increasing tunnel MTU" || fail=1
1830
1831	return ${fail}
1832}
1833
1834test_pmtu_vti4_udp_routed_exception() {
1835	setup namespaces routing vti4routed xfrm4udprouted || return $ksft_skip
1836	trace "${ns_a}" veth_A-R1    "${ns_b}" veth_B-R1 \
1837	      "${ns_a}" vti4_a       "${ns_b}" vti4_b
1838
1839	veth_mtu=1500
1840	vti_mtu=$((veth_mtu - 20))
1841
1842	#                                UDP   SPI   SN   IV  ICV   pad length   next header
1843	esp_payload_rfc4106=$((vti_mtu - 8   - 4   - 4  - 8 - 16  - 1          - 1))
1844	ping_payload=$((esp_payload_rfc4106 - 28))
1845
1846        mtu "${ns_a}"  veth_A-R1 ${veth_mtu}
1847        mtu "${ns_r1}" veth_R1-A ${veth_mtu}
1848        mtu "${ns_b}"  veth_B-R1 ${veth_mtu}
1849        mtu "${ns_r1}" veth_R1-B ${veth_mtu}
1850
1851	mtu "${ns_a}" vti4_a ${vti_mtu}
1852	mtu "${ns_b}" vti4_b ${vti_mtu}
1853
1854	# Send DF packet without exceeding link layer MTU, check that no
1855	# exception is created
1856	run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s ${ping_payload} ${tunnel4_b_addr}
1857	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1858	check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
1859
1860	# Now decrease link layer MTU by 8 bytes on R1, check that exception is created
1861	# with the right PMTU value
1862        mtu "${ns_r1}" veth_R1-B $((veth_mtu - 8))
1863	run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((ping_payload)) ${tunnel4_b_addr}
1864	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1865	check_pmtu_value "$((esp_payload_rfc4106 - 8))" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106)))"
1866}
1867
1868test_pmtu_vti6_udp_routed_exception() {
1869	setup namespaces routing vti6routed xfrm6udprouted || return $ksft_skip
1870	trace "${ns_a}" veth_A-R1    "${ns_b}" veth_B-R1 \
1871	      "${ns_a}" vti6_a       "${ns_b}" vti6_b
1872
1873	veth_mtu=1500
1874	vti_mtu=$((veth_mtu - 40))
1875
1876	#                                UDP   SPI   SN   IV  ICV   pad length   next header
1877	esp_payload_rfc4106=$((vti_mtu - 8   - 4   - 4  - 8 - 16  - 1          - 1))
1878	ping_payload=$((esp_payload_rfc4106 - 48))
1879
1880        mtu "${ns_a}"  veth_A-R1 ${veth_mtu}
1881        mtu "${ns_r1}" veth_R1-A ${veth_mtu}
1882        mtu "${ns_b}"  veth_B-R1 ${veth_mtu}
1883        mtu "${ns_r1}" veth_R1-B ${veth_mtu}
1884
1885	# mtu "${ns_a}" vti6_a ${vti_mtu}
1886	# mtu "${ns_b}" vti6_b ${vti_mtu}
1887
1888	run_cmd ${ns_a} ${ping6} -q -M want -i 0.1 -w 1 -s ${ping_payload} ${tunnel6_b_addr}
1889
1890	# Check that exception was not created
1891	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1892	check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
1893
1894	# Now decrease link layer MTU by 8 bytes on R1, check that exception is created
1895	# with the right PMTU value
1896        mtu "${ns_r1}" veth_R1-B $((veth_mtu - 8))
1897	run_cmd ${ns_a} ${ping6} -q -M want -i 0.1 -w 1 -s $((ping_payload)) ${tunnel6_b_addr}
1898	pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1899	check_pmtu_value "$((esp_payload_rfc4106 - 8))" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106)))"
1900
1901}
1902
1903test_pmtu_vti4_default_mtu() {
1904	setup namespaces veth vti4 || return $ksft_skip
1905
1906	# Check that MTU of vti device is MTU of veth minus IPv4 header length
1907	veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
1908	vti4_mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1909	if [ $((veth_mtu - vti4_mtu)) -ne 20 ]; then
1910		err "  vti MTU ${vti4_mtu} is not veth MTU ${veth_mtu} minus IPv4 header length"
1911		return 1
1912	fi
1913}
1914
1915test_pmtu_vti6_default_mtu() {
1916	setup namespaces veth vti6 || return $ksft_skip
1917
1918	# Check that MTU of vti device is MTU of veth minus IPv6 header length
1919	veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
1920	vti6_mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1921	if [ $((veth_mtu - vti6_mtu)) -ne 40 ]; then
1922		err "  vti MTU ${vti6_mtu} is not veth MTU ${veth_mtu} minus IPv6 header length"
1923		return 1
1924	fi
1925}
1926
1927test_pmtu_vti4_link_add_mtu() {
1928	setup namespaces || return $ksft_skip
1929
1930	run_cmd ${ns_a} ip link add vti4_a type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1931	[ $? -ne 0 ] && err "  vti not supported" && return $ksft_skip
1932	run_cmd ${ns_a} ip link del vti4_a
1933
1934	fail=0
1935
1936	min=68
1937	max=$((65535 - 20))
1938	# Check invalid values first
1939	for v in $((min - 1)) $((max + 1)); do
1940		run_cmd ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1941		# This can fail, or MTU can be adjusted to a proper value
1942		[ $? -ne 0 ] && continue
1943		mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1944		if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
1945			err "  vti tunnel created with invalid MTU ${mtu}"
1946			fail=1
1947		fi
1948		run_cmd ${ns_a} ip link del vti4_a
1949	done
1950
1951	# Now check valid values
1952	for v in ${min} 1300 ${max}; do
1953		run_cmd ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1954		mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1955		run_cmd ${ns_a} ip link del vti4_a
1956		if [ "${mtu}" != "${v}" ]; then
1957			err "  vti MTU ${mtu} doesn't match configured value ${v}"
1958			fail=1
1959		fi
1960	done
1961
1962	return ${fail}
1963}
1964
1965test_pmtu_vti6_link_add_mtu() {
1966	setup namespaces || return $ksft_skip
1967
1968	run_cmd ${ns_a} ip link add vti6_a type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1969	[ $? -ne 0 ] && err "  vti6 not supported" && return $ksft_skip
1970	run_cmd ${ns_a} ip link del vti6_a
1971
1972	fail=0
1973
1974	min=68			# vti6 can carry IPv4 packets too
1975	max=$((65535 - 40))
1976	# Check invalid values first
1977	for v in $((min - 1)) $((max + 1)); do
1978		run_cmd ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1979		# This can fail, or MTU can be adjusted to a proper value
1980		[ $? -ne 0 ] && continue
1981		mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1982		if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
1983			err "  vti6 tunnel created with invalid MTU ${v}"
1984			fail=1
1985		fi
1986		run_cmd ${ns_a} ip link del vti6_a
1987	done
1988
1989	# Now check valid values
1990	for v in 68 1280 1300 $((65535 - 40)); do
1991		run_cmd ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1992		mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1993		run_cmd ${ns_a} ip link del vti6_a
1994		if [ "${mtu}" != "${v}" ]; then
1995			err "  vti6 MTU ${mtu} doesn't match configured value ${v}"
1996			fail=1
1997		fi
1998	done
1999
2000	return ${fail}
2001}
2002
2003test_pmtu_vti6_link_change_mtu() {
2004	setup namespaces || return $ksft_skip
2005
2006	run_cmd ${ns_a} ip link add dummy0 mtu 1500 type dummy
2007	[ $? -ne 0 ] && err "  dummy not supported" && return $ksft_skip
2008	run_cmd ${ns_a} ip link add dummy1 mtu 3000 type dummy
2009	run_cmd ${ns_a} ip link set dummy0 up
2010	run_cmd ${ns_a} ip link set dummy1 up
2011
2012	run_cmd ${ns_a} ip addr add ${dummy6_0_prefix}1/${dummy6_mask} dev dummy0
2013	run_cmd ${ns_a} ip addr add ${dummy6_1_prefix}1/${dummy6_mask} dev dummy1
2014
2015	fail=0
2016
2017	# Create vti6 interface bound to device, passing MTU, check it
2018	run_cmd ${ns_a} ip link add vti6_a mtu 1300 type vti6 remote ${dummy6_0_prefix}2 local ${dummy6_0_prefix}1
2019	mtu="$(link_get_mtu "${ns_a}" vti6_a)"
2020	if [ ${mtu} -ne 1300 ]; then
2021		err "  vti6 MTU ${mtu} doesn't match configured value 1300"
2022		fail=1
2023	fi
2024
2025	# Move to another device with different MTU, without passing MTU, check
2026	# MTU is adjusted
2027	run_cmd ${ns_a} ip link set vti6_a type vti6 remote ${dummy6_1_prefix}2 local ${dummy6_1_prefix}1
2028	mtu="$(link_get_mtu "${ns_a}" vti6_a)"
2029	if [ ${mtu} -ne $((3000 - 40)) ]; then
2030		err "  vti MTU ${mtu} is not dummy MTU 3000 minus IPv6 header length"
2031		fail=1
2032	fi
2033
2034	# Move it back, passing MTU, check MTU is not overridden
2035	run_cmd ${ns_a} ip link set vti6_a mtu 1280 type vti6 remote ${dummy6_0_prefix}2 local ${dummy6_0_prefix}1
2036	mtu="$(link_get_mtu "${ns_a}" vti6_a)"
2037	if [ ${mtu} -ne 1280 ]; then
2038		err "  vti6 MTU ${mtu} doesn't match configured value 1280"
2039		fail=1
2040	fi
2041
2042	return ${fail}
2043}
2044
2045check_command() {
2046	cmd=${1}
2047
2048	if ! which ${cmd} > /dev/null 2>&1; then
2049		err "  missing required command: '${cmd}'"
2050		return 1
2051	fi
2052	return 0
2053}
2054
2055check_running() {
2056	pid=${1}
2057	cmd=${2}
2058
2059	[ "$(cat /proc/${pid}/cmdline 2>/dev/null | tr -d '\0')" = "{cmd}" ]
2060}
2061
2062test_cleanup_vxlanX_exception() {
2063	outer="${1}"
2064	encap="vxlan"
2065	ll_mtu=4000
2066
2067	check_command taskset || return $ksft_skip
2068	cpu_list=$(grep -m 2 processor /proc/cpuinfo | cut -d ' ' -f 2)
2069
2070	setup namespaces routing ${encap}${outer} || return $ksft_skip
2071	trace "${ns_a}" ${encap}_a   "${ns_b}"  ${encap}_b \
2072	      "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
2073	      "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
2074
2075	# Create route exception by exceeding link layer MTU
2076	mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
2077	mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
2078	mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
2079	mtu "${ns_r1}" veth_R1-B ${ll_mtu}
2080
2081	mtu "${ns_a}" ${encap}_a $((${ll_mtu} + 1000))
2082	mtu "${ns_b}" ${encap}_b $((${ll_mtu} + 1000))
2083
2084	# Fill exception cache for multiple CPUs (2)
2085	# we can always use inner IPv4 for that
2086	for cpu in ${cpu_list}; do
2087		run_cmd taskset --cpu-list ${cpu} ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${tunnel4_b_addr}
2088	done
2089
2090	${ns_a} ip link del dev veth_A-R1 &
2091	iplink_pid=$!
2092	for i in $(seq 1 20); do
2093		check_running ${iplink_pid} "iplinkdeldevveth_A-R1" || return 0
2094		sleep 0.1
2095	done
2096	err "  can't delete veth device in a timely manner, PMTU dst likely leaked"
2097	return 1
2098}
2099
2100test_cleanup_ipv6_exception() {
2101	test_cleanup_vxlanX_exception 6
2102}
2103
2104test_cleanup_ipv4_exception() {
2105	test_cleanup_vxlanX_exception 4
2106}
2107
2108run_test() {
2109	(
2110	tname="$1"
2111	tdesc="$2"
2112
2113	unset IFS
2114
2115	# Since cleanup() relies on variables modified by this subshell, it
2116	# has to run in this context.
2117	trap cleanup EXIT
2118
2119	if [ "$VERBOSE" = "1" ]; then
2120		printf "\n##########################################################################\n\n"
2121	fi
2122
2123	eval test_${tname}
2124	ret=$?
2125
2126	if [ $ret -eq 0 ]; then
2127		printf "TEST: %-60s  [ OK ]\n" "${tdesc}"
2128	elif [ $ret -eq 1 ]; then
2129		printf "TEST: %-60s  [FAIL]\n" "${tdesc}"
2130		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
2131			echo
2132			echo "Pausing. Hit enter to continue"
2133			read a
2134		fi
2135		err_flush
2136		exit 1
2137	elif [ $ret -eq $ksft_skip ]; then
2138		printf "TEST: %-60s  [SKIP]\n" "${tdesc}"
2139		err_flush
2140	fi
2141
2142	return $ret
2143	)
2144	ret=$?
2145	case $ret in
2146		0)
2147			all_skipped=false
2148			[ $exitcode -eq $ksft_skip ] && exitcode=0
2149		;;
2150		$ksft_skip)
2151			[ $all_skipped = true ] && exitcode=$ksft_skip
2152		;;
2153		*)
2154			all_skipped=false
2155			exitcode=1
2156		;;
2157	esac
2158
2159	return $ret
2160}
2161
2162run_test_nh() {
2163	tname="$1"
2164	tdesc="$2"
2165
2166	USE_NH=yes
2167	run_test "${tname}" "${tdesc} - nexthop objects"
2168	USE_NH=no
2169}
2170
2171test_list_flush_ipv4_exception() {
2172	setup namespaces routing || return $ksft_skip
2173	trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
2174	      "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
2175	      "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
2176	      "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
2177
2178	dst_prefix1="${prefix4}.${b_r1}."
2179	dst2="${prefix4}.${b_r2}.1"
2180
2181	# Set up initial MTU values
2182	mtu "${ns_a}"  veth_A-R1 2000
2183	mtu "${ns_r1}" veth_R1-A 2000
2184	mtu "${ns_r1}" veth_R1-B 1500
2185	mtu "${ns_b}"  veth_B-R1 1500
2186
2187	mtu "${ns_a}"  veth_A-R2 2000
2188	mtu "${ns_r2}" veth_R2-A 2000
2189	mtu "${ns_r2}" veth_R2-B 1500
2190	mtu "${ns_b}"  veth_B-R2 1500
2191
2192	fail=0
2193
2194	# Add 100 addresses for veth endpoint on B reached by default A route
2195	for i in $(seq 100 199); do
2196		run_cmd ${ns_b} ip addr add "${dst_prefix1}${i}" dev veth_B-R1
2197	done
2198
2199	# Create 100 cached route exceptions for path via R1, one via R2. Note
2200	# that with IPv4 we need to actually cause a route lookup that matches
2201	# the exception caused by ICMP, in order to actually have a cached
2202	# route, so we need to ping each destination twice
2203	for i in $(seq 100 199); do
2204		run_cmd ${ns_a} ping -q -M want -i 0.1 -c 2 -s 1800 "${dst_prefix1}${i}"
2205	done
2206	run_cmd ${ns_a} ping -q -M want -i 0.1 -c 2 -s 1800 "${dst2}"
2207
2208	if [ "$(${ns_a} ip -oneline route list cache | wc -l)" -ne 101 ]; then
2209		err "  can't list cached exceptions"
2210		fail=1
2211	fi
2212
2213	run_cmd ${ns_a} ip route flush cache
2214	pmtu1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst_prefix}1)"
2215	pmtu2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst_prefix}2)"
2216	if [ -n "${pmtu1}" ] || [ -n "${pmtu2}" ] || \
2217	   [ -n "$(${ns_a} ip route list cache)" ]; then
2218		err "  can't flush cached exceptions"
2219		fail=1
2220	fi
2221
2222	return ${fail}
2223}
2224
2225test_list_flush_ipv6_exception() {
2226	setup namespaces routing || return $ksft_skip
2227	trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
2228	      "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
2229	      "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
2230	      "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
2231
2232	dst_prefix1="${prefix6}:${b_r1}::"
2233	dst2="${prefix6}:${b_r2}::1"
2234
2235	# Set up initial MTU values
2236	mtu "${ns_a}"  veth_A-R1 2000
2237	mtu "${ns_r1}" veth_R1-A 2000
2238	mtu "${ns_r1}" veth_R1-B 1500
2239	mtu "${ns_b}"  veth_B-R1 1500
2240
2241	mtu "${ns_a}"  veth_A-R2 2000
2242	mtu "${ns_r2}" veth_R2-A 2000
2243	mtu "${ns_r2}" veth_R2-B 1500
2244	mtu "${ns_b}"  veth_B-R2 1500
2245
2246	fail=0
2247
2248	# Add 100 addresses for veth endpoint on B reached by default A route
2249	for i in $(seq 100 199); do
2250		run_cmd ${ns_b} ip addr add "${dst_prefix1}${i}" dev veth_B-R1
2251	done
2252
2253	# Create 100 cached route exceptions for path via R1, one via R2
2254	for i in $(seq 100 199); do
2255		run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s 1800 "${dst_prefix1}${i}"
2256	done
2257	run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s 1800 "${dst2}"
2258	if [ "$(${ns_a} ip -oneline -6 route list cache | wc -l)" -ne 101 ]; then
2259		err "  can't list cached exceptions"
2260		fail=1
2261	fi
2262
2263	run_cmd ${ns_a} ip -6 route flush cache
2264	pmtu1="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst_prefix1}100")"
2265	pmtu2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
2266	if [ -n "${pmtu1}" ] || [ -n "${pmtu2}" ] || \
2267	   [ -n "$(${ns_a} ip -6 route list cache)" ]; then
2268		err "  can't flush cached exceptions"
2269		fail=1
2270	fi
2271
2272	return ${fail}
2273}
2274
2275test_pmtu_ipvX_route_change() {
2276	family=${1}
2277
2278	setup namespaces routing || return 2
2279	trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
2280	      "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
2281	      "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
2282	      "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
2283
2284	if [ ${family} -eq 4 ]; then
2285		ping=ping
2286		dst1="${prefix4}.${b_r1}.1"
2287		dst2="${prefix4}.${b_r2}.1"
2288		gw="${prefix4}.${a_r1}.2"
2289	else
2290		ping=${ping6}
2291		dst1="${prefix6}:${b_r1}::1"
2292		dst2="${prefix6}:${b_r2}::1"
2293		gw="${prefix6}:${a_r1}::2"
2294	fi
2295
2296	# Set up initial MTU values
2297	mtu "${ns_a}"  veth_A-R1 2000
2298	mtu "${ns_r1}" veth_R1-A 2000
2299	mtu "${ns_r1}" veth_R1-B 1400
2300	mtu "${ns_b}"  veth_B-R1 1400
2301
2302	mtu "${ns_a}"  veth_A-R2 2000
2303	mtu "${ns_r2}" veth_R2-A 2000
2304	mtu "${ns_r2}" veth_R2-B 1500
2305	mtu "${ns_b}"  veth_B-R2 1500
2306
2307	# Create route exceptions
2308	run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst1}
2309	run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst2}
2310
2311	# Check that exceptions have been created with the correct PMTU
2312	pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
2313	check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
2314	pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
2315	check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
2316
2317	# Replace the route from A to R1
2318	run_cmd ${ns_a} ip route change default via ${gw}
2319
2320	# Delete the device in A
2321	run_cmd ${ns_a} ip link del "veth_A-R1"
2322}
2323
2324test_pmtu_ipv4_route_change() {
2325	test_pmtu_ipvX_route_change 4
2326}
2327
2328test_pmtu_ipv6_route_change() {
2329	test_pmtu_ipvX_route_change 6
2330}
2331
2332usage() {
2333	echo
2334	echo "$0 [OPTIONS] [TEST]..."
2335	echo "If no TEST argument is given, all tests will be run."
2336	echo
2337	echo "Options"
2338	echo "  --trace: capture traffic to TEST_INTERFACE.pcap"
2339	echo
2340	echo "Available tests${tests}"
2341	exit 1
2342}
2343
2344################################################################################
2345#
2346exitcode=0
2347desc=0
2348all_skipped=true
2349
2350while getopts :ptv o
2351do
2352	case $o in
2353	p) PAUSE_ON_FAIL=yes;;
2354	v) VERBOSE=1;;
2355	t) if which tcpdump > /dev/null 2>&1; then
2356		TRACING=1
2357	   else
2358		echo "=== tcpdump not available, tracing disabled"
2359	   fi
2360	   ;;
2361	*) usage;;
2362	esac
2363done
2364shift $(($OPTIND-1))
2365
2366IFS="
2367"
2368
2369for arg do
2370	# Check first that all requested tests are available before running any
2371	command -v > /dev/null "test_${arg}" || { echo "=== Test ${arg} not found"; usage; }
2372done
2373
2374trap cleanup EXIT
2375
2376# start clean
2377cleanup
2378
2379HAVE_NH=no
2380ip nexthop ls >/dev/null 2>&1
2381[ $? -eq 0 ] && HAVE_NH=yes
2382
2383name=""
2384desc=""
2385rerun_nh=0
2386for t in ${tests}; do
2387	[ "${name}" = "" ]	&& name="${t}"	&& continue
2388	[ "${desc}" = "" ]	&& desc="${t}"	&& continue
2389
2390	if [ "${HAVE_NH}" = "yes" ]; then
2391		rerun_nh="${t}"
2392	fi
2393
2394	run_this=1
2395	for arg do
2396		[ "${arg}" != "${arg#--*}" ] && continue
2397		[ "${arg}" = "${name}" ] && run_this=1 && break
2398		run_this=0
2399	done
2400	if [ $run_this -eq 1 ]; then
2401		run_test "${name}" "${desc}"
2402		# if test was skipped no need to retry with nexthop objects
2403		[ $? -eq $ksft_skip ] && rerun_nh=0
2404
2405		if [ "${rerun_nh}" = "1" ]; then
2406			run_test_nh "${name}" "${desc}"
2407		fi
2408	fi
2409	name=""
2410	desc=""
2411	rerun_nh=0
2412done
2413
2414exit ${exitcode}
2415