1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# +---------------------------+                +------------------------------+
5# |                    vrf-h1 |                |                       vrf-h2 |
6# |    + $h1                  |                |    + $h2                     |
7# |    | 10.1.1.101/24        |                |    | 10.1.2.101/24           |
8# |    | default via 10.1.1.1 |                |    | default via 10.1.2.1    |
9# +----|----------------------+                +----|-------------------------+
10#      |                                            |
11# +----|--------------------------------------------|-------------------------+
12# | SW |                                            |                         |
13# | +--|--------------------------------------------|-----------------------+ |
14# | |  + $swp1                         br1          + $swp2                 | |
15# | |     vid 10 pvid untagged                         vid 20 pvid untagged | |
16# | |                                                                       | |
17# | |  + vx10                                       + vx20                  | |
18# | |    local 10.0.0.1                               local 10.0.0.1        | |
19# | |    remote 10.0.0.2                              remote 10.0.0.2       | |
20# | |    id 1010                                      id 1020               | |
21# | |    dstport 4789                                 dstport 4789          | |
22# | |    vid 10 pvid untagged                         vid 20 pvid untagged  | |
23# | |                                                                       | |
24# | |                             + vx4001                                  | |
25# | |                               local 10.0.0.1                          | |
26# | |                               remote 10.0.0.2                         | |
27# | |                               id 104001                               | |
28# | |                               dstport 4789                            | |
29# | |                               vid 4001 pvid untagged                  | |
30# | |                                                                       | |
31# | +-----------------------------------+-----------------------------------+ |
32# |                                     |                                     |
33# | +-----------------------------------|-----------------------------------+ |
34# | |                                   |                                   | |
35# | |  +--------------------------------+--------------------------------+  | |
36# | |  |                                |                                |  | |
37# | |  + vlan10                         |                         vlan20 +  | |
38# | |  | 10.1.1.11/24                   |                   10.1.2.11/24 |  | |
39# | |  |                                |                                |  | |
40# | |  + vlan10-v (macvlan)             +             vlan20-v (macvlan) +  | |
41# | |    10.1.1.1/24                vlan4001                 10.1.2.1/24    | |
42# | |    00:00:5e:00:01:01                             00:00:5e:00:01:01    | |
43# | |                               vrf-green                               | |
44# | +-----------------------------------------------------------------------+ |
45# |                                                                           |
46# |    + $rp1                                       +lo                       |
47# |    | 192.0.2.1/24                                10.0.0.1/32              |
48# +----|----------------------------------------------------------------------+
49#      |
50# +----|--------------------------------------------------------+
51# |    |                            vrf-spine                   |
52# |    + $rp2                                                   |
53# |      192.0.2.2/24                                           |
54# |                                                             |   (maybe) HW
55# =============================================================================
56# |                                                             |  (likely) SW
57# |                                                             |
58# |    + v1 (veth)                                              |
59# |    | 192.0.3.2/24                                           |
60# +----|--------------------------------------------------------+
61#      |
62# +----|----------------------------------------------------------------------+
63# |    + v2 (veth)                                  +lo           NS1 (netns) |
64# |      192.0.3.1/24                                10.0.0.2/32              |
65# |                                                                           |
66# | +-----------------------------------------------------------------------+ |
67# | |                               vrf-green                               | |
68# | |  + vlan10-v (macvlan)                           vlan20-v (macvlan) +  | |
69# | |  | 10.1.1.1/24                                         10.1.2.1/24 |  | |
70# | |  | 00:00:5e:00:01:01                             00:00:5e:00:01:01 |  | |
71# | |  |                            vlan4001                             |  | |
72# | |  + vlan10                         +                         vlan20 +  | |
73# | |  | 10.1.1.12/24                   |                   10.1.2.12/24 |  | |
74# | |  |                                |                                |  | |
75# | |  +--------------------------------+--------------------------------+  | |
76# | |                                   |                                   | |
77# | +-----------------------------------|-----------------------------------+ |
78# |                                     |                                     |
79# | +-----------------------------------+-----------------------------------+ |
80# | |                                                                       | |
81# | |  + vx10                                     + vx20                    | |
82# | |    local 10.0.0.2                             local 10.0.0.2          | |
83# | |    remote 10.0.0.1                            remote 10.0.0.1         | |
84# | |    id 1010                                    id 1020                 | |
85# | |    dstport 4789                               dstport 4789            | |
86# | |    vid 10 pvid untagged                       vid 20 pvid untagged    | |
87# | |                                                                       | |
88# | |                             + vx4001                                  | |
89# | |                               local 10.0.0.2                          | |
90# | |                               remote 10.0.0.1                         | |
91# | |                               id 104001                               | |
92# | |                               dstport 4789                            | |
93# | |                               vid 4001 pvid untagged                  | |
94# | |                                                                       | |
95# | |  + w1 (veth)                                + w3 (veth)               | |
96# | |  | vid 10 pvid untagged          br1        | vid 20 pvid untagged    | |
97# | +--|------------------------------------------|-------------------------+ |
98# |    |                                          |                           |
99# |    |                                          |                           |
100# | +--|----------------------+                +--|-------------------------+ |
101# | |  |               vrf-h1 |                |  |                  vrf-h2 | |
102# | |  + w2 (veth)            |                |  + w4 (veth)               | |
103# | |    10.1.1.102/24        |                |    10.1.2.102/24           | |
104# | |    default via 10.1.1.1 |                |    default via 10.1.2.1    | |
105# | +-------------------------+                +----------------------------+ |
106# +---------------------------------------------------------------------------+
107
108ALL_TESTS="
109	ping_ipv4
110"
111NUM_NETIFS=6
112source lib.sh
113
114hx_create()
115{
116	local vrf_name=$1; shift
117	local if_name=$1; shift
118	local ip_addr=$1; shift
119	local gw_ip=$1; shift
120
121	vrf_create $vrf_name
122	ip link set dev $if_name master $vrf_name
123	ip link set dev $vrf_name up
124	ip link set dev $if_name up
125
126	ip address add $ip_addr/24 dev $if_name
127	ip neigh replace $gw_ip lladdr 00:00:5e:00:01:01 nud permanent \
128		dev $if_name
129	ip route add default vrf $vrf_name nexthop via $gw_ip
130}
131export -f hx_create
132
133hx_destroy()
134{
135	local vrf_name=$1; shift
136	local if_name=$1; shift
137	local ip_addr=$1; shift
138	local gw_ip=$1; shift
139
140	ip route del default vrf $vrf_name nexthop via $gw_ip
141	ip neigh del $gw_ip dev $if_name
142	ip address del $ip_addr/24 dev $if_name
143
144	ip link set dev $if_name down
145	vrf_destroy $vrf_name
146}
147
148h1_create()
149{
150	hx_create "vrf-h1" $h1 10.1.1.101 10.1.1.1
151}
152
153h1_destroy()
154{
155	hx_destroy "vrf-h1" $h1 10.1.1.101 10.1.1.1
156}
157
158h2_create()
159{
160	hx_create "vrf-h2" $h2 10.1.2.101 10.1.2.1
161}
162
163h2_destroy()
164{
165	hx_destroy "vrf-h2" $h2 10.1.2.101 10.1.2.1
166}
167
168switch_create()
169{
170	ip link add name br1 type bridge vlan_filtering 1 vlan_default_pvid 0 \
171		mcast_snooping 0
172	# Make sure the bridge uses the MAC address of the local port and not
173	# that of the VxLAN's device.
174	ip link set dev br1 address $(mac_get $swp1)
175	ip link set dev br1 up
176
177	ip link set dev $rp1 up
178	ip address add dev $rp1 192.0.2.1/24
179	ip route add 10.0.0.2/32 nexthop via 192.0.2.2
180
181	ip link add name vx10 type vxlan id 1010		\
182		local 10.0.0.1 remote 10.0.0.2 dstport 4789	\
183		nolearning noudpcsum tos inherit ttl 100
184	ip link set dev vx10 up
185
186	ip link set dev vx10 master br1
187	bridge vlan add vid 10 dev vx10 pvid untagged
188
189	ip link add name vx20 type vxlan id 1020		\
190		local 10.0.0.1 remote 10.0.0.2 dstport 4789	\
191		nolearning noudpcsum tos inherit ttl 100
192	ip link set dev vx20 up
193
194	ip link set dev vx20 master br1
195	bridge vlan add vid 20 dev vx20 pvid untagged
196
197	ip link set dev $swp1 master br1
198	ip link set dev $swp1 up
199	bridge vlan add vid 10 dev $swp1 pvid untagged
200
201	ip link set dev $swp2 master br1
202	ip link set dev $swp2 up
203	bridge vlan add vid 20 dev $swp2 pvid untagged
204
205	ip link add name vx4001 type vxlan id 104001		\
206		local 10.0.0.1 dstport 4789			\
207		nolearning noudpcsum tos inherit ttl 100
208	ip link set dev vx4001 up
209
210	ip link set dev vx4001 master br1
211	bridge vlan add vid 4001 dev vx4001 pvid untagged
212
213	ip address add 10.0.0.1/32 dev lo
214
215	# Create SVIs
216	vrf_create "vrf-green"
217	ip link set dev vrf-green up
218
219	ip link add link br1 name vlan10 up master vrf-green type vlan id 10
220	ip address add 10.1.1.11/24 dev vlan10
221	ip link add link vlan10 name vlan10-v up master vrf-green \
222		address 00:00:5e:00:01:01 type macvlan mode private
223	ip address add 10.1.1.1/24 dev vlan10-v
224
225	ip link add link br1 name vlan20 up master vrf-green type vlan id 20
226	ip address add 10.1.2.11/24 dev vlan20
227	ip link add link vlan20 name vlan20-v up master vrf-green \
228		address 00:00:5e:00:01:01 type macvlan mode private
229	ip address add 10.1.2.1/24 dev vlan20-v
230
231	ip link add link br1 name vlan4001 up master vrf-green \
232		type vlan id 4001
233
234	bridge vlan add vid 10 dev br1 self
235	bridge vlan add vid 20 dev br1 self
236	bridge vlan add vid 4001 dev br1 self
237
238	bridge fdb add 00:00:5e:00:01:01 dev br1 self local vlan 10
239	bridge fdb add 00:00:5e:00:01:01 dev br1 self local vlan 20
240}
241
242switch_destroy()
243{
244	bridge fdb del 00:00:5e:00:01:01 dev br1 self local vlan 20
245	bridge fdb del 00:00:5e:00:01:01 dev br1 self local vlan 10
246
247	bridge vlan del vid 4001 dev br1 self
248	bridge vlan del vid 20 dev br1 self
249	bridge vlan del vid 10 dev br1 self
250
251	ip link del dev vlan4001
252
253	ip link del dev vlan20
254
255	ip link del dev vlan10
256
257	vrf_destroy "vrf-green"
258
259	ip address del 10.0.0.1/32 dev lo
260
261	bridge vlan del vid 20 dev $swp2
262	ip link set dev $swp2 down
263	ip link set dev $swp2 nomaster
264
265	bridge vlan del vid 10 dev $swp1
266	ip link set dev $swp1 down
267	ip link set dev $swp1 nomaster
268
269	bridge vlan del vid 4001 dev vx4001
270	ip link set dev vx4001 nomaster
271
272	ip link set dev vx4001 down
273	ip link del dev vx4001
274
275	bridge vlan del vid 20 dev vx20
276	ip link set dev vx20 nomaster
277
278	ip link set dev vx20 down
279	ip link del dev vx20
280
281	bridge vlan del vid 10 dev vx10
282	ip link set dev vx10 nomaster
283
284	ip link set dev vx10 down
285	ip link del dev vx10
286
287	ip route del 10.0.0.2/32 nexthop via 192.0.2.2
288	ip address del dev $rp1 192.0.2.1/24
289	ip link set dev $rp1 down
290
291	ip link set dev br1 down
292	ip link del dev br1
293}
294
295spine_create()
296{
297	vrf_create "vrf-spine"
298	ip link set dev $rp2 master vrf-spine
299	ip link set dev v1 master vrf-spine
300	ip link set dev vrf-spine up
301	ip link set dev $rp2 up
302	ip link set dev v1 up
303
304	ip address add 192.0.2.2/24 dev $rp2
305	ip address add 192.0.3.2/24 dev v1
306
307	ip route add 10.0.0.1/32 vrf vrf-spine nexthop via 192.0.2.1
308	ip route add 10.0.0.2/32 vrf vrf-spine nexthop via 192.0.3.1
309}
310
311spine_destroy()
312{
313	ip route del 10.0.0.2/32 vrf vrf-spine nexthop via 192.0.3.1
314	ip route del 10.0.0.1/32 vrf vrf-spine nexthop via 192.0.2.1
315
316	ip address del 192.0.3.2/24 dev v1
317	ip address del 192.0.2.2/24 dev $rp2
318
319	ip link set dev v1 down
320	ip link set dev $rp2 down
321	vrf_destroy "vrf-spine"
322}
323
324ns_h1_create()
325{
326	hx_create "vrf-h1" w2 10.1.1.102 10.1.1.1
327}
328export -f ns_h1_create
329
330ns_h2_create()
331{
332	hx_create "vrf-h2" w4 10.1.2.102 10.1.2.1
333}
334export -f ns_h2_create
335
336ns_switch_create()
337{
338	ip link add name br1 type bridge vlan_filtering 1 vlan_default_pvid 0 \
339		mcast_snooping 0
340	ip link set dev br1 up
341
342	ip link set dev v2 up
343	ip address add dev v2 192.0.3.1/24
344	ip route add 10.0.0.1/32 nexthop via 192.0.3.2
345
346	ip link add name vx10 type vxlan id 1010		\
347		local 10.0.0.2 remote 10.0.0.1 dstport 4789	\
348		nolearning noudpcsum tos inherit ttl 100
349	ip link set dev vx10 up
350
351	ip link set dev vx10 master br1
352	bridge vlan add vid 10 dev vx10 pvid untagged
353
354	ip link add name vx20 type vxlan id 1020		\
355		local 10.0.0.2 remote 10.0.0.1 dstport 4789	\
356		nolearning noudpcsum tos inherit ttl 100
357	ip link set dev vx20 up
358
359	ip link set dev vx20 master br1
360	bridge vlan add vid 20 dev vx20 pvid untagged
361
362	ip link add name vx4001 type vxlan id 104001		\
363		local 10.0.0.2 dstport 4789			\
364		nolearning noudpcsum tos inherit ttl 100
365	ip link set dev vx4001 up
366
367	ip link set dev vx4001 master br1
368	bridge vlan add vid 4001 dev vx4001 pvid untagged
369
370	ip link set dev w1 master br1
371	ip link set dev w1 up
372	bridge vlan add vid 10 dev w1 pvid untagged
373
374	ip link set dev w3 master br1
375	ip link set dev w3 up
376	bridge vlan add vid 20 dev w3 pvid untagged
377
378	ip address add 10.0.0.2/32 dev lo
379
380	# Create SVIs
381	vrf_create "vrf-green"
382	ip link set dev vrf-green up
383
384	ip link add link br1 name vlan10 up master vrf-green type vlan id 10
385	ip address add 10.1.1.12/24 dev vlan10
386	ip link add link vlan10 name vlan10-v up master vrf-green \
387		address 00:00:5e:00:01:01 type macvlan mode private
388	ip address add 10.1.1.1/24 dev vlan10-v
389
390	ip link add link br1 name vlan20 up master vrf-green type vlan id 20
391	ip address add 10.1.2.12/24 dev vlan20
392	ip link add link vlan20 name vlan20-v up master vrf-green \
393		address 00:00:5e:00:01:01 type macvlan mode private
394	ip address add 10.1.2.1/24 dev vlan20-v
395
396	ip link add link br1 name vlan4001 up master vrf-green \
397		type vlan id 4001
398
399	bridge vlan add vid 10 dev br1 self
400	bridge vlan add vid 20 dev br1 self
401	bridge vlan add vid 4001 dev br1 self
402
403	bridge fdb add 00:00:5e:00:01:01 dev br1 self local vlan 10
404	bridge fdb add 00:00:5e:00:01:01 dev br1 self local vlan 20
405}
406export -f ns_switch_create
407
408ns_init()
409{
410	ip link add name w1 type veth peer name w2
411	ip link add name w3 type veth peer name w4
412
413	ip link set dev lo up
414
415	ns_h1_create
416	ns_h2_create
417	ns_switch_create
418}
419export -f ns_init
420
421ns1_create()
422{
423	ip netns add ns1
424	ip link set dev v2 netns ns1
425	in_ns ns1 ns_init
426}
427
428ns1_destroy()
429{
430	ip netns exec ns1 ip link set dev v2 netns 1
431	ip netns del ns1
432}
433
434__l2_vni_init()
435{
436	local mac1=$1; shift
437	local mac2=$1; shift
438	local ip1=$1; shift
439	local ip2=$1; shift
440	local dst=$1; shift
441
442	bridge fdb add $mac1 dev vx10 self master extern_learn static \
443		dst $dst vlan 10
444	bridge fdb add $mac2 dev vx20 self master extern_learn static \
445		dst $dst vlan 20
446
447	ip neigh add $ip1 lladdr $mac1 nud noarp dev vlan10 \
448		extern_learn
449	ip neigh add $ip2 lladdr $mac2 nud noarp dev vlan20 \
450		extern_learn
451}
452export -f __l2_vni_init
453
454l2_vni_init()
455{
456	local h1_ns_mac=$(in_ns ns1 mac_get w2)
457	local h2_ns_mac=$(in_ns ns1 mac_get w4)
458	local h1_mac=$(mac_get $h1)
459	local h2_mac=$(mac_get $h2)
460
461	__l2_vni_init $h1_ns_mac $h2_ns_mac 10.1.1.102 10.1.2.102 10.0.0.2
462	in_ns ns1 __l2_vni_init $h1_mac $h2_mac 10.1.1.101 10.1.2.101 10.0.0.1
463}
464
465__l3_vni_init()
466{
467	local mac=$1; shift
468	local vtep_ip=$1; shift
469	local host1_ip=$1; shift
470	local host2_ip=$1; shift
471
472	bridge fdb add $mac dev vx4001 self master extern_learn static \
473		dst $vtep_ip vlan 4001
474
475	ip neigh add $vtep_ip lladdr $mac nud noarp dev vlan4001 extern_learn
476
477	ip route add $host1_ip/32 vrf vrf-green nexthop via $vtep_ip \
478		dev vlan4001 onlink
479	ip route add $host2_ip/32 vrf vrf-green nexthop via $vtep_ip \
480		dev vlan4001 onlink
481}
482export -f __l3_vni_init
483
484l3_vni_init()
485{
486	local vlan4001_ns_mac=$(in_ns ns1 mac_get vlan4001)
487	local vlan4001_mac=$(mac_get vlan4001)
488
489	__l3_vni_init $vlan4001_ns_mac 10.0.0.2 10.1.1.102 10.1.2.102
490	in_ns ns1 __l3_vni_init $vlan4001_mac 10.0.0.1 10.1.1.101 10.1.2.101
491}
492
493setup_prepare()
494{
495	h1=${NETIFS[p1]}
496	swp1=${NETIFS[p2]}
497
498	swp2=${NETIFS[p3]}
499	h2=${NETIFS[p4]}
500
501	rp1=${NETIFS[p5]}
502	rp2=${NETIFS[p6]}
503
504	vrf_prepare
505	forwarding_enable
506
507	h1_create
508	h2_create
509	switch_create
510
511	ip link add name v1 type veth peer name v2
512	spine_create
513	ns1_create
514
515	l2_vni_init
516	l3_vni_init
517}
518
519cleanup()
520{
521	pre_cleanup
522
523	ns1_destroy
524	spine_destroy
525	ip link del dev v1
526
527	switch_destroy
528	h2_destroy
529	h1_destroy
530
531	forwarding_restore
532	vrf_cleanup
533}
534
535ping_ipv4()
536{
537	ping_test $h1 10.1.2.101 ": local->local vid 10->vid 20"
538	ping_test $h1 10.1.1.102 ": local->remote vid 10->vid 10"
539	ping_test $h2 10.1.2.102 ": local->remote vid 20->vid 20"
540	ping_test $h1 10.1.2.102 ": local->remote vid 10->vid 20"
541	ping_test $h2 10.1.1.102 ": local->remote vid 20->vid 10"
542}
543
544trap cleanup EXIT
545
546setup_prepare
547setup_wait
548
549tests_run
550
551exit $EXIT_STATUS
552