xref: /openbmc/linux/tools/testing/selftests/net/fib_tests.sh (revision 8f8d5745bb520c76b81abef4a2cb3023d0313bfd)
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
7ret=0
8# Kselftest framework requirement - SKIP code is 4.
9ksft_skip=4
10
11# all tests in this script. Can be overridden with -t option
12TESTS="unregister down carrier nexthop ipv6_rt ipv4_rt ipv6_addr_metric ipv4_addr_metric ipv6_route_metrics ipv4_route_metrics"
13VERBOSE=0
14PAUSE_ON_FAIL=no
15PAUSE=no
16IP="ip -netns ns1"
17
18log_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
47setup()
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
63cleanup()
64{
65	$IP link del dev dummy0 &> /dev/null
66	ip netns del ns1
67	ip netns del ns2 &> /dev/null
68}
69
70get_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
92fib_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
118fib_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
169fib_unreg_test()
170{
171	fib_unreg_unicast_test
172	fib_unreg_multipath_test
173}
174
175fib_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
201fib_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
235fib_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
293fib_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.
300fib_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
363fib_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
428fib_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
438add_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
465fib4_nexthop()
466{
467	echo
468	echo "IPv4 nexthop tests"
469
470	echo "<<< write me >>>"
471}
472
473fib6_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
540fib_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
586run_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
608check_expected()
609{
610	local out="$1"
611	local expected="$2"
612	local rc=0
613
614	[ "${out}" = "${expected}" ] && return 0
615
616	if [ -z "${out}" ]; then
617		if [ "$VERBOSE" = "1" ]; then
618			printf "\nNo route entry found\n"
619			printf "Expected:\n"
620			printf "    ${expected}\n"
621		fi
622		return 1
623	fi
624
625	# tricky way to convert output to 1-line without ip's
626	# messy '\'; this drops all extra white space
627	out=$(echo ${out})
628	if [ "${out}" != "${expected}" ]; then
629		rc=1
630		if [ "${VERBOSE}" = "1" ]; then
631			printf "    Unexpected route entry. Have:\n"
632			printf "        ${out}\n"
633			printf "    Expected:\n"
634			printf "        ${expected}\n\n"
635		fi
636	fi
637
638	return $rc
639}
640
641# add route for a prefix, flushing any existing routes first
642# expected to be the first step of a test
643add_route6()
644{
645	local pfx="$1"
646	local nh="$2"
647	local out
648
649	if [ "$VERBOSE" = "1" ]; then
650		echo
651		echo "    ##################################################"
652		echo
653	fi
654
655	run_cmd "$IP -6 ro flush ${pfx}"
656	[ $? -ne 0 ] && exit 1
657
658	out=$($IP -6 ro ls match ${pfx})
659	if [ -n "$out" ]; then
660		echo "Failed to flush routes for prefix used for tests."
661		exit 1
662	fi
663
664	run_cmd "$IP -6 ro add ${pfx} ${nh}"
665	if [ $? -ne 0 ]; then
666		echo "Failed to add initial route for test."
667		exit 1
668	fi
669}
670
671# add initial route - used in replace route tests
672add_initial_route6()
673{
674	add_route6 "2001:db8:104::/64" "$1"
675}
676
677check_route6()
678{
679	local pfx
680	local expected="$1"
681	local out
682	local rc=0
683
684	set -- $expected
685	pfx=$1
686
687	out=$($IP -6 ro ls match ${pfx} | sed -e 's/ pref medium//')
688	check_expected "${out}" "${expected}"
689}
690
691route_cleanup()
692{
693	$IP li del red 2>/dev/null
694	$IP li del dummy1 2>/dev/null
695	$IP li del veth1 2>/dev/null
696	$IP li del veth3 2>/dev/null
697
698	cleanup &> /dev/null
699}
700
701route_setup()
702{
703	route_cleanup
704	setup
705
706	[ "${VERBOSE}" = "1" ] && set -x
707	set -e
708
709	ip netns add ns2
710	ip -netns ns2 link set dev lo up
711	ip netns exec ns2 sysctl -qw net.ipv4.ip_forward=1
712	ip netns exec ns2 sysctl -qw net.ipv6.conf.all.forwarding=1
713
714	$IP li add veth1 type veth peer name veth2
715	$IP li add veth3 type veth peer name veth4
716
717	$IP li set veth1 up
718	$IP li set veth3 up
719	$IP li set veth2 netns ns2 up
720	$IP li set veth4 netns ns2 up
721	ip -netns ns2 li add dummy1 type dummy
722	ip -netns ns2 li set dummy1 up
723
724	$IP -6 addr add 2001:db8:101::1/64 dev veth1 nodad
725	$IP -6 addr add 2001:db8:103::1/64 dev veth3 nodad
726	$IP addr add 172.16.101.1/24 dev veth1
727	$IP addr add 172.16.103.1/24 dev veth3
728
729	ip -netns ns2 -6 addr add 2001:db8:101::2/64 dev veth2 nodad
730	ip -netns ns2 -6 addr add 2001:db8:103::2/64 dev veth4 nodad
731	ip -netns ns2 -6 addr add 2001:db8:104::1/64 dev dummy1 nodad
732
733	ip -netns ns2 addr add 172.16.101.2/24 dev veth2
734	ip -netns ns2 addr add 172.16.103.2/24 dev veth4
735	ip -netns ns2 addr add 172.16.104.1/24 dev dummy1
736
737	set +e
738}
739
740# assumption is that basic add of a single path route works
741# otherwise just adding an address on an interface is broken
742ipv6_rt_add()
743{
744	local rc
745
746	echo
747	echo "IPv6 route add / append tests"
748
749	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
750	add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
751	run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::2"
752	log_test $? 2 "Attempt to add duplicate route - gw"
753
754	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
755	add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
756	run_cmd "$IP -6 ro add 2001:db8:104::/64 dev veth3"
757	log_test $? 2 "Attempt to add duplicate route - dev only"
758
759	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
760	add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
761	run_cmd "$IP -6 ro add unreachable 2001:db8:104::/64"
762	log_test $? 2 "Attempt to add duplicate route - reject route"
763
764	# route append with same prefix adds a new route
765	# - iproute2 sets NLM_F_CREATE | NLM_F_APPEND
766	add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
767	run_cmd "$IP -6 ro append 2001:db8:104::/64 via 2001:db8:103::2"
768	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"
769	log_test $? 0 "Append nexthop to existing route - gw"
770
771	# insert mpath directly
772	add_route6 "2001:db8:104::/64" "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
773	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"
774	log_test $? 0 "Add multipath route"
775
776	add_route6 "2001:db8:104::/64" "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
777	run_cmd "$IP -6 ro add 2001:db8:104::/64 nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
778	log_test $? 2 "Attempt to add duplicate multipath route"
779
780	# insert of a second route without append but different metric
781	add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
782	run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::2 metric 512"
783	rc=$?
784	if [ $rc -eq 0 ]; then
785		run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::3 metric 256"
786		rc=$?
787	fi
788	log_test $rc 0 "Route add with different metrics"
789
790	run_cmd "$IP -6 ro del 2001:db8:104::/64 metric 512"
791	rc=$?
792	if [ $rc -eq 0 ]; then
793		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"
794		rc=$?
795	fi
796	log_test $rc 0 "Route delete with metric"
797}
798
799ipv6_rt_replace_single()
800{
801	# single path with single path
802	#
803	add_initial_route6 "via 2001:db8:101::2"
804	run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:103::2"
805	check_route6 "2001:db8:104::/64 via 2001:db8:103::2 dev veth3 metric 1024"
806	log_test $? 0 "Single path with single path"
807
808	# single path with multipath
809	#
810	add_initial_route6 "nexthop via 2001:db8:101::2"
811	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::2"
812	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"
813	log_test $? 0 "Single path with multipath"
814
815	# single path with single path using MULTIPATH attribute
816	#
817	add_initial_route6 "via 2001:db8:101::2"
818	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:103::2"
819	check_route6 "2001:db8:104::/64 via 2001:db8:103::2 dev veth3 metric 1024"
820	log_test $? 0 "Single path with single path via multipath attribute"
821
822	# route replace fails - invalid nexthop
823	add_initial_route6 "via 2001:db8:101::2"
824	run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:104::2"
825	if [ $? -eq 0 ]; then
826		# previous command is expected to fail so if it returns 0
827		# that means the test failed.
828		log_test 0 1 "Invalid nexthop"
829	else
830		check_route6 "2001:db8:104::/64 via 2001:db8:101::2 dev veth1 metric 1024"
831		log_test $? 0 "Invalid nexthop"
832	fi
833
834	# replace non-existent route
835	# - note use of change versus replace since ip adds NLM_F_CREATE
836	#   for replace
837	add_initial_route6 "via 2001:db8:101::2"
838	run_cmd "$IP -6 ro change 2001:db8:105::/64 via 2001:db8:101::2"
839	log_test $? 2 "Single path - replace of non-existent route"
840}
841
842ipv6_rt_replace_mpath()
843{
844	# multipath with multipath
845	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
846	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::3"
847	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"
848	log_test $? 0 "Multipath with multipath"
849
850	# multipath with single
851	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
852	run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:101::3"
853	check_route6  "2001:db8:104::/64 via 2001:db8:101::3 dev veth1 metric 1024"
854	log_test $? 0 "Multipath with single path"
855
856	# multipath with single
857	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
858	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3"
859	check_route6 "2001:db8:104::/64 via 2001:db8:101::3 dev veth1 metric 1024"
860	log_test $? 0 "Multipath with single path via multipath attribute"
861
862	# route replace fails - invalid nexthop 1
863	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
864	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:111::3 nexthop via 2001:db8:103::3"
865	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"
866	log_test $? 0 "Multipath - invalid first nexthop"
867
868	# route replace fails - invalid nexthop 2
869	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
870	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:113::3"
871	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"
872	log_test $? 0 "Multipath - invalid second nexthop"
873
874	# multipath non-existent route
875	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
876	run_cmd "$IP -6 ro change 2001:db8:105::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::3"
877	log_test $? 2 "Multipath - replace of non-existent route"
878}
879
880ipv6_rt_replace()
881{
882	echo
883	echo "IPv6 route replace tests"
884
885	ipv6_rt_replace_single
886	ipv6_rt_replace_mpath
887}
888
889ipv6_route_test()
890{
891	route_setup
892
893	ipv6_rt_add
894	ipv6_rt_replace
895
896	route_cleanup
897}
898
899ip_addr_metric_check()
900{
901	ip addr help 2>&1 | grep -q metric
902	if [ $? -ne 0 ]; then
903		echo "iproute2 command does not support metric for addresses. Skipping test"
904		return 1
905	fi
906
907	return 0
908}
909
910ipv6_addr_metric_test()
911{
912	local rc
913
914	echo
915	echo "IPv6 prefix route tests"
916
917	ip_addr_metric_check || return 1
918
919	setup
920
921	set -e
922	$IP li add dummy1 type dummy
923	$IP li add dummy2 type dummy
924	$IP li set dummy1 up
925	$IP li set dummy2 up
926
927	# default entry is metric 256
928	run_cmd "$IP -6 addr add dev dummy1 2001:db8:104::1/64"
929	run_cmd "$IP -6 addr add dev dummy2 2001:db8:104::2/64"
930	set +e
931
932	check_route6 "2001:db8:104::/64 dev dummy1 proto kernel metric 256 2001:db8:104::/64 dev dummy2 proto kernel metric 256"
933	log_test $? 0 "Default metric"
934
935	set -e
936	run_cmd "$IP -6 addr flush dev dummy1"
937	run_cmd "$IP -6 addr add dev dummy1 2001:db8:104::1/64 metric 257"
938	set +e
939
940	check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 256 2001:db8:104::/64 dev dummy1 proto kernel metric 257"
941	log_test $? 0 "User specified metric on first device"
942
943	set -e
944	run_cmd "$IP -6 addr flush dev dummy2"
945	run_cmd "$IP -6 addr add dev dummy2 2001:db8:104::2/64 metric 258"
946	set +e
947
948	check_route6 "2001:db8:104::/64 dev dummy1 proto kernel metric 257 2001:db8:104::/64 dev dummy2 proto kernel metric 258"
949	log_test $? 0 "User specified metric on second device"
950
951	run_cmd "$IP -6 addr del dev dummy1 2001:db8:104::1/64 metric 257"
952	rc=$?
953	if [ $rc -eq 0 ]; then
954		check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 258"
955		rc=$?
956	fi
957	log_test $rc 0 "Delete of address on first device"
958
959	run_cmd "$IP -6 addr change dev dummy2 2001:db8:104::2/64 metric 259"
960	rc=$?
961	if [ $rc -eq 0 ]; then
962		check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 259"
963		rc=$?
964	fi
965	log_test $rc 0 "Modify metric of address"
966
967	# verify prefix route removed on down
968	run_cmd "ip netns exec ns1 sysctl -qw net.ipv6.conf.all.keep_addr_on_down=1"
969	run_cmd "$IP li set dev dummy2 down"
970	rc=$?
971	if [ $rc -eq 0 ]; then
972		out=$($IP -6 ro ls match 2001:db8:104::/64)
973		check_expected "${out}" ""
974		rc=$?
975	fi
976	log_test $rc 0 "Prefix route removed on link down"
977
978	# verify prefix route re-inserted with assigned metric
979	run_cmd "$IP li set dev dummy2 up"
980	rc=$?
981	if [ $rc -eq 0 ]; then
982		check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 259"
983		rc=$?
984	fi
985	log_test $rc 0 "Prefix route with metric on link up"
986
987	$IP li del dummy1
988	$IP li del dummy2
989	cleanup
990}
991
992ipv6_route_metrics_test()
993{
994	local rc
995
996	echo
997	echo "IPv6 routes with metrics"
998
999	route_setup
1000
1001	#
1002	# single path with metrics
1003	#
1004	run_cmd "$IP -6 ro add 2001:db8:111::/64 via 2001:db8:101::2 mtu 1400"
1005	rc=$?
1006	if [ $rc -eq 0 ]; then
1007		check_route6  "2001:db8:111::/64 via 2001:db8:101::2 dev veth1 metric 1024 mtu 1400"
1008		rc=$?
1009	fi
1010	log_test $rc 0 "Single path route with mtu metric"
1011
1012
1013	#
1014	# multipath via separate routes with metrics
1015	#
1016	run_cmd "$IP -6 ro add 2001:db8:112::/64 via 2001:db8:101::2 mtu 1400"
1017	run_cmd "$IP -6 ro append 2001:db8:112::/64 via 2001:db8:103::2"
1018	rc=$?
1019	if [ $rc -eq 0 ]; then
1020		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"
1021		rc=$?
1022	fi
1023	log_test $rc 0 "Multipath route via 2 single routes with mtu metric on first"
1024
1025	# second route is coalesced to first to make a multipath route.
1026	# MTU of the second path is hidden from display!
1027	run_cmd "$IP -6 ro add 2001:db8:113::/64 via 2001:db8:101::2"
1028	run_cmd "$IP -6 ro append 2001:db8:113::/64 via 2001:db8:103::2 mtu 1400"
1029	rc=$?
1030	if [ $rc -eq 0 ]; then
1031		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"
1032		rc=$?
1033	fi
1034	log_test $rc 0 "Multipath route via 2 single routes with mtu metric on 2nd"
1035
1036	run_cmd "$IP -6 ro del 2001:db8:113::/64 via 2001:db8:101::2"
1037	if [ $? -eq 0 ]; then
1038		check_route6 "2001:db8:113::/64 via 2001:db8:103::2 dev veth3 metric 1024 mtu 1400"
1039		log_test $? 0 "    MTU of second leg"
1040	fi
1041
1042	#
1043	# multipath with metrics
1044	#
1045	run_cmd "$IP -6 ro add 2001:db8:115::/64 mtu 1400 nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1046	rc=$?
1047	if [ $rc -eq 0 ]; then
1048		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"
1049		rc=$?
1050	fi
1051	log_test $rc 0 "Multipath route with mtu metric"
1052
1053	$IP -6 ro add 2001:db8:104::/64 via 2001:db8:101::2 mtu 1300
1054	run_cmd "ip netns exec ns1 ping6 -w1 -c1 -s 1500 2001:db8:104::1"
1055	log_test $? 0 "Using route with mtu metric"
1056
1057	run_cmd "$IP -6 ro add 2001:db8:114::/64 via  2001:db8:101::2  congctl lock foo"
1058	log_test $? 2 "Invalid metric (fails metric_convert)"
1059
1060	route_cleanup
1061}
1062
1063# add route for a prefix, flushing any existing routes first
1064# expected to be the first step of a test
1065add_route()
1066{
1067	local pfx="$1"
1068	local nh="$2"
1069	local out
1070
1071	if [ "$VERBOSE" = "1" ]; then
1072		echo
1073		echo "    ##################################################"
1074		echo
1075	fi
1076
1077	run_cmd "$IP ro flush ${pfx}"
1078	[ $? -ne 0 ] && exit 1
1079
1080	out=$($IP ro ls match ${pfx})
1081	if [ -n "$out" ]; then
1082		echo "Failed to flush routes for prefix used for tests."
1083		exit 1
1084	fi
1085
1086	run_cmd "$IP ro add ${pfx} ${nh}"
1087	if [ $? -ne 0 ]; then
1088		echo "Failed to add initial route for test."
1089		exit 1
1090	fi
1091}
1092
1093# add initial route - used in replace route tests
1094add_initial_route()
1095{
1096	add_route "172.16.104.0/24" "$1"
1097}
1098
1099check_route()
1100{
1101	local pfx
1102	local expected="$1"
1103	local out
1104
1105	set -- $expected
1106	pfx=$1
1107	[ "${pfx}" = "unreachable" ] && pfx=$2
1108
1109	out=$($IP ro ls match ${pfx})
1110	check_expected "${out}" "${expected}"
1111}
1112
1113# assumption is that basic add of a single path route works
1114# otherwise just adding an address on an interface is broken
1115ipv4_rt_add()
1116{
1117	local rc
1118
1119	echo
1120	echo "IPv4 route add / append tests"
1121
1122	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1123	add_route "172.16.104.0/24" "via 172.16.101.2"
1124	run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.2"
1125	log_test $? 2 "Attempt to add duplicate route - gw"
1126
1127	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1128	add_route "172.16.104.0/24" "via 172.16.101.2"
1129	run_cmd "$IP ro add 172.16.104.0/24 dev veth3"
1130	log_test $? 2 "Attempt to add duplicate route - dev only"
1131
1132	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1133	add_route "172.16.104.0/24" "via 172.16.101.2"
1134	run_cmd "$IP ro add unreachable 172.16.104.0/24"
1135	log_test $? 2 "Attempt to add duplicate route - reject route"
1136
1137	# iproute2 prepend only sets NLM_F_CREATE
1138	# - adds a new route; does NOT convert existing route to ECMP
1139	add_route "172.16.104.0/24" "via 172.16.101.2"
1140	run_cmd "$IP ro prepend 172.16.104.0/24 via 172.16.103.2"
1141	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"
1142	log_test $? 0 "Add new nexthop for existing prefix"
1143
1144	# route append with same prefix adds a new route
1145	# - iproute2 sets NLM_F_CREATE | NLM_F_APPEND
1146	add_route "172.16.104.0/24" "via 172.16.101.2"
1147	run_cmd "$IP ro append 172.16.104.0/24 via 172.16.103.2"
1148	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"
1149	log_test $? 0 "Append nexthop to existing route - gw"
1150
1151	add_route "172.16.104.0/24" "via 172.16.101.2"
1152	run_cmd "$IP ro append 172.16.104.0/24 dev veth3"
1153	check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 dev veth3 scope link"
1154	log_test $? 0 "Append nexthop to existing route - dev only"
1155
1156	add_route "172.16.104.0/24" "via 172.16.101.2"
1157	run_cmd "$IP ro append unreachable 172.16.104.0/24"
1158	check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 unreachable 172.16.104.0/24"
1159	log_test $? 0 "Append nexthop to existing route - reject route"
1160
1161	run_cmd "$IP ro flush 172.16.104.0/24"
1162	run_cmd "$IP ro add unreachable 172.16.104.0/24"
1163	run_cmd "$IP ro append 172.16.104.0/24 via 172.16.103.2"
1164	check_route "unreachable 172.16.104.0/24 172.16.104.0/24 via 172.16.103.2 dev veth3"
1165	log_test $? 0 "Append nexthop to existing reject route - gw"
1166
1167	run_cmd "$IP ro flush 172.16.104.0/24"
1168	run_cmd "$IP ro add unreachable 172.16.104.0/24"
1169	run_cmd "$IP ro append 172.16.104.0/24 dev veth3"
1170	check_route "unreachable 172.16.104.0/24 172.16.104.0/24 dev veth3 scope link"
1171	log_test $? 0 "Append nexthop to existing reject route - dev only"
1172
1173	# insert mpath directly
1174	add_route "172.16.104.0/24" "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1175	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"
1176	log_test $? 0 "add multipath route"
1177
1178	add_route "172.16.104.0/24" "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1179	run_cmd "$IP ro add 172.16.104.0/24 nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1180	log_test $? 2 "Attempt to add duplicate multipath route"
1181
1182	# insert of a second route without append but different metric
1183	add_route "172.16.104.0/24" "via 172.16.101.2"
1184	run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.2 metric 512"
1185	rc=$?
1186	if [ $rc -eq 0 ]; then
1187		run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.3 metric 256"
1188		rc=$?
1189	fi
1190	log_test $rc 0 "Route add with different metrics"
1191
1192	run_cmd "$IP ro del 172.16.104.0/24 metric 512"
1193	rc=$?
1194	if [ $rc -eq 0 ]; then
1195		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"
1196		rc=$?
1197	fi
1198	log_test $rc 0 "Route delete with metric"
1199}
1200
1201ipv4_rt_replace_single()
1202{
1203	# single path with single path
1204	#
1205	add_initial_route "via 172.16.101.2"
1206	run_cmd "$IP ro replace 172.16.104.0/24 via 172.16.103.2"
1207	check_route "172.16.104.0/24 via 172.16.103.2 dev veth3"
1208	log_test $? 0 "Single path with single path"
1209
1210	# single path with multipath
1211	#
1212	add_initial_route "nexthop via 172.16.101.2"
1213	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.2"
1214	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"
1215	log_test $? 0 "Single path with multipath"
1216
1217	# single path with reject
1218	#
1219	add_initial_route "nexthop via 172.16.101.2"
1220	run_cmd "$IP ro replace unreachable 172.16.104.0/24"
1221	check_route "unreachable 172.16.104.0/24"
1222	log_test $? 0 "Single path with reject route"
1223
1224	# single path with single path using MULTIPATH attribute
1225	#
1226	add_initial_route "via 172.16.101.2"
1227	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.103.2"
1228	check_route "172.16.104.0/24 via 172.16.103.2 dev veth3"
1229	log_test $? 0 "Single path with single path via multipath attribute"
1230
1231	# route replace fails - invalid nexthop
1232	add_initial_route "via 172.16.101.2"
1233	run_cmd "$IP ro replace 172.16.104.0/24 via 2001:db8:104::2"
1234	if [ $? -eq 0 ]; then
1235		# previous command is expected to fail so if it returns 0
1236		# that means the test failed.
1237		log_test 0 1 "Invalid nexthop"
1238	else
1239		check_route "172.16.104.0/24 via 172.16.101.2 dev veth1"
1240		log_test $? 0 "Invalid nexthop"
1241	fi
1242
1243	# replace non-existent route
1244	# - note use of change versus replace since ip adds NLM_F_CREATE
1245	#   for replace
1246	add_initial_route "via 172.16.101.2"
1247	run_cmd "$IP ro change 172.16.105.0/24 via 172.16.101.2"
1248	log_test $? 2 "Single path - replace of non-existent route"
1249}
1250
1251ipv4_rt_replace_mpath()
1252{
1253	# multipath with multipath
1254	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1255	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.3"
1256	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"
1257	log_test $? 0 "Multipath with multipath"
1258
1259	# multipath with single
1260	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1261	run_cmd "$IP ro replace 172.16.104.0/24 via 172.16.101.3"
1262	check_route  "172.16.104.0/24 via 172.16.101.3 dev veth1"
1263	log_test $? 0 "Multipath with single path"
1264
1265	# multipath with single
1266	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1267	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3"
1268	check_route "172.16.104.0/24 via 172.16.101.3 dev veth1"
1269	log_test $? 0 "Multipath with single path via multipath attribute"
1270
1271	# multipath with reject
1272	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1273	run_cmd "$IP ro replace unreachable 172.16.104.0/24"
1274	check_route "unreachable 172.16.104.0/24"
1275	log_test $? 0 "Multipath with reject route"
1276
1277	# route replace fails - invalid nexthop 1
1278	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1279	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.111.3 nexthop via 172.16.103.3"
1280	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"
1281	log_test $? 0 "Multipath - invalid first nexthop"
1282
1283	# route replace fails - invalid nexthop 2
1284	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1285	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.113.3"
1286	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"
1287	log_test $? 0 "Multipath - invalid second nexthop"
1288
1289	# multipath non-existent route
1290	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1291	run_cmd "$IP ro change 172.16.105.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.3"
1292	log_test $? 2 "Multipath - replace of non-existent route"
1293}
1294
1295ipv4_rt_replace()
1296{
1297	echo
1298	echo "IPv4 route replace tests"
1299
1300	ipv4_rt_replace_single
1301	ipv4_rt_replace_mpath
1302}
1303
1304ipv4_route_test()
1305{
1306	route_setup
1307
1308	ipv4_rt_add
1309	ipv4_rt_replace
1310
1311	route_cleanup
1312}
1313
1314ipv4_addr_metric_test()
1315{
1316	local rc
1317
1318	echo
1319	echo "IPv4 prefix route tests"
1320
1321	ip_addr_metric_check || return 1
1322
1323	setup
1324
1325	set -e
1326	$IP li add dummy1 type dummy
1327	$IP li add dummy2 type dummy
1328	$IP li set dummy1 up
1329	$IP li set dummy2 up
1330
1331	# default entry is metric 256
1332	run_cmd "$IP addr add dev dummy1 172.16.104.1/24"
1333	run_cmd "$IP addr add dev dummy2 172.16.104.2/24"
1334	set +e
1335
1336	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"
1337	log_test $? 0 "Default metric"
1338
1339	set -e
1340	run_cmd "$IP addr flush dev dummy1"
1341	run_cmd "$IP addr add dev dummy1 172.16.104.1/24 metric 257"
1342	set +e
1343
1344	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"
1345	log_test $? 0 "User specified metric on first device"
1346
1347	set -e
1348	run_cmd "$IP addr flush dev dummy2"
1349	run_cmd "$IP addr add dev dummy2 172.16.104.2/24 metric 258"
1350	set +e
1351
1352	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"
1353	log_test $? 0 "User specified metric on second device"
1354
1355	run_cmd "$IP addr del dev dummy1 172.16.104.1/24 metric 257"
1356	rc=$?
1357	if [ $rc -eq 0 ]; then
1358		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 258"
1359		rc=$?
1360	fi
1361	log_test $rc 0 "Delete of address on first device"
1362
1363	run_cmd "$IP addr change dev dummy2 172.16.104.2/24 metric 259"
1364	rc=$?
1365	if [ $rc -eq 0 ]; then
1366		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 259"
1367		rc=$?
1368	fi
1369	log_test $rc 0 "Modify metric of address"
1370
1371	# verify prefix route removed on down
1372	run_cmd "$IP li set dev dummy2 down"
1373	rc=$?
1374	if [ $rc -eq 0 ]; then
1375		out=$($IP ro ls match 172.16.104.0/24)
1376		check_expected "${out}" ""
1377		rc=$?
1378	fi
1379	log_test $rc 0 "Prefix route removed on link down"
1380
1381	# verify prefix route re-inserted with assigned metric
1382	run_cmd "$IP li set dev dummy2 up"
1383	rc=$?
1384	if [ $rc -eq 0 ]; then
1385		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 259"
1386		rc=$?
1387	fi
1388	log_test $rc 0 "Prefix route with metric on link up"
1389
1390	$IP li del dummy1
1391	$IP li del dummy2
1392	cleanup
1393}
1394
1395ipv4_route_metrics_test()
1396{
1397	local rc
1398
1399	echo
1400	echo "IPv4 route add / append tests"
1401
1402	route_setup
1403
1404	run_cmd "$IP ro add 172.16.111.0/24 via 172.16.101.2 mtu 1400"
1405	rc=$?
1406	if [ $rc -eq 0 ]; then
1407		check_route "172.16.111.0/24 via 172.16.101.2 dev veth1 mtu 1400"
1408		rc=$?
1409	fi
1410	log_test $rc 0 "Single path route with mtu metric"
1411
1412
1413	run_cmd "$IP ro add 172.16.112.0/24 mtu 1400 nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1414	rc=$?
1415	if [ $rc -eq 0 ]; then
1416		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"
1417		rc=$?
1418	fi
1419	log_test $rc 0 "Multipath route with mtu metric"
1420
1421	$IP ro add 172.16.104.0/24 via 172.16.101.2 mtu 1300
1422	run_cmd "ip netns exec ns1 ping -w1 -c1 -s 1500 172.16.104.1"
1423	log_test $? 0 "Using route with mtu metric"
1424
1425	run_cmd "$IP ro add 172.16.111.0/24 via 172.16.101.2 congctl lock foo"
1426	log_test $? 2 "Invalid metric (fails metric_convert)"
1427
1428	route_cleanup
1429}
1430
1431
1432################################################################################
1433# usage
1434
1435usage()
1436{
1437	cat <<EOF
1438usage: ${0##*/} OPTS
1439
1440        -t <test>   Test(s) to run (default: all)
1441                    (options: $TESTS)
1442        -p          Pause on fail
1443        -P          Pause after each test before cleanup
1444        -v          verbose mode (show commands and output)
1445EOF
1446}
1447
1448################################################################################
1449# main
1450
1451while getopts :t:pPhv o
1452do
1453	case $o in
1454		t) TESTS=$OPTARG;;
1455		p) PAUSE_ON_FAIL=yes;;
1456		P) PAUSE=yes;;
1457		v) VERBOSE=$(($VERBOSE + 1));;
1458		h) usage; exit 0;;
1459		*) usage; exit 1;;
1460	esac
1461done
1462
1463PEER_CMD="ip netns exec ${PEER_NS}"
1464
1465# make sure we don't pause twice
1466[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
1467
1468if [ "$(id -u)" -ne 0 ];then
1469	echo "SKIP: Need root privileges"
1470	exit $ksft_skip;
1471fi
1472
1473if [ ! -x "$(command -v ip)" ]; then
1474	echo "SKIP: Could not run test without ip tool"
1475	exit $ksft_skip
1476fi
1477
1478ip route help 2>&1 | grep -q fibmatch
1479if [ $? -ne 0 ]; then
1480	echo "SKIP: iproute2 too old, missing fibmatch"
1481	exit $ksft_skip
1482fi
1483
1484# start clean
1485cleanup &> /dev/null
1486
1487for t in $TESTS
1488do
1489	case $t in
1490	fib_unreg_test|unregister)	fib_unreg_test;;
1491	fib_down_test|down)		fib_down_test;;
1492	fib_carrier_test|carrier)	fib_carrier_test;;
1493	fib_nexthop_test|nexthop)	fib_nexthop_test;;
1494	ipv6_route_test|ipv6_rt)	ipv6_route_test;;
1495	ipv4_route_test|ipv4_rt)	ipv4_route_test;;
1496	ipv6_addr_metric)		ipv6_addr_metric_test;;
1497	ipv4_addr_metric)		ipv4_addr_metric_test;;
1498	ipv6_route_metrics)		ipv6_route_metrics_test;;
1499	ipv4_route_metrics)		ipv4_route_metrics_test;;
1500
1501	help) echo "Test names: $TESTS"; exit 0;;
1502	esac
1503done
1504
1505if [ "$TESTS" != "none" ]; then
1506	printf "\nTests passed: %3d\n" ${nsuccess}
1507	printf "Tests failed: %3d\n"   ${nfail}
1508fi
1509
1510exit $ret
1511