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