1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Test routing after VXLAN decapsulation and verify that the order of
5# configuration does not impact switch behavior. Verify that RIF is added
6# correctly for existing mapping and that new mapping uses the correct RIF.
7
8# +---------------------------+
9# |                        H1 |
10# |    + $h1                  |
11# |    | 192.0.2.1/28         |
12# +----|----------------------+
13#      |
14# +----|----------------------------------------------------------------------+
15# | SW |                                                                      |
16# | +--|--------------------------------------------------------------------+ |
17# | |  + $swp1                         br1                                  | |
18# | |     vid 10 pvid untagged                                              | |
19# | |                                                                       | |
20# | |                                                                       | |
21# | |                                            + vx4001                   | |
22# | |                                              local 192.0.2.17         | |
23# | |                                              remote 192.0.2.18        | |
24# | |                                              id 104001                | |
25# | |                                              dstport $VXPORT          | |
26# | |                                              vid 4001 pvid untagged   | |
27# | |                                                                       | |
28# | +----------------------------------+------------------------------------+ |
29# |                                    |                                      |
30# | +----------------------------------|------------------------------------+ |
31# | |                                  |                                    | |
32# | |  +-------------------------------+---------------------------------+  | |
33# | |  |                                                                 |  | |
34# | |  + vlan10                                                 vlan4001 +  | |
35# | |    192.0.2.2/28                                                       | |
36# | |                                                                       | |
37# | |                               vrf-green                               | |
38# | +-----------------------------------------------------------------------+ |
39# |                                                                           |
40# |    + $rp1                                       +lo                       |
41# |    | 198.51.100.1/24                             192.0.2.17/32            |
42# +----|----------------------------------------------------------------------+
43#      |
44# +----|--------------------------------------------------------+
45# |    |                                             v$rp2      |
46# |    + $rp2                                                   |
47# |      198.51.100.2/24                                        |
48# |                                                             |
49# +-------------------------------------------------------------+
50
51lib_dir=$(dirname $0)/../../../net/forwarding
52
53ALL_TESTS="
54	vni_fid_map_rif
55	rif_vni_fid_map
56"
57
58NUM_NETIFS=4
59source $lib_dir/lib.sh
60source $lib_dir/tc_common.sh
61source $lib_dir/devlink_lib.sh
62
63: ${VXPORT:=4789}
64export VXPORT
65
66h1_create()
67{
68	simple_if_init $h1 192.0.2.1/28
69}
70
71h1_destroy()
72{
73	simple_if_fini $h1 192.0.2.1/28
74}
75
76switch_create()
77{
78	ip link add name br1 type bridge vlan_filtering 1 vlan_default_pvid 0 \
79		mcast_snooping 0
80	# Make sure the bridge uses the MAC address of the local port and not
81	# that of the VxLAN's device.
82	ip link set dev br1 address $(mac_get $swp1)
83	ip link set dev br1 up
84
85	ip link set dev $rp1 up
86	ip address add dev $rp1 198.51.100.1/24
87
88	ip link set dev $swp1 master br1
89	ip link set dev $swp1 up
90	bridge vlan add vid 10 dev $swp1 pvid untagged
91
92	tc qdisc add dev $swp1 clsact
93
94	ip link add name vx4001 type vxlan id 104001 \
95		local 192.0.2.17 dstport $VXPORT \
96		nolearning noudpcsum tos inherit ttl 100
97	ip link set dev vx4001 up
98
99	ip link set dev vx4001 master br1
100
101	ip address add 192.0.2.17/32 dev lo
102
103	# Create SVIs.
104	vrf_create "vrf-green"
105	ip link set dev vrf-green up
106
107	ip link add link br1 name vlan10 up master vrf-green type vlan id 10
108
109	# Replace neighbor to avoid 1 packet which is forwarded in software due
110	# to "unresolved neigh".
111	ip neigh replace dev vlan10 192.0.2.1 lladdr $(mac_get $h1)
112
113	ip address add 192.0.2.2/28 dev vlan10
114
115	bridge vlan add vid 10 dev br1 self
116	bridge vlan add vid 4001 dev br1 self
117
118	sysctl_set net.ipv4.conf.all.rp_filter 0
119}
120
121switch_destroy()
122{
123	sysctl_restore net.ipv4.conf.all.rp_filter
124
125	bridge vlan del vid 4001 dev br1 self
126	bridge vlan del vid 10 dev br1 self
127
128	ip link del dev vlan10
129
130	vrf_destroy "vrf-green"
131
132	ip address del 192.0.2.17/32 dev lo
133
134	tc qdisc del dev $swp1 clsact
135
136	bridge vlan del vid 10 dev $swp1
137	ip link set dev $swp1 down
138	ip link set dev $swp1 nomaster
139
140	ip link set dev vx4001 nomaster
141
142	ip link set dev vx4001 down
143	ip link del dev vx4001
144
145	ip address del dev $rp1 198.51.100.1/24
146	ip link set dev $rp1 down
147
148	ip link set dev br1 down
149	ip link del dev br1
150}
151
152vrp2_create()
153{
154	simple_if_init $rp2 198.51.100.2/24
155
156	ip route add 192.0.2.17/32 vrf v$rp2 nexthop via 198.51.100.1
157}
158
159vrp2_destroy()
160{
161	ip route del 192.0.2.17/32 vrf v$rp2 nexthop via 198.51.100.1
162
163	simple_if_fini $rp2 198.51.100.2/24
164}
165
166setup_prepare()
167{
168	h1=${NETIFS[p1]}
169	swp1=${NETIFS[p2]}
170
171	rp1=${NETIFS[p3]}
172	rp2=${NETIFS[p4]}
173
174	vrf_prepare
175	forwarding_enable
176
177	h1_create
178	switch_create
179
180	vrp2_create
181}
182
183cleanup()
184{
185	pre_cleanup
186
187	vrp2_destroy
188
189	switch_destroy
190	h1_destroy
191
192	forwarding_restore
193	vrf_cleanup
194}
195
196payload_get()
197{
198	local dest_mac=$(mac_get vlan4001)
199	local src_mac=$(mac_get $rp1)
200
201	p=$(:
202		)"08:"$(                      : VXLAN flags
203		)"00:00:00:"$(                : VXLAN reserved
204		)"01:96:41:"$(                : VXLAN VNI : 104001
205		)"00:"$(                      : VXLAN reserved
206		)"$dest_mac:"$(               : ETH daddr
207		)"$src_mac:"$(                : ETH saddr
208		)"08:00:"$(                   : ETH type
209		)"45:"$(                      : IP version + IHL
210		)"00:"$(                      : IP TOS
211		)"00:54:"$(                   : IP total length
212		)"3f:49:"$(                   : IP identification
213		)"00:00:"$(                   : IP flags + frag off
214		)"3f:"$(                      : IP TTL
215		)"01:"$(                      : IP proto
216		)"50:21:"$(                   : IP header csum
217		)"c6:33:64:0a:"$(             : IP saddr: 198.51.100.10
218		)"c0:00:02:01:"$(             : IP daddr: 192.0.2.1
219	)
220	echo $p
221}
222
223vlan_rif_add()
224{
225	rifs_occ_t0=$(devlink_resource_occ_get rifs)
226
227	ip link add link br1 name vlan4001 up master vrf-green \
228		type vlan id 4001
229
230	rifs_occ_t1=$(devlink_resource_occ_get rifs)
231	expected_rifs=$((rifs_occ_t0 + 1))
232
233	[[ $expected_rifs -eq $rifs_occ_t1 ]]
234	check_err $? "Expected $expected_rifs RIFs, $rifs_occ_t1 are used"
235}
236
237vlan_rif_del()
238{
239	ip link del dev vlan4001
240}
241
242vni_fid_map_rif()
243{
244	local rp1_mac=$(mac_get $rp1)
245
246	RET=0
247
248	# First add VNI->FID mapping to the FID of VLAN 4001
249	bridge vlan add vid 4001 dev vx4001 pvid untagged
250
251	# Add a RIF to the FID with VNI->FID mapping
252	vlan_rif_add
253
254	tc filter add dev $swp1 egress protocol ip pref 1 handle 101 \
255		flower skip_sw dst_ip 192.0.2.1 action pass
256
257	payload=$(payload_get)
258	ip vrf exec v$rp2 $MZ $rp2 -c 10 -d 1msec -b $rp1_mac \
259		-B 192.0.2.17 -A 192.0.2.18 \
260		-t udp sp=12345,dp=$VXPORT,p=$payload -q
261
262	tc_check_at_least_x_packets "dev $swp1 egress" 101 10
263	check_err $? "Packets were not routed in hardware"
264
265	log_test "Add RIF for existing VNI->FID mapping"
266
267	tc filter del dev $swp1 egress
268
269	bridge vlan del vid 4001 dev vx4001 pvid untagged
270	vlan_rif_del
271}
272
273rif_vni_fid_map()
274{
275	local rp1_mac=$(mac_get $rp1)
276
277	RET=0
278
279	# First add a RIF to the FID of VLAN 4001
280	vlan_rif_add
281
282	# Add VNI->FID mapping to FID with a RIF
283	bridge vlan add vid 4001 dev vx4001 pvid untagged
284
285	tc filter add dev $swp1 egress protocol ip pref 1 handle 101 \
286		flower skip_sw dst_ip 192.0.2.1 action pass
287
288	payload=$(payload_get)
289	ip vrf exec v$rp2 $MZ $rp2 -c 10 -d 1msec -b $rp1_mac \
290		-B 192.0.2.17 -A 192.0.2.18 \
291		-t udp sp=12345,dp=$VXPORT,p=$payload -q
292
293	tc_check_at_least_x_packets "dev $swp1 egress" 101 10
294	check_err $? "Packets were not routed in hardware"
295
296	log_test "Add VNI->FID mapping for FID with a RIF"
297
298	tc filter del dev $swp1 egress
299
300	bridge vlan del vid 4001 dev vx4001 pvid untagged
301	vlan_rif_del
302}
303
304trap cleanup EXIT
305
306setup_prepare
307setup_wait
308
309tests_run
310
311exit $EXIT_STATUS
312