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