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