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