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