blob: f72001008db007c08adaf5e83b3be21d2125fbf3 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/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_vxlan4_exception
30# Set up the same network topology as pmtu_ipv4, create a VXLAN tunnel
31# over IPv4 between A and B, routed via R1. On the link between R1 and B,
32# set a MTU lower than the VXLAN MTU and the MTU on the link between A and
33# R1. Send IPv4 packets, exceeding the MTU between R1 and B, over VXLAN
34# from A to B and check that the PMTU exception is created with the right
35# value on A
36#
37# - pmtu_ipv6_vxlan4_exception
38# Same as pmtu_ipv4_vxlan4_exception, but send IPv6 packets from A to B
39#
40# - pmtu_ipv4_vxlan6_exception
41# Same as pmtu_ipv4_vxlan4_exception, but use IPv6 transport from A to B
42#
43# - pmtu_ipv6_vxlan6_exception
44# Same as pmtu_ipv4_vxlan6_exception, but send IPv6 packets from A to B
45#
46# - pmtu_ipv4_geneve4_exception
47# Same as pmtu_ipv4_vxlan4_exception, but using a GENEVE tunnel instead of
48# VXLAN
49#
50# - pmtu_ipv6_geneve4_exception
51# Same as pmtu_ipv6_vxlan4_exception, but using a GENEVE tunnel instead of
52# VXLAN
53#
54# - pmtu_ipv4_geneve6_exception
55# Same as pmtu_ipv4_vxlan6_exception, but using a GENEVE tunnel instead of
56# VXLAN
57#
58# - pmtu_ipv6_geneve6_exception
59# Same as pmtu_ipv6_vxlan6_exception, but using a GENEVE tunnel instead of
60# VXLAN
61#
62# - pmtu_ipv{4,6}_fou{4,6}_exception
63# Same as pmtu_ipv4_vxlan4, but using a direct IPv4/IPv6 encapsulation
64# (FoU) over IPv4/IPv6, instead of VXLAN
65#
66# - pmtu_ipv{4,6}_fou{4,6}_exception
67# Same as pmtu_ipv4_vxlan4, but using a generic UDP IPv4/IPv6
68# encapsulation (GUE) over IPv4/IPv6, instead of VXLAN
69#
70# - pmtu_vti4_exception
71# Set up vti tunnel on top of veth, with xfrm states and policies, in two
72# namespaces with matching endpoints. Check that route exception is not
73# created if link layer MTU is not exceeded, then exceed it and check that
74# exception is created with the expected PMTU. The approach described
75# below for IPv6 doesn't apply here, because, on IPv4, administrative MTU
76# changes alone won't affect PMTU
77#
78# - pmtu_vti6_exception
79# Set up vti6 tunnel on top of veth, with xfrm states and policies, in two
80# namespaces with matching endpoints. Check that route exception is
81# created by exceeding link layer MTU with ping to other endpoint. Then
82# decrease and increase MTU of tunnel, checking that route exception PMTU
83# changes accordingly
84#
85# - pmtu_vti4_default_mtu
86# Set up vti4 tunnel on top of veth, in two namespaces with matching
87# endpoints. Check that MTU assigned to vti interface is the MTU of the
88# lower layer (veth) minus additional lower layer headers (zero, for veth)
89# minus IPv4 header length
90#
91# - pmtu_vti6_default_mtu
92# Same as above, for IPv6
93#
94# - pmtu_vti4_link_add_mtu
95# Set up vti4 interface passing MTU value at link creation, check MTU is
96# configured, and that link is not created with invalid MTU values
97#
98# - pmtu_vti6_link_add_mtu
99# Same as above, for IPv6
100#
101# - pmtu_vti6_link_change_mtu
102# Set up two dummy interfaces with different MTUs, create a vti6 tunnel
103# and check that configured MTU is used on link creation and changes, and
104# that MTU is properly calculated instead when MTU is not configured from
105# userspace
106#
107# - cleanup_ipv4_exception
108# Similar to pmtu_ipv4_vxlan4_exception, but explicitly generate PMTU
109# exceptions on multiple CPUs and check that the veth device tear-down
110# happens in a timely manner
111#
112# - cleanup_ipv6_exception
113# Same as above, but use IPv6 transport from A to B
114#
115# - list_flush_ipv4_exception
116# Using the same topology as in pmtu_ipv4, create exceptions, and check
117# they are shown when listing exception caches, gone after flushing them
118#
119# - list_flush_ipv6_exception
120# Using the same topology as in pmtu_ipv6, create exceptions, and check
121# they are shown when listing exception caches, gone after flushing them
122#
123# - pmtu_ipv4_route_change
124# Use the same topology as in pmtu_ipv4, but issue a route replacement
125# command and delete the corresponding device afterward. This tests for
126# proper cleanup of the PMTU exceptions by the route replacement path.
127# Device unregistration should complete successfully
128#
129# - pmtu_ipv6_route_change
130# Same as above but with IPv6
131
132# Kselftest framework requirement - SKIP code is 4.
133ksft_skip=4
134
135PAUSE_ON_FAIL=no
136VERBOSE=0
137TRACING=0
138
139# Some systems don't have a ping6 binary anymore
140which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
141
142# Name Description re-run with nh
143tests="
144 pmtu_ipv4_exception ipv4: PMTU exceptions 1
145 pmtu_ipv6_exception ipv6: PMTU exceptions 1
146 pmtu_ipv4_vxlan4_exception IPv4 over vxlan4: PMTU exceptions 1
147 pmtu_ipv6_vxlan4_exception IPv6 over vxlan4: PMTU exceptions 1
148 pmtu_ipv4_vxlan6_exception IPv4 over vxlan6: PMTU exceptions 1
149 pmtu_ipv6_vxlan6_exception IPv6 over vxlan6: PMTU exceptions 1
150 pmtu_ipv4_geneve4_exception IPv4 over geneve4: PMTU exceptions 1
151 pmtu_ipv6_geneve4_exception IPv6 over geneve4: PMTU exceptions 1
152 pmtu_ipv4_geneve6_exception IPv4 over geneve6: PMTU exceptions 1
153 pmtu_ipv6_geneve6_exception IPv6 over geneve6: PMTU exceptions 1
154 pmtu_ipv4_fou4_exception IPv4 over fou4: PMTU exceptions 1
155 pmtu_ipv6_fou4_exception IPv6 over fou4: PMTU exceptions 1
156 pmtu_ipv4_fou6_exception IPv4 over fou6: PMTU exceptions 1
157 pmtu_ipv6_fou6_exception IPv6 over fou6: PMTU exceptions 1
158 pmtu_ipv4_gue4_exception IPv4 over gue4: PMTU exceptions 1
159 pmtu_ipv6_gue4_exception IPv6 over gue4: PMTU exceptions 1
160 pmtu_ipv4_gue6_exception IPv4 over gue6: PMTU exceptions 1
161 pmtu_ipv6_gue6_exception IPv6 over gue6: PMTU exceptions 1
162 pmtu_vti6_exception vti6: PMTU exceptions 0
163 pmtu_vti4_exception vti4: PMTU exceptions 0
164 pmtu_vti4_default_mtu vti4: default MTU assignment 0
165 pmtu_vti6_default_mtu vti6: default MTU assignment 0
166 pmtu_vti4_link_add_mtu vti4: MTU setting on link creation 0
167 pmtu_vti6_link_add_mtu vti6: MTU setting on link creation 0
168 pmtu_vti6_link_change_mtu vti6: MTU changes on link changes 0
169 cleanup_ipv4_exception ipv4: cleanup of cached exceptions 1
170 cleanup_ipv6_exception ipv6: cleanup of cached exceptions 1
171 list_flush_ipv4_exception ipv4: list and flush cached exceptions 1
172 list_flush_ipv6_exception ipv6: list and flush cached exceptions 1
173 pmtu_ipv4_route_change ipv4: PMTU exception w/route replace 1
174 pmtu_ipv6_route_change ipv6: PMTU exception w/route replace 1"
175
176NS_A="ns-A"
177NS_B="ns-B"
178NS_R1="ns-R1"
179NS_R2="ns-R2"
180ns_a="ip netns exec ${NS_A}"
181ns_b="ip netns exec ${NS_B}"
182ns_r1="ip netns exec ${NS_R1}"
183ns_r2="ip netns exec ${NS_R2}"
184
185# Addressing and routing for tests with routers: four network segments, with
186# index SEGMENT between 1 and 4, a common prefix (PREFIX4 or PREFIX6) and an
187# identifier ID, which is 1 for hosts (A and B), 2 for routers (R1 and R2).
188# Addresses are:
189# - IPv4: PREFIX4.SEGMENT.ID (/24)
190# - IPv6: PREFIX6:SEGMENT::ID (/64)
191prefix4="10.0"
192prefix6="fc00"
193a_r1=1
194a_r2=2
195b_r1=3
196b_r2=4
197# ns peer segment
198routing_addrs="
199 A R1 ${a_r1}
200 A R2 ${a_r2}
201 B R1 ${b_r1}
202 B R2 ${b_r2}
203"
204# Traffic from A to B goes through R1 by default, and through R2, if destined to
205# B's address on the b_r2 segment.
206# Traffic from B to A goes through R1.
207# ns destination gateway
208routes="
209 A default ${prefix4}.${a_r1}.2
210 A ${prefix4}.${b_r2}.1 ${prefix4}.${a_r2}.2
211 B default ${prefix4}.${b_r1}.2
212
213 A default ${prefix6}:${a_r1}::2
214 A ${prefix6}:${b_r2}::1 ${prefix6}:${a_r2}::2
215 B default ${prefix6}:${b_r1}::2
216"
217
218USE_NH="no"
219# ns family nh id destination gateway
220nexthops="
221 A 4 41 ${prefix4}.${a_r1}.2 veth_A-R1
222 A 4 42 ${prefix4}.${a_r2}.2 veth_A-R2
223 B 4 41 ${prefix4}.${b_r1}.2 veth_B-R1
224
225 A 6 61 ${prefix6}:${a_r1}::2 veth_A-R1
226 A 6 62 ${prefix6}:${a_r2}::2 veth_A-R2
227 B 6 61 ${prefix6}:${b_r1}::2 veth_B-R1
228"
229
230# nexthop id correlates to id in nexthops config above
231# ns family prefix nh id
232routes_nh="
233 A 4 default 41
234 A 4 ${prefix4}.${b_r2}.1 42
235 B 4 default 41
236
237 A 6 default 61
238 A 6 ${prefix6}:${b_r2}::1 62
239 B 6 default 61
240"
241
242veth4_a_addr="192.168.1.1"
243veth4_b_addr="192.168.1.2"
244veth4_mask="24"
245veth6_a_addr="fd00:1::a"
246veth6_b_addr="fd00:1::b"
247veth6_mask="64"
248
249tunnel4_a_addr="192.168.2.1"
250tunnel4_b_addr="192.168.2.2"
251tunnel4_mask="24"
252tunnel6_a_addr="fd00:2::a"
253tunnel6_b_addr="fd00:2::b"
254tunnel6_mask="64"
255
256dummy6_0_prefix="fc00:1000::"
257dummy6_1_prefix="fc00:1001::"
258dummy6_mask="64"
259
260err_buf=
261tcpdump_pids=
262
263err() {
264 err_buf="${err_buf}${1}
265"
266}
267
268err_flush() {
269 echo -n "${err_buf}"
270 err_buf=
271}
272
273run_cmd() {
274 cmd="$*"
275
276 if [ "$VERBOSE" = "1" ]; then
277 printf " COMMAND: $cmd\n"
278 fi
279
280 out="$($cmd 2>&1)"
281 rc=$?
282 if [ "$VERBOSE" = "1" -a -n "$out" ]; then
283 echo " $out"
284 echo
285 fi
286
287 return $rc
288}
289
290# Find the auto-generated name for this namespace
291nsname() {
292 eval echo \$NS_$1
293}
294
295setup_fou_or_gue() {
296 outer="${1}"
297 inner="${2}"
298 encap="${3}"
299
300 if [ "${outer}" = "4" ]; then
301 modprobe fou || return 2
302 a_addr="${prefix4}.${a_r1}.1"
303 b_addr="${prefix4}.${b_r1}.1"
304 if [ "${inner}" = "4" ]; then
305 type="ipip"
306 ipproto="4"
307 else
308 type="sit"
309 ipproto="41"
310 fi
311 else
312 modprobe fou6 || return 2
313 a_addr="${prefix6}:${a_r1}::1"
314 b_addr="${prefix6}:${b_r1}::1"
315 if [ "${inner}" = "4" ]; then
316 type="ip6tnl"
317 mode="mode ipip6"
318 ipproto="4 -6"
319 else
320 type="ip6tnl"
321 mode="mode ip6ip6"
322 ipproto="41 -6"
323 fi
324 fi
325
326 run_cmd ${ns_a} ip fou add port 5555 ipproto ${ipproto} || return 2
327 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 2
328
329 run_cmd ${ns_b} ip fou add port 5556 ipproto ${ipproto}
330 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
331
332 if [ "${inner}" = "4" ]; then
333 run_cmd ${ns_a} ip addr add ${tunnel4_a_addr}/${tunnel4_mask} dev ${encap}_a
334 run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ${encap}_b
335 else
336 run_cmd ${ns_a} ip addr add ${tunnel6_a_addr}/${tunnel6_mask} dev ${encap}_a
337 run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ${encap}_b
338 fi
339
340 run_cmd ${ns_a} ip link set ${encap}_a up
341 run_cmd ${ns_b} ip link set ${encap}_b up
342}
343
344setup_fou44() {
345 setup_fou_or_gue 4 4 fou
346}
347
348setup_fou46() {
349 setup_fou_or_gue 4 6 fou
350}
351
352setup_fou64() {
353 setup_fou_or_gue 6 4 fou
354}
355
356setup_fou66() {
357 setup_fou_or_gue 6 6 fou
358}
359
360setup_gue44() {
361 setup_fou_or_gue 4 4 gue
362}
363
364setup_gue46() {
365 setup_fou_or_gue 4 6 gue
366}
367
368setup_gue64() {
369 setup_fou_or_gue 6 4 gue
370}
371
372setup_gue66() {
373 setup_fou_or_gue 6 6 gue
374}
375
376setup_namespaces() {
377 for n in ${NS_A} ${NS_B} ${NS_R1} ${NS_R2}; do
378 ip netns add ${n} || return 1
379
380 # Disable DAD, so that we don't have to wait to use the
381 # configured IPv6 addresses
382 ip netns exec ${n} sysctl -q net/ipv6/conf/default/accept_dad=0
383 done
384}
385
386setup_veth() {
387 run_cmd ${ns_a} ip link add veth_a type veth peer name veth_b || return 1
388 run_cmd ${ns_a} ip link set veth_b netns ${NS_B}
389
390 run_cmd ${ns_a} ip addr add ${veth4_a_addr}/${veth4_mask} dev veth_a
391 run_cmd ${ns_b} ip addr add ${veth4_b_addr}/${veth4_mask} dev veth_b
392
393 run_cmd ${ns_a} ip addr add ${veth6_a_addr}/${veth6_mask} dev veth_a
394 run_cmd ${ns_b} ip addr add ${veth6_b_addr}/${veth6_mask} dev veth_b
395
396 run_cmd ${ns_a} ip link set veth_a up
397 run_cmd ${ns_b} ip link set veth_b up
398}
399
400setup_vti() {
401 proto=${1}
402 veth_a_addr="${2}"
403 veth_b_addr="${3}"
404 vti_a_addr="${4}"
405 vti_b_addr="${5}"
406 vti_mask=${6}
407
408 [ ${proto} -eq 6 ] && vti_type="vti6" || vti_type="vti"
409
410 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
411 run_cmd ${ns_b} ip link add vti${proto}_b type ${vti_type} local ${veth_b_addr} remote ${veth_a_addr} key 10
412
413 run_cmd ${ns_a} ip addr add ${vti_a_addr}/${vti_mask} dev vti${proto}_a
414 run_cmd ${ns_b} ip addr add ${vti_b_addr}/${vti_mask} dev vti${proto}_b
415
416 run_cmd ${ns_a} ip link set vti${proto}_a up
417 run_cmd ${ns_b} ip link set vti${proto}_b up
418}
419
420setup_vti4() {
421 setup_vti 4 ${veth4_a_addr} ${veth4_b_addr} ${tunnel4_a_addr} ${tunnel4_b_addr} ${tunnel4_mask}
422}
423
424setup_vti6() {
425 setup_vti 6 ${veth6_a_addr} ${veth6_b_addr} ${tunnel6_a_addr} ${tunnel6_b_addr} ${tunnel6_mask}
426}
427
428setup_vxlan_or_geneve() {
429 type="${1}"
430 a_addr="${2}"
431 b_addr="${3}"
432 opts="${4}"
433
434 if [ "${type}" = "vxlan" ]; then
435 opts="${opts} ttl 64 dstport 4789"
436 opts_a="local ${a_addr}"
437 opts_b="local ${b_addr}"
438 else
439 opts_a=""
440 opts_b=""
441 fi
442
443 run_cmd ${ns_a} ip link add ${type}_a type ${type} id 1 ${opts_a} remote ${b_addr} ${opts} || return 1
444 run_cmd ${ns_b} ip link add ${type}_b type ${type} id 1 ${opts_b} remote ${a_addr} ${opts}
445
446 run_cmd ${ns_a} ip addr add ${tunnel4_a_addr}/${tunnel4_mask} dev ${type}_a
447 run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ${type}_b
448
449 run_cmd ${ns_a} ip addr add ${tunnel6_a_addr}/${tunnel6_mask} dev ${type}_a
450 run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ${type}_b
451
452 run_cmd ${ns_a} ip link set ${type}_a up
453 run_cmd ${ns_b} ip link set ${type}_b up
454}
455
456setup_geneve4() {
457 setup_vxlan_or_geneve geneve ${prefix4}.${a_r1}.1 ${prefix4}.${b_r1}.1 "df set"
458}
459
460setup_vxlan4() {
461 setup_vxlan_or_geneve vxlan ${prefix4}.${a_r1}.1 ${prefix4}.${b_r1}.1 "df set"
462}
463
464setup_geneve6() {
465 setup_vxlan_or_geneve geneve ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1
466}
467
468setup_vxlan6() {
469 setup_vxlan_or_geneve vxlan ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1
470}
471
472setup_xfrm() {
473 proto=${1}
474 veth_a_addr="${2}"
475 veth_b_addr="${3}"
476
477 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 || return 1
478 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
479 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
480 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
481
482 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
483 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
484 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
485 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
486}
487
488setup_xfrm4() {
489 setup_xfrm 4 ${veth4_a_addr} ${veth4_b_addr}
490}
491
492setup_xfrm6() {
493 setup_xfrm 6 ${veth6_a_addr} ${veth6_b_addr}
494}
495
496setup_routing_old() {
497 for i in ${routes}; do
498 [ "${ns}" = "" ] && ns="${i}" && continue
499 [ "${addr}" = "" ] && addr="${i}" && continue
500 [ "${gw}" = "" ] && gw="${i}"
501
502 ns_name="$(nsname ${ns})"
503
504 ip -n ${ns_name} route add ${addr} via ${gw}
505
506 ns=""; addr=""; gw=""
507 done
508}
509
510setup_routing_new() {
511 for i in ${nexthops}; do
512 [ "${ns}" = "" ] && ns="${i}" && continue
513 [ "${fam}" = "" ] && fam="${i}" && continue
514 [ "${nhid}" = "" ] && nhid="${i}" && continue
515 [ "${gw}" = "" ] && gw="${i}" && continue
516 [ "${dev}" = "" ] && dev="${i}"
517
518 ns_name="$(nsname ${ns})"
519
520 ip -n ${ns_name} -${fam} nexthop add id ${nhid} via ${gw} dev ${dev}
521
522 ns=""; fam=""; nhid=""; gw=""; dev=""
523
524 done
525
526 for i in ${routes_nh}; do
527 [ "${ns}" = "" ] && ns="${i}" && continue
528 [ "${fam}" = "" ] && fam="${i}" && continue
529 [ "${addr}" = "" ] && addr="${i}" && continue
530 [ "${nhid}" = "" ] && nhid="${i}"
531
532 ns_name="$(nsname ${ns})"
533
534 ip -n ${ns_name} -${fam} route add ${addr} nhid ${nhid}
535
536 ns=""; fam=""; addr=""; nhid=""
537 done
538}
539
540setup_routing() {
541 for i in ${NS_R1} ${NS_R2}; do
542 ip netns exec ${i} sysctl -q net/ipv4/ip_forward=1
543 ip netns exec ${i} sysctl -q net/ipv6/conf/all/forwarding=1
544 done
545
546 for i in ${routing_addrs}; do
547 [ "${ns}" = "" ] && ns="${i}" && continue
548 [ "${peer}" = "" ] && peer="${i}" && continue
549 [ "${segment}" = "" ] && segment="${i}"
550
551 ns_name="$(nsname ${ns})"
552 peer_name="$(nsname ${peer})"
553 if="veth_${ns}-${peer}"
554 ifpeer="veth_${peer}-${ns}"
555
556 # Create veth links
557 ip link add ${if} up netns ${ns_name} type veth peer name ${ifpeer} netns ${peer_name} || return 1
558 ip -n ${peer_name} link set dev ${ifpeer} up
559
560 # Add addresses
561 ip -n ${ns_name} addr add ${prefix4}.${segment}.1/24 dev ${if}
562 ip -n ${ns_name} addr add ${prefix6}:${segment}::1/64 dev ${if}
563
564 ip -n ${peer_name} addr add ${prefix4}.${segment}.2/24 dev ${ifpeer}
565 ip -n ${peer_name} addr add ${prefix6}:${segment}::2/64 dev ${ifpeer}
566
567 ns=""; peer=""; segment=""
568 done
569
570 if [ "$USE_NH" = "yes" ]; then
571 setup_routing_new
572 else
573 setup_routing_old
574 fi
575
576 return 0
577}
578
579setup() {
580 [ "$(id -u)" -ne 0 ] && echo " need to run as root" && return $ksft_skip
581
582 for arg do
583 eval setup_${arg} || { echo " ${arg} not supported"; return 1; }
584 done
585}
586
587trace() {
588 [ $TRACING -eq 0 ] && return
589
590 for arg do
591 [ "${ns_cmd}" = "" ] && ns_cmd="${arg}" && continue
592 ${ns_cmd} tcpdump --immediate-mode -s 0 -i "${arg}" -w "${name}_${arg}.pcap" 2> /dev/null &
593 tcpdump_pids="${tcpdump_pids} $!"
594 ns_cmd=
595 done
596 sleep 1
597}
598
599cleanup() {
600 for pid in ${tcpdump_pids}; do
601 kill ${pid}
602 done
603 tcpdump_pids=
604
605 for n in ${NS_A} ${NS_B} ${NS_R1} ${NS_R2}; do
606 ip netns del ${n} 2> /dev/null
607 done
608}
609
610mtu() {
611 ns_cmd="${1}"
612 dev="${2}"
613 mtu="${3}"
614
615 ${ns_cmd} ip link set dev ${dev} mtu ${mtu}
616}
617
618mtu_parse() {
619 input="${1}"
620
621 next=0
622 for i in ${input}; do
623 [ ${next} -eq 1 -a "${i}" = "lock" ] && next=2 && continue
624 [ ${next} -eq 1 ] && echo "${i}" && return
625 [ ${next} -eq 2 ] && echo "lock ${i}" && return
626 [ "${i}" = "mtu" ] && next=1
627 done
628}
629
630link_get() {
631 ns_cmd="${1}"
632 name="${2}"
633
634 ${ns_cmd} ip link show dev "${name}"
635}
636
637link_get_mtu() {
638 ns_cmd="${1}"
639 name="${2}"
640
641 mtu_parse "$(link_get "${ns_cmd}" ${name})"
642}
643
644route_get_dst_exception() {
645 ns_cmd="${1}"
646 dst="${2}"
647
648 ${ns_cmd} ip route get "${dst}"
649}
650
651route_get_dst_pmtu_from_exception() {
652 ns_cmd="${1}"
653 dst="${2}"
654
655 mtu_parse "$(route_get_dst_exception "${ns_cmd}" ${dst})"
656}
657
658check_pmtu_value() {
659 expected="${1}"
660 value="${2}"
661 event="${3}"
662
663 [ "${expected}" = "any" ] && [ -n "${value}" ] && return 0
664 [ "${value}" = "${expected}" ] && return 0
665 [ -z "${value}" ] && err " PMTU exception wasn't created after ${event}" && return 1
666 [ -z "${expected}" ] && err " PMTU exception shouldn't exist after ${event}" && return 1
667 err " found PMTU exception with incorrect MTU ${value}, expected ${expected}, after ${event}"
668 return 1
669}
670
671test_pmtu_ipvX() {
672 family=${1}
673
674 setup namespaces routing || return 2
675 trace "${ns_a}" veth_A-R1 "${ns_r1}" veth_R1-A \
676 "${ns_r1}" veth_R1-B "${ns_b}" veth_B-R1 \
677 "${ns_a}" veth_A-R2 "${ns_r2}" veth_R2-A \
678 "${ns_r2}" veth_R2-B "${ns_b}" veth_B-R2
679
680 if [ ${family} -eq 4 ]; then
681 ping=ping
682 dst1="${prefix4}.${b_r1}.1"
683 dst2="${prefix4}.${b_r2}.1"
684 else
685 ping=${ping6}
686 dst1="${prefix6}:${b_r1}::1"
687 dst2="${prefix6}:${b_r2}::1"
688 fi
689
690 # Set up initial MTU values
691 mtu "${ns_a}" veth_A-R1 2000
692 mtu "${ns_r1}" veth_R1-A 2000
693 mtu "${ns_r1}" veth_R1-B 1400
694 mtu "${ns_b}" veth_B-R1 1400
695
696 mtu "${ns_a}" veth_A-R2 2000
697 mtu "${ns_r2}" veth_R2-A 2000
698 mtu "${ns_r2}" veth_R2-B 1500
699 mtu "${ns_b}" veth_B-R2 1500
700
701 # Create route exceptions
702 run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst1}
703 run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst2}
704
705 # Check that exceptions have been created with the correct PMTU
706 pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
707 check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
708 pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
709 check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
710
711 # Decrease local MTU below PMTU, check for PMTU decrease in route exception
712 mtu "${ns_a}" veth_A-R1 1300
713 mtu "${ns_r1}" veth_R1-A 1300
714 pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
715 check_pmtu_value "1300" "${pmtu_1}" "decreasing local MTU" || return 1
716 # Second exception shouldn't be modified
717 pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
718 check_pmtu_value "1500" "${pmtu_2}" "changing local MTU on a link not on this path" || return 1
719
720 # Increase MTU, check for PMTU increase in route exception
721 mtu "${ns_a}" veth_A-R1 1700
722 mtu "${ns_r1}" veth_R1-A 1700
723 pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
724 check_pmtu_value "1700" "${pmtu_1}" "increasing local MTU" || return 1
725 # Second exception shouldn't be modified
726 pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
727 check_pmtu_value "1500" "${pmtu_2}" "changing local MTU on a link not on this path" || return 1
728
729 # Skip PMTU locking tests for IPv6
730 [ $family -eq 6 ] && return 0
731
732 # Decrease remote MTU on path via R2, get new exception
733 mtu "${ns_r2}" veth_R2-B 400
734 mtu "${ns_b}" veth_B-R2 400
735 run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1400 ${dst2}
736 pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
737 check_pmtu_value "lock 552" "${pmtu_2}" "exceeding MTU, with MTU < min_pmtu" || return 1
738
739 # Decrease local MTU below PMTU
740 mtu "${ns_a}" veth_A-R2 500
741 mtu "${ns_r2}" veth_R2-A 500
742 pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
743 check_pmtu_value "500" "${pmtu_2}" "decreasing local MTU" || return 1
744
745 # Increase local MTU
746 mtu "${ns_a}" veth_A-R2 1500
747 mtu "${ns_r2}" veth_R2-A 1500
748 pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
749 check_pmtu_value "1500" "${pmtu_2}" "increasing local MTU" || return 1
750
751 # Get new exception
752 run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1400 ${dst2}
753 pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
754 check_pmtu_value "lock 552" "${pmtu_2}" "exceeding MTU, with MTU < min_pmtu" || return 1
755}
756
757test_pmtu_ipv4_exception() {
758 test_pmtu_ipvX 4
759}
760
761test_pmtu_ipv6_exception() {
762 test_pmtu_ipvX 6
763}
764
765test_pmtu_ipvX_over_vxlanY_or_geneveY_exception() {
766 type=${1}
767 family=${2}
768 outer_family=${3}
769 ll_mtu=4000
770
771 if [ ${outer_family} -eq 4 ]; then
772 setup namespaces routing ${type}4 || return 2
773 # IPv4 header UDP header VXLAN/GENEVE header Ethernet header
774 exp_mtu=$((${ll_mtu} - 20 - 8 - 8 - 14))
775 else
776 setup namespaces routing ${type}6 || return 2
777 # IPv6 header UDP header VXLAN/GENEVE header Ethernet header
778 exp_mtu=$((${ll_mtu} - 40 - 8 - 8 - 14))
779 fi
780
781 trace "${ns_a}" ${type}_a "${ns_b}" ${type}_b \
782 "${ns_a}" veth_A-R1 "${ns_r1}" veth_R1-A \
783 "${ns_b}" veth_B-R1 "${ns_r1}" veth_R1-B
784
785 if [ ${family} -eq 4 ]; then
786 ping=ping
787 dst=${tunnel4_b_addr}
788 else
789 ping=${ping6}
790 dst=${tunnel6_b_addr}
791 fi
792
793 # Create route exception by exceeding link layer MTU
794 mtu "${ns_a}" veth_A-R1 $((${ll_mtu} + 1000))
795 mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
796 mtu "${ns_b}" veth_B-R1 ${ll_mtu}
797 mtu "${ns_r1}" veth_R1-B ${ll_mtu}
798
799 mtu "${ns_a}" ${type}_a $((${ll_mtu} + 1000))
800 mtu "${ns_b}" ${type}_b $((${ll_mtu} + 1000))
801 run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
802
803 # Check that exception was created
804 pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
805 check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ${type} interface"
806}
807
808test_pmtu_ipv4_vxlan4_exception() {
809 test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan 4 4
810}
811
812test_pmtu_ipv6_vxlan4_exception() {
813 test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan 6 4
814}
815
816test_pmtu_ipv4_geneve4_exception() {
817 test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 4 4
818}
819
820test_pmtu_ipv6_geneve4_exception() {
821 test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 6 4
822}
823
824test_pmtu_ipv4_vxlan6_exception() {
825 test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan 4 6
826}
827
828test_pmtu_ipv6_vxlan6_exception() {
829 test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan 6 6
830}
831
832test_pmtu_ipv4_geneve6_exception() {
833 test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 4 6
834}
835
836test_pmtu_ipv6_geneve6_exception() {
837 test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 6 6
838}
839
840test_pmtu_ipvX_over_fouY_or_gueY() {
841 inner_family=${1}
842 outer_family=${2}
843 encap=${3}
844 ll_mtu=4000
845
846 setup namespaces routing ${encap}${outer_family}${inner_family} || return 2
847 trace "${ns_a}" ${encap}_a "${ns_b}" ${encap}_b \
848 "${ns_a}" veth_A-R1 "${ns_r1}" veth_R1-A \
849 "${ns_b}" veth_B-R1 "${ns_r1}" veth_R1-B
850
851 if [ ${inner_family} -eq 4 ]; then
852 ping=ping
853 dst=${tunnel4_b_addr}
854 else
855 ping=${ping6}
856 dst=${tunnel6_b_addr}
857 fi
858
859 if [ "${encap}" = "gue" ]; then
860 encap_overhead=4
861 else
862 encap_overhead=0
863 fi
864
865 if [ ${outer_family} -eq 4 ]; then
866 # IPv4 header UDP header
867 exp_mtu=$((${ll_mtu} - 20 - 8 - ${encap_overhead}))
868 else
869 # IPv6 header Option 4 UDP header
870 exp_mtu=$((${ll_mtu} - 40 - 8 - 8 - ${encap_overhead}))
871 fi
872
873 # Create route exception by exceeding link layer MTU
874 mtu "${ns_a}" veth_A-R1 $((${ll_mtu} + 1000))
875 mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
876 mtu "${ns_b}" veth_B-R1 ${ll_mtu}
877 mtu "${ns_r1}" veth_R1-B ${ll_mtu}
878
879 mtu "${ns_a}" ${encap}_a $((${ll_mtu} + 1000))
880 mtu "${ns_b}" ${encap}_b $((${ll_mtu} + 1000))
881 run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
882
883 # Check that exception was created
884 pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
885 check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ${encap} interface"
886}
887
888test_pmtu_ipv4_fou4_exception() {
889 test_pmtu_ipvX_over_fouY_or_gueY 4 4 fou
890}
891
892test_pmtu_ipv6_fou4_exception() {
893 test_pmtu_ipvX_over_fouY_or_gueY 6 4 fou
894}
895
896test_pmtu_ipv4_fou6_exception() {
897 test_pmtu_ipvX_over_fouY_or_gueY 4 6 fou
898}
899
900test_pmtu_ipv6_fou6_exception() {
901 test_pmtu_ipvX_over_fouY_or_gueY 6 6 fou
902}
903
904test_pmtu_ipv4_gue4_exception() {
905 test_pmtu_ipvX_over_fouY_or_gueY 4 4 gue
906}
907
908test_pmtu_ipv6_gue4_exception() {
909 test_pmtu_ipvX_over_fouY_or_gueY 6 4 gue
910}
911
912test_pmtu_ipv4_gue6_exception() {
913 test_pmtu_ipvX_over_fouY_or_gueY 4 6 gue
914}
915
916test_pmtu_ipv6_gue6_exception() {
917 test_pmtu_ipvX_over_fouY_or_gueY 6 6 gue
918}
919
920test_pmtu_vti4_exception() {
921 setup namespaces veth vti4 xfrm4 || return 2
922 trace "${ns_a}" veth_a "${ns_b}" veth_b \
923 "${ns_a}" vti4_a "${ns_b}" vti4_b
924
925 veth_mtu=1500
926 vti_mtu=$((veth_mtu - 20))
927
928 # SPI SN IV ICV pad length next header
929 esp_payload_rfc4106=$((vti_mtu - 4 - 4 - 8 - 16 - 1 - 1))
930 ping_payload=$((esp_payload_rfc4106 - 28))
931
932 mtu "${ns_a}" veth_a ${veth_mtu}
933 mtu "${ns_b}" veth_b ${veth_mtu}
934 mtu "${ns_a}" vti4_a ${vti_mtu}
935 mtu "${ns_b}" vti4_b ${vti_mtu}
936
937 # Send DF packet without exceeding link layer MTU, check that no
938 # exception is created
939 run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s ${ping_payload} ${tunnel4_b_addr}
940 pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
941 check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
942
943 # Now exceed link layer MTU by one byte, check that exception is created
944 # with the right PMTU value
945 run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((ping_payload + 1)) ${tunnel4_b_addr}
946 pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
947 check_pmtu_value "${esp_payload_rfc4106}" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106 + 1)))"
948}
949
950test_pmtu_vti6_exception() {
951 setup namespaces veth vti6 xfrm6 || return 2
952 trace "${ns_a}" veth_a "${ns_b}" veth_b \
953 "${ns_a}" vti6_a "${ns_b}" vti6_b
954 fail=0
955
956 # Create route exception by exceeding link layer MTU
957 mtu "${ns_a}" veth_a 4000
958 mtu "${ns_b}" veth_b 4000
959 mtu "${ns_a}" vti6_a 5000
960 mtu "${ns_b}" vti6_b 5000
961 run_cmd ${ns_a} ${ping6} -q -i 0.1 -w 1 -s 60000 ${tunnel6_b_addr}
962
963 # Check that exception was created
964 pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
965 check_pmtu_value any "${pmtu}" "creating tunnel exceeding link layer MTU" || return 1
966
967 # Decrease tunnel MTU, check for PMTU decrease in route exception
968 mtu "${ns_a}" vti6_a 3000
969 pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
970 check_pmtu_value "3000" "${pmtu}" "decreasing tunnel MTU" || fail=1
971
972 # Increase tunnel MTU, check for PMTU increase in route exception
973 mtu "${ns_a}" vti6_a 9000
974 pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
975 check_pmtu_value "9000" "${pmtu}" "increasing tunnel MTU" || fail=1
976
977 return ${fail}
978}
979
980test_pmtu_vti4_default_mtu() {
981 setup namespaces veth vti4 || return 2
982
983 # Check that MTU of vti device is MTU of veth minus IPv4 header length
984 veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
985 vti4_mtu="$(link_get_mtu "${ns_a}" vti4_a)"
986 if [ $((veth_mtu - vti4_mtu)) -ne 20 ]; then
987 err " vti MTU ${vti4_mtu} is not veth MTU ${veth_mtu} minus IPv4 header length"
988 return 1
989 fi
990}
991
992test_pmtu_vti6_default_mtu() {
993 setup namespaces veth vti6 || return 2
994
995 # Check that MTU of vti device is MTU of veth minus IPv6 header length
996 veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
997 vti6_mtu="$(link_get_mtu "${ns_a}" vti6_a)"
998 if [ $((veth_mtu - vti6_mtu)) -ne 40 ]; then
999 err " vti MTU ${vti6_mtu} is not veth MTU ${veth_mtu} minus IPv6 header length"
1000 return 1
1001 fi
1002}
1003
1004test_pmtu_vti4_link_add_mtu() {
1005 setup namespaces || return 2
1006
1007 run_cmd ${ns_a} ip link add vti4_a type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1008 [ $? -ne 0 ] && err " vti not supported" && return 2
1009 run_cmd ${ns_a} ip link del vti4_a
1010
1011 fail=0
1012
1013 min=68
1014 max=$((65535 - 20))
1015 # Check invalid values first
1016 for v in $((min - 1)) $((max + 1)); do
1017 run_cmd ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1018 # This can fail, or MTU can be adjusted to a proper value
1019 [ $? -ne 0 ] && continue
1020 mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1021 if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
1022 err " vti tunnel created with invalid MTU ${mtu}"
1023 fail=1
1024 fi
1025 run_cmd ${ns_a} ip link del vti4_a
1026 done
1027
1028 # Now check valid values
1029 for v in ${min} 1300 ${max}; do
1030 run_cmd ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1031 mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1032 run_cmd ${ns_a} ip link del vti4_a
1033 if [ "${mtu}" != "${v}" ]; then
1034 err " vti MTU ${mtu} doesn't match configured value ${v}"
1035 fail=1
1036 fi
1037 done
1038
1039 return ${fail}
1040}
1041
1042test_pmtu_vti6_link_add_mtu() {
1043 setup namespaces || return 2
1044
1045 run_cmd ${ns_a} ip link add vti6_a type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1046 [ $? -ne 0 ] && err " vti6 not supported" && return 2
1047 run_cmd ${ns_a} ip link del vti6_a
1048
1049 fail=0
1050
1051 min=68 # vti6 can carry IPv4 packets too
1052 max=$((65535 - 40))
1053 # Check invalid values first
1054 for v in $((min - 1)) $((max + 1)); do
1055 run_cmd ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1056 # This can fail, or MTU can be adjusted to a proper value
1057 [ $? -ne 0 ] && continue
1058 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1059 if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
1060 err " vti6 tunnel created with invalid MTU ${v}"
1061 fail=1
1062 fi
1063 run_cmd ${ns_a} ip link del vti6_a
1064 done
1065
1066 # Now check valid values
1067 for v in 68 1280 1300 $((65535 - 40)); do
1068 run_cmd ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1069 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1070 run_cmd ${ns_a} ip link del vti6_a
1071 if [ "${mtu}" != "${v}" ]; then
1072 err " vti6 MTU ${mtu} doesn't match configured value ${v}"
1073 fail=1
1074 fi
1075 done
1076
1077 return ${fail}
1078}
1079
1080test_pmtu_vti6_link_change_mtu() {
1081 setup namespaces || return 2
1082
1083 run_cmd ${ns_a} ip link add dummy0 mtu 1500 type dummy
1084 [ $? -ne 0 ] && err " dummy not supported" && return 2
1085 run_cmd ${ns_a} ip link add dummy1 mtu 3000 type dummy
1086 run_cmd ${ns_a} ip link set dummy0 up
1087 run_cmd ${ns_a} ip link set dummy1 up
1088
1089 run_cmd ${ns_a} ip addr add ${dummy6_0_prefix}1/${dummy6_mask} dev dummy0
1090 run_cmd ${ns_a} ip addr add ${dummy6_1_prefix}1/${dummy6_mask} dev dummy1
1091
1092 fail=0
1093
1094 # Create vti6 interface bound to device, passing MTU, check it
1095 run_cmd ${ns_a} ip link add vti6_a mtu 1300 type vti6 remote ${dummy6_0_prefix}2 local ${dummy6_0_prefix}1
1096 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1097 if [ ${mtu} -ne 1300 ]; then
1098 err " vti6 MTU ${mtu} doesn't match configured value 1300"
1099 fail=1
1100 fi
1101
1102 # Move to another device with different MTU, without passing MTU, check
1103 # MTU is adjusted
1104 run_cmd ${ns_a} ip link set vti6_a type vti6 remote ${dummy6_1_prefix}2 local ${dummy6_1_prefix}1
1105 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1106 if [ ${mtu} -ne $((3000 - 40)) ]; then
1107 err " vti MTU ${mtu} is not dummy MTU 3000 minus IPv6 header length"
1108 fail=1
1109 fi
1110
1111 # Move it back, passing MTU, check MTU is not overridden
1112 run_cmd ${ns_a} ip link set vti6_a mtu 1280 type vti6 remote ${dummy6_0_prefix}2 local ${dummy6_0_prefix}1
1113 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1114 if [ ${mtu} -ne 1280 ]; then
1115 err " vti6 MTU ${mtu} doesn't match configured value 1280"
1116 fail=1
1117 fi
1118
1119 return ${fail}
1120}
1121
1122check_command() {
1123 cmd=${1}
1124
1125 if ! which ${cmd} > /dev/null 2>&1; then
1126 err " missing required command: '${cmd}'"
1127 return 1
1128 fi
1129 return 0
1130}
1131
1132check_running() {
1133 pid=${1}
1134 cmd=${2}
1135
1136 [ "$(cat /proc/${pid}/cmdline 2>/dev/null | tr -d '\0')" = "${cmd}" ]
1137}
1138
1139test_cleanup_vxlanX_exception() {
1140 outer="${1}"
1141 encap="vxlan"
1142 ll_mtu=4000
1143
1144 check_command taskset || return 2
1145 cpu_list=$(grep -m 2 processor /proc/cpuinfo | cut -d ' ' -f 2)
1146
1147 setup namespaces routing ${encap}${outer} || return 2
1148 trace "${ns_a}" ${encap}_a "${ns_b}" ${encap}_b \
1149 "${ns_a}" veth_A-R1 "${ns_r1}" veth_R1-A \
1150 "${ns_b}" veth_B-R1 "${ns_r1}" veth_R1-B
1151
1152 # Create route exception by exceeding link layer MTU
1153 mtu "${ns_a}" veth_A-R1 $((${ll_mtu} + 1000))
1154 mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1155 mtu "${ns_b}" veth_B-R1 ${ll_mtu}
1156 mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1157
1158 mtu "${ns_a}" ${encap}_a $((${ll_mtu} + 1000))
1159 mtu "${ns_b}" ${encap}_b $((${ll_mtu} + 1000))
1160
1161 # Fill exception cache for multiple CPUs (2)
1162 # we can always use inner IPv4 for that
1163 for cpu in ${cpu_list}; do
1164 run_cmd taskset --cpu-list ${cpu} ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${tunnel4_b_addr}
1165 done
1166
1167 ${ns_a} ip link del dev veth_A-R1 &
1168 iplink_pid=$!
1169 for i in $(seq 1 20); do
1170 check_running ${iplink_pid} "iplinkdeldevveth_A-R1" || return 0
1171 sleep 0.1
1172 done
1173 err " can't delete veth device in a timely manner, PMTU dst likely leaked"
1174 return 1
1175}
1176
1177test_cleanup_ipv6_exception() {
1178 test_cleanup_vxlanX_exception 6
1179}
1180
1181test_cleanup_ipv4_exception() {
1182 test_cleanup_vxlanX_exception 4
1183}
1184
1185run_test() {
1186 (
1187 tname="$1"
1188 tdesc="$2"
1189
1190 unset IFS
1191
1192 # Since cleanup() relies on variables modified by this subshell, it
1193 # has to run in this context.
1194 trap cleanup EXIT
1195
1196 if [ "$VERBOSE" = "1" ]; then
1197 printf "\n##########################################################################\n\n"
1198 fi
1199
1200 eval test_${tname}
1201 ret=$?
1202
1203 if [ $ret -eq 0 ]; then
1204 printf "TEST: %-60s [ OK ]\n" "${tdesc}"
1205 elif [ $ret -eq 1 ]; then
1206 printf "TEST: %-60s [FAIL]\n" "${tdesc}"
1207 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
1208 echo
1209 echo "Pausing. Hit enter to continue"
1210 read a
1211 fi
1212 err_flush
1213 exit 1
1214 elif [ $ret -eq 2 ]; then
1215 printf "TEST: %-60s [SKIP]\n" "${tdesc}"
1216 err_flush
1217 fi
1218
1219 return $ret
1220 )
1221 ret=$?
1222 [ $ret -ne 0 ] && exitcode=1
1223
1224 return $ret
1225}
1226
1227run_test_nh() {
1228 tname="$1"
1229 tdesc="$2"
1230
1231 USE_NH=yes
1232 run_test "${tname}" "${tdesc} - nexthop objects"
1233 USE_NH=no
1234}
1235
1236test_list_flush_ipv4_exception() {
1237 setup namespaces routing || return 2
1238 trace "${ns_a}" veth_A-R1 "${ns_r1}" veth_R1-A \
1239 "${ns_r1}" veth_R1-B "${ns_b}" veth_B-R1 \
1240 "${ns_a}" veth_A-R2 "${ns_r2}" veth_R2-A \
1241 "${ns_r2}" veth_R2-B "${ns_b}" veth_B-R2
1242
1243 dst_prefix1="${prefix4}.${b_r1}."
1244 dst2="${prefix4}.${b_r2}.1"
1245
1246 # Set up initial MTU values
1247 mtu "${ns_a}" veth_A-R1 2000
1248 mtu "${ns_r1}" veth_R1-A 2000
1249 mtu "${ns_r1}" veth_R1-B 1500
1250 mtu "${ns_b}" veth_B-R1 1500
1251
1252 mtu "${ns_a}" veth_A-R2 2000
1253 mtu "${ns_r2}" veth_R2-A 2000
1254 mtu "${ns_r2}" veth_R2-B 1500
1255 mtu "${ns_b}" veth_B-R2 1500
1256
1257 fail=0
1258
1259 # Add 100 addresses for veth endpoint on B reached by default A route
1260 for i in $(seq 100 199); do
1261 run_cmd ${ns_b} ip addr add "${dst_prefix1}${i}" dev veth_B-R1
1262 done
1263
1264 # Create 100 cached route exceptions for path via R1, one via R2. Note
1265 # that with IPv4 we need to actually cause a route lookup that matches
1266 # the exception caused by ICMP, in order to actually have a cached
1267 # route, so we need to ping each destination twice
1268 for i in $(seq 100 199); do
1269 run_cmd ${ns_a} ping -q -M want -i 0.1 -c 2 -s 1800 "${dst_prefix1}${i}"
1270 done
1271 run_cmd ${ns_a} ping -q -M want -i 0.1 -c 2 -s 1800 "${dst2}"
1272
1273 if [ "$(${ns_a} ip -oneline route list cache | wc -l)" -ne 101 ]; then
1274 err " can't list cached exceptions"
1275 fail=1
1276 fi
1277
1278 run_cmd ${ns_a} ip route flush cache
1279 pmtu1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst_prefix}1)"
1280 pmtu2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst_prefix}2)"
1281 if [ -n "${pmtu1}" ] || [ -n "${pmtu2}" ] || \
1282 [ -n "$(${ns_a} ip route list cache)" ]; then
1283 err " can't flush cached exceptions"
1284 fail=1
1285 fi
1286
1287 return ${fail}
1288}
1289
1290test_list_flush_ipv6_exception() {
1291 setup namespaces routing || return 2
1292 trace "${ns_a}" veth_A-R1 "${ns_r1}" veth_R1-A \
1293 "${ns_r1}" veth_R1-B "${ns_b}" veth_B-R1 \
1294 "${ns_a}" veth_A-R2 "${ns_r2}" veth_R2-A \
1295 "${ns_r2}" veth_R2-B "${ns_b}" veth_B-R2
1296
1297 dst_prefix1="${prefix6}:${b_r1}::"
1298 dst2="${prefix6}:${b_r2}::1"
1299
1300 # Set up initial MTU values
1301 mtu "${ns_a}" veth_A-R1 2000
1302 mtu "${ns_r1}" veth_R1-A 2000
1303 mtu "${ns_r1}" veth_R1-B 1500
1304 mtu "${ns_b}" veth_B-R1 1500
1305
1306 mtu "${ns_a}" veth_A-R2 2000
1307 mtu "${ns_r2}" veth_R2-A 2000
1308 mtu "${ns_r2}" veth_R2-B 1500
1309 mtu "${ns_b}" veth_B-R2 1500
1310
1311 fail=0
1312
1313 # Add 100 addresses for veth endpoint on B reached by default A route
1314 for i in $(seq 100 199); do
1315 run_cmd ${ns_b} ip addr add "${dst_prefix1}${i}" dev veth_B-R1
1316 done
1317
1318 # Create 100 cached route exceptions for path via R1, one via R2
1319 for i in $(seq 100 199); do
1320 run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s 1800 "${dst_prefix1}${i}"
1321 done
1322 run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s 1800 "${dst2}"
1323 if [ "$(${ns_a} ip -oneline -6 route list cache | wc -l)" -ne 101 ]; then
1324 err " can't list cached exceptions"
1325 fail=1
1326 fi
1327
1328 run_cmd ${ns_a} ip -6 route flush cache
1329 pmtu1="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst_prefix1}100")"
1330 pmtu2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1331 if [ -n "${pmtu1}" ] || [ -n "${pmtu2}" ] || \
1332 [ -n "$(${ns_a} ip -6 route list cache)" ]; then
1333 err " can't flush cached exceptions"
1334 fail=1
1335 fi
1336
1337 return ${fail}
1338}
1339
1340test_pmtu_ipvX_route_change() {
1341 family=${1}
1342
1343 setup namespaces routing || return 2
1344 trace "${ns_a}" veth_A-R1 "${ns_r1}" veth_R1-A \
1345 "${ns_r1}" veth_R1-B "${ns_b}" veth_B-R1 \
1346 "${ns_a}" veth_A-R2 "${ns_r2}" veth_R2-A \
1347 "${ns_r2}" veth_R2-B "${ns_b}" veth_B-R2
1348
1349 if [ ${family} -eq 4 ]; then
1350 ping=ping
1351 dst1="${prefix4}.${b_r1}.1"
1352 dst2="${prefix4}.${b_r2}.1"
1353 gw="${prefix4}.${a_r1}.2"
1354 else
1355 ping=${ping6}
1356 dst1="${prefix6}:${b_r1}::1"
1357 dst2="${prefix6}:${b_r2}::1"
1358 gw="${prefix6}:${a_r1}::2"
1359 fi
1360
1361 # Set up initial MTU values
1362 mtu "${ns_a}" veth_A-R1 2000
1363 mtu "${ns_r1}" veth_R1-A 2000
1364 mtu "${ns_r1}" veth_R1-B 1400
1365 mtu "${ns_b}" veth_B-R1 1400
1366
1367 mtu "${ns_a}" veth_A-R2 2000
1368 mtu "${ns_r2}" veth_R2-A 2000
1369 mtu "${ns_r2}" veth_R2-B 1500
1370 mtu "${ns_b}" veth_B-R2 1500
1371
1372 # Create route exceptions
1373 run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst1}
1374 run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst2}
1375
1376 # Check that exceptions have been created with the correct PMTU
1377 pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
1378 check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
1379 pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1380 check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
1381
1382 # Replace the route from A to R1
1383 run_cmd ${ns_a} ip route change default via ${gw}
1384
1385 # Delete the device in A
1386 run_cmd ${ns_a} ip link del "veth_A-R1"
1387}
1388
1389test_pmtu_ipv4_route_change() {
1390 test_pmtu_ipvX_route_change 4
1391}
1392
1393test_pmtu_ipv6_route_change() {
1394 test_pmtu_ipvX_route_change 6
1395}
1396
1397usage() {
1398 echo
1399 echo "$0 [OPTIONS] [TEST]..."
1400 echo "If no TEST argument is given, all tests will be run."
1401 echo
1402 echo "Options"
1403 echo " --trace: capture traffic to TEST_INTERFACE.pcap"
1404 echo
1405 echo "Available tests${tests}"
1406 exit 1
1407}
1408
1409################################################################################
1410#
1411exitcode=0
1412desc=0
1413
1414while getopts :ptv o
1415do
1416 case $o in
1417 p) PAUSE_ON_FAIL=yes;;
1418 v) VERBOSE=1;;
1419 t) if which tcpdump > /dev/null 2>&1; then
1420 TRACING=1
1421 else
1422 echo "=== tcpdump not available, tracing disabled"
1423 fi
1424 ;;
1425 *) usage;;
1426 esac
1427done
1428shift $(($OPTIND-1))
1429
1430IFS="
1431"
1432
1433for arg do
1434 # Check first that all requested tests are available before running any
1435 command -v > /dev/null "test_${arg}" || { echo "=== Test ${arg} not found"; usage; }
1436done
1437
1438trap cleanup EXIT
1439
1440# start clean
1441cleanup
1442
1443HAVE_NH=no
1444ip nexthop ls >/dev/null 2>&1
1445[ $? -eq 0 ] && HAVE_NH=yes
1446
1447name=""
1448desc=""
1449rerun_nh=0
1450for t in ${tests}; do
1451 [ "${name}" = "" ] && name="${t}" && continue
1452 [ "${desc}" = "" ] && desc="${t}" && continue
1453
1454 if [ "${HAVE_NH}" = "yes" ]; then
1455 rerun_nh="${t}"
1456 fi
1457
1458 run_this=1
1459 for arg do
1460 [ "${arg}" != "${arg#--*}" ] && continue
1461 [ "${arg}" = "${name}" ] && run_this=1 && break
1462 run_this=0
1463 done
1464 if [ $run_this -eq 1 ]; then
1465 run_test "${name}" "${desc}"
1466 # if test was skipped no need to retry with nexthop objects
1467 [ $? -eq 2 ] && rerun_nh=0
1468
1469 if [ "${rerun_nh}" = "1" ]; then
1470 run_test_nh "${name}" "${desc}"
1471 fi
1472 fi
1473 name=""
1474 desc=""
1475 rerun_nh=0
1476done
1477
1478exit ${exitcode}