]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/testing/selftests/net/fib_tests.sh
Merge tag '9p-for-5.1' of git://github.com/martinetd/linux
[linux.git] / tools / testing / selftests / net / fib_tests.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3
4 # This test is for checking IPv4 and IPv6 FIB behavior in response to
5 # different events.
6
7 ret=0
8 # Kselftest framework requirement - SKIP code is 4.
9 ksft_skip=4
10
11 # all tests in this script. Can be overridden with -t option
12 TESTS="unregister down carrier nexthop ipv6_rt ipv4_rt ipv6_addr_metric ipv4_addr_metric ipv6_route_metrics ipv4_route_metrics"
13 VERBOSE=0
14 PAUSE_ON_FAIL=no
15 PAUSE=no
16 IP="ip -netns ns1"
17
18 log_test()
19 {
20         local rc=$1
21         local expected=$2
22         local msg="$3"
23
24         if [ ${rc} -eq ${expected} ]; then
25                 printf "    TEST: %-60s  [ OK ]\n" "${msg}"
26                 nsuccess=$((nsuccess+1))
27         else
28                 ret=1
29                 nfail=$((nfail+1))
30                 printf "    TEST: %-60s  [FAIL]\n" "${msg}"
31                 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
32                 echo
33                         echo "hit enter to continue, 'q' to quit"
34                         read a
35                         [ "$a" = "q" ] && exit 1
36                 fi
37         fi
38
39         if [ "${PAUSE}" = "yes" ]; then
40                 echo
41                 echo "hit enter to continue, 'q' to quit"
42                 read a
43                 [ "$a" = "q" ] && exit 1
44         fi
45 }
46
47 setup()
48 {
49         set -e
50         ip netns add ns1
51         $IP link set dev lo up
52         ip netns exec ns1 sysctl -qw net.ipv4.ip_forward=1
53         ip netns exec ns1 sysctl -qw net.ipv6.conf.all.forwarding=1
54
55         $IP link add dummy0 type dummy
56         $IP link set dev dummy0 up
57         $IP address add 198.51.100.1/24 dev dummy0
58         $IP -6 address add 2001:db8:1::1/64 dev dummy0
59         set +e
60
61 }
62
63 cleanup()
64 {
65         $IP link del dev dummy0 &> /dev/null
66         ip netns del ns1
67         ip netns del ns2 &> /dev/null
68 }
69
70 get_linklocal()
71 {
72         local dev=$1
73         local addr
74
75         addr=$($IP -6 -br addr show dev ${dev} | \
76         awk '{
77                 for (i = 3; i <= NF; ++i) {
78                         if ($i ~ /^fe80/)
79                                 print $i
80                 }
81         }'
82         )
83         addr=${addr/\/*}
84
85         [ -z "$addr" ] && return 1
86
87         echo $addr
88
89         return 0
90 }
91
92 fib_unreg_unicast_test()
93 {
94         echo
95         echo "Single path route test"
96
97         setup
98
99         echo "    Start point"
100         $IP route get fibmatch 198.51.100.2 &> /dev/null
101         log_test $? 0 "IPv4 fibmatch"
102         $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
103         log_test $? 0 "IPv6 fibmatch"
104
105         set -e
106         $IP link del dev dummy0
107         set +e
108
109         echo "    Nexthop device deleted"
110         $IP route get fibmatch 198.51.100.2 &> /dev/null
111         log_test $? 2 "IPv4 fibmatch - no route"
112         $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
113         log_test $? 2 "IPv6 fibmatch - no route"
114
115         cleanup
116 }
117
118 fib_unreg_multipath_test()
119 {
120
121         echo
122         echo "Multipath route test"
123
124         setup
125
126         set -e
127         $IP link add dummy1 type dummy
128         $IP link set dev dummy1 up
129         $IP address add 192.0.2.1/24 dev dummy1
130         $IP -6 address add 2001:db8:2::1/64 dev dummy1
131
132         $IP route add 203.0.113.0/24 \
133                 nexthop via 198.51.100.2 dev dummy0 \
134                 nexthop via 192.0.2.2 dev dummy1
135         $IP -6 route add 2001:db8:3::/64 \
136                 nexthop via 2001:db8:1::2 dev dummy0 \
137                 nexthop via 2001:db8:2::2 dev dummy1
138         set +e
139
140         echo "    Start point"
141         $IP route get fibmatch 203.0.113.1 &> /dev/null
142         log_test $? 0 "IPv4 fibmatch"
143         $IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null
144         log_test $? 0 "IPv6 fibmatch"
145
146         set -e
147         $IP link del dev dummy0
148         set +e
149
150         echo "    One nexthop device deleted"
151         $IP route get fibmatch 203.0.113.1 &> /dev/null
152         log_test $? 2 "IPv4 - multipath route removed on delete"
153
154         $IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null
155         # In IPv6 we do not flush the entire multipath route.
156         log_test $? 0 "IPv6 - multipath down to single path"
157
158         set -e
159         $IP link del dev dummy1
160         set +e
161
162         echo "    Second nexthop device deleted"
163         $IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null
164         log_test $? 2 "IPv6 - no route"
165
166         cleanup
167 }
168
169 fib_unreg_test()
170 {
171         fib_unreg_unicast_test
172         fib_unreg_multipath_test
173 }
174
175 fib_down_unicast_test()
176 {
177         echo
178         echo "Single path, admin down"
179
180         setup
181
182         echo "    Start point"
183         $IP route get fibmatch 198.51.100.2 &> /dev/null
184         log_test $? 0 "IPv4 fibmatch"
185         $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
186         log_test $? 0 "IPv6 fibmatch"
187
188         set -e
189         $IP link set dev dummy0 down
190         set +e
191
192         echo "    Route deleted on down"
193         $IP route get fibmatch 198.51.100.2 &> /dev/null
194         log_test $? 2 "IPv4 fibmatch"
195         $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
196         log_test $? 2 "IPv6 fibmatch"
197
198         cleanup
199 }
200
201 fib_down_multipath_test_do()
202 {
203         local down_dev=$1
204         local up_dev=$2
205
206         $IP route get fibmatch 203.0.113.1 \
207                 oif $down_dev &> /dev/null
208         log_test $? 2 "IPv4 fibmatch on down device"
209         $IP -6 route get fibmatch 2001:db8:3::1 \
210                 oif $down_dev &> /dev/null
211         log_test $? 2 "IPv6 fibmatch on down device"
212
213         $IP route get fibmatch 203.0.113.1 \
214                 oif $up_dev &> /dev/null
215         log_test $? 0 "IPv4 fibmatch on up device"
216         $IP -6 route get fibmatch 2001:db8:3::1 \
217                 oif $up_dev &> /dev/null
218         log_test $? 0 "IPv6 fibmatch on up device"
219
220         $IP route get fibmatch 203.0.113.1 | \
221                 grep $down_dev | grep -q "dead linkdown"
222         log_test $? 0 "IPv4 flags on down device"
223         $IP -6 route get fibmatch 2001:db8:3::1 | \
224                 grep $down_dev | grep -q "dead linkdown"
225         log_test $? 0 "IPv6 flags on down device"
226
227         $IP route get fibmatch 203.0.113.1 | \
228                 grep $up_dev | grep -q "dead linkdown"
229         log_test $? 1 "IPv4 flags on up device"
230         $IP -6 route get fibmatch 2001:db8:3::1 | \
231                 grep $up_dev | grep -q "dead linkdown"
232         log_test $? 1 "IPv6 flags on up device"
233 }
234
235 fib_down_multipath_test()
236 {
237         echo
238         echo "Admin down multipath"
239
240         setup
241
242         set -e
243         $IP link add dummy1 type dummy
244         $IP link set dev dummy1 up
245
246         $IP address add 192.0.2.1/24 dev dummy1
247         $IP -6 address add 2001:db8:2::1/64 dev dummy1
248
249         $IP route add 203.0.113.0/24 \
250                 nexthop via 198.51.100.2 dev dummy0 \
251                 nexthop via 192.0.2.2 dev dummy1
252         $IP -6 route add 2001:db8:3::/64 \
253                 nexthop via 2001:db8:1::2 dev dummy0 \
254                 nexthop via 2001:db8:2::2 dev dummy1
255         set +e
256
257         echo "    Verify start point"
258         $IP route get fibmatch 203.0.113.1 &> /dev/null
259         log_test $? 0 "IPv4 fibmatch"
260
261         $IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null
262         log_test $? 0 "IPv6 fibmatch"
263
264         set -e
265         $IP link set dev dummy0 down
266         set +e
267
268         echo "    One device down, one up"
269         fib_down_multipath_test_do "dummy0" "dummy1"
270
271         set -e
272         $IP link set dev dummy0 up
273         $IP link set dev dummy1 down
274         set +e
275
276         echo "    Other device down and up"
277         fib_down_multipath_test_do "dummy1" "dummy0"
278
279         set -e
280         $IP link set dev dummy0 down
281         set +e
282
283         echo "    Both devices down"
284         $IP route get fibmatch 203.0.113.1 &> /dev/null
285         log_test $? 2 "IPv4 fibmatch"
286         $IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null
287         log_test $? 2 "IPv6 fibmatch"
288
289         $IP link del dev dummy1
290         cleanup
291 }
292
293 fib_down_test()
294 {
295         fib_down_unicast_test
296         fib_down_multipath_test
297 }
298
299 # Local routes should not be affected when carrier changes.
300 fib_carrier_local_test()
301 {
302         echo
303         echo "Local carrier tests - single path"
304
305         setup
306
307         set -e
308         $IP link set dev dummy0 carrier on
309         set +e
310
311         echo "    Start point"
312         $IP route get fibmatch 198.51.100.1 &> /dev/null
313         log_test $? 0 "IPv4 fibmatch"
314         $IP -6 route get fibmatch 2001:db8:1::1 &> /dev/null
315         log_test $? 0 "IPv6 fibmatch"
316
317         $IP route get fibmatch 198.51.100.1 | \
318                 grep -q "linkdown"
319         log_test $? 1 "IPv4 - no linkdown flag"
320         $IP -6 route get fibmatch 2001:db8:1::1 | \
321                 grep -q "linkdown"
322         log_test $? 1 "IPv6 - no linkdown flag"
323
324         set -e
325         $IP link set dev dummy0 carrier off
326         sleep 1
327         set +e
328
329         echo "    Carrier off on nexthop"
330         $IP route get fibmatch 198.51.100.1 &> /dev/null
331         log_test $? 0 "IPv4 fibmatch"
332         $IP -6 route get fibmatch 2001:db8:1::1 &> /dev/null
333         log_test $? 0 "IPv6 fibmatch"
334
335         $IP route get fibmatch 198.51.100.1 | \
336                 grep -q "linkdown"
337         log_test $? 1 "IPv4 - linkdown flag set"
338         $IP -6 route get fibmatch 2001:db8:1::1 | \
339                 grep -q "linkdown"
340         log_test $? 1 "IPv6 - linkdown flag set"
341
342         set -e
343         $IP address add 192.0.2.1/24 dev dummy0
344         $IP -6 address add 2001:db8:2::1/64 dev dummy0
345         set +e
346
347         echo "    Route to local address with carrier down"
348         $IP route get fibmatch 192.0.2.1 &> /dev/null
349         log_test $? 0 "IPv4 fibmatch"
350         $IP -6 route get fibmatch 2001:db8:2::1 &> /dev/null
351         log_test $? 0 "IPv6 fibmatch"
352
353         $IP route get fibmatch 192.0.2.1 | \
354                 grep -q "linkdown"
355         log_test $? 1 "IPv4 linkdown flag set"
356         $IP -6 route get fibmatch 2001:db8:2::1 | \
357                 grep -q "linkdown"
358         log_test $? 1 "IPv6 linkdown flag set"
359
360         cleanup
361 }
362
363 fib_carrier_unicast_test()
364 {
365         ret=0
366
367         echo
368         echo "Single path route carrier test"
369
370         setup
371
372         set -e
373         $IP link set dev dummy0 carrier on
374         set +e
375
376         echo "    Start point"
377         $IP route get fibmatch 198.51.100.2 &> /dev/null
378         log_test $? 0 "IPv4 fibmatch"
379         $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
380         log_test $? 0 "IPv6 fibmatch"
381
382         $IP route get fibmatch 198.51.100.2 | \
383                 grep -q "linkdown"
384         log_test $? 1 "IPv4 no linkdown flag"
385         $IP -6 route get fibmatch 2001:db8:1::2 | \
386                 grep -q "linkdown"
387         log_test $? 1 "IPv6 no linkdown flag"
388
389         set -e
390         $IP link set dev dummy0 carrier off
391         sleep 1
392         set +e
393
394         echo "    Carrier down"
395         $IP route get fibmatch 198.51.100.2 &> /dev/null
396         log_test $? 0 "IPv4 fibmatch"
397         $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
398         log_test $? 0 "IPv6 fibmatch"
399
400         $IP route get fibmatch 198.51.100.2 | \
401                 grep -q "linkdown"
402         log_test $? 0 "IPv4 linkdown flag set"
403         $IP -6 route get fibmatch 2001:db8:1::2 | \
404                 grep -q "linkdown"
405         log_test $? 0 "IPv6 linkdown flag set"
406
407         set -e
408         $IP address add 192.0.2.1/24 dev dummy0
409         $IP -6 address add 2001:db8:2::1/64 dev dummy0
410         set +e
411
412         echo "    Second address added with carrier down"
413         $IP route get fibmatch 192.0.2.2 &> /dev/null
414         log_test $? 0 "IPv4 fibmatch"
415         $IP -6 route get fibmatch 2001:db8:2::2 &> /dev/null
416         log_test $? 0 "IPv6 fibmatch"
417
418         $IP route get fibmatch 192.0.2.2 | \
419                 grep -q "linkdown"
420         log_test $? 0 "IPv4 linkdown flag set"
421         $IP -6 route get fibmatch 2001:db8:2::2 | \
422                 grep -q "linkdown"
423         log_test $? 0 "IPv6 linkdown flag set"
424
425         cleanup
426 }
427
428 fib_carrier_test()
429 {
430         fib_carrier_local_test
431         fib_carrier_unicast_test
432 }
433
434 ################################################################################
435 # Tests on nexthop spec
436
437 # run 'ip route add' with given spec
438 add_rt()
439 {
440         local desc="$1"
441         local erc=$2
442         local vrf=$3
443         local pfx=$4
444         local gw=$5
445         local dev=$6
446         local cmd out rc
447
448         [ "$vrf" = "-" ] && vrf="default"
449         [ -n "$gw" ] && gw="via $gw"
450         [ -n "$dev" ] && dev="dev $dev"
451
452         cmd="$IP route add vrf $vrf $pfx $gw $dev"
453         if [ "$VERBOSE" = "1" ]; then
454                 printf "\n    COMMAND: $cmd\n"
455         fi
456
457         out=$(eval $cmd 2>&1)
458         rc=$?
459         if [ "$VERBOSE" = "1" -a -n "$out" ]; then
460                 echo "    $out"
461         fi
462         log_test $rc $erc "$desc"
463 }
464
465 fib4_nexthop()
466 {
467         echo
468         echo "IPv4 nexthop tests"
469
470         echo "<<< write me >>>"
471 }
472
473 fib6_nexthop()
474 {
475         local lldummy=$(get_linklocal dummy0)
476         local llv1=$(get_linklocal dummy0)
477
478         if [ -z "$lldummy" ]; then
479                 echo "Failed to get linklocal address for dummy0"
480                 return 1
481         fi
482         if [ -z "$llv1" ]; then
483                 echo "Failed to get linklocal address for veth1"
484                 return 1
485         fi
486
487         echo
488         echo "IPv6 nexthop tests"
489
490         add_rt "Directly connected nexthop, unicast address" 0 \
491                 - 2001:db8:101::/64 2001:db8:1::2
492         add_rt "Directly connected nexthop, unicast address with device" 0 \
493                 - 2001:db8:102::/64 2001:db8:1::2 "dummy0"
494         add_rt "Gateway is linklocal address" 0 \
495                 - 2001:db8:103::1/64 $llv1 "veth0"
496
497         # fails because LL address requires a device
498         add_rt "Gateway is linklocal address, no device" 2 \
499                 - 2001:db8:104::1/64 $llv1
500
501         # local address can not be a gateway
502         add_rt "Gateway can not be local unicast address" 2 \
503                 - 2001:db8:105::/64 2001:db8:1::1
504         add_rt "Gateway can not be local unicast address, with device" 2 \
505                 - 2001:db8:106::/64 2001:db8:1::1 "dummy0"
506         add_rt "Gateway can not be a local linklocal address" 2 \
507                 - 2001:db8:107::1/64 $lldummy "dummy0"
508
509         # VRF tests
510         add_rt "Gateway can be local address in a VRF" 0 \
511                 - 2001:db8:108::/64 2001:db8:51::2
512         add_rt "Gateway can be local address in a VRF, with device" 0 \
513                 - 2001:db8:109::/64 2001:db8:51::2 "veth0"
514         add_rt "Gateway can be local linklocal address in a VRF" 0 \
515                 - 2001:db8:110::1/64 $llv1 "veth0"
516
517         add_rt "Redirect to VRF lookup" 0 \
518                 - 2001:db8:111::/64 "" "red"
519
520         add_rt "VRF route, gateway can be local address in default VRF" 0 \
521                 red 2001:db8:112::/64 2001:db8:51::1
522
523         # local address in same VRF fails
524         add_rt "VRF route, gateway can not be a local address" 2 \
525                 red 2001:db8:113::1/64 2001:db8:2::1
526         add_rt "VRF route, gateway can not be a local addr with device" 2 \
527                 red 2001:db8:114::1/64 2001:db8:2::1 "dummy1"
528 }
529
530 # Default VRF:
531 #   dummy0 - 198.51.100.1/24 2001:db8:1::1/64
532 #   veth0  - 192.0.2.1/24    2001:db8:51::1/64
533 #
534 # VRF red:
535 #   dummy1 - 192.168.2.1/24 2001:db8:2::1/64
536 #   veth1  - 192.0.2.2/24   2001:db8:51::2/64
537 #
538 #  [ dummy0   veth0 ]--[ veth1   dummy1 ]
539
540 fib_nexthop_test()
541 {
542         setup
543
544         set -e
545
546         $IP -4 rule add pref 32765 table local
547         $IP -4 rule del pref 0
548         $IP -6 rule add pref 32765 table local
549         $IP -6 rule del pref 0
550
551         $IP link add red type vrf table 1
552         $IP link set red up
553         $IP -4 route add vrf red unreachable default metric 4278198272
554         $IP -6 route add vrf red unreachable default metric 4278198272
555
556         $IP link add veth0 type veth peer name veth1
557         $IP link set dev veth0 up
558         $IP address add 192.0.2.1/24 dev veth0
559         $IP -6 address add 2001:db8:51::1/64 dev veth0
560
561         $IP link set dev veth1 vrf red up
562         $IP address add 192.0.2.2/24 dev veth1
563         $IP -6 address add 2001:db8:51::2/64 dev veth1
564
565         $IP link add dummy1 type dummy
566         $IP link set dev dummy1 vrf red up
567         $IP address add 192.168.2.1/24 dev dummy1
568         $IP -6 address add 2001:db8:2::1/64 dev dummy1
569         set +e
570
571         sleep 1
572         fib4_nexthop
573         fib6_nexthop
574
575         (
576         $IP link del dev dummy1
577         $IP link del veth0
578         $IP link del red
579         ) 2>/dev/null
580         cleanup
581 }
582
583 ################################################################################
584 # Tests on route add and replace
585
586 run_cmd()
587 {
588         local cmd="$1"
589         local out
590         local stderr="2>/dev/null"
591
592         if [ "$VERBOSE" = "1" ]; then
593                 printf "    COMMAND: $cmd\n"
594                 stderr=
595         fi
596
597         out=$(eval $cmd $stderr)
598         rc=$?
599         if [ "$VERBOSE" = "1" -a -n "$out" ]; then
600                 echo "    $out"
601         fi
602
603         [ "$VERBOSE" = "1" ] && echo
604
605         return $rc
606 }
607
608 # add route for a prefix, flushing any existing routes first
609 # expected to be the first step of a test
610 add_route6()
611 {
612         local pfx="$1"
613         local nh="$2"
614         local out
615
616         if [ "$VERBOSE" = "1" ]; then
617                 echo
618                 echo "    ##################################################"
619                 echo
620         fi
621
622         run_cmd "$IP -6 ro flush ${pfx}"
623         [ $? -ne 0 ] && exit 1
624
625         out=$($IP -6 ro ls match ${pfx})
626         if [ -n "$out" ]; then
627                 echo "Failed to flush routes for prefix used for tests."
628                 exit 1
629         fi
630
631         run_cmd "$IP -6 ro add ${pfx} ${nh}"
632         if [ $? -ne 0 ]; then
633                 echo "Failed to add initial route for test."
634                 exit 1
635         fi
636 }
637
638 # add initial route - used in replace route tests
639 add_initial_route6()
640 {
641         add_route6 "2001:db8:104::/64" "$1"
642 }
643
644 check_route6()
645 {
646         local pfx
647         local expected="$1"
648         local out
649         local rc=0
650
651         set -- $expected
652         pfx=$1
653
654         out=$($IP -6 ro ls match ${pfx} | sed -e 's/ pref medium//')
655         [ "${out}" = "${expected}" ] && return 0
656
657         if [ -z "${out}" ]; then
658                 if [ "$VERBOSE" = "1" ]; then
659                         printf "\nNo route entry found\n"
660                         printf "Expected:\n"
661                         printf "    ${expected}\n"
662                 fi
663                 return 1
664         fi
665
666         # tricky way to convert output to 1-line without ip's
667         # messy '\'; this drops all extra white space
668         out=$(echo ${out})
669         if [ "${out}" != "${expected}" ]; then
670                 rc=1
671                 if [ "${VERBOSE}" = "1" ]; then
672                         printf "    Unexpected route entry. Have:\n"
673                         printf "        ${out}\n"
674                         printf "    Expected:\n"
675                         printf "        ${expected}\n\n"
676                 fi
677         fi
678
679         return $rc
680 }
681
682 route_cleanup()
683 {
684         $IP li del red 2>/dev/null
685         $IP li del dummy1 2>/dev/null
686         $IP li del veth1 2>/dev/null
687         $IP li del veth3 2>/dev/null
688
689         cleanup &> /dev/null
690 }
691
692 route_setup()
693 {
694         route_cleanup
695         setup
696
697         [ "${VERBOSE}" = "1" ] && set -x
698         set -e
699
700         ip netns add ns2
701         ip -netns ns2 link set dev lo up
702         ip netns exec ns2 sysctl -qw net.ipv4.ip_forward=1
703         ip netns exec ns2 sysctl -qw net.ipv6.conf.all.forwarding=1
704
705         $IP li add veth1 type veth peer name veth2
706         $IP li add veth3 type veth peer name veth4
707
708         $IP li set veth1 up
709         $IP li set veth3 up
710         $IP li set veth2 netns ns2 up
711         $IP li set veth4 netns ns2 up
712         ip -netns ns2 li add dummy1 type dummy
713         ip -netns ns2 li set dummy1 up
714
715         $IP -6 addr add 2001:db8:101::1/64 dev veth1 nodad
716         $IP -6 addr add 2001:db8:103::1/64 dev veth3 nodad
717         $IP addr add 172.16.101.1/24 dev veth1
718         $IP addr add 172.16.103.1/24 dev veth3
719
720         ip -netns ns2 -6 addr add 2001:db8:101::2/64 dev veth2 nodad
721         ip -netns ns2 -6 addr add 2001:db8:103::2/64 dev veth4 nodad
722         ip -netns ns2 -6 addr add 2001:db8:104::1/64 dev dummy1 nodad
723
724         ip -netns ns2 addr add 172.16.101.2/24 dev veth2
725         ip -netns ns2 addr add 172.16.103.2/24 dev veth4
726         ip -netns ns2 addr add 172.16.104.1/24 dev dummy1
727
728         set +ex
729 }
730
731 # assumption is that basic add of a single path route works
732 # otherwise just adding an address on an interface is broken
733 ipv6_rt_add()
734 {
735         local rc
736
737         echo
738         echo "IPv6 route add / append tests"
739
740         # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
741         add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
742         run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::2"
743         log_test $? 2 "Attempt to add duplicate route - gw"
744
745         # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
746         add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
747         run_cmd "$IP -6 ro add 2001:db8:104::/64 dev veth3"
748         log_test $? 2 "Attempt to add duplicate route - dev only"
749
750         # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
751         add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
752         run_cmd "$IP -6 ro add unreachable 2001:db8:104::/64"
753         log_test $? 2 "Attempt to add duplicate route - reject route"
754
755         # route append with same prefix adds a new route
756         # - iproute2 sets NLM_F_CREATE | NLM_F_APPEND
757         add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
758         run_cmd "$IP -6 ro append 2001:db8:104::/64 via 2001:db8:103::2"
759         check_route6 "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
760         log_test $? 0 "Append nexthop to existing route - gw"
761
762         # insert mpath directly
763         add_route6 "2001:db8:104::/64" "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
764         check_route6  "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
765         log_test $? 0 "Add multipath route"
766
767         add_route6 "2001:db8:104::/64" "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
768         run_cmd "$IP -6 ro add 2001:db8:104::/64 nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
769         log_test $? 2 "Attempt to add duplicate multipath route"
770
771         # insert of a second route without append but different metric
772         add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
773         run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::2 metric 512"
774         rc=$?
775         if [ $rc -eq 0 ]; then
776                 run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::3 metric 256"
777                 rc=$?
778         fi
779         log_test $rc 0 "Route add with different metrics"
780
781         run_cmd "$IP -6 ro del 2001:db8:104::/64 metric 512"
782         rc=$?
783         if [ $rc -eq 0 ]; then
784                 check_route6 "2001:db8:104::/64 via 2001:db8:103::3 dev veth3 metric 256 2001:db8:104::/64 via 2001:db8:101::2 dev veth1 metric 1024"
785                 rc=$?
786         fi
787         log_test $rc 0 "Route delete with metric"
788 }
789
790 ipv6_rt_replace_single()
791 {
792         # single path with single path
793         #
794         add_initial_route6 "via 2001:db8:101::2"
795         run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:103::2"
796         check_route6 "2001:db8:104::/64 via 2001:db8:103::2 dev veth3 metric 1024"
797         log_test $? 0 "Single path with single path"
798
799         # single path with multipath
800         #
801         add_initial_route6 "nexthop via 2001:db8:101::2"
802         run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::2"
803         check_route6 "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::3 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
804         log_test $? 0 "Single path with multipath"
805
806         # single path with single path using MULTIPATH attribute
807         #
808         add_initial_route6 "via 2001:db8:101::2"
809         run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:103::2"
810         check_route6 "2001:db8:104::/64 via 2001:db8:103::2 dev veth3 metric 1024"
811         log_test $? 0 "Single path with single path via multipath attribute"
812
813         # route replace fails - invalid nexthop
814         add_initial_route6 "via 2001:db8:101::2"
815         run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:104::2"
816         if [ $? -eq 0 ]; then
817                 # previous command is expected to fail so if it returns 0
818                 # that means the test failed.
819                 log_test 0 1 "Invalid nexthop"
820         else
821                 check_route6 "2001:db8:104::/64 via 2001:db8:101::2 dev veth1 metric 1024"
822                 log_test $? 0 "Invalid nexthop"
823         fi
824
825         # replace non-existent route
826         # - note use of change versus replace since ip adds NLM_F_CREATE
827         #   for replace
828         add_initial_route6 "via 2001:db8:101::2"
829         run_cmd "$IP -6 ro change 2001:db8:105::/64 via 2001:db8:101::2"
830         log_test $? 2 "Single path - replace of non-existent route"
831 }
832
833 ipv6_rt_replace_mpath()
834 {
835         # multipath with multipath
836         add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
837         run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::3"
838         check_route6  "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::3 dev veth1 weight 1 nexthop via 2001:db8:103::3 dev veth3 weight 1"
839         log_test $? 0 "Multipath with multipath"
840
841         # multipath with single
842         add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
843         run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:101::3"
844         check_route6  "2001:db8:104::/64 via 2001:db8:101::3 dev veth1 metric 1024"
845         log_test $? 0 "Multipath with single path"
846
847         # multipath with single
848         add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
849         run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3"
850         check_route6 "2001:db8:104::/64 via 2001:db8:101::3 dev veth1 metric 1024"
851         log_test $? 0 "Multipath with single path via multipath attribute"
852
853         # route replace fails - invalid nexthop 1
854         add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
855         run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:111::3 nexthop via 2001:db8:103::3"
856         check_route6  "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
857         log_test $? 0 "Multipath - invalid first nexthop"
858
859         # route replace fails - invalid nexthop 2
860         add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
861         run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:113::3"
862         check_route6  "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
863         log_test $? 0 "Multipath - invalid second nexthop"
864
865         # multipath non-existent route
866         add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
867         run_cmd "$IP -6 ro change 2001:db8:105::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::3"
868         log_test $? 2 "Multipath - replace of non-existent route"
869 }
870
871 ipv6_rt_replace()
872 {
873         echo
874         echo "IPv6 route replace tests"
875
876         ipv6_rt_replace_single
877         ipv6_rt_replace_mpath
878 }
879
880 ipv6_route_test()
881 {
882         route_setup
883
884         ipv6_rt_add
885         ipv6_rt_replace
886
887         route_cleanup
888 }
889
890 ip_addr_metric_check()
891 {
892         ip addr help 2>&1 | grep -q metric
893         if [ $? -ne 0 ]; then
894                 echo "iproute2 command does not support metric for addresses. Skipping test"
895                 return 1
896         fi
897
898         return 0
899 }
900
901 ipv6_addr_metric_test()
902 {
903         local rc
904
905         echo
906         echo "IPv6 prefix route tests"
907
908         ip_addr_metric_check || return 1
909
910         setup
911
912         set -e
913         $IP li add dummy1 type dummy
914         $IP li add dummy2 type dummy
915         $IP li set dummy1 up
916         $IP li set dummy2 up
917
918         # default entry is metric 256
919         run_cmd "$IP -6 addr add dev dummy1 2001:db8:104::1/64"
920         run_cmd "$IP -6 addr add dev dummy2 2001:db8:104::2/64"
921         set +e
922
923         check_route6 "2001:db8:104::/64 dev dummy1 proto kernel metric 256 2001:db8:104::/64 dev dummy2 proto kernel metric 256"
924         log_test $? 0 "Default metric"
925
926         set -e
927         run_cmd "$IP -6 addr flush dev dummy1"
928         run_cmd "$IP -6 addr add dev dummy1 2001:db8:104::1/64 metric 257"
929         set +e
930
931         check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 256 2001:db8:104::/64 dev dummy1 proto kernel metric 257"
932         log_test $? 0 "User specified metric on first device"
933
934         set -e
935         run_cmd "$IP -6 addr flush dev dummy2"
936         run_cmd "$IP -6 addr add dev dummy2 2001:db8:104::2/64 metric 258"
937         set +e
938
939         check_route6 "2001:db8:104::/64 dev dummy1 proto kernel metric 257 2001:db8:104::/64 dev dummy2 proto kernel metric 258"
940         log_test $? 0 "User specified metric on second device"
941
942         run_cmd "$IP -6 addr del dev dummy1 2001:db8:104::1/64 metric 257"
943         rc=$?
944         if [ $rc -eq 0 ]; then
945                 check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 258"
946                 rc=$?
947         fi
948         log_test $rc 0 "Delete of address on first device"
949
950         run_cmd "$IP -6 addr change dev dummy2 2001:db8:104::2/64 metric 259"
951         rc=$?
952         if [ $rc -eq 0 ]; then
953                 check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 259"
954                 rc=$?
955         fi
956         log_test $rc 0 "Modify metric of address"
957
958         # verify prefix route removed on down
959         run_cmd "ip netns exec ns1 sysctl -qw net.ipv6.conf.all.keep_addr_on_down=1"
960         run_cmd "$IP li set dev dummy2 down"
961         rc=$?
962         if [ $rc -eq 0 ]; then
963                 check_route6 ""
964                 rc=$?
965         fi
966         log_test $rc 0 "Prefix route removed on link down"
967
968         # verify prefix route re-inserted with assigned metric
969         run_cmd "$IP li set dev dummy2 up"
970         rc=$?
971         if [ $rc -eq 0 ]; then
972                 check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 259"
973                 rc=$?
974         fi
975         log_test $rc 0 "Prefix route with metric on link up"
976
977         $IP li del dummy1
978         $IP li del dummy2
979         cleanup
980 }
981
982 ipv6_route_metrics_test()
983 {
984         local rc
985
986         echo
987         echo "IPv6 routes with metrics"
988
989         route_setup
990
991         #
992         # single path with metrics
993         #
994         run_cmd "$IP -6 ro add 2001:db8:111::/64 via 2001:db8:101::2 mtu 1400"
995         rc=$?
996         if [ $rc -eq 0 ]; then
997                 check_route6  "2001:db8:111::/64 via 2001:db8:101::2 dev veth1 metric 1024 mtu 1400"
998                 rc=$?
999         fi
1000         log_test $rc 0 "Single path route with mtu metric"
1001
1002
1003         #
1004         # multipath via separate routes with metrics
1005         #
1006         run_cmd "$IP -6 ro add 2001:db8:112::/64 via 2001:db8:101::2 mtu 1400"
1007         run_cmd "$IP -6 ro append 2001:db8:112::/64 via 2001:db8:103::2"
1008         rc=$?
1009         if [ $rc -eq 0 ]; then
1010                 check_route6 "2001:db8:112::/64 metric 1024 mtu 1400 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1011                 rc=$?
1012         fi
1013         log_test $rc 0 "Multipath route via 2 single routes with mtu metric on first"
1014
1015         # second route is coalesced to first to make a multipath route.
1016         # MTU of the second path is hidden from display!
1017         run_cmd "$IP -6 ro add 2001:db8:113::/64 via 2001:db8:101::2"
1018         run_cmd "$IP -6 ro append 2001:db8:113::/64 via 2001:db8:103::2 mtu 1400"
1019         rc=$?
1020         if [ $rc -eq 0 ]; then
1021                 check_route6 "2001:db8:113::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1022                 rc=$?
1023         fi
1024         log_test $rc 0 "Multipath route via 2 single routes with mtu metric on 2nd"
1025
1026         run_cmd "$IP -6 ro del 2001:db8:113::/64 via 2001:db8:101::2"
1027         if [ $? -eq 0 ]; then
1028                 check_route6 "2001:db8:113::/64 via 2001:db8:103::2 dev veth3 metric 1024 mtu 1400"
1029                 log_test $? 0 "    MTU of second leg"
1030         fi
1031
1032         #
1033         # multipath with metrics
1034         #
1035         run_cmd "$IP -6 ro add 2001:db8:115::/64 mtu 1400 nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1036         rc=$?
1037         if [ $rc -eq 0 ]; then
1038                 check_route6  "2001:db8:115::/64 metric 1024 mtu 1400 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1039                 rc=$?
1040         fi
1041         log_test $rc 0 "Multipath route with mtu metric"
1042
1043         $IP -6 ro add 2001:db8:104::/64 via 2001:db8:101::2 mtu 1300
1044         run_cmd "ip netns exec ns1 ping6 -w1 -c1 -s 1500 2001:db8:104::1"
1045         log_test $? 0 "Using route with mtu metric"
1046
1047         run_cmd "$IP -6 ro add 2001:db8:114::/64 via  2001:db8:101::2  congctl lock foo"
1048         log_test $? 2 "Invalid metric (fails metric_convert)"
1049
1050         route_cleanup
1051 }
1052
1053 # add route for a prefix, flushing any existing routes first
1054 # expected to be the first step of a test
1055 add_route()
1056 {
1057         local pfx="$1"
1058         local nh="$2"
1059         local out
1060
1061         if [ "$VERBOSE" = "1" ]; then
1062                 echo
1063                 echo "    ##################################################"
1064                 echo
1065         fi
1066
1067         run_cmd "$IP ro flush ${pfx}"
1068         [ $? -ne 0 ] && exit 1
1069
1070         out=$($IP ro ls match ${pfx})
1071         if [ -n "$out" ]; then
1072                 echo "Failed to flush routes for prefix used for tests."
1073                 exit 1
1074         fi
1075
1076         run_cmd "$IP ro add ${pfx} ${nh}"
1077         if [ $? -ne 0 ]; then
1078                 echo "Failed to add initial route for test."
1079                 exit 1
1080         fi
1081 }
1082
1083 # add initial route - used in replace route tests
1084 add_initial_route()
1085 {
1086         add_route "172.16.104.0/24" "$1"
1087 }
1088
1089 check_route()
1090 {
1091         local pfx
1092         local expected="$1"
1093         local out
1094         local rc=0
1095
1096         set -- $expected
1097         pfx=$1
1098         [ "${pfx}" = "unreachable" ] && pfx=$2
1099
1100         out=$($IP ro ls match ${pfx})
1101         [ "${out}" = "${expected}" ] && return 0
1102
1103         if [ -z "${out}" ]; then
1104                 if [ "$VERBOSE" = "1" ]; then
1105                         printf "\nNo route entry found\n"
1106                         printf "Expected:\n"
1107                         printf "    ${expected}\n"
1108                 fi
1109                 return 1
1110         fi
1111
1112         # tricky way to convert output to 1-line without ip's
1113         # messy '\'; this drops all extra white space
1114         out=$(echo ${out})
1115         if [ "${out}" != "${expected}" ]; then
1116                 rc=1
1117                 if [ "${VERBOSE}" = "1" ]; then
1118                         printf "    Unexpected route entry. Have:\n"
1119                         printf "        ${out}\n"
1120                         printf "    Expected:\n"
1121                         printf "        ${expected}\n\n"
1122                 fi
1123         fi
1124
1125         return $rc
1126 }
1127
1128 # assumption is that basic add of a single path route works
1129 # otherwise just adding an address on an interface is broken
1130 ipv4_rt_add()
1131 {
1132         local rc
1133
1134         echo
1135         echo "IPv4 route add / append tests"
1136
1137         # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1138         add_route "172.16.104.0/24" "via 172.16.101.2"
1139         run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.2"
1140         log_test $? 2 "Attempt to add duplicate route - gw"
1141
1142         # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1143         add_route "172.16.104.0/24" "via 172.16.101.2"
1144         run_cmd "$IP ro add 172.16.104.0/24 dev veth3"
1145         log_test $? 2 "Attempt to add duplicate route - dev only"
1146
1147         # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1148         add_route "172.16.104.0/24" "via 172.16.101.2"
1149         run_cmd "$IP ro add unreachable 172.16.104.0/24"
1150         log_test $? 2 "Attempt to add duplicate route - reject route"
1151
1152         # iproute2 prepend only sets NLM_F_CREATE
1153         # - adds a new route; does NOT convert existing route to ECMP
1154         add_route "172.16.104.0/24" "via 172.16.101.2"
1155         run_cmd "$IP ro prepend 172.16.104.0/24 via 172.16.103.2"
1156         check_route "172.16.104.0/24 via 172.16.103.2 dev veth3 172.16.104.0/24 via 172.16.101.2 dev veth1"
1157         log_test $? 0 "Add new nexthop for existing prefix"
1158
1159         # route append with same prefix adds a new route
1160         # - iproute2 sets NLM_F_CREATE | NLM_F_APPEND
1161         add_route "172.16.104.0/24" "via 172.16.101.2"
1162         run_cmd "$IP ro append 172.16.104.0/24 via 172.16.103.2"
1163         check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 via 172.16.103.2 dev veth3"
1164         log_test $? 0 "Append nexthop to existing route - gw"
1165
1166         add_route "172.16.104.0/24" "via 172.16.101.2"
1167         run_cmd "$IP ro append 172.16.104.0/24 dev veth3"
1168         check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 dev veth3 scope link"
1169         log_test $? 0 "Append nexthop to existing route - dev only"
1170
1171         add_route "172.16.104.0/24" "via 172.16.101.2"
1172         run_cmd "$IP ro append unreachable 172.16.104.0/24"
1173         check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 unreachable 172.16.104.0/24"
1174         log_test $? 0 "Append nexthop to existing route - reject route"
1175
1176         run_cmd "$IP ro flush 172.16.104.0/24"
1177         run_cmd "$IP ro add unreachable 172.16.104.0/24"
1178         run_cmd "$IP ro append 172.16.104.0/24 via 172.16.103.2"
1179         check_route "unreachable 172.16.104.0/24 172.16.104.0/24 via 172.16.103.2 dev veth3"
1180         log_test $? 0 "Append nexthop to existing reject route - gw"
1181
1182         run_cmd "$IP ro flush 172.16.104.0/24"
1183         run_cmd "$IP ro add unreachable 172.16.104.0/24"
1184         run_cmd "$IP ro append 172.16.104.0/24 dev veth3"
1185         check_route "unreachable 172.16.104.0/24 172.16.104.0/24 dev veth3 scope link"
1186         log_test $? 0 "Append nexthop to existing reject route - dev only"
1187
1188         # insert mpath directly
1189         add_route "172.16.104.0/24" "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1190         check_route  "172.16.104.0/24 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
1191         log_test $? 0 "add multipath route"
1192
1193         add_route "172.16.104.0/24" "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1194         run_cmd "$IP ro add 172.16.104.0/24 nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1195         log_test $? 2 "Attempt to add duplicate multipath route"
1196
1197         # insert of a second route without append but different metric
1198         add_route "172.16.104.0/24" "via 172.16.101.2"
1199         run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.2 metric 512"
1200         rc=$?
1201         if [ $rc -eq 0 ]; then
1202                 run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.3 metric 256"
1203                 rc=$?
1204         fi
1205         log_test $rc 0 "Route add with different metrics"
1206
1207         run_cmd "$IP ro del 172.16.104.0/24 metric 512"
1208         rc=$?
1209         if [ $rc -eq 0 ]; then
1210                 check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 via 172.16.103.3 dev veth3 metric 256"
1211                 rc=$?
1212         fi
1213         log_test $rc 0 "Route delete with metric"
1214 }
1215
1216 ipv4_rt_replace_single()
1217 {
1218         # single path with single path
1219         #
1220         add_initial_route "via 172.16.101.2"
1221         run_cmd "$IP ro replace 172.16.104.0/24 via 172.16.103.2"
1222         check_route "172.16.104.0/24 via 172.16.103.2 dev veth3"
1223         log_test $? 0 "Single path with single path"
1224
1225         # single path with multipath
1226         #
1227         add_initial_route "nexthop via 172.16.101.2"
1228         run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.2"
1229         check_route "172.16.104.0/24 nexthop via 172.16.101.3 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
1230         log_test $? 0 "Single path with multipath"
1231
1232         # single path with reject
1233         #
1234         add_initial_route "nexthop via 172.16.101.2"
1235         run_cmd "$IP ro replace unreachable 172.16.104.0/24"
1236         check_route "unreachable 172.16.104.0/24"
1237         log_test $? 0 "Single path with reject route"
1238
1239         # single path with single path using MULTIPATH attribute
1240         #
1241         add_initial_route "via 172.16.101.2"
1242         run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.103.2"
1243         check_route "172.16.104.0/24 via 172.16.103.2 dev veth3"
1244         log_test $? 0 "Single path with single path via multipath attribute"
1245
1246         # route replace fails - invalid nexthop
1247         add_initial_route "via 172.16.101.2"
1248         run_cmd "$IP ro replace 172.16.104.0/24 via 2001:db8:104::2"
1249         if [ $? -eq 0 ]; then
1250                 # previous command is expected to fail so if it returns 0
1251                 # that means the test failed.
1252                 log_test 0 1 "Invalid nexthop"
1253         else
1254                 check_route "172.16.104.0/24 via 172.16.101.2 dev veth1"
1255                 log_test $? 0 "Invalid nexthop"
1256         fi
1257
1258         # replace non-existent route
1259         # - note use of change versus replace since ip adds NLM_F_CREATE
1260         #   for replace
1261         add_initial_route "via 172.16.101.2"
1262         run_cmd "$IP ro change 172.16.105.0/24 via 172.16.101.2"
1263         log_test $? 2 "Single path - replace of non-existent route"
1264 }
1265
1266 ipv4_rt_replace_mpath()
1267 {
1268         # multipath with multipath
1269         add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1270         run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.3"
1271         check_route  "172.16.104.0/24 nexthop via 172.16.101.3 dev veth1 weight 1 nexthop via 172.16.103.3 dev veth3 weight 1"
1272         log_test $? 0 "Multipath with multipath"
1273
1274         # multipath with single
1275         add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1276         run_cmd "$IP ro replace 172.16.104.0/24 via 172.16.101.3"
1277         check_route  "172.16.104.0/24 via 172.16.101.3 dev veth1"
1278         log_test $? 0 "Multipath with single path"
1279
1280         # multipath with single
1281         add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1282         run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3"
1283         check_route "172.16.104.0/24 via 172.16.101.3 dev veth1"
1284         log_test $? 0 "Multipath with single path via multipath attribute"
1285
1286         # multipath with reject
1287         add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1288         run_cmd "$IP ro replace unreachable 172.16.104.0/24"
1289         check_route "unreachable 172.16.104.0/24"
1290         log_test $? 0 "Multipath with reject route"
1291
1292         # route replace fails - invalid nexthop 1
1293         add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1294         run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.111.3 nexthop via 172.16.103.3"
1295         check_route  "172.16.104.0/24 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
1296         log_test $? 0 "Multipath - invalid first nexthop"
1297
1298         # route replace fails - invalid nexthop 2
1299         add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1300         run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.113.3"
1301         check_route  "172.16.104.0/24 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
1302         log_test $? 0 "Multipath - invalid second nexthop"
1303
1304         # multipath non-existent route
1305         add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1306         run_cmd "$IP ro change 172.16.105.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.3"
1307         log_test $? 2 "Multipath - replace of non-existent route"
1308 }
1309
1310 ipv4_rt_replace()
1311 {
1312         echo
1313         echo "IPv4 route replace tests"
1314
1315         ipv4_rt_replace_single
1316         ipv4_rt_replace_mpath
1317 }
1318
1319 ipv4_route_test()
1320 {
1321         route_setup
1322
1323         ipv4_rt_add
1324         ipv4_rt_replace
1325
1326         route_cleanup
1327 }
1328
1329 ipv4_addr_metric_test()
1330 {
1331         local rc
1332
1333         echo
1334         echo "IPv4 prefix route tests"
1335
1336         ip_addr_metric_check || return 1
1337
1338         setup
1339
1340         set -e
1341         $IP li add dummy1 type dummy
1342         $IP li add dummy2 type dummy
1343         $IP li set dummy1 up
1344         $IP li set dummy2 up
1345
1346         # default entry is metric 256
1347         run_cmd "$IP addr add dev dummy1 172.16.104.1/24"
1348         run_cmd "$IP addr add dev dummy2 172.16.104.2/24"
1349         set +e
1350
1351         check_route "172.16.104.0/24 dev dummy1 proto kernel scope link src 172.16.104.1 172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2"
1352         log_test $? 0 "Default metric"
1353
1354         set -e
1355         run_cmd "$IP addr flush dev dummy1"
1356         run_cmd "$IP addr add dev dummy1 172.16.104.1/24 metric 257"
1357         set +e
1358
1359         check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 172.16.104.0/24 dev dummy1 proto kernel scope link src 172.16.104.1 metric 257"
1360         log_test $? 0 "User specified metric on first device"
1361
1362         set -e
1363         run_cmd "$IP addr flush dev dummy2"
1364         run_cmd "$IP addr add dev dummy2 172.16.104.2/24 metric 258"
1365         set +e
1366
1367         check_route "172.16.104.0/24 dev dummy1 proto kernel scope link src 172.16.104.1 metric 257 172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 258"
1368         log_test $? 0 "User specified metric on second device"
1369
1370         run_cmd "$IP addr del dev dummy1 172.16.104.1/24 metric 257"
1371         rc=$?
1372         if [ $rc -eq 0 ]; then
1373                 check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 258"
1374                 rc=$?
1375         fi
1376         log_test $rc 0 "Delete of address on first device"
1377
1378         run_cmd "$IP addr change dev dummy2 172.16.104.2/24 metric 259"
1379         rc=$?
1380         if [ $rc -eq 0 ]; then
1381                 check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 259"
1382                 rc=$?
1383         fi
1384         log_test $rc 0 "Modify metric of address"
1385
1386         # verify prefix route removed on down
1387         run_cmd "$IP li set dev dummy2 down"
1388         rc=$?
1389         if [ $rc -eq 0 ]; then
1390                 check_route ""
1391                 rc=$?
1392         fi
1393         log_test $rc 0 "Prefix route removed on link down"
1394
1395         # verify prefix route re-inserted with assigned metric
1396         run_cmd "$IP li set dev dummy2 up"
1397         rc=$?
1398         if [ $rc -eq 0 ]; then
1399                 check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 259"
1400                 rc=$?
1401         fi
1402         log_test $rc 0 "Prefix route with metric on link up"
1403
1404         $IP li del dummy1
1405         $IP li del dummy2
1406         cleanup
1407 }
1408
1409 ipv4_route_metrics_test()
1410 {
1411         local rc
1412
1413         echo
1414         echo "IPv4 route add / append tests"
1415
1416         route_setup
1417
1418         run_cmd "$IP ro add 172.16.111.0/24 via 172.16.101.2 mtu 1400"
1419         rc=$?
1420         if [ $rc -eq 0 ]; then
1421                 check_route "172.16.111.0/24 via 172.16.101.2 dev veth1 mtu 1400"
1422                 rc=$?
1423         fi
1424         log_test $rc 0 "Single path route with mtu metric"
1425
1426
1427         run_cmd "$IP ro add 172.16.112.0/24 mtu 1400 nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1428         rc=$?
1429         if [ $rc -eq 0 ]; then
1430                 check_route "172.16.112.0/24 mtu 1400 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
1431                 rc=$?
1432         fi
1433         log_test $rc 0 "Multipath route with mtu metric"
1434
1435         $IP ro add 172.16.104.0/24 via 172.16.101.2 mtu 1300
1436         run_cmd "ip netns exec ns1 ping -w1 -c1 -s 1500 172.16.104.1"
1437         log_test $? 0 "Using route with mtu metric"
1438
1439         run_cmd "$IP ro add 172.16.111.0/24 via 172.16.101.2 congctl lock foo"
1440         log_test $? 2 "Invalid metric (fails metric_convert)"
1441
1442         route_cleanup
1443 }
1444
1445
1446 ################################################################################
1447 # usage
1448
1449 usage()
1450 {
1451         cat <<EOF
1452 usage: ${0##*/} OPTS
1453
1454         -t <test>   Test(s) to run (default: all)
1455                     (options: $TESTS)
1456         -p          Pause on fail
1457         -P          Pause after each test before cleanup
1458         -v          verbose mode (show commands and output)
1459 EOF
1460 }
1461
1462 ################################################################################
1463 # main
1464
1465 while getopts :t:pPhv o
1466 do
1467         case $o in
1468                 t) TESTS=$OPTARG;;
1469                 p) PAUSE_ON_FAIL=yes;;
1470                 P) PAUSE=yes;;
1471                 v) VERBOSE=$(($VERBOSE + 1));;
1472                 h) usage; exit 0;;
1473                 *) usage; exit 1;;
1474         esac
1475 done
1476
1477 PEER_CMD="ip netns exec ${PEER_NS}"
1478
1479 # make sure we don't pause twice
1480 [ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
1481
1482 if [ "$(id -u)" -ne 0 ];then
1483         echo "SKIP: Need root privileges"
1484         exit $ksft_skip;
1485 fi
1486
1487 if [ ! -x "$(command -v ip)" ]; then
1488         echo "SKIP: Could not run test without ip tool"
1489         exit $ksft_skip
1490 fi
1491
1492 ip route help 2>&1 | grep -q fibmatch
1493 if [ $? -ne 0 ]; then
1494         echo "SKIP: iproute2 too old, missing fibmatch"
1495         exit $ksft_skip
1496 fi
1497
1498 # start clean
1499 cleanup &> /dev/null
1500
1501 for t in $TESTS
1502 do
1503         case $t in
1504         fib_unreg_test|unregister)      fib_unreg_test;;
1505         fib_down_test|down)             fib_down_test;;
1506         fib_carrier_test|carrier)       fib_carrier_test;;
1507         fib_nexthop_test|nexthop)       fib_nexthop_test;;
1508         ipv6_route_test|ipv6_rt)        ipv6_route_test;;
1509         ipv4_route_test|ipv4_rt)        ipv4_route_test;;
1510         ipv6_addr_metric)               ipv6_addr_metric_test;;
1511         ipv4_addr_metric)               ipv4_addr_metric_test;;
1512         ipv6_route_metrics)             ipv6_route_metrics_test;;
1513         ipv4_route_metrics)             ipv4_route_metrics_test;;
1514
1515         help) echo "Test names: $TESTS"; exit 0;;
1516         esac
1517 done
1518
1519 if [ "$TESTS" != "none" ]; then
1520         printf "\nTests passed: %3d\n" ${nsuccess}
1521         printf "Tests failed: %3d\n"   ${nfail}
1522 fi
1523
1524 exit $ret