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