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	# TO-DO:
751	# existing route with old nexthop; append route with new nexthop
752	# existing route with old nexthop; replace route with new
753	# existing route with new nexthop; replace route with old
754	# route with src address and using nexthop - not allowed
755}
756
757ipv6_large_grp()
758{
759	local ecmp=32
760
761	echo
762	echo "IPv6 large groups (x$ecmp)"
763	echo "---------------------"
764
765	check_large_grp 6 $ecmp
766
767	$IP nexthop flush >/dev/null 2>&1
768}
769
770ipv6_del_add_loop1()
771{
772	while :; do
773		$IP nexthop del id 100
774		$IP nexthop add id 100 via 2001:db8:91::2 dev veth1
775	done >/dev/null 2>&1
776}
777
778ipv6_grp_replace_loop()
779{
780	while :; do
781		$IP nexthop replace id 102 group 100/101
782	done >/dev/null 2>&1
783}
784
785ipv6_torture()
786{
787	local pid1
788	local pid2
789	local pid3
790	local pid4
791	local pid5
792
793	echo
794	echo "IPv6 runtime torture"
795	echo "--------------------"
796	if [ ! -x "$(command -v mausezahn)" ]; then
797		echo "SKIP: Could not run test; need mausezahn tool"
798		return
799	fi
800
801	run_cmd "$IP nexthop add id 100 via 2001:db8:91::2 dev veth1"
802	run_cmd "$IP nexthop add id 101 via 2001:db8:92::2 dev veth3"
803	run_cmd "$IP nexthop add id 102 group 100/101"
804	run_cmd "$IP route add 2001:db8:101::1 nhid 102"
805	run_cmd "$IP route add 2001:db8:101::2 nhid 102"
806
807	ipv6_del_add_loop1 &
808	pid1=$!
809	ipv6_grp_replace_loop &
810	pid2=$!
811	ip netns exec me ping -f 2001:db8:101::1 >/dev/null 2>&1 &
812	pid3=$!
813	ip netns exec me ping -f 2001:db8:101::2 >/dev/null 2>&1 &
814	pid4=$!
815	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 &
816	pid5=$!
817
818	sleep 300
819	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
820
821	# if we did not crash, success
822	log_test 0 0 "IPv6 torture test"
823}
824
825
826ipv4_fcnal()
827{
828	local rc
829
830	echo
831	echo "IPv4 functional"
832	echo "----------------------"
833
834	#
835	# basic IPv4 ops - add, get, delete
836	#
837	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
838	rc=$?
839	log_test $rc 0 "Create nexthop with id, gw, dev"
840	if [ $rc -ne 0 ]; then
841		echo "Basic IPv4 create fails; can not continue"
842		return 1
843	fi
844
845	run_cmd "$IP nexthop get id 12"
846	log_test $? 0 "Get nexthop by id"
847	check_nexthop "id 12" "id 12 via 172.16.1.2 dev veth1 scope link"
848
849	run_cmd "$IP nexthop del id 12"
850	log_test $? 0 "Delete nexthop by id"
851	check_nexthop "id 52" ""
852
853	#
854	# gw, device spec
855	#
856	# gw validation, no device - fails since dev is required
857	run_cmd "$IP nexthop add id 12 via 172.16.2.3"
858	log_test $? 2 "Create nexthop - gw only"
859
860	# gw not reachable through given dev
861	run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1"
862	log_test $? 2 "Create nexthop - invalid gw+dev combination"
863
864	# onlink flag overrides gw+dev lookup
865	run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1 onlink"
866	log_test $? 0 "Create nexthop - gw+dev and onlink"
867
868	# admin down should delete nexthops
869	set -e
870	run_cmd "$IP nexthop add id 15 via 172.16.1.3 dev veth1"
871	run_cmd "$IP nexthop add id 16 via 172.16.1.4 dev veth1"
872	run_cmd "$IP nexthop add id 17 via 172.16.1.5 dev veth1"
873	run_cmd "$IP li set dev veth1 down"
874	set +e
875	check_nexthop "dev veth1" ""
876	log_test $? 0 "Nexthops removed on admin down"
877}
878
879ipv4_grp_fcnal()
880{
881	local rc
882
883	echo
884	echo "IPv4 groups functional"
885	echo "----------------------"
886
887	# basic functionality: create a nexthop group, default weight
888	run_cmd "$IP nexthop add id 11 via 172.16.1.2 dev veth1"
889	run_cmd "$IP nexthop add id 101 group 11"
890	log_test $? 0 "Create nexthop group with single nexthop"
891
892	# get nexthop group
893	run_cmd "$IP nexthop get id 101"
894	log_test $? 0 "Get nexthop group by id"
895	check_nexthop "id 101" "id 101 group 11"
896
897	# delete nexthop group
898	run_cmd "$IP nexthop del id 101"
899	log_test $? 0 "Delete nexthop group by id"
900	check_nexthop "id 101" ""
901
902	$IP nexthop flush >/dev/null 2>&1
903
904	#
905	# create group with multiple nexthops
906	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
907	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
908	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
909	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
910	run_cmd "$IP nexthop add id 102 group 12/13/14/15"
911	log_test $? 0 "Nexthop group with multiple nexthops"
912	check_nexthop "id 102" "id 102 group 12/13/14/15"
913
914	# Delete nexthop in a group and group is updated
915	run_cmd "$IP nexthop del id 13"
916	check_nexthop "id 102" "id 102 group 12/14/15"
917	log_test $? 0 "Nexthop group updated when entry is deleted"
918
919	# create group with multiple weighted nexthops
920	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
921	run_cmd "$IP nexthop add id 103 group 12/13,2/14,3/15,4"
922	log_test $? 0 "Nexthop group with weighted nexthops"
923	check_nexthop "id 103" "id 103 group 12/13,2/14,3/15,4"
924
925	# Delete nexthop in a weighted group and group is updated
926	run_cmd "$IP nexthop del id 13"
927	check_nexthop "id 103" "id 103 group 12/14,3/15,4"
928	log_test $? 0 "Weighted nexthop group updated when entry is deleted"
929
930	# admin down - nexthop is removed from group
931	run_cmd "$IP li set dev veth1 down"
932	check_nexthop "dev veth1" ""
933	log_test $? 0 "Nexthops in groups removed on admin down"
934
935	# expect groups to have been deleted as well
936	check_nexthop "" ""
937
938	run_cmd "$IP li set dev veth1 up"
939
940	$IP nexthop flush >/dev/null 2>&1
941
942	# group with nexthops using different devices
943	set -e
944	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
945	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
946	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
947	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
948
949	run_cmd "$IP nexthop add id 22 via 172.16.2.2 dev veth3"
950	run_cmd "$IP nexthop add id 23 via 172.16.2.3 dev veth3"
951	run_cmd "$IP nexthop add id 24 via 172.16.2.4 dev veth3"
952	run_cmd "$IP nexthop add id 25 via 172.16.2.5 dev veth3"
953	set +e
954
955	# multiple groups with same nexthop
956	run_cmd "$IP nexthop add id 104 group 12"
957	run_cmd "$IP nexthop add id 105 group 12"
958	check_nexthop "group" "id 104 group 12 id 105 group 12"
959	log_test $? 0 "Multiple groups with same nexthop"
960
961	run_cmd "$IP nexthop flush groups"
962	[ $? -ne 0 ] && return 1
963
964	# on admin down of veth1, it should be removed from the group
965	run_cmd "$IP nexthop add id 105 group 12/13/22/23/14"
966	run_cmd "$IP li set veth1 down"
967	check_nexthop "id 105" "id 105 group 22/23"
968	log_test $? 0 "Nexthops in group removed on admin down - mixed group"
969
970	run_cmd "$IP nexthop add id 106 group 105/24"
971	log_test $? 2 "Nexthop group can not have a group as an entry"
972
973	# a group can have a blackhole entry only if it is the only
974	# nexthop in the group. Needed for atomic replace with an
975	# actual nexthop group
976	run_cmd "$IP nexthop add id 31 blackhole"
977	run_cmd "$IP nexthop add id 107 group 31"
978	log_test $? 0 "Nexthop group with a blackhole entry"
979
980	run_cmd "$IP nexthop add id 108 group 31/24"
981	log_test $? 2 "Nexthop group can not have a blackhole and another nexthop"
982}
983
984ipv4_withv6_fcnal()
985{
986	local lladdr
987
988	set -e
989	lladdr=$(get_linklocal veth2 peer)
990	run_cmd "$IP nexthop add id 11 via ${lladdr} dev veth1"
991	set +e
992	run_cmd "$IP ro add 172.16.101.1/32 nhid 11"
993	log_test $? 0 "IPv6 nexthop with IPv4 route"
994	check_route "172.16.101.1" "172.16.101.1 nhid 11 via inet6 ${lladdr} dev veth1"
995
996	set -e
997	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
998	run_cmd "$IP nexthop add id 101 group 11/12"
999	set +e
1000	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
1001	log_test $? 0 "IPv6 nexthop with IPv4 route"
1002
1003	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"
1004
1005	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1006	log_test $? 0 "IPv4 route with IPv6 gateway"
1007	check_route "172.16.101.1" "172.16.101.1 via inet6 ${lladdr} dev veth1"
1008
1009	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 2001:db8:50::1 dev veth1"
1010	log_test $? 2 "IPv4 route with invalid IPv6 gateway"
1011}
1012
1013ipv4_fcnal_runtime()
1014{
1015	local lladdr
1016	local rc
1017
1018	echo
1019	echo "IPv4 functional runtime"
1020	echo "-----------------------"
1021
1022	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
1023	run_cmd "$IP ro add 172.16.101.1/32 nhid 21"
1024	log_test $? 0 "Route add"
1025	check_route "172.16.101.1" "172.16.101.1 nhid 21 via 172.16.1.2 dev veth1"
1026
1027	run_cmd "$IP ro delete 172.16.101.1/32 nhid 21"
1028	log_test $? 0 "Route delete"
1029
1030	#
1031	# scope mismatch
1032	#
1033	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
1034	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
1035	log_test $? 2 "Route add - scope conflict with nexthop"
1036
1037	run_cmd "$IP nexthop replace id 22 dev veth3"
1038	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
1039	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
1040	log_test $? 2 "Nexthop replace with invalid scope for existing route"
1041
1042	#
1043	# add route with nexthop and check traffic
1044	#
1045	run_cmd "$IP nexthop replace id 21 via 172.16.1.2 dev veth1"
1046	run_cmd "$IP ro replace 172.16.101.1/32 nhid 21"
1047	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1048	log_test $? 0 "Basic ping"
1049
1050	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
1051	run_cmd "$IP nexthop add id 122 group 21/22"
1052	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
1053	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1054	log_test $? 0 "Ping - multipath"
1055
1056	run_cmd "$IP ro delete 172.16.101.1/32 nhid 122"
1057
1058	#
1059	# multiple default routes
1060	# - tests fib_select_default
1061	run_cmd "$IP nexthop add id 501 via 172.16.1.2 dev veth1"
1062	run_cmd "$IP ro add default nhid 501"
1063	run_cmd "$IP ro add default via 172.16.1.3 dev veth1 metric 20"
1064	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1065	log_test $? 0 "Ping - multiple default routes, nh first"
1066
1067	# flip the order
1068	run_cmd "$IP ro del default nhid 501"
1069	run_cmd "$IP ro del default via 172.16.1.3 dev veth1 metric 20"
1070	run_cmd "$IP ro add default via 172.16.1.2 dev veth1 metric 20"
1071	run_cmd "$IP nexthop replace id 501 via 172.16.1.3 dev veth1"
1072	run_cmd "$IP ro add default nhid 501 metric 20"
1073	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1074	log_test $? 0 "Ping - multiple default routes, nh second"
1075
1076	run_cmd "$IP nexthop delete nhid 501"
1077	run_cmd "$IP ro del default"
1078
1079	#
1080	# IPv4 with blackhole nexthops
1081	#
1082	run_cmd "$IP nexthop add id 23 blackhole"
1083	run_cmd "$IP ro replace 172.16.101.1/32 nhid 23"
1084	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1085	log_test $? 2 "Ping - blackhole"
1086
1087	run_cmd "$IP nexthop replace id 23 via 172.16.1.2 dev veth1"
1088	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1089	log_test $? 0 "Ping - blackhole replaced with gateway"
1090
1091	run_cmd "$IP nexthop replace id 23 blackhole"
1092	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1093	log_test $? 2 "Ping - gateway replaced by blackhole"
1094
1095	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
1096	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1097	if [ $? -eq 0 ]; then
1098		run_cmd "$IP nexthop replace id 122 group 23"
1099		run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1100		log_test $? 2 "Ping - group with blackhole"
1101
1102		run_cmd "$IP nexthop replace id 122 group 21/22"
1103		run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1104		log_test $? 0 "Ping - group blackhole replaced with gateways"
1105	else
1106		log_test 2 0 "Ping - multipath failed"
1107	fi
1108
1109	#
1110	# device only and gw + dev only mix
1111	#
1112	run_cmd "$IP nexthop add id 85 dev veth1"
1113	run_cmd "$IP ro replace 172.16.101.1/32 nhid 85"
1114	log_test $? 0 "IPv4 route with device only nexthop"
1115	check_route "172.16.101.1" "172.16.101.1 nhid 85 dev veth1"
1116
1117	run_cmd "$IP nexthop add id 123 group 21/85"
1118	run_cmd "$IP ro replace 172.16.101.1/32 nhid 123"
1119	log_test $? 0 "IPv4 multipath route with nexthop mix - dev only + gw"
1120	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"
1121
1122	#
1123	# IPv4 with IPv6
1124	#
1125	set -e
1126	lladdr=$(get_linklocal veth2 peer)
1127	run_cmd "$IP nexthop add id 24 via ${lladdr} dev veth1"
1128	set +e
1129	run_cmd "$IP ro replace 172.16.101.1/32 nhid 24"
1130	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1131	log_test $? 0 "IPv6 nexthop with IPv4 route"
1132
1133	$IP neigh sh | grep -q "${lladdr} dev veth1"
1134	if [ $? -eq 1 ]; then
1135		echo "    WARNING: Neigh entry missing for ${lladdr}"
1136		$IP neigh sh | grep 'dev veth1'
1137	fi
1138
1139	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
1140	if [ $? -eq 0 ]; then
1141		echo "    WARNING: Neigh entry exists for 172.16.101.1"
1142		$IP neigh sh | grep 'dev veth1'
1143	fi
1144
1145	set -e
1146	run_cmd "$IP nexthop add id 25 via 172.16.1.2 dev veth1"
1147	run_cmd "$IP nexthop add id 101 group 24/25"
1148	set +e
1149	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
1150	log_test $? 0 "IPv4 route with mixed v4-v6 multipath route"
1151
1152	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"
1153
1154	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1155	log_test $? 0 "IPv6 nexthop with IPv4 route"
1156
1157	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1158	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1159	log_test $? 0 "IPv4 route with IPv6 gateway"
1160
1161	$IP neigh sh | grep -q "${lladdr} dev veth1"
1162	if [ $? -eq 1 ]; then
1163		echo "    WARNING: Neigh entry missing for ${lladdr}"
1164		$IP neigh sh | grep 'dev veth1'
1165	fi
1166
1167	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
1168	if [ $? -eq 0 ]; then
1169		echo "    WARNING: Neigh entry exists for 172.16.101.1"
1170		$IP neigh sh | grep 'dev veth1'
1171	fi
1172
1173	run_cmd "$IP ro del 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
1174	run_cmd "$IP -4 ro add default via inet6 ${lladdr} dev veth1"
1175	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
1176	log_test $? 0 "IPv4 default route with IPv6 gateway"
1177
1178	#
1179	# MPLS as an example of LWT encap
1180	#
1181	run_cmd "$IP nexthop add id 51 encap mpls 101 via 172.16.1.2 dev veth1"
1182	log_test $? 0 "IPv4 route with MPLS encap"
1183	check_nexthop "id 51" "id 51 encap mpls 101 via 172.16.1.2 dev veth1 scope link"
1184	log_test $? 0 "IPv4 route with MPLS encap - check"
1185
1186	run_cmd "$IP nexthop add id 52 encap mpls 102 via inet6 2001:db8:91::2 dev veth1"
1187	log_test $? 0 "IPv4 route with MPLS encap and v6 gateway"
1188	check_nexthop "id 52" "id 52 encap mpls 102 via 2001:db8:91::2 dev veth1 scope link"
1189	log_test $? 0 "IPv4 route with MPLS encap, v6 gw - check"
1190}
1191
1192ipv4_large_grp()
1193{
1194	local ecmp=32
1195
1196	echo
1197	echo "IPv4 large groups (x$ecmp)"
1198	echo "---------------------"
1199
1200	check_large_grp 4 $ecmp
1201
1202	$IP nexthop flush >/dev/null 2>&1
1203}
1204
1205sysctl_nexthop_compat_mode_check()
1206{
1207	local sysctlname="net.ipv4.nexthop_compat_mode"
1208	local lprefix=$1
1209
1210	IPE="ip netns exec me"
1211
1212	$IPE sysctl -q $sysctlname 2>&1 >/dev/null
1213	if [ $? -ne 0 ]; then
1214		echo "SKIP: kernel lacks nexthop compat mode sysctl control"
1215		return $ksft_skip
1216	fi
1217
1218	out=$($IPE sysctl $sysctlname 2>/dev/null)
1219	log_test $? 0 "$lprefix default nexthop compat mode check"
1220	check_output "${out}" "$sysctlname = 1"
1221}
1222
1223sysctl_nexthop_compat_mode_set()
1224{
1225	local sysctlname="net.ipv4.nexthop_compat_mode"
1226	local mode=$1
1227	local lprefix=$2
1228
1229	IPE="ip netns exec me"
1230
1231	out=$($IPE sysctl -w $sysctlname=$mode)
1232	log_test $? 0 "$lprefix set compat mode - $mode"
1233	check_output "${out}" "net.ipv4.nexthop_compat_mode = $mode"
1234}
1235
1236ipv6_compat_mode()
1237{
1238	local rc
1239
1240	echo
1241	echo "IPv6 nexthop api compat mode test"
1242	echo "--------------------------------"
1243
1244	sysctl_nexthop_compat_mode_check "IPv6"
1245	if [ $? -eq $ksft_skip ]; then
1246		return $ksft_skip
1247	fi
1248
1249	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
1250	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
1251	run_cmd "$IP nexthop add id 122 group 62/63"
1252	ipmout=$(start_ip_monitor route)
1253
1254	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 122"
1255	# route add notification should contain expanded nexthops
1256	stop_ip_monitor $ipmout 3
1257	log_test $? 0 "IPv6 compat mode on - route add notification"
1258
1259	# route dump should contain expanded nexthops
1260	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"
1261	log_test $? 0 "IPv6 compat mode on - route dump"
1262
1263	# change in nexthop group should generate route notification
1264	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
1265	ipmout=$(start_ip_monitor route)
1266	run_cmd "$IP nexthop replace id 122 group 62/64"
1267	stop_ip_monitor $ipmout 3
1268
1269	log_test $? 0 "IPv6 compat mode on - nexthop change"
1270
1271	# set compat mode off
1272	sysctl_nexthop_compat_mode_set 0 "IPv6"
1273
1274	run_cmd "$IP -6 ro del 2001:db8:101::1/128 nhid 122"
1275
1276	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
1277	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
1278	run_cmd "$IP nexthop add id 122 group 62/63"
1279	ipmout=$(start_ip_monitor route)
1280
1281	run_cmd "$IP -6 ro add 2001:db8:101::1/128 nhid 122"
1282	# route add notification should not contain expanded nexthops
1283	stop_ip_monitor $ipmout 1
1284	log_test $? 0 "IPv6 compat mode off - route add notification"
1285
1286	# route dump should not contain expanded nexthops
1287	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 122 metric 1024"
1288	log_test $? 0 "IPv6 compat mode off - route dump"
1289
1290	# change in nexthop group should not generate route notification
1291	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
1292	ipmout=$(start_ip_monitor route)
1293	run_cmd "$IP nexthop replace id 122 group 62/64"
1294	stop_ip_monitor $ipmout 0
1295	log_test $? 0 "IPv6 compat mode off - nexthop change"
1296
1297	# nexthop delete should not generate route notification
1298	ipmout=$(start_ip_monitor route)
1299	run_cmd "$IP nexthop del id 122"
1300	stop_ip_monitor $ipmout 0
1301	log_test $? 0 "IPv6 compat mode off - nexthop delete"
1302
1303	# set compat mode back on
1304	sysctl_nexthop_compat_mode_set 1 "IPv6"
1305}
1306
1307ipv4_compat_mode()
1308{
1309	local rc
1310
1311	echo
1312	echo "IPv4 nexthop api compat mode"
1313	echo "----------------------------"
1314
1315	sysctl_nexthop_compat_mode_check "IPv4"
1316	if [ $? -eq $ksft_skip ]; then
1317		return $ksft_skip
1318	fi
1319
1320	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
1321	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
1322	run_cmd "$IP nexthop add id 122 group 21/22"
1323	ipmout=$(start_ip_monitor route)
1324
1325	run_cmd "$IP ro add 172.16.101.1/32 nhid 122"
1326	stop_ip_monitor $ipmout 3
1327
1328	# route add notification should contain expanded nexthops
1329	log_test $? 0 "IPv4 compat mode on - route add notification"
1330
1331	# route dump should contain expanded nexthops
1332	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"
1333	log_test $? 0 "IPv4 compat mode on - route dump"
1334
1335	# change in nexthop group should generate route notification
1336	run_cmd "$IP nexthop add id 23 via 172.16.1.3 dev veth1"
1337	ipmout=$(start_ip_monitor route)
1338	run_cmd "$IP nexthop replace id 122 group 21/23"
1339	stop_ip_monitor $ipmout 3
1340	log_test $? 0 "IPv4 compat mode on - nexthop change"
1341
1342	sysctl_nexthop_compat_mode_set 0 "IPv4"
1343
1344	# cleanup
1345	run_cmd "$IP ro del 172.16.101.1/32 nhid 122"
1346
1347	ipmout=$(start_ip_monitor route)
1348	run_cmd "$IP ro add 172.16.101.1/32 nhid 122"
1349	stop_ip_monitor $ipmout 1
1350	# route add notification should not contain expanded nexthops
1351	log_test $? 0 "IPv4 compat mode off - route add notification"
1352
1353	# route dump should not contain expanded nexthops
1354	check_route "172.16.101.1" "172.16.101.1 nhid 122"
1355	log_test $? 0 "IPv4 compat mode off - route dump"
1356
1357	# change in nexthop group should not generate route notification
1358	ipmout=$(start_ip_monitor route)
1359	run_cmd "$IP nexthop replace id 122 group 21/22"
1360	stop_ip_monitor $ipmout 0
1361	log_test $? 0 "IPv4 compat mode off - nexthop change"
1362
1363	# nexthop delete should not generate route notification
1364	ipmout=$(start_ip_monitor route)
1365	run_cmd "$IP nexthop del id 122"
1366	stop_ip_monitor $ipmout 0
1367	log_test $? 0 "IPv4 compat mode off - nexthop delete"
1368
1369	sysctl_nexthop_compat_mode_set 1 "IPv4"
1370}
1371
1372ipv4_del_add_loop1()
1373{
1374	while :; do
1375		$IP nexthop del id 100
1376		$IP nexthop add id 100 via 172.16.1.2 dev veth1
1377	done >/dev/null 2>&1
1378}
1379
1380ipv4_grp_replace_loop()
1381{
1382	while :; do
1383		$IP nexthop replace id 102 group 100/101
1384	done >/dev/null 2>&1
1385}
1386
1387ipv4_torture()
1388{
1389	local pid1
1390	local pid2
1391	local pid3
1392	local pid4
1393	local pid5
1394
1395	echo
1396	echo "IPv4 runtime torture"
1397	echo "--------------------"
1398	if [ ! -x "$(command -v mausezahn)" ]; then
1399		echo "SKIP: Could not run test; need mausezahn tool"
1400		return
1401	fi
1402
1403	run_cmd "$IP nexthop add id 100 via 172.16.1.2 dev veth1"
1404	run_cmd "$IP nexthop add id 101 via 172.16.2.2 dev veth3"
1405	run_cmd "$IP nexthop add id 102 group 100/101"
1406	run_cmd "$IP route add 172.16.101.1 nhid 102"
1407	run_cmd "$IP route add 172.16.101.2 nhid 102"
1408
1409	ipv4_del_add_loop1 &
1410	pid1=$!
1411	ipv4_grp_replace_loop &
1412	pid2=$!
1413	ip netns exec me ping -f 172.16.101.1 >/dev/null 2>&1 &
1414	pid3=$!
1415	ip netns exec me ping -f 172.16.101.2 >/dev/null 2>&1 &
1416	pid4=$!
1417	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 &
1418	pid5=$!
1419
1420	sleep 300
1421	kill -9 $pid1 $pid2 $pid3 $pid4 $pid5
1422
1423	# if we did not crash, success
1424	log_test 0 0 "IPv4 torture test"
1425}
1426
1427basic()
1428{
1429	echo
1430	echo "Basic functional tests"
1431	echo "----------------------"
1432	run_cmd "$IP nexthop ls"
1433	log_test $? 0 "List with nothing defined"
1434
1435	run_cmd "$IP nexthop get id 1"
1436	log_test $? 2 "Nexthop get on non-existent id"
1437
1438	# attempt to create nh without a device or gw - fails
1439	run_cmd "$IP nexthop add id 1"
1440	log_test $? 2 "Nexthop with no device or gateway"
1441
1442	# attempt to create nh with down device - fails
1443	$IP li set veth1 down
1444	run_cmd "$IP nexthop add id 1 dev veth1"
1445	log_test $? 2 "Nexthop with down device"
1446
1447	# create nh with linkdown device - fails
1448	$IP li set veth1 up
1449	ip -netns peer li set veth2 down
1450	run_cmd "$IP nexthop add id 1 dev veth1"
1451	log_test $? 2 "Nexthop with device that is linkdown"
1452	ip -netns peer li set veth2 up
1453
1454	# device only
1455	run_cmd "$IP nexthop add id 1 dev veth1"
1456	log_test $? 0 "Nexthop with device only"
1457
1458	# create nh with duplicate id
1459	run_cmd "$IP nexthop add id 1 dev veth3"
1460	log_test $? 2 "Nexthop with duplicate id"
1461
1462	# blackhole nexthop
1463	run_cmd "$IP nexthop add id 2 blackhole"
1464	log_test $? 0 "Blackhole nexthop"
1465
1466	# blackhole nexthop can not have other specs
1467	run_cmd "$IP nexthop replace id 2 blackhole dev veth1"
1468	log_test $? 2 "Blackhole nexthop with other attributes"
1469
1470	#
1471	# groups
1472	#
1473
1474	run_cmd "$IP nexthop add id 101 group 1"
1475	log_test $? 0 "Create group"
1476
1477	run_cmd "$IP nexthop add id 102 group 2"
1478	log_test $? 0 "Create group with blackhole nexthop"
1479
1480	# multipath group can not have a blackhole as 1 path
1481	run_cmd "$IP nexthop add id 103 group 1/2"
1482	log_test $? 2 "Create multipath group where 1 path is a blackhole"
1483
1484	# multipath group can not have a member replaced by a blackhole
1485	run_cmd "$IP nexthop replace id 2 dev veth3"
1486	run_cmd "$IP nexthop replace id 102 group 1/2"
1487	run_cmd "$IP nexthop replace id 2 blackhole"
1488	log_test $? 2 "Multipath group can not have a member replaced by blackhole"
1489
1490	# attempt to create group with non-existent nexthop
1491	run_cmd "$IP nexthop add id 103 group 12"
1492	log_test $? 2 "Create group with non-existent nexthop"
1493
1494	# attempt to create group with same nexthop
1495	run_cmd "$IP nexthop add id 103 group 1/1"
1496	log_test $? 2 "Create group with same nexthop multiple times"
1497
1498	# replace nexthop with a group - fails
1499	run_cmd "$IP nexthop replace id 2 group 1"
1500	log_test $? 2 "Replace nexthop with nexthop group"
1501
1502	# replace nexthop group with a nexthop - fails
1503	run_cmd "$IP nexthop replace id 101 dev veth1"
1504	log_test $? 2 "Replace nexthop group with nexthop"
1505
1506	# nexthop group with other attributes fail
1507	run_cmd "$IP nexthop add id 104 group 1 dev veth1"
1508	log_test $? 2 "Nexthop group and device"
1509
1510	# Tests to ensure that flushing works as expected.
1511	run_cmd "$IP nexthop add id 105 blackhole proto 99"
1512	run_cmd "$IP nexthop add id 106 blackhole proto 100"
1513	run_cmd "$IP nexthop add id 107 blackhole proto 99"
1514	run_cmd "$IP nexthop flush proto 99"
1515	check_nexthop "id 105" ""
1516	check_nexthop "id 106" "id 106 blackhole proto 100"
1517	check_nexthop "id 107" ""
1518	run_cmd "$IP nexthop flush proto 100"
1519	check_nexthop "id 106" ""
1520
1521	run_cmd "$IP nexthop flush proto 100"
1522	log_test $? 0 "Test proto flush"
1523
1524	run_cmd "$IP nexthop add id 104 group 1 blackhole"
1525	log_test $? 2 "Nexthop group and blackhole"
1526
1527	$IP nexthop flush >/dev/null 2>&1
1528}
1529
1530################################################################################
1531# usage
1532
1533usage()
1534{
1535	cat <<EOF
1536usage: ${0##*/} OPTS
1537
1538        -t <test>   Test(s) to run (default: all)
1539                    (options: $ALL_TESTS)
1540        -4          IPv4 tests only
1541        -6          IPv6 tests only
1542        -p          Pause on fail
1543        -P          Pause after each test before cleanup
1544        -v          verbose mode (show commands and output)
1545
1546    Runtime test
1547	-n num	    Number of nexthops to target
1548	-N    	    Use new style to install routes in DUT
1549
1550done
1551EOF
1552}
1553
1554################################################################################
1555# main
1556
1557while getopts :t:pP46hv o
1558do
1559	case $o in
1560		t) TESTS=$OPTARG;;
1561		4) TESTS=${IPV4_TESTS};;
1562		6) TESTS=${IPV6_TESTS};;
1563		p) PAUSE_ON_FAIL=yes;;
1564		P) PAUSE=yes;;
1565		v) VERBOSE=$(($VERBOSE + 1));;
1566		h) usage; exit 0;;
1567		*) usage; exit 1;;
1568	esac
1569done
1570
1571# make sure we don't pause twice
1572[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
1573
1574if [ "$(id -u)" -ne 0 ];then
1575	echo "SKIP: Need root privileges"
1576	exit $ksft_skip;
1577fi
1578
1579if [ ! -x "$(command -v ip)" ]; then
1580	echo "SKIP: Could not run test without ip tool"
1581	exit $ksft_skip
1582fi
1583
1584ip help 2>&1 | grep -q nexthop
1585if [ $? -ne 0 ]; then
1586	echo "SKIP: iproute2 too old, missing nexthop command"
1587	exit $ksft_skip
1588fi
1589
1590out=$(ip nexthop ls 2>&1 | grep -q "Operation not supported")
1591if [ $? -eq 0 ]; then
1592	echo "SKIP: kernel lacks nexthop support"
1593	exit $ksft_skip
1594fi
1595
1596for t in $TESTS
1597do
1598	case $t in
1599	none) IP="ip -netns peer"; setup; exit 0;;
1600	*) setup; $t; cleanup;;
1601	esac
1602done
1603
1604if [ "$TESTS" != "none" ]; then
1605	printf "\nTests passed: %3d\n" ${nsuccess}
1606	printf "Tests failed: %3d\n"   ${nfail}
1607fi
1608
1609exit $ret
1610