1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Test VLAN classification after routing and verify that the order of
5# configuration does not impact switch behavior. Verify that {RIF, Port}->VID
6# mapping is added correctly for existing {Port, VID}->FID mapping and that
7# {RIF, Port}->VID mapping is added correctly for new {Port, VID}->FID mapping.
8
9# +-------------------+                   +--------------------+
10# | H1                |                   | H2                 |
11# |                   |                   |                    |
12# |         $h1.10 +  |                   |  + $h2.10          |
13# |   192.0.2.1/28 |  |                   |  | 192.0.2.3/28    |
14# |                |  |                   |  |                 |
15# |            $h1 +  |                   |  + $h2             |
16# +----------------|--+                   +--|-----------------+
17#                  |                         |
18# +----------------|-------------------------|-----------------+
19# | SW       $swp1 +                         + $swp2           |
20# |                |                         |                 |
21# | +--------------|-------------------------|---------------+ |
22# | |     $swp1.10 +                         + $swp2.10      | |
23# | |                                                        | |
24# | |                           br0                          | |
25# | |                       192.0.2.2/28                     | |
26# | +--------------------------------------------------------+ |
27# |                                                            |
28# |      $swp3.20 +                                            |
29# | 192.0.2.17/28 |                                            |
30# |               |                                            |
31# |         $swp3 +                                            |
32# +---------------|--------------------------------------------+
33#                 |
34# +---------------|--+
35# |           $h3 +  |
36# |               |  |
37# |        $h3.20 +  |
38# | 192.0.2.18/28    |
39# |                  |
40# | H3               |
41# +------------------+
42
43lib_dir=$(dirname $0)/../../../net/forwarding
44
45ALL_TESTS="
46	port_vid_map_rif
47	rif_port_vid_map
48"
49
50NUM_NETIFS=6
51source $lib_dir/lib.sh
52source $lib_dir/tc_common.sh
53source $lib_dir/devlink_lib.sh
54
55h1_create()
56{
57	simple_if_init $h1
58	vlan_create $h1 10 v$h1 192.0.2.1/28
59
60	ip route add 192.0.2.16/28 vrf v$h1 nexthop via 192.0.2.2
61}
62
63h1_destroy()
64{
65	ip route del 192.0.2.16/28 vrf v$h1 nexthop via 192.0.2.2
66
67	vlan_destroy $h1 10
68	simple_if_fini $h1
69}
70
71h2_create()
72{
73	simple_if_init $h2
74	vlan_create $h2 10 v$h2 192.0.2.3/28
75}
76
77h2_destroy()
78{
79	vlan_destroy $h2 10
80	simple_if_fini $h2
81}
82
83h3_create()
84{
85	simple_if_init $h3
86	vlan_create $h3 20 v$h3 192.0.2.18/28
87
88	ip route add 192.0.2.0/28 vrf v$h3 nexthop via 192.0.2.17
89}
90
91h3_destroy()
92{
93	ip route del 192.0.2.0/28 vrf v$h3 nexthop via 192.0.2.17
94
95	vlan_destroy $h3 20
96	simple_if_fini $h3
97}
98
99switch_create()
100{
101	ip link set dev $swp1 up
102	tc qdisc add dev $swp1 clsact
103
104	ip link add dev br0 type bridge mcast_snooping 0
105
106	# By default, a link-local address is generated when netdevice becomes
107	# up. Adding an address to the bridge will cause creating a RIF for it.
108	# Prevent generating link-local address to be able to control when the
109	# RIF is added.
110	sysctl_set net.ipv6.conf.br0.addr_gen_mode 1
111	ip link set dev br0 up
112
113	ip link set dev $swp2 up
114	vlan_create $swp2 10
115	ip link set dev $swp2.10 master br0
116
117	ip link set dev $swp3 up
118	vlan_create $swp3 20 "" 192.0.2.17/28
119
120	# Replace neighbor to avoid 1 packet which is forwarded in software due
121	# to "unresolved neigh".
122	ip neigh replace dev $swp3.20 192.0.2.18 lladdr $(mac_get $h3.20)
123}
124
125switch_destroy()
126{
127	vlan_destroy $swp3 20
128	ip link set dev $swp3 down
129
130	ip link set dev $swp2.10 nomaster
131	vlan_destroy $swp2 10
132	ip link set dev $swp2 down
133
134	ip link set dev br0 down
135	sysctl_restore net.ipv6.conf.br0.addr_gen_mode
136	ip link del dev br0
137
138	tc qdisc del dev $swp1 clsact
139	ip link set dev $swp1 down
140}
141
142setup_prepare()
143{
144	h1=${NETIFS[p1]}
145	swp1=${NETIFS[p2]}
146
147	swp2=${NETIFS[p3]}
148	h2=${NETIFS[p4]}
149
150	swp3=${NETIFS[p5]}
151	h3=${NETIFS[p6]}
152
153	vrf_prepare
154	forwarding_enable
155
156	h1_create
157	h2_create
158	h3_create
159
160	switch_create
161}
162
163cleanup()
164{
165	pre_cleanup
166
167	switch_destroy
168
169	h3_destroy
170	h2_destroy
171	h1_destroy
172
173	forwarding_restore
174	vrf_cleanup
175}
176
177bridge_rif_add()
178{
179	rifs_occ_t0=$(devlink_resource_occ_get rifs)
180	__addr_add_del br0 add 192.0.2.2/28
181	rifs_occ_t1=$(devlink_resource_occ_get rifs)
182
183	expected_rifs=$((rifs_occ_t0 + 1))
184
185	[[ $expected_rifs -eq $rifs_occ_t1 ]]
186	check_err $? "Expected $expected_rifs RIFs, $rifs_occ_t1 are used"
187
188	sleep 1
189}
190
191bridge_rif_del()
192{
193	__addr_add_del br0 del 192.0.2.2/28
194}
195
196port_vid_map_rif()
197{
198	RET=0
199
200	# First add {port, VID}->FID for swp1.10, then add a RIF and verify that
201	# packets get the correct VID after routing.
202	vlan_create $swp1 10
203	ip link set dev $swp1.10 master br0
204	bridge_rif_add
205
206	# Replace neighbor to avoid 1 packet which is forwarded in software due
207	# to "unresolved neigh".
208	ip neigh replace dev br0 192.0.2.1 lladdr $(mac_get $h1.10)
209
210	# The hardware matches on the first ethertype which is not VLAN,
211	# so the protocol should be IP.
212	tc filter add dev $swp1 egress protocol ip pref 1 handle 101 \
213		flower skip_sw dst_ip 192.0.2.1 action pass
214
215	ping_do $h1.10 192.0.2.18
216	check_err $? "Ping failed"
217
218	tc_check_at_least_x_packets "dev $swp1 egress" 101 10
219	check_err $? "Packets were not routed in hardware"
220
221	log_test "Add RIF for existing {port, VID}->FID mapping"
222
223	tc filter del dev $swp1 egress
224
225	bridge_rif_del
226	ip link set dev $swp1.10 nomaster
227	vlan_destroy $swp1 10
228}
229
230rif_port_vid_map()
231{
232	RET=0
233
234	# First add an address to the bridge, which will create a RIF on top of
235	# it, then add a new {port, VID}->FID mapping and verify that packets
236	# get the correct VID after routing.
237	bridge_rif_add
238	vlan_create $swp1 10
239	ip link set dev $swp1.10 master br0
240
241	# Replace neighbor to avoid 1 packet which is forwarded in software due
242	# to "unresolved neigh".
243	ip neigh replace dev br0 192.0.2.1 lladdr $(mac_get $h1.10)
244
245	# The hardware matches on the first ethertype which is not VLAN,
246	# so the protocol should be IP.
247	tc filter add dev $swp1 egress protocol ip pref 1 handle 101 \
248		flower skip_sw dst_ip 192.0.2.1 action pass
249
250	ping_do $h1.10 192.0.2.18
251	check_err $? "Ping failed"
252
253	tc_check_at_least_x_packets "dev $swp1 egress" 101 10
254	check_err $? "Packets were not routed in hardware"
255
256	log_test "Add {port, VID}->FID mapping for FID with a RIF"
257
258	tc filter del dev $swp1 egress
259
260	ip link set dev $swp1.10 nomaster
261	vlan_destroy $swp1 10
262	bridge_rif_del
263}
264
265trap cleanup EXIT
266
267setup_prepare
268setup_wait
269
270tests_run
271
272exit $EXIT_STATUS
273