1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# +--------------------+                               +----------------------+
5# | H1 (vrf)           |                               |             H2 (vrf) |
6# |    + h1.10         |                               |  + h2.20             |
7# |    | 192.0.2.1/28  |                               |  | 192.0.2.2/28      |
8# |    |               |                               |  |                   |
9# |    + $h1           |                               |  + $h2               |
10# |    |               |                               |  |                   |
11# +----|---------------+                               +--|-------------------+
12#      |                                                  |
13# +----|--------------------------------------------------|--------------------+
14# | SW |                                                  |                    |
15# | +--|-------------------------------+ +----------------|------------------+ |
16# | |  + $swp1         BR1 (802.1ad)   | | BR2 (802.1d)   + $swp2            | |
17# | |    vid 100 pvid untagged         | |                |                  | |
18# | |                                  | |                + $swp2.20         | |
19# | |                                  | |                                   | |
20# | |  + vx100 (vxlan)                 | |  + vx200 (vxlan)                  | |
21# | |    local 192.0.2.17              | |    local 192.0.2.17               | |
22# | |    remote 192.0.2.34             | |    remote 192.0.2.50              | |
23# | |    id 1000 dstport $VXPORT       | |    id 2000 dstport $VXPORT        | |
24# | |    vid 100 pvid untagged         | |                                   | |
25# | +--------------------------------- + +-----------------------------------+ |
26# |                                                                            |
27# |  192.0.2.32/28 via 192.0.2.18                                              |
28# |  192.0.2.48/28 via 192.0.2.18                                              |
29# |                                                                            |
30# |    + $rp1                                                                  |
31# |    | 192.0.2.17/28                                                         |
32# +----|-----------------------------------------------------------------------+
33#      |
34# +----|--------------------------------------------------------+
35# |    |                                             VRP2 (vrf) |
36# |    + $rp2                                                   |
37# |      192.0.2.18/28                                          |
38# |                                                             |   (maybe) HW
39# =============================================================================
40# |                                                             |  (likely) SW
41# |    + v1 (veth)                             + v3 (veth)      |
42# |    | 192.0.2.33/28                         | 192.0.2.49/28  |
43# +----|---------------------------------------|----------------+
44#      |                                       |
45# +----|------------------------------+   +----|------------------------------+
46# |    + v2 (veth)        NS1 (netns) |   |    + v4 (veth)        NS2 (netns) |
47# |      192.0.2.34/28                |   |      192.0.2.50/28                |
48# |                                   |   |                                   |
49# |   192.0.2.16/28 via 192.0.2.33    |   |   192.0.2.16/28 via 192.0.2.49    |
50# |   192.0.2.50/32 via 192.0.2.33    |   |   192.0.2.34/32 via 192.0.2.49    |
51# |                                   |   |                                   |
52# | +-------------------------------+ |   | +-------------------------------+ |
53# | |                 BR3 (802.1ad) | |   | |                  BR3 (802.1d) | |
54# | |  + vx100 (vxlan)              | |   | |  + vx200 (vxlan)              | |
55# | |    local 192.0.2.34           | |   | |    local 192.0.2.50           | |
56# | |    remote 192.0.2.17          | |   | |    remote 192.0.2.17          | |
57# | |    remote 192.0.2.50          | |   | |    remote 192.0.2.34          | |
58# | |    id 1000 dstport $VXPORT    | |   | |    id 2000 dstport $VXPORT    | |
59# | |    vid 100 pvid untagged      | |   | |                               | |
60# | |                               | |   | |  + w1.20                      | |
61# | |                               | |   | |  |                            | |
62# | |  + w1 (veth)                  | |   | |  + w1 (veth)                  | |
63# | |  | vid 100 pvid untagged      | |   | |  |                            | |
64# | +--|----------------------------+ |   | +--|----------------------------+ |
65# |    |                              |   |    |                              |
66# | +--|----------------------------+ |   | +--|----------------------------+ |
67# | |  |                  VW2 (vrf) | |   | |  |                  VW2 (vrf) | |
68# | |  + w2 (veth)                  | |   | |  + w2 (veth)                  | |
69# | |  |                            | |   | |  |                            | |
70# | |  |                            | |   | |  |                            | |
71# | |  + w2.10                      | |   | |  + w2.20                      | |
72# | |    192.0.2.3/28               | |   | |    192.0.2.4/28               | |
73# | +-------------------------------+ |   | +-------------------------------+ |
74# +-----------------------------------+   +-----------------------------------+
75
76: ${VXPORT:=4789}
77export VXPORT
78
79: ${ALL_TESTS:="
80	ping_ipv4
81    "}
82
83NUM_NETIFS=6
84source lib.sh
85
86h1_create()
87{
88	simple_if_init $h1
89	tc qdisc add dev $h1 clsact
90	vlan_create $h1 10 v$h1 192.0.2.1/28
91}
92
93h1_destroy()
94{
95	vlan_destroy $h1 10
96	tc qdisc del dev $h1 clsact
97	simple_if_fini $h1
98}
99
100h2_create()
101{
102	simple_if_init $h2
103	tc qdisc add dev $h2 clsact
104	vlan_create $h2 20 v$h2 192.0.2.2/28
105}
106
107h2_destroy()
108{
109	vlan_destroy $h2 20
110	tc qdisc del dev $h2 clsact
111	simple_if_fini $h2
112}
113
114rp1_set_addr()
115{
116	ip address add dev $rp1 192.0.2.17/28
117
118	ip route add 192.0.2.32/28 nexthop via 192.0.2.18
119	ip route add 192.0.2.48/28 nexthop via 192.0.2.18
120}
121
122rp1_unset_addr()
123{
124	ip route del 192.0.2.48/28 nexthop via 192.0.2.18
125	ip route del 192.0.2.32/28 nexthop via 192.0.2.18
126
127	ip address del dev $rp1 192.0.2.17/28
128}
129
130switch_create()
131{
132	#### BR1 ####
133	ip link add name br1 type bridge vlan_filtering 1 \
134		vlan_protocol 802.1ad vlan_default_pvid 0 mcast_snooping 0
135	# Make sure the bridge uses the MAC address of the local port and not
136	# that of the VxLAN's device.
137	ip link set dev br1 address $(mac_get $swp1)
138	ip link set dev br1 up
139
140	#### BR2 ####
141	ip link add name br2 type bridge vlan_filtering 0 mcast_snooping 0
142	# Make sure the bridge uses the MAC address of the local port and not
143	# that of the VxLAN's device.
144	ip link set dev br2 address $(mac_get $swp2)
145	ip link set dev br2 up
146
147	ip link set dev $rp1 up
148	rp1_set_addr
149
150	#### VX100 ####
151	ip link add name vx100 type vxlan id 1000 local 192.0.2.17 \
152		dstport "$VXPORT" nolearning noudpcsum tos inherit ttl 100
153	ip link set dev vx100 up
154
155	ip link set dev vx100 master br1
156	bridge vlan add vid 100 dev vx100 pvid untagged
157
158	ip link set dev $swp1 master br1
159	ip link set dev $swp1 up
160	bridge vlan add vid 100 dev $swp1 pvid untagged
161
162	#### VX200 ####
163	ip link add name vx200 type vxlan id 2000 local 192.0.2.17 \
164		dstport "$VXPORT" nolearning noudpcsum tos inherit ttl 100
165	ip link set dev vx200 up
166
167	ip link set dev vx200 master br2
168
169	ip link set dev $swp2 up
170	ip link add name $swp2.20 link $swp2 type vlan id 20
171	ip link set dev $swp2.20 master br2
172	ip link set dev $swp2.20 up
173
174	bridge fdb append dev vx100 00:00:00:00:00:00 dst 192.0.2.34 self
175	bridge fdb append dev vx200 00:00:00:00:00:00 dst 192.0.2.50 self
176}
177
178switch_destroy()
179{
180	bridge fdb del dev vx200 00:00:00:00:00:00 dst 192.0.2.50 self
181	bridge fdb del dev vx100 00:00:00:00:00:00 dst 192.0.2.34 self
182
183	ip link set dev vx200 nomaster
184	ip link set dev vx200 down
185	ip link del dev vx200
186
187	ip link del dev $swp2.20
188	ip link set dev $swp2 down
189	ip link set dev $swp2 nomaster
190
191	bridge vlan del vid 100 dev $swp1
192	ip link set dev $swp1 down
193	ip link set dev $swp1 nomaster
194
195	ip link set dev vx100 nomaster
196	ip link set dev vx100 down
197	ip link del dev vx100
198
199	rp1_unset_addr
200	ip link set dev $rp1 down
201
202	ip link set dev br2 down
203	ip link del dev br2
204
205	ip link set dev br1 down
206	ip link del dev br1
207}
208
209vrp2_create()
210{
211	simple_if_init $rp2 192.0.2.18/28
212	__simple_if_init v1 v$rp2 192.0.2.33/28
213	__simple_if_init v3 v$rp2 192.0.2.49/28
214	tc qdisc add dev v1 clsact
215}
216
217vrp2_destroy()
218{
219	tc qdisc del dev v1 clsact
220	__simple_if_fini v3 192.0.2.49/28
221	__simple_if_fini v1 192.0.2.33/28
222	simple_if_fini $rp2 192.0.2.18/28
223}
224
225ns_init_common()
226{
227	local in_if=$1; shift
228	local in_addr=$1; shift
229	local other_in_addr=$1; shift
230	local vxlan_name=$1; shift
231	local vxlan_id=$1; shift
232	local vlan_id=$1; shift
233	local host_addr=$1; shift
234	local nh_addr=$1; shift
235
236	ip link set dev $in_if up
237	ip address add dev $in_if $in_addr/28
238	tc qdisc add dev $in_if clsact
239
240	ip link add name br3 type bridge vlan_filtering 0
241	ip link set dev br3 up
242
243	ip link add name w1 type veth peer name w2
244
245	ip link set dev w1 master br3
246	ip link set dev w1 up
247
248	ip link add name $vxlan_name type vxlan id $vxlan_id local $in_addr \
249		dstport "$VXPORT"
250	ip link set dev $vxlan_name up
251	bridge fdb append dev $vxlan_name 00:00:00:00:00:00 dst 192.0.2.17 self
252	bridge fdb append dev $vxlan_name 00:00:00:00:00:00 dst $other_in_addr self
253
254	ip link set dev $vxlan_name master br3
255	tc qdisc add dev $vxlan_name clsact
256
257	simple_if_init w2
258	vlan_create w2 $vlan_id vw2 $host_addr/28
259
260	ip route add 192.0.2.16/28 nexthop via $nh_addr
261	ip route add $other_in_addr/32 nexthop via $nh_addr
262}
263export -f ns_init_common
264
265ns1_create()
266{
267	ip netns add ns1
268	ip link set dev v2 netns ns1
269	in_ns ns1 \
270	      ns_init_common v2 192.0.2.34 192.0.2.50 vx100 1000 10 192.0.2.3 \
271	      192.0.2.33
272
273	in_ns ns1 bridge vlan add vid 100 dev vx100 pvid untagged
274}
275
276ns1_destroy()
277{
278	ip netns exec ns1 ip link set dev v2 netns 1
279	ip netns del ns1
280}
281
282ns2_create()
283{
284	ip netns add ns2
285	ip link set dev v4 netns ns2
286	in_ns ns2 \
287	      ns_init_common v4 192.0.2.50 192.0.2.34 vx200 2000 20 192.0.2.4 \
288	      192.0.2.49
289
290	in_ns ns2 ip link add name w1.20 link w1 type vlan id 20
291	in_ns ns2 ip link set dev w1.20 master br3
292	in_ns ns2 ip link set dev w1.20 up
293}
294
295ns2_destroy()
296{
297	ip netns exec ns2 ip link set dev v4 netns 1
298	ip netns del ns2
299}
300
301setup_prepare()
302{
303	h1=${NETIFS[p1]}
304	swp1=${NETIFS[p2]}
305
306	swp2=${NETIFS[p3]}
307	h2=${NETIFS[p4]}
308
309	rp1=${NETIFS[p5]}
310	rp2=${NETIFS[p6]}
311
312	vrf_prepare
313	forwarding_enable
314
315	h1_create
316	h2_create
317	switch_create
318
319	ip link add name v1 type veth peer name v2
320	ip link add name v3 type veth peer name v4
321	vrp2_create
322	ns1_create
323	ns2_create
324
325	r1_mac=$(in_ns ns1 mac_get w2)
326	r2_mac=$(in_ns ns2 mac_get w2)
327	h2_mac=$(mac_get $h2)
328}
329
330cleanup()
331{
332	pre_cleanup
333
334	ns2_destroy
335	ns1_destroy
336	vrp2_destroy
337	ip link del dev v3
338	ip link del dev v1
339
340	switch_destroy
341	h2_destroy
342	h1_destroy
343
344	forwarding_restore
345	vrf_cleanup
346}
347
348ping_ipv4()
349{
350	ping_test $h1 192.0.2.3 ": local->remote 1 through VxLAN with an 802.1ad bridge"
351	ping_test $h2 192.0.2.4 ": local->remote 2 through VxLAN with an 802.1d bridge"
352}
353
354test_all()
355{
356	echo "Running tests with UDP port $VXPORT"
357	tests_run
358}
359
360trap cleanup EXIT
361
362setup_prepare
363setup_wait
364test_all
365
366exit $EXIT_STATUS
367