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 102"
415	log_test $? 0 "Fdb nexthop delete"
416
417	$IP link del dev vx10
418}
419
420ipv4_fdb_grp_fcnal()
421{
422	local rc
423
424	echo
425	echo "IPv4 fdb groups functional"
426	echo "--------------------------"
427
428	check_nexthop_fdb_support
429	if [ $? -eq $ksft_skip ]; then
430		return $ksft_skip
431	fi
432
433	# create group with multiple nexthops
434	run_cmd "$IP nexthop add id 12 via 172.16.1.2 fdb"
435	run_cmd "$IP nexthop add id 13 via 172.16.1.3 fdb"
436	run_cmd "$IP nexthop add id 102 group 12/13 fdb"
437	check_nexthop "id 102" "id 102 group 12/13 fdb"
438	log_test $? 0 "Fdb Nexthop group with multiple nexthops"
439
440	# get nexthop group
441	run_cmd "$IP nexthop get id 102"
442	check_nexthop "id 102" "id 102 group 12/13 fdb"
443	log_test $? 0 "Get Fdb nexthop group by id"
444
445	# fdb nexthop group can only contain fdb nexthops
446	run_cmd "$IP nexthop add id 14 via 172.16.1.2"
447	run_cmd "$IP nexthop add id 15 via 172.16.1.3"
448	run_cmd "$IP nexthop add id 103 group 14/15 fdb"
449	log_test $? 2 "Fdb Nexthop group with non-fdb nexthops"
450
451	# Non fdb nexthop group can not contain fdb nexthops
452	run_cmd "$IP nexthop add id 16 via 172.16.1.2 fdb"
453	run_cmd "$IP nexthop add id 17 via 172.16.1.3 fdb"
454	run_cmd "$IP nexthop add id 104 group 14/15"
455	log_test $? 2 "Non-Fdb Nexthop group with fdb nexthops"
456
457	# fdb nexthop cannot have blackhole
458	run_cmd "$IP nexthop add id 18 blackhole fdb"
459	log_test $? 2 "Fdb Nexthop with blackhole"
460
461	# fdb nexthop with oif
462	run_cmd "$IP nexthop add id 16 via 172.16.1.2 dev veth1 fdb"
463	log_test $? 2 "Fdb Nexthop with oif"
464
465	# fdb nexthop with onlink
466	run_cmd "$IP nexthop add id 16 via 172.16.1.2 onlink fdb"
467	log_test $? 2 "Fdb Nexthop with onlink"
468
469	# fdb nexthop with encap
470	run_cmd "$IP nexthop add id 17 encap mpls 101 via 172.16.1.2 dev veth1 fdb"
471	log_test $? 2 "Fdb Nexthop with encap"
472
473	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"
474	run_cmd "$BRIDGE fdb add 02:02:00:00:00:13 dev vx10 nhid 102 self"
475	log_test $? 0 "Fdb mac add with nexthop group"
476
477	# fdb nexthops can only reference nexthop groups and not nexthops
478	run_cmd "$BRIDGE fdb add 02:02:00:00:00:14 dev vx10 nhid 12 self"
479	log_test $? 255 "Fdb mac add with nexthop"
480
481	run_cmd "$IP ro add 172.16.0.0/22 nhid 15"
482	log_test $? 2 "Route add with fdb nexthop"
483
484	run_cmd "$IP ro add 172.16.0.0/22 nhid 103"
485	log_test $? 2 "Route add with fdb nexthop group"
486
487	run_cmd "$IP nexthop del id 102"
488	log_test $? 0 "Fdb nexthop delete"
489
490	$IP link del dev vx10
491}
492
493################################################################################
494# basic operations (add, delete, replace) on nexthops and nexthop groups
495#
496# IPv6
497
498ipv6_fcnal()
499{
500	local rc
501
502	echo
503	echo "IPv6"
504	echo "----------------------"
505
506	run_cmd "$IP nexthop add id 52 via 2001:db8:91::2 dev veth1"
507	rc=$?
508	log_test $rc 0 "Create nexthop with id, gw, dev"
509	if [ $rc -ne 0 ]; then
510		echo "Basic IPv6 create fails; can not continue"
511		return 1
512	fi
513
514	run_cmd "$IP nexthop get id 52"
515	log_test $? 0 "Get nexthop by id"
516	check_nexthop "id 52" "id 52 via 2001:db8:91::2 dev veth1 scope link"
517
518	run_cmd "$IP nexthop del id 52"
519	log_test $? 0 "Delete nexthop by id"
520	check_nexthop "id 52" ""
521
522	#
523	# gw, device spec
524	#
525	# gw validation, no device - fails since dev required
526	run_cmd "$IP nexthop add id 52 via 2001:db8:92::3"
527	log_test $? 2 "Create nexthop - gw only"
528
529	# gw is not reachable throught given dev
530	run_cmd "$IP nexthop add id 53 via 2001:db8:3::3 dev veth1"
531	log_test $? 2 "Create nexthop - invalid gw+dev combination"
532
533	# onlink arg overrides gw+dev lookup
534	run_cmd "$IP nexthop add id 53 via 2001:db8:3::3 dev veth1 onlink"
535	log_test $? 0 "Create nexthop - gw+dev and onlink"
536
537	# admin down should delete nexthops
538	set -e
539	run_cmd "$IP -6 nexthop add id 55 via 2001:db8:91::3 dev veth1"
540	run_cmd "$IP nexthop add id 56 via 2001:db8:91::4 dev veth1"
541	run_cmd "$IP nexthop add id 57 via 2001:db8:91::5 dev veth1"
542	run_cmd "$IP li set dev veth1 down"
543	set +e
544	check_nexthop "dev veth1" ""
545	log_test $? 0 "Nexthops removed on admin down"
546}
547
548ipv6_grp_fcnal()
549{
550	local rc
551
552	echo
553	echo "IPv6 groups functional"
554	echo "----------------------"
555
556	# basic functionality: create a nexthop group, default weight
557	run_cmd "$IP nexthop add id 61 via 2001:db8:91::2 dev veth1"
558	run_cmd "$IP nexthop add id 101 group 61"
559	log_test $? 0 "Create nexthop group with single nexthop"
560
561	# get nexthop group
562	run_cmd "$IP nexthop get id 101"
563	log_test $? 0 "Get nexthop group by id"
564	check_nexthop "id 101" "id 101 group 61"
565
566	# delete nexthop group
567	run_cmd "$IP nexthop del id 101"
568	log_test $? 0 "Delete nexthop group by id"
569	check_nexthop "id 101" ""
570
571	$IP nexthop flush >/dev/null 2>&1
572	check_nexthop "id 101" ""
573
574	#
575	# create group with multiple nexthops - mix of gw and dev only
576	#
577	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
578	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
579	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
580	run_cmd "$IP nexthop add id 65 dev veth1"
581	run_cmd "$IP nexthop add id 102 group 62/63/64/65"
582	log_test $? 0 "Nexthop group with multiple nexthops"
583	check_nexthop "id 102" "id 102 group 62/63/64/65"
584
585	# Delete nexthop in a group and group is updated
586	run_cmd "$IP nexthop del id 63"
587	check_nexthop "id 102" "id 102 group 62/64/65"
588	log_test $? 0 "Nexthop group updated when entry is deleted"
589
590	# create group with multiple weighted nexthops
591	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
592	run_cmd "$IP nexthop add id 103 group 62/63,2/64,3/65,4"
593	log_test $? 0 "Nexthop group with weighted nexthops"
594	check_nexthop "id 103" "id 103 group 62/63,2/64,3/65,4"
595
596	# Delete nexthop in a weighted group and group is updated
597	run_cmd "$IP nexthop del id 63"
598	check_nexthop "id 103" "id 103 group 62/64,3/65,4"
599	log_test $? 0 "Weighted nexthop group updated when entry is deleted"
600
601	# admin down - nexthop is removed from group
602	run_cmd "$IP li set dev veth1 down"
603	check_nexthop "dev veth1" ""
604	log_test $? 0 "Nexthops in groups removed on admin down"
605
606	# expect groups to have been deleted as well
607	check_nexthop "" ""
608
609	run_cmd "$IP li set dev veth1 up"
610
611	$IP nexthop flush >/dev/null 2>&1
612
613	# group with nexthops using different devices
614	set -e
615	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
616	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
617	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
618	run_cmd "$IP nexthop add id 65 via 2001:db8:91::5 dev veth1"
619
620	run_cmd "$IP nexthop add id 72 via 2001:db8:92::2 dev veth3"
621	run_cmd "$IP nexthop add id 73 via 2001:db8:92::3 dev veth3"
622	run_cmd "$IP nexthop add id 74 via 2001:db8:92::4 dev veth3"
623	run_cmd "$IP nexthop add id 75 via 2001:db8:92::5 dev veth3"
624	set +e
625
626	# multiple groups with same nexthop
627	run_cmd "$IP nexthop add id 104 group 62"
628	run_cmd "$IP nexthop add id 105 group 62"
629	check_nexthop "group" "id 104 group 62 id 105 group 62"
630	log_test $? 0 "Multiple groups with same nexthop"
631
632	run_cmd "$IP nexthop flush groups"
633	[ $? -ne 0 ] && return 1
634
635	# on admin down of veth1, it should be removed from the group
636	run_cmd "$IP nexthop add id 105 group 62/63/72/73/64"
637	run_cmd "$IP li set veth1 down"
638	check_nexthop "id 105" "id 105 group 72/73"
639	log_test $? 0 "Nexthops in group removed on admin down - mixed group"
640
641	run_cmd "$IP nexthop add id 106 group 105/74"
642	log_test $? 2 "Nexthop group can not have a group as an entry"
643
644	# a group can have a blackhole entry only if it is the only
645	# nexthop in the group. Needed for atomic replace with an
646	# actual nexthop group
647	run_cmd "$IP -6 nexthop add id 31 blackhole"
648	run_cmd "$IP nexthop add id 107 group 31"
649	log_test $? 0 "Nexthop group with a blackhole entry"
650
651	run_cmd "$IP nexthop add id 108 group 31/24"
652	log_test $? 2 "Nexthop group can not have a blackhole and another nexthop"
653}
654
655ipv6_fcnal_runtime()
656{
657	local rc
658
659	echo
660	echo "IPv6 functional runtime"
661	echo "-----------------------"
662
663	#
664	# IPv6 - the basics
665	#
666	run_cmd "$IP nexthop add id 81 via 2001:db8:91::2 dev veth1"
667	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
668	log_test $? 0 "Route add"
669
670	run_cmd "$IP ro delete 2001:db8:101::1/128 nhid 81"
671	log_test $? 0 "Route delete"
672
673	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
674	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
675	log_test $? 0 "Ping with nexthop"
676
677	run_cmd "$IP nexthop add id 82 via 2001:db8:92::2 dev veth3"
678	run_cmd "$IP nexthop add id 122 group 81/82"
679	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
680	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
681	log_test $? 0 "Ping - multipath"
682
683	#
684	# IPv6 with blackhole nexthops
685	#
686	run_cmd "$IP -6 nexthop add id 83 blackhole"
687	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 83"
688	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
689	log_test $? 2 "Ping - blackhole"
690
691	run_cmd "$IP nexthop replace id 83 via 2001:db8:91::2 dev veth1"
692	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
693	log_test $? 0 "Ping - blackhole replaced with gateway"
694
695	run_cmd "$IP -6 nexthop replace id 83 blackhole"
696	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
697	log_test $? 2 "Ping - gateway replaced by blackhole"
698
699	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
700	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
701	if [ $? -eq 0 ]; then
702		run_cmd "$IP nexthop replace id 122 group 83"
703		run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
704		log_test $? 2 "Ping - group with blackhole"
705
706		run_cmd "$IP nexthop replace id 122 group 81/82"
707		run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
708		log_test $? 0 "Ping - group blackhole replaced with gateways"
709	else
710		log_test 2 0 "Ping - multipath failed"
711	fi
712
713	#
714	# device only and gw + dev only mix
715	#
716	run_cmd "$IP -6 nexthop add id 85 dev veth1"
717	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 85"
718	log_test $? 0 "IPv6 route with device only nexthop"
719	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 85 dev veth1 metric 1024"
720
721	run_cmd "$IP nexthop add id 123 group 81/85"
722	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 123"
723	log_test $? 0 "IPv6 multipath route with nexthop mix - dev only + gw"
724	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"
725
726	#
727	# IPv6 route with v4 nexthop - not allowed
728	#
729	run_cmd "$IP ro delete 2001:db8:101::1/128"
730	run_cmd "$IP nexthop add id 84 via 172.16.1.1 dev veth1"
731	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 84"
732	log_test $? 2 "IPv6 route can not have a v4 gateway"
733
734	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 81"
735	run_cmd "$IP nexthop replace id 81 via 172.16.1.1 dev veth1"
736	log_test $? 2 "Nexthop replace - v6 route, v4 nexthop"
737
738	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
739	run_cmd "$IP nexthop replace id 81 via 172.16.1.1 dev veth1"
740	log_test $? 2 "Nexthop replace of group entry - v6 route, v4 nexthop"
741
742	$IP nexthop flush >/dev/null 2>&1
743
744	#
745	# weird IPv6 cases
746	#
747	run_cmd "$IP nexthop add id 86 via 2001:db8:91::2 dev veth1"
748	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
749
750	# rpfilter and default route
751	$IP nexthop flush >/dev/null 2>&1
752	run_cmd "ip netns exec me ip6tables -t mangle -I PREROUTING 1 -m rpfilter --invert -j DROP"
753	run_cmd "$IP nexthop add id 91 via 2001:db8:91::2 dev veth1"
754	run_cmd "$IP nexthop add id 92 via 2001:db8:92::2 dev veth3"
755	run_cmd "$IP nexthop add id 93 group 91/92"
756	run_cmd "$IP -6 ro add default nhid 91"
757	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
758	log_test $? 0 "Nexthop with default route and rpfilter"
759	run_cmd "$IP -6 ro replace default nhid 93"
760	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
761	log_test $? 0 "Nexthop with multipath default route and rpfilter"
762
763	# TO-DO:
764	# existing route with old nexthop; append route with new nexthop
765	# existing route with old nexthop; replace route with new
766	# existing route with new nexthop; replace route with old
767	# route with src address and using nexthop - not allowed
768}
769
770ipv6_large_grp()
771{
772	local ecmp=32
773
774	echo
775	echo "IPv6 large groups (x$ecmp)"
776	echo "---------------------"
777
778	check_large_grp 6 $ecmp
779
780	$IP nexthop flush >/dev/null 2>&1
781}
782
783ipv6_del_add_loop1()
784{
785	while :; do
786		$IP nexthop del id 100
787		$IP nexthop add id 100 via 2001:db8:91::2 dev veth1
788	done >/dev/null 2>&1
789}
790
791ipv6_grp_replace_loop()
792{
793	while :; do
794		$IP nexthop replace id 102 group 100/101
795	done >/dev/null 2>&1
796}
797
798ipv6_torture()
799{
800	local pid1
801	local pid2
802	local pid3
803	local pid4
804	local pid5
805
806	echo
807	echo "IPv6 runtime torture"
808	echo "--------------------"
809	if [ ! -x "$(command -v mausezahn)" ]; then
810		echo "SKIP: Could not run test; need mausezahn tool"
811		return
812	fi
813
814	run_cmd "$IP nexthop add id 100 via 2001:db8:91::2 dev veth1"
815	run_cmd "$IP nexthop add id 101 via 2001:db8:92::2 dev veth3"
816	run_cmd "$IP nexthop add id 102 group 100/101"
817	run_cmd "$IP route add 2001:db8:101::1 nhid 102"
818	run_cmd "$IP route add 2001:db8:101::2 nhid 102"
819
820	ipv6_del_add_loop1 &
821	pid1=$!
822	ipv6_grp_replace_loop &
823	pid2=$!
824	ip netns exec me ping -f 2001:db8:101::1 >/dev/null 2>&1 &
825	pid3=$!
826	ip netns exec me ping -f 2001:db8:101::2 >/dev/null 2>&1 &
827	pid4=$!
828	ip netns exec me mausezahn veth1 -B 2001:db8:101::2 -A 2001:db8:91::1 -c 0 -t tcp "dp=1-1023, flags=syn" >/dev/null 2>&1 &
829	pid5=$!
830
831	sleep 300
832	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
833
834	# if we did not crash, success
835	log_test 0 0 "IPv6 torture test"
836}
837
838
839ipv4_fcnal()
840{
841	local rc
842
843	echo
844	echo "IPv4 functional"
845	echo "----------------------"
846
847	#
848	# basic IPv4 ops - add, get, delete
849	#
850	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
851	rc=$?
852	log_test $rc 0 "Create nexthop with id, gw, dev"
853	if [ $rc -ne 0 ]; then
854		echo "Basic IPv4 create fails; can not continue"
855		return 1
856	fi
857
858	run_cmd "$IP nexthop get id 12"
859	log_test $? 0 "Get nexthop by id"
860	check_nexthop "id 12" "id 12 via 172.16.1.2 dev veth1 scope link"
861
862	run_cmd "$IP nexthop del id 12"
863	log_test $? 0 "Delete nexthop by id"
864	check_nexthop "id 52" ""
865
866	#
867	# gw, device spec
868	#
869	# gw validation, no device - fails since dev is required
870	run_cmd "$IP nexthop add id 12 via 172.16.2.3"
871	log_test $? 2 "Create nexthop - gw only"
872
873	# gw not reachable through given dev
874	run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1"
875	log_test $? 2 "Create nexthop - invalid gw+dev combination"
876
877	# onlink flag overrides gw+dev lookup
878	run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1 onlink"
879	log_test $? 0 "Create nexthop - gw+dev and onlink"
880
881	# admin down should delete nexthops
882	set -e
883	run_cmd "$IP nexthop add id 15 via 172.16.1.3 dev veth1"
884	run_cmd "$IP nexthop add id 16 via 172.16.1.4 dev veth1"
885	run_cmd "$IP nexthop add id 17 via 172.16.1.5 dev veth1"
886	run_cmd "$IP li set dev veth1 down"
887	set +e
888	check_nexthop "dev veth1" ""
889	log_test $? 0 "Nexthops removed on admin down"
890}
891
892ipv4_grp_fcnal()
893{
894	local rc
895
896	echo
897	echo "IPv4 groups functional"
898	echo "----------------------"
899
900	# basic functionality: create a nexthop group, default weight
901	run_cmd "$IP nexthop add id 11 via 172.16.1.2 dev veth1"
902	run_cmd "$IP nexthop add id 101 group 11"
903	log_test $? 0 "Create nexthop group with single nexthop"
904
905	# get nexthop group
906	run_cmd "$IP nexthop get id 101"
907	log_test $? 0 "Get nexthop group by id"
908	check_nexthop "id 101" "id 101 group 11"
909
910	# delete nexthop group
911	run_cmd "$IP nexthop del id 101"
912	log_test $? 0 "Delete nexthop group by id"
913	check_nexthop "id 101" ""
914
915	$IP nexthop flush >/dev/null 2>&1
916
917	#
918	# create group with multiple nexthops
919	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
920	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
921	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
922	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
923	run_cmd "$IP nexthop add id 102 group 12/13/14/15"
924	log_test $? 0 "Nexthop group with multiple nexthops"
925	check_nexthop "id 102" "id 102 group 12/13/14/15"
926
927	# Delete nexthop in a group and group is updated
928	run_cmd "$IP nexthop del id 13"
929	check_nexthop "id 102" "id 102 group 12/14/15"
930	log_test $? 0 "Nexthop group updated when entry is deleted"
931
932	# create group with multiple weighted nexthops
933	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
934	run_cmd "$IP nexthop add id 103 group 12/13,2/14,3/15,4"
935	log_test $? 0 "Nexthop group with weighted nexthops"
936	check_nexthop "id 103" "id 103 group 12/13,2/14,3/15,4"
937
938	# Delete nexthop in a weighted group and group is updated
939	run_cmd "$IP nexthop del id 13"
940	check_nexthop "id 103" "id 103 group 12/14,3/15,4"
941	log_test $? 0 "Weighted nexthop group updated when entry is deleted"
942
943	# admin down - nexthop is removed from group
944	run_cmd "$IP li set dev veth1 down"
945	check_nexthop "dev veth1" ""
946	log_test $? 0 "Nexthops in groups removed on admin down"
947
948	# expect groups to have been deleted as well
949	check_nexthop "" ""
950
951	run_cmd "$IP li set dev veth1 up"
952
953	$IP nexthop flush >/dev/null 2>&1
954
955	# group with nexthops using different devices
956	set -e
957	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
958	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
959	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
960	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
961
962	run_cmd "$IP nexthop add id 22 via 172.16.2.2 dev veth3"
963	run_cmd "$IP nexthop add id 23 via 172.16.2.3 dev veth3"
964	run_cmd "$IP nexthop add id 24 via 172.16.2.4 dev veth3"
965	run_cmd "$IP nexthop add id 25 via 172.16.2.5 dev veth3"
966	set +e
967
968	# multiple groups with same nexthop
969	run_cmd "$IP nexthop add id 104 group 12"
970	run_cmd "$IP nexthop add id 105 group 12"
971	check_nexthop "group" "id 104 group 12 id 105 group 12"
972	log_test $? 0 "Multiple groups with same nexthop"
973
974	run_cmd "$IP nexthop flush groups"
975	[ $? -ne 0 ] && return 1
976
977	# on admin down of veth1, it should be removed from the group
978	run_cmd "$IP nexthop add id 105 group 12/13/22/23/14"
979	run_cmd "$IP li set veth1 down"
980	check_nexthop "id 105" "id 105 group 22/23"
981	log_test $? 0 "Nexthops in group removed on admin down - mixed group"
982
983	run_cmd "$IP nexthop add id 106 group 105/24"
984	log_test $? 2 "Nexthop group can not have a group as an entry"
985
986	# a group can have a blackhole entry only if it is the only
987	# nexthop in the group. Needed for atomic replace with an
988	# actual nexthop group
989	run_cmd "$IP nexthop add id 31 blackhole"
990	run_cmd "$IP nexthop add id 107 group 31"
991	log_test $? 0 "Nexthop group with a blackhole entry"
992
993	run_cmd "$IP nexthop add id 108 group 31/24"
994	log_test $? 2 "Nexthop group can not have a blackhole and another nexthop"
995}
996
997ipv4_withv6_fcnal()
998{
999	local lladdr
1000
1001	set -e
1002	lladdr=$(get_linklocal veth2 peer)
1003	run_cmd "$IP nexthop add id 11 via ${lladdr} dev veth1"
1004	set +e
1005	run_cmd "$IP ro add 172.16.101.1/32 nhid 11"
1006	log_test $? 0 "IPv6 nexthop with IPv4 route"
1007	check_route "172.16.101.1" "172.16.101.1 nhid 11 via inet6 ${lladdr} dev veth1"
1008
1009	set -e
1010	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
1011	run_cmd "$IP nexthop add id 101 group 11/12"
1012	set +e
1013	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
1014	log_test $? 0 "IPv6 nexthop with IPv4 route"
1015
1016	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"
1017
1018	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1019	log_test $? 0 "IPv4 route with IPv6 gateway"
1020	check_route "172.16.101.1" "172.16.101.1 via inet6 ${lladdr} dev veth1"
1021
1022	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 2001:db8:50::1 dev veth1"
1023	log_test $? 2 "IPv4 route with invalid IPv6 gateway"
1024}
1025
1026ipv4_fcnal_runtime()
1027{
1028	local lladdr
1029	local rc
1030
1031	echo
1032	echo "IPv4 functional runtime"
1033	echo "-----------------------"
1034
1035	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
1036	run_cmd "$IP ro add 172.16.101.1/32 nhid 21"
1037	log_test $? 0 "Route add"
1038	check_route "172.16.101.1" "172.16.101.1 nhid 21 via 172.16.1.2 dev veth1"
1039
1040	run_cmd "$IP ro delete 172.16.101.1/32 nhid 21"
1041	log_test $? 0 "Route delete"
1042
1043	#
1044	# scope mismatch
1045	#
1046	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
1047	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
1048	log_test $? 2 "Route add - scope conflict with nexthop"
1049
1050	run_cmd "$IP nexthop replace id 22 dev veth3"
1051	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
1052	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
1053	log_test $? 2 "Nexthop replace with invalid scope for existing route"
1054
1055	#
1056	# add route with nexthop and check traffic
1057	#
1058	run_cmd "$IP nexthop replace id 21 via 172.16.1.2 dev veth1"
1059	run_cmd "$IP ro replace 172.16.101.1/32 nhid 21"
1060	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1061	log_test $? 0 "Basic ping"
1062
1063	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
1064	run_cmd "$IP nexthop add id 122 group 21/22"
1065	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
1066	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1067	log_test $? 0 "Ping - multipath"
1068
1069	run_cmd "$IP ro delete 172.16.101.1/32 nhid 122"
1070
1071	#
1072	# multiple default routes
1073	# - tests fib_select_default
1074	run_cmd "$IP nexthop add id 501 via 172.16.1.2 dev veth1"
1075	run_cmd "$IP ro add default nhid 501"
1076	run_cmd "$IP ro add default via 172.16.1.3 dev veth1 metric 20"
1077	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1078	log_test $? 0 "Ping - multiple default routes, nh first"
1079
1080	# flip the order
1081	run_cmd "$IP ro del default nhid 501"
1082	run_cmd "$IP ro del default via 172.16.1.3 dev veth1 metric 20"
1083	run_cmd "$IP ro add default via 172.16.1.2 dev veth1 metric 20"
1084	run_cmd "$IP nexthop replace id 501 via 172.16.1.3 dev veth1"
1085	run_cmd "$IP ro add default nhid 501 metric 20"
1086	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1087	log_test $? 0 "Ping - multiple default routes, nh second"
1088
1089	run_cmd "$IP nexthop delete nhid 501"
1090	run_cmd "$IP ro del default"
1091
1092	#
1093	# IPv4 with blackhole nexthops
1094	#
1095	run_cmd "$IP nexthop add id 23 blackhole"
1096	run_cmd "$IP ro replace 172.16.101.1/32 nhid 23"
1097	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1098	log_test $? 2 "Ping - blackhole"
1099
1100	run_cmd "$IP nexthop replace id 23 via 172.16.1.2 dev veth1"
1101	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1102	log_test $? 0 "Ping - blackhole replaced with gateway"
1103
1104	run_cmd "$IP nexthop replace id 23 blackhole"
1105	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1106	log_test $? 2 "Ping - gateway replaced by blackhole"
1107
1108	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
1109	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1110	if [ $? -eq 0 ]; then
1111		run_cmd "$IP nexthop replace id 122 group 23"
1112		run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1113		log_test $? 2 "Ping - group with blackhole"
1114
1115		run_cmd "$IP nexthop replace id 122 group 21/22"
1116		run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1117		log_test $? 0 "Ping - group blackhole replaced with gateways"
1118	else
1119		log_test 2 0 "Ping - multipath failed"
1120	fi
1121
1122	#
1123	# device only and gw + dev only mix
1124	#
1125	run_cmd "$IP nexthop add id 85 dev veth1"
1126	run_cmd "$IP ro replace 172.16.101.1/32 nhid 85"
1127	log_test $? 0 "IPv4 route with device only nexthop"
1128	check_route "172.16.101.1" "172.16.101.1 nhid 85 dev veth1"
1129
1130	run_cmd "$IP nexthop add id 123 group 21/85"
1131	run_cmd "$IP ro replace 172.16.101.1/32 nhid 123"
1132	log_test $? 0 "IPv4 multipath route with nexthop mix - dev only + gw"
1133	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"
1134
1135	#
1136	# IPv4 with IPv6
1137	#
1138	set -e
1139	lladdr=$(get_linklocal veth2 peer)
1140	run_cmd "$IP nexthop add id 24 via ${lladdr} dev veth1"
1141	set +e
1142	run_cmd "$IP ro replace 172.16.101.1/32 nhid 24"
1143	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1144	log_test $? 0 "IPv6 nexthop with IPv4 route"
1145
1146	$IP neigh sh | grep -q "${lladdr} dev veth1"
1147	if [ $? -eq 1 ]; then
1148		echo "    WARNING: Neigh entry missing for ${lladdr}"
1149		$IP neigh sh | grep 'dev veth1'
1150	fi
1151
1152	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
1153	if [ $? -eq 0 ]; then
1154		echo "    WARNING: Neigh entry exists for 172.16.101.1"
1155		$IP neigh sh | grep 'dev veth1'
1156	fi
1157
1158	set -e
1159	run_cmd "$IP nexthop add id 25 via 172.16.1.2 dev veth1"
1160	run_cmd "$IP nexthop add id 101 group 24/25"
1161	set +e
1162	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
1163	log_test $? 0 "IPv4 route with mixed v4-v6 multipath route"
1164
1165	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"
1166
1167	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1168	log_test $? 0 "IPv6 nexthop with IPv4 route"
1169
1170	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1171	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1172	log_test $? 0 "IPv4 route with IPv6 gateway"
1173
1174	$IP neigh sh | grep -q "${lladdr} dev veth1"
1175	if [ $? -eq 1 ]; then
1176		echo "    WARNING: Neigh entry missing for ${lladdr}"
1177		$IP neigh sh | grep 'dev veth1'
1178	fi
1179
1180	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
1181	if [ $? -eq 0 ]; then
1182		echo "    WARNING: Neigh entry exists for 172.16.101.1"
1183		$IP neigh sh | grep 'dev veth1'
1184	fi
1185
1186	run_cmd "$IP ro del 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1187	run_cmd "$IP -4 ro add default via inet6 ${lladdr} dev veth1"
1188	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1189	log_test $? 0 "IPv4 default route with IPv6 gateway"
1190
1191	#
1192	# MPLS as an example of LWT encap
1193	#
1194	run_cmd "$IP nexthop add id 51 encap mpls 101 via 172.16.1.2 dev veth1"
1195	log_test $? 0 "IPv4 route with MPLS encap"
1196	check_nexthop "id 51" "id 51 encap mpls 101 via 172.16.1.2 dev veth1 scope link"
1197	log_test $? 0 "IPv4 route with MPLS encap - check"
1198
1199	run_cmd "$IP nexthop add id 52 encap mpls 102 via inet6 2001:db8:91::2 dev veth1"
1200	log_test $? 0 "IPv4 route with MPLS encap and v6 gateway"
1201	check_nexthop "id 52" "id 52 encap mpls 102 via 2001:db8:91::2 dev veth1 scope link"
1202	log_test $? 0 "IPv4 route with MPLS encap, v6 gw - check"
1203}
1204
1205ipv4_large_grp()
1206{
1207	local ecmp=32
1208
1209	echo
1210	echo "IPv4 large groups (x$ecmp)"
1211	echo "---------------------"
1212
1213	check_large_grp 4 $ecmp
1214
1215	$IP nexthop flush >/dev/null 2>&1
1216}
1217
1218sysctl_nexthop_compat_mode_check()
1219{
1220	local sysctlname="net.ipv4.nexthop_compat_mode"
1221	local lprefix=$1
1222
1223	IPE="ip netns exec me"
1224
1225	$IPE sysctl -q $sysctlname 2>&1 >/dev/null
1226	if [ $? -ne 0 ]; then
1227		echo "SKIP: kernel lacks nexthop compat mode sysctl control"
1228		return $ksft_skip
1229	fi
1230
1231	out=$($IPE sysctl $sysctlname 2>/dev/null)
1232	log_test $? 0 "$lprefix default nexthop compat mode check"
1233	check_output "${out}" "$sysctlname = 1"
1234}
1235
1236sysctl_nexthop_compat_mode_set()
1237{
1238	local sysctlname="net.ipv4.nexthop_compat_mode"
1239	local mode=$1
1240	local lprefix=$2
1241
1242	IPE="ip netns exec me"
1243
1244	out=$($IPE sysctl -w $sysctlname=$mode)
1245	log_test $? 0 "$lprefix set compat mode - $mode"
1246	check_output "${out}" "net.ipv4.nexthop_compat_mode = $mode"
1247}
1248
1249ipv6_compat_mode()
1250{
1251	local rc
1252
1253	echo
1254	echo "IPv6 nexthop api compat mode test"
1255	echo "--------------------------------"
1256
1257	sysctl_nexthop_compat_mode_check "IPv6"
1258	if [ $? -eq $ksft_skip ]; then
1259		return $ksft_skip
1260	fi
1261
1262	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
1263	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
1264	run_cmd "$IP nexthop add id 122 group 62/63"
1265	ipmout=$(start_ip_monitor route)
1266
1267	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 122"
1268	# route add notification should contain expanded nexthops
1269	stop_ip_monitor $ipmout 3
1270	log_test $? 0 "IPv6 compat mode on - route add notification"
1271
1272	# route dump should contain expanded nexthops
1273	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"
1274	log_test $? 0 "IPv6 compat mode on - route dump"
1275
1276	# change in nexthop group should generate route notification
1277	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
1278	ipmout=$(start_ip_monitor route)
1279	run_cmd "$IP nexthop replace id 122 group 62/64"
1280	stop_ip_monitor $ipmout 3
1281
1282	log_test $? 0 "IPv6 compat mode on - nexthop change"
1283
1284	# set compat mode off
1285	sysctl_nexthop_compat_mode_set 0 "IPv6"
1286
1287	run_cmd "$IP -6 ro del 2001:db8:101::1/128 nhid 122"
1288
1289	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
1290	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
1291	run_cmd "$IP nexthop add id 122 group 62/63"
1292	ipmout=$(start_ip_monitor route)
1293
1294	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 122"
1295	# route add notification should not contain expanded nexthops
1296	stop_ip_monitor $ipmout 1
1297	log_test $? 0 "IPv6 compat mode off - route add notification"
1298
1299	# route dump should not contain expanded nexthops
1300	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 122 metric 1024"
1301	log_test $? 0 "IPv6 compat mode off - route dump"
1302
1303	# change in nexthop group should not generate route notification
1304	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
1305	ipmout=$(start_ip_monitor route)
1306	run_cmd "$IP nexthop replace id 122 group 62/64"
1307	stop_ip_monitor $ipmout 0
1308	log_test $? 0 "IPv6 compat mode off - nexthop change"
1309
1310	# nexthop delete should not generate route notification
1311	ipmout=$(start_ip_monitor route)
1312	run_cmd "$IP nexthop del id 122"
1313	stop_ip_monitor $ipmout 0
1314	log_test $? 0 "IPv6 compat mode off - nexthop delete"
1315
1316	# set compat mode back on
1317	sysctl_nexthop_compat_mode_set 1 "IPv6"
1318}
1319
1320ipv4_compat_mode()
1321{
1322	local rc
1323
1324	echo
1325	echo "IPv4 nexthop api compat mode"
1326	echo "----------------------------"
1327
1328	sysctl_nexthop_compat_mode_check "IPv4"
1329	if [ $? -eq $ksft_skip ]; then
1330		return $ksft_skip
1331	fi
1332
1333	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
1334	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
1335	run_cmd "$IP nexthop add id 122 group 21/22"
1336	ipmout=$(start_ip_monitor route)
1337
1338	run_cmd "$IP ro add 172.16.101.1/32 nhid 122"
1339	stop_ip_monitor $ipmout 3
1340
1341	# route add notification should contain expanded nexthops
1342	log_test $? 0 "IPv4 compat mode on - route add notification"
1343
1344	# route dump should contain expanded nexthops
1345	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"
1346	log_test $? 0 "IPv4 compat mode on - route dump"
1347
1348	# change in nexthop group should generate route notification
1349	run_cmd "$IP nexthop add id 23 via 172.16.1.3 dev veth1"
1350	ipmout=$(start_ip_monitor route)
1351	run_cmd "$IP nexthop replace id 122 group 21/23"
1352	stop_ip_monitor $ipmout 3
1353	log_test $? 0 "IPv4 compat mode on - nexthop change"
1354
1355	sysctl_nexthop_compat_mode_set 0 "IPv4"
1356
1357	# cleanup
1358	run_cmd "$IP ro del 172.16.101.1/32 nhid 122"
1359
1360	ipmout=$(start_ip_monitor route)
1361	run_cmd "$IP ro add 172.16.101.1/32 nhid 122"
1362	stop_ip_monitor $ipmout 1
1363	# route add notification should not contain expanded nexthops
1364	log_test $? 0 "IPv4 compat mode off - route add notification"
1365
1366	# route dump should not contain expanded nexthops
1367	check_route "172.16.101.1" "172.16.101.1 nhid 122"
1368	log_test $? 0 "IPv4 compat mode off - route dump"
1369
1370	# change in nexthop group should not generate route notification
1371	ipmout=$(start_ip_monitor route)
1372	run_cmd "$IP nexthop replace id 122 group 21/22"
1373	stop_ip_monitor $ipmout 0
1374	log_test $? 0 "IPv4 compat mode off - nexthop change"
1375
1376	# nexthop delete should not generate route notification
1377	ipmout=$(start_ip_monitor route)
1378	run_cmd "$IP nexthop del id 122"
1379	stop_ip_monitor $ipmout 0
1380	log_test $? 0 "IPv4 compat mode off - nexthop delete"
1381
1382	sysctl_nexthop_compat_mode_set 1 "IPv4"
1383}
1384
1385ipv4_del_add_loop1()
1386{
1387	while :; do
1388		$IP nexthop del id 100
1389		$IP nexthop add id 100 via 172.16.1.2 dev veth1
1390	done >/dev/null 2>&1
1391}
1392
1393ipv4_grp_replace_loop()
1394{
1395	while :; do
1396		$IP nexthop replace id 102 group 100/101
1397	done >/dev/null 2>&1
1398}
1399
1400ipv4_torture()
1401{
1402	local pid1
1403	local pid2
1404	local pid3
1405	local pid4
1406	local pid5
1407
1408	echo
1409	echo "IPv4 runtime torture"
1410	echo "--------------------"
1411	if [ ! -x "$(command -v mausezahn)" ]; then
1412		echo "SKIP: Could not run test; need mausezahn tool"
1413		return
1414	fi
1415
1416	run_cmd "$IP nexthop add id 100 via 172.16.1.2 dev veth1"
1417	run_cmd "$IP nexthop add id 101 via 172.16.2.2 dev veth3"
1418	run_cmd "$IP nexthop add id 102 group 100/101"
1419	run_cmd "$IP route add 172.16.101.1 nhid 102"
1420	run_cmd "$IP route add 172.16.101.2 nhid 102"
1421
1422	ipv4_del_add_loop1 &
1423	pid1=$!
1424	ipv4_grp_replace_loop &
1425	pid2=$!
1426	ip netns exec me ping -f 172.16.101.1 >/dev/null 2>&1 &
1427	pid3=$!
1428	ip netns exec me ping -f 172.16.101.2 >/dev/null 2>&1 &
1429	pid4=$!
1430	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 &
1431	pid5=$!
1432
1433	sleep 300
1434	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
1435
1436	# if we did not crash, success
1437	log_test 0 0 "IPv4 torture test"
1438}
1439
1440basic()
1441{
1442	echo
1443	echo "Basic functional tests"
1444	echo "----------------------"
1445	run_cmd "$IP nexthop ls"
1446	log_test $? 0 "List with nothing defined"
1447
1448	run_cmd "$IP nexthop get id 1"
1449	log_test $? 2 "Nexthop get on non-existent id"
1450
1451	# attempt to create nh without a device or gw - fails
1452	run_cmd "$IP nexthop add id 1"
1453	log_test $? 2 "Nexthop with no device or gateway"
1454
1455	# attempt to create nh with down device - fails
1456	$IP li set veth1 down
1457	run_cmd "$IP nexthop add id 1 dev veth1"
1458	log_test $? 2 "Nexthop with down device"
1459
1460	# create nh with linkdown device - fails
1461	$IP li set veth1 up
1462	ip -netns peer li set veth2 down
1463	run_cmd "$IP nexthop add id 1 dev veth1"
1464	log_test $? 2 "Nexthop with device that is linkdown"
1465	ip -netns peer li set veth2 up
1466
1467	# device only
1468	run_cmd "$IP nexthop add id 1 dev veth1"
1469	log_test $? 0 "Nexthop with device only"
1470
1471	# create nh with duplicate id
1472	run_cmd "$IP nexthop add id 1 dev veth3"
1473	log_test $? 2 "Nexthop with duplicate id"
1474
1475	# blackhole nexthop
1476	run_cmd "$IP nexthop add id 2 blackhole"
1477	log_test $? 0 "Blackhole nexthop"
1478
1479	# blackhole nexthop can not have other specs
1480	run_cmd "$IP nexthop replace id 2 blackhole dev veth1"
1481	log_test $? 2 "Blackhole nexthop with other attributes"
1482
1483	#
1484	# groups
1485	#
1486
1487	run_cmd "$IP nexthop add id 101 group 1"
1488	log_test $? 0 "Create group"
1489
1490	run_cmd "$IP nexthop add id 102 group 2"
1491	log_test $? 0 "Create group with blackhole nexthop"
1492
1493	# multipath group can not have a blackhole as 1 path
1494	run_cmd "$IP nexthop add id 103 group 1/2"
1495	log_test $? 2 "Create multipath group where 1 path is a blackhole"
1496
1497	# multipath group can not have a member replaced by a blackhole
1498	run_cmd "$IP nexthop replace id 2 dev veth3"
1499	run_cmd "$IP nexthop replace id 102 group 1/2"
1500	run_cmd "$IP nexthop replace id 2 blackhole"
1501	log_test $? 2 "Multipath group can not have a member replaced by blackhole"
1502
1503	# attempt to create group with non-existent nexthop
1504	run_cmd "$IP nexthop add id 103 group 12"
1505	log_test $? 2 "Create group with non-existent nexthop"
1506
1507	# attempt to create group with same nexthop
1508	run_cmd "$IP nexthop add id 103 group 1/1"
1509	log_test $? 2 "Create group with same nexthop multiple times"
1510
1511	# replace nexthop with a group - fails
1512	run_cmd "$IP nexthop replace id 2 group 1"
1513	log_test $? 2 "Replace nexthop with nexthop group"
1514
1515	# replace nexthop group with a nexthop - fails
1516	run_cmd "$IP nexthop replace id 101 dev veth1"
1517	log_test $? 2 "Replace nexthop group with nexthop"
1518
1519	# nexthop group with other attributes fail
1520	run_cmd "$IP nexthop add id 104 group 1 dev veth1"
1521	log_test $? 2 "Nexthop group and device"
1522
1523	# Tests to ensure that flushing works as expected.
1524	run_cmd "$IP nexthop add id 105 blackhole proto 99"
1525	run_cmd "$IP nexthop add id 106 blackhole proto 100"
1526	run_cmd "$IP nexthop add id 107 blackhole proto 99"
1527	run_cmd "$IP nexthop flush proto 99"
1528	check_nexthop "id 105" ""
1529	check_nexthop "id 106" "id 106 blackhole proto 100"
1530	check_nexthop "id 107" ""
1531	run_cmd "$IP nexthop flush proto 100"
1532	check_nexthop "id 106" ""
1533
1534	run_cmd "$IP nexthop flush proto 100"
1535	log_test $? 0 "Test proto flush"
1536
1537	run_cmd "$IP nexthop add id 104 group 1 blackhole"
1538	log_test $? 2 "Nexthop group and blackhole"
1539
1540	$IP nexthop flush >/dev/null 2>&1
1541}
1542
1543################################################################################
1544# usage
1545
1546usage()
1547{
1548	cat <<EOF
1549usage: ${0##*/} OPTS
1550
1551        -t <test>   Test(s) to run (default: all)
1552                    (options: $ALL_TESTS)
1553        -4          IPv4 tests only
1554        -6          IPv6 tests only
1555        -p          Pause on fail
1556        -P          Pause after each test before cleanup
1557        -v          verbose mode (show commands and output)
1558
1559    Runtime test
1560	-n num	    Number of nexthops to target
1561	-N    	    Use new style to install routes in DUT
1562
1563done
1564EOF
1565}
1566
1567################################################################################
1568# main
1569
1570while getopts :t:pP46hv o
1571do
1572	case $o in
1573		t) TESTS=$OPTARG;;
1574		4) TESTS=${IPV4_TESTS};;
1575		6) TESTS=${IPV6_TESTS};;
1576		p) PAUSE_ON_FAIL=yes;;
1577		P) PAUSE=yes;;
1578		v) VERBOSE=$(($VERBOSE + 1));;
1579		h) usage; exit 0;;
1580		*) usage; exit 1;;
1581	esac
1582done
1583
1584# make sure we don't pause twice
1585[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
1586
1587if [ "$(id -u)" -ne 0 ];then
1588	echo "SKIP: Need root privileges"
1589	exit $ksft_skip;
1590fi
1591
1592if [ ! -x "$(command -v ip)" ]; then
1593	echo "SKIP: Could not run test without ip tool"
1594	exit $ksft_skip
1595fi
1596
1597ip help 2>&1 | grep -q nexthop
1598if [ $? -ne 0 ]; then
1599	echo "SKIP: iproute2 too old, missing nexthop command"
1600	exit $ksft_skip
1601fi
1602
1603out=$(ip nexthop ls 2>&1 | grep -q "Operation not supported")
1604if [ $? -eq 0 ]; then
1605	echo "SKIP: kernel lacks nexthop support"
1606	exit $ksft_skip
1607fi
1608
1609for t in $TESTS
1610do
1611	case $t in
1612	none) IP="ip -netns peer"; setup; exit 0;;
1613	*) setup; $t; cleanup;;
1614	esac
1615done
1616
1617if [ "$TESTS" != "none" ]; then
1618	printf "\nTests passed: %3d\n" ${nsuccess}
1619	printf "Tests failed: %3d\n"   ${nfail}
1620fi
1621
1622exit $ret
1623