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	vlan_dev_deletion_test
26	lag_unlink_slaves_test
27	lag_dev_deletion_test
28	vlan_interface_uppers_test
29	bridge_extern_learn_test
30	neigh_offload_test
31	nexthop_offload_test
32	devlink_reload_test
33"
34NUM_NETIFS=2
35source $lib_dir/lib.sh
36source $lib_dir/devlink_lib.sh
37
38setup_prepare()
39{
40	swp1=${NETIFS[p1]}
41	swp2=${NETIFS[p2]}
42
43	ip link set dev $swp1 up
44	ip link set dev $swp2 up
45}
46
47cleanup()
48{
49	pre_cleanup
50
51	ip link set dev $swp2 down
52	ip link set dev $swp1 down
53}
54
55rif_set_addr_test()
56{
57	local swp1_mac=$(mac_get $swp1)
58	local swp2_mac=$(mac_get $swp2)
59
60	RET=0
61
62	# $swp1 and $swp2 likely got their IPv6 local addresses already, but
63	# here we need to test the transition to RIF.
64	ip addr flush dev $swp1
65	ip addr flush dev $swp2
66	sleep .1
67
68	ip addr add dev $swp1 192.0.2.1/28
69	check_err $?
70
71	ip link set dev $swp1 addr 00:11:22:33:44:55
72	check_err $?
73
74	# IP address enablement should be rejected if the MAC address prefix
75	# doesn't match other RIFs.
76	ip addr add dev $swp2 192.0.2.2/28 &>/dev/null
77	check_fail $? "IP address addition passed for a device with a wrong MAC"
78	ip addr add dev $swp2 192.0.2.2/28 2>&1 >/dev/null \
79	    | grep -q mlxsw_spectrum
80	check_err $? "no extack for IP address addition"
81
82	ip link set dev $swp2 addr 00:11:22:33:44:66
83	check_err $?
84	ip addr add dev $swp2 192.0.2.2/28 &>/dev/null
85	check_err $?
86
87	# Change of MAC address of a RIF should be forbidden if the new MAC
88	# doesn't share the prefix with other MAC addresses.
89	ip link set dev $swp2 addr 00:11:22:33:00:66 &>/dev/null
90	check_fail $? "change of MAC address passed for a wrong MAC"
91	ip link set dev $swp2 addr 00:11:22:33:00:66 2>&1 >/dev/null \
92	    | grep -q mlxsw_spectrum
93	check_err $? "no extack for MAC address change"
94
95	log_test "RIF - bad MAC change"
96
97	ip addr del dev $swp2 192.0.2.2/28
98	ip addr del dev $swp1 192.0.2.1/28
99
100	ip link set dev $swp2 addr $swp2_mac
101	ip link set dev $swp1 addr $swp1_mac
102}
103
104rif_vrf_set_addr_test()
105{
106	# Test that it is possible to set an IP address on a VRF upper despite
107	# its random MAC address.
108	RET=0
109
110	ip link add name vrf-test type vrf table 10
111	ip link set dev $swp1 master vrf-test
112
113	ip -4 address add 192.0.2.1/24 dev vrf-test
114	check_err $? "failed to set IPv4 address on VRF"
115	ip -6 address add 2001:db8:1::1/64 dev vrf-test
116	check_err $? "failed to set IPv6 address on VRF"
117
118	log_test "RIF - setting IP address on VRF"
119
120	ip link del dev vrf-test
121}
122
123rif_inherit_bridge_addr_test()
124{
125	RET=0
126
127	# Create first RIF
128	ip addr add dev $swp1 192.0.2.1/28
129	check_err $?
130
131	# Create a FID RIF
132	ip link add name br1 up type bridge vlan_filtering 0
133	ip link set dev $swp2 master br1
134	ip addr add dev br1 192.0.2.17/28
135	check_err $?
136
137	# Prepare a device with a low MAC address
138	ip link add name d up type dummy
139	ip link set dev d addr 00:11:22:33:44:55
140
141	# Attach the device to br1. That prompts bridge address change, which
142	# should be vetoed, thus preventing the attachment.
143	ip link set dev d master br1 &>/dev/null
144	check_fail $? "Device with low MAC was permitted to attach a bridge with RIF"
145	ip link set dev d master br1 2>&1 >/dev/null \
146	    | grep -q mlxsw_spectrum
147	check_err $? "no extack for bridge attach rejection"
148
149	ip link set dev $swp2 addr 00:11:22:33:44:55 &>/dev/null
150	check_fail $? "Changing swp2's MAC address permitted"
151	ip link set dev $swp2 addr 00:11:22:33:44:55 2>&1 >/dev/null \
152	    | grep -q mlxsw_spectrum
153	check_err $? "no extack for bridge port MAC address change rejection"
154
155	log_test "RIF - attach port with bad MAC to bridge"
156
157	ip link del dev d
158	ip link del dev br1
159	ip addr del dev $swp1 192.0.2.1/28
160}
161
162rif_non_inherit_bridge_addr_test()
163{
164	local swp2_mac=$(mac_get $swp2)
165
166	RET=0
167
168	# Create first RIF
169	ip addr add dev $swp1 192.0.2.1/28
170	check_err $?
171
172	# Create a FID RIF
173	ip link add name br1 up type bridge vlan_filtering 0
174	ip link set dev br1 addr $swp2_mac
175	ip link set dev $swp2 master br1
176	ip addr add dev br1 192.0.2.17/28
177	check_err $?
178
179	# Prepare a device with a low MAC address
180	ip link add name d up type dummy
181	ip link set dev d addr 00:11:22:33:44:55
182
183	# Attach the device to br1. Since the bridge address was set, it should
184	# work.
185	ip link set dev d master br1 &>/dev/null
186	check_err $? "Could not attach a device with low MAC to a bridge with RIF"
187
188	# Port MAC address change should be allowed for a bridge with set MAC.
189	ip link set dev $swp2 addr 00:11:22:33:44:55
190	check_err $? "Changing swp2's MAC address not permitted"
191
192	log_test "RIF - attach port with bad MAC to bridge with set MAC"
193
194	ip link set dev $swp2 addr $swp2_mac
195	ip link del dev d
196	ip link del dev br1
197	ip addr del dev $swp1 192.0.2.1/28
198}
199
200vlan_interface_deletion_test()
201{
202	# Test that when a VLAN interface is deleted, its associated router
203	# interface (RIF) is correctly deleted and not leaked. See commit
204	# c360867ec46a ("mlxsw: spectrum: Delete RIF when VLAN device is
205	# removed") for more details
206	RET=0
207
208	ip link add name br0 type bridge vlan_filtering 1
209	ip link set dev $swp1 master br0
210
211	ip link add link br0 name br0.10 type vlan id 10
212	ip -6 address add 2001:db8:1::1/64 dev br0.10
213	ip link del dev br0.10
214
215	# If we leaked the previous RIF, then this should produce a trace
216	ip link add link br0 name br0.20 type vlan id 20
217	ip -6 address add 2001:db8:1::1/64 dev br0.20
218	ip link del dev br0.20
219
220	log_test "vlan interface deletion"
221
222	ip link del dev br0
223}
224
225bridge_deletion_test()
226{
227	# Test that when a bridge with VLAN interfaces is deleted, we correctly
228	# delete the associated RIFs. See commit 602b74eda813 ("mlxsw:
229	# spectrum_switchdev: Do not leak RIFs when removing bridge") for more
230	# details
231	RET=0
232
233	ip link add name br0 type bridge vlan_filtering 1
234	ip link set dev $swp1 master br0
235	ip -6 address add 2001:db8::1/64 dev br0
236
237	ip link add link br0 name br0.10 type vlan id 10
238	ip -6 address add 2001:db8:1::1/64 dev br0.10
239
240	ip link add link br0 name br0.20 type vlan id 20
241	ip -6 address add 2001:db8:2::1/64 dev br0.20
242
243	ip link del dev br0
244
245	# If we leaked previous RIFs, then this should produce a trace
246	ip -6 address add 2001:db8:1::1/64 dev $swp1
247	ip -6 address del 2001:db8:1::1/64 dev $swp1
248
249	log_test "bridge deletion"
250}
251
252bridge_vlan_flags_test()
253{
254	# Test that when bridge VLAN flags are toggled, we do not take
255	# unnecessary references on related structs. See commit 9e25826ffc94
256	# ("mlxsw: spectrum_switchdev: Fix port_vlan refcounting") for more
257	# details
258	RET=0
259
260	ip link add name br0 type bridge vlan_filtering 1
261	ip link set dev $swp1 master br0
262
263	bridge vlan add vid 10 dev $swp1 pvid untagged
264	bridge vlan add vid 10 dev $swp1 untagged
265	bridge vlan add vid 10 dev $swp1 pvid
266	bridge vlan add vid 10 dev $swp1
267	ip link del dev br0
268
269	# If we did not handle references correctly, then this should produce a
270	# trace
271	devlink dev reload "$DEVLINK_DEV"
272
273	# Allow netdevices to be re-created following the reload
274	sleep 20
275
276	log_test "bridge vlan flags"
277}
278
279vlan_1_test()
280{
281	# Test that VLAN 1 can be configured over mlxsw ports. In the past it
282	# was used internally for untagged traffic. See commit 47bf9df2e820
283	# ("mlxsw: spectrum: Forbid creation of VLAN 1 over port/LAG") for more
284	# details
285	RET=0
286
287	ip link add link $swp1 name $swp1.1 type vlan id 1
288	check_err $? "did not manage to create vlan 1 when should"
289
290	log_test "vlan 1"
291
292	ip link del dev $swp1.1
293}
294
295lag_bridge_upper_test()
296{
297	# Test that ports cannot be enslaved to LAG devices that have uppers
298	# and that failure is handled gracefully. See commit b3529af6bb0d
299	# ("spectrum: Reference count VLAN entries") for more details
300	RET=0
301
302	ip link add name bond1 type bond mode 802.3ad
303
304	ip link add name br0 type bridge vlan_filtering 1
305	ip link set dev bond1 master br0
306
307	ip link set dev $swp1 down
308	ip link set dev $swp1 master bond1 &> /dev/null
309	check_fail $? "managed to enslave port to lag when should not"
310
311	# This might generate a trace, if we did not handle the failure
312	# correctly
313	ip -6 address add 2001:db8:1::1/64 dev $swp1
314	ip -6 address del 2001:db8:1::1/64 dev $swp1
315
316	log_test "lag with bridge upper"
317
318	ip link del dev br0
319	ip link del dev bond1
320}
321
322duplicate_vlans_test()
323{
324	# Test that on a given port a VLAN is only used once. Either as VLAN
325	# in a VLAN-aware bridge or as a VLAN device
326	RET=0
327
328	ip link add name br0 type bridge vlan_filtering 1
329	ip link set dev $swp1 master br0
330	bridge vlan add vid 10 dev $swp1
331
332	ip link add link $swp1 name $swp1.10 type vlan id 10 &> /dev/null
333	check_fail $? "managed to create vlan device when should not"
334
335	bridge vlan del vid 10 dev $swp1
336	ip link add link $swp1 name $swp1.10 type vlan id 10
337	check_err $? "did not manage to create vlan device when should"
338	bridge vlan add vid 10 dev $swp1 &> /dev/null
339	check_fail $? "managed to add bridge vlan when should not"
340
341	log_test "duplicate vlans"
342
343	ip link del dev $swp1.10
344	ip link del dev br0
345}
346
347vlan_rif_refcount_test()
348{
349	# Test that RIFs representing VLAN interfaces are not affected from
350	# ports member in the VLAN. We use the offload indication on routes
351	# configured on the RIF to understand if it was created / destroyed
352	RET=0
353
354	ip link add name br0 type bridge vlan_filtering 1
355	ip link set dev $swp1 master br0
356
357	ip link set dev $swp1 up
358	ip link set dev br0 up
359
360	ip link add link br0 name br0.10 up type vlan id 10
361	ip -6 address add 2001:db8:1::1/64 dev br0.10
362
363	ip -6 route get fibmatch 2001:db8:1::2 dev br0.10 | grep -q offload
364	check_err $? "vlan rif was not created before adding port to vlan"
365
366	bridge vlan add vid 10 dev $swp1
367	ip -6 route get fibmatch 2001:db8:1::2 dev br0.10 | grep -q offload
368	check_err $? "vlan rif was destroyed after adding port to vlan"
369
370	bridge vlan del vid 10 dev $swp1
371	ip -6 route get fibmatch 2001:db8:1::2 dev br0.10 | grep -q offload
372	check_err $? "vlan rif was destroyed after removing port from vlan"
373
374	ip link set dev $swp1 nomaster
375	ip -6 route get fibmatch 2001:db8:1::2 dev br0.10 | grep -q offload
376	check_fail $? "vlan rif was not destroyed after unlinking port from bridge"
377
378	log_test "vlan rif refcount"
379
380	ip link del dev br0.10
381	ip link set dev $swp1 down
382	ip link del dev br0
383}
384
385subport_rif_refcount_test()
386{
387	# Test that RIFs representing upper devices of physical ports are
388	# reference counted correctly and destroyed when should. We use the
389	# offload indication on routes configured on the RIF to understand if
390	# it was created / destroyed
391	RET=0
392
393	ip link add name bond1 type bond mode 802.3ad
394	ip link set dev $swp1 down
395	ip link set dev $swp2 down
396	ip link set dev $swp1 master bond1
397	ip link set dev $swp2 master bond1
398
399	ip link set dev bond1 up
400	ip link add link bond1 name bond1.10 up type vlan id 10
401	ip -6 address add 2001:db8:1::1/64 dev bond1
402	ip -6 address add 2001:db8:2::1/64 dev bond1.10
403
404	ip -6 route get fibmatch 2001:db8:1::2 dev bond1 | grep -q offload
405	check_err $? "subport rif was not created on lag device"
406	ip -6 route get fibmatch 2001:db8:2::2 dev bond1.10 | grep -q offload
407	check_err $? "subport rif was not created on vlan device"
408
409	ip link set dev $swp1 nomaster
410	ip -6 route get fibmatch 2001:db8:1::2 dev bond1 | grep -q offload
411	check_err $? "subport rif of lag device was destroyed when should not"
412	ip -6 route get fibmatch 2001:db8:2::2 dev bond1.10 | grep -q offload
413	check_err $? "subport rif of vlan device was destroyed when should not"
414
415	ip link set dev $swp2 nomaster
416	ip -6 route get fibmatch 2001:db8:1::2 dev bond1 | grep -q offload
417	check_fail $? "subport rif of lag device was not destroyed when should"
418	ip -6 route get fibmatch 2001:db8:2::2 dev bond1.10 | grep -q offload
419	check_fail $? "subport rif of vlan device was not destroyed when should"
420
421	log_test "subport rif refcount"
422
423	ip link del dev bond1.10
424	ip link del dev bond1
425}
426
427vlan_dev_deletion_test()
428{
429	# Test that VLAN devices are correctly deleted / unlinked when enslaved
430	# to bridge
431	RET=0
432
433	ip link add name br10 type bridge
434	ip link add name br20 type bridge
435	ip link add name br30 type bridge
436	ip link add link $swp1 name $swp1.10 type vlan id 10
437	ip link add link $swp1 name $swp1.20 type vlan id 20
438	ip link add link $swp1 name $swp1.30 type vlan id 30
439	ip link set dev $swp1.10 master br10
440	ip link set dev $swp1.20 master br20
441	ip link set dev $swp1.30 master br30
442
443	# If we did not handle the situation correctly, then these operations
444	# might produce a trace
445	ip link set dev $swp1.30 nomaster
446	ip link del dev $swp1.20
447	# Deletion via ioctl uses different code paths from netlink
448	vconfig rem $swp1.10 &> /dev/null
449
450	log_test "vlan device deletion"
451
452	ip link del dev $swp1.30
453	ip link del dev br30
454	ip link del dev br20
455	ip link del dev br10
456}
457
458lag_create()
459{
460	ip link add name bond1 type bond mode 802.3ad
461	ip link set dev $swp1 down
462	ip link set dev $swp2 down
463	ip link set dev $swp1 master bond1
464	ip link set dev $swp2 master bond1
465
466	ip link add link bond1 name bond1.10 type vlan id 10
467	ip link add link bond1 name bond1.20 type vlan id 20
468
469	ip link add name br0 type bridge vlan_filtering 1
470	ip link set dev bond1 master br0
471
472	ip link add name br10 type bridge
473	ip link set dev bond1.10 master br10
474
475	ip link add name br20 type bridge
476	ip link set dev bond1.20 master br20
477}
478
479lag_unlink_slaves_test()
480{
481	# Test that ports are correctly unlinked from their LAG master, when
482	# the LAG and its VLAN uppers are enslaved to bridges
483	RET=0
484
485	lag_create
486
487	ip link set dev $swp1 nomaster
488	check_err $? "lag slave $swp1 was not unlinked from master"
489	ip link set dev $swp2 nomaster
490	check_err $? "lag slave $swp2 was not unlinked from master"
491
492	# Try to configure corresponding VLANs as router interfaces
493	ip -6 address add 2001:db8:1::1/64 dev $swp1
494	check_err $? "failed to configure ip address on $swp1"
495
496	ip link add link $swp1 name $swp1.10 type vlan id 10
497	ip -6 address add 2001:db8:10::1/64 dev $swp1.10
498	check_err $? "failed to configure ip address on $swp1.10"
499
500	ip link add link $swp1 name $swp1.20 type vlan id 20
501	ip -6 address add 2001:db8:20::1/64 dev $swp1.20
502	check_err $? "failed to configure ip address on $swp1.20"
503
504	log_test "lag slaves unlinking"
505
506	ip link del dev $swp1.20
507	ip link del dev $swp1.10
508	ip address flush dev $swp1
509
510	ip link del dev br20
511	ip link del dev br10
512	ip link del dev br0
513	ip link del dev bond1
514}
515
516lag_dev_deletion_test()
517{
518	# Test that LAG device is correctly deleted, when the LAG and its VLAN
519	# uppers are enslaved to bridges
520	RET=0
521
522	lag_create
523
524	ip link del dev bond1
525
526	log_test "lag device deletion"
527
528	ip link del dev br20
529	ip link del dev br10
530	ip link del dev br0
531}
532
533vlan_interface_uppers_test()
534{
535	# Test that uppers of a VLAN interface are correctly sanitized
536	RET=0
537
538	ip link add name br0 type bridge vlan_filtering 1
539	ip link set dev $swp1 master br0
540
541	ip link add link br0 name br0.10 type vlan id 10
542	ip link add link br0.10 name macvlan0 \
543		type macvlan mode private &> /dev/null
544	check_fail $? "managed to create a macvlan when should not"
545
546	ip -6 address add 2001:db8:1::1/64 dev br0.10
547	ip link add link br0.10 name macvlan0 type macvlan mode private
548	check_err $? "did not manage to create a macvlan when should"
549
550	ip link del dev macvlan0
551
552	ip link add name vrf-test type vrf table 10
553	ip link set dev br0.10 master vrf-test
554	check_err $? "did not manage to enslave vlan interface to vrf"
555	ip link del dev vrf-test
556
557	ip link add name br-test type bridge
558	ip link set dev br0.10 master br-test &> /dev/null
559	check_fail $? "managed to enslave vlan interface to bridge when should not"
560	ip link del dev br-test
561
562	log_test "vlan interface uppers"
563
564	ip link del dev br0
565}
566
567bridge_extern_learn_test()
568{
569	# Test that externally learned entries added from user space are
570	# marked as offloaded
571	RET=0
572
573	ip link add name br0 type bridge
574	ip link set dev $swp1 master br0
575
576	bridge fdb add de:ad:be:ef:13:37 dev $swp1 master extern_learn
577
578	bridge fdb show brport $swp1 | grep de:ad:be:ef:13:37 | grep -q offload
579	check_err $? "fdb entry not marked as offloaded when should"
580
581	log_test "externally learned fdb entry"
582
583	ip link del dev br0
584}
585
586neigh_offload_test()
587{
588	# Test that IPv4 and IPv6 neighbour entries are marked as offloaded
589	RET=0
590
591	ip -4 address add 192.0.2.1/24 dev $swp1
592	ip -6 address add 2001:db8:1::1/64 dev $swp1
593
594	ip -4 neigh add 192.0.2.2 lladdr de:ad:be:ef:13:37 nud perm dev $swp1
595	ip -6 neigh add 2001:db8:1::2 lladdr de:ad:be:ef:13:37 nud perm \
596		dev $swp1
597
598	ip -4 neigh show dev $swp1 | grep 192.0.2.2 | grep -q offload
599	check_err $? "ipv4 neigh entry not marked as offloaded when should"
600	ip -6 neigh show dev $swp1 | grep 2001:db8:1::2 | grep -q offload
601	check_err $? "ipv6 neigh entry not marked as offloaded when should"
602
603	log_test "neighbour offload indication"
604
605	ip -6 neigh del 2001:db8:1::2 dev $swp1
606	ip -4 neigh del 192.0.2.2 dev $swp1
607	ip -6 address del 2001:db8:1::1/64 dev $swp1
608	ip -4 address del 192.0.2.1/24 dev $swp1
609}
610
611nexthop_offload_test()
612{
613	# Test that IPv4 and IPv6 nexthops are marked as offloaded
614	RET=0
615
616	sysctl_set net.ipv6.conf.$swp2.keep_addr_on_down 1
617	simple_if_init $swp1 192.0.2.1/24 2001:db8:1::1/64
618	simple_if_init $swp2 192.0.2.2/24 2001:db8:1::2/64
619	setup_wait
620
621	ip -4 route add 198.51.100.0/24 vrf v$swp1 \
622		nexthop via 192.0.2.2 dev $swp1
623	ip -6 route add 2001:db8:2::/64 vrf v$swp1 \
624		nexthop via 2001:db8:1::2 dev $swp1
625
626	ip -4 route show 198.51.100.0/24 vrf v$swp1 | grep -q offload
627	check_err $? "ipv4 nexthop not marked as offloaded when should"
628	ip -6 route show 2001:db8:2::/64 vrf v$swp1 | grep -q offload
629	check_err $? "ipv6 nexthop not marked as offloaded when should"
630
631	ip link set dev $swp2 down
632	sleep 1
633
634	ip -4 route show 198.51.100.0/24 vrf v$swp1 | grep -q offload
635	check_fail $? "ipv4 nexthop marked as offloaded when should not"
636	ip -6 route show 2001:db8:2::/64 vrf v$swp1 | grep -q offload
637	check_fail $? "ipv6 nexthop marked as offloaded when should not"
638
639	ip link set dev $swp2 up
640	setup_wait
641
642	ip -4 route show 198.51.100.0/24 vrf v$swp1 | grep -q offload
643	check_err $? "ipv4 nexthop not marked as offloaded after neigh add"
644	ip -6 route show 2001:db8:2::/64 vrf v$swp1 | grep -q offload
645	check_err $? "ipv6 nexthop not marked as offloaded after neigh add"
646
647	log_test "nexthop offload indication"
648
649	ip -6 route del 2001:db8:2::/64 vrf v$swp1
650	ip -4 route del 198.51.100.0/24 vrf v$swp1
651
652	simple_if_fini $swp2 192.0.2.2/24 2001:db8:1::2/64
653	simple_if_fini $swp1 192.0.2.1/24 2001:db8:1::1/64
654	sysctl_restore net.ipv6.conf.$swp2.keep_addr_on_down
655}
656
657devlink_reload_test()
658{
659	# Test that after executing all the above configuration tests, a
660	# devlink reload can be performed without errors
661	RET=0
662
663	devlink dev reload "$DEVLINK_DEV"
664	check_err $? "devlink reload failed"
665
666	log_test "devlink reload - last test"
667
668	sleep 20
669}
670
671trap cleanup EXIT
672
673setup_prepare
674setup_wait
675
676tests_run
677
678exit $EXIT_STATUS
679