1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# ns: me               | ns: peer              | ns: remote
5#   2001:db8:91::1     |       2001:db8:91::2  |
6#   172.16.1.1         |       172.16.1.2      |
7#            veth1 <---|---> veth2             |
8#                      |              veth5 <--|--> veth6  172.16.101.1
9#            veth3 <---|---> veth4             |           2001:db8:101::1
10#   172.16.2.1         |       172.16.2.2      |
11#   2001:db8:92::1     |       2001:db8:92::2  |
12#
13# This test is for checking IPv4 and IPv6 FIB behavior with nexthop
14# objects. Device reference counts and network namespace cleanup tested
15# by use of network namespace for peer.
16
17ret=0
18# Kselftest framework requirement - SKIP code is 4.
19ksft_skip=4
20
21# all tests in this script. Can be overridden with -t option
22IPV4_TESTS="
23	ipv4_fcnal
24	ipv4_grp_fcnal
25	ipv4_res_grp_fcnal
26	ipv4_withv6_fcnal
27	ipv4_fcnal_runtime
28	ipv4_large_grp
29	ipv4_large_res_grp
30	ipv4_compat_mode
31	ipv4_fdb_grp_fcnal
32	ipv4_mpath_select
33	ipv4_torture
34	ipv4_res_torture
35"
36
37IPV6_TESTS="
38	ipv6_fcnal
39	ipv6_grp_fcnal
40	ipv6_res_grp_fcnal
41	ipv6_fcnal_runtime
42	ipv6_large_grp
43	ipv6_large_res_grp
44	ipv6_compat_mode
45	ipv6_fdb_grp_fcnal
46	ipv6_mpath_select
47	ipv6_torture
48	ipv6_res_torture
49"
50
51ALL_TESTS="
52	basic
53	basic_res
54	${IPV4_TESTS}
55	${IPV6_TESTS}
56"
57TESTS="${ALL_TESTS}"
58VERBOSE=0
59PAUSE_ON_FAIL=no
60PAUSE=no
61PING_TIMEOUT=5
62
63nsid=100
64
65################################################################################
66# utilities
67
68log_test()
69{
70	local rc=$1
71	local expected=$2
72	local msg="$3"
73
74	if [ ${rc} -eq ${expected} ]; then
75		printf "TEST: %-60s  [ OK ]\n" "${msg}"
76		nsuccess=$((nsuccess+1))
77	else
78		ret=1
79		nfail=$((nfail+1))
80		printf "TEST: %-60s  [FAIL]\n" "${msg}"
81		if [ "$VERBOSE" = "1" ]; then
82			echo "    rc=$rc, expected $expected"
83		fi
84
85		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
86		echo
87			echo "hit enter to continue, 'q' to quit"
88			read a
89			[ "$a" = "q" ] && exit 1
90		fi
91	fi
92
93	if [ "${PAUSE}" = "yes" ]; then
94		echo
95		echo "hit enter to continue, 'q' to quit"
96		read a
97		[ "$a" = "q" ] && exit 1
98	fi
99
100	[ "$VERBOSE" = "1" ] && echo
101}
102
103run_cmd()
104{
105	local cmd="$1"
106	local out
107	local stderr="2>/dev/null"
108
109	if [ "$VERBOSE" = "1" ]; then
110		printf "COMMAND: $cmd\n"
111		stderr=
112	fi
113
114	out=$(eval $cmd $stderr)
115	rc=$?
116	if [ "$VERBOSE" = "1" -a -n "$out" ]; then
117		echo "    $out"
118	fi
119
120	return $rc
121}
122
123get_linklocal()
124{
125	local dev=$1
126	local ns
127	local addr
128
129	[ -n "$2" ] && ns="-netns $2"
130	addr=$(ip $ns -6 -br addr show dev ${dev} | \
131	awk '{
132		for (i = 3; i <= NF; ++i) {
133			if ($i ~ /^fe80/)
134				print $i
135		}
136	}'
137	)
138	addr=${addr/\/*}
139
140	[ -z "$addr" ] && return 1
141
142	echo $addr
143
144	return 0
145}
146
147create_ns()
148{
149	local n=${1}
150
151	ip netns del ${n} 2>/dev/null
152
153	set -e
154	ip netns add ${n}
155	ip netns set ${n} $((nsid++))
156	ip -netns ${n} addr add 127.0.0.1/8 dev lo
157	ip -netns ${n} link set lo up
158
159	ip netns exec ${n} sysctl -qw net.ipv4.ip_forward=1
160	ip netns exec ${n} sysctl -qw net.ipv4.fib_multipath_use_neigh=1
161	ip netns exec ${n} sysctl -qw net.ipv4.conf.default.ignore_routes_with_linkdown=1
162	ip netns exec ${n} sysctl -qw net.ipv6.conf.all.keep_addr_on_down=1
163	ip netns exec ${n} sysctl -qw net.ipv6.conf.all.forwarding=1
164	ip netns exec ${n} sysctl -qw net.ipv6.conf.default.forwarding=1
165	ip netns exec ${n} sysctl -qw net.ipv6.conf.default.ignore_routes_with_linkdown=1
166	ip netns exec ${n} sysctl -qw net.ipv6.conf.all.accept_dad=0
167	ip netns exec ${n} sysctl -qw net.ipv6.conf.default.accept_dad=0
168
169	set +e
170}
171
172setup()
173{
174	cleanup
175
176	create_ns me
177	create_ns peer
178	create_ns remote
179
180	IP="ip -netns me"
181	BRIDGE="bridge -netns me"
182	set -e
183	$IP li add veth1 type veth peer name veth2
184	$IP li set veth1 up
185	$IP addr add 172.16.1.1/24 dev veth1
186	$IP -6 addr add 2001:db8:91::1/64 dev veth1 nodad
187
188	$IP li add veth3 type veth peer name veth4
189	$IP li set veth3 up
190	$IP addr add 172.16.2.1/24 dev veth3
191	$IP -6 addr add 2001:db8:92::1/64 dev veth3 nodad
192
193	$IP li set veth2 netns peer up
194	ip -netns peer addr add 172.16.1.2/24 dev veth2
195	ip -netns peer -6 addr add 2001:db8:91::2/64 dev veth2 nodad
196
197	$IP li set veth4 netns peer up
198	ip -netns peer addr add 172.16.2.2/24 dev veth4
199	ip -netns peer -6 addr add 2001:db8:92::2/64 dev veth4 nodad
200
201	ip -netns remote li add veth5 type veth peer name veth6
202	ip -netns remote li set veth5 up
203	ip -netns remote addr add dev veth5 172.16.101.1/24
204	ip -netns remote -6 addr add dev veth5 2001:db8:101::1/64 nodad
205	ip -netns remote ro add 172.16.0.0/22 via 172.16.101.2
206	ip -netns remote -6 ro add 2001:db8:90::/40 via 2001:db8:101::2
207
208	ip -netns remote li set veth6 netns peer up
209	ip -netns peer addr add dev veth6 172.16.101.2/24
210	ip -netns peer -6 addr add dev veth6 2001:db8:101::2/64 nodad
211	set +e
212}
213
214cleanup()
215{
216	local ns
217
218	for ns in me peer remote; do
219		ip netns del ${ns} 2>/dev/null
220	done
221}
222
223check_output()
224{
225	local out="$1"
226	local expected="$2"
227	local rc=0
228
229	[ "${out}" = "${expected}" ] && return 0
230
231	if [ -z "${out}" ]; then
232		if [ "$VERBOSE" = "1" ]; then
233			printf "\nNo entry found\n"
234			printf "Expected:\n"
235			printf "    ${expected}\n"
236		fi
237		return 1
238	fi
239
240	out=$(echo ${out})
241	if [ "${out}" != "${expected}" ]; then
242		rc=1
243		if [ "${VERBOSE}" = "1" ]; then
244			printf "    Unexpected entry. Have:\n"
245			printf "        ${out}\n"
246			printf "    Expected:\n"
247			printf "        ${expected}\n\n"
248		else
249			echo "      WARNING: Unexpected route entry"
250		fi
251	fi
252
253	return $rc
254}
255
256check_nexthop()
257{
258	local nharg="$1"
259	local expected="$2"
260	local out
261
262	out=$($IP nexthop ls ${nharg} 2>/dev/null)
263
264	check_output "${out}" "${expected}"
265}
266
267check_nexthop_bucket()
268{
269	local nharg="$1"
270	local expected="$2"
271	local out
272
273	# remove the idle time since we cannot match it
274	out=$($IP nexthop bucket ${nharg} \
275		| sed s/idle_time\ [0-9.]*\ // 2>/dev/null)
276
277	check_output "${out}" "${expected}"
278}
279
280check_route()
281{
282	local pfx="$1"
283	local expected="$2"
284	local out
285
286	out=$($IP route ls match ${pfx} 2>/dev/null)
287
288	check_output "${out}" "${expected}"
289}
290
291check_route6()
292{
293	local pfx="$1"
294	local expected="$2"
295	local out
296
297	out=$($IP -6 route ls match ${pfx} 2>/dev/null | sed -e 's/pref medium//')
298
299	check_output "${out}" "${expected}"
300}
301
302check_large_grp()
303{
304	local ipv=$1
305	local ecmp=$2
306	local grpnum=100
307	local nhidstart=100
308	local grpidstart=1000
309	local iter=0
310	local nhidstr=""
311	local grpidstr=""
312	local grpstr=""
313	local ipstr=""
314
315	if [ $ipv -eq 4 ]; then
316		ipstr="172.16.1."
317	else
318		ipstr="2001:db8:91::"
319	fi
320
321	#
322	# Create $grpnum groups with specified $ecmp and dump them
323	#
324
325	# create nexthops with different gateways
326	iter=2
327	while [ $iter -le $(($ecmp + 1)) ]
328	do
329		nhidstr="$(($nhidstart + $iter))"
330		run_cmd "$IP nexthop add id $nhidstr via $ipstr$iter dev veth1"
331		check_nexthop "id $nhidstr" "id $nhidstr via $ipstr$iter dev veth1 scope link"
332
333		if [ $iter -le $ecmp ]; then
334			grpstr+="$nhidstr/"
335		else
336			grpstr+="$nhidstr"
337		fi
338		((iter++))
339	done
340
341	# create duplicate large ecmp groups
342	iter=0
343	while [ $iter -le $grpnum ]
344	do
345		grpidstr="$(($grpidstart + $iter))"
346		run_cmd "$IP nexthop add id $grpidstr group $grpstr"
347		check_nexthop "id $grpidstr" "id $grpidstr group $grpstr"
348		((iter++))
349	done
350
351	# dump large groups
352	run_cmd "$IP nexthop list"
353	log_test $? 0 "Dump large (x$ecmp) ecmp groups"
354}
355
356check_large_res_grp()
357{
358	local ipv=$1
359	local buckets=$2
360	local ipstr=""
361
362	if [ $ipv -eq 4 ]; then
363		ipstr="172.16.1.2"
364	else
365		ipstr="2001:db8:91::2"
366	fi
367
368	# create a resilient group with $buckets buckets and dump them
369	run_cmd "$IP nexthop add id 100 via $ipstr dev veth1"
370	run_cmd "$IP nexthop add id 1000 group 100 type resilient buckets $buckets"
371	run_cmd "$IP nexthop bucket list"
372	log_test $? 0 "Dump large (x$buckets) nexthop buckets"
373}
374
375get_route_dev()
376{
377	local pfx="$1"
378	local out
379
380	if out=$($IP -j route get "$pfx" | jq -re ".[0].dev"); then
381		echo "$out"
382	fi
383}
384
385check_route_dev()
386{
387	local pfx="$1"
388	local expected="$2"
389	local out
390
391	out=$(get_route_dev "$pfx")
392
393	check_output "$out" "$expected"
394}
395
396start_ip_monitor()
397{
398	local mtype=$1
399
400	# start the monitor in the background
401	tmpfile=`mktemp /var/run/nexthoptestXXX`
402	mpid=`($IP monitor $mtype > $tmpfile & echo $!) 2>/dev/null`
403	sleep 0.2
404	echo "$mpid $tmpfile"
405}
406
407stop_ip_monitor()
408{
409	local mpid=$1
410	local tmpfile=$2
411	local el=$3
412
413	# check the monitor results
414	kill $mpid
415	lines=`wc -l $tmpfile | cut "-d " -f1`
416	test $lines -eq $el
417	rc=$?
418	rm -rf $tmpfile
419
420	return $rc
421}
422
423check_nexthop_fdb_support()
424{
425	$IP nexthop help 2>&1 | grep -q fdb
426	if [ $? -ne 0 ]; then
427		echo "SKIP: iproute2 too old, missing fdb nexthop support"
428		return $ksft_skip
429	fi
430}
431
432check_nexthop_res_support()
433{
434	$IP nexthop help 2>&1 | grep -q resilient
435	if [ $? -ne 0 ]; then
436		echo "SKIP: iproute2 too old, missing resilient nexthop group support"
437		return $ksft_skip
438	fi
439}
440
441ipv6_fdb_grp_fcnal()
442{
443	local rc
444
445	echo
446	echo "IPv6 fdb groups functional"
447	echo "--------------------------"
448
449	check_nexthop_fdb_support
450	if [ $? -eq $ksft_skip ]; then
451		return $ksft_skip
452	fi
453
454	# create group with multiple nexthops
455	run_cmd "$IP nexthop add id 61 via 2001:db8:91::2 fdb"
456	run_cmd "$IP nexthop add id 62 via 2001:db8:91::3 fdb"
457	run_cmd "$IP nexthop add id 102 group 61/62 fdb"
458	check_nexthop "id 102" "id 102 group 61/62 fdb"
459	log_test $? 0 "Fdb Nexthop group with multiple nexthops"
460
461	## get nexthop group
462	run_cmd "$IP nexthop get id 102"
463	check_nexthop "id 102" "id 102 group 61/62 fdb"
464	log_test $? 0 "Get Fdb nexthop group by id"
465
466	# fdb nexthop group can only contain fdb nexthops
467	run_cmd "$IP nexthop add id 63 via 2001:db8:91::4"
468	run_cmd "$IP nexthop add id 64 via 2001:db8:91::5"
469	run_cmd "$IP nexthop add id 103 group 63/64 fdb"
470	log_test $? 2 "Fdb Nexthop group with non-fdb nexthops"
471
472	# Non fdb nexthop group can not contain fdb nexthops
473	run_cmd "$IP nexthop add id 65 via 2001:db8:91::5 fdb"
474	run_cmd "$IP nexthop add id 66 via 2001:db8:91::6 fdb"
475	run_cmd "$IP nexthop add id 104 group 65/66"
476	log_test $? 2 "Non-Fdb Nexthop group with fdb nexthops"
477
478	# fdb nexthop cannot have blackhole
479	run_cmd "$IP nexthop add id 67 blackhole fdb"
480	log_test $? 2 "Fdb Nexthop with blackhole"
481
482	# fdb nexthop with oif
483	run_cmd "$IP nexthop add id 68 via 2001:db8:91::7 dev veth1 fdb"
484	log_test $? 2 "Fdb Nexthop with oif"
485
486	# fdb nexthop with onlink
487	run_cmd "$IP nexthop add id 68 via 2001:db8:91::7 onlink fdb"
488	log_test $? 2 "Fdb Nexthop with onlink"
489
490	# fdb nexthop with encap
491	run_cmd "$IP nexthop add id 69 encap mpls 101 via 2001:db8:91::8 dev veth1 fdb"
492	log_test $? 2 "Fdb Nexthop with encap"
493
494	run_cmd "$IP link add name vx10 type vxlan id 1010 local 2001:db8:91::9 remote 2001:db8:91::10 dstport 4789 nolearning noudpcsum tos inherit ttl 100"
495	run_cmd "$BRIDGE fdb add 02:02:00:00:00:13 dev vx10 nhid 102 self"
496	log_test $? 0 "Fdb mac add with nexthop group"
497
498	## fdb nexthops can only reference nexthop groups and not nexthops
499	run_cmd "$BRIDGE fdb add 02:02:00:00:00:14 dev vx10 nhid 61 self"
500	log_test $? 255 "Fdb mac add with nexthop"
501
502	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 66"
503	log_test $? 2 "Route add with fdb nexthop"
504
505	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 103"
506	log_test $? 2 "Route add with fdb nexthop group"
507
508	run_cmd "$IP nexthop del id 61"
509	run_cmd "$BRIDGE fdb get to 02:02:00:00:00:13 dev vx10 self"
510	log_test $? 0 "Fdb entry after deleting a single nexthop"
511
512	run_cmd "$IP nexthop del id 102"
513	log_test $? 0 "Fdb nexthop delete"
514
515	run_cmd "$BRIDGE fdb get to 02:02:00:00:00:13 dev vx10 self"
516	log_test $? 254 "Fdb entry after deleting a nexthop group"
517
518	$IP link del dev vx10
519}
520
521ipv4_fdb_grp_fcnal()
522{
523	local rc
524
525	echo
526	echo "IPv4 fdb groups functional"
527	echo "--------------------------"
528
529	check_nexthop_fdb_support
530	if [ $? -eq $ksft_skip ]; then
531		return $ksft_skip
532	fi
533
534	# create group with multiple nexthops
535	run_cmd "$IP nexthop add id 12 via 172.16.1.2 fdb"
536	run_cmd "$IP nexthop add id 13 via 172.16.1.3 fdb"
537	run_cmd "$IP nexthop add id 102 group 12/13 fdb"
538	check_nexthop "id 102" "id 102 group 12/13 fdb"
539	log_test $? 0 "Fdb Nexthop group with multiple nexthops"
540
541	# get nexthop group
542	run_cmd "$IP nexthop get id 102"
543	check_nexthop "id 102" "id 102 group 12/13 fdb"
544	log_test $? 0 "Get Fdb nexthop group by id"
545
546	# fdb nexthop group can only contain fdb nexthops
547	run_cmd "$IP nexthop add id 14 via 172.16.1.2"
548	run_cmd "$IP nexthop add id 15 via 172.16.1.3"
549	run_cmd "$IP nexthop add id 103 group 14/15 fdb"
550	log_test $? 2 "Fdb Nexthop group with non-fdb nexthops"
551
552	# Non fdb nexthop group can not contain fdb nexthops
553	run_cmd "$IP nexthop add id 16 via 172.16.1.2 fdb"
554	run_cmd "$IP nexthop add id 17 via 172.16.1.3 fdb"
555	run_cmd "$IP nexthop add id 104 group 14/15"
556	log_test $? 2 "Non-Fdb Nexthop group with fdb nexthops"
557
558	# fdb nexthop cannot have blackhole
559	run_cmd "$IP nexthop add id 18 blackhole fdb"
560	log_test $? 2 "Fdb Nexthop with blackhole"
561
562	# fdb nexthop with oif
563	run_cmd "$IP nexthop add id 16 via 172.16.1.2 dev veth1 fdb"
564	log_test $? 2 "Fdb Nexthop with oif"
565
566	# fdb nexthop with onlink
567	run_cmd "$IP nexthop add id 16 via 172.16.1.2 onlink fdb"
568	log_test $? 2 "Fdb Nexthop with onlink"
569
570	# fdb nexthop with encap
571	run_cmd "$IP nexthop add id 17 encap mpls 101 via 172.16.1.2 dev veth1 fdb"
572	log_test $? 2 "Fdb Nexthop with encap"
573
574	run_cmd "$IP link add name vx10 type vxlan id 1010 local 10.0.0.1 remote 10.0.0.2 dstport 4789 nolearning noudpcsum tos inherit ttl 100"
575	run_cmd "$BRIDGE fdb add 02:02:00:00:00:13 dev vx10 nhid 102 self"
576	log_test $? 0 "Fdb mac add with nexthop group"
577
578	# fdb nexthops can only reference nexthop groups and not nexthops
579	run_cmd "$BRIDGE fdb add 02:02:00:00:00:14 dev vx10 nhid 12 self"
580	log_test $? 255 "Fdb mac add with nexthop"
581
582	run_cmd "$IP ro add 172.16.0.0/22 nhid 15"
583	log_test $? 2 "Route add with fdb nexthop"
584
585	run_cmd "$IP ro add 172.16.0.0/22 nhid 103"
586	log_test $? 2 "Route add with fdb nexthop group"
587
588	run_cmd "$IP nexthop del id 12"
589	run_cmd "$BRIDGE fdb get to 02:02:00:00:00:13 dev vx10 self"
590	log_test $? 0 "Fdb entry after deleting a single nexthop"
591
592	run_cmd "$IP nexthop del id 102"
593	log_test $? 0 "Fdb nexthop delete"
594
595	run_cmd "$BRIDGE fdb get to 02:02:00:00:00:13 dev vx10 self"
596	log_test $? 254 "Fdb entry after deleting a nexthop group"
597
598	$IP link del dev vx10
599}
600
601ipv4_mpath_select()
602{
603	local rc dev match h addr
604
605	echo
606	echo "IPv4 multipath selection"
607	echo "------------------------"
608	if [ ! -x "$(command -v jq)" ]; then
609		echo "SKIP: Could not run test; need jq tool"
610		return $ksft_skip
611	fi
612
613	# Use status of existing neighbor entry when determining nexthop for
614	# multipath routes.
615	local -A gws
616	gws=([veth1]=172.16.1.2 [veth3]=172.16.2.2)
617	local -A other_dev
618	other_dev=([veth1]=veth3 [veth3]=veth1)
619
620	run_cmd "$IP nexthop add id 1 via ${gws["veth1"]} dev veth1"
621	run_cmd "$IP nexthop add id 2 via ${gws["veth3"]} dev veth3"
622	run_cmd "$IP nexthop add id 1001 group 1/2"
623	run_cmd "$IP ro add 172.16.101.0/24 nhid 1001"
624	rc=0
625	for dev in veth1 veth3; do
626		match=0
627		for h in {1..254}; do
628			addr="172.16.101.$h"
629			if [ "$(get_route_dev "$addr")" = "$dev" ]; then
630				match=1
631				break
632			fi
633		done
634		if (( match == 0 )); then
635			echo "SKIP: Did not find a route using device $dev"
636			return $ksft_skip
637		fi
638		run_cmd "$IP neigh add ${gws[$dev]} dev $dev nud failed"
639		if ! check_route_dev "$addr" "${other_dev[$dev]}"; then
640			rc=1
641			break
642		fi
643		run_cmd "$IP neigh del ${gws[$dev]} dev $dev"
644	done
645	log_test $rc 0 "Use valid neighbor during multipath selection"
646
647	run_cmd "$IP neigh add 172.16.1.2 dev veth1 nud incomplete"
648	run_cmd "$IP neigh add 172.16.2.2 dev veth3 nud incomplete"
649	run_cmd "$IP route get 172.16.101.1"
650	# if we did not crash, success
651	log_test $rc 0 "Multipath selection with no valid neighbor"
652}
653
654ipv6_mpath_select()
655{
656	local rc dev match h addr
657
658	echo
659	echo "IPv6 multipath selection"
660	echo "------------------------"
661	if [ ! -x "$(command -v jq)" ]; then
662		echo "SKIP: Could not run test; need jq tool"
663		return $ksft_skip
664	fi
665
666	# Use status of existing neighbor entry when determining nexthop for
667	# multipath routes.
668	local -A gws
669	gws=([veth1]=2001:db8:91::2 [veth3]=2001:db8:92::2)
670	local -A other_dev
671	other_dev=([veth1]=veth3 [veth3]=veth1)
672
673	run_cmd "$IP nexthop add id 1 via ${gws["veth1"]} dev veth1"
674	run_cmd "$IP nexthop add id 2 via ${gws["veth3"]} dev veth3"
675	run_cmd "$IP nexthop add id 1001 group 1/2"
676	run_cmd "$IP ro add 2001:db8:101::/64 nhid 1001"
677	rc=0
678	for dev in veth1 veth3; do
679		match=0
680		for h in {1..65535}; do
681			addr=$(printf "2001:db8:101::%x" $h)
682			if [ "$(get_route_dev "$addr")" = "$dev" ]; then
683				match=1
684				break
685			fi
686		done
687		if (( match == 0 )); then
688			echo "SKIP: Did not find a route using device $dev"
689			return $ksft_skip
690		fi
691		run_cmd "$IP neigh add ${gws[$dev]} dev $dev nud failed"
692		if ! check_route_dev "$addr" "${other_dev[$dev]}"; then
693			rc=1
694			break
695		fi
696		run_cmd "$IP neigh del ${gws[$dev]} dev $dev"
697	done
698	log_test $rc 0 "Use valid neighbor during multipath selection"
699
700	run_cmd "$IP neigh add 2001:db8:91::2 dev veth1 nud incomplete"
701	run_cmd "$IP neigh add 2001:db8:92::2 dev veth3 nud incomplete"
702	run_cmd "$IP route get 2001:db8:101::1"
703	# if we did not crash, success
704	log_test $rc 0 "Multipath selection with no valid neighbor"
705}
706
707################################################################################
708# basic operations (add, delete, replace) on nexthops and nexthop groups
709#
710# IPv6
711
712ipv6_fcnal()
713{
714	local rc
715
716	echo
717	echo "IPv6"
718	echo "----------------------"
719
720	run_cmd "$IP nexthop add id 52 via 2001:db8:91::2 dev veth1"
721	rc=$?
722	log_test $rc 0 "Create nexthop with id, gw, dev"
723	if [ $rc -ne 0 ]; then
724		echo "Basic IPv6 create fails; can not continue"
725		return 1
726	fi
727
728	run_cmd "$IP nexthop get id 52"
729	log_test $? 0 "Get nexthop by id"
730	check_nexthop "id 52" "id 52 via 2001:db8:91::2 dev veth1 scope link"
731
732	run_cmd "$IP nexthop del id 52"
733	log_test $? 0 "Delete nexthop by id"
734	check_nexthop "id 52" ""
735
736	#
737	# gw, device spec
738	#
739	# gw validation, no device - fails since dev required
740	run_cmd "$IP nexthop add id 52 via 2001:db8:92::3"
741	log_test $? 2 "Create nexthop - gw only"
742
743	# gw is not reachable throught given dev
744	run_cmd "$IP nexthop add id 53 via 2001:db8:3::3 dev veth1"
745	log_test $? 2 "Create nexthop - invalid gw+dev combination"
746
747	# onlink arg overrides gw+dev lookup
748	run_cmd "$IP nexthop add id 53 via 2001:db8:3::3 dev veth1 onlink"
749	log_test $? 0 "Create nexthop - gw+dev and onlink"
750
751	# admin down should delete nexthops
752	set -e
753	run_cmd "$IP -6 nexthop add id 55 via 2001:db8:91::3 dev veth1"
754	run_cmd "$IP nexthop add id 56 via 2001:db8:91::4 dev veth1"
755	run_cmd "$IP nexthop add id 57 via 2001:db8:91::5 dev veth1"
756	run_cmd "$IP li set dev veth1 down"
757	set +e
758	check_nexthop "dev veth1" ""
759	log_test $? 0 "Nexthops removed on admin down"
760}
761
762ipv6_grp_refs()
763{
764	if [ ! -x "$(command -v mausezahn)" ]; then
765		echo "SKIP: Could not run test; need mausezahn tool"
766		return
767	fi
768
769	run_cmd "$IP link set dev veth1 up"
770	run_cmd "$IP link add veth1.10 link veth1 up type vlan id 10"
771	run_cmd "$IP link add veth1.20 link veth1 up type vlan id 20"
772	run_cmd "$IP -6 addr add 2001:db8:91::1/64 dev veth1.10"
773	run_cmd "$IP -6 addr add 2001:db8:92::1/64 dev veth1.20"
774	run_cmd "$IP -6 neigh add 2001:db8:91::2 lladdr 00:11:22:33:44:55 dev veth1.10"
775	run_cmd "$IP -6 neigh add 2001:db8:92::2 lladdr 00:11:22:33:44:55 dev veth1.20"
776	run_cmd "$IP nexthop add id 100 via 2001:db8:91::2 dev veth1.10"
777	run_cmd "$IP nexthop add id 101 via 2001:db8:92::2 dev veth1.20"
778	run_cmd "$IP nexthop add id 102 group 100"
779	run_cmd "$IP route add 2001:db8:101::1/128 nhid 102"
780
781	# create per-cpu dsts through nh 100
782	run_cmd "ip netns exec me mausezahn -6 veth1.10 -B 2001:db8:101::1 -A 2001:db8:91::1 -c 5 -t tcp "dp=1-1023, flags=syn" >/dev/null 2>&1"
783
784	# remove nh 100 from the group to delete the route potentially leaving
785	# a stale per-cpu dst which holds a reference to the nexthop's net
786	# device and to the IPv6 route
787	run_cmd "$IP nexthop replace id 102 group 101"
788	run_cmd "$IP route del 2001:db8:101::1/128"
789
790	# add both nexthops to the group so a reference is taken on them
791	run_cmd "$IP nexthop replace id 102 group 100/101"
792
793	# if the bug described in commit "net: nexthop: release IPv6 per-cpu
794	# dsts when replacing a nexthop group" exists at this point we have
795	# an unlinked IPv6 route (but not freed due to stale dst) with a
796	# reference over the group so we delete the group which will again
797	# only unlink it due to the route reference
798	run_cmd "$IP nexthop del id 102"
799
800	# delete the nexthop with stale dst, since we have an unlinked
801	# group with a ref to it and an unlinked IPv6 route with ref to the
802	# group, the nh will only be unlinked and not freed so the stale dst
803	# remains forever and we get a net device refcount imbalance
804	run_cmd "$IP nexthop del id 100"
805
806	# if a reference was lost this command will hang because the net device
807	# cannot be removed
808	timeout -s KILL 5 ip netns exec me ip link del veth1.10 >/dev/null 2>&1
809
810	# we can't cleanup if the command is hung trying to delete the netdev
811	if [ $? -eq 137 ]; then
812		return 1
813	fi
814
815	# cleanup
816	run_cmd "$IP link del veth1.20"
817	run_cmd "$IP nexthop flush"
818
819	return 0
820}
821
822ipv6_grp_fcnal()
823{
824	local rc
825
826	echo
827	echo "IPv6 groups functional"
828	echo "----------------------"
829
830	# basic functionality: create a nexthop group, default weight
831	run_cmd "$IP nexthop add id 61 via 2001:db8:91::2 dev veth1"
832	run_cmd "$IP nexthop add id 101 group 61"
833	log_test $? 0 "Create nexthop group with single nexthop"
834
835	# get nexthop group
836	run_cmd "$IP nexthop get id 101"
837	log_test $? 0 "Get nexthop group by id"
838	check_nexthop "id 101" "id 101 group 61"
839
840	# delete nexthop group
841	run_cmd "$IP nexthop del id 101"
842	log_test $? 0 "Delete nexthop group by id"
843	check_nexthop "id 101" ""
844
845	$IP nexthop flush >/dev/null 2>&1
846	check_nexthop "id 101" ""
847
848	#
849	# create group with multiple nexthops - mix of gw and dev only
850	#
851	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
852	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
853	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
854	run_cmd "$IP nexthop add id 65 dev veth1"
855	run_cmd "$IP nexthop add id 102 group 62/63/64/65"
856	log_test $? 0 "Nexthop group with multiple nexthops"
857	check_nexthop "id 102" "id 102 group 62/63/64/65"
858
859	# Delete nexthop in a group and group is updated
860	run_cmd "$IP nexthop del id 63"
861	check_nexthop "id 102" "id 102 group 62/64/65"
862	log_test $? 0 "Nexthop group updated when entry is deleted"
863
864	# create group with multiple weighted nexthops
865	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
866	run_cmd "$IP nexthop add id 103 group 62/63,2/64,3/65,4"
867	log_test $? 0 "Nexthop group with weighted nexthops"
868	check_nexthop "id 103" "id 103 group 62/63,2/64,3/65,4"
869
870	# Delete nexthop in a weighted group and group is updated
871	run_cmd "$IP nexthop del id 63"
872	check_nexthop "id 103" "id 103 group 62/64,3/65,4"
873	log_test $? 0 "Weighted nexthop group updated when entry is deleted"
874
875	# admin down - nexthop is removed from group
876	run_cmd "$IP li set dev veth1 down"
877	check_nexthop "dev veth1" ""
878	log_test $? 0 "Nexthops in groups removed on admin down"
879
880	# expect groups to have been deleted as well
881	check_nexthop "" ""
882
883	run_cmd "$IP li set dev veth1 up"
884
885	$IP nexthop flush >/dev/null 2>&1
886
887	# group with nexthops using different devices
888	set -e
889	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
890	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
891	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
892	run_cmd "$IP nexthop add id 65 via 2001:db8:91::5 dev veth1"
893
894	run_cmd "$IP nexthop add id 72 via 2001:db8:92::2 dev veth3"
895	run_cmd "$IP nexthop add id 73 via 2001:db8:92::3 dev veth3"
896	run_cmd "$IP nexthop add id 74 via 2001:db8:92::4 dev veth3"
897	run_cmd "$IP nexthop add id 75 via 2001:db8:92::5 dev veth3"
898	set +e
899
900	# multiple groups with same nexthop
901	run_cmd "$IP nexthop add id 104 group 62"
902	run_cmd "$IP nexthop add id 105 group 62"
903	check_nexthop "group" "id 104 group 62 id 105 group 62"
904	log_test $? 0 "Multiple groups with same nexthop"
905
906	run_cmd "$IP nexthop flush groups"
907	[ $? -ne 0 ] && return 1
908
909	# on admin down of veth1, it should be removed from the group
910	run_cmd "$IP nexthop add id 105 group 62/63/72/73/64"
911	run_cmd "$IP li set veth1 down"
912	check_nexthop "id 105" "id 105 group 72/73"
913	log_test $? 0 "Nexthops in group removed on admin down - mixed group"
914
915	run_cmd "$IP nexthop add id 106 group 105/74"
916	log_test $? 2 "Nexthop group can not have a group as an entry"
917
918	# a group can have a blackhole entry only if it is the only
919	# nexthop in the group. Needed for atomic replace with an
920	# actual nexthop group
921	run_cmd "$IP -6 nexthop add id 31 blackhole"
922	run_cmd "$IP nexthop add id 107 group 31"
923	log_test $? 0 "Nexthop group with a blackhole entry"
924
925	run_cmd "$IP nexthop add id 108 group 31/24"
926	log_test $? 2 "Nexthop group can not have a blackhole and another nexthop"
927
928	ipv6_grp_refs
929	log_test $? 0 "Nexthop group replace refcounts"
930}
931
932ipv6_res_grp_fcnal()
933{
934	local rc
935
936	echo
937	echo "IPv6 resilient groups functional"
938	echo "--------------------------------"
939
940	check_nexthop_res_support
941	if [ $? -eq $ksft_skip ]; then
942		return $ksft_skip
943	fi
944
945	#
946	# migration of nexthop buckets - equal weights
947	#
948	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
949	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
950	run_cmd "$IP nexthop add id 102 group 62/63 type resilient buckets 2 idle_timer 0"
951
952	run_cmd "$IP nexthop del id 63"
953	check_nexthop "id 102" \
954		"id 102 group 62 type resilient buckets 2 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
955	log_test $? 0 "Nexthop group updated when entry is deleted"
956	check_nexthop_bucket "list id 102" \
957		"id 102 index 0 nhid 62 id 102 index 1 nhid 62"
958	log_test $? 0 "Nexthop buckets updated when entry is deleted"
959
960	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
961	run_cmd "$IP nexthop replace id 102 group 62/63 type resilient buckets 2 idle_timer 0"
962	check_nexthop "id 102" \
963		"id 102 group 62/63 type resilient buckets 2 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
964	log_test $? 0 "Nexthop group updated after replace"
965	check_nexthop_bucket "list id 102" \
966		"id 102 index 0 nhid 63 id 102 index 1 nhid 62"
967	log_test $? 0 "Nexthop buckets updated after replace"
968
969	$IP nexthop flush >/dev/null 2>&1
970
971	#
972	# migration of nexthop buckets - unequal weights
973	#
974	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
975	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
976	run_cmd "$IP nexthop add id 102 group 62,3/63,1 type resilient buckets 4 idle_timer 0"
977
978	run_cmd "$IP nexthop del id 63"
979	check_nexthop "id 102" \
980		"id 102 group 62,3 type resilient buckets 4 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
981	log_test $? 0 "Nexthop group updated when entry is deleted - nECMP"
982	check_nexthop_bucket "list id 102" \
983		"id 102 index 0 nhid 62 id 102 index 1 nhid 62 id 102 index 2 nhid 62 id 102 index 3 nhid 62"
984	log_test $? 0 "Nexthop buckets updated when entry is deleted - nECMP"
985
986	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
987	run_cmd "$IP nexthop replace id 102 group 62,3/63,1 type resilient buckets 4 idle_timer 0"
988	check_nexthop "id 102" \
989		"id 102 group 62,3/63 type resilient buckets 4 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
990	log_test $? 0 "Nexthop group updated after replace - nECMP"
991	check_nexthop_bucket "list id 102" \
992		"id 102 index 0 nhid 63 id 102 index 1 nhid 62 id 102 index 2 nhid 62 id 102 index 3 nhid 62"
993	log_test $? 0 "Nexthop buckets updated after replace - nECMP"
994}
995
996ipv6_fcnal_runtime()
997{
998	local rc
999
1000	echo
1001	echo "IPv6 functional runtime"
1002	echo "-----------------------"
1003
1004	#
1005	# IPv6 - the basics
1006	#
1007	run_cmd "$IP nexthop add id 81 via 2001:db8:91::2 dev veth1"
1008	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
1009	log_test $? 0 "Route add"
1010
1011	run_cmd "$IP ro delete 2001:db8:101::1/128 nhid 81"
1012	log_test $? 0 "Route delete"
1013
1014	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
1015	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 2001:db8:101::1"
1016	log_test $? 0 "Ping with nexthop"
1017
1018	run_cmd "$IP nexthop add id 82 via 2001:db8:92::2 dev veth3"
1019	run_cmd "$IP nexthop add id 122 group 81/82"
1020	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
1021	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 2001:db8:101::1"
1022	log_test $? 0 "Ping - multipath"
1023
1024	#
1025	# IPv6 with blackhole nexthops
1026	#
1027	run_cmd "$IP -6 nexthop add id 83 blackhole"
1028	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 83"
1029	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 2001:db8:101::1"
1030	log_test $? 2 "Ping - blackhole"
1031
1032	run_cmd "$IP nexthop replace id 83 via 2001:db8:91::2 dev veth1"
1033	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 2001:db8:101::1"
1034	log_test $? 0 "Ping - blackhole replaced with gateway"
1035
1036	run_cmd "$IP -6 nexthop replace id 83 blackhole"
1037	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 2001:db8:101::1"
1038	log_test $? 2 "Ping - gateway replaced by blackhole"
1039
1040	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
1041	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 2001:db8:101::1"
1042	if [ $? -eq 0 ]; then
1043		run_cmd "$IP nexthop replace id 122 group 83"
1044		run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 2001:db8:101::1"
1045		log_test $? 2 "Ping - group with blackhole"
1046
1047		run_cmd "$IP nexthop replace id 122 group 81/82"
1048		run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 2001:db8:101::1"
1049		log_test $? 0 "Ping - group blackhole replaced with gateways"
1050	else
1051		log_test 2 0 "Ping - multipath failed"
1052	fi
1053
1054	#
1055	# device only and gw + dev only mix
1056	#
1057	run_cmd "$IP -6 nexthop add id 85 dev veth1"
1058	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 85"
1059	log_test $? 0 "IPv6 route with device only nexthop"
1060	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 85 dev veth1 metric 1024"
1061
1062	run_cmd "$IP nexthop add id 123 group 81/85"
1063	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 123"
1064	log_test $? 0 "IPv6 multipath route with nexthop mix - dev only + gw"
1065	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 123 metric 1024 nexthop via 2001:db8:91::2 dev veth1 weight 1 nexthop dev veth1 weight 1"
1066
1067	#
1068	# IPv6 route with v4 nexthop - not allowed
1069	#
1070	run_cmd "$IP ro delete 2001:db8:101::1/128"
1071	run_cmd "$IP nexthop add id 84 via 172.16.1.1 dev veth1"
1072	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 84"
1073	log_test $? 2 "IPv6 route can not have a v4 gateway"
1074
1075	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 81"
1076	run_cmd "$IP nexthop replace id 81 via 172.16.1.1 dev veth1"
1077	log_test $? 2 "Nexthop replace - v6 route, v4 nexthop"
1078
1079	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
1080	run_cmd "$IP nexthop replace id 81 via 172.16.1.1 dev veth1"
1081	log_test $? 2 "Nexthop replace of group entry - v6 route, v4 nexthop"
1082
1083	run_cmd "$IP nexthop add id 86 via 2001:db8:92::2 dev veth3"
1084	run_cmd "$IP nexthop add id 87 via 172.16.1.1 dev veth1"
1085	run_cmd "$IP nexthop add id 88 via 172.16.1.1 dev veth1"
1086	run_cmd "$IP nexthop add id 124 group 86/87/88"
1087	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
1088	log_test $? 2 "IPv6 route can not have a group with v4 and v6 gateways"
1089
1090	run_cmd "$IP nexthop del id 88"
1091	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
1092	log_test $? 2 "IPv6 route can not have a group with v4 and v6 gateways"
1093
1094	run_cmd "$IP nexthop del id 87"
1095	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
1096	log_test $? 0 "IPv6 route using a group after removing v4 gateways"
1097
1098	run_cmd "$IP ro delete 2001:db8:101::1/128"
1099	run_cmd "$IP nexthop add id 87 via 172.16.1.1 dev veth1"
1100	run_cmd "$IP nexthop add id 88 via 172.16.1.1 dev veth1"
1101	run_cmd "$IP nexthop replace id 124 group 86/87/88"
1102	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
1103	log_test $? 2 "IPv6 route can not have a group with v4 and v6 gateways"
1104
1105	run_cmd "$IP nexthop replace id 88 via 2001:db8:92::2 dev veth3"
1106	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
1107	log_test $? 2 "IPv6 route can not have a group with v4 and v6 gateways"
1108
1109	run_cmd "$IP nexthop replace id 87 via 2001:db8:92::2 dev veth3"
1110	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
1111	log_test $? 0 "IPv6 route using a group after replacing v4 gateways"
1112
1113	$IP nexthop flush >/dev/null 2>&1
1114
1115	#
1116	# weird IPv6 cases
1117	#
1118	run_cmd "$IP nexthop add id 86 via 2001:db8:91::2 dev veth1"
1119	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
1120
1121	# route can not use prefsrc with nexthops
1122	run_cmd "$IP ro add 2001:db8:101::2/128 nhid 86 from 2001:db8:91::1"
1123	log_test $? 2 "IPv6 route can not use src routing with external nexthop"
1124
1125	# check cleanup path on invalid metric
1126	run_cmd "$IP ro add 2001:db8:101::2/128 nhid 86 congctl lock foo"
1127	log_test $? 2 "IPv6 route with invalid metric"
1128
1129	# rpfilter and default route
1130	$IP nexthop flush >/dev/null 2>&1
1131	run_cmd "ip netns exec me ip6tables -t mangle -I PREROUTING 1 -m rpfilter --invert -j DROP"
1132	run_cmd "$IP nexthop add id 91 via 2001:db8:91::2 dev veth1"
1133	run_cmd "$IP nexthop add id 92 via 2001:db8:92::2 dev veth3"
1134	run_cmd "$IP nexthop add id 93 group 91/92"
1135	run_cmd "$IP -6 ro add default nhid 91"
1136	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 2001:db8:101::1"
1137	log_test $? 0 "Nexthop with default route and rpfilter"
1138	run_cmd "$IP -6 ro replace default nhid 93"
1139	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 2001:db8:101::1"
1140	log_test $? 0 "Nexthop with multipath default route and rpfilter"
1141
1142	# TO-DO:
1143	# existing route with old nexthop; append route with new nexthop
1144	# existing route with old nexthop; replace route with new
1145	# existing route with new nexthop; replace route with old
1146	# route with src address and using nexthop - not allowed
1147}
1148
1149ipv6_large_grp()
1150{
1151	local ecmp=32
1152
1153	echo
1154	echo "IPv6 large groups (x$ecmp)"
1155	echo "---------------------"
1156
1157	check_large_grp 6 $ecmp
1158
1159	$IP nexthop flush >/dev/null 2>&1
1160}
1161
1162ipv6_large_res_grp()
1163{
1164	echo
1165	echo "IPv6 large resilient group (128k buckets)"
1166	echo "-----------------------------------------"
1167
1168	check_nexthop_res_support
1169	if [ $? -eq $ksft_skip ]; then
1170		return $ksft_skip
1171	fi
1172
1173	check_large_res_grp 6 $((128 * 1024))
1174
1175	$IP nexthop flush >/dev/null 2>&1
1176}
1177
1178ipv6_del_add_loop1()
1179{
1180	while :; do
1181		$IP nexthop del id 100
1182		$IP nexthop add id 100 via 2001:db8:91::2 dev veth1
1183	done >/dev/null 2>&1
1184}
1185
1186ipv6_grp_replace_loop()
1187{
1188	while :; do
1189		$IP nexthop replace id 102 group 100/101
1190	done >/dev/null 2>&1
1191}
1192
1193ipv6_torture()
1194{
1195	local pid1
1196	local pid2
1197	local pid3
1198	local pid4
1199	local pid5
1200
1201	echo
1202	echo "IPv6 runtime torture"
1203	echo "--------------------"
1204	if [ ! -x "$(command -v mausezahn)" ]; then
1205		echo "SKIP: Could not run test; need mausezahn tool"
1206		return
1207	fi
1208
1209	run_cmd "$IP nexthop add id 100 via 2001:db8:91::2 dev veth1"
1210	run_cmd "$IP nexthop add id 101 via 2001:db8:92::2 dev veth3"
1211	run_cmd "$IP nexthop add id 102 group 100/101"
1212	run_cmd "$IP route add 2001:db8:101::1 nhid 102"
1213	run_cmd "$IP route add 2001:db8:101::2 nhid 102"
1214
1215	ipv6_del_add_loop1 &
1216	pid1=$!
1217	ipv6_grp_replace_loop &
1218	pid2=$!
1219	ip netns exec me ping -f 2001:db8:101::1 >/dev/null 2>&1 &
1220	pid3=$!
1221	ip netns exec me ping -f 2001:db8:101::2 >/dev/null 2>&1 &
1222	pid4=$!
1223	ip netns exec me mausezahn -6 veth1 -B 2001:db8:101::2 -A 2001:db8:91::1 -c 0 -t tcp "dp=1-1023, flags=syn" >/dev/null 2>&1 &
1224	pid5=$!
1225
1226	sleep 300
1227	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
1228	wait $pid1 $pid2 $pid3 $pid4 $pid5 2>/dev/null
1229
1230	# if we did not crash, success
1231	log_test 0 0 "IPv6 torture test"
1232}
1233
1234ipv6_res_grp_replace_loop()
1235{
1236	while :; do
1237		$IP nexthop replace id 102 group 100/101 type resilient
1238	done >/dev/null 2>&1
1239}
1240
1241ipv6_res_torture()
1242{
1243	local pid1
1244	local pid2
1245	local pid3
1246	local pid4
1247	local pid5
1248
1249	echo
1250	echo "IPv6 runtime resilient nexthop group torture"
1251	echo "--------------------------------------------"
1252
1253	check_nexthop_res_support
1254	if [ $? -eq $ksft_skip ]; then
1255		return $ksft_skip
1256	fi
1257
1258	if [ ! -x "$(command -v mausezahn)" ]; then
1259		echo "SKIP: Could not run test; need mausezahn tool"
1260		return
1261	fi
1262
1263	run_cmd "$IP nexthop add id 100 via 2001:db8:91::2 dev veth1"
1264	run_cmd "$IP nexthop add id 101 via 2001:db8:92::2 dev veth3"
1265	run_cmd "$IP nexthop add id 102 group 100/101 type resilient buckets 512 idle_timer 0"
1266	run_cmd "$IP route add 2001:db8:101::1 nhid 102"
1267	run_cmd "$IP route add 2001:db8:101::2 nhid 102"
1268
1269	ipv6_del_add_loop1 &
1270	pid1=$!
1271	ipv6_res_grp_replace_loop &
1272	pid2=$!
1273	ip netns exec me ping -f 2001:db8:101::1 >/dev/null 2>&1 &
1274	pid3=$!
1275	ip netns exec me ping -f 2001:db8:101::2 >/dev/null 2>&1 &
1276	pid4=$!
1277	ip netns exec me mausezahn -6 veth1 \
1278			    -B 2001:db8:101::2 -A 2001:db8:91::1 -c 0 \
1279			    -t tcp "dp=1-1023, flags=syn" >/dev/null 2>&1 &
1280	pid5=$!
1281
1282	sleep 300
1283	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
1284	wait $pid1 $pid2 $pid3 $pid4 $pid5 2>/dev/null
1285
1286	# if we did not crash, success
1287	log_test 0 0 "IPv6 resilient nexthop group torture test"
1288}
1289
1290ipv4_fcnal()
1291{
1292	local rc
1293
1294	echo
1295	echo "IPv4 functional"
1296	echo "----------------------"
1297
1298	#
1299	# basic IPv4 ops - add, get, delete
1300	#
1301	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1302	rc=$?
1303	log_test $rc 0 "Create nexthop with id, gw, dev"
1304	if [ $rc -ne 0 ]; then
1305		echo "Basic IPv4 create fails; can not continue"
1306		return 1
1307	fi
1308
1309	run_cmd "$IP nexthop get id 12"
1310	log_test $? 0 "Get nexthop by id"
1311	check_nexthop "id 12" "id 12 via 172.16.1.2 dev veth1 scope link"
1312
1313	run_cmd "$IP nexthop del id 12"
1314	log_test $? 0 "Delete nexthop by id"
1315	check_nexthop "id 52" ""
1316
1317	#
1318	# gw, device spec
1319	#
1320	# gw validation, no device - fails since dev is required
1321	run_cmd "$IP nexthop add id 12 via 172.16.2.3"
1322	log_test $? 2 "Create nexthop - gw only"
1323
1324	# gw not reachable through given dev
1325	run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1"
1326	log_test $? 2 "Create nexthop - invalid gw+dev combination"
1327
1328	# onlink flag overrides gw+dev lookup
1329	run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1 onlink"
1330	log_test $? 0 "Create nexthop - gw+dev and onlink"
1331
1332	# admin down should delete nexthops
1333	set -e
1334	run_cmd "$IP nexthop add id 15 via 172.16.1.3 dev veth1"
1335	run_cmd "$IP nexthop add id 16 via 172.16.1.4 dev veth1"
1336	run_cmd "$IP nexthop add id 17 via 172.16.1.5 dev veth1"
1337	run_cmd "$IP li set dev veth1 down"
1338	set +e
1339	check_nexthop "dev veth1" ""
1340	log_test $? 0 "Nexthops removed on admin down"
1341
1342	# nexthop route delete warning: route add with nhid and delete
1343	# using device
1344	run_cmd "$IP li set dev veth1 up"
1345	run_cmd "$IP nexthop add id 12 via 172.16.1.3 dev veth1"
1346	out1=`dmesg | grep "WARNING:.*fib_nh_match.*" | wc -l`
1347	run_cmd "$IP route add 172.16.101.1/32 nhid 12"
1348	run_cmd "$IP route delete 172.16.101.1/32 dev veth1"
1349	out2=`dmesg | grep "WARNING:.*fib_nh_match.*" | wc -l`
1350	[ $out1 -eq $out2 ]
1351	rc=$?
1352	log_test $rc 0 "Delete nexthop route warning"
1353	run_cmd "$IP route delete 172.16.101.1/32 nhid 12"
1354	run_cmd "$IP nexthop del id 12"
1355
1356	run_cmd "$IP nexthop add id 21 via 172.16.1.6 dev veth1"
1357	run_cmd "$IP ro add 172.16.101.0/24 nhid 21"
1358	run_cmd "$IP ro del 172.16.101.0/24 nexthop via 172.16.1.7 dev veth1 nexthop via 172.16.1.8 dev veth1"
1359	log_test $? 2 "Delete multipath route with only nh id based entry"
1360
1361	run_cmd "$IP nexthop add id 22 via 172.16.1.6 dev veth1"
1362	run_cmd "$IP ro add 172.16.102.0/24 nhid 22"
1363	run_cmd "$IP ro del 172.16.102.0/24 dev veth1"
1364	log_test $? 2 "Delete route when specifying only nexthop device"
1365
1366	run_cmd "$IP ro del 172.16.102.0/24 via 172.16.1.6"
1367	log_test $? 2 "Delete route when specifying only gateway"
1368
1369	run_cmd "$IP ro del 172.16.102.0/24"
1370	log_test $? 0 "Delete route when not specifying nexthop attributes"
1371}
1372
1373ipv4_grp_fcnal()
1374{
1375	local rc
1376
1377	echo
1378	echo "IPv4 groups functional"
1379	echo "----------------------"
1380
1381	# basic functionality: create a nexthop group, default weight
1382	run_cmd "$IP nexthop add id 11 via 172.16.1.2 dev veth1"
1383	run_cmd "$IP nexthop add id 101 group 11"
1384	log_test $? 0 "Create nexthop group with single nexthop"
1385
1386	# get nexthop group
1387	run_cmd "$IP nexthop get id 101"
1388	log_test $? 0 "Get nexthop group by id"
1389	check_nexthop "id 101" "id 101 group 11"
1390
1391	# delete nexthop group
1392	run_cmd "$IP nexthop del id 101"
1393	log_test $? 0 "Delete nexthop group by id"
1394	check_nexthop "id 101" ""
1395
1396	$IP nexthop flush >/dev/null 2>&1
1397
1398	#
1399	# create group with multiple nexthops
1400	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1401	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1402	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
1403	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
1404	run_cmd "$IP nexthop add id 102 group 12/13/14/15"
1405	log_test $? 0 "Nexthop group with multiple nexthops"
1406	check_nexthop "id 102" "id 102 group 12/13/14/15"
1407
1408	# Delete nexthop in a group and group is updated
1409	run_cmd "$IP nexthop del id 13"
1410	check_nexthop "id 102" "id 102 group 12/14/15"
1411	log_test $? 0 "Nexthop group updated when entry is deleted"
1412
1413	# create group with multiple weighted nexthops
1414	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1415	run_cmd "$IP nexthop add id 103 group 12/13,2/14,3/15,4"
1416	log_test $? 0 "Nexthop group with weighted nexthops"
1417	check_nexthop "id 103" "id 103 group 12/13,2/14,3/15,4"
1418
1419	# Delete nexthop in a weighted group and group is updated
1420	run_cmd "$IP nexthop del id 13"
1421	check_nexthop "id 103" "id 103 group 12/14,3/15,4"
1422	log_test $? 0 "Weighted nexthop group updated when entry is deleted"
1423
1424	# admin down - nexthop is removed from group
1425	run_cmd "$IP li set dev veth1 down"
1426	check_nexthop "dev veth1" ""
1427	log_test $? 0 "Nexthops in groups removed on admin down"
1428
1429	# expect groups to have been deleted as well
1430	check_nexthop "" ""
1431
1432	run_cmd "$IP li set dev veth1 up"
1433
1434	$IP nexthop flush >/dev/null 2>&1
1435
1436	# group with nexthops using different devices
1437	set -e
1438	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1439	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1440	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
1441	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
1442
1443	run_cmd "$IP nexthop add id 22 via 172.16.2.2 dev veth3"
1444	run_cmd "$IP nexthop add id 23 via 172.16.2.3 dev veth3"
1445	run_cmd "$IP nexthop add id 24 via 172.16.2.4 dev veth3"
1446	run_cmd "$IP nexthop add id 25 via 172.16.2.5 dev veth3"
1447	set +e
1448
1449	# multiple groups with same nexthop
1450	run_cmd "$IP nexthop add id 104 group 12"
1451	run_cmd "$IP nexthop add id 105 group 12"
1452	check_nexthop "group" "id 104 group 12 id 105 group 12"
1453	log_test $? 0 "Multiple groups with same nexthop"
1454
1455	run_cmd "$IP nexthop flush groups"
1456	[ $? -ne 0 ] && return 1
1457
1458	# on admin down of veth1, it should be removed from the group
1459	run_cmd "$IP nexthop add id 105 group 12/13/22/23/14"
1460	run_cmd "$IP li set veth1 down"
1461	check_nexthop "id 105" "id 105 group 22/23"
1462	log_test $? 0 "Nexthops in group removed on admin down - mixed group"
1463
1464	run_cmd "$IP nexthop add id 106 group 105/24"
1465	log_test $? 2 "Nexthop group can not have a group as an entry"
1466
1467	# a group can have a blackhole entry only if it is the only
1468	# nexthop in the group. Needed for atomic replace with an
1469	# actual nexthop group
1470	run_cmd "$IP nexthop add id 31 blackhole"
1471	run_cmd "$IP nexthop add id 107 group 31"
1472	log_test $? 0 "Nexthop group with a blackhole entry"
1473
1474	run_cmd "$IP nexthop add id 108 group 31/24"
1475	log_test $? 2 "Nexthop group can not have a blackhole and another nexthop"
1476}
1477
1478ipv4_res_grp_fcnal()
1479{
1480	local rc
1481
1482	echo
1483	echo "IPv4 resilient groups functional"
1484	echo "--------------------------------"
1485
1486	check_nexthop_res_support
1487	if [ $? -eq $ksft_skip ]; then
1488		return $ksft_skip
1489	fi
1490
1491	#
1492	# migration of nexthop buckets - equal weights
1493	#
1494	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1495	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1496	run_cmd "$IP nexthop add id 102 group 12/13 type resilient buckets 2 idle_timer 0"
1497
1498	run_cmd "$IP nexthop del id 13"
1499	check_nexthop "id 102" \
1500		"id 102 group 12 type resilient buckets 2 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
1501	log_test $? 0 "Nexthop group updated when entry is deleted"
1502	check_nexthop_bucket "list id 102" \
1503		"id 102 index 0 nhid 12 id 102 index 1 nhid 12"
1504	log_test $? 0 "Nexthop buckets updated when entry is deleted"
1505
1506	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1507	run_cmd "$IP nexthop replace id 102 group 12/13 type resilient buckets 2 idle_timer 0"
1508	check_nexthop "id 102" \
1509		"id 102 group 12/13 type resilient buckets 2 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
1510	log_test $? 0 "Nexthop group updated after replace"
1511	check_nexthop_bucket "list id 102" \
1512		"id 102 index 0 nhid 13 id 102 index 1 nhid 12"
1513	log_test $? 0 "Nexthop buckets updated after replace"
1514
1515	$IP nexthop flush >/dev/null 2>&1
1516
1517	#
1518	# migration of nexthop buckets - unequal weights
1519	#
1520	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1521	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1522	run_cmd "$IP nexthop add id 102 group 12,3/13,1 type resilient buckets 4 idle_timer 0"
1523
1524	run_cmd "$IP nexthop del id 13"
1525	check_nexthop "id 102" \
1526		"id 102 group 12,3 type resilient buckets 4 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
1527	log_test $? 0 "Nexthop group updated when entry is deleted - nECMP"
1528	check_nexthop_bucket "list id 102" \
1529		"id 102 index 0 nhid 12 id 102 index 1 nhid 12 id 102 index 2 nhid 12 id 102 index 3 nhid 12"
1530	log_test $? 0 "Nexthop buckets updated when entry is deleted - nECMP"
1531
1532	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1533	run_cmd "$IP nexthop replace id 102 group 12,3/13,1 type resilient buckets 4 idle_timer 0"
1534	check_nexthop "id 102" \
1535		"id 102 group 12,3/13 type resilient buckets 4 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
1536	log_test $? 0 "Nexthop group updated after replace - nECMP"
1537	check_nexthop_bucket "list id 102" \
1538		"id 102 index 0 nhid 13 id 102 index 1 nhid 12 id 102 index 2 nhid 12 id 102 index 3 nhid 12"
1539	log_test $? 0 "Nexthop buckets updated after replace - nECMP"
1540}
1541
1542ipv4_withv6_fcnal()
1543{
1544	local lladdr
1545
1546	set -e
1547	lladdr=$(get_linklocal veth2 peer)
1548	run_cmd "$IP nexthop add id 11 via ${lladdr} dev veth1"
1549	set +e
1550	run_cmd "$IP ro add 172.16.101.1/32 nhid 11"
1551	log_test $? 0 "IPv6 nexthop with IPv4 route"
1552	check_route "172.16.101.1" "172.16.101.1 nhid 11 via inet6 ${lladdr} dev veth1"
1553
1554	set -e
1555	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1556	run_cmd "$IP nexthop add id 101 group 11/12"
1557	set +e
1558	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
1559	log_test $? 0 "IPv6 nexthop with IPv4 route"
1560
1561	check_route "172.16.101.1" "172.16.101.1 nhid 101 nexthop via inet6 ${lladdr} dev veth1 weight 1 nexthop via 172.16.1.2 dev veth1 weight 1"
1562
1563	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1564	log_test $? 0 "IPv4 route with IPv6 gateway"
1565	check_route "172.16.101.1" "172.16.101.1 via inet6 ${lladdr} dev veth1"
1566
1567	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 2001:db8:50::1 dev veth1"
1568	log_test $? 2 "IPv4 route with invalid IPv6 gateway"
1569}
1570
1571ipv4_fcnal_runtime()
1572{
1573	local lladdr
1574	local rc
1575
1576	echo
1577	echo "IPv4 functional runtime"
1578	echo "-----------------------"
1579
1580	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
1581	run_cmd "$IP ro add 172.16.101.1/32 nhid 21"
1582	log_test $? 0 "Route add"
1583	check_route "172.16.101.1" "172.16.101.1 nhid 21 via 172.16.1.2 dev veth1"
1584
1585	run_cmd "$IP ro delete 172.16.101.1/32 nhid 21"
1586	log_test $? 0 "Route delete"
1587
1588	#
1589	# scope mismatch
1590	#
1591	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
1592	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
1593	log_test $? 2 "Route add - scope conflict with nexthop"
1594
1595	run_cmd "$IP nexthop replace id 22 dev veth3"
1596	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
1597	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
1598	log_test $? 2 "Nexthop replace with invalid scope for existing route"
1599
1600	# check cleanup path on invalid metric
1601	run_cmd "$IP ro add 172.16.101.2/32 nhid 22 congctl lock foo"
1602	log_test $? 2 "IPv4 route with invalid metric"
1603
1604	#
1605	# add route with nexthop and check traffic
1606	#
1607	run_cmd "$IP nexthop replace id 21 via 172.16.1.2 dev veth1"
1608	run_cmd "$IP ro replace 172.16.101.1/32 nhid 21"
1609	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1610	log_test $? 0 "Basic ping"
1611
1612	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
1613	run_cmd "$IP nexthop add id 122 group 21/22"
1614	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
1615	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1616	log_test $? 0 "Ping - multipath"
1617
1618	run_cmd "$IP ro delete 172.16.101.1/32 nhid 122"
1619
1620	#
1621	# multiple default routes
1622	# - tests fib_select_default
1623	run_cmd "$IP nexthop add id 501 via 172.16.1.2 dev veth1"
1624	run_cmd "$IP ro add default nhid 501"
1625	run_cmd "$IP ro add default via 172.16.1.3 dev veth1 metric 20"
1626	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1627	log_test $? 0 "Ping - multiple default routes, nh first"
1628
1629	# flip the order
1630	run_cmd "$IP ro del default nhid 501"
1631	run_cmd "$IP ro del default via 172.16.1.3 dev veth1 metric 20"
1632	run_cmd "$IP ro add default via 172.16.1.2 dev veth1 metric 20"
1633	run_cmd "$IP nexthop replace id 501 via 172.16.1.3 dev veth1"
1634	run_cmd "$IP ro add default nhid 501 metric 20"
1635	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1636	log_test $? 0 "Ping - multiple default routes, nh second"
1637
1638	run_cmd "$IP nexthop delete nhid 501"
1639	run_cmd "$IP ro del default"
1640
1641	#
1642	# IPv4 with blackhole nexthops
1643	#
1644	run_cmd "$IP nexthop add id 23 blackhole"
1645	run_cmd "$IP ro replace 172.16.101.1/32 nhid 23"
1646	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1647	log_test $? 2 "Ping - blackhole"
1648
1649	run_cmd "$IP nexthop replace id 23 via 172.16.1.2 dev veth1"
1650	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1651	log_test $? 0 "Ping - blackhole replaced with gateway"
1652
1653	run_cmd "$IP nexthop replace id 23 blackhole"
1654	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1655	log_test $? 2 "Ping - gateway replaced by blackhole"
1656
1657	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
1658	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1659	if [ $? -eq 0 ]; then
1660		run_cmd "$IP nexthop replace id 122 group 23"
1661		run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1662		log_test $? 2 "Ping - group with blackhole"
1663
1664		run_cmd "$IP nexthop replace id 122 group 21/22"
1665		run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1666		log_test $? 0 "Ping - group blackhole replaced with gateways"
1667	else
1668		log_test 2 0 "Ping - multipath failed"
1669	fi
1670
1671	#
1672	# device only and gw + dev only mix
1673	#
1674	run_cmd "$IP nexthop add id 85 dev veth1"
1675	run_cmd "$IP ro replace 172.16.101.1/32 nhid 85"
1676	log_test $? 0 "IPv4 route with device only nexthop"
1677	check_route "172.16.101.1" "172.16.101.1 nhid 85 dev veth1"
1678
1679	run_cmd "$IP nexthop add id 123 group 21/85"
1680	run_cmd "$IP ro replace 172.16.101.1/32 nhid 123"
1681	log_test $? 0 "IPv4 multipath route with nexthop mix - dev only + gw"
1682	check_route "172.16.101.1" "172.16.101.1 nhid 123 nexthop via 172.16.1.2 dev veth1 weight 1 nexthop dev veth1 weight 1"
1683
1684	#
1685	# IPv4 with IPv6
1686	#
1687	set -e
1688	lladdr=$(get_linklocal veth2 peer)
1689	run_cmd "$IP nexthop add id 24 via ${lladdr} dev veth1"
1690	set +e
1691	run_cmd "$IP ro replace 172.16.101.1/32 nhid 24"
1692	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1693	log_test $? 0 "IPv6 nexthop with IPv4 route"
1694
1695	$IP neigh sh | grep -q "${lladdr} dev veth1"
1696	if [ $? -eq 1 ]; then
1697		echo "    WARNING: Neigh entry missing for ${lladdr}"
1698		$IP neigh sh | grep 'dev veth1'
1699	fi
1700
1701	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
1702	if [ $? -eq 0 ]; then
1703		echo "    WARNING: Neigh entry exists for 172.16.101.1"
1704		$IP neigh sh | grep 'dev veth1'
1705	fi
1706
1707	set -e
1708	run_cmd "$IP nexthop add id 25 via 172.16.1.2 dev veth1"
1709	run_cmd "$IP nexthop add id 101 group 24/25"
1710	set +e
1711	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
1712	log_test $? 0 "IPv4 route with mixed v4-v6 multipath route"
1713
1714	check_route "172.16.101.1" "172.16.101.1 nhid 101 nexthop via inet6 ${lladdr} dev veth1 weight 1 nexthop via 172.16.1.2 dev veth1 weight 1"
1715
1716	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1717	log_test $? 0 "IPv6 nexthop with IPv4 route"
1718
1719	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1720	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1721	log_test $? 0 "IPv4 route with IPv6 gateway"
1722
1723	$IP neigh sh | grep -q "${lladdr} dev veth1"
1724	if [ $? -eq 1 ]; then
1725		echo "    WARNING: Neigh entry missing for ${lladdr}"
1726		$IP neigh sh | grep 'dev veth1'
1727	fi
1728
1729	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
1730	if [ $? -eq 0 ]; then
1731		echo "    WARNING: Neigh entry exists for 172.16.101.1"
1732		$IP neigh sh | grep 'dev veth1'
1733	fi
1734
1735	run_cmd "$IP ro del 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1736	run_cmd "$IP -4 ro add default via inet6 ${lladdr} dev veth1"
1737	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1738	log_test $? 0 "IPv4 default route with IPv6 gateway"
1739
1740	#
1741	# MPLS as an example of LWT encap
1742	#
1743	run_cmd "$IP nexthop add id 51 encap mpls 101 via 172.16.1.2 dev veth1"
1744	log_test $? 0 "IPv4 route with MPLS encap"
1745	check_nexthop "id 51" "id 51 encap mpls 101 via 172.16.1.2 dev veth1 scope link"
1746	log_test $? 0 "IPv4 route with MPLS encap - check"
1747
1748	run_cmd "$IP nexthop add id 52 encap mpls 102 via inet6 2001:db8:91::2 dev veth1"
1749	log_test $? 0 "IPv4 route with MPLS encap and v6 gateway"
1750	check_nexthop "id 52" "id 52 encap mpls 102 via 2001:db8:91::2 dev veth1 scope link"
1751	log_test $? 0 "IPv4 route with MPLS encap, v6 gw - check"
1752}
1753
1754ipv4_large_grp()
1755{
1756	local ecmp=32
1757
1758	echo
1759	echo "IPv4 large groups (x$ecmp)"
1760	echo "---------------------"
1761
1762	check_large_grp 4 $ecmp
1763
1764	$IP nexthop flush >/dev/null 2>&1
1765}
1766
1767ipv4_large_res_grp()
1768{
1769	echo
1770	echo "IPv4 large resilient group (128k buckets)"
1771	echo "-----------------------------------------"
1772
1773	check_nexthop_res_support
1774	if [ $? -eq $ksft_skip ]; then
1775		return $ksft_skip
1776	fi
1777
1778	check_large_res_grp 4 $((128 * 1024))
1779
1780	$IP nexthop flush >/dev/null 2>&1
1781}
1782
1783sysctl_nexthop_compat_mode_check()
1784{
1785	local sysctlname="net.ipv4.nexthop_compat_mode"
1786	local lprefix=$1
1787
1788	IPE="ip netns exec me"
1789
1790	$IPE sysctl -q $sysctlname 2>&1 >/dev/null
1791	if [ $? -ne 0 ]; then
1792		echo "SKIP: kernel lacks nexthop compat mode sysctl control"
1793		return $ksft_skip
1794	fi
1795
1796	out=$($IPE sysctl $sysctlname 2>/dev/null)
1797	log_test $? 0 "$lprefix default nexthop compat mode check"
1798	check_output "${out}" "$sysctlname = 1"
1799}
1800
1801sysctl_nexthop_compat_mode_set()
1802{
1803	local sysctlname="net.ipv4.nexthop_compat_mode"
1804	local mode=$1
1805	local lprefix=$2
1806
1807	IPE="ip netns exec me"
1808
1809	out=$($IPE sysctl -w $sysctlname=$mode)
1810	log_test $? 0 "$lprefix set compat mode - $mode"
1811	check_output "${out}" "net.ipv4.nexthop_compat_mode = $mode"
1812}
1813
1814ipv6_compat_mode()
1815{
1816	local rc
1817
1818	echo
1819	echo "IPv6 nexthop api compat mode test"
1820	echo "--------------------------------"
1821
1822	sysctl_nexthop_compat_mode_check "IPv6"
1823	if [ $? -eq $ksft_skip ]; then
1824		return $ksft_skip
1825	fi
1826
1827	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
1828	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
1829	run_cmd "$IP nexthop add id 122 group 62/63"
1830	ipmout=$(start_ip_monitor route)
1831
1832	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 122"
1833	# route add notification should contain expanded nexthops
1834	stop_ip_monitor $ipmout 3
1835	log_test $? 0 "IPv6 compat mode on - route add notification"
1836
1837	# route dump should contain expanded nexthops
1838	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 122 metric 1024 nexthop via 2001:db8:91::2 dev veth1 weight 1 nexthop via 2001:db8:91::3 dev veth1 weight 1"
1839	log_test $? 0 "IPv6 compat mode on - route dump"
1840
1841	# change in nexthop group should generate route notification
1842	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
1843	ipmout=$(start_ip_monitor route)
1844	run_cmd "$IP nexthop replace id 122 group 62/64"
1845	stop_ip_monitor $ipmout 3
1846
1847	log_test $? 0 "IPv6 compat mode on - nexthop change"
1848
1849	# set compat mode off
1850	sysctl_nexthop_compat_mode_set 0 "IPv6"
1851
1852	run_cmd "$IP -6 ro del 2001:db8:101::1/128 nhid 122"
1853
1854	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
1855	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
1856	run_cmd "$IP nexthop add id 122 group 62/63"
1857	ipmout=$(start_ip_monitor route)
1858
1859	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 122"
1860	# route add notification should not contain expanded nexthops
1861	stop_ip_monitor $ipmout 1
1862	log_test $? 0 "IPv6 compat mode off - route add notification"
1863
1864	# route dump should not contain expanded nexthops
1865	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 122 metric 1024"
1866	log_test $? 0 "IPv6 compat mode off - route dump"
1867
1868	# change in nexthop group should not generate route notification
1869	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
1870	ipmout=$(start_ip_monitor route)
1871	run_cmd "$IP nexthop replace id 122 group 62/64"
1872	stop_ip_monitor $ipmout 0
1873	log_test $? 0 "IPv6 compat mode off - nexthop change"
1874
1875	# nexthop delete should not generate route notification
1876	ipmout=$(start_ip_monitor route)
1877	run_cmd "$IP nexthop del id 122"
1878	stop_ip_monitor $ipmout 0
1879	log_test $? 0 "IPv6 compat mode off - nexthop delete"
1880
1881	# set compat mode back on
1882	sysctl_nexthop_compat_mode_set 1 "IPv6"
1883}
1884
1885ipv4_compat_mode()
1886{
1887	local rc
1888
1889	echo
1890	echo "IPv4 nexthop api compat mode"
1891	echo "----------------------------"
1892
1893	sysctl_nexthop_compat_mode_check "IPv4"
1894	if [ $? -eq $ksft_skip ]; then
1895		return $ksft_skip
1896	fi
1897
1898	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
1899	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
1900	run_cmd "$IP nexthop add id 122 group 21/22"
1901	ipmout=$(start_ip_monitor route)
1902
1903	run_cmd "$IP ro add 172.16.101.1/32 nhid 122"
1904	stop_ip_monitor $ipmout 3
1905
1906	# route add notification should contain expanded nexthops
1907	log_test $? 0 "IPv4 compat mode on - route add notification"
1908
1909	# route dump should contain expanded nexthops
1910	check_route "172.16.101.1" "172.16.101.1 nhid 122 nexthop via 172.16.1.2 dev veth1 weight 1 nexthop via 172.16.1.2 dev veth1 weight 1"
1911	log_test $? 0 "IPv4 compat mode on - route dump"
1912
1913	# change in nexthop group should generate route notification
1914	run_cmd "$IP nexthop add id 23 via 172.16.1.3 dev veth1"
1915	ipmout=$(start_ip_monitor route)
1916	run_cmd "$IP nexthop replace id 122 group 21/23"
1917	stop_ip_monitor $ipmout 3
1918	log_test $? 0 "IPv4 compat mode on - nexthop change"
1919
1920	sysctl_nexthop_compat_mode_set 0 "IPv4"
1921
1922	# cleanup
1923	run_cmd "$IP ro del 172.16.101.1/32 nhid 122"
1924
1925	ipmout=$(start_ip_monitor route)
1926	run_cmd "$IP ro add 172.16.101.1/32 nhid 122"
1927	stop_ip_monitor $ipmout 1
1928	# route add notification should not contain expanded nexthops
1929	log_test $? 0 "IPv4 compat mode off - route add notification"
1930
1931	# route dump should not contain expanded nexthops
1932	check_route "172.16.101.1" "172.16.101.1 nhid 122"
1933	log_test $? 0 "IPv4 compat mode off - route dump"
1934
1935	# change in nexthop group should not generate route notification
1936	ipmout=$(start_ip_monitor route)
1937	run_cmd "$IP nexthop replace id 122 group 21/22"
1938	stop_ip_monitor $ipmout 0
1939	log_test $? 0 "IPv4 compat mode off - nexthop change"
1940
1941	# nexthop delete should not generate route notification
1942	ipmout=$(start_ip_monitor route)
1943	run_cmd "$IP nexthop del id 122"
1944	stop_ip_monitor $ipmout 0
1945	log_test $? 0 "IPv4 compat mode off - nexthop delete"
1946
1947	sysctl_nexthop_compat_mode_set 1 "IPv4"
1948}
1949
1950ipv4_del_add_loop1()
1951{
1952	while :; do
1953		$IP nexthop del id 100
1954		$IP nexthop add id 100 via 172.16.1.2 dev veth1
1955	done >/dev/null 2>&1
1956}
1957
1958ipv4_grp_replace_loop()
1959{
1960	while :; do
1961		$IP nexthop replace id 102 group 100/101
1962	done >/dev/null 2>&1
1963}
1964
1965ipv4_torture()
1966{
1967	local pid1
1968	local pid2
1969	local pid3
1970	local pid4
1971	local pid5
1972
1973	echo
1974	echo "IPv4 runtime torture"
1975	echo "--------------------"
1976	if [ ! -x "$(command -v mausezahn)" ]; then
1977		echo "SKIP: Could not run test; need mausezahn tool"
1978		return
1979	fi
1980
1981	run_cmd "$IP nexthop add id 100 via 172.16.1.2 dev veth1"
1982	run_cmd "$IP nexthop add id 101 via 172.16.2.2 dev veth3"
1983	run_cmd "$IP nexthop add id 102 group 100/101"
1984	run_cmd "$IP route add 172.16.101.1 nhid 102"
1985	run_cmd "$IP route add 172.16.101.2 nhid 102"
1986
1987	ipv4_del_add_loop1 &
1988	pid1=$!
1989	ipv4_grp_replace_loop &
1990	pid2=$!
1991	ip netns exec me ping -f 172.16.101.1 >/dev/null 2>&1 &
1992	pid3=$!
1993	ip netns exec me ping -f 172.16.101.2 >/dev/null 2>&1 &
1994	pid4=$!
1995	ip netns exec me mausezahn veth1 -B 172.16.101.2 -A 172.16.1.1 -c 0 -t tcp "dp=1-1023, flags=syn" >/dev/null 2>&1 &
1996	pid5=$!
1997
1998	sleep 300
1999	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
2000	wait $pid1 $pid2 $pid3 $pid4 $pid5 2>/dev/null
2001
2002	# if we did not crash, success
2003	log_test 0 0 "IPv4 torture test"
2004}
2005
2006ipv4_res_grp_replace_loop()
2007{
2008	while :; do
2009		$IP nexthop replace id 102 group 100/101 type resilient
2010	done >/dev/null 2>&1
2011}
2012
2013ipv4_res_torture()
2014{
2015	local pid1
2016	local pid2
2017	local pid3
2018	local pid4
2019	local pid5
2020
2021	echo
2022	echo "IPv4 runtime resilient nexthop group torture"
2023	echo "--------------------------------------------"
2024
2025	check_nexthop_res_support
2026	if [ $? -eq $ksft_skip ]; then
2027		return $ksft_skip
2028	fi
2029
2030	if [ ! -x "$(command -v mausezahn)" ]; then
2031		echo "SKIP: Could not run test; need mausezahn tool"
2032		return
2033	fi
2034
2035	run_cmd "$IP nexthop add id 100 via 172.16.1.2 dev veth1"
2036	run_cmd "$IP nexthop add id 101 via 172.16.2.2 dev veth3"
2037	run_cmd "$IP nexthop add id 102 group 100/101 type resilient buckets 512 idle_timer 0"
2038	run_cmd "$IP route add 172.16.101.1 nhid 102"
2039	run_cmd "$IP route add 172.16.101.2 nhid 102"
2040
2041	ipv4_del_add_loop1 &
2042	pid1=$!
2043	ipv4_res_grp_replace_loop &
2044	pid2=$!
2045	ip netns exec me ping -f 172.16.101.1 >/dev/null 2>&1 &
2046	pid3=$!
2047	ip netns exec me ping -f 172.16.101.2 >/dev/null 2>&1 &
2048	pid4=$!
2049	ip netns exec me mausezahn veth1 \
2050				-B 172.16.101.2 -A 172.16.1.1 -c 0 \
2051				-t tcp "dp=1-1023, flags=syn" >/dev/null 2>&1 &
2052	pid5=$!
2053
2054	sleep 300
2055	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
2056	wait $pid1 $pid2 $pid3 $pid4 $pid5 2>/dev/null
2057
2058	# if we did not crash, success
2059	log_test 0 0 "IPv4 resilient nexthop group torture test"
2060}
2061
2062basic()
2063{
2064	echo
2065	echo "Basic functional tests"
2066	echo "----------------------"
2067	run_cmd "$IP nexthop ls"
2068	log_test $? 0 "List with nothing defined"
2069
2070	run_cmd "$IP nexthop get id 1"
2071	log_test $? 2 "Nexthop get on non-existent id"
2072
2073	# attempt to create nh without a device or gw - fails
2074	run_cmd "$IP nexthop add id 1"
2075	log_test $? 2 "Nexthop with no device or gateway"
2076
2077	# attempt to create nh with down device - fails
2078	$IP li set veth1 down
2079	run_cmd "$IP nexthop add id 1 dev veth1"
2080	log_test $? 2 "Nexthop with down device"
2081
2082	# create nh with linkdown device - fails
2083	$IP li set veth1 up
2084	ip -netns peer li set veth2 down
2085	run_cmd "$IP nexthop add id 1 dev veth1"
2086	log_test $? 2 "Nexthop with device that is linkdown"
2087	ip -netns peer li set veth2 up
2088
2089	# device only
2090	run_cmd "$IP nexthop add id 1 dev veth1"
2091	log_test $? 0 "Nexthop with device only"
2092
2093	# create nh with duplicate id
2094	run_cmd "$IP nexthop add id 1 dev veth3"
2095	log_test $? 2 "Nexthop with duplicate id"
2096
2097	# blackhole nexthop
2098	run_cmd "$IP nexthop add id 2 blackhole"
2099	log_test $? 0 "Blackhole nexthop"
2100
2101	# blackhole nexthop can not have other specs
2102	run_cmd "$IP nexthop replace id 2 blackhole dev veth1"
2103	log_test $? 2 "Blackhole nexthop with other attributes"
2104
2105	# blackhole nexthop should not be affected by the state of the loopback
2106	# device
2107	run_cmd "$IP link set dev lo down"
2108	check_nexthop "id 2" "id 2 blackhole"
2109	log_test $? 0 "Blackhole nexthop with loopback device down"
2110
2111	run_cmd "$IP link set dev lo up"
2112
2113	# Dump should not loop endlessly when maximum nexthop ID is configured.
2114	run_cmd "$IP nexthop add id $((2**32-1)) blackhole"
2115	run_cmd "timeout 5 $IP nexthop"
2116	log_test $? 0 "Maximum nexthop ID dump"
2117
2118	#
2119	# groups
2120	#
2121
2122	run_cmd "$IP nexthop add id 101 group 1"
2123	log_test $? 0 "Create group"
2124
2125	run_cmd "$IP nexthop add id 102 group 2"
2126	log_test $? 0 "Create group with blackhole nexthop"
2127
2128	# multipath group can not have a blackhole as 1 path
2129	run_cmd "$IP nexthop add id 103 group 1/2"
2130	log_test $? 2 "Create multipath group where 1 path is a blackhole"
2131
2132	# multipath group can not have a member replaced by a blackhole
2133	run_cmd "$IP nexthop replace id 2 dev veth3"
2134	run_cmd "$IP nexthop replace id 102 group 1/2"
2135	run_cmd "$IP nexthop replace id 2 blackhole"
2136	log_test $? 2 "Multipath group can not have a member replaced by blackhole"
2137
2138	# attempt to create group with non-existent nexthop
2139	run_cmd "$IP nexthop add id 103 group 12"
2140	log_test $? 2 "Create group with non-existent nexthop"
2141
2142	# attempt to create group with same nexthop
2143	run_cmd "$IP nexthop add id 103 group 1/1"
2144	log_test $? 2 "Create group with same nexthop multiple times"
2145
2146	# replace nexthop with a group - fails
2147	run_cmd "$IP nexthop replace id 2 group 1"
2148	log_test $? 2 "Replace nexthop with nexthop group"
2149
2150	# replace nexthop group with a nexthop - fails
2151	run_cmd "$IP nexthop replace id 101 dev veth1"
2152	log_test $? 2 "Replace nexthop group with nexthop"
2153
2154	# nexthop group with other attributes fail
2155	run_cmd "$IP nexthop add id 104 group 1 dev veth1"
2156	log_test $? 2 "Nexthop group and device"
2157
2158	# Tests to ensure that flushing works as expected.
2159	run_cmd "$IP nexthop add id 105 blackhole proto 99"
2160	run_cmd "$IP nexthop add id 106 blackhole proto 100"
2161	run_cmd "$IP nexthop add id 107 blackhole proto 99"
2162	run_cmd "$IP nexthop flush proto 99"
2163	check_nexthop "id 105" ""
2164	check_nexthop "id 106" "id 106 blackhole proto 100"
2165	check_nexthop "id 107" ""
2166	run_cmd "$IP nexthop flush proto 100"
2167	check_nexthop "id 106" ""
2168
2169	run_cmd "$IP nexthop flush proto 100"
2170	log_test $? 0 "Test proto flush"
2171
2172	run_cmd "$IP nexthop add id 104 group 1 blackhole"
2173	log_test $? 2 "Nexthop group and blackhole"
2174
2175	$IP nexthop flush >/dev/null 2>&1
2176
2177	# Test to ensure that flushing with a multi-part nexthop dump works as
2178	# expected.
2179	local batch_file=$(mktemp)
2180
2181	for i in $(seq 1 $((64 * 1024))); do
2182		echo "nexthop add id $i blackhole" >> $batch_file
2183	done
2184
2185	$IP -b $batch_file
2186	$IP nexthop flush >/dev/null 2>&1
2187	[[ $($IP nexthop | wc -l) -eq 0 ]]
2188	log_test $? 0 "Large scale nexthop flushing"
2189
2190	rm $batch_file
2191}
2192
2193check_nexthop_buckets_balance()
2194{
2195	local nharg=$1; shift
2196	local ret
2197
2198	while (($# > 0)); do
2199		local selector=$1; shift
2200		local condition=$1; shift
2201		local count
2202
2203		count=$($IP -j nexthop bucket ${nharg} ${selector} | jq length)
2204		(( $count $condition ))
2205		ret=$?
2206		if ((ret != 0)); then
2207			return $ret
2208		fi
2209	done
2210
2211	return 0
2212}
2213
2214basic_res()
2215{
2216	echo
2217	echo "Basic resilient nexthop group functional tests"
2218	echo "----------------------------------------------"
2219
2220	check_nexthop_res_support
2221	if [ $? -eq $ksft_skip ]; then
2222		return $ksft_skip
2223	fi
2224
2225	run_cmd "$IP nexthop add id 1 dev veth1"
2226
2227	#
2228	# resilient nexthop group addition
2229	#
2230
2231	run_cmd "$IP nexthop add id 101 group 1 type resilient buckets 8"
2232	log_test $? 0 "Add a nexthop group with default parameters"
2233
2234	run_cmd "$IP nexthop get id 101"
2235	check_nexthop "id 101" \
2236		"id 101 group 1 type resilient buckets 8 idle_timer 120 unbalanced_timer 0 unbalanced_time 0"
2237	log_test $? 0 "Get a nexthop group with default parameters"
2238
2239	run_cmd "$IP nexthop add id 102 group 1 type resilient
2240			buckets 4 idle_timer 100 unbalanced_timer 5"
2241	run_cmd "$IP nexthop get id 102"
2242	check_nexthop "id 102" \
2243		"id 102 group 1 type resilient buckets 4 idle_timer 100 unbalanced_timer 5 unbalanced_time 0"
2244	log_test $? 0 "Get a nexthop group with non-default parameters"
2245
2246	run_cmd "$IP nexthop add id 103 group 1 type resilient buckets 0"
2247	log_test $? 2 "Add a nexthop group with 0 buckets"
2248
2249	#
2250	# resilient nexthop group replacement
2251	#
2252
2253	run_cmd "$IP nexthop replace id 101 group 1 type resilient
2254			buckets 8 idle_timer 240 unbalanced_timer 80"
2255	log_test $? 0 "Replace nexthop group parameters"
2256	check_nexthop "id 101" \
2257		"id 101 group 1 type resilient buckets 8 idle_timer 240 unbalanced_timer 80 unbalanced_time 0"
2258	log_test $? 0 "Get a nexthop group after replacing parameters"
2259
2260	run_cmd "$IP nexthop replace id 101 group 1 type resilient idle_timer 512"
2261	log_test $? 0 "Replace idle timer"
2262	check_nexthop "id 101" \
2263		"id 101 group 1 type resilient buckets 8 idle_timer 512 unbalanced_timer 80 unbalanced_time 0"
2264	log_test $? 0 "Get a nexthop group after replacing idle timer"
2265
2266	run_cmd "$IP nexthop replace id 101 group 1 type resilient unbalanced_timer 256"
2267	log_test $? 0 "Replace unbalanced timer"
2268	check_nexthop "id 101" \
2269		"id 101 group 1 type resilient buckets 8 idle_timer 512 unbalanced_timer 256 unbalanced_time 0"
2270	log_test $? 0 "Get a nexthop group after replacing unbalanced timer"
2271
2272	run_cmd "$IP nexthop replace id 101 group 1 type resilient"
2273	log_test $? 0 "Replace with no parameters"
2274	check_nexthop "id 101" \
2275		"id 101 group 1 type resilient buckets 8 idle_timer 512 unbalanced_timer 256 unbalanced_time 0"
2276	log_test $? 0 "Get a nexthop group after replacing no parameters"
2277
2278	run_cmd "$IP nexthop replace id 101 group 1"
2279	log_test $? 2 "Replace nexthop group type - implicit"
2280
2281	run_cmd "$IP nexthop replace id 101 group 1 type mpath"
2282	log_test $? 2 "Replace nexthop group type - explicit"
2283
2284	run_cmd "$IP nexthop replace id 101 group 1 type resilient buckets 1024"
2285	log_test $? 2 "Replace number of nexthop buckets"
2286
2287	check_nexthop "id 101" \
2288		"id 101 group 1 type resilient buckets 8 idle_timer 512 unbalanced_timer 256 unbalanced_time 0"
2289	log_test $? 0 "Get a nexthop group after replacing with invalid parameters"
2290
2291	#
2292	# resilient nexthop buckets dump
2293	#
2294
2295	$IP nexthop flush >/dev/null 2>&1
2296	run_cmd "$IP nexthop add id 1 dev veth1"
2297	run_cmd "$IP nexthop add id 2 dev veth3"
2298	run_cmd "$IP nexthop add id 101 group 1/2 type resilient buckets 4"
2299	run_cmd "$IP nexthop add id 201 group 1/2"
2300
2301	check_nexthop_bucket "" \
2302		"id 101 index 0 nhid 2 id 101 index 1 nhid 2 id 101 index 2 nhid 1 id 101 index 3 nhid 1"
2303	log_test $? 0 "Dump all nexthop buckets"
2304
2305	check_nexthop_bucket "list id 101" \
2306		"id 101 index 0 nhid 2 id 101 index 1 nhid 2 id 101 index 2 nhid 1 id 101 index 3 nhid 1"
2307	log_test $? 0 "Dump all nexthop buckets in a group"
2308
2309	sleep 0.1
2310	(( $($IP -j nexthop bucket list id 101 |
2311	     jq '[.[] | select(.bucket.idle_time > 0 and
2312	                       .bucket.idle_time < 2)] | length') == 4 ))
2313	log_test $? 0 "All nexthop buckets report a positive near-zero idle time"
2314
2315	check_nexthop_bucket "list dev veth1" \
2316		"id 101 index 2 nhid 1 id 101 index 3 nhid 1"
2317	log_test $? 0 "Dump all nexthop buckets with a specific nexthop device"
2318
2319	check_nexthop_bucket "list nhid 2" \
2320		"id 101 index 0 nhid 2 id 101 index 1 nhid 2"
2321	log_test $? 0 "Dump all nexthop buckets with a specific nexthop identifier"
2322
2323	run_cmd "$IP nexthop bucket list id 111"
2324	log_test $? 2 "Dump all nexthop buckets in a non-existent group"
2325
2326	run_cmd "$IP nexthop bucket list id 201"
2327	log_test $? 2 "Dump all nexthop buckets in a non-resilient group"
2328
2329	run_cmd "$IP nexthop bucket list dev bla"
2330	log_test $? 255 "Dump all nexthop buckets using a non-existent device"
2331
2332	run_cmd "$IP nexthop bucket list groups"
2333	log_test $? 255 "Dump all nexthop buckets with invalid 'groups' keyword"
2334
2335	run_cmd "$IP nexthop bucket list fdb"
2336	log_test $? 255 "Dump all nexthop buckets with invalid 'fdb' keyword"
2337
2338	# Dump should not loop endlessly when maximum nexthop ID is configured.
2339	run_cmd "$IP nexthop add id $((2**32-1)) group 1/2 type resilient buckets 4"
2340	run_cmd "timeout 5 $IP nexthop bucket"
2341	log_test $? 0 "Maximum nexthop ID dump"
2342
2343	#
2344	# resilient nexthop buckets get requests
2345	#
2346
2347	check_nexthop_bucket "get id 101 index 0" "id 101 index 0 nhid 2"
2348	log_test $? 0 "Get a valid nexthop bucket"
2349
2350	run_cmd "$IP nexthop bucket get id 101 index 999"
2351	log_test $? 2 "Get a nexthop bucket with valid group, but invalid index"
2352
2353	run_cmd "$IP nexthop bucket get id 201 index 0"
2354	log_test $? 2 "Get a nexthop bucket from a non-resilient group"
2355
2356	run_cmd "$IP nexthop bucket get id 999 index 0"
2357	log_test $? 2 "Get a nexthop bucket from a non-existent group"
2358
2359	#
2360	# tests for bucket migration
2361	#
2362
2363	$IP nexthop flush >/dev/null 2>&1
2364
2365	run_cmd "$IP nexthop add id 1 dev veth1"
2366	run_cmd "$IP nexthop add id 2 dev veth3"
2367	run_cmd "$IP nexthop add id 101
2368			group 1/2 type resilient buckets 10
2369			idle_timer 1 unbalanced_timer 20"
2370
2371	check_nexthop_buckets_balance "list id 101" \
2372				      "nhid 1" "== 5" \
2373				      "nhid 2" "== 5"
2374	log_test $? 0 "Initial bucket allocation"
2375
2376	run_cmd "$IP nexthop replace id 101
2377			group 1,2/2,3 type resilient"
2378	check_nexthop_buckets_balance "list id 101" \
2379				      "nhid 1" "== 4" \
2380				      "nhid 2" "== 6"
2381	log_test $? 0 "Bucket allocation after replace"
2382
2383	# Check that increase in idle timer does not make buckets appear busy.
2384	run_cmd "$IP nexthop replace id 101
2385			group 1,2/2,3 type resilient
2386			idle_timer 10"
2387	run_cmd "$IP nexthop replace id 101
2388			group 1/2 type resilient"
2389	check_nexthop_buckets_balance "list id 101" \
2390				      "nhid 1" "== 5" \
2391				      "nhid 2" "== 5"
2392	log_test $? 0 "Buckets migrated after idle timer change"
2393
2394	$IP nexthop flush >/dev/null 2>&1
2395}
2396
2397################################################################################
2398# usage
2399
2400usage()
2401{
2402	cat <<EOF
2403usage: ${0##*/} OPTS
2404
2405        -t <test>   Test(s) to run (default: all)
2406                    (options: $ALL_TESTS)
2407        -4          IPv4 tests only
2408        -6          IPv6 tests only
2409        -p          Pause on fail
2410        -P          Pause after each test before cleanup
2411        -v          verbose mode (show commands and output)
2412	-w	    Timeout for ping
2413
2414    Runtime test
2415	-n num	    Number of nexthops to target
2416	-N    	    Use new style to install routes in DUT
2417
2418done
2419EOF
2420}
2421
2422################################################################################
2423# main
2424
2425while getopts :t:pP46hvw: o
2426do
2427	case $o in
2428		t) TESTS=$OPTARG;;
2429		4) TESTS=${IPV4_TESTS};;
2430		6) TESTS=${IPV6_TESTS};;
2431		p) PAUSE_ON_FAIL=yes;;
2432		P) PAUSE=yes;;
2433		v) VERBOSE=$(($VERBOSE + 1));;
2434		w) PING_TIMEOUT=$OPTARG;;
2435		h) usage; exit 0;;
2436		*) usage; exit 1;;
2437	esac
2438done
2439
2440# make sure we don't pause twice
2441[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
2442
2443if [ "$(id -u)" -ne 0 ];then
2444	echo "SKIP: Need root privileges"
2445	exit $ksft_skip;
2446fi
2447
2448if [ ! -x "$(command -v ip)" ]; then
2449	echo "SKIP: Could not run test without ip tool"
2450	exit $ksft_skip
2451fi
2452
2453ip help 2>&1 | grep -q nexthop
2454if [ $? -ne 0 ]; then
2455	echo "SKIP: iproute2 too old, missing nexthop command"
2456	exit $ksft_skip
2457fi
2458
2459out=$(ip nexthop ls 2>&1 | grep -q "Operation not supported")
2460if [ $? -eq 0 ]; then
2461	echo "SKIP: kernel lacks nexthop support"
2462	exit $ksft_skip
2463fi
2464
2465for t in $TESTS
2466do
2467	case $t in
2468	none) IP="ip -netns peer"; setup; exit 0;;
2469	*) setup; $t; cleanup;;
2470	esac
2471done
2472
2473if [ "$TESTS" != "none" ]; then
2474	printf "\nTests passed: %3d\n" ${nsuccess}
2475	printf "Tests failed: %3d\n"   ${nfail}
2476fi
2477
2478exit $ret
2479