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