1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Test various interface configuration scenarios. Observe that configurations
5# deemed valid by mlxsw succeed, invalid configurations fail and that no traces
6# are produced. To prevent the test from passing in case traces are produced,
7# the user can set the 'kernel.panic_on_warn' and 'kernel.panic_on_oops'
8# sysctls in its environment.
9
10lib_dir=$(dirname $0)/../../../net/forwarding
11
12ALL_TESTS="
13	rif_set_addr_test
14	rif_vrf_set_addr_test
15	rif_inherit_bridge_addr_test
16	rif_non_inherit_bridge_addr_test
17	vlan_interface_deletion_test
18	bridge_deletion_test
19	bridge_vlan_flags_test
20	vlan_1_test
21	lag_bridge_upper_test
22	duplicate_vlans_test
23	vlan_rif_refcount_test
24	subport_rif_refcount_test
25	subport_rif_lag_join_test
26	vlan_dev_deletion_test
27	lag_unlink_slaves_test
28	lag_dev_deletion_test
29	vlan_interface_uppers_test
30	bridge_extern_learn_test
31	neigh_offload_test
32	nexthop_offload_test
33	nexthop_obj_invalid_test
34	nexthop_obj_offload_test
35	nexthop_obj_group_offload_test
36	nexthop_obj_blackhole_offload_test
37	nexthop_obj_route_offload_test
38	devlink_reload_test
39"
40NUM_NETIFS=2
41: ${TIMEOUT:=20000} # ms
42source $lib_dir/lib.sh
43source $lib_dir/devlink_lib.sh
44
45setup_prepare()
46{
47	swp1=${NETIFS[p1]}
48	swp2=${NETIFS[p2]}
49
50	ip link set dev $swp1 up
51	ip link set dev $swp2 up
52}
53
54cleanup()
55{
56	pre_cleanup
57
58	ip link set dev $swp2 down
59	ip link set dev $swp1 down
60}
61
62rif_set_addr_test()
63{
64	local swp1_mac=$(mac_get $swp1)
65	local swp2_mac=$(mac_get $swp2)
66
67	RET=0
68
69	# $swp1 and $swp2 likely got their IPv6 local addresses already, but
70	# here we need to test the transition to RIF.
71	ip addr flush dev $swp1
72	ip addr flush dev $swp2
73	sleep .1
74
75	ip addr add dev $swp1 192.0.2.1/28
76	check_err $?
77
78	ip link set dev $swp1 addr 00:11:22:33:44:55
79	check_err $?
80
81	# IP address enablement should be rejected if the MAC address prefix
82	# doesn't match other RIFs.
83	ip addr add dev $swp2 192.0.2.2/28 &>/dev/null
84	check_fail $? "IP address addition passed for a device with a wrong MAC"
85	ip addr add dev $swp2 192.0.2.2/28 2>&1 >/dev/null \
86	    | grep -q mlxsw_spectrum
87	check_err $? "no extack for IP address addition"
88
89	ip link set dev $swp2 addr 00:11:22:33:44:66
90	check_err $?
91	ip addr add dev $swp2 192.0.2.2/28 &>/dev/null
92	check_err $?
93
94	# Change of MAC address of a RIF should be forbidden if the new MAC
95	# doesn't share the prefix with other MAC addresses.
96	ip link set dev $swp2 addr 00:11:22:33:00:66 &>/dev/null
97	check_fail $? "change of MAC address passed for a wrong MAC"
98	ip link set dev $swp2 addr 00:11:22:33:00:66 2>&1 >/dev/null \
99	    | grep -q mlxsw_spectrum
100	check_err $? "no extack for MAC address change"
101
102	log_test "RIF - bad MAC change"
103
104	ip addr del dev $swp2 192.0.2.2/28
105	ip addr del dev $swp1 192.0.2.1/28
106
107	ip link set dev $swp2 addr $swp2_mac
108	ip link set dev $swp1 addr $swp1_mac
109}
110
111rif_vrf_set_addr_test()
112{
113	# Test that it is possible to set an IP address on a VRF upper despite
114	# its random MAC address.
115	RET=0
116
117	ip link add name vrf-test type vrf table 10
118	ip link set dev $swp1 master vrf-test
119
120	ip -4 address add 192.0.2.1/24 dev vrf-test
121	check_err $? "failed to set IPv4 address on VRF"
122	ip -6 address add 2001:db8:1::1/64 dev vrf-test
123	check_err $? "failed to set IPv6 address on VRF"
124
125	log_test "RIF - setting IP address on VRF"
126
127	ip link del dev vrf-test
128}
129
130rif_inherit_bridge_addr_test()
131{
132	RET=0
133
134	# Create first RIF
135	ip addr add dev $swp1 192.0.2.1/28
136	check_err $?
137
138	# Create a FID RIF
139	ip link add name br1 up type bridge vlan_filtering 0
140	ip link set dev $swp2 master br1
141	ip addr add dev br1 192.0.2.17/28
142	check_err $?
143
144	# Prepare a device with a low MAC address
145	ip link add name d up type dummy
146	ip link set dev d addr 00:11:22:33:44:55
147
148	# Attach the device to br1. That prompts bridge address change, which
149	# should be vetoed, thus preventing the attachment.
150	ip link set dev d master br1 &>/dev/null
151	check_fail $? "Device with low MAC was permitted to attach a bridge with RIF"
152	ip link set dev d master br1 2>&1 >/dev/null \
153	    | grep -q mlxsw_spectrum
154	check_err $? "no extack for bridge attach rejection"
155
156	ip link set dev $swp2 addr 00:11:22:33:44:55 &>/dev/null
157	check_fail $? "Changing swp2's MAC address permitted"
158	ip link set dev $swp2 addr 00:11:22:33:44:55 2>&1 >/dev/null \
159	    | grep -q mlxsw_spectrum
160	check_err $? "no extack for bridge port MAC address change rejection"
161
162	log_test "RIF - attach port with bad MAC to bridge"
163
164	ip link del dev d
165	ip link del dev br1
166	ip addr del dev $swp1 192.0.2.1/28
167}
168
169rif_non_inherit_bridge_addr_test()
170{
171	local swp2_mac=$(mac_get $swp2)
172
173	RET=0
174
175	# Create first RIF
176	ip addr add dev $swp1 192.0.2.1/28
177	check_err $?
178
179	# Create a FID RIF
180	ip link add name br1 up type bridge vlan_filtering 0
181	ip link set dev br1 addr $swp2_mac
182	ip link set dev $swp2 master br1
183	ip addr add dev br1 192.0.2.17/28
184	check_err $?
185
186	# Prepare a device with a low MAC address
187	ip link add name d up type dummy
188	ip link set dev d addr 00:11:22:33:44:55
189
190	# Attach the device to br1. Since the bridge address was set, it should
191	# work.
192	ip link set dev d master br1 &>/dev/null
193	check_err $? "Could not attach a device with low MAC to a bridge with RIF"
194
195	# Port MAC address change should be allowed for a bridge with set MAC.
196	ip link set dev $swp2 addr 00:11:22:33:44:55
197	check_err $? "Changing swp2's MAC address not permitted"
198
199	log_test "RIF - attach port with bad MAC to bridge with set MAC"
200
201	ip link set dev $swp2 addr $swp2_mac
202	ip link del dev d
203	ip link del dev br1
204	ip addr del dev $swp1 192.0.2.1/28
205}
206
207vlan_interface_deletion_test()
208{
209	# Test that when a VLAN interface is deleted, its associated router
210	# interface (RIF) is correctly deleted and not leaked. See commit
211	# c360867ec46a ("mlxsw: spectrum: Delete RIF when VLAN device is
212	# removed") for more details
213	RET=0
214
215	ip link add name br0 type bridge vlan_filtering 1
216	ip link set dev $swp1 master br0
217
218	ip link add link br0 name br0.10 type vlan id 10
219	ip -6 address add 2001:db8:1::1/64 dev br0.10
220	ip link del dev br0.10
221
222	# If we leaked the previous RIF, then this should produce a trace
223	ip link add link br0 name br0.20 type vlan id 20
224	ip -6 address add 2001:db8:1::1/64 dev br0.20
225	ip link del dev br0.20
226
227	log_test "vlan interface deletion"
228
229	ip link del dev br0
230}
231
232bridge_deletion_test()
233{
234	# Test that when a bridge with VLAN interfaces is deleted, we correctly
235	# delete the associated RIFs. See commit 602b74eda813 ("mlxsw:
236	# spectrum_switchdev: Do not leak RIFs when removing bridge") for more
237	# details
238	RET=0
239
240	ip link add name br0 type bridge vlan_filtering 1
241	ip link set dev $swp1 master br0
242	ip -6 address add 2001:db8::1/64 dev br0
243
244	ip link add link br0 name br0.10 type vlan id 10
245	ip -6 address add 2001:db8:1::1/64 dev br0.10
246
247	ip link add link br0 name br0.20 type vlan id 20
248	ip -6 address add 2001:db8:2::1/64 dev br0.20
249
250	ip link del dev br0
251
252	# If we leaked previous RIFs, then this should produce a trace
253	ip -6 address add 2001:db8:1::1/64 dev $swp1
254	ip -6 address del 2001:db8:1::1/64 dev $swp1
255
256	log_test "bridge deletion"
257}
258
259bridge_vlan_flags_test()
260{
261	# Test that when bridge VLAN flags are toggled, we do not take
262	# unnecessary references on related structs. See commit 9e25826ffc94
263	# ("mlxsw: spectrum_switchdev: Fix port_vlan refcounting") for more
264	# details
265	RET=0
266
267	ip link add name br0 type bridge vlan_filtering 1
268	ip link set dev $swp1 master br0
269
270	bridge vlan add vid 10 dev $swp1 pvid untagged
271	bridge vlan add vid 10 dev $swp1 untagged
272	bridge vlan add vid 10 dev $swp1 pvid
273	bridge vlan add vid 10 dev $swp1
274	ip link del dev br0
275
276	# If we did not handle references correctly, then this should produce a
277	# trace
278	devlink dev reload "$DEVLINK_DEV"
279
280	# Allow netdevices to be re-created following the reload
281	sleep 20
282
283	log_test "bridge vlan flags"
284}
285
286vlan_1_test()
287{
288	# Test that VLAN 1 can be configured over mlxsw ports. In the past it
289	# was used internally for untagged traffic. See commit 47bf9df2e820
290	# ("mlxsw: spectrum: Forbid creation of VLAN 1 over port/LAG") for more
291	# details
292	RET=0
293
294	ip link add link $swp1 name $swp1.1 type vlan id 1
295	check_err $? "did not manage to create vlan 1 when should"
296
297	log_test "vlan 1"
298
299	ip link del dev $swp1.1
300}
301
302lag_bridge_upper_test()
303{
304	# Test that ports cannot be enslaved to LAG devices that have uppers
305	# and that failure is handled gracefully. See commit b3529af6bb0d
306	# ("spectrum: Reference count VLAN entries") for more details
307	RET=0
308
309	ip link add name bond1 type bond mode 802.3ad
310
311	ip link add name br0 type bridge vlan_filtering 1
312	ip link set dev bond1 master br0
313
314	ip link set dev $swp1 down
315	ip link set dev $swp1 master bond1 &> /dev/null
316	check_fail $? "managed to enslave port to lag when should not"
317
318	# This might generate a trace, if we did not handle the failure
319	# correctly
320	ip -6 address add 2001:db8:1::1/64 dev $swp1
321	ip -6 address del 2001:db8:1::1/64 dev $swp1
322
323	log_test "lag with bridge upper"
324
325	ip link del dev br0
326	ip link del dev bond1
327}
328
329duplicate_vlans_test()
330{
331	# Test that on a given port a VLAN is only used once. Either as VLAN
332	# in a VLAN-aware bridge or as a VLAN device
333	RET=0
334
335	ip link add name br0 type bridge vlan_filtering 1
336	ip link set dev $swp1 master br0
337	bridge vlan add vid 10 dev $swp1
338
339	ip link add link $swp1 name $swp1.10 type vlan id 10 &> /dev/null
340	check_fail $? "managed to create vlan device when should not"
341
342	bridge vlan del vid 10 dev $swp1
343	ip link add link $swp1 name $swp1.10 type vlan id 10
344	check_err $? "did not manage to create vlan device when should"
345	bridge vlan add vid 10 dev $swp1 &> /dev/null
346	check_fail $? "managed to add bridge vlan when should not"
347
348	log_test "duplicate vlans"
349
350	ip link del dev $swp1.10
351	ip link del dev br0
352}
353
354vlan_rif_refcount_test()
355{
356	# Test that RIFs representing VLAN interfaces are not affected from
357	# ports member in the VLAN. We use the offload indication on routes
358	# configured on the RIF to understand if it was created / destroyed
359	RET=0
360
361	ip link add name br0 type bridge vlan_filtering 1
362	ip link set dev $swp1 master br0
363
364	ip link set dev $swp1 up
365	ip link set dev br0 up
366
367	ip link add link br0 name br0.10 up type vlan id 10
368	ip -6 address add 2001:db8:1::1/64 dev br0.10
369
370	busywait "$TIMEOUT" wait_for_offload \
371		ip -6 route get fibmatch 2001:db8:1::2 dev br0.10
372	check_err $? "vlan rif was not created before adding port to vlan"
373
374	bridge vlan add vid 10 dev $swp1
375	busywait "$TIMEOUT" wait_for_offload \
376		ip -6 route get fibmatch 2001:db8:1::2 dev br0.10
377	check_err $? "vlan rif was destroyed after adding port to vlan"
378
379	bridge vlan del vid 10 dev $swp1
380	busywait "$TIMEOUT" wait_for_offload \
381		ip -6 route get fibmatch 2001:db8:1::2 dev br0.10
382	check_err $? "vlan rif was destroyed after removing port from vlan"
383
384	ip link set dev $swp1 nomaster
385	busywait "$TIMEOUT" not wait_for_offload \
386		ip -6 route get fibmatch 2001:db8:1::2 dev br0.10
387	check_err $? "vlan rif was not destroyed after unlinking port from bridge"
388
389	log_test "vlan rif refcount"
390
391	ip link del dev br0.10
392	ip link set dev $swp1 down
393	ip link del dev br0
394}
395
396subport_rif_refcount_test()
397{
398	# Test that RIFs representing upper devices of physical ports are
399	# reference counted correctly and destroyed when should. We use the
400	# offload indication on routes configured on the RIF to understand if
401	# it was created / destroyed
402	RET=0
403
404	ip link add name bond1 type bond mode 802.3ad
405	ip link set dev $swp1 down
406	ip link set dev $swp2 down
407	ip link set dev $swp1 master bond1
408	ip link set dev $swp2 master bond1
409
410	ip link set dev bond1 up
411	ip link add link bond1 name bond1.10 up type vlan id 10
412	ip -6 address add 2001:db8:1::1/64 dev bond1
413	ip -6 address add 2001:db8:2::1/64 dev bond1.10
414
415	busywait "$TIMEOUT" wait_for_offload \
416		ip -6 route get fibmatch 2001:db8:1::2 dev bond1
417	check_err $? "subport rif was not created on lag device"
418	busywait "$TIMEOUT" wait_for_offload \
419		ip -6 route get fibmatch 2001:db8:2::2 dev bond1.10
420	check_err $? "subport rif was not created on vlan device"
421
422	ip link set dev $swp1 nomaster
423	busywait "$TIMEOUT" wait_for_offload \
424		ip -6 route get fibmatch 2001:db8:1::2 dev bond1
425	check_err $? "subport rif of lag device was destroyed when should not"
426	busywait "$TIMEOUT" wait_for_offload \
427		ip -6 route get fibmatch 2001:db8:2::2 dev bond1.10
428	check_err $? "subport rif of vlan device was destroyed when should not"
429
430	ip link set dev $swp2 nomaster
431	busywait "$TIMEOUT" not wait_for_offload \
432		ip -6 route get fibmatch 2001:db8:1::2 dev bond1
433	check_err $? "subport rif of lag device was not destroyed when should"
434	busywait "$TIMEOUT" not wait_for_offload \
435		ip -6 route get fibmatch 2001:db8:2::2 dev bond1.10
436	check_err $? "subport rif of vlan device was not destroyed when should"
437
438	log_test "subport rif refcount"
439
440	ip link del dev bond1.10
441	ip link del dev bond1
442}
443
444subport_rif_lag_join_test()
445{
446	# Test that the reference count of a RIF configured for a LAG is
447	# incremented / decremented when ports join / leave the LAG. We use the
448	# offload indication on routes configured on the RIF to understand if
449	# it was created / destroyed
450	RET=0
451
452	ip link add name bond1 type bond mode 802.3ad
453	ip link set dev $swp1 down
454	ip link set dev $swp2 down
455	ip link set dev $swp1 master bond1
456	ip link set dev $swp2 master bond1
457
458	ip link set dev bond1 up
459	ip -6 address add 2001:db8:1::1/64 dev bond1
460
461	busywait "$TIMEOUT" wait_for_offload \
462		ip -6 route get fibmatch 2001:db8:1::2 dev bond1
463	check_err $? "subport rif was not created on lag device"
464
465	ip link set dev $swp1 nomaster
466	busywait "$TIMEOUT" wait_for_offload \
467		ip -6 route get fibmatch 2001:db8:1::2 dev bond1
468	check_err $? "subport rif of lag device was destroyed after removing one port"
469
470	ip link set dev $swp1 master bond1
471	ip link set dev $swp2 nomaster
472	busywait "$TIMEOUT" wait_for_offload \
473		ip -6 route get fibmatch 2001:db8:1::2 dev bond1
474	check_err $? "subport rif of lag device was destroyed after re-adding a port and removing another"
475
476	ip link set dev $swp1 nomaster
477	busywait "$TIMEOUT" not wait_for_offload \
478		ip -6 route get fibmatch 2001:db8:1::2 dev bond1
479	check_err $? "subport rif of lag device was not destroyed when should"
480
481	log_test "subport rif lag join"
482
483	ip link del dev bond1
484}
485
486vlan_dev_deletion_test()
487{
488	# Test that VLAN devices are correctly deleted / unlinked when enslaved
489	# to bridge
490	RET=0
491
492	ip link add name br10 type bridge
493	ip link add name br20 type bridge
494	ip link add name br30 type bridge
495	ip link add link $swp1 name $swp1.10 type vlan id 10
496	ip link add link $swp1 name $swp1.20 type vlan id 20
497	ip link add link $swp1 name $swp1.30 type vlan id 30
498	ip link set dev $swp1.10 master br10
499	ip link set dev $swp1.20 master br20
500	ip link set dev $swp1.30 master br30
501
502	# If we did not handle the situation correctly, then these operations
503	# might produce a trace
504	ip link set dev $swp1.30 nomaster
505	ip link del dev $swp1.20
506	# Deletion via ioctl uses different code paths from netlink
507	vconfig rem $swp1.10 &> /dev/null
508
509	log_test "vlan device deletion"
510
511	ip link del dev $swp1.30
512	ip link del dev br30
513	ip link del dev br20
514	ip link del dev br10
515}
516
517lag_create()
518{
519	ip link add name bond1 type bond mode 802.3ad
520	ip link set dev $swp1 down
521	ip link set dev $swp2 down
522	ip link set dev $swp1 master bond1
523	ip link set dev $swp2 master bond1
524
525	ip link add link bond1 name bond1.10 type vlan id 10
526	ip link add link bond1 name bond1.20 type vlan id 20
527
528	ip link add name br0 type bridge vlan_filtering 1
529	ip link set dev bond1 master br0
530
531	ip link add name br10 type bridge
532	ip link set dev bond1.10 master br10
533
534	ip link add name br20 type bridge
535	ip link set dev bond1.20 master br20
536}
537
538lag_unlink_slaves_test()
539{
540	# Test that ports are correctly unlinked from their LAG master, when
541	# the LAG and its VLAN uppers are enslaved to bridges
542	RET=0
543
544	lag_create
545
546	ip link set dev $swp1 nomaster
547	check_err $? "lag slave $swp1 was not unlinked from master"
548	ip link set dev $swp2 nomaster
549	check_err $? "lag slave $swp2 was not unlinked from master"
550
551	# Try to configure corresponding VLANs as router interfaces
552	ip -6 address add 2001:db8:1::1/64 dev $swp1
553	check_err $? "failed to configure ip address on $swp1"
554
555	ip link add link $swp1 name $swp1.10 type vlan id 10
556	ip -6 address add 2001:db8:10::1/64 dev $swp1.10
557	check_err $? "failed to configure ip address on $swp1.10"
558
559	ip link add link $swp1 name $swp1.20 type vlan id 20
560	ip -6 address add 2001:db8:20::1/64 dev $swp1.20
561	check_err $? "failed to configure ip address on $swp1.20"
562
563	log_test "lag slaves unlinking"
564
565	ip link del dev $swp1.20
566	ip link del dev $swp1.10
567	ip address flush dev $swp1
568
569	ip link del dev br20
570	ip link del dev br10
571	ip link del dev br0
572	ip link del dev bond1
573}
574
575lag_dev_deletion_test()
576{
577	# Test that LAG device is correctly deleted, when the LAG and its VLAN
578	# uppers are enslaved to bridges
579	RET=0
580
581	lag_create
582
583	ip link del dev bond1
584
585	log_test "lag device deletion"
586
587	ip link del dev br20
588	ip link del dev br10
589	ip link del dev br0
590}
591
592vlan_interface_uppers_test()
593{
594	# Test that uppers of a VLAN interface are correctly sanitized
595	RET=0
596
597	ip link add name br0 type bridge vlan_filtering 1
598	ip link set dev $swp1 master br0
599
600	ip link add link br0 name br0.10 type vlan id 10
601	ip link add link br0.10 name macvlan0 \
602		type macvlan mode private &> /dev/null
603	check_fail $? "managed to create a macvlan when should not"
604
605	ip -6 address add 2001:db8:1::1/64 dev br0.10
606	ip link add link br0.10 name macvlan0 type macvlan mode private
607	check_err $? "did not manage to create a macvlan when should"
608
609	ip link del dev macvlan0
610
611	ip link add name vrf-test type vrf table 10
612	ip link set dev br0.10 master vrf-test
613	check_err $? "did not manage to enslave vlan interface to vrf"
614	ip link del dev vrf-test
615
616	ip link add name br-test type bridge
617	ip link set dev br0.10 master br-test &> /dev/null
618	check_fail $? "managed to enslave vlan interface to bridge when should not"
619	ip link del dev br-test
620
621	log_test "vlan interface uppers"
622
623	ip link del dev br0
624}
625
626bridge_extern_learn_test()
627{
628	# Test that externally learned entries added from user space are
629	# marked as offloaded
630	RET=0
631
632	ip link add name br0 type bridge
633	ip link set dev $swp1 master br0
634
635	bridge fdb add de:ad:be:ef:13:37 dev $swp1 master extern_learn
636
637	busywait "$TIMEOUT" wait_for_offload \
638		bridge fdb show brport $swp1 de:ad:be:ef:13:37
639	check_err $? "fdb entry not marked as offloaded when should"
640
641	log_test "externally learned fdb entry"
642
643	ip link del dev br0
644}
645
646neigh_offload_test()
647{
648	# Test that IPv4 and IPv6 neighbour entries are marked as offloaded
649	RET=0
650
651	ip -4 address add 192.0.2.1/24 dev $swp1
652	ip -6 address add 2001:db8:1::1/64 dev $swp1
653
654	ip -4 neigh add 192.0.2.2 lladdr de:ad:be:ef:13:37 nud perm dev $swp1
655	ip -6 neigh add 2001:db8:1::2 lladdr de:ad:be:ef:13:37 nud perm \
656		dev $swp1
657
658	busywait "$TIMEOUT" wait_for_offload \
659		ip -4 neigh show dev $swp1 192.0.2.2
660	check_err $? "ipv4 neigh entry not marked as offloaded when should"
661	busywait "$TIMEOUT" wait_for_offload \
662		ip -6 neigh show dev $swp1 2001:db8:1::2
663	check_err $? "ipv6 neigh entry not marked as offloaded when should"
664
665	log_test "neighbour offload indication"
666
667	ip -6 neigh del 2001:db8:1::2 dev $swp1
668	ip -4 neigh del 192.0.2.2 dev $swp1
669	ip -6 address del 2001:db8:1::1/64 dev $swp1
670	ip -4 address del 192.0.2.1/24 dev $swp1
671}
672
673nexthop_offload_test()
674{
675	# Test that IPv4 and IPv6 nexthops are marked as offloaded
676	RET=0
677
678	sysctl_set net.ipv6.conf.$swp2.keep_addr_on_down 1
679	simple_if_init $swp1 192.0.2.1/24 2001:db8:1::1/64
680	simple_if_init $swp2 192.0.2.2/24 2001:db8:1::2/64
681	setup_wait
682
683	ip -4 route add 198.51.100.0/24 vrf v$swp1 \
684		nexthop via 192.0.2.2 dev $swp1
685	ip -6 route add 2001:db8:2::/64 vrf v$swp1 \
686		nexthop via 2001:db8:1::2 dev $swp1
687
688	busywait "$TIMEOUT" wait_for_offload \
689		ip -4 route show 198.51.100.0/24 vrf v$swp1
690	check_err $? "ipv4 nexthop not marked as offloaded when should"
691	busywait "$TIMEOUT" wait_for_offload \
692		ip -6 route show 2001:db8:2::/64 vrf v$swp1
693	check_err $? "ipv6 nexthop not marked as offloaded when should"
694
695	ip link set dev $swp2 down
696	sleep 1
697
698	busywait "$TIMEOUT" not wait_for_offload \
699		ip -4 route show 198.51.100.0/24 vrf v$swp1
700	check_err $? "ipv4 nexthop marked as offloaded when should not"
701	busywait "$TIMEOUT" not wait_for_offload \
702		ip -6 route show 2001:db8:2::/64 vrf v$swp1
703	check_err $? "ipv6 nexthop marked as offloaded when should not"
704
705	ip link set dev $swp2 up
706	setup_wait
707
708	busywait "$TIMEOUT" wait_for_offload \
709		ip -4 route show 198.51.100.0/24 vrf v$swp1
710	check_err $? "ipv4 nexthop not marked as offloaded after neigh add"
711	busywait "$TIMEOUT" wait_for_offload \
712		ip -6 route show 2001:db8:2::/64 vrf v$swp1
713	check_err $? "ipv6 nexthop not marked as offloaded after neigh add"
714
715	log_test "nexthop offload indication"
716
717	ip -6 route del 2001:db8:2::/64 vrf v$swp1
718	ip -4 route del 198.51.100.0/24 vrf v$swp1
719
720	simple_if_fini $swp2 192.0.2.2/24 2001:db8:1::2/64
721	simple_if_fini $swp1 192.0.2.1/24 2001:db8:1::1/64
722	sysctl_restore net.ipv6.conf.$swp2.keep_addr_on_down
723}
724
725nexthop_obj_invalid_test()
726{
727	# Test that invalid nexthop object configurations are rejected
728	RET=0
729
730	simple_if_init $swp1 192.0.2.1/24 2001:db8:1::1/64
731	simple_if_init $swp2 192.0.2.2/24 2001:db8:1::2/64
732	setup_wait
733
734	ip nexthop add id 1 via 192.0.2.3 fdb
735	check_fail $? "managed to configure an FDB nexthop when should not"
736
737	ip nexthop add id 1 encap mpls 200/300 via 192.0.2.3 dev $swp1
738	check_fail $? "managed to configure a nexthop with MPLS encap when should not"
739
740	ip nexthop add id 1 dev $swp1
741	ip nexthop add id 2 dev $swp1
742	ip nexthop add id 10 group 1/2
743	check_fail $? "managed to configure a nexthop group with device-only nexthops when should not"
744
745	log_test "nexthop objects - invalid configurations"
746
747	ip nexthop del id 2
748	ip nexthop del id 1
749
750	simple_if_fini $swp2 192.0.2.2/24 2001:db8:1::2/64
751	simple_if_fini $swp1 192.0.2.1/24 2001:db8:1::1/64
752}
753
754nexthop_obj_offload_test()
755{
756	# Test offload indication of nexthop objects
757	RET=0
758
759	simple_if_init $swp1 192.0.2.1/24 2001:db8:1::1/64
760	simple_if_init $swp2
761	setup_wait
762
763	ip nexthop add id 1 via 192.0.2.2 dev $swp1
764	ip neigh replace 192.0.2.2 lladdr 00:11:22:33:44:55 nud reachable \
765		dev $swp1
766
767	busywait "$TIMEOUT" wait_for_offload \
768		ip nexthop show id 1
769	check_err $? "nexthop not marked as offloaded when should"
770
771	ip neigh replace 192.0.2.2 nud failed dev $swp1
772	busywait "$TIMEOUT" not wait_for_offload \
773		ip nexthop show id 1
774	check_err $? "nexthop marked as offloaded after setting neigh to failed state"
775
776	ip neigh replace 192.0.2.2 lladdr 00:11:22:33:44:55 nud reachable \
777		dev $swp1
778	busywait "$TIMEOUT" wait_for_offload \
779		ip nexthop show id 1
780	check_err $? "nexthop not marked as offloaded after neigh replace"
781
782	ip nexthop replace id 1 via 192.0.2.3 dev $swp1
783	busywait "$TIMEOUT" not wait_for_offload \
784		ip nexthop show id 1
785	check_err $? "nexthop marked as offloaded after replacing to use an invalid address"
786
787	ip nexthop replace id 1 via 192.0.2.2 dev $swp1
788	busywait "$TIMEOUT" wait_for_offload \
789		ip nexthop show id 1
790	check_err $? "nexthop not marked as offloaded after replacing to use a valid address"
791
792	log_test "nexthop objects offload indication"
793
794	ip neigh del 192.0.2.2 dev $swp1
795	ip nexthop del id 1
796
797	simple_if_fini $swp2
798	simple_if_fini $swp1 192.0.2.1/24 2001:db8:1::1/64
799}
800
801nexthop_obj_group_offload_test()
802{
803	# Test offload indication of nexthop group objects
804	RET=0
805
806	simple_if_init $swp1 192.0.2.1/24 2001:db8:1::1/64
807	simple_if_init $swp2
808	setup_wait
809
810	ip nexthop add id 1 via 192.0.2.2 dev $swp1
811	ip nexthop add id 2 via 2001:db8:1::2 dev $swp1
812	ip nexthop add id 10 group 1/2
813	ip neigh replace 192.0.2.2 lladdr 00:11:22:33:44:55 nud reachable \
814		dev $swp1
815	ip neigh replace 192.0.2.3 lladdr 00:11:22:33:44:55 nud reachable \
816		dev $swp1
817	ip neigh replace 2001:db8:1::2 lladdr 00:11:22:33:44:55 nud reachable \
818		dev $swp1
819
820	busywait "$TIMEOUT" wait_for_offload \
821		ip nexthop show id 1
822	check_err $? "IPv4 nexthop not marked as offloaded when should"
823	busywait "$TIMEOUT" wait_for_offload \
824		ip nexthop show id 2
825	check_err $? "IPv6 nexthop not marked as offloaded when should"
826	busywait "$TIMEOUT" wait_for_offload \
827		ip nexthop show id 10
828	check_err $? "nexthop group not marked as offloaded when should"
829
830	# Invalidate nexthop id 1
831	ip neigh replace 192.0.2.2 nud failed dev $swp1
832	busywait "$TIMEOUT" not wait_for_offload \
833		ip nexthop show id 10
834	check_fail $? "nexthop group not marked as offloaded with one valid nexthop"
835
836	# Invalidate nexthop id 2
837	ip neigh replace 2001:db8:1::2 nud failed dev $swp1
838	busywait "$TIMEOUT" not wait_for_offload \
839		ip nexthop show id 10
840	check_err $? "nexthop group marked as offloaded when should not"
841
842	# Revalidate nexthop id 1
843	ip nexthop replace id 1 via 192.0.2.3 dev $swp1
844	busywait "$TIMEOUT" wait_for_offload \
845		ip nexthop show id 10
846	check_err $? "nexthop group not marked as offloaded after revalidating nexthop"
847
848	log_test "nexthop group objects offload indication"
849
850	ip neigh del 2001:db8:1::2 dev $swp1
851	ip neigh del 192.0.2.3 dev $swp1
852	ip neigh del 192.0.2.2 dev $swp1
853	ip nexthop del id 10
854	ip nexthop del id 2
855	ip nexthop del id 1
856
857	simple_if_fini $swp2
858	simple_if_fini $swp1 192.0.2.1/24 2001:db8:1::1/64
859}
860
861nexthop_obj_blackhole_offload_test()
862{
863	# Test offload indication of blackhole nexthop objects
864	RET=0
865
866	ip nexthop add id 1 blackhole
867	busywait "$TIMEOUT" wait_for_offload \
868		ip nexthop show id 1
869	check_err $? "Blackhole nexthop not marked as offloaded when should"
870
871	ip nexthop add id 10 group 1
872	busywait "$TIMEOUT" wait_for_offload \
873		ip nexthop show id 10
874	check_err $? "Nexthop group not marked as offloaded when should"
875
876	log_test "blackhole nexthop objects offload indication"
877
878	ip nexthop del id 10
879	ip nexthop del id 1
880}
881
882nexthop_obj_route_offload_test()
883{
884	# Test offload indication of routes using nexthop objects
885	RET=0
886
887	simple_if_init $swp1 192.0.2.1/24 2001:db8:1::1/64
888	simple_if_init $swp2
889	setup_wait
890
891	ip nexthop add id 1 via 192.0.2.2 dev $swp1
892	ip neigh replace 192.0.2.2 lladdr 00:11:22:33:44:55 nud reachable \
893		dev $swp1
894	ip neigh replace 192.0.2.3 lladdr 00:11:22:33:44:55 nud reachable \
895		dev $swp1
896
897	ip route replace 198.51.100.0/24 nhid 1
898	busywait "$TIMEOUT" wait_for_offload \
899		ip route show 198.51.100.0/24
900	check_err $? "route not marked as offloaded when using valid nexthop"
901
902	ip nexthop replace id 1 via 192.0.2.3 dev $swp1
903	busywait "$TIMEOUT" wait_for_offload \
904		ip route show 198.51.100.0/24
905	check_err $? "route not marked as offloaded after replacing valid nexthop with a valid one"
906
907	ip nexthop replace id 1 via 192.0.2.4 dev $swp1
908	busywait "$TIMEOUT" not wait_for_offload \
909		ip route show 198.51.100.0/24
910	check_err $? "route marked as offloaded after replacing valid nexthop with an invalid one"
911
912	ip nexthop replace id 1 via 192.0.2.2 dev $swp1
913	busywait "$TIMEOUT" wait_for_offload \
914		ip route show 198.51.100.0/24
915	check_err $? "route not marked as offloaded after replacing invalid nexthop with a valid one"
916
917	log_test "routes using nexthop objects offload indication"
918
919	ip route del 198.51.100.0/24
920	ip neigh del 192.0.2.3 dev $swp1
921	ip neigh del 192.0.2.2 dev $swp1
922	ip nexthop del id 1
923
924	simple_if_fini $swp2
925	simple_if_fini $swp1 192.0.2.1/24 2001:db8:1::1/64
926}
927
928devlink_reload_test()
929{
930	# Test that after executing all the above configuration tests, a
931	# devlink reload can be performed without errors
932	RET=0
933
934	devlink dev reload "$DEVLINK_DEV"
935	check_err $? "devlink reload failed"
936
937	log_test "devlink reload - last test"
938
939	sleep 20
940}
941
942trap cleanup EXIT
943
944setup_prepare
945setup_wait
946
947tests_run
948
949exit $EXIT_STATUS
950