power: supply: core: Fix parsing of battery chemistry/technology
[linux-2.6-microblaze.git] / tools / testing / selftests / net / pmtu.sh
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_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}_br_vxlan{4,6}_exception
63 #       Set up three namespaces, A, B, and C, with routing between A and B over
64 #       R1. R2 is unused in these tests. A has a veth connection to C, and is
65 #       connected to B via a VXLAN endpoint, which is directly bridged to C.
66 #       MTU on the B-R1 link is lower than other MTUs.
67 #
68 #       Check that both C and A are able to communicate with B over the VXLAN
69 #       tunnel, and that PMTU exceptions with the correct values are created.
70 #
71 #                         segment a_r1    segment b_r1            b_r1: 4000
72 #                       .--------------R1--------------.    everything
73 #          C---veth     A                               B         else: 5000
74 #               ' bridge                                |
75 #                   '---- - - - - - VXLAN - - - - - - - '
76 #
77 # - pmtu_ipv{4,6}_br_geneve{4,6}_exception
78 #       Same as pmtu_ipv{4,6}_br_vxlan{4,6}_exception, with a GENEVE tunnel
79 #       instead.
80 #
81 # - pmtu_ipv{4,6}_ovs_vxlan{4,6}_exception
82 #       Set up two namespaces, B, and C, with routing between the init namespace
83 #       and B over R1. A and R2 are unused in these tests. The init namespace
84 #       has a veth connection to C, and is connected to B via a VXLAN endpoint,
85 #       which is handled by Open vSwitch and bridged to C. MTU on the B-R1 link
86 #       is lower than other MTUs.
87 #
88 #       Check that C is able to communicate with B over the VXLAN tunnel, and
89 #       that PMTU exceptions with the correct values are created.
90 #
91 #                         segment a_r1    segment b_r1            b_r1: 4000
92 #                       .--------------R1--------------.    everything
93 #          C---veth    init                             B         else: 5000
94 #               '- ovs                                  |
95 #                   '---- - - - - - VXLAN - - - - - - - '
96 #
97 # - pmtu_ipv{4,6}_ovs_geneve{4,6}_exception
98 #       Same as pmtu_ipv{4,6}_ovs_vxlan{4,6}_exception, with a GENEVE tunnel
99 #       instead.
100 #
101 # - pmtu_ipv{4,6}_fou{4,6}_exception
102 #       Same as pmtu_ipv4_vxlan4, but using a direct IPv4/IPv6 encapsulation
103 #       (FoU) over IPv4/IPv6, instead of VXLAN
104 #
105 # - pmtu_ipv{4,6}_fou{4,6}_exception
106 #       Same as pmtu_ipv4_vxlan4, but using a generic UDP IPv4/IPv6
107 #       encapsulation (GUE) over IPv4/IPv6, instead of VXLAN
108 #
109 # - pmtu_ipv{4,6}_ipv{4,6}_exception
110 #       Same as pmtu_ipv4_vxlan4, but using a IPv4/IPv6 tunnel over IPv4/IPv6,
111 #       instead of VXLAN
112 #
113 # - pmtu_vti4_exception
114 #       Set up vti tunnel on top of veth, with xfrm states and policies, in two
115 #       namespaces with matching endpoints. Check that route exception is not
116 #       created if link layer MTU is not exceeded, then exceed it and check that
117 #       exception is created with the expected PMTU. The approach described
118 #       below for IPv6 doesn't apply here, because, on IPv4, administrative MTU
119 #       changes alone won't affect PMTU
120 #
121 # - pmtu_vti6_exception
122 #       Set up vti6 tunnel on top of veth, with xfrm states and policies, in two
123 #       namespaces with matching endpoints. Check that route exception is
124 #       created by exceeding link layer MTU with ping to other endpoint. Then
125 #       decrease and increase MTU of tunnel, checking that route exception PMTU
126 #       changes accordingly
127 #
128 # - pmtu_vti4_default_mtu
129 #       Set up vti4 tunnel on top of veth, in two namespaces with matching
130 #       endpoints. Check that MTU assigned to vti interface is the MTU of the
131 #       lower layer (veth) minus additional lower layer headers (zero, for veth)
132 #       minus IPv4 header length
133 #
134 # - pmtu_vti6_default_mtu
135 #       Same as above, for IPv6
136 #
137 # - pmtu_vti4_link_add_mtu
138 #       Set up vti4 interface passing MTU value at link creation, check MTU is
139 #       configured, and that link is not created with invalid MTU values
140 #
141 # - pmtu_vti6_link_add_mtu
142 #       Same as above, for IPv6
143 #
144 # - pmtu_vti6_link_change_mtu
145 #       Set up two dummy interfaces with different MTUs, create a vti6 tunnel
146 #       and check that configured MTU is used on link creation and changes, and
147 #       that MTU is properly calculated instead when MTU is not configured from
148 #       userspace
149 #
150 # - cleanup_ipv4_exception
151 #       Similar to pmtu_ipv4_vxlan4_exception, but explicitly generate PMTU
152 #       exceptions on multiple CPUs and check that the veth device tear-down
153 #       happens in a timely manner
154 #
155 # - cleanup_ipv6_exception
156 #       Same as above, but use IPv6 transport from A to B
157 #
158 # - list_flush_ipv4_exception
159 #       Using the same topology as in pmtu_ipv4, create exceptions, and check
160 #       they are shown when listing exception caches, gone after flushing them
161 #
162 # - list_flush_ipv6_exception
163 #       Using the same topology as in pmtu_ipv6, create exceptions, and check
164 #       they are shown when listing exception caches, gone after flushing them
165 #
166 # - pmtu_ipv4_route_change
167 #       Use the same topology as in pmtu_ipv4, but issue a route replacement
168 #       command and delete the corresponding device afterward. This tests for
169 #       proper cleanup of the PMTU exceptions by the route replacement path.
170 #       Device unregistration should complete successfully
171 #
172 # - pmtu_ipv6_route_change
173 #       Same as above but with IPv6
174
175 # Kselftest framework requirement - SKIP code is 4.
176 ksft_skip=4
177
178 PAUSE_ON_FAIL=no
179 VERBOSE=0
180 TRACING=0
181
182 # Some systems don't have a ping6 binary anymore
183 which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
184
185 #               Name                          Description                  re-run with nh
186 tests="
187         pmtu_ipv4_exception             ipv4: PMTU exceptions                   1
188         pmtu_ipv6_exception             ipv6: PMTU exceptions                   1
189         pmtu_ipv4_vxlan4_exception      IPv4 over vxlan4: PMTU exceptions       1
190         pmtu_ipv6_vxlan4_exception      IPv6 over vxlan4: PMTU exceptions       1
191         pmtu_ipv4_vxlan6_exception      IPv4 over vxlan6: PMTU exceptions       1
192         pmtu_ipv6_vxlan6_exception      IPv6 over vxlan6: PMTU exceptions       1
193         pmtu_ipv4_geneve4_exception     IPv4 over geneve4: PMTU exceptions      1
194         pmtu_ipv6_geneve4_exception     IPv6 over geneve4: PMTU exceptions      1
195         pmtu_ipv4_geneve6_exception     IPv4 over geneve6: PMTU exceptions      1
196         pmtu_ipv6_geneve6_exception     IPv6 over geneve6: PMTU exceptions      1
197         pmtu_ipv4_br_vxlan4_exception   IPv4, bridged vxlan4: PMTU exceptions   1
198         pmtu_ipv6_br_vxlan4_exception   IPv6, bridged vxlan4: PMTU exceptions   1
199         pmtu_ipv4_br_vxlan6_exception   IPv4, bridged vxlan6: PMTU exceptions   1
200         pmtu_ipv6_br_vxlan6_exception   IPv6, bridged vxlan6: PMTU exceptions   1
201         pmtu_ipv4_br_geneve4_exception  IPv4, bridged geneve4: PMTU exceptions  1
202         pmtu_ipv6_br_geneve4_exception  IPv6, bridged geneve4: PMTU exceptions  1
203         pmtu_ipv4_br_geneve6_exception  IPv4, bridged geneve6: PMTU exceptions  1
204         pmtu_ipv6_br_geneve6_exception  IPv6, bridged geneve6: PMTU exceptions  1
205         pmtu_ipv4_ovs_vxlan4_exception  IPv4, OVS vxlan4: PMTU exceptions       1
206         pmtu_ipv6_ovs_vxlan4_exception  IPv6, OVS vxlan4: PMTU exceptions       1
207         pmtu_ipv4_ovs_vxlan6_exception  IPv4, OVS vxlan6: PMTU exceptions       1
208         pmtu_ipv6_ovs_vxlan6_exception  IPv6, OVS vxlan6: PMTU exceptions       1
209         pmtu_ipv4_ovs_geneve4_exception IPv4, OVS geneve4: PMTU exceptions      1
210         pmtu_ipv6_ovs_geneve4_exception IPv6, OVS geneve4: PMTU exceptions      1
211         pmtu_ipv4_ovs_geneve6_exception IPv4, OVS geneve6: PMTU exceptions      1
212         pmtu_ipv6_ovs_geneve6_exception IPv6, OVS geneve6: PMTU exceptions      1
213         pmtu_ipv4_fou4_exception        IPv4 over fou4: PMTU exceptions         1
214         pmtu_ipv6_fou4_exception        IPv6 over fou4: PMTU exceptions         1
215         pmtu_ipv4_fou6_exception        IPv4 over fou6: PMTU exceptions         1
216         pmtu_ipv6_fou6_exception        IPv6 over fou6: PMTU exceptions         1
217         pmtu_ipv4_gue4_exception        IPv4 over gue4: PMTU exceptions         1
218         pmtu_ipv6_gue4_exception        IPv6 over gue4: PMTU exceptions         1
219         pmtu_ipv4_gue6_exception        IPv4 over gue6: PMTU exceptions         1
220         pmtu_ipv6_gue6_exception        IPv6 over gue6: PMTU exceptions         1
221         pmtu_ipv4_ipv4_exception        IPv4 over IPv4: PMTU exceptions         1
222         pmtu_ipv6_ipv4_exception        IPv6 over IPv4: PMTU exceptions         1
223         pmtu_ipv4_ipv6_exception        IPv4 over IPv6: PMTU exceptions         1
224         pmtu_ipv6_ipv6_exception        IPv6 over IPv6: PMTU exceptions         1
225         pmtu_vti6_exception             vti6: PMTU exceptions                   0
226         pmtu_vti4_exception             vti4: PMTU exceptions                   0
227         pmtu_vti4_default_mtu           vti4: default MTU assignment            0
228         pmtu_vti6_default_mtu           vti6: default MTU assignment            0
229         pmtu_vti4_link_add_mtu          vti4: MTU setting on link creation      0
230         pmtu_vti6_link_add_mtu          vti6: MTU setting on link creation      0
231         pmtu_vti6_link_change_mtu       vti6: MTU changes on link changes       0
232         cleanup_ipv4_exception          ipv4: cleanup of cached exceptions      1
233         cleanup_ipv6_exception          ipv6: cleanup of cached exceptions      1
234         list_flush_ipv4_exception       ipv4: list and flush cached exceptions  1
235         list_flush_ipv6_exception       ipv6: list and flush cached exceptions  1
236         pmtu_ipv4_route_change          ipv4: PMTU exception w/route replace    1
237         pmtu_ipv6_route_change          ipv6: PMTU exception w/route replace    1"
238
239 NS_A="ns-A"
240 NS_B="ns-B"
241 NS_C="ns-C"
242 NS_R1="ns-R1"
243 NS_R2="ns-R2"
244 ns_a="ip netns exec ${NS_A}"
245 ns_b="ip netns exec ${NS_B}"
246 ns_c="ip netns exec ${NS_C}"
247 ns_r1="ip netns exec ${NS_R1}"
248 ns_r2="ip netns exec ${NS_R2}"
249
250 # Addressing and routing for tests with routers: four network segments, with
251 # index SEGMENT between 1 and 4, a common prefix (PREFIX4 or PREFIX6) and an
252 # identifier ID, which is 1 for hosts (A and B), 2 for routers (R1 and R2).
253 # Addresses are:
254 # - IPv4: PREFIX4.SEGMENT.ID (/24)
255 # - IPv6: PREFIX6:SEGMENT::ID (/64)
256 prefix4="10.0"
257 prefix6="fc00"
258 a_r1=1
259 a_r2=2
260 b_r1=3
261 b_r2=4
262 #       ns      peer    segment
263 routing_addrs="
264         A       R1      ${a_r1}
265         A       R2      ${a_r2}
266         B       R1      ${b_r1}
267         B       R2      ${b_r2}
268 "
269 # Traffic from A to B goes through R1 by default, and through R2, if destined to
270 # B's address on the b_r2 segment.
271 # Traffic from B to A goes through R1.
272 #       ns      destination             gateway
273 routes="
274         A       default                 ${prefix4}.${a_r1}.2
275         A       ${prefix4}.${b_r2}.1    ${prefix4}.${a_r2}.2
276         B       default                 ${prefix4}.${b_r1}.2
277
278         A       default                 ${prefix6}:${a_r1}::2
279         A       ${prefix6}:${b_r2}::1   ${prefix6}:${a_r2}::2
280         B       default                 ${prefix6}:${b_r1}::2
281 "
282
283 USE_NH="no"
284 #       ns      family  nh id      destination          gateway
285 nexthops="
286         A       4       41      ${prefix4}.${a_r1}.2    veth_A-R1
287         A       4       42      ${prefix4}.${a_r2}.2    veth_A-R2
288         B       4       41      ${prefix4}.${b_r1}.2    veth_B-R1
289
290         A       6       61      ${prefix6}:${a_r1}::2   veth_A-R1
291         A       6       62      ${prefix6}:${a_r2}::2   veth_A-R2
292         B       6       61      ${prefix6}:${b_r1}::2   veth_B-R1
293 "
294
295 # nexthop id correlates to id in nexthops config above
296 #       ns    family    prefix                  nh id
297 routes_nh="
298         A       4       default                 41
299         A       4       ${prefix4}.${b_r2}.1    42
300         B       4       default                 41
301
302         A       6       default                 61
303         A       6       ${prefix6}:${b_r2}::1   62
304         B       6       default                 61
305 "
306
307 veth4_a_addr="192.168.1.1"
308 veth4_b_addr="192.168.1.2"
309 veth4_c_addr="192.168.2.10"
310 veth4_mask="24"
311 veth6_a_addr="fd00:1::a"
312 veth6_b_addr="fd00:1::b"
313 veth6_c_addr="fd00:2::c"
314 veth6_mask="64"
315
316 tunnel4_a_addr="192.168.2.1"
317 tunnel4_b_addr="192.168.2.2"
318 tunnel4_mask="24"
319 tunnel6_a_addr="fd00:2::a"
320 tunnel6_b_addr="fd00:2::b"
321 tunnel6_mask="64"
322
323 dummy6_0_prefix="fc00:1000::"
324 dummy6_1_prefix="fc00:1001::"
325 dummy6_mask="64"
326
327 err_buf=
328 tcpdump_pids=
329
330 err() {
331         err_buf="${err_buf}${1}
332 "
333 }
334
335 err_flush() {
336         echo -n "${err_buf}"
337         err_buf=
338 }
339
340 run_cmd() {
341         cmd="$*"
342
343         if [ "$VERBOSE" = "1" ]; then
344                 printf "    COMMAND: $cmd\n"
345         fi
346
347         out="$($cmd 2>&1)"
348         rc=$?
349         if [ "$VERBOSE" = "1" -a -n "$out" ]; then
350                 echo "    $out"
351                 echo
352         fi
353
354         return $rc
355 }
356
357 # Find the auto-generated name for this namespace
358 nsname() {
359         eval echo \$NS_$1
360 }
361
362 setup_fou_or_gue() {
363         outer="${1}"
364         inner="${2}"
365         encap="${3}"
366
367         if [ "${outer}" = "4" ]; then
368                 modprobe fou || return $ksft_skip
369                 a_addr="${prefix4}.${a_r1}.1"
370                 b_addr="${prefix4}.${b_r1}.1"
371                 if [ "${inner}" = "4" ]; then
372                         type="ipip"
373                         ipproto="4"
374                 else
375                         type="sit"
376                         ipproto="41"
377                 fi
378         else
379                 modprobe fou6 || return $ksft_skip
380                 a_addr="${prefix6}:${a_r1}::1"
381                 b_addr="${prefix6}:${b_r1}::1"
382                 if [ "${inner}" = "4" ]; then
383                         type="ip6tnl"
384                         mode="mode ipip6"
385                         ipproto="4 -6"
386                 else
387                         type="ip6tnl"
388                         mode="mode ip6ip6"
389                         ipproto="41 -6"
390                 fi
391         fi
392
393         run_cmd ${ns_a} ip fou add port 5555 ipproto ${ipproto} || return $ksft_skip
394         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
395
396         run_cmd ${ns_b} ip fou add port 5556 ipproto ${ipproto}
397         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
398
399         if [ "${inner}" = "4" ]; then
400                 run_cmd ${ns_a} ip addr add ${tunnel4_a_addr}/${tunnel4_mask} dev ${encap}_a
401                 run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ${encap}_b
402         else
403                 run_cmd ${ns_a} ip addr add ${tunnel6_a_addr}/${tunnel6_mask} dev ${encap}_a
404                 run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ${encap}_b
405         fi
406
407         run_cmd ${ns_a} ip link set ${encap}_a up
408         run_cmd ${ns_b} ip link set ${encap}_b up
409 }
410
411 setup_fou44() {
412         setup_fou_or_gue 4 4 fou
413 }
414
415 setup_fou46() {
416         setup_fou_or_gue 4 6 fou
417 }
418
419 setup_fou64() {
420         setup_fou_or_gue 6 4 fou
421 }
422
423 setup_fou66() {
424         setup_fou_or_gue 6 6 fou
425 }
426
427 setup_gue44() {
428         setup_fou_or_gue 4 4 gue
429 }
430
431 setup_gue46() {
432         setup_fou_or_gue 4 6 gue
433 }
434
435 setup_gue64() {
436         setup_fou_or_gue 6 4 gue
437 }
438
439 setup_gue66() {
440         setup_fou_or_gue 6 6 gue
441 }
442
443 setup_ipvX_over_ipvY() {
444         inner=${1}
445         outer=${2}
446
447         if [ "${outer}" -eq 4 ]; then
448                 a_addr="${prefix4}.${a_r1}.1"
449                 b_addr="${prefix4}.${b_r1}.1"
450                 if [ "${inner}" -eq 4 ]; then
451                         type="ipip"
452                         mode="ipip"
453                 else
454                         type="sit"
455                         mode="ip6ip"
456                 fi
457         else
458                 a_addr="${prefix6}:${a_r1}::1"
459                 b_addr="${prefix6}:${b_r1}::1"
460                 type="ip6tnl"
461                 if [ "${inner}" -eq 4 ]; then
462                         mode="ipip6"
463                 else
464                         mode="ip6ip6"
465                 fi
466         fi
467
468         run_cmd ${ns_a} ip link add ip_a type ${type} local ${a_addr} remote ${b_addr} mode ${mode} || return $ksft_skip
469         run_cmd ${ns_b} ip link add ip_b type ${type} local ${b_addr} remote ${a_addr} mode ${mode}
470
471         run_cmd ${ns_a} ip link set ip_a up
472         run_cmd ${ns_b} ip link set ip_b up
473
474         if [ "${inner}" = "4" ]; then
475                 run_cmd ${ns_a} ip addr add ${tunnel4_a_addr}/${tunnel4_mask} dev ip_a
476                 run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ip_b
477         else
478                 run_cmd ${ns_a} ip addr add ${tunnel6_a_addr}/${tunnel6_mask} dev ip_a
479                 run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ip_b
480         fi
481 }
482
483 setup_ip4ip4() {
484         setup_ipvX_over_ipvY 4 4
485 }
486
487 setup_ip6ip4() {
488         setup_ipvX_over_ipvY 6 4
489 }
490
491 setup_ip4ip6() {
492         setup_ipvX_over_ipvY 4 6
493 }
494
495 setup_ip6ip6() {
496         setup_ipvX_over_ipvY 6 6
497 }
498
499 setup_namespaces() {
500         for n in ${NS_A} ${NS_B} ${NS_C} ${NS_R1} ${NS_R2}; do
501                 ip netns add ${n} || return 1
502
503                 # Disable DAD, so that we don't have to wait to use the
504                 # configured IPv6 addresses
505                 ip netns exec ${n} sysctl -q net/ipv6/conf/default/accept_dad=0
506         done
507 }
508
509 setup_veth() {
510         run_cmd ${ns_a} ip link add veth_a type veth peer name veth_b || return 1
511         run_cmd ${ns_a} ip link set veth_b netns ${NS_B}
512
513         run_cmd ${ns_a} ip addr add ${veth4_a_addr}/${veth4_mask} dev veth_a
514         run_cmd ${ns_b} ip addr add ${veth4_b_addr}/${veth4_mask} dev veth_b
515
516         run_cmd ${ns_a} ip addr add ${veth6_a_addr}/${veth6_mask} dev veth_a
517         run_cmd ${ns_b} ip addr add ${veth6_b_addr}/${veth6_mask} dev veth_b
518
519         run_cmd ${ns_a} ip link set veth_a up
520         run_cmd ${ns_b} ip link set veth_b up
521 }
522
523 setup_vti() {
524         proto=${1}
525         veth_a_addr="${2}"
526         veth_b_addr="${3}"
527         vti_a_addr="${4}"
528         vti_b_addr="${5}"
529         vti_mask=${6}
530
531         [ ${proto} -eq 6 ] && vti_type="vti6" || vti_type="vti"
532
533         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
534         run_cmd ${ns_b} ip link add vti${proto}_b type ${vti_type} local ${veth_b_addr} remote ${veth_a_addr} key 10
535
536         run_cmd ${ns_a} ip addr add ${vti_a_addr}/${vti_mask} dev vti${proto}_a
537         run_cmd ${ns_b} ip addr add ${vti_b_addr}/${vti_mask} dev vti${proto}_b
538
539         run_cmd ${ns_a} ip link set vti${proto}_a up
540         run_cmd ${ns_b} ip link set vti${proto}_b up
541 }
542
543 setup_vti4() {
544         setup_vti 4 ${veth4_a_addr} ${veth4_b_addr} ${tunnel4_a_addr} ${tunnel4_b_addr} ${tunnel4_mask}
545 }
546
547 setup_vti6() {
548         setup_vti 6 ${veth6_a_addr} ${veth6_b_addr} ${tunnel6_a_addr} ${tunnel6_b_addr} ${tunnel6_mask}
549 }
550
551 setup_vxlan_or_geneve() {
552         type="${1}"
553         a_addr="${2}"
554         b_addr="${3}"
555         opts="${4}"
556         br_if_a="${5}"
557
558         if [ "${type}" = "vxlan" ]; then
559                 opts="${opts} ttl 64 dstport 4789"
560                 opts_a="local ${a_addr}"
561                 opts_b="local ${b_addr}"
562         else
563                 opts_a=""
564                 opts_b=""
565         fi
566
567         run_cmd ${ns_a} ip link add ${type}_a type ${type} id 1 ${opts_a} remote ${b_addr} ${opts} || return 1
568         run_cmd ${ns_b} ip link add ${type}_b type ${type} id 1 ${opts_b} remote ${a_addr} ${opts}
569
570         if [ -n "${br_if_a}" ]; then
571                 run_cmd ${ns_a} ip addr add ${tunnel4_a_addr}/${tunnel4_mask} dev ${br_if_a}
572                 run_cmd ${ns_a} ip addr add ${tunnel6_a_addr}/${tunnel6_mask} dev ${br_if_a}
573                 run_cmd ${ns_a} ip link set ${type}_a master ${br_if_a}
574         else
575                 run_cmd ${ns_a} ip addr add ${tunnel4_a_addr}/${tunnel4_mask} dev ${type}_a
576                 run_cmd ${ns_a} ip addr add ${tunnel6_a_addr}/${tunnel6_mask} dev ${type}_a
577         fi
578
579         run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ${type}_b
580         run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ${type}_b
581
582         run_cmd ${ns_a} ip link set ${type}_a up
583         run_cmd ${ns_b} ip link set ${type}_b up
584 }
585
586 setup_geneve4() {
587         setup_vxlan_or_geneve geneve ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1  "df set"
588 }
589
590 setup_vxlan4() {
591         setup_vxlan_or_geneve vxlan  ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1  "df set"
592 }
593
594 setup_geneve6() {
595         setup_vxlan_or_geneve geneve ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 ""
596 }
597
598 setup_vxlan6() {
599         setup_vxlan_or_geneve vxlan  ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 ""
600 }
601
602 setup_bridged_geneve4() {
603         setup_vxlan_or_geneve geneve ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1  "df set" "br0"
604 }
605
606 setup_bridged_vxlan4() {
607         setup_vxlan_or_geneve vxlan  ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1  "df set" "br0"
608 }
609
610 setup_bridged_geneve6() {
611         setup_vxlan_or_geneve geneve ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 "" "br0"
612 }
613
614 setup_bridged_vxlan6() {
615         setup_vxlan_or_geneve vxlan  ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1 "" "br0"
616 }
617
618 setup_xfrm() {
619         proto=${1}
620         veth_a_addr="${2}"
621         veth_b_addr="${3}"
622
623         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
624         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
625         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
626         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
627
628         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
629         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
630         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
631         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
632 }
633
634 setup_xfrm4() {
635         setup_xfrm 4 ${veth4_a_addr} ${veth4_b_addr}
636 }
637
638 setup_xfrm6() {
639         setup_xfrm 6 ${veth6_a_addr} ${veth6_b_addr}
640 }
641
642 setup_routing_old() {
643         for i in ${routes}; do
644                 [ "${ns}" = "" ]        && ns="${i}"            && continue
645                 [ "${addr}" = "" ]      && addr="${i}"          && continue
646                 [ "${gw}" = "" ]        && gw="${i}"
647
648                 ns_name="$(nsname ${ns})"
649
650                 ip -n ${ns_name} route add ${addr} via ${gw}
651
652                 ns=""; addr=""; gw=""
653         done
654 }
655
656 setup_routing_new() {
657         for i in ${nexthops}; do
658                 [ "${ns}" = "" ]        && ns="${i}"            && continue
659                 [ "${fam}" = "" ]       && fam="${i}"           && continue
660                 [ "${nhid}" = "" ]      && nhid="${i}"          && continue
661                 [ "${gw}" = "" ]        && gw="${i}"            && continue
662                 [ "${dev}" = "" ]       && dev="${i}"
663
664                 ns_name="$(nsname ${ns})"
665
666                 ip -n ${ns_name} -${fam} nexthop add id ${nhid} via ${gw} dev ${dev}
667
668                 ns=""; fam=""; nhid=""; gw=""; dev=""
669
670         done
671
672         for i in ${routes_nh}; do
673                 [ "${ns}" = "" ]        && ns="${i}"            && continue
674                 [ "${fam}" = "" ]       && fam="${i}"           && continue
675                 [ "${addr}" = "" ]      && addr="${i}"          && continue
676                 [ "${nhid}" = "" ]      && nhid="${i}"
677
678                 ns_name="$(nsname ${ns})"
679
680                 ip -n ${ns_name} -${fam} route add ${addr} nhid ${nhid}
681
682                 ns=""; fam=""; addr=""; nhid=""
683         done
684 }
685
686 setup_routing() {
687         for i in ${NS_R1} ${NS_R2}; do
688                 ip netns exec ${i} sysctl -q net/ipv4/ip_forward=1
689                 ip netns exec ${i} sysctl -q net/ipv6/conf/all/forwarding=1
690         done
691
692         for i in ${routing_addrs}; do
693                 [ "${ns}" = "" ]        && ns="${i}"            && continue
694                 [ "${peer}" = "" ]      && peer="${i}"          && continue
695                 [ "${segment}" = "" ]   && segment="${i}"
696
697                 ns_name="$(nsname ${ns})"
698                 peer_name="$(nsname ${peer})"
699                 if="veth_${ns}-${peer}"
700                 ifpeer="veth_${peer}-${ns}"
701
702                 # Create veth links
703                 ip link add ${if} up netns ${ns_name} type veth peer name ${ifpeer} netns ${peer_name} || return 1
704                 ip -n ${peer_name} link set dev ${ifpeer} up
705
706                 # Add addresses
707                 ip -n ${ns_name}   addr add ${prefix4}.${segment}.1/24  dev ${if}
708                 ip -n ${ns_name}   addr add ${prefix6}:${segment}::1/64 dev ${if}
709
710                 ip -n ${peer_name} addr add ${prefix4}.${segment}.2/24  dev ${ifpeer}
711                 ip -n ${peer_name} addr add ${prefix6}:${segment}::2/64 dev ${ifpeer}
712
713                 ns=""; peer=""; segment=""
714         done
715
716         if [ "$USE_NH" = "yes" ]; then
717                 setup_routing_new
718         else
719                 setup_routing_old
720         fi
721
722         return 0
723 }
724
725 setup_bridge() {
726         run_cmd ${ns_a} ip link add br0 type bridge || return $ksft_skip
727         run_cmd ${ns_a} ip link set br0 up
728
729         run_cmd ${ns_c} ip link add veth_C-A type veth peer name veth_A-C
730         run_cmd ${ns_c} ip link set veth_A-C netns ns-A
731
732         run_cmd ${ns_a} ip link set veth_A-C up
733         run_cmd ${ns_c} ip link set veth_C-A up
734         run_cmd ${ns_c} ip addr add ${veth4_c_addr}/${veth4_mask} dev veth_C-A
735         run_cmd ${ns_c} ip addr add ${veth6_c_addr}/${veth6_mask} dev veth_C-A
736         run_cmd ${ns_a} ip link set veth_A-C master br0
737 }
738
739 setup_ovs_vxlan_or_geneve() {
740         type="${1}"
741         a_addr="${2}"
742         b_addr="${3}"
743
744         if [ "${type}" = "vxlan" ]; then
745                 opts="${opts} ttl 64 dstport 4789"
746                 opts_b="local ${b_addr}"
747         fi
748
749         run_cmd ovs-vsctl add-port ovs_br0 ${type}_a -- \
750                 set interface ${type}_a type=${type} \
751                 options:remote_ip=${b_addr} options:key=1 options:csum=true || return 1
752
753         run_cmd ${ns_b} ip link add ${type}_b type ${type} id 1 ${opts_b} remote ${a_addr} ${opts} || return 1
754
755         run_cmd ${ns_b} ip addr add ${tunnel4_b_addr}/${tunnel4_mask} dev ${type}_b
756         run_cmd ${ns_b} ip addr add ${tunnel6_b_addr}/${tunnel6_mask} dev ${type}_b
757
758         run_cmd ${ns_b} ip link set ${type}_b up
759 }
760
761 setup_ovs_geneve4() {
762         setup_ovs_vxlan_or_geneve geneve ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1
763 }
764
765 setup_ovs_vxlan4() {
766         setup_ovs_vxlan_or_geneve vxlan  ${prefix4}.${a_r1}.1  ${prefix4}.${b_r1}.1
767 }
768
769 setup_ovs_geneve6() {
770         setup_ovs_vxlan_or_geneve geneve ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1
771 }
772
773 setup_ovs_vxlan6() {
774         setup_ovs_vxlan_or_geneve vxlan  ${prefix6}:${a_r1}::1 ${prefix6}:${b_r1}::1
775 }
776
777 setup_ovs_bridge() {
778         run_cmd ovs-vsctl add-br ovs_br0 || return $ksft_skip
779         run_cmd ip link set ovs_br0 up
780
781         run_cmd ${ns_c} ip link add veth_C-A type veth peer name veth_A-C
782         run_cmd ${ns_c} ip link set veth_A-C netns 1
783
784         run_cmd         ip link set veth_A-C up
785         run_cmd ${ns_c} ip link set veth_C-A up
786         run_cmd ${ns_c} ip addr add ${veth4_c_addr}/${veth4_mask} dev veth_C-A
787         run_cmd ${ns_c} ip addr add ${veth6_c_addr}/${veth6_mask} dev veth_C-A
788         run_cmd ovs-vsctl add-port ovs_br0 veth_A-C
789
790         # Move veth_A-R1 to init
791         run_cmd ${ns_a} ip link set veth_A-R1 netns 1
792         run_cmd ip addr add ${prefix4}.${a_r1}.1/${veth4_mask} dev veth_A-R1
793         run_cmd ip addr add ${prefix6}:${a_r1}::1/${veth6_mask} dev veth_A-R1
794         run_cmd ip link set veth_A-R1 up
795         run_cmd ip route add ${prefix4}.${b_r1}.1 via ${prefix4}.${a_r1}.2
796         run_cmd ip route add ${prefix6}:${b_r1}::1 via ${prefix6}:${a_r1}::2
797 }
798
799 setup() {
800         [ "$(id -u)" -ne 0 ] && echo "  need to run as root" && return $ksft_skip
801
802         cleanup
803         for arg do
804                 eval setup_${arg} || { echo "  ${arg} not supported"; return 1; }
805         done
806 }
807
808 trace() {
809         [ $TRACING -eq 0 ] && return
810
811         for arg do
812                 [ "${ns_cmd}" = "" ] && ns_cmd="${arg}" && continue
813                 ${ns_cmd} tcpdump -s 0 -i "${arg}" -w "${name}_${arg}.pcap" 2> /dev/null &
814                 tcpdump_pids="${tcpdump_pids} $!"
815                 ns_cmd=
816         done
817         sleep 1
818 }
819
820 cleanup() {
821         for pid in ${tcpdump_pids}; do
822                 kill ${pid}
823         done
824         tcpdump_pids=
825
826         for n in ${NS_A} ${NS_B} ${NS_C} ${NS_R1} ${NS_R2}; do
827                 ip netns del ${n} 2> /dev/null
828         done
829
830         ip link del veth_A-C                    2>/dev/null
831         ip link del veth_A-R1                   2>/dev/null
832         ovs-vsctl --if-exists del-port vxlan_a  2>/dev/null
833         ovs-vsctl --if-exists del-br ovs_br0    2>/dev/null
834 }
835
836 mtu() {
837         ns_cmd="${1}"
838         dev="${2}"
839         mtu="${3}"
840
841         ${ns_cmd} ip link set dev ${dev} mtu ${mtu}
842 }
843
844 mtu_parse() {
845         input="${1}"
846
847         next=0
848         for i in ${input}; do
849                 [ ${next} -eq 1 -a "${i}" = "lock" ] && next=2 && continue
850                 [ ${next} -eq 1 ] && echo "${i}" && return
851                 [ ${next} -eq 2 ] && echo "lock ${i}" && return
852                 [ "${i}" = "mtu" ] && next=1
853         done
854 }
855
856 link_get() {
857         ns_cmd="${1}"
858         name="${2}"
859
860         ${ns_cmd} ip link show dev "${name}"
861 }
862
863 link_get_mtu() {
864         ns_cmd="${1}"
865         name="${2}"
866
867         mtu_parse "$(link_get "${ns_cmd}" ${name})"
868 }
869
870 route_get_dst_exception() {
871         ns_cmd="${1}"
872         dst="${2}"
873
874         ${ns_cmd} ip route get "${dst}"
875 }
876
877 route_get_dst_pmtu_from_exception() {
878         ns_cmd="${1}"
879         dst="${2}"
880
881         mtu_parse "$(route_get_dst_exception "${ns_cmd}" ${dst})"
882 }
883
884 check_pmtu_value() {
885         expected="${1}"
886         value="${2}"
887         event="${3}"
888
889         [ "${expected}" = "any" ] && [ -n "${value}" ] && return 0
890         [ "${value}" = "${expected}" ] && return 0
891         [ -z "${value}" ] &&    err "  PMTU exception wasn't created after ${event}" && return 1
892         [ -z "${expected}" ] && err "  PMTU exception shouldn't exist after ${event}" && return 1
893         err "  found PMTU exception with incorrect MTU ${value}, expected ${expected}, after ${event}"
894         return 1
895 }
896
897 test_pmtu_ipvX() {
898         family=${1}
899
900         setup namespaces routing || return $ksft_skip
901         trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
902               "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
903               "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
904               "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
905
906         if [ ${family} -eq 4 ]; then
907                 ping=ping
908                 dst1="${prefix4}.${b_r1}.1"
909                 dst2="${prefix4}.${b_r2}.1"
910         else
911                 ping=${ping6}
912                 dst1="${prefix6}:${b_r1}::1"
913                 dst2="${prefix6}:${b_r2}::1"
914         fi
915
916         # Set up initial MTU values
917         mtu "${ns_a}"  veth_A-R1 2000
918         mtu "${ns_r1}" veth_R1-A 2000
919         mtu "${ns_r1}" veth_R1-B 1400
920         mtu "${ns_b}"  veth_B-R1 1400
921
922         mtu "${ns_a}"  veth_A-R2 2000
923         mtu "${ns_r2}" veth_R2-A 2000
924         mtu "${ns_r2}" veth_R2-B 1500
925         mtu "${ns_b}"  veth_B-R2 1500
926
927         # Create route exceptions
928         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst1}
929         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst2}
930
931         # Check that exceptions have been created with the correct PMTU
932         pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
933         check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
934         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
935         check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
936
937         # Decrease local MTU below PMTU, check for PMTU decrease in route exception
938         mtu "${ns_a}"  veth_A-R1 1300
939         mtu "${ns_r1}" veth_R1-A 1300
940         pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
941         check_pmtu_value "1300" "${pmtu_1}" "decreasing local MTU" || return 1
942         # Second exception shouldn't be modified
943         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
944         check_pmtu_value "1500" "${pmtu_2}" "changing local MTU on a link not on this path" || return 1
945
946         # Increase MTU, check for PMTU increase in route exception
947         mtu "${ns_a}"  veth_A-R1 1700
948         mtu "${ns_r1}" veth_R1-A 1700
949         pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
950         check_pmtu_value "1700" "${pmtu_1}" "increasing local MTU" || return 1
951         # Second exception shouldn't be modified
952         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
953         check_pmtu_value "1500" "${pmtu_2}" "changing local MTU on a link not on this path" || return 1
954
955         # Skip PMTU locking tests for IPv6
956         [ $family -eq 6 ] && return 0
957
958         # Decrease remote MTU on path via R2, get new exception
959         mtu "${ns_r2}" veth_R2-B 400
960         mtu "${ns_b}"  veth_B-R2 400
961         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1400 ${dst2}
962         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
963         check_pmtu_value "lock 552" "${pmtu_2}" "exceeding MTU, with MTU < min_pmtu" || return 1
964
965         # Decrease local MTU below PMTU
966         mtu "${ns_a}"  veth_A-R2 500
967         mtu "${ns_r2}" veth_R2-A 500
968         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
969         check_pmtu_value "500" "${pmtu_2}" "decreasing local MTU" || return 1
970
971         # Increase local MTU
972         mtu "${ns_a}"  veth_A-R2 1500
973         mtu "${ns_r2}" veth_R2-A 1500
974         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
975         check_pmtu_value "1500" "${pmtu_2}" "increasing local MTU" || return 1
976
977         # Get new exception
978         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1400 ${dst2}
979         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
980         check_pmtu_value "lock 552" "${pmtu_2}" "exceeding MTU, with MTU < min_pmtu" || return 1
981 }
982
983 test_pmtu_ipv4_exception() {
984         test_pmtu_ipvX 4
985 }
986
987 test_pmtu_ipv6_exception() {
988         test_pmtu_ipvX 6
989 }
990
991 test_pmtu_ipvX_over_vxlanY_or_geneveY_exception() {
992         type=${1}
993         family=${2}
994         outer_family=${3}
995         ll_mtu=4000
996
997         if [ ${outer_family} -eq 4 ]; then
998                 setup namespaces routing ${type}4 || return $ksft_skip
999                 #                      IPv4 header   UDP header   VXLAN/GENEVE header   Ethernet header
1000                 exp_mtu=$((${ll_mtu} - 20          - 8          - 8                   - 14))
1001         else
1002                 setup namespaces routing ${type}6 || return $ksft_skip
1003                 #                      IPv6 header   UDP header   VXLAN/GENEVE header   Ethernet header
1004                 exp_mtu=$((${ll_mtu} - 40          - 8          - 8                   - 14))
1005         fi
1006
1007         trace "${ns_a}" ${type}_a    "${ns_b}"  ${type}_b \
1008               "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1009               "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1010
1011         if [ ${family} -eq 4 ]; then
1012                 ping=ping
1013                 dst=${tunnel4_b_addr}
1014         else
1015                 ping=${ping6}
1016                 dst=${tunnel6_b_addr}
1017         fi
1018
1019         # Create route exception by exceeding link layer MTU
1020         mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1021         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1022         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1023         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1024
1025         mtu "${ns_a}" ${type}_a $((${ll_mtu} + 1000))
1026         mtu "${ns_b}" ${type}_b $((${ll_mtu} + 1000))
1027         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
1028
1029         # Check that exception was created
1030         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1031         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ${type} interface"
1032 }
1033
1034 test_pmtu_ipv4_vxlan4_exception() {
1035         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  4 4
1036 }
1037
1038 test_pmtu_ipv6_vxlan4_exception() {
1039         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  6 4
1040 }
1041
1042 test_pmtu_ipv4_geneve4_exception() {
1043         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 4 4
1044 }
1045
1046 test_pmtu_ipv6_geneve4_exception() {
1047         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 6 4
1048 }
1049
1050 test_pmtu_ipv4_vxlan6_exception() {
1051         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  4 6
1052 }
1053
1054 test_pmtu_ipv6_vxlan6_exception() {
1055         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception vxlan  6 6
1056 }
1057
1058 test_pmtu_ipv4_geneve6_exception() {
1059         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 4 6
1060 }
1061
1062 test_pmtu_ipv6_geneve6_exception() {
1063         test_pmtu_ipvX_over_vxlanY_or_geneveY_exception geneve 6 6
1064 }
1065
1066 test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception() {
1067         type=${1}
1068         family=${2}
1069         outer_family=${3}
1070         ll_mtu=4000
1071
1072         if [ ${outer_family} -eq 4 ]; then
1073                 setup namespaces routing bridge bridged_${type}4 || return $ksft_skip
1074                 #                      IPv4 header   UDP header   VXLAN/GENEVE header   Ethernet header
1075                 exp_mtu=$((${ll_mtu} - 20          - 8          - 8                   - 14))
1076         else
1077                 setup namespaces routing bridge bridged_${type}6 || return $ksft_skip
1078                 #                      IPv6 header   UDP header   VXLAN/GENEVE header   Ethernet header
1079                 exp_mtu=$((${ll_mtu} - 40          - 8          - 8                   - 14))
1080         fi
1081
1082         trace "${ns_a}" ${type}_a    "${ns_b}"  ${type}_b \
1083               "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1084               "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B \
1085               "${ns_a}" br0          "${ns_a}"  veth-A-C  \
1086               "${ns_c}" veth_C-A
1087
1088         if [ ${family} -eq 4 ]; then
1089                 ping=ping
1090                 dst=${tunnel4_b_addr}
1091         else
1092                 ping=${ping6}
1093                 dst=${tunnel6_b_addr}
1094         fi
1095
1096         # Create route exception by exceeding link layer MTU
1097         mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1098         mtu "${ns_a}"  br0       $((${ll_mtu} + 1000))
1099         mtu "${ns_a}"  veth_A-C  $((${ll_mtu} + 1000))
1100         mtu "${ns_c}"  veth_C-A  $((${ll_mtu} + 1000))
1101         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1102         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1103         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1104
1105         mtu "${ns_a}" ${type}_a $((${ll_mtu} + 1000))
1106         mtu "${ns_b}" ${type}_b $((${ll_mtu} + 1000))
1107
1108         run_cmd ${ns_c} ${ping} -q -M want -i 0.1 -c 10 -s $((${ll_mtu} + 500)) ${dst} || return 1
1109         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1  -s $((${ll_mtu} + 500)) ${dst} || return 1
1110
1111         # Check that exceptions were created
1112         pmtu="$(route_get_dst_pmtu_from_exception "${ns_c}" ${dst})"
1113         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on bridged ${type} interface"
1114         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1115         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on locally bridged ${type} interface"
1116 }
1117
1118 test_pmtu_ipv4_br_vxlan4_exception() {
1119         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  4 4
1120 }
1121
1122 test_pmtu_ipv6_br_vxlan4_exception() {
1123         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  6 4
1124 }
1125
1126 test_pmtu_ipv4_br_geneve4_exception() {
1127         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 4 4
1128 }
1129
1130 test_pmtu_ipv6_br_geneve4_exception() {
1131         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 6 4
1132 }
1133
1134 test_pmtu_ipv4_br_vxlan6_exception() {
1135         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  4 6
1136 }
1137
1138 test_pmtu_ipv6_br_vxlan6_exception() {
1139         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception vxlan  6 6
1140 }
1141
1142 test_pmtu_ipv4_br_geneve6_exception() {
1143         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 4 6
1144 }
1145
1146 test_pmtu_ipv6_br_geneve6_exception() {
1147         test_pmtu_ipvX_over_bridged_vxlanY_or_geneveY_exception geneve 6 6
1148 }
1149
1150 test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception() {
1151         type=${1}
1152         family=${2}
1153         outer_family=${3}
1154         ll_mtu=4000
1155
1156         if [ ${outer_family} -eq 4 ]; then
1157                 setup namespaces routing ovs_bridge ovs_${type}4 || return $ksft_skip
1158                 #                      IPv4 header   UDP header   VXLAN/GENEVE header   Ethernet header
1159                 exp_mtu=$((${ll_mtu} - 20          - 8          - 8                   - 14))
1160         else
1161                 setup namespaces routing ovs_bridge ovs_${type}6 || return $ksft_skip
1162                 #                      IPv6 header   UDP header   VXLAN/GENEVE header   Ethernet header
1163                 exp_mtu=$((${ll_mtu} - 40          - 8          - 8                   - 14))
1164         fi
1165
1166         if [ "${type}" = "vxlan" ]; then
1167                 tun_a="vxlan_sys_4789"
1168         elif [ "${type}" = "geneve" ]; then
1169                 tun_a="genev_sys_6081"
1170         fi
1171
1172         trace ""        "${tun_a}"  "${ns_b}"  ${type}_b \
1173               ""        veth_A-R1   "${ns_r1}" veth_R1-A \
1174               "${ns_b}" veth_B-R1   "${ns_r1}" veth_R1-B \
1175               ""        ovs_br0     ""         veth-A-C  \
1176               "${ns_c}" veth_C-A
1177
1178         if [ ${family} -eq 4 ]; then
1179                 ping=ping
1180                 dst=${tunnel4_b_addr}
1181         else
1182                 ping=${ping6}
1183                 dst=${tunnel6_b_addr}
1184         fi
1185
1186         # Create route exception by exceeding link layer MTU
1187         mtu ""         veth_A-R1 $((${ll_mtu} + 1000))
1188         mtu ""         ovs_br0   $((${ll_mtu} + 1000))
1189         mtu ""         veth_A-C  $((${ll_mtu} + 1000))
1190         mtu "${ns_c}"  veth_C-A  $((${ll_mtu} + 1000))
1191         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1192         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1193         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1194
1195         mtu ""        ${tun_a}  $((${ll_mtu} + 1000))
1196         mtu "${ns_b}" ${type}_b $((${ll_mtu} + 1000))
1197
1198         run_cmd ${ns_c} ${ping} -q -M want -i 0.1 -c 20 -s $((${ll_mtu} + 500)) ${dst} || return 1
1199
1200         # Check that exceptions were created
1201         pmtu="$(route_get_dst_pmtu_from_exception "${ns_c}" ${dst})"
1202         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on Open vSwitch ${type} interface"
1203 }
1204
1205 test_pmtu_ipv4_ovs_vxlan4_exception() {
1206         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  4 4
1207 }
1208
1209 test_pmtu_ipv6_ovs_vxlan4_exception() {
1210         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  6 4
1211 }
1212
1213 test_pmtu_ipv4_ovs_geneve4_exception() {
1214         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 4 4
1215 }
1216
1217 test_pmtu_ipv6_ovs_geneve4_exception() {
1218         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 6 4
1219 }
1220
1221 test_pmtu_ipv4_ovs_vxlan6_exception() {
1222         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  4 6
1223 }
1224
1225 test_pmtu_ipv6_ovs_vxlan6_exception() {
1226         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception vxlan  6 6
1227 }
1228
1229 test_pmtu_ipv4_ovs_geneve6_exception() {
1230         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 4 6
1231 }
1232
1233 test_pmtu_ipv6_ovs_geneve6_exception() {
1234         test_pmtu_ipvX_over_ovs_vxlanY_or_geneveY_exception geneve 6 6
1235 }
1236
1237 test_pmtu_ipvX_over_fouY_or_gueY() {
1238         inner_family=${1}
1239         outer_family=${2}
1240         encap=${3}
1241         ll_mtu=4000
1242
1243         setup namespaces routing ${encap}${outer_family}${inner_family} || return $ksft_skip
1244         trace "${ns_a}" ${encap}_a   "${ns_b}"  ${encap}_b \
1245               "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1246               "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1247
1248         if [ ${inner_family} -eq 4 ]; then
1249                 ping=ping
1250                 dst=${tunnel4_b_addr}
1251         else
1252                 ping=${ping6}
1253                 dst=${tunnel6_b_addr}
1254         fi
1255
1256         if [ "${encap}" = "gue" ]; then
1257                 encap_overhead=4
1258         else
1259                 encap_overhead=0
1260         fi
1261
1262         if [ ${outer_family} -eq 4 ]; then
1263                 #                      IPv4 header   UDP header
1264                 exp_mtu=$((${ll_mtu} - 20          - 8         - ${encap_overhead}))
1265         else
1266                 #                      IPv6 header   Option 4   UDP header
1267                 exp_mtu=$((${ll_mtu} - 40          - 8        - 8       - ${encap_overhead}))
1268         fi
1269
1270         # Create route exception by exceeding link layer MTU
1271         mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1272         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1273         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1274         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1275
1276         mtu "${ns_a}" ${encap}_a $((${ll_mtu} + 1000))
1277         mtu "${ns_b}" ${encap}_b $((${ll_mtu} + 1000))
1278         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
1279
1280         # Check that exception was created
1281         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1282         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ${encap} interface"
1283 }
1284
1285 test_pmtu_ipv4_fou4_exception() {
1286         test_pmtu_ipvX_over_fouY_or_gueY 4 4 fou
1287 }
1288
1289 test_pmtu_ipv6_fou4_exception() {
1290         test_pmtu_ipvX_over_fouY_or_gueY 6 4 fou
1291 }
1292
1293 test_pmtu_ipv4_fou6_exception() {
1294         test_pmtu_ipvX_over_fouY_or_gueY 4 6 fou
1295 }
1296
1297 test_pmtu_ipv6_fou6_exception() {
1298         test_pmtu_ipvX_over_fouY_or_gueY 6 6 fou
1299 }
1300
1301 test_pmtu_ipv4_gue4_exception() {
1302         test_pmtu_ipvX_over_fouY_or_gueY 4 4 gue
1303 }
1304
1305 test_pmtu_ipv6_gue4_exception() {
1306         test_pmtu_ipvX_over_fouY_or_gueY 6 4 gue
1307 }
1308
1309 test_pmtu_ipv4_gue6_exception() {
1310         test_pmtu_ipvX_over_fouY_or_gueY 4 6 gue
1311 }
1312
1313 test_pmtu_ipv6_gue6_exception() {
1314         test_pmtu_ipvX_over_fouY_or_gueY 6 6 gue
1315 }
1316
1317 test_pmtu_ipvX_over_ipvY_exception() {
1318         inner=${1}
1319         outer=${2}
1320         ll_mtu=4000
1321
1322         setup namespaces routing ip${inner}ip${outer} || return $ksft_skip
1323
1324         trace "${ns_a}" ip_a         "${ns_b}"  ip_b  \
1325               "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1326               "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1327
1328         if [ ${inner} -eq 4 ]; then
1329                 ping=ping
1330                 dst=${tunnel4_b_addr}
1331         else
1332                 ping=${ping6}
1333                 dst=${tunnel6_b_addr}
1334         fi
1335
1336         if [ ${outer} -eq 4 ]; then
1337                 #                      IPv4 header
1338                 exp_mtu=$((${ll_mtu} - 20))
1339         else
1340                 #                      IPv6 header   Option 4
1341                 exp_mtu=$((${ll_mtu} - 40          - 8))
1342         fi
1343
1344         # Create route exception by exceeding link layer MTU
1345         mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1346         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1347         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1348         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1349
1350         mtu "${ns_a}" ip_a $((${ll_mtu} + 1000)) || return
1351         mtu "${ns_b}" ip_b $((${ll_mtu} + 1000)) || return
1352         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${dst}
1353
1354         # Check that exception was created
1355         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst})"
1356         check_pmtu_value ${exp_mtu} "${pmtu}" "exceeding link layer MTU on ip${inner}ip${outer} interface"
1357 }
1358
1359 test_pmtu_ipv4_ipv4_exception() {
1360         test_pmtu_ipvX_over_ipvY_exception 4 4
1361 }
1362
1363 test_pmtu_ipv6_ipv4_exception() {
1364         test_pmtu_ipvX_over_ipvY_exception 6 4
1365 }
1366
1367 test_pmtu_ipv4_ipv6_exception() {
1368         test_pmtu_ipvX_over_ipvY_exception 4 6
1369 }
1370
1371 test_pmtu_ipv6_ipv6_exception() {
1372         test_pmtu_ipvX_over_ipvY_exception 6 6
1373 }
1374
1375 test_pmtu_vti4_exception() {
1376         setup namespaces veth vti4 xfrm4 || return $ksft_skip
1377         trace "${ns_a}" veth_a    "${ns_b}" veth_b \
1378               "${ns_a}" vti4_a    "${ns_b}" vti4_b
1379
1380         veth_mtu=1500
1381         vti_mtu=$((veth_mtu - 20))
1382
1383         #                                SPI   SN   IV  ICV   pad length   next header
1384         esp_payload_rfc4106=$((vti_mtu - 4   - 4  - 8 - 16  - 1          - 1))
1385         ping_payload=$((esp_payload_rfc4106 - 28))
1386
1387         mtu "${ns_a}" veth_a ${veth_mtu}
1388         mtu "${ns_b}" veth_b ${veth_mtu}
1389         mtu "${ns_a}" vti4_a ${vti_mtu}
1390         mtu "${ns_b}" vti4_b ${vti_mtu}
1391
1392         # Send DF packet without exceeding link layer MTU, check that no
1393         # exception is created
1394         run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s ${ping_payload} ${tunnel4_b_addr}
1395         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1396         check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
1397
1398         # Now exceed link layer MTU by one byte, check that exception is created
1399         # with the right PMTU value
1400         run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((ping_payload + 1)) ${tunnel4_b_addr}
1401         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel4_b_addr})"
1402         check_pmtu_value "${esp_payload_rfc4106}" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106 + 1)))"
1403 }
1404
1405 test_pmtu_vti6_exception() {
1406         setup namespaces veth vti6 xfrm6 || return $ksft_skip
1407         trace "${ns_a}" veth_a    "${ns_b}" veth_b \
1408               "${ns_a}" vti6_a    "${ns_b}" vti6_b
1409         fail=0
1410
1411         # Create route exception by exceeding link layer MTU
1412         mtu "${ns_a}" veth_a 4000
1413         mtu "${ns_b}" veth_b 4000
1414         mtu "${ns_a}" vti6_a 5000
1415         mtu "${ns_b}" vti6_b 5000
1416         run_cmd ${ns_a} ${ping6} -q -i 0.1 -w 1 -s 60000 ${tunnel6_b_addr}
1417
1418         # Check that exception was created
1419         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1420         check_pmtu_value any "${pmtu}" "creating tunnel exceeding link layer MTU" || return 1
1421
1422         # Decrease tunnel MTU, check for PMTU decrease in route exception
1423         mtu "${ns_a}" vti6_a 3000
1424         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1425         check_pmtu_value "3000" "${pmtu}" "decreasing tunnel MTU" || fail=1
1426
1427         # Increase tunnel MTU, check for PMTU increase in route exception
1428         mtu "${ns_a}" vti6_a 9000
1429         pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${tunnel6_b_addr})"
1430         check_pmtu_value "9000" "${pmtu}" "increasing tunnel MTU" || fail=1
1431
1432         return ${fail}
1433 }
1434
1435 test_pmtu_vti4_default_mtu() {
1436         setup namespaces veth vti4 || return $ksft_skip
1437
1438         # Check that MTU of vti device is MTU of veth minus IPv4 header length
1439         veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
1440         vti4_mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1441         if [ $((veth_mtu - vti4_mtu)) -ne 20 ]; then
1442                 err "  vti MTU ${vti4_mtu} is not veth MTU ${veth_mtu} minus IPv4 header length"
1443                 return 1
1444         fi
1445 }
1446
1447 test_pmtu_vti6_default_mtu() {
1448         setup namespaces veth vti6 || return $ksft_skip
1449
1450         # Check that MTU of vti device is MTU of veth minus IPv6 header length
1451         veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
1452         vti6_mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1453         if [ $((veth_mtu - vti6_mtu)) -ne 40 ]; then
1454                 err "  vti MTU ${vti6_mtu} is not veth MTU ${veth_mtu} minus IPv6 header length"
1455                 return 1
1456         fi
1457 }
1458
1459 test_pmtu_vti4_link_add_mtu() {
1460         setup namespaces || return $ksft_skip
1461
1462         run_cmd ${ns_a} ip link add vti4_a type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1463         [ $? -ne 0 ] && err "  vti not supported" && return $ksft_skip
1464         run_cmd ${ns_a} ip link del vti4_a
1465
1466         fail=0
1467
1468         min=68
1469         max=$((65535 - 20))
1470         # Check invalid values first
1471         for v in $((min - 1)) $((max + 1)); do
1472                 run_cmd ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1473                 # This can fail, or MTU can be adjusted to a proper value
1474                 [ $? -ne 0 ] && continue
1475                 mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1476                 if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
1477                         err "  vti tunnel created with invalid MTU ${mtu}"
1478                         fail=1
1479                 fi
1480                 run_cmd ${ns_a} ip link del vti4_a
1481         done
1482
1483         # Now check valid values
1484         for v in ${min} 1300 ${max}; do
1485                 run_cmd ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
1486                 mtu="$(link_get_mtu "${ns_a}" vti4_a)"
1487                 run_cmd ${ns_a} ip link del vti4_a
1488                 if [ "${mtu}" != "${v}" ]; then
1489                         err "  vti MTU ${mtu} doesn't match configured value ${v}"
1490                         fail=1
1491                 fi
1492         done
1493
1494         return ${fail}
1495 }
1496
1497 test_pmtu_vti6_link_add_mtu() {
1498         setup namespaces || return $ksft_skip
1499
1500         run_cmd ${ns_a} ip link add vti6_a type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1501         [ $? -ne 0 ] && err "  vti6 not supported" && return $ksft_skip
1502         run_cmd ${ns_a} ip link del vti6_a
1503
1504         fail=0
1505
1506         min=68                  # vti6 can carry IPv4 packets too
1507         max=$((65535 - 40))
1508         # Check invalid values first
1509         for v in $((min - 1)) $((max + 1)); do
1510                 run_cmd ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1511                 # This can fail, or MTU can be adjusted to a proper value
1512                 [ $? -ne 0 ] && continue
1513                 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1514                 if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
1515                         err "  vti6 tunnel created with invalid MTU ${v}"
1516                         fail=1
1517                 fi
1518                 run_cmd ${ns_a} ip link del vti6_a
1519         done
1520
1521         # Now check valid values
1522         for v in 68 1280 1300 $((65535 - 40)); do
1523                 run_cmd ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
1524                 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1525                 run_cmd ${ns_a} ip link del vti6_a
1526                 if [ "${mtu}" != "${v}" ]; then
1527                         err "  vti6 MTU ${mtu} doesn't match configured value ${v}"
1528                         fail=1
1529                 fi
1530         done
1531
1532         return ${fail}
1533 }
1534
1535 test_pmtu_vti6_link_change_mtu() {
1536         setup namespaces || return $ksft_skip
1537
1538         run_cmd ${ns_a} ip link add dummy0 mtu 1500 type dummy
1539         [ $? -ne 0 ] && err "  dummy not supported" && return $ksft_skip
1540         run_cmd ${ns_a} ip link add dummy1 mtu 3000 type dummy
1541         run_cmd ${ns_a} ip link set dummy0 up
1542         run_cmd ${ns_a} ip link set dummy1 up
1543
1544         run_cmd ${ns_a} ip addr add ${dummy6_0_prefix}1/${dummy6_mask} dev dummy0
1545         run_cmd ${ns_a} ip addr add ${dummy6_1_prefix}1/${dummy6_mask} dev dummy1
1546
1547         fail=0
1548
1549         # Create vti6 interface bound to device, passing MTU, check it
1550         run_cmd ${ns_a} ip link add vti6_a mtu 1300 type vti6 remote ${dummy6_0_prefix}2 local ${dummy6_0_prefix}1
1551         mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1552         if [ ${mtu} -ne 1300 ]; then
1553                 err "  vti6 MTU ${mtu} doesn't match configured value 1300"
1554                 fail=1
1555         fi
1556
1557         # Move to another device with different MTU, without passing MTU, check
1558         # MTU is adjusted
1559         run_cmd ${ns_a} ip link set vti6_a type vti6 remote ${dummy6_1_prefix}2 local ${dummy6_1_prefix}1
1560         mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1561         if [ ${mtu} -ne $((3000 - 40)) ]; then
1562                 err "  vti MTU ${mtu} is not dummy MTU 3000 minus IPv6 header length"
1563                 fail=1
1564         fi
1565
1566         # Move it back, passing MTU, check MTU is not overridden
1567         run_cmd ${ns_a} ip link set vti6_a mtu 1280 type vti6 remote ${dummy6_0_prefix}2 local ${dummy6_0_prefix}1
1568         mtu="$(link_get_mtu "${ns_a}" vti6_a)"
1569         if [ ${mtu} -ne 1280 ]; then
1570                 err "  vti6 MTU ${mtu} doesn't match configured value 1280"
1571                 fail=1
1572         fi
1573
1574         return ${fail}
1575 }
1576
1577 check_command() {
1578         cmd=${1}
1579
1580         if ! which ${cmd} > /dev/null 2>&1; then
1581                 err "  missing required command: '${cmd}'"
1582                 return 1
1583         fi
1584         return 0
1585 }
1586
1587 test_cleanup_vxlanX_exception() {
1588         outer="${1}"
1589         encap="vxlan"
1590         ll_mtu=4000
1591
1592         check_command taskset || return $ksft_skip
1593         cpu_list=$(grep -m 2 processor /proc/cpuinfo | cut -d ' ' -f 2)
1594
1595         setup namespaces routing ${encap}${outer} || return $ksft_skip
1596         trace "${ns_a}" ${encap}_a   "${ns_b}"  ${encap}_b \
1597               "${ns_a}" veth_A-R1    "${ns_r1}" veth_R1-A \
1598               "${ns_b}" veth_B-R1    "${ns_r1}" veth_R1-B
1599
1600         # Create route exception by exceeding link layer MTU
1601         mtu "${ns_a}"  veth_A-R1 $((${ll_mtu} + 1000))
1602         mtu "${ns_r1}" veth_R1-A $((${ll_mtu} + 1000))
1603         mtu "${ns_b}"  veth_B-R1 ${ll_mtu}
1604         mtu "${ns_r1}" veth_R1-B ${ll_mtu}
1605
1606         mtu "${ns_a}" ${encap}_a $((${ll_mtu} + 1000))
1607         mtu "${ns_b}" ${encap}_b $((${ll_mtu} + 1000))
1608
1609         # Fill exception cache for multiple CPUs (2)
1610         # we can always use inner IPv4 for that
1611         for cpu in ${cpu_list}; do
1612                 run_cmd taskset --cpu-list ${cpu} ${ns_a} ping -q -M want -i 0.1 -w 1 -s $((${ll_mtu} + 500)) ${tunnel4_b_addr}
1613         done
1614
1615         ${ns_a} ip link del dev veth_A-R1 &
1616         iplink_pid=$!
1617         sleep 1
1618         if [ "$(cat /proc/${iplink_pid}/cmdline 2>/dev/null | tr -d '\0')" = "iplinkdeldevveth_A-R1" ]; then
1619                 err "  can't delete veth device in a timely manner, PMTU dst likely leaked"
1620                 return 1
1621         fi
1622 }
1623
1624 test_cleanup_ipv6_exception() {
1625         test_cleanup_vxlanX_exception 6
1626 }
1627
1628 test_cleanup_ipv4_exception() {
1629         test_cleanup_vxlanX_exception 4
1630 }
1631
1632 run_test() {
1633         (
1634         tname="$1"
1635         tdesc="$2"
1636
1637         unset IFS
1638
1639         if [ "$VERBOSE" = "1" ]; then
1640                 printf "\n##########################################################################\n\n"
1641         fi
1642
1643         eval test_${tname}
1644         ret=$?
1645
1646         if [ $ret -eq 0 ]; then
1647                 printf "TEST: %-60s  [ OK ]\n" "${tdesc}"
1648         elif [ $ret -eq 1 ]; then
1649                 printf "TEST: %-60s  [FAIL]\n" "${tdesc}"
1650                 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
1651                         echo
1652                         echo "Pausing. Hit enter to continue"
1653                         read a
1654                 fi
1655                 err_flush
1656                 exit 1
1657         elif [ $ret -eq $ksft_skip ]; then
1658                 printf "TEST: %-60s  [SKIP]\n" "${tdesc}"
1659                 err_flush
1660         fi
1661
1662         return $ret
1663         )
1664         ret=$?
1665         case $ret in
1666                 0)
1667                         all_skipped=false
1668                         [ $exitcode=$ksft_skip ] && exitcode=0
1669                 ;;
1670                 $ksft_skip)
1671                         [ $all_skipped = true ] && exitcode=$ksft_skip
1672                 ;;
1673                 *)
1674                         all_skipped=false
1675                         exitcode=1
1676                 ;;
1677         esac
1678
1679         return $ret
1680 }
1681
1682 run_test_nh() {
1683         tname="$1"
1684         tdesc="$2"
1685
1686         USE_NH=yes
1687         run_test "${tname}" "${tdesc} - nexthop objects"
1688         USE_NH=no
1689 }
1690
1691 test_list_flush_ipv4_exception() {
1692         setup namespaces routing || return $ksft_skip
1693         trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
1694               "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
1695               "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
1696               "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
1697
1698         dst_prefix1="${prefix4}.${b_r1}."
1699         dst2="${prefix4}.${b_r2}.1"
1700
1701         # Set up initial MTU values
1702         mtu "${ns_a}"  veth_A-R1 2000
1703         mtu "${ns_r1}" veth_R1-A 2000
1704         mtu "${ns_r1}" veth_R1-B 1500
1705         mtu "${ns_b}"  veth_B-R1 1500
1706
1707         mtu "${ns_a}"  veth_A-R2 2000
1708         mtu "${ns_r2}" veth_R2-A 2000
1709         mtu "${ns_r2}" veth_R2-B 1500
1710         mtu "${ns_b}"  veth_B-R2 1500
1711
1712         fail=0
1713
1714         # Add 100 addresses for veth endpoint on B reached by default A route
1715         for i in $(seq 100 199); do
1716                 run_cmd ${ns_b} ip addr add "${dst_prefix1}${i}" dev veth_B-R1
1717         done
1718
1719         # Create 100 cached route exceptions for path via R1, one via R2. Note
1720         # that with IPv4 we need to actually cause a route lookup that matches
1721         # the exception caused by ICMP, in order to actually have a cached
1722         # route, so we need to ping each destination twice
1723         for i in $(seq 100 199); do
1724                 run_cmd ${ns_a} ping -q -M want -i 0.1 -c 2 -s 1800 "${dst_prefix1}${i}"
1725         done
1726         run_cmd ${ns_a} ping -q -M want -i 0.1 -c 2 -s 1800 "${dst2}"
1727
1728         if [ "$(${ns_a} ip -oneline route list cache | wc -l)" -ne 101 ]; then
1729                 err "  can't list cached exceptions"
1730                 fail=1
1731         fi
1732
1733         run_cmd ${ns_a} ip route flush cache
1734         pmtu1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst_prefix}1)"
1735         pmtu2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst_prefix}2)"
1736         if [ -n "${pmtu1}" ] || [ -n "${pmtu2}" ] || \
1737            [ -n "$(${ns_a} ip route list cache)" ]; then
1738                 err "  can't flush cached exceptions"
1739                 fail=1
1740         fi
1741
1742         return ${fail}
1743 }
1744
1745 test_list_flush_ipv6_exception() {
1746         setup namespaces routing || return $ksft_skip
1747         trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
1748               "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
1749               "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
1750               "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
1751
1752         dst_prefix1="${prefix6}:${b_r1}::"
1753         dst2="${prefix6}:${b_r2}::1"
1754
1755         # Set up initial MTU values
1756         mtu "${ns_a}"  veth_A-R1 2000
1757         mtu "${ns_r1}" veth_R1-A 2000
1758         mtu "${ns_r1}" veth_R1-B 1500
1759         mtu "${ns_b}"  veth_B-R1 1500
1760
1761         mtu "${ns_a}"  veth_A-R2 2000
1762         mtu "${ns_r2}" veth_R2-A 2000
1763         mtu "${ns_r2}" veth_R2-B 1500
1764         mtu "${ns_b}"  veth_B-R2 1500
1765
1766         fail=0
1767
1768         # Add 100 addresses for veth endpoint on B reached by default A route
1769         for i in $(seq 100 199); do
1770                 run_cmd ${ns_b} ip addr add "${dst_prefix1}${i}" dev veth_B-R1
1771         done
1772
1773         # Create 100 cached route exceptions for path via R1, one via R2
1774         for i in $(seq 100 199); do
1775                 run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s 1800 "${dst_prefix1}${i}"
1776         done
1777         run_cmd ${ns_a} ping -q -M want -i 0.1 -w 1 -s 1800 "${dst2}"
1778         if [ "$(${ns_a} ip -oneline -6 route list cache | wc -l)" -ne 101 ]; then
1779                 err "  can't list cached exceptions"
1780                 fail=1
1781         fi
1782
1783         run_cmd ${ns_a} ip -6 route flush cache
1784         pmtu1="$(route_get_dst_pmtu_from_exception "${ns_a}" "${dst_prefix1}100")"
1785         pmtu2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1786         if [ -n "${pmtu1}" ] || [ -n "${pmtu2}" ] || \
1787            [ -n "$(${ns_a} ip -6 route list cache)" ]; then
1788                 err "  can't flush cached exceptions"
1789                 fail=1
1790         fi
1791
1792         return ${fail}
1793 }
1794
1795 test_pmtu_ipvX_route_change() {
1796         family=${1}
1797
1798         setup namespaces routing || return 2
1799         trace "${ns_a}"  veth_A-R1    "${ns_r1}" veth_R1-A \
1800               "${ns_r1}" veth_R1-B    "${ns_b}"  veth_B-R1 \
1801               "${ns_a}"  veth_A-R2    "${ns_r2}" veth_R2-A \
1802               "${ns_r2}" veth_R2-B    "${ns_b}"  veth_B-R2
1803
1804         if [ ${family} -eq 4 ]; then
1805                 ping=ping
1806                 dst1="${prefix4}.${b_r1}.1"
1807                 dst2="${prefix4}.${b_r2}.1"
1808                 gw="${prefix4}.${a_r1}.2"
1809         else
1810                 ping=${ping6}
1811                 dst1="${prefix6}:${b_r1}::1"
1812                 dst2="${prefix6}:${b_r2}::1"
1813                 gw="${prefix6}:${a_r1}::2"
1814         fi
1815
1816         # Set up initial MTU values
1817         mtu "${ns_a}"  veth_A-R1 2000
1818         mtu "${ns_r1}" veth_R1-A 2000
1819         mtu "${ns_r1}" veth_R1-B 1400
1820         mtu "${ns_b}"  veth_B-R1 1400
1821
1822         mtu "${ns_a}"  veth_A-R2 2000
1823         mtu "${ns_r2}" veth_R2-A 2000
1824         mtu "${ns_r2}" veth_R2-B 1500
1825         mtu "${ns_b}"  veth_B-R2 1500
1826
1827         # Create route exceptions
1828         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst1}
1829         run_cmd ${ns_a} ${ping} -q -M want -i 0.1 -w 1 -s 1800 ${dst2}
1830
1831         # Check that exceptions have been created with the correct PMTU
1832         pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
1833         check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
1834         pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
1835         check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
1836
1837         # Replace the route from A to R1
1838         run_cmd ${ns_a} ip route change default via ${gw}
1839
1840         # Delete the device in A
1841         run_cmd ${ns_a} ip link del "veth_A-R1"
1842 }
1843
1844 test_pmtu_ipv4_route_change() {
1845         test_pmtu_ipvX_route_change 4
1846 }
1847
1848 test_pmtu_ipv6_route_change() {
1849         test_pmtu_ipvX_route_change 6
1850 }
1851
1852 usage() {
1853         echo
1854         echo "$0 [OPTIONS] [TEST]..."
1855         echo "If no TEST argument is given, all tests will be run."
1856         echo
1857         echo "Options"
1858         echo "  --trace: capture traffic to TEST_INTERFACE.pcap"
1859         echo
1860         echo "Available tests${tests}"
1861         exit 1
1862 }
1863
1864 ################################################################################
1865 #
1866 exitcode=0
1867 desc=0
1868 all_skipped=true
1869
1870 while getopts :ptv o
1871 do
1872         case $o in
1873         p) PAUSE_ON_FAIL=yes;;
1874         v) VERBOSE=1;;
1875         t) if which tcpdump > /dev/null 2>&1; then
1876                 TRACING=1
1877            else
1878                 echo "=== tcpdump not available, tracing disabled"
1879            fi
1880            ;;
1881         *) usage;;
1882         esac
1883 done
1884 shift $(($OPTIND-1))
1885
1886 IFS="   
1887 "
1888
1889 for arg do
1890         # Check first that all requested tests are available before running any
1891         command -v > /dev/null "test_${arg}" || { echo "=== Test ${arg} not found"; usage; }
1892 done
1893
1894 trap cleanup EXIT
1895
1896 # start clean
1897 cleanup
1898
1899 HAVE_NH=no
1900 ip nexthop ls >/dev/null 2>&1
1901 [ $? -eq 0 ] && HAVE_NH=yes
1902
1903 name=""
1904 desc=""
1905 rerun_nh=0
1906 for t in ${tests}; do
1907         [ "${name}" = "" ]      && name="${t}"  && continue
1908         [ "${desc}" = "" ]      && desc="${t}"  && continue
1909
1910         if [ "${HAVE_NH}" = "yes" ]; then
1911                 rerun_nh="${t}"
1912         fi
1913
1914         run_this=1
1915         for arg do
1916                 [ "${arg}" != "${arg#--*}" ] && continue
1917                 [ "${arg}" = "${name}" ] && run_this=1 && break
1918                 run_this=0
1919         done
1920         if [ $run_this -eq 1 ]; then
1921                 run_test "${name}" "${desc}"
1922                 # if test was skipped no need to retry with nexthop objects
1923                 [ $? -eq $ksft_skip ] && rerun_nh=0
1924
1925                 if [ "${rerun_nh}" = "1" ]; then
1926                         run_test_nh "${name}" "${desc}"
1927                 fi
1928         fi
1929         name=""
1930         desc=""
1931         rerun_nh=0
1932 done
1933
1934 exit ${exitcode}