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