xref: /linux/tools/testing/selftests/net/fib_nexthops.sh (revision 26fbb4c8c7c3ee9a4c3b4de555a8587b5a19154e)
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="ipv4_fcnal ipv4_grp_fcnal ipv4_withv6_fcnal ipv4_fcnal_runtime ipv4_large_grp ipv4_compat_mode ipv4_fdb_grp_fcnal ipv4_torture"
23IPV6_TESTS="ipv6_fcnal ipv6_grp_fcnal ipv6_fcnal_runtime ipv6_large_grp ipv6_compat_mode ipv6_fdb_grp_fcnal ipv6_torture"
24
25ALL_TESTS="basic ${IPV4_TESTS} ${IPV6_TESTS}"
26TESTS="${ALL_TESTS}"
27VERBOSE=0
28PAUSE_ON_FAIL=no
29PAUSE=no
30
31nsid=100
32
33################################################################################
34# utilities
35
36log_test()
37{
38	local rc=$1
39	local expected=$2
40	local msg="$3"
41
42	if [ ${rc} -eq ${expected} ]; then
43		printf "TEST: %-60s  [ OK ]\n" "${msg}"
44		nsuccess=$((nsuccess+1))
45	else
46		ret=1
47		nfail=$((nfail+1))
48		printf "TEST: %-60s  [FAIL]\n" "${msg}"
49		if [ "$VERBOSE" = "1" ]; then
50			echo "    rc=$rc, expected $expected"
51		fi
52
53		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
54		echo
55			echo "hit enter to continue, 'q' to quit"
56			read a
57			[ "$a" = "q" ] && exit 1
58		fi
59	fi
60
61	if [ "${PAUSE}" = "yes" ]; then
62		echo
63		echo "hit enter to continue, 'q' to quit"
64		read a
65		[ "$a" = "q" ] && exit 1
66	fi
67
68	[ "$VERBOSE" = "1" ] && echo
69}
70
71run_cmd()
72{
73	local cmd="$1"
74	local out
75	local stderr="2>/dev/null"
76
77	if [ "$VERBOSE" = "1" ]; then
78		printf "COMMAND: $cmd\n"
79		stderr=
80	fi
81
82	out=$(eval $cmd $stderr)
83	rc=$?
84	if [ "$VERBOSE" = "1" -a -n "$out" ]; then
85		echo "    $out"
86	fi
87
88	return $rc
89}
90
91get_linklocal()
92{
93	local dev=$1
94	local ns
95	local addr
96
97	[ -n "$2" ] && ns="-netns $2"
98	addr=$(ip $ns -6 -br addr show dev ${dev} | \
99	awk '{
100		for (i = 3; i <= NF; ++i) {
101			if ($i ~ /^fe80/)
102				print $i
103		}
104	}'
105	)
106	addr=${addr/\/*}
107
108	[ -z "$addr" ] && return 1
109
110	echo $addr
111
112	return 0
113}
114
115create_ns()
116{
117	local n=${1}
118
119	ip netns del ${n} 2>/dev/null
120
121	set -e
122	ip netns add ${n}
123	ip netns set ${n} $((nsid++))
124	ip -netns ${n} addr add 127.0.0.1/8 dev lo
125	ip -netns ${n} link set lo up
126
127	ip netns exec ${n} sysctl -qw net.ipv4.ip_forward=1
128	ip netns exec ${n} sysctl -qw net.ipv4.fib_multipath_use_neigh=1
129	ip netns exec ${n} sysctl -qw net.ipv4.conf.default.ignore_routes_with_linkdown=1
130	ip netns exec ${n} sysctl -qw net.ipv6.conf.all.keep_addr_on_down=1
131	ip netns exec ${n} sysctl -qw net.ipv6.conf.all.forwarding=1
132	ip netns exec ${n} sysctl -qw net.ipv6.conf.default.forwarding=1
133	ip netns exec ${n} sysctl -qw net.ipv6.conf.default.ignore_routes_with_linkdown=1
134	ip netns exec ${n} sysctl -qw net.ipv6.conf.all.accept_dad=0
135	ip netns exec ${n} sysctl -qw net.ipv6.conf.default.accept_dad=0
136
137	set +e
138}
139
140setup()
141{
142	cleanup
143
144	create_ns me
145	create_ns peer
146	create_ns remote
147
148	IP="ip -netns me"
149	BRIDGE="bridge -netns me"
150	set -e
151	$IP li add veth1 type veth peer name veth2
152	$IP li set veth1 up
153	$IP addr add 172.16.1.1/24 dev veth1
154	$IP -6 addr add 2001:db8:91::1/64 dev veth1 nodad
155
156	$IP li add veth3 type veth peer name veth4
157	$IP li set veth3 up
158	$IP addr add 172.16.2.1/24 dev veth3
159	$IP -6 addr add 2001:db8:92::1/64 dev veth3 nodad
160
161	$IP li set veth2 netns peer up
162	ip -netns peer addr add 172.16.1.2/24 dev veth2
163	ip -netns peer -6 addr add 2001:db8:91::2/64 dev veth2 nodad
164
165	$IP li set veth4 netns peer up
166	ip -netns peer addr add 172.16.2.2/24 dev veth4
167	ip -netns peer -6 addr add 2001:db8:92::2/64 dev veth4 nodad
168
169	ip -netns remote li add veth5 type veth peer name veth6
170	ip -netns remote li set veth5 up
171	ip -netns remote addr add dev veth5 172.16.101.1/24
172	ip -netns remote -6 addr add dev veth5 2001:db8:101::1/64 nodad
173	ip -netns remote ro add 172.16.0.0/22 via 172.16.101.2
174	ip -netns remote -6 ro add 2001:db8:90::/40 via 2001:db8:101::2
175
176	ip -netns remote li set veth6 netns peer up
177	ip -netns peer addr add dev veth6 172.16.101.2/24
178	ip -netns peer -6 addr add dev veth6 2001:db8:101::2/64 nodad
179	set +e
180}
181
182cleanup()
183{
184	local ns
185
186	for ns in me peer remote; do
187		ip netns del ${ns} 2>/dev/null
188	done
189}
190
191check_output()
192{
193	local out="$1"
194	local expected="$2"
195	local rc=0
196
197	[ "${out}" = "${expected}" ] && return 0
198
199	if [ -z "${out}" ]; then
200		if [ "$VERBOSE" = "1" ]; then
201			printf "\nNo entry found\n"
202			printf "Expected:\n"
203			printf "    ${expected}\n"
204		fi
205		return 1
206	fi
207
208	out=$(echo ${out})
209	if [ "${out}" != "${expected}" ]; then
210		rc=1
211		if [ "${VERBOSE}" = "1" ]; then
212			printf "    Unexpected entry. Have:\n"
213			printf "        ${out}\n"
214			printf "    Expected:\n"
215			printf "        ${expected}\n\n"
216		else
217			echo "      WARNING: Unexpected route entry"
218		fi
219	fi
220
221	return $rc
222}
223
224check_nexthop()
225{
226	local nharg="$1"
227	local expected="$2"
228	local out
229
230	out=$($IP nexthop ls ${nharg} 2>/dev/null)
231
232	check_output "${out}" "${expected}"
233}
234
235check_route()
236{
237	local pfx="$1"
238	local expected="$2"
239	local out
240
241	out=$($IP route ls match ${pfx} 2>/dev/null)
242
243	check_output "${out}" "${expected}"
244}
245
246check_route6()
247{
248	local pfx="$1"
249	local expected="$2"
250	local out
251
252	out=$($IP -6 route ls match ${pfx} 2>/dev/null | sed -e 's/pref medium//')
253
254	check_output "${out}" "${expected}"
255}
256
257check_large_grp()
258{
259	local ipv=$1
260	local ecmp=$2
261	local grpnum=100
262	local nhidstart=100
263	local grpidstart=1000
264	local iter=0
265	local nhidstr=""
266	local grpidstr=""
267	local grpstr=""
268	local ipstr=""
269
270	if [ $ipv -eq 4 ]; then
271		ipstr="172.16.1."
272	else
273		ipstr="2001:db8:91::"
274	fi
275
276	#
277	# Create $grpnum groups with specified $ecmp and dump them
278	#
279
280	# create nexthops with different gateways
281	iter=2
282	while [ $iter -le $(($ecmp + 1)) ]
283	do
284		nhidstr="$(($nhidstart + $iter))"
285		run_cmd "$IP nexthop add id $nhidstr via $ipstr$iter dev veth1"
286		check_nexthop "id $nhidstr" "id $nhidstr via $ipstr$iter dev veth1 scope link"
287
288		if [ $iter -le $ecmp ]; then
289			grpstr+="$nhidstr/"
290		else
291			grpstr+="$nhidstr"
292		fi
293		((iter++))
294	done
295
296	# create duplicate large ecmp groups
297	iter=0
298	while [ $iter -le $grpnum ]
299	do
300		grpidstr="$(($grpidstart + $iter))"
301		run_cmd "$IP nexthop add id $grpidstr group $grpstr"
302		check_nexthop "id $grpidstr" "id $grpidstr group $grpstr"
303		((iter++))
304	done
305
306	# dump large groups
307	run_cmd "$IP nexthop list"
308	log_test $? 0 "Dump large (x$ecmp) ecmp groups"
309}
310
311start_ip_monitor()
312{
313	local mtype=$1
314
315	# start the monitor in the background
316	tmpfile=`mktemp /var/run/nexthoptestXXX`
317	mpid=`($IP monitor $mtype > $tmpfile & echo $!) 2>/dev/null`
318	sleep 0.2
319	echo "$mpid $tmpfile"
320}
321
322stop_ip_monitor()
323{
324	local mpid=$1
325	local tmpfile=$2
326	local el=$3
327
328	# check the monitor results
329	kill $mpid
330	lines=`wc -l $tmpfile | cut "-d " -f1`
331	test $lines -eq $el
332	rc=$?
333	rm -rf $tmpfile
334
335	return $rc
336}
337
338check_nexthop_fdb_support()
339{
340	$IP nexthop help 2>&1 | grep -q fdb
341	if [ $? -ne 0 ]; then
342		echo "SKIP: iproute2 too old, missing fdb nexthop support"
343		return $ksft_skip
344	fi
345}
346
347ipv6_fdb_grp_fcnal()
348{
349	local rc
350
351	echo
352	echo "IPv6 fdb groups functional"
353	echo "--------------------------"
354
355	check_nexthop_fdb_support
356	if [ $? -eq $ksft_skip ]; then
357		return $ksft_skip
358	fi
359
360	# create group with multiple nexthops
361	run_cmd "$IP nexthop add id 61 via 2001:db8:91::2 fdb"
362	run_cmd "$IP nexthop add id 62 via 2001:db8:91::3 fdb"
363	run_cmd "$IP nexthop add id 102 group 61/62 fdb"
364	check_nexthop "id 102" "id 102 group 61/62 fdb"
365	log_test $? 0 "Fdb Nexthop group with multiple nexthops"
366
367	## get nexthop group
368	run_cmd "$IP nexthop get id 102"
369	check_nexthop "id 102" "id 102 group 61/62 fdb"
370	log_test $? 0 "Get Fdb nexthop group by id"
371
372	# fdb nexthop group can only contain fdb nexthops
373	run_cmd "$IP nexthop add id 63 via 2001:db8:91::4"
374	run_cmd "$IP nexthop add id 64 via 2001:db8:91::5"
375	run_cmd "$IP nexthop add id 103 group 63/64 fdb"
376	log_test $? 2 "Fdb Nexthop group with non-fdb nexthops"
377
378	# Non fdb nexthop group can not contain fdb nexthops
379	run_cmd "$IP nexthop add id 65 via 2001:db8:91::5 fdb"
380	run_cmd "$IP nexthop add id 66 via 2001:db8:91::6 fdb"
381	run_cmd "$IP nexthop add id 104 group 65/66"
382	log_test $? 2 "Non-Fdb Nexthop group with fdb nexthops"
383
384	# fdb nexthop cannot have blackhole
385	run_cmd "$IP nexthop add id 67 blackhole fdb"
386	log_test $? 2 "Fdb Nexthop with blackhole"
387
388	# fdb nexthop with oif
389	run_cmd "$IP nexthop add id 68 via 2001:db8:91::7 dev veth1 fdb"
390	log_test $? 2 "Fdb Nexthop with oif"
391
392	# fdb nexthop with onlink
393	run_cmd "$IP nexthop add id 68 via 2001:db8:91::7 onlink fdb"
394	log_test $? 2 "Fdb Nexthop with onlink"
395
396	# fdb nexthop with encap
397	run_cmd "$IP nexthop add id 69 encap mpls 101 via 2001:db8:91::8 dev veth1 fdb"
398	log_test $? 2 "Fdb Nexthop with encap"
399
400	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"
401	run_cmd "$BRIDGE fdb add 02:02:00:00:00:13 dev vx10 nhid 102 self"
402	log_test $? 0 "Fdb mac add with nexthop group"
403
404	## fdb nexthops can only reference nexthop groups and not nexthops
405	run_cmd "$BRIDGE fdb add 02:02:00:00:00:14 dev vx10 nhid 61 self"
406	log_test $? 255 "Fdb mac add with nexthop"
407
408	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 66"
409	log_test $? 2 "Route add with fdb nexthop"
410
411	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 103"
412	log_test $? 2 "Route add with fdb nexthop group"
413
414	run_cmd "$IP nexthop del id 61"
415	run_cmd "$BRIDGE fdb get to 02:02:00:00:00:13 dev vx10 self"
416	log_test $? 0 "Fdb entry after deleting a single nexthop"
417
418	run_cmd "$IP nexthop del id 102"
419	log_test $? 0 "Fdb nexthop delete"
420
421	run_cmd "$BRIDGE fdb get to 02:02:00:00:00:13 dev vx10 self"
422	log_test $? 254 "Fdb entry after deleting a nexthop group"
423
424	$IP link del dev vx10
425}
426
427ipv4_fdb_grp_fcnal()
428{
429	local rc
430
431	echo
432	echo "IPv4 fdb groups functional"
433	echo "--------------------------"
434
435	check_nexthop_fdb_support
436	if [ $? -eq $ksft_skip ]; then
437		return $ksft_skip
438	fi
439
440	# create group with multiple nexthops
441	run_cmd "$IP nexthop add id 12 via 172.16.1.2 fdb"
442	run_cmd "$IP nexthop add id 13 via 172.16.1.3 fdb"
443	run_cmd "$IP nexthop add id 102 group 12/13 fdb"
444	check_nexthop "id 102" "id 102 group 12/13 fdb"
445	log_test $? 0 "Fdb Nexthop group with multiple nexthops"
446
447	# get nexthop group
448	run_cmd "$IP nexthop get id 102"
449	check_nexthop "id 102" "id 102 group 12/13 fdb"
450	log_test $? 0 "Get Fdb nexthop group by id"
451
452	# fdb nexthop group can only contain fdb nexthops
453	run_cmd "$IP nexthop add id 14 via 172.16.1.2"
454	run_cmd "$IP nexthop add id 15 via 172.16.1.3"
455	run_cmd "$IP nexthop add id 103 group 14/15 fdb"
456	log_test $? 2 "Fdb Nexthop group with non-fdb nexthops"
457
458	# Non fdb nexthop group can not contain fdb nexthops
459	run_cmd "$IP nexthop add id 16 via 172.16.1.2 fdb"
460	run_cmd "$IP nexthop add id 17 via 172.16.1.3 fdb"
461	run_cmd "$IP nexthop add id 104 group 14/15"
462	log_test $? 2 "Non-Fdb Nexthop group with fdb nexthops"
463
464	# fdb nexthop cannot have blackhole
465	run_cmd "$IP nexthop add id 18 blackhole fdb"
466	log_test $? 2 "Fdb Nexthop with blackhole"
467
468	# fdb nexthop with oif
469	run_cmd "$IP nexthop add id 16 via 172.16.1.2 dev veth1 fdb"
470	log_test $? 2 "Fdb Nexthop with oif"
471
472	# fdb nexthop with onlink
473	run_cmd "$IP nexthop add id 16 via 172.16.1.2 onlink fdb"
474	log_test $? 2 "Fdb Nexthop with onlink"
475
476	# fdb nexthop with encap
477	run_cmd "$IP nexthop add id 17 encap mpls 101 via 172.16.1.2 dev veth1 fdb"
478	log_test $? 2 "Fdb Nexthop with encap"
479
480	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"
481	run_cmd "$BRIDGE fdb add 02:02:00:00:00:13 dev vx10 nhid 102 self"
482	log_test $? 0 "Fdb mac add with nexthop group"
483
484	# fdb nexthops can only reference nexthop groups and not nexthops
485	run_cmd "$BRIDGE fdb add 02:02:00:00:00:14 dev vx10 nhid 12 self"
486	log_test $? 255 "Fdb mac add with nexthop"
487
488	run_cmd "$IP ro add 172.16.0.0/22 nhid 15"
489	log_test $? 2 "Route add with fdb nexthop"
490
491	run_cmd "$IP ro add 172.16.0.0/22 nhid 103"
492	log_test $? 2 "Route add with fdb nexthop group"
493
494	run_cmd "$IP nexthop del id 12"
495	run_cmd "$BRIDGE fdb get to 02:02:00:00:00:13 dev vx10 self"
496	log_test $? 0 "Fdb entry after deleting a single nexthop"
497
498	run_cmd "$IP nexthop del id 102"
499	log_test $? 0 "Fdb nexthop delete"
500
501	run_cmd "$BRIDGE fdb get to 02:02:00:00:00:13 dev vx10 self"
502	log_test $? 254 "Fdb entry after deleting a nexthop group"
503
504	$IP link del dev vx10
505}
506
507################################################################################
508# basic operations (add, delete, replace) on nexthops and nexthop groups
509#
510# IPv6
511
512ipv6_fcnal()
513{
514	local rc
515
516	echo
517	echo "IPv6"
518	echo "----------------------"
519
520	run_cmd "$IP nexthop add id 52 via 2001:db8:91::2 dev veth1"
521	rc=$?
522	log_test $rc 0 "Create nexthop with id, gw, dev"
523	if [ $rc -ne 0 ]; then
524		echo "Basic IPv6 create fails; can not continue"
525		return 1
526	fi
527
528	run_cmd "$IP nexthop get id 52"
529	log_test $? 0 "Get nexthop by id"
530	check_nexthop "id 52" "id 52 via 2001:db8:91::2 dev veth1 scope link"
531
532	run_cmd "$IP nexthop del id 52"
533	log_test $? 0 "Delete nexthop by id"
534	check_nexthop "id 52" ""
535
536	#
537	# gw, device spec
538	#
539	# gw validation, no device - fails since dev required
540	run_cmd "$IP nexthop add id 52 via 2001:db8:92::3"
541	log_test $? 2 "Create nexthop - gw only"
542
543	# gw is not reachable throught given dev
544	run_cmd "$IP nexthop add id 53 via 2001:db8:3::3 dev veth1"
545	log_test $? 2 "Create nexthop - invalid gw+dev combination"
546
547	# onlink arg overrides gw+dev lookup
548	run_cmd "$IP nexthop add id 53 via 2001:db8:3::3 dev veth1 onlink"
549	log_test $? 0 "Create nexthop - gw+dev and onlink"
550
551	# admin down should delete nexthops
552	set -e
553	run_cmd "$IP -6 nexthop add id 55 via 2001:db8:91::3 dev veth1"
554	run_cmd "$IP nexthop add id 56 via 2001:db8:91::4 dev veth1"
555	run_cmd "$IP nexthop add id 57 via 2001:db8:91::5 dev veth1"
556	run_cmd "$IP li set dev veth1 down"
557	set +e
558	check_nexthop "dev veth1" ""
559	log_test $? 0 "Nexthops removed on admin down"
560}
561
562ipv6_grp_fcnal()
563{
564	local rc
565
566	echo
567	echo "IPv6 groups functional"
568	echo "----------------------"
569
570	# basic functionality: create a nexthop group, default weight
571	run_cmd "$IP nexthop add id 61 via 2001:db8:91::2 dev veth1"
572	run_cmd "$IP nexthop add id 101 group 61"
573	log_test $? 0 "Create nexthop group with single nexthop"
574
575	# get nexthop group
576	run_cmd "$IP nexthop get id 101"
577	log_test $? 0 "Get nexthop group by id"
578	check_nexthop "id 101" "id 101 group 61"
579
580	# delete nexthop group
581	run_cmd "$IP nexthop del id 101"
582	log_test $? 0 "Delete nexthop group by id"
583	check_nexthop "id 101" ""
584
585	$IP nexthop flush >/dev/null 2>&1
586	check_nexthop "id 101" ""
587
588	#
589	# create group with multiple nexthops - mix of gw and dev only
590	#
591	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
592	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
593	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
594	run_cmd "$IP nexthop add id 65 dev veth1"
595	run_cmd "$IP nexthop add id 102 group 62/63/64/65"
596	log_test $? 0 "Nexthop group with multiple nexthops"
597	check_nexthop "id 102" "id 102 group 62/63/64/65"
598
599	# Delete nexthop in a group and group is updated
600	run_cmd "$IP nexthop del id 63"
601	check_nexthop "id 102" "id 102 group 62/64/65"
602	log_test $? 0 "Nexthop group updated when entry is deleted"
603
604	# create group with multiple weighted nexthops
605	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
606	run_cmd "$IP nexthop add id 103 group 62/63,2/64,3/65,4"
607	log_test $? 0 "Nexthop group with weighted nexthops"
608	check_nexthop "id 103" "id 103 group 62/63,2/64,3/65,4"
609
610	# Delete nexthop in a weighted group and group is updated
611	run_cmd "$IP nexthop del id 63"
612	check_nexthop "id 103" "id 103 group 62/64,3/65,4"
613	log_test $? 0 "Weighted nexthop group updated when entry is deleted"
614
615	# admin down - nexthop is removed from group
616	run_cmd "$IP li set dev veth1 down"
617	check_nexthop "dev veth1" ""
618	log_test $? 0 "Nexthops in groups removed on admin down"
619
620	# expect groups to have been deleted as well
621	check_nexthop "" ""
622
623	run_cmd "$IP li set dev veth1 up"
624
625	$IP nexthop flush >/dev/null 2>&1
626
627	# group with nexthops using different devices
628	set -e
629	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
630	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
631	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
632	run_cmd "$IP nexthop add id 65 via 2001:db8:91::5 dev veth1"
633
634	run_cmd "$IP nexthop add id 72 via 2001:db8:92::2 dev veth3"
635	run_cmd "$IP nexthop add id 73 via 2001:db8:92::3 dev veth3"
636	run_cmd "$IP nexthop add id 74 via 2001:db8:92::4 dev veth3"
637	run_cmd "$IP nexthop add id 75 via 2001:db8:92::5 dev veth3"
638	set +e
639
640	# multiple groups with same nexthop
641	run_cmd "$IP nexthop add id 104 group 62"
642	run_cmd "$IP nexthop add id 105 group 62"
643	check_nexthop "group" "id 104 group 62 id 105 group 62"
644	log_test $? 0 "Multiple groups with same nexthop"
645
646	run_cmd "$IP nexthop flush groups"
647	[ $? -ne 0 ] && return 1
648
649	# on admin down of veth1, it should be removed from the group
650	run_cmd "$IP nexthop add id 105 group 62/63/72/73/64"
651	run_cmd "$IP li set veth1 down"
652	check_nexthop "id 105" "id 105 group 72/73"
653	log_test $? 0 "Nexthops in group removed on admin down - mixed group"
654
655	run_cmd "$IP nexthop add id 106 group 105/74"
656	log_test $? 2 "Nexthop group can not have a group as an entry"
657
658	# a group can have a blackhole entry only if it is the only
659	# nexthop in the group. Needed for atomic replace with an
660	# actual nexthop group
661	run_cmd "$IP -6 nexthop add id 31 blackhole"
662	run_cmd "$IP nexthop add id 107 group 31"
663	log_test $? 0 "Nexthop group with a blackhole entry"
664
665	run_cmd "$IP nexthop add id 108 group 31/24"
666	log_test $? 2 "Nexthop group can not have a blackhole and another nexthop"
667}
668
669ipv6_fcnal_runtime()
670{
671	local rc
672
673	echo
674	echo "IPv6 functional runtime"
675	echo "-----------------------"
676
677	#
678	# IPv6 - the basics
679	#
680	run_cmd "$IP nexthop add id 81 via 2001:db8:91::2 dev veth1"
681	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
682	log_test $? 0 "Route add"
683
684	run_cmd "$IP ro delete 2001:db8:101::1/128 nhid 81"
685	log_test $? 0 "Route delete"
686
687	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
688	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
689	log_test $? 0 "Ping with nexthop"
690
691	run_cmd "$IP nexthop add id 82 via 2001:db8:92::2 dev veth3"
692	run_cmd "$IP nexthop add id 122 group 81/82"
693	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
694	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
695	log_test $? 0 "Ping - multipath"
696
697	#
698	# IPv6 with blackhole nexthops
699	#
700	run_cmd "$IP -6 nexthop add id 83 blackhole"
701	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 83"
702	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
703	log_test $? 2 "Ping - blackhole"
704
705	run_cmd "$IP nexthop replace id 83 via 2001:db8:91::2 dev veth1"
706	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
707	log_test $? 0 "Ping - blackhole replaced with gateway"
708
709	run_cmd "$IP -6 nexthop replace id 83 blackhole"
710	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
711	log_test $? 2 "Ping - gateway replaced by blackhole"
712
713	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
714	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
715	if [ $? -eq 0 ]; then
716		run_cmd "$IP nexthop replace id 122 group 83"
717		run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
718		log_test $? 2 "Ping - group with blackhole"
719
720		run_cmd "$IP nexthop replace id 122 group 81/82"
721		run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
722		log_test $? 0 "Ping - group blackhole replaced with gateways"
723	else
724		log_test 2 0 "Ping - multipath failed"
725	fi
726
727	#
728	# device only and gw + dev only mix
729	#
730	run_cmd "$IP -6 nexthop add id 85 dev veth1"
731	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 85"
732	log_test $? 0 "IPv6 route with device only nexthop"
733	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 85 dev veth1 metric 1024"
734
735	run_cmd "$IP nexthop add id 123 group 81/85"
736	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 123"
737	log_test $? 0 "IPv6 multipath route with nexthop mix - dev only + gw"
738	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"
739
740	#
741	# IPv6 route with v4 nexthop - not allowed
742	#
743	run_cmd "$IP ro delete 2001:db8:101::1/128"
744	run_cmd "$IP nexthop add id 84 via 172.16.1.1 dev veth1"
745	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 84"
746	log_test $? 2 "IPv6 route can not have a v4 gateway"
747
748	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 81"
749	run_cmd "$IP nexthop replace id 81 via 172.16.1.1 dev veth1"
750	log_test $? 2 "Nexthop replace - v6 route, v4 nexthop"
751
752	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
753	run_cmd "$IP nexthop replace id 81 via 172.16.1.1 dev veth1"
754	log_test $? 2 "Nexthop replace of group entry - v6 route, v4 nexthop"
755
756	run_cmd "$IP nexthop add id 86 via 2001:db8:92::2 dev veth3"
757	run_cmd "$IP nexthop add id 87 via 172.16.1.1 dev veth1"
758	run_cmd "$IP nexthop add id 88 via 172.16.1.1 dev veth1"
759	run_cmd "$IP nexthop add id 124 group 86/87/88"
760	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
761	log_test $? 2 "IPv6 route can not have a group with v4 and v6 gateways"
762
763	run_cmd "$IP nexthop del id 88"
764	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
765	log_test $? 2 "IPv6 route can not have a group with v4 and v6 gateways"
766
767	run_cmd "$IP nexthop del id 87"
768	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
769	log_test $? 0 "IPv6 route using a group after removing v4 gateways"
770
771	run_cmd "$IP ro delete 2001:db8:101::1/128"
772	run_cmd "$IP nexthop add id 87 via 172.16.1.1 dev veth1"
773	run_cmd "$IP nexthop add id 88 via 172.16.1.1 dev veth1"
774	run_cmd "$IP nexthop replace id 124 group 86/87/88"
775	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
776	log_test $? 2 "IPv6 route can not have a group with v4 and v6 gateways"
777
778	run_cmd "$IP nexthop replace id 88 via 2001:db8:92::2 dev veth3"
779	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
780	log_test $? 2 "IPv6 route can not have a group with v4 and v6 gateways"
781
782	run_cmd "$IP nexthop replace id 87 via 2001:db8:92::2 dev veth3"
783	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 124"
784	log_test $? 0 "IPv6 route using a group after replacing v4 gateways"
785
786	$IP nexthop flush >/dev/null 2>&1
787
788	#
789	# weird IPv6 cases
790	#
791	run_cmd "$IP nexthop add id 86 via 2001:db8:91::2 dev veth1"
792	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
793
794	# rpfilter and default route
795	$IP nexthop flush >/dev/null 2>&1
796	run_cmd "ip netns exec me ip6tables -t mangle -I PREROUTING 1 -m rpfilter --invert -j DROP"
797	run_cmd "$IP nexthop add id 91 via 2001:db8:91::2 dev veth1"
798	run_cmd "$IP nexthop add id 92 via 2001:db8:92::2 dev veth3"
799	run_cmd "$IP nexthop add id 93 group 91/92"
800	run_cmd "$IP -6 ro add default nhid 91"
801	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
802	log_test $? 0 "Nexthop with default route and rpfilter"
803	run_cmd "$IP -6 ro replace default nhid 93"
804	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
805	log_test $? 0 "Nexthop with multipath default route and rpfilter"
806
807	# TO-DO:
808	# existing route with old nexthop; append route with new nexthop
809	# existing route with old nexthop; replace route with new
810	# existing route with new nexthop; replace route with old
811	# route with src address and using nexthop - not allowed
812}
813
814ipv6_large_grp()
815{
816	local ecmp=32
817
818	echo
819	echo "IPv6 large groups (x$ecmp)"
820	echo "---------------------"
821
822	check_large_grp 6 $ecmp
823
824	$IP nexthop flush >/dev/null 2>&1
825}
826
827ipv6_del_add_loop1()
828{
829	while :; do
830		$IP nexthop del id 100
831		$IP nexthop add id 100 via 2001:db8:91::2 dev veth1
832	done >/dev/null 2>&1
833}
834
835ipv6_grp_replace_loop()
836{
837	while :; do
838		$IP nexthop replace id 102 group 100/101
839	done >/dev/null 2>&1
840}
841
842ipv6_torture()
843{
844	local pid1
845	local pid2
846	local pid3
847	local pid4
848	local pid5
849
850	echo
851	echo "IPv6 runtime torture"
852	echo "--------------------"
853	if [ ! -x "$(command -v mausezahn)" ]; then
854		echo "SKIP: Could not run test; need mausezahn tool"
855		return
856	fi
857
858	run_cmd "$IP nexthop add id 100 via 2001:db8:91::2 dev veth1"
859	run_cmd "$IP nexthop add id 101 via 2001:db8:92::2 dev veth3"
860	run_cmd "$IP nexthop add id 102 group 100/101"
861	run_cmd "$IP route add 2001:db8:101::1 nhid 102"
862	run_cmd "$IP route add 2001:db8:101::2 nhid 102"
863
864	ipv6_del_add_loop1 &
865	pid1=$!
866	ipv6_grp_replace_loop &
867	pid2=$!
868	ip netns exec me ping -f 2001:db8:101::1 >/dev/null 2>&1 &
869	pid3=$!
870	ip netns exec me ping -f 2001:db8:101::2 >/dev/null 2>&1 &
871	pid4=$!
872	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 &
873	pid5=$!
874
875	sleep 300
876	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
877
878	# if we did not crash, success
879	log_test 0 0 "IPv6 torture test"
880}
881
882
883ipv4_fcnal()
884{
885	local rc
886
887	echo
888	echo "IPv4 functional"
889	echo "----------------------"
890
891	#
892	# basic IPv4 ops - add, get, delete
893	#
894	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
895	rc=$?
896	log_test $rc 0 "Create nexthop with id, gw, dev"
897	if [ $rc -ne 0 ]; then
898		echo "Basic IPv4 create fails; can not continue"
899		return 1
900	fi
901
902	run_cmd "$IP nexthop get id 12"
903	log_test $? 0 "Get nexthop by id"
904	check_nexthop "id 12" "id 12 via 172.16.1.2 dev veth1 scope link"
905
906	run_cmd "$IP nexthop del id 12"
907	log_test $? 0 "Delete nexthop by id"
908	check_nexthop "id 52" ""
909
910	#
911	# gw, device spec
912	#
913	# gw validation, no device - fails since dev is required
914	run_cmd "$IP nexthop add id 12 via 172.16.2.3"
915	log_test $? 2 "Create nexthop - gw only"
916
917	# gw not reachable through given dev
918	run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1"
919	log_test $? 2 "Create nexthop - invalid gw+dev combination"
920
921	# onlink flag overrides gw+dev lookup
922	run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1 onlink"
923	log_test $? 0 "Create nexthop - gw+dev and onlink"
924
925	# admin down should delete nexthops
926	set -e
927	run_cmd "$IP nexthop add id 15 via 172.16.1.3 dev veth1"
928	run_cmd "$IP nexthop add id 16 via 172.16.1.4 dev veth1"
929	run_cmd "$IP nexthop add id 17 via 172.16.1.5 dev veth1"
930	run_cmd "$IP li set dev veth1 down"
931	set +e
932	check_nexthop "dev veth1" ""
933	log_test $? 0 "Nexthops removed on admin down"
934}
935
936ipv4_grp_fcnal()
937{
938	local rc
939
940	echo
941	echo "IPv4 groups functional"
942	echo "----------------------"
943
944	# basic functionality: create a nexthop group, default weight
945	run_cmd "$IP nexthop add id 11 via 172.16.1.2 dev veth1"
946	run_cmd "$IP nexthop add id 101 group 11"
947	log_test $? 0 "Create nexthop group with single nexthop"
948
949	# get nexthop group
950	run_cmd "$IP nexthop get id 101"
951	log_test $? 0 "Get nexthop group by id"
952	check_nexthop "id 101" "id 101 group 11"
953
954	# delete nexthop group
955	run_cmd "$IP nexthop del id 101"
956	log_test $? 0 "Delete nexthop group by id"
957	check_nexthop "id 101" ""
958
959	$IP nexthop flush >/dev/null 2>&1
960
961	#
962	# create group with multiple nexthops
963	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
964	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
965	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
966	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
967	run_cmd "$IP nexthop add id 102 group 12/13/14/15"
968	log_test $? 0 "Nexthop group with multiple nexthops"
969	check_nexthop "id 102" "id 102 group 12/13/14/15"
970
971	# Delete nexthop in a group and group is updated
972	run_cmd "$IP nexthop del id 13"
973	check_nexthop "id 102" "id 102 group 12/14/15"
974	log_test $? 0 "Nexthop group updated when entry is deleted"
975
976	# create group with multiple weighted nexthops
977	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
978	run_cmd "$IP nexthop add id 103 group 12/13,2/14,3/15,4"
979	log_test $? 0 "Nexthop group with weighted nexthops"
980	check_nexthop "id 103" "id 103 group 12/13,2/14,3/15,4"
981
982	# Delete nexthop in a weighted group and group is updated
983	run_cmd "$IP nexthop del id 13"
984	check_nexthop "id 103" "id 103 group 12/14,3/15,4"
985	log_test $? 0 "Weighted nexthop group updated when entry is deleted"
986
987	# admin down - nexthop is removed from group
988	run_cmd "$IP li set dev veth1 down"
989	check_nexthop "dev veth1" ""
990	log_test $? 0 "Nexthops in groups removed on admin down"
991
992	# expect groups to have been deleted as well
993	check_nexthop "" ""
994
995	run_cmd "$IP li set dev veth1 up"
996
997	$IP nexthop flush >/dev/null 2>&1
998
999	# group with nexthops using different devices
1000	set -e
1001	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1002	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
1003	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
1004	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
1005
1006	run_cmd "$IP nexthop add id 22 via 172.16.2.2 dev veth3"
1007	run_cmd "$IP nexthop add id 23 via 172.16.2.3 dev veth3"
1008	run_cmd "$IP nexthop add id 24 via 172.16.2.4 dev veth3"
1009	run_cmd "$IP nexthop add id 25 via 172.16.2.5 dev veth3"
1010	set +e
1011
1012	# multiple groups with same nexthop
1013	run_cmd "$IP nexthop add id 104 group 12"
1014	run_cmd "$IP nexthop add id 105 group 12"
1015	check_nexthop "group" "id 104 group 12 id 105 group 12"
1016	log_test $? 0 "Multiple groups with same nexthop"
1017
1018	run_cmd "$IP nexthop flush groups"
1019	[ $? -ne 0 ] && return 1
1020
1021	# on admin down of veth1, it should be removed from the group
1022	run_cmd "$IP nexthop add id 105 group 12/13/22/23/14"
1023	run_cmd "$IP li set veth1 down"
1024	check_nexthop "id 105" "id 105 group 22/23"
1025	log_test $? 0 "Nexthops in group removed on admin down - mixed group"
1026
1027	run_cmd "$IP nexthop add id 106 group 105/24"
1028	log_test $? 2 "Nexthop group can not have a group as an entry"
1029
1030	# a group can have a blackhole entry only if it is the only
1031	# nexthop in the group. Needed for atomic replace with an
1032	# actual nexthop group
1033	run_cmd "$IP nexthop add id 31 blackhole"
1034	run_cmd "$IP nexthop add id 107 group 31"
1035	log_test $? 0 "Nexthop group with a blackhole entry"
1036
1037	run_cmd "$IP nexthop add id 108 group 31/24"
1038	log_test $? 2 "Nexthop group can not have a blackhole and another nexthop"
1039}
1040
1041ipv4_withv6_fcnal()
1042{
1043	local lladdr
1044
1045	set -e
1046	lladdr=$(get_linklocal veth2 peer)
1047	run_cmd "$IP nexthop add id 11 via ${lladdr} dev veth1"
1048	set +e
1049	run_cmd "$IP ro add 172.16.101.1/32 nhid 11"
1050	log_test $? 0 "IPv6 nexthop with IPv4 route"
1051	check_route "172.16.101.1" "172.16.101.1 nhid 11 via inet6 ${lladdr} dev veth1"
1052
1053	set -e
1054	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1055	run_cmd "$IP nexthop add id 101 group 11/12"
1056	set +e
1057	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
1058	log_test $? 0 "IPv6 nexthop with IPv4 route"
1059
1060	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"
1061
1062	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1063	log_test $? 0 "IPv4 route with IPv6 gateway"
1064	check_route "172.16.101.1" "172.16.101.1 via inet6 ${lladdr} dev veth1"
1065
1066	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 2001:db8:50::1 dev veth1"
1067	log_test $? 2 "IPv4 route with invalid IPv6 gateway"
1068}
1069
1070ipv4_fcnal_runtime()
1071{
1072	local lladdr
1073	local rc
1074
1075	echo
1076	echo "IPv4 functional runtime"
1077	echo "-----------------------"
1078
1079	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
1080	run_cmd "$IP ro add 172.16.101.1/32 nhid 21"
1081	log_test $? 0 "Route add"
1082	check_route "172.16.101.1" "172.16.101.1 nhid 21 via 172.16.1.2 dev veth1"
1083
1084	run_cmd "$IP ro delete 172.16.101.1/32 nhid 21"
1085	log_test $? 0 "Route delete"
1086
1087	#
1088	# scope mismatch
1089	#
1090	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
1091	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
1092	log_test $? 2 "Route add - scope conflict with nexthop"
1093
1094	run_cmd "$IP nexthop replace id 22 dev veth3"
1095	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
1096	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
1097	log_test $? 2 "Nexthop replace with invalid scope for existing route"
1098
1099	#
1100	# add route with nexthop and check traffic
1101	#
1102	run_cmd "$IP nexthop replace id 21 via 172.16.1.2 dev veth1"
1103	run_cmd "$IP ro replace 172.16.101.1/32 nhid 21"
1104	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1105	log_test $? 0 "Basic ping"
1106
1107	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
1108	run_cmd "$IP nexthop add id 122 group 21/22"
1109	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
1110	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1111	log_test $? 0 "Ping - multipath"
1112
1113	run_cmd "$IP ro delete 172.16.101.1/32 nhid 122"
1114
1115	#
1116	# multiple default routes
1117	# - tests fib_select_default
1118	run_cmd "$IP nexthop add id 501 via 172.16.1.2 dev veth1"
1119	run_cmd "$IP ro add default nhid 501"
1120	run_cmd "$IP ro add default via 172.16.1.3 dev veth1 metric 20"
1121	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1122	log_test $? 0 "Ping - multiple default routes, nh first"
1123
1124	# flip the order
1125	run_cmd "$IP ro del default nhid 501"
1126	run_cmd "$IP ro del default via 172.16.1.3 dev veth1 metric 20"
1127	run_cmd "$IP ro add default via 172.16.1.2 dev veth1 metric 20"
1128	run_cmd "$IP nexthop replace id 501 via 172.16.1.3 dev veth1"
1129	run_cmd "$IP ro add default nhid 501 metric 20"
1130	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1131	log_test $? 0 "Ping - multiple default routes, nh second"
1132
1133	run_cmd "$IP nexthop delete nhid 501"
1134	run_cmd "$IP ro del default"
1135
1136	#
1137	# IPv4 with blackhole nexthops
1138	#
1139	run_cmd "$IP nexthop add id 23 blackhole"
1140	run_cmd "$IP ro replace 172.16.101.1/32 nhid 23"
1141	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1142	log_test $? 2 "Ping - blackhole"
1143
1144	run_cmd "$IP nexthop replace id 23 via 172.16.1.2 dev veth1"
1145	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1146	log_test $? 0 "Ping - blackhole replaced with gateway"
1147
1148	run_cmd "$IP nexthop replace id 23 blackhole"
1149	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1150	log_test $? 2 "Ping - gateway replaced by blackhole"
1151
1152	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
1153	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1154	if [ $? -eq 0 ]; then
1155		run_cmd "$IP nexthop replace id 122 group 23"
1156		run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1157		log_test $? 2 "Ping - group with blackhole"
1158
1159		run_cmd "$IP nexthop replace id 122 group 21/22"
1160		run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1161		log_test $? 0 "Ping - group blackhole replaced with gateways"
1162	else
1163		log_test 2 0 "Ping - multipath failed"
1164	fi
1165
1166	#
1167	# device only and gw + dev only mix
1168	#
1169	run_cmd "$IP nexthop add id 85 dev veth1"
1170	run_cmd "$IP ro replace 172.16.101.1/32 nhid 85"
1171	log_test $? 0 "IPv4 route with device only nexthop"
1172	check_route "172.16.101.1" "172.16.101.1 nhid 85 dev veth1"
1173
1174	run_cmd "$IP nexthop add id 123 group 21/85"
1175	run_cmd "$IP ro replace 172.16.101.1/32 nhid 123"
1176	log_test $? 0 "IPv4 multipath route with nexthop mix - dev only + gw"
1177	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"
1178
1179	#
1180	# IPv4 with IPv6
1181	#
1182	set -e
1183	lladdr=$(get_linklocal veth2 peer)
1184	run_cmd "$IP nexthop add id 24 via ${lladdr} dev veth1"
1185	set +e
1186	run_cmd "$IP ro replace 172.16.101.1/32 nhid 24"
1187	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1188	log_test $? 0 "IPv6 nexthop with IPv4 route"
1189
1190	$IP neigh sh | grep -q "${lladdr} dev veth1"
1191	if [ $? -eq 1 ]; then
1192		echo "    WARNING: Neigh entry missing for ${lladdr}"
1193		$IP neigh sh | grep 'dev veth1'
1194	fi
1195
1196	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
1197	if [ $? -eq 0 ]; then
1198		echo "    WARNING: Neigh entry exists for 172.16.101.1"
1199		$IP neigh sh | grep 'dev veth1'
1200	fi
1201
1202	set -e
1203	run_cmd "$IP nexthop add id 25 via 172.16.1.2 dev veth1"
1204	run_cmd "$IP nexthop add id 101 group 24/25"
1205	set +e
1206	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
1207	log_test $? 0 "IPv4 route with mixed v4-v6 multipath route"
1208
1209	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"
1210
1211	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1212	log_test $? 0 "IPv6 nexthop with IPv4 route"
1213
1214	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1215	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1216	log_test $? 0 "IPv4 route with IPv6 gateway"
1217
1218	$IP neigh sh | grep -q "${lladdr} dev veth1"
1219	if [ $? -eq 1 ]; then
1220		echo "    WARNING: Neigh entry missing for ${lladdr}"
1221		$IP neigh sh | grep 'dev veth1'
1222	fi
1223
1224	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
1225	if [ $? -eq 0 ]; then
1226		echo "    WARNING: Neigh entry exists for 172.16.101.1"
1227		$IP neigh sh | grep 'dev veth1'
1228	fi
1229
1230	run_cmd "$IP ro del 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1231	run_cmd "$IP -4 ro add default via inet6 ${lladdr} dev veth1"
1232	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1233	log_test $? 0 "IPv4 default route with IPv6 gateway"
1234
1235	#
1236	# MPLS as an example of LWT encap
1237	#
1238	run_cmd "$IP nexthop add id 51 encap mpls 101 via 172.16.1.2 dev veth1"
1239	log_test $? 0 "IPv4 route with MPLS encap"
1240	check_nexthop "id 51" "id 51 encap mpls 101 via 172.16.1.2 dev veth1 scope link"
1241	log_test $? 0 "IPv4 route with MPLS encap - check"
1242
1243	run_cmd "$IP nexthop add id 52 encap mpls 102 via inet6 2001:db8:91::2 dev veth1"
1244	log_test $? 0 "IPv4 route with MPLS encap and v6 gateway"
1245	check_nexthop "id 52" "id 52 encap mpls 102 via 2001:db8:91::2 dev veth1 scope link"
1246	log_test $? 0 "IPv4 route with MPLS encap, v6 gw - check"
1247}
1248
1249ipv4_large_grp()
1250{
1251	local ecmp=32
1252
1253	echo
1254	echo "IPv4 large groups (x$ecmp)"
1255	echo "---------------------"
1256
1257	check_large_grp 4 $ecmp
1258
1259	$IP nexthop flush >/dev/null 2>&1
1260}
1261
1262sysctl_nexthop_compat_mode_check()
1263{
1264	local sysctlname="net.ipv4.nexthop_compat_mode"
1265	local lprefix=$1
1266
1267	IPE="ip netns exec me"
1268
1269	$IPE sysctl -q $sysctlname 2>&1 >/dev/null
1270	if [ $? -ne 0 ]; then
1271		echo "SKIP: kernel lacks nexthop compat mode sysctl control"
1272		return $ksft_skip
1273	fi
1274
1275	out=$($IPE sysctl $sysctlname 2>/dev/null)
1276	log_test $? 0 "$lprefix default nexthop compat mode check"
1277	check_output "${out}" "$sysctlname = 1"
1278}
1279
1280sysctl_nexthop_compat_mode_set()
1281{
1282	local sysctlname="net.ipv4.nexthop_compat_mode"
1283	local mode=$1
1284	local lprefix=$2
1285
1286	IPE="ip netns exec me"
1287
1288	out=$($IPE sysctl -w $sysctlname=$mode)
1289	log_test $? 0 "$lprefix set compat mode - $mode"
1290	check_output "${out}" "net.ipv4.nexthop_compat_mode = $mode"
1291}
1292
1293ipv6_compat_mode()
1294{
1295	local rc
1296
1297	echo
1298	echo "IPv6 nexthop api compat mode test"
1299	echo "--------------------------------"
1300
1301	sysctl_nexthop_compat_mode_check "IPv6"
1302	if [ $? -eq $ksft_skip ]; then
1303		return $ksft_skip
1304	fi
1305
1306	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
1307	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
1308	run_cmd "$IP nexthop add id 122 group 62/63"
1309	ipmout=$(start_ip_monitor route)
1310
1311	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 122"
1312	# route add notification should contain expanded nexthops
1313	stop_ip_monitor $ipmout 3
1314	log_test $? 0 "IPv6 compat mode on - route add notification"
1315
1316	# route dump should contain expanded nexthops
1317	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"
1318	log_test $? 0 "IPv6 compat mode on - route dump"
1319
1320	# change in nexthop group should generate route notification
1321	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
1322	ipmout=$(start_ip_monitor route)
1323	run_cmd "$IP nexthop replace id 122 group 62/64"
1324	stop_ip_monitor $ipmout 3
1325
1326	log_test $? 0 "IPv6 compat mode on - nexthop change"
1327
1328	# set compat mode off
1329	sysctl_nexthop_compat_mode_set 0 "IPv6"
1330
1331	run_cmd "$IP -6 ro del 2001:db8:101::1/128 nhid 122"
1332
1333	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
1334	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
1335	run_cmd "$IP nexthop add id 122 group 62/63"
1336	ipmout=$(start_ip_monitor route)
1337
1338	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 122"
1339	# route add notification should not contain expanded nexthops
1340	stop_ip_monitor $ipmout 1
1341	log_test $? 0 "IPv6 compat mode off - route add notification"
1342
1343	# route dump should not contain expanded nexthops
1344	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 122 metric 1024"
1345	log_test $? 0 "IPv6 compat mode off - route dump"
1346
1347	# change in nexthop group should not generate route notification
1348	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
1349	ipmout=$(start_ip_monitor route)
1350	run_cmd "$IP nexthop replace id 122 group 62/64"
1351	stop_ip_monitor $ipmout 0
1352	log_test $? 0 "IPv6 compat mode off - nexthop change"
1353
1354	# nexthop delete should not generate route notification
1355	ipmout=$(start_ip_monitor route)
1356	run_cmd "$IP nexthop del id 122"
1357	stop_ip_monitor $ipmout 0
1358	log_test $? 0 "IPv6 compat mode off - nexthop delete"
1359
1360	# set compat mode back on
1361	sysctl_nexthop_compat_mode_set 1 "IPv6"
1362}
1363
1364ipv4_compat_mode()
1365{
1366	local rc
1367
1368	echo
1369	echo "IPv4 nexthop api compat mode"
1370	echo "----------------------------"
1371
1372	sysctl_nexthop_compat_mode_check "IPv4"
1373	if [ $? -eq $ksft_skip ]; then
1374		return $ksft_skip
1375	fi
1376
1377	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
1378	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
1379	run_cmd "$IP nexthop add id 122 group 21/22"
1380	ipmout=$(start_ip_monitor route)
1381
1382	run_cmd "$IP ro add 172.16.101.1/32 nhid 122"
1383	stop_ip_monitor $ipmout 3
1384
1385	# route add notification should contain expanded nexthops
1386	log_test $? 0 "IPv4 compat mode on - route add notification"
1387
1388	# route dump should contain expanded nexthops
1389	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"
1390	log_test $? 0 "IPv4 compat mode on - route dump"
1391
1392	# change in nexthop group should generate route notification
1393	run_cmd "$IP nexthop add id 23 via 172.16.1.3 dev veth1"
1394	ipmout=$(start_ip_monitor route)
1395	run_cmd "$IP nexthop replace id 122 group 21/23"
1396	stop_ip_monitor $ipmout 3
1397	log_test $? 0 "IPv4 compat mode on - nexthop change"
1398
1399	sysctl_nexthop_compat_mode_set 0 "IPv4"
1400
1401	# cleanup
1402	run_cmd "$IP ro del 172.16.101.1/32 nhid 122"
1403
1404	ipmout=$(start_ip_monitor route)
1405	run_cmd "$IP ro add 172.16.101.1/32 nhid 122"
1406	stop_ip_monitor $ipmout 1
1407	# route add notification should not contain expanded nexthops
1408	log_test $? 0 "IPv4 compat mode off - route add notification"
1409
1410	# route dump should not contain expanded nexthops
1411	check_route "172.16.101.1" "172.16.101.1 nhid 122"
1412	log_test $? 0 "IPv4 compat mode off - route dump"
1413
1414	# change in nexthop group should not generate route notification
1415	ipmout=$(start_ip_monitor route)
1416	run_cmd "$IP nexthop replace id 122 group 21/22"
1417	stop_ip_monitor $ipmout 0
1418	log_test $? 0 "IPv4 compat mode off - nexthop change"
1419
1420	# nexthop delete should not generate route notification
1421	ipmout=$(start_ip_monitor route)
1422	run_cmd "$IP nexthop del id 122"
1423	stop_ip_monitor $ipmout 0
1424	log_test $? 0 "IPv4 compat mode off - nexthop delete"
1425
1426	sysctl_nexthop_compat_mode_set 1 "IPv4"
1427}
1428
1429ipv4_del_add_loop1()
1430{
1431	while :; do
1432		$IP nexthop del id 100
1433		$IP nexthop add id 100 via 172.16.1.2 dev veth1
1434	done >/dev/null 2>&1
1435}
1436
1437ipv4_grp_replace_loop()
1438{
1439	while :; do
1440		$IP nexthop replace id 102 group 100/101
1441	done >/dev/null 2>&1
1442}
1443
1444ipv4_torture()
1445{
1446	local pid1
1447	local pid2
1448	local pid3
1449	local pid4
1450	local pid5
1451
1452	echo
1453	echo "IPv4 runtime torture"
1454	echo "--------------------"
1455	if [ ! -x "$(command -v mausezahn)" ]; then
1456		echo "SKIP: Could not run test; need mausezahn tool"
1457		return
1458	fi
1459
1460	run_cmd "$IP nexthop add id 100 via 172.16.1.2 dev veth1"
1461	run_cmd "$IP nexthop add id 101 via 172.16.2.2 dev veth3"
1462	run_cmd "$IP nexthop add id 102 group 100/101"
1463	run_cmd "$IP route add 172.16.101.1 nhid 102"
1464	run_cmd "$IP route add 172.16.101.2 nhid 102"
1465
1466	ipv4_del_add_loop1 &
1467	pid1=$!
1468	ipv4_grp_replace_loop &
1469	pid2=$!
1470	ip netns exec me ping -f 172.16.101.1 >/dev/null 2>&1 &
1471	pid3=$!
1472	ip netns exec me ping -f 172.16.101.2 >/dev/null 2>&1 &
1473	pid4=$!
1474	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 &
1475	pid5=$!
1476
1477	sleep 300
1478	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
1479
1480	# if we did not crash, success
1481	log_test 0 0 "IPv4 torture test"
1482}
1483
1484basic()
1485{
1486	echo
1487	echo "Basic functional tests"
1488	echo "----------------------"
1489	run_cmd "$IP nexthop ls"
1490	log_test $? 0 "List with nothing defined"
1491
1492	run_cmd "$IP nexthop get id 1"
1493	log_test $? 2 "Nexthop get on non-existent id"
1494
1495	# attempt to create nh without a device or gw - fails
1496	run_cmd "$IP nexthop add id 1"
1497	log_test $? 2 "Nexthop with no device or gateway"
1498
1499	# attempt to create nh with down device - fails
1500	$IP li set veth1 down
1501	run_cmd "$IP nexthop add id 1 dev veth1"
1502	log_test $? 2 "Nexthop with down device"
1503
1504	# create nh with linkdown device - fails
1505	$IP li set veth1 up
1506	ip -netns peer li set veth2 down
1507	run_cmd "$IP nexthop add id 1 dev veth1"
1508	log_test $? 2 "Nexthop with device that is linkdown"
1509	ip -netns peer li set veth2 up
1510
1511	# device only
1512	run_cmd "$IP nexthop add id 1 dev veth1"
1513	log_test $? 0 "Nexthop with device only"
1514
1515	# create nh with duplicate id
1516	run_cmd "$IP nexthop add id 1 dev veth3"
1517	log_test $? 2 "Nexthop with duplicate id"
1518
1519	# blackhole nexthop
1520	run_cmd "$IP nexthop add id 2 blackhole"
1521	log_test $? 0 "Blackhole nexthop"
1522
1523	# blackhole nexthop can not have other specs
1524	run_cmd "$IP nexthop replace id 2 blackhole dev veth1"
1525	log_test $? 2 "Blackhole nexthop with other attributes"
1526
1527	#
1528	# groups
1529	#
1530
1531	run_cmd "$IP nexthop add id 101 group 1"
1532	log_test $? 0 "Create group"
1533
1534	run_cmd "$IP nexthop add id 102 group 2"
1535	log_test $? 0 "Create group with blackhole nexthop"
1536
1537	# multipath group can not have a blackhole as 1 path
1538	run_cmd "$IP nexthop add id 103 group 1/2"
1539	log_test $? 2 "Create multipath group where 1 path is a blackhole"
1540
1541	# multipath group can not have a member replaced by a blackhole
1542	run_cmd "$IP nexthop replace id 2 dev veth3"
1543	run_cmd "$IP nexthop replace id 102 group 1/2"
1544	run_cmd "$IP nexthop replace id 2 blackhole"
1545	log_test $? 2 "Multipath group can not have a member replaced by blackhole"
1546
1547	# attempt to create group with non-existent nexthop
1548	run_cmd "$IP nexthop add id 103 group 12"
1549	log_test $? 2 "Create group with non-existent nexthop"
1550
1551	# attempt to create group with same nexthop
1552	run_cmd "$IP nexthop add id 103 group 1/1"
1553	log_test $? 2 "Create group with same nexthop multiple times"
1554
1555	# replace nexthop with a group - fails
1556	run_cmd "$IP nexthop replace id 2 group 1"
1557	log_test $? 2 "Replace nexthop with nexthop group"
1558
1559	# replace nexthop group with a nexthop - fails
1560	run_cmd "$IP nexthop replace id 101 dev veth1"
1561	log_test $? 2 "Replace nexthop group with nexthop"
1562
1563	# nexthop group with other attributes fail
1564	run_cmd "$IP nexthop add id 104 group 1 dev veth1"
1565	log_test $? 2 "Nexthop group and device"
1566
1567	# Tests to ensure that flushing works as expected.
1568	run_cmd "$IP nexthop add id 105 blackhole proto 99"
1569	run_cmd "$IP nexthop add id 106 blackhole proto 100"
1570	run_cmd "$IP nexthop add id 107 blackhole proto 99"
1571	run_cmd "$IP nexthop flush proto 99"
1572	check_nexthop "id 105" ""
1573	check_nexthop "id 106" "id 106 blackhole proto 100"
1574	check_nexthop "id 107" ""
1575	run_cmd "$IP nexthop flush proto 100"
1576	check_nexthop "id 106" ""
1577
1578	run_cmd "$IP nexthop flush proto 100"
1579	log_test $? 0 "Test proto flush"
1580
1581	run_cmd "$IP nexthop add id 104 group 1 blackhole"
1582	log_test $? 2 "Nexthop group and blackhole"
1583
1584	$IP nexthop flush >/dev/null 2>&1
1585}
1586
1587################################################################################
1588# usage
1589
1590usage()
1591{
1592	cat <<EOF
1593usage: ${0##*/} OPTS
1594
1595        -t <test>   Test(s) to run (default: all)
1596                    (options: $ALL_TESTS)
1597        -4          IPv4 tests only
1598        -6          IPv6 tests only
1599        -p          Pause on fail
1600        -P          Pause after each test before cleanup
1601        -v          verbose mode (show commands and output)
1602
1603    Runtime test
1604	-n num	    Number of nexthops to target
1605	-N    	    Use new style to install routes in DUT
1606
1607done
1608EOF
1609}
1610
1611################################################################################
1612# main
1613
1614while getopts :t:pP46hv o
1615do
1616	case $o in
1617		t) TESTS=$OPTARG;;
1618		4) TESTS=${IPV4_TESTS};;
1619		6) TESTS=${IPV6_TESTS};;
1620		p) PAUSE_ON_FAIL=yes;;
1621		P) PAUSE=yes;;
1622		v) VERBOSE=$(($VERBOSE + 1));;
1623		h) usage; exit 0;;
1624		*) usage; exit 1;;
1625	esac
1626done
1627
1628# make sure we don't pause twice
1629[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
1630
1631if [ "$(id -u)" -ne 0 ];then
1632	echo "SKIP: Need root privileges"
1633	exit $ksft_skip;
1634fi
1635
1636if [ ! -x "$(command -v ip)" ]; then
1637	echo "SKIP: Could not run test without ip tool"
1638	exit $ksft_skip
1639fi
1640
1641ip help 2>&1 | grep -q nexthop
1642if [ $? -ne 0 ]; then
1643	echo "SKIP: iproute2 too old, missing nexthop command"
1644	exit $ksft_skip
1645fi
1646
1647out=$(ip nexthop ls 2>&1 | grep -q "Operation not supported")
1648if [ $? -eq 0 ]; then
1649	echo "SKIP: kernel lacks nexthop support"
1650	exit $ksft_skip
1651fi
1652
1653for t in $TESTS
1654do
1655	case $t in
1656	none) IP="ip -netns peer"; setup; exit 0;;
1657	*) setup; $t; cleanup;;
1658	esac
1659done
1660
1661if [ "$TESTS" != "none" ]; then
1662	printf "\nTests passed: %3d\n" ${nsuccess}
1663	printf "Tests failed: %3d\n"   ${nfail}
1664fi
1665
1666exit $ret
1667