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
1233ipv4_grp_fcnal()
1234{
1235	local rc
1236
1237	echo
1238	echo "IPv4 groups functional"
1239	echo "----------------------"
1240
1241	# basic functionality: create a nexthop group, default weight
1242	run_cmd "$IP nexthop add id 11 via 172.16.1.2 dev veth1"
1243	run_cmd "$IP nexthop add id 101 group 11"
1244	log_test $? 0 "Create nexthop group with single nexthop"
1245
1246	# get nexthop group
1247	run_cmd "$IP nexthop get id 101"
1248	log_test $? 0 "Get nexthop group by id"
1249	check_nexthop "id 101" "id 101 group 11"
1250
1251	# delete nexthop group
1252	run_cmd "$IP nexthop del id 101"
1253	log_test $? 0 "Delete nexthop group by id"
1254	check_nexthop "id 101" ""
1255
1256	$IP nexthop flush >/dev/null 2>&1
1257
1258	#
1259	# create group with multiple nexthops
1260	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1261	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1262	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
1263	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
1264	run_cmd "$IP nexthop add id 102 group 12/13/14/15"
1265	log_test $? 0 "Nexthop group with multiple nexthops"
1266	check_nexthop "id 102" "id 102 group 12/13/14/15"
1267
1268	# Delete nexthop in a group and group is updated
1269	run_cmd "$IP nexthop del id 13"
1270	check_nexthop "id 102" "id 102 group 12/14/15"
1271	log_test $? 0 "Nexthop group updated when entry is deleted"
1272
1273	# create group with multiple weighted nexthops
1274	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1275	run_cmd "$IP nexthop add id 103 group 12/13,2/14,3/15,4"
1276	log_test $? 0 "Nexthop group with weighted nexthops"
1277	check_nexthop "id 103" "id 103 group 12/13,2/14,3/15,4"
1278
1279	# Delete nexthop in a weighted group and group is updated
1280	run_cmd "$IP nexthop del id 13"
1281	check_nexthop "id 103" "id 103 group 12/14,3/15,4"
1282	log_test $? 0 "Weighted nexthop group updated when entry is deleted"
1283
1284	# admin down - nexthop is removed from group
1285	run_cmd "$IP li set dev veth1 down"
1286	check_nexthop "dev veth1" ""
1287	log_test $? 0 "Nexthops in groups removed on admin down"
1288
1289	# expect groups to have been deleted as well
1290	check_nexthop "" ""
1291
1292	run_cmd "$IP li set dev veth1 up"
1293
1294	$IP nexthop flush >/dev/null 2>&1
1295
1296	# group with nexthops using different devices
1297	set -e
1298	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1299	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1300	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
1301	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
1302
1303	run_cmd "$IP nexthop add id 22 via 172.16.2.2 dev veth3"
1304	run_cmd "$IP nexthop add id 23 via 172.16.2.3 dev veth3"
1305	run_cmd "$IP nexthop add id 24 via 172.16.2.4 dev veth3"
1306	run_cmd "$IP nexthop add id 25 via 172.16.2.5 dev veth3"
1307	set +e
1308
1309	# multiple groups with same nexthop
1310	run_cmd "$IP nexthop add id 104 group 12"
1311	run_cmd "$IP nexthop add id 105 group 12"
1312	check_nexthop "group" "id 104 group 12 id 105 group 12"
1313	log_test $? 0 "Multiple groups with same nexthop"
1314
1315	run_cmd "$IP nexthop flush groups"
1316	[ $? -ne 0 ] && return 1
1317
1318	# on admin down of veth1, it should be removed from the group
1319	run_cmd "$IP nexthop add id 105 group 12/13/22/23/14"
1320	run_cmd "$IP li set veth1 down"
1321	check_nexthop "id 105" "id 105 group 22/23"
1322	log_test $? 0 "Nexthops in group removed on admin down - mixed group"
1323
1324	run_cmd "$IP nexthop add id 106 group 105/24"
1325	log_test $? 2 "Nexthop group can not have a group as an entry"
1326
1327	# a group can have a blackhole entry only if it is the only
1328	# nexthop in the group. Needed for atomic replace with an
1329	# actual nexthop group
1330	run_cmd "$IP nexthop add id 31 blackhole"
1331	run_cmd "$IP nexthop add id 107 group 31"
1332	log_test $? 0 "Nexthop group with a blackhole entry"
1333
1334	run_cmd "$IP nexthop add id 108 group 31/24"
1335	log_test $? 2 "Nexthop group can not have a blackhole and another nexthop"
1336}
1337
1338ipv4_res_grp_fcnal()
1339{
1340	local rc
1341
1342	echo
1343	echo "IPv4 resilient groups functional"
1344	echo "--------------------------------"
1345
1346	check_nexthop_res_support
1347	if [ $? -eq $ksft_skip ]; then
1348		return $ksft_skip
1349	fi
1350
1351	#
1352	# migration of nexthop buckets - equal weights
1353	#
1354	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1355	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1356	run_cmd "$IP nexthop add id 102 group 12/13 type resilient buckets 2 idle_timer 0"
1357
1358	run_cmd "$IP nexthop del id 13"
1359	check_nexthop "id 102" \
1360		"id 102 group 12 type resilient buckets 2 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
1361	log_test $? 0 "Nexthop group updated when entry is deleted"
1362	check_nexthop_bucket "list id 102" \
1363		"id 102 index 0 nhid 12 id 102 index 1 nhid 12"
1364	log_test $? 0 "Nexthop buckets updated when entry is deleted"
1365
1366	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1367	run_cmd "$IP nexthop replace id 102 group 12/13 type resilient buckets 2 idle_timer 0"
1368	check_nexthop "id 102" \
1369		"id 102 group 12/13 type resilient buckets 2 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
1370	log_test $? 0 "Nexthop group updated after replace"
1371	check_nexthop_bucket "list id 102" \
1372		"id 102 index 0 nhid 13 id 102 index 1 nhid 12"
1373	log_test $? 0 "Nexthop buckets updated after replace"
1374
1375	$IP nexthop flush >/dev/null 2>&1
1376
1377	#
1378	# migration of nexthop buckets - unequal weights
1379	#
1380	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1381	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1382	run_cmd "$IP nexthop add id 102 group 12,3/13,1 type resilient buckets 4 idle_timer 0"
1383
1384	run_cmd "$IP nexthop del id 13"
1385	check_nexthop "id 102" \
1386		"id 102 group 12,3 type resilient buckets 4 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
1387	log_test $? 0 "Nexthop group updated when entry is deleted - nECMP"
1388	check_nexthop_bucket "list id 102" \
1389		"id 102 index 0 nhid 12 id 102 index 1 nhid 12 id 102 index 2 nhid 12 id 102 index 3 nhid 12"
1390	log_test $? 0 "Nexthop buckets updated when entry is deleted - nECMP"
1391
1392	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1393	run_cmd "$IP nexthop replace id 102 group 12,3/13,1 type resilient buckets 4 idle_timer 0"
1394	check_nexthop "id 102" \
1395		"id 102 group 12,3/13 type resilient buckets 4 idle_timer 0 unbalanced_timer 0 unbalanced_time 0"
1396	log_test $? 0 "Nexthop group updated after replace - nECMP"
1397	check_nexthop_bucket "list id 102" \
1398		"id 102 index 0 nhid 13 id 102 index 1 nhid 12 id 102 index 2 nhid 12 id 102 index 3 nhid 12"
1399	log_test $? 0 "Nexthop buckets updated after replace - nECMP"
1400}
1401
1402ipv4_withv6_fcnal()
1403{
1404	local lladdr
1405
1406	set -e
1407	lladdr=$(get_linklocal veth2 peer)
1408	run_cmd "$IP nexthop add id 11 via ${lladdr} dev veth1"
1409	set +e
1410	run_cmd "$IP ro add 172.16.101.1/32 nhid 11"
1411	log_test $? 0 "IPv6 nexthop with IPv4 route"
1412	check_route "172.16.101.1" "172.16.101.1 nhid 11 via inet6 ${lladdr} dev veth1"
1413
1414	set -e
1415	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1416	run_cmd "$IP nexthop add id 101 group 11/12"
1417	set +e
1418	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
1419	log_test $? 0 "IPv6 nexthop with IPv4 route"
1420
1421	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"
1422
1423	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1424	log_test $? 0 "IPv4 route with IPv6 gateway"
1425	check_route "172.16.101.1" "172.16.101.1 via inet6 ${lladdr} dev veth1"
1426
1427	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 2001:db8:50::1 dev veth1"
1428	log_test $? 2 "IPv4 route with invalid IPv6 gateway"
1429}
1430
1431ipv4_fcnal_runtime()
1432{
1433	local lladdr
1434	local rc
1435
1436	echo
1437	echo "IPv4 functional runtime"
1438	echo "-----------------------"
1439
1440	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
1441	run_cmd "$IP ro add 172.16.101.1/32 nhid 21"
1442	log_test $? 0 "Route add"
1443	check_route "172.16.101.1" "172.16.101.1 nhid 21 via 172.16.1.2 dev veth1"
1444
1445	run_cmd "$IP ro delete 172.16.101.1/32 nhid 21"
1446	log_test $? 0 "Route delete"
1447
1448	#
1449	# scope mismatch
1450	#
1451	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
1452	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
1453	log_test $? 2 "Route add - scope conflict with nexthop"
1454
1455	run_cmd "$IP nexthop replace id 22 dev veth3"
1456	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
1457	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
1458	log_test $? 2 "Nexthop replace with invalid scope for existing route"
1459
1460	# check cleanup path on invalid metric
1461	run_cmd "$IP ro add 172.16.101.2/32 nhid 22 congctl lock foo"
1462	log_test $? 2 "IPv4 route with invalid metric"
1463
1464	#
1465	# add route with nexthop and check traffic
1466	#
1467	run_cmd "$IP nexthop replace id 21 via 172.16.1.2 dev veth1"
1468	run_cmd "$IP ro replace 172.16.101.1/32 nhid 21"
1469	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1470	log_test $? 0 "Basic ping"
1471
1472	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
1473	run_cmd "$IP nexthop add id 122 group 21/22"
1474	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
1475	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1476	log_test $? 0 "Ping - multipath"
1477
1478	run_cmd "$IP ro delete 172.16.101.1/32 nhid 122"
1479
1480	#
1481	# multiple default routes
1482	# - tests fib_select_default
1483	run_cmd "$IP nexthop add id 501 via 172.16.1.2 dev veth1"
1484	run_cmd "$IP ro add default nhid 501"
1485	run_cmd "$IP ro add default via 172.16.1.3 dev veth1 metric 20"
1486	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1487	log_test $? 0 "Ping - multiple default routes, nh first"
1488
1489	# flip the order
1490	run_cmd "$IP ro del default nhid 501"
1491	run_cmd "$IP ro del default via 172.16.1.3 dev veth1 metric 20"
1492	run_cmd "$IP ro add default via 172.16.1.2 dev veth1 metric 20"
1493	run_cmd "$IP nexthop replace id 501 via 172.16.1.3 dev veth1"
1494	run_cmd "$IP ro add default nhid 501 metric 20"
1495	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1496	log_test $? 0 "Ping - multiple default routes, nh second"
1497
1498	run_cmd "$IP nexthop delete nhid 501"
1499	run_cmd "$IP ro del default"
1500
1501	#
1502	# IPv4 with blackhole nexthops
1503	#
1504	run_cmd "$IP nexthop add id 23 blackhole"
1505	run_cmd "$IP ro replace 172.16.101.1/32 nhid 23"
1506	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1507	log_test $? 2 "Ping - blackhole"
1508
1509	run_cmd "$IP nexthop replace id 23 via 172.16.1.2 dev veth1"
1510	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1511	log_test $? 0 "Ping - blackhole replaced with gateway"
1512
1513	run_cmd "$IP nexthop replace id 23 blackhole"
1514	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1515	log_test $? 2 "Ping - gateway replaced by blackhole"
1516
1517	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
1518	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1519	if [ $? -eq 0 ]; then
1520		run_cmd "$IP nexthop replace id 122 group 23"
1521		run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1522		log_test $? 2 "Ping - group with blackhole"
1523
1524		run_cmd "$IP nexthop replace id 122 group 21/22"
1525		run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1526		log_test $? 0 "Ping - group blackhole replaced with gateways"
1527	else
1528		log_test 2 0 "Ping - multipath failed"
1529	fi
1530
1531	#
1532	# device only and gw + dev only mix
1533	#
1534	run_cmd "$IP nexthop add id 85 dev veth1"
1535	run_cmd "$IP ro replace 172.16.101.1/32 nhid 85"
1536	log_test $? 0 "IPv4 route with device only nexthop"
1537	check_route "172.16.101.1" "172.16.101.1 nhid 85 dev veth1"
1538
1539	run_cmd "$IP nexthop add id 123 group 21/85"
1540	run_cmd "$IP ro replace 172.16.101.1/32 nhid 123"
1541	log_test $? 0 "IPv4 multipath route with nexthop mix - dev only + gw"
1542	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"
1543
1544	#
1545	# IPv4 with IPv6
1546	#
1547	set -e
1548	lladdr=$(get_linklocal veth2 peer)
1549	run_cmd "$IP nexthop add id 24 via ${lladdr} dev veth1"
1550	set +e
1551	run_cmd "$IP ro replace 172.16.101.1/32 nhid 24"
1552	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1553	log_test $? 0 "IPv6 nexthop with IPv4 route"
1554
1555	$IP neigh sh | grep -q "${lladdr} dev veth1"
1556	if [ $? -eq 1 ]; then
1557		echo "    WARNING: Neigh entry missing for ${lladdr}"
1558		$IP neigh sh | grep 'dev veth1'
1559	fi
1560
1561	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
1562	if [ $? -eq 0 ]; then
1563		echo "    WARNING: Neigh entry exists for 172.16.101.1"
1564		$IP neigh sh | grep 'dev veth1'
1565	fi
1566
1567	set -e
1568	run_cmd "$IP nexthop add id 25 via 172.16.1.2 dev veth1"
1569	run_cmd "$IP nexthop add id 101 group 24/25"
1570	set +e
1571	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
1572	log_test $? 0 "IPv4 route with mixed v4-v6 multipath route"
1573
1574	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"
1575
1576	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1577	log_test $? 0 "IPv6 nexthop with IPv4 route"
1578
1579	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1580	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1581	log_test $? 0 "IPv4 route with IPv6 gateway"
1582
1583	$IP neigh sh | grep -q "${lladdr} dev veth1"
1584	if [ $? -eq 1 ]; then
1585		echo "    WARNING: Neigh entry missing for ${lladdr}"
1586		$IP neigh sh | grep 'dev veth1'
1587	fi
1588
1589	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
1590	if [ $? -eq 0 ]; then
1591		echo "    WARNING: Neigh entry exists for 172.16.101.1"
1592		$IP neigh sh | grep 'dev veth1'
1593	fi
1594
1595	run_cmd "$IP ro del 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1596	run_cmd "$IP -4 ro add default via inet6 ${lladdr} dev veth1"
1597	run_cmd "ip netns exec me ping -c1 -w$PING_TIMEOUT 172.16.101.1"
1598	log_test $? 0 "IPv4 default route with IPv6 gateway"
1599
1600	#
1601	# MPLS as an example of LWT encap
1602	#
1603	run_cmd "$IP nexthop add id 51 encap mpls 101 via 172.16.1.2 dev veth1"
1604	log_test $? 0 "IPv4 route with MPLS encap"
1605	check_nexthop "id 51" "id 51 encap mpls 101 via 172.16.1.2 dev veth1 scope link"
1606	log_test $? 0 "IPv4 route with MPLS encap - check"
1607
1608	run_cmd "$IP nexthop add id 52 encap mpls 102 via inet6 2001:db8:91::2 dev veth1"
1609	log_test $? 0 "IPv4 route with MPLS encap and v6 gateway"
1610	check_nexthop "id 52" "id 52 encap mpls 102 via 2001:db8:91::2 dev veth1 scope link"
1611	log_test $? 0 "IPv4 route with MPLS encap, v6 gw - check"
1612}
1613
1614ipv4_large_grp()
1615{
1616	local ecmp=32
1617
1618	echo
1619	echo "IPv4 large groups (x$ecmp)"
1620	echo "---------------------"
1621
1622	check_large_grp 4 $ecmp
1623
1624	$IP nexthop flush >/dev/null 2>&1
1625}
1626
1627ipv4_large_res_grp()
1628{
1629	echo
1630	echo "IPv4 large resilient group (128k buckets)"
1631	echo "-----------------------------------------"
1632
1633	check_nexthop_res_support
1634	if [ $? -eq $ksft_skip ]; then
1635		return $ksft_skip
1636	fi
1637
1638	check_large_res_grp 4 $((128 * 1024))
1639
1640	$IP nexthop flush >/dev/null 2>&1
1641}
1642
1643sysctl_nexthop_compat_mode_check()
1644{
1645	local sysctlname="net.ipv4.nexthop_compat_mode"
1646	local lprefix=$1
1647
1648	IPE="ip netns exec me"
1649
1650	$IPE sysctl -q $sysctlname 2>&1 >/dev/null
1651	if [ $? -ne 0 ]; then
1652		echo "SKIP: kernel lacks nexthop compat mode sysctl control"
1653		return $ksft_skip
1654	fi
1655
1656	out=$($IPE sysctl $sysctlname 2>/dev/null)
1657	log_test $? 0 "$lprefix default nexthop compat mode check"
1658	check_output "${out}" "$sysctlname = 1"
1659}
1660
1661sysctl_nexthop_compat_mode_set()
1662{
1663	local sysctlname="net.ipv4.nexthop_compat_mode"
1664	local mode=$1
1665	local lprefix=$2
1666
1667	IPE="ip netns exec me"
1668
1669	out=$($IPE sysctl -w $sysctlname=$mode)
1670	log_test $? 0 "$lprefix set compat mode - $mode"
1671	check_output "${out}" "net.ipv4.nexthop_compat_mode = $mode"
1672}
1673
1674ipv6_compat_mode()
1675{
1676	local rc
1677
1678	echo
1679	echo "IPv6 nexthop api compat mode test"
1680	echo "--------------------------------"
1681
1682	sysctl_nexthop_compat_mode_check "IPv6"
1683	if [ $? -eq $ksft_skip ]; then
1684		return $ksft_skip
1685	fi
1686
1687	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
1688	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
1689	run_cmd "$IP nexthop add id 122 group 62/63"
1690	ipmout=$(start_ip_monitor route)
1691
1692	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 122"
1693	# route add notification should contain expanded nexthops
1694	stop_ip_monitor $ipmout 3
1695	log_test $? 0 "IPv6 compat mode on - route add notification"
1696
1697	# route dump should contain expanded nexthops
1698	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"
1699	log_test $? 0 "IPv6 compat mode on - route dump"
1700
1701	# change in nexthop group should generate route notification
1702	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
1703	ipmout=$(start_ip_monitor route)
1704	run_cmd "$IP nexthop replace id 122 group 62/64"
1705	stop_ip_monitor $ipmout 3
1706
1707	log_test $? 0 "IPv6 compat mode on - nexthop change"
1708
1709	# set compat mode off
1710	sysctl_nexthop_compat_mode_set 0 "IPv6"
1711
1712	run_cmd "$IP -6 ro del 2001:db8:101::1/128 nhid 122"
1713
1714	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
1715	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
1716	run_cmd "$IP nexthop add id 122 group 62/63"
1717	ipmout=$(start_ip_monitor route)
1718
1719	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 122"
1720	# route add notification should not contain expanded nexthops
1721	stop_ip_monitor $ipmout 1
1722	log_test $? 0 "IPv6 compat mode off - route add notification"
1723
1724	# route dump should not contain expanded nexthops
1725	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 122 metric 1024"
1726	log_test $? 0 "IPv6 compat mode off - route dump"
1727
1728	# change in nexthop group should not generate route notification
1729	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
1730	ipmout=$(start_ip_monitor route)
1731	run_cmd "$IP nexthop replace id 122 group 62/64"
1732	stop_ip_monitor $ipmout 0
1733	log_test $? 0 "IPv6 compat mode off - nexthop change"
1734
1735	# nexthop delete should not generate route notification
1736	ipmout=$(start_ip_monitor route)
1737	run_cmd "$IP nexthop del id 122"
1738	stop_ip_monitor $ipmout 0
1739	log_test $? 0 "IPv6 compat mode off - nexthop delete"
1740
1741	# set compat mode back on
1742	sysctl_nexthop_compat_mode_set 1 "IPv6"
1743}
1744
1745ipv4_compat_mode()
1746{
1747	local rc
1748
1749	echo
1750	echo "IPv4 nexthop api compat mode"
1751	echo "----------------------------"
1752
1753	sysctl_nexthop_compat_mode_check "IPv4"
1754	if [ $? -eq $ksft_skip ]; then
1755		return $ksft_skip
1756	fi
1757
1758	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
1759	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
1760	run_cmd "$IP nexthop add id 122 group 21/22"
1761	ipmout=$(start_ip_monitor route)
1762
1763	run_cmd "$IP ro add 172.16.101.1/32 nhid 122"
1764	stop_ip_monitor $ipmout 3
1765
1766	# route add notification should contain expanded nexthops
1767	log_test $? 0 "IPv4 compat mode on - route add notification"
1768
1769	# route dump should contain expanded nexthops
1770	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"
1771	log_test $? 0 "IPv4 compat mode on - route dump"
1772
1773	# change in nexthop group should generate route notification
1774	run_cmd "$IP nexthop add id 23 via 172.16.1.3 dev veth1"
1775	ipmout=$(start_ip_monitor route)
1776	run_cmd "$IP nexthop replace id 122 group 21/23"
1777	stop_ip_monitor $ipmout 3
1778	log_test $? 0 "IPv4 compat mode on - nexthop change"
1779
1780	sysctl_nexthop_compat_mode_set 0 "IPv4"
1781
1782	# cleanup
1783	run_cmd "$IP ro del 172.16.101.1/32 nhid 122"
1784
1785	ipmout=$(start_ip_monitor route)
1786	run_cmd "$IP ro add 172.16.101.1/32 nhid 122"
1787	stop_ip_monitor $ipmout 1
1788	# route add notification should not contain expanded nexthops
1789	log_test $? 0 "IPv4 compat mode off - route add notification"
1790
1791	# route dump should not contain expanded nexthops
1792	check_route "172.16.101.1" "172.16.101.1 nhid 122"
1793	log_test $? 0 "IPv4 compat mode off - route dump"
1794
1795	# change in nexthop group should not generate route notification
1796	ipmout=$(start_ip_monitor route)
1797	run_cmd "$IP nexthop replace id 122 group 21/22"
1798	stop_ip_monitor $ipmout 0
1799	log_test $? 0 "IPv4 compat mode off - nexthop change"
1800
1801	# nexthop delete should not generate route notification
1802	ipmout=$(start_ip_monitor route)
1803	run_cmd "$IP nexthop del id 122"
1804	stop_ip_monitor $ipmout 0
1805	log_test $? 0 "IPv4 compat mode off - nexthop delete"
1806
1807	sysctl_nexthop_compat_mode_set 1 "IPv4"
1808}
1809
1810ipv4_del_add_loop1()
1811{
1812	while :; do
1813		$IP nexthop del id 100
1814		$IP nexthop add id 100 via 172.16.1.2 dev veth1
1815	done >/dev/null 2>&1
1816}
1817
1818ipv4_grp_replace_loop()
1819{
1820	while :; do
1821		$IP nexthop replace id 102 group 100/101
1822	done >/dev/null 2>&1
1823}
1824
1825ipv4_torture()
1826{
1827	local pid1
1828	local pid2
1829	local pid3
1830	local pid4
1831	local pid5
1832
1833	echo
1834	echo "IPv4 runtime torture"
1835	echo "--------------------"
1836	if [ ! -x "$(command -v mausezahn)" ]; then
1837		echo "SKIP: Could not run test; need mausezahn tool"
1838		return
1839	fi
1840
1841	run_cmd "$IP nexthop add id 100 via 172.16.1.2 dev veth1"
1842	run_cmd "$IP nexthop add id 101 via 172.16.2.2 dev veth3"
1843	run_cmd "$IP nexthop add id 102 group 100/101"
1844	run_cmd "$IP route add 172.16.101.1 nhid 102"
1845	run_cmd "$IP route add 172.16.101.2 nhid 102"
1846
1847	ipv4_del_add_loop1 &
1848	pid1=$!
1849	ipv4_grp_replace_loop &
1850	pid2=$!
1851	ip netns exec me ping -f 172.16.101.1 >/dev/null 2>&1 &
1852	pid3=$!
1853	ip netns exec me ping -f 172.16.101.2 >/dev/null 2>&1 &
1854	pid4=$!
1855	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 &
1856	pid5=$!
1857
1858	sleep 300
1859	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
1860	wait $pid1 $pid2 $pid3 $pid4 $pid5 2>/dev/null
1861
1862	# if we did not crash, success
1863	log_test 0 0 "IPv4 torture test"
1864}
1865
1866ipv4_res_grp_replace_loop()
1867{
1868	while :; do
1869		$IP nexthop replace id 102 group 100/101 type resilient
1870	done >/dev/null 2>&1
1871}
1872
1873ipv4_res_torture()
1874{
1875	local pid1
1876	local pid2
1877	local pid3
1878	local pid4
1879	local pid5
1880
1881	echo
1882	echo "IPv4 runtime resilient nexthop group torture"
1883	echo "--------------------------------------------"
1884
1885	check_nexthop_res_support
1886	if [ $? -eq $ksft_skip ]; then
1887		return $ksft_skip
1888	fi
1889
1890	if [ ! -x "$(command -v mausezahn)" ]; then
1891		echo "SKIP: Could not run test; need mausezahn tool"
1892		return
1893	fi
1894
1895	run_cmd "$IP nexthop add id 100 via 172.16.1.2 dev veth1"
1896	run_cmd "$IP nexthop add id 101 via 172.16.2.2 dev veth3"
1897	run_cmd "$IP nexthop add id 102 group 100/101 type resilient buckets 512 idle_timer 0"
1898	run_cmd "$IP route add 172.16.101.1 nhid 102"
1899	run_cmd "$IP route add 172.16.101.2 nhid 102"
1900
1901	ipv4_del_add_loop1 &
1902	pid1=$!
1903	ipv4_res_grp_replace_loop &
1904	pid2=$!
1905	ip netns exec me ping -f 172.16.101.1 >/dev/null 2>&1 &
1906	pid3=$!
1907	ip netns exec me ping -f 172.16.101.2 >/dev/null 2>&1 &
1908	pid4=$!
1909	ip netns exec me mausezahn veth1 \
1910				-B 172.16.101.2 -A 172.16.1.1 -c 0 \
1911				-t tcp "dp=1-1023, flags=syn" >/dev/null 2>&1 &
1912	pid5=$!
1913
1914	sleep 300
1915	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
1916	wait $pid1 $pid2 $pid3 $pid4 $pid5 2>/dev/null
1917
1918	# if we did not crash, success
1919	log_test 0 0 "IPv4 resilient nexthop group torture test"
1920}
1921
1922basic()
1923{
1924	echo
1925	echo "Basic functional tests"
1926	echo "----------------------"
1927	run_cmd "$IP nexthop ls"
1928	log_test $? 0 "List with nothing defined"
1929
1930	run_cmd "$IP nexthop get id 1"
1931	log_test $? 2 "Nexthop get on non-existent id"
1932
1933	# attempt to create nh without a device or gw - fails
1934	run_cmd "$IP nexthop add id 1"
1935	log_test $? 2 "Nexthop with no device or gateway"
1936
1937	# attempt to create nh with down device - fails
1938	$IP li set veth1 down
1939	run_cmd "$IP nexthop add id 1 dev veth1"
1940	log_test $? 2 "Nexthop with down device"
1941
1942	# create nh with linkdown device - fails
1943	$IP li set veth1 up
1944	ip -netns peer li set veth2 down
1945	run_cmd "$IP nexthop add id 1 dev veth1"
1946	log_test $? 2 "Nexthop with device that is linkdown"
1947	ip -netns peer li set veth2 up
1948
1949	# device only
1950	run_cmd "$IP nexthop add id 1 dev veth1"
1951	log_test $? 0 "Nexthop with device only"
1952
1953	# create nh with duplicate id
1954	run_cmd "$IP nexthop add id 1 dev veth3"
1955	log_test $? 2 "Nexthop with duplicate id"
1956
1957	# blackhole nexthop
1958	run_cmd "$IP nexthop add id 2 blackhole"
1959	log_test $? 0 "Blackhole nexthop"
1960
1961	# blackhole nexthop can not have other specs
1962	run_cmd "$IP nexthop replace id 2 blackhole dev veth1"
1963	log_test $? 2 "Blackhole nexthop with other attributes"
1964
1965	# blackhole nexthop should not be affected by the state of the loopback
1966	# device
1967	run_cmd "$IP link set dev lo down"
1968	check_nexthop "id 2" "id 2 blackhole"
1969	log_test $? 0 "Blackhole nexthop with loopback device down"
1970
1971	run_cmd "$IP link set dev lo up"
1972
1973	#
1974	# groups
1975	#
1976
1977	run_cmd "$IP nexthop add id 101 group 1"
1978	log_test $? 0 "Create group"
1979
1980	run_cmd "$IP nexthop add id 102 group 2"
1981	log_test $? 0 "Create group with blackhole nexthop"
1982
1983	# multipath group can not have a blackhole as 1 path
1984	run_cmd "$IP nexthop add id 103 group 1/2"
1985	log_test $? 2 "Create multipath group where 1 path is a blackhole"
1986
1987	# multipath group can not have a member replaced by a blackhole
1988	run_cmd "$IP nexthop replace id 2 dev veth3"
1989	run_cmd "$IP nexthop replace id 102 group 1/2"
1990	run_cmd "$IP nexthop replace id 2 blackhole"
1991	log_test $? 2 "Multipath group can not have a member replaced by blackhole"
1992
1993	# attempt to create group with non-existent nexthop
1994	run_cmd "$IP nexthop add id 103 group 12"
1995	log_test $? 2 "Create group with non-existent nexthop"
1996
1997	# attempt to create group with same nexthop
1998	run_cmd "$IP nexthop add id 103 group 1/1"
1999	log_test $? 2 "Create group with same nexthop multiple times"
2000
2001	# replace nexthop with a group - fails
2002	run_cmd "$IP nexthop replace id 2 group 1"
2003	log_test $? 2 "Replace nexthop with nexthop group"
2004
2005	# replace nexthop group with a nexthop - fails
2006	run_cmd "$IP nexthop replace id 101 dev veth1"
2007	log_test $? 2 "Replace nexthop group with nexthop"
2008
2009	# nexthop group with other attributes fail
2010	run_cmd "$IP nexthop add id 104 group 1 dev veth1"
2011	log_test $? 2 "Nexthop group and device"
2012
2013	# Tests to ensure that flushing works as expected.
2014	run_cmd "$IP nexthop add id 105 blackhole proto 99"
2015	run_cmd "$IP nexthop add id 106 blackhole proto 100"
2016	run_cmd "$IP nexthop add id 107 blackhole proto 99"
2017	run_cmd "$IP nexthop flush proto 99"
2018	check_nexthop "id 105" ""
2019	check_nexthop "id 106" "id 106 blackhole proto 100"
2020	check_nexthop "id 107" ""
2021	run_cmd "$IP nexthop flush proto 100"
2022	check_nexthop "id 106" ""
2023
2024	run_cmd "$IP nexthop flush proto 100"
2025	log_test $? 0 "Test proto flush"
2026
2027	run_cmd "$IP nexthop add id 104 group 1 blackhole"
2028	log_test $? 2 "Nexthop group and blackhole"
2029
2030	$IP nexthop flush >/dev/null 2>&1
2031
2032	# Test to ensure that flushing with a multi-part nexthop dump works as
2033	# expected.
2034	local batch_file=$(mktemp)
2035
2036	for i in $(seq 1 $((64 * 1024))); do
2037		echo "nexthop add id $i blackhole" >> $batch_file
2038	done
2039
2040	$IP -b $batch_file
2041	$IP nexthop flush >/dev/null 2>&1
2042	[[ $($IP nexthop | wc -l) -eq 0 ]]
2043	log_test $? 0 "Large scale nexthop flushing"
2044
2045	rm $batch_file
2046}
2047
2048check_nexthop_buckets_balance()
2049{
2050	local nharg=$1; shift
2051	local ret
2052
2053	while (($# > 0)); do
2054		local selector=$1; shift
2055		local condition=$1; shift
2056		local count
2057
2058		count=$($IP -j nexthop bucket ${nharg} ${selector} | jq length)
2059		(( $count $condition ))
2060		ret=$?
2061		if ((ret != 0)); then
2062			return $ret
2063		fi
2064	done
2065
2066	return 0
2067}
2068
2069basic_res()
2070{
2071	echo
2072	echo "Basic resilient nexthop group functional tests"
2073	echo "----------------------------------------------"
2074
2075	check_nexthop_res_support
2076	if [ $? -eq $ksft_skip ]; then
2077		return $ksft_skip
2078	fi
2079
2080	run_cmd "$IP nexthop add id 1 dev veth1"
2081
2082	#
2083	# resilient nexthop group addition
2084	#
2085
2086	run_cmd "$IP nexthop add id 101 group 1 type resilient buckets 8"
2087	log_test $? 0 "Add a nexthop group with default parameters"
2088
2089	run_cmd "$IP nexthop get id 101"
2090	check_nexthop "id 101" \
2091		"id 101 group 1 type resilient buckets 8 idle_timer 120 unbalanced_timer 0 unbalanced_time 0"
2092	log_test $? 0 "Get a nexthop group with default parameters"
2093
2094	run_cmd "$IP nexthop add id 102 group 1 type resilient
2095			buckets 4 idle_timer 100 unbalanced_timer 5"
2096	run_cmd "$IP nexthop get id 102"
2097	check_nexthop "id 102" \
2098		"id 102 group 1 type resilient buckets 4 idle_timer 100 unbalanced_timer 5 unbalanced_time 0"
2099	log_test $? 0 "Get a nexthop group with non-default parameters"
2100
2101	run_cmd "$IP nexthop add id 103 group 1 type resilient buckets 0"
2102	log_test $? 2 "Add a nexthop group with 0 buckets"
2103
2104	#
2105	# resilient nexthop group replacement
2106	#
2107
2108	run_cmd "$IP nexthop replace id 101 group 1 type resilient
2109			buckets 8 idle_timer 240 unbalanced_timer 80"
2110	log_test $? 0 "Replace nexthop group parameters"
2111	check_nexthop "id 101" \
2112		"id 101 group 1 type resilient buckets 8 idle_timer 240 unbalanced_timer 80 unbalanced_time 0"
2113	log_test $? 0 "Get a nexthop group after replacing parameters"
2114
2115	run_cmd "$IP nexthop replace id 101 group 1 type resilient idle_timer 512"
2116	log_test $? 0 "Replace idle timer"
2117	check_nexthop "id 101" \
2118		"id 101 group 1 type resilient buckets 8 idle_timer 512 unbalanced_timer 80 unbalanced_time 0"
2119	log_test $? 0 "Get a nexthop group after replacing idle timer"
2120
2121	run_cmd "$IP nexthop replace id 101 group 1 type resilient unbalanced_timer 256"
2122	log_test $? 0 "Replace unbalanced timer"
2123	check_nexthop "id 101" \
2124		"id 101 group 1 type resilient buckets 8 idle_timer 512 unbalanced_timer 256 unbalanced_time 0"
2125	log_test $? 0 "Get a nexthop group after replacing unbalanced timer"
2126
2127	run_cmd "$IP nexthop replace id 101 group 1 type resilient"
2128	log_test $? 0 "Replace with no parameters"
2129	check_nexthop "id 101" \
2130		"id 101 group 1 type resilient buckets 8 idle_timer 512 unbalanced_timer 256 unbalanced_time 0"
2131	log_test $? 0 "Get a nexthop group after replacing no parameters"
2132
2133	run_cmd "$IP nexthop replace id 101 group 1"
2134	log_test $? 2 "Replace nexthop group type - implicit"
2135
2136	run_cmd "$IP nexthop replace id 101 group 1 type mpath"
2137	log_test $? 2 "Replace nexthop group type - explicit"
2138
2139	run_cmd "$IP nexthop replace id 101 group 1 type resilient buckets 1024"
2140	log_test $? 2 "Replace number of nexthop buckets"
2141
2142	check_nexthop "id 101" \
2143		"id 101 group 1 type resilient buckets 8 idle_timer 512 unbalanced_timer 256 unbalanced_time 0"
2144	log_test $? 0 "Get a nexthop group after replacing with invalid parameters"
2145
2146	#
2147	# resilient nexthop buckets dump
2148	#
2149
2150	$IP nexthop flush >/dev/null 2>&1
2151	run_cmd "$IP nexthop add id 1 dev veth1"
2152	run_cmd "$IP nexthop add id 2 dev veth3"
2153	run_cmd "$IP nexthop add id 101 group 1/2 type resilient buckets 4"
2154	run_cmd "$IP nexthop add id 201 group 1/2"
2155
2156	check_nexthop_bucket "" \
2157		"id 101 index 0 nhid 2 id 101 index 1 nhid 2 id 101 index 2 nhid 1 id 101 index 3 nhid 1"
2158	log_test $? 0 "Dump all nexthop buckets"
2159
2160	check_nexthop_bucket "list id 101" \
2161		"id 101 index 0 nhid 2 id 101 index 1 nhid 2 id 101 index 2 nhid 1 id 101 index 3 nhid 1"
2162	log_test $? 0 "Dump all nexthop buckets in a group"
2163
2164	sleep 0.1
2165	(( $($IP -j nexthop bucket list id 101 |
2166	     jq '[.[] | select(.bucket.idle_time > 0 and
2167	                       .bucket.idle_time < 2)] | length') == 4 ))
2168	log_test $? 0 "All nexthop buckets report a positive near-zero idle time"
2169
2170	check_nexthop_bucket "list dev veth1" \
2171		"id 101 index 2 nhid 1 id 101 index 3 nhid 1"
2172	log_test $? 0 "Dump all nexthop buckets with a specific nexthop device"
2173
2174	check_nexthop_bucket "list nhid 2" \
2175		"id 101 index 0 nhid 2 id 101 index 1 nhid 2"
2176	log_test $? 0 "Dump all nexthop buckets with a specific nexthop identifier"
2177
2178	run_cmd "$IP nexthop bucket list id 111"
2179	log_test $? 2 "Dump all nexthop buckets in a non-existent group"
2180
2181	run_cmd "$IP nexthop bucket list id 201"
2182	log_test $? 2 "Dump all nexthop buckets in a non-resilient group"
2183
2184	run_cmd "$IP nexthop bucket list dev bla"
2185	log_test $? 255 "Dump all nexthop buckets using a non-existent device"
2186
2187	run_cmd "$IP nexthop bucket list groups"
2188	log_test $? 255 "Dump all nexthop buckets with invalid 'groups' keyword"
2189
2190	run_cmd "$IP nexthop bucket list fdb"
2191	log_test $? 255 "Dump all nexthop buckets with invalid 'fdb' keyword"
2192
2193	#
2194	# resilient nexthop buckets get requests
2195	#
2196
2197	check_nexthop_bucket "get id 101 index 0" "id 101 index 0 nhid 2"
2198	log_test $? 0 "Get a valid nexthop bucket"
2199
2200	run_cmd "$IP nexthop bucket get id 101 index 999"
2201	log_test $? 2 "Get a nexthop bucket with valid group, but invalid index"
2202
2203	run_cmd "$IP nexthop bucket get id 201 index 0"
2204	log_test $? 2 "Get a nexthop bucket from a non-resilient group"
2205
2206	run_cmd "$IP nexthop bucket get id 999 index 0"
2207	log_test $? 2 "Get a nexthop bucket from a non-existent group"
2208
2209	#
2210	# tests for bucket migration
2211	#
2212
2213	$IP nexthop flush >/dev/null 2>&1
2214
2215	run_cmd "$IP nexthop add id 1 dev veth1"
2216	run_cmd "$IP nexthop add id 2 dev veth3"
2217	run_cmd "$IP nexthop add id 101
2218			group 1/2 type resilient buckets 10
2219			idle_timer 1 unbalanced_timer 20"
2220
2221	check_nexthop_buckets_balance "list id 101" \
2222				      "nhid 1" "== 5" \
2223				      "nhid 2" "== 5"
2224	log_test $? 0 "Initial bucket allocation"
2225
2226	run_cmd "$IP nexthop replace id 101
2227			group 1,2/2,3 type resilient"
2228	check_nexthop_buckets_balance "list id 101" \
2229				      "nhid 1" "== 4" \
2230				      "nhid 2" "== 6"
2231	log_test $? 0 "Bucket allocation after replace"
2232
2233	# Check that increase in idle timer does not make buckets appear busy.
2234	run_cmd "$IP nexthop replace id 101
2235			group 1,2/2,3 type resilient
2236			idle_timer 10"
2237	run_cmd "$IP nexthop replace id 101
2238			group 1/2 type resilient"
2239	check_nexthop_buckets_balance "list id 101" \
2240				      "nhid 1" "== 5" \
2241				      "nhid 2" "== 5"
2242	log_test $? 0 "Buckets migrated after idle timer change"
2243
2244	$IP nexthop flush >/dev/null 2>&1
2245}
2246
2247################################################################################
2248# usage
2249
2250usage()
2251{
2252	cat <<EOF
2253usage: ${0##*/} OPTS
2254
2255        -t <test>   Test(s) to run (default: all)
2256                    (options: $ALL_TESTS)
2257        -4          IPv4 tests only
2258        -6          IPv6 tests only
2259        -p          Pause on fail
2260        -P          Pause after each test before cleanup
2261        -v          verbose mode (show commands and output)
2262	-w	    Timeout for ping
2263
2264    Runtime test
2265	-n num	    Number of nexthops to target
2266	-N    	    Use new style to install routes in DUT
2267
2268done
2269EOF
2270}
2271
2272################################################################################
2273# main
2274
2275while getopts :t:pP46hv:w: o
2276do
2277	case $o in
2278		t) TESTS=$OPTARG;;
2279		4) TESTS=${IPV4_TESTS};;
2280		6) TESTS=${IPV6_TESTS};;
2281		p) PAUSE_ON_FAIL=yes;;
2282		P) PAUSE=yes;;
2283		v) VERBOSE=$(($VERBOSE + 1));;
2284		w) PING_TIMEOUT=$OPTARG;;
2285		h) usage; exit 0;;
2286		*) usage; exit 1;;
2287	esac
2288done
2289
2290# make sure we don't pause twice
2291[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
2292
2293if [ "$(id -u)" -ne 0 ];then
2294	echo "SKIP: Need root privileges"
2295	exit $ksft_skip;
2296fi
2297
2298if [ ! -x "$(command -v ip)" ]; then
2299	echo "SKIP: Could not run test without ip tool"
2300	exit $ksft_skip
2301fi
2302
2303ip help 2>&1 | grep -q nexthop
2304if [ $? -ne 0 ]; then
2305	echo "SKIP: iproute2 too old, missing nexthop command"
2306	exit $ksft_skip
2307fi
2308
2309out=$(ip nexthop ls 2>&1 | grep -q "Operation not supported")
2310if [ $? -eq 0 ]; then
2311	echo "SKIP: kernel lacks nexthop support"
2312	exit $ksft_skip
2313fi
2314
2315for t in $TESTS
2316do
2317	case $t in
2318	none) IP="ip -netns peer"; setup; exit 0;;
2319	*) setup; $t; cleanup;;
2320	esac
2321done
2322
2323if [ "$TESTS" != "none" ]; then
2324	printf "\nTests passed: %3d\n" ${nsuccess}
2325	printf "Tests failed: %3d\n"   ${nfail}
2326fi
2327
2328exit $ret
2329