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