1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# +---------------------------------------------+      +----------------------+
5# | H1 (vrf)                                    |      |             H2 (vrf) |
6# |    + $h1.100            + $h1.200           |      |  + $h2               |
7# |    | 192.0.2.1/28       | 192.0.2.17/28     |      |  | 192.0.2.130/28    |
8# |    | 2001:db8:1::1/64   | 2001:db8:3::1/64  |      |  | 192.0.2.146/28    |
9# |    \_________ __________/                   |      |  | 2001:db8:2::2/64  |
10# |              V                              |      |  | 2001:db8:4::2/64  |
11# |              + $h1                          |      |  |                   |
12# +--------------|------------------------------+      +--|-------------------+
13#                |                                        |
14# +--------------|----------------------------------------|-------------------+
15# | SW           + $swp1                                  + $swp2             |
16# |              |                                          192.0.2.129/28    |
17# |              |                                          192.0.2.145/28    |
18# |              |                                          2001:db8:2::1/64  |
19# |      ________^___________________________               2001:db8:4::1/64  |
20# |     /                                    \                                |
21# | +---|------------------------------+ +---|------------------------------+ |
22# | |   + $swp1.100   BR1 (802.1d)     | |   + $swp1.200   BR2 (802.1d)     | |
23# | |                 192.0.2.2/28     | |                 192.0.2.18/28    | |
24# | |                 2001:db8:1::2/64 | |                 2001:db8:3::2/64 | |
25# | |                                  | |                                  | |
26# | +----------------------------------+ +----------------------------------+ |
27# +---------------------------------------------------------------------------+
28
29ALL_TESTS="
30	ping_ipv4
31	ping_ipv6
32	config_remaster
33	ping_ipv4
34	ping_ipv6
35"
36NUM_NETIFS=4
37source lib.sh
38
39h1_create()
40{
41	simple_if_init $h1
42	vlan_create $h1 100 v$h1 192.0.2.1/28 2001:db8:1::1/64
43	vlan_create $h1 200 v$h1 192.0.2.17/28 2001:db8:3::1/64
44	ip -4 route add 192.0.2.128/28 vrf v$h1 nexthop via 192.0.2.2
45	ip -4 route add 192.0.2.144/28 vrf v$h1 nexthop via 192.0.2.18
46	ip -6 route add 2001:db8:2::/64 vrf v$h1 nexthop via 2001:db8:1::2
47	ip -6 route add 2001:db8:4::/64 vrf v$h1 nexthop via 2001:db8:3::2
48}
49
50h1_destroy()
51{
52	ip -6 route del 2001:db8:4::/64 vrf v$h1
53	ip -6 route del 2001:db8:2::/64 vrf v$h1
54	ip -4 route del 192.0.2.144/28 vrf v$h1
55	ip -4 route del 192.0.2.128/28 vrf v$h1
56	vlan_destroy $h1 200
57	vlan_destroy $h1 100
58	simple_if_fini $h1
59}
60
61h2_create()
62{
63	simple_if_init $h2 192.0.2.130/28 2001:db8:2::2/64 \
64			   192.0.2.146/28 2001:db8:4::2/64
65	ip -4 route add 192.0.2.0/28 vrf v$h2 nexthop via 192.0.2.129
66	ip -4 route add 192.0.2.16/28 vrf v$h2 nexthop via 192.0.2.145
67	ip -6 route add 2001:db8:1::/64 vrf v$h2 nexthop via 2001:db8:2::1
68	ip -6 route add 2001:db8:3::/64 vrf v$h2 nexthop via 2001:db8:4::1
69}
70
71h2_destroy()
72{
73	ip -6 route del 2001:db8:3::/64 vrf v$h2
74	ip -6 route del 2001:db8:1::/64 vrf v$h2
75	ip -4 route del 192.0.2.16/28 vrf v$h2
76	ip -4 route del 192.0.2.0/28 vrf v$h2
77	simple_if_fini $h2 192.0.2.130/28 2001:db8:2::2/64 \
78			   192.0.2.146/28 2001:db8:4::2/64
79}
80
81router_create()
82{
83	ip link set dev $swp1 up
84
85	vlan_create $swp1 100
86	ip link add name br1 type bridge vlan_filtering 0
87	ip link set dev br1 address $(mac_get $swp1.100)
88	ip link set dev $swp1.100 master br1
89	__addr_add_del br1 add 192.0.2.2/28 2001:db8:1::2/64
90	ip link set dev br1 up
91
92	vlan_create $swp1 200
93	ip link add name br2 type bridge vlan_filtering 0
94	ip link set dev br2 address $(mac_get $swp1.200)
95	ip link set dev $swp1.200 master br2
96	__addr_add_del br2 add 192.0.2.18/28 2001:db8:3::2/64
97	ip link set dev br2 up
98
99	ip link set dev $swp2 up
100	__addr_add_del $swp2 add 192.0.2.129/28 2001:db8:2::1/64 \
101				 192.0.2.145/28 2001:db8:4::1/64
102}
103
104router_destroy()
105{
106	__addr_add_del $swp2 del 192.0.2.129/28 2001:db8:2::1/64 \
107				 192.0.2.145/28 2001:db8:4::1/64
108	ip link set dev $swp2 down
109
110	__addr_add_del br2 del 192.0.2.18/28 2001:db8:3::2/64
111	ip link set dev $swp1.200 nomaster
112	ip link del dev br2
113	vlan_destroy $swp1 200
114
115	__addr_add_del br1 del 192.0.2.2/28 2001:db8:1::2/64
116	ip link set dev $swp1.100 nomaster
117	ip link del dev br1
118	vlan_destroy $swp1 100
119
120	ip link set dev $swp1 down
121}
122
123config_remaster()
124{
125	log_info "Remaster bridge slaves"
126
127	ip link set dev $swp1.100 nomaster
128	ip link set dev $swp1.200 nomaster
129	sleep 2
130	ip link set dev $swp1.200 master br2
131	ip link set dev $swp1.100 master br1
132}
133
134setup_prepare()
135{
136	h1=${NETIFS[p1]}
137	swp1=${NETIFS[p2]}
138
139	swp2=${NETIFS[p3]}
140	h2=${NETIFS[p4]}
141
142	vrf_prepare
143
144	h1_create
145	h2_create
146
147	router_create
148
149	forwarding_enable
150}
151
152cleanup()
153{
154	pre_cleanup
155
156	forwarding_restore
157
158	router_destroy
159
160	h2_destroy
161	h1_destroy
162
163	vrf_cleanup
164}
165
166ping_ipv4()
167{
168	ping_test $h1 192.0.2.130 ": via 100"
169	ping_test $h1 192.0.2.146 ": via 200"
170}
171
172ping_ipv6()
173{
174	ping6_test $h1 2001:db8:2::2 ": via 100"
175	ping6_test $h1 2001:db8:4::2 ": via 200"
176}
177
178trap cleanup EXIT
179
180setup_prepare
181setup_wait
182
183tests_run
184
185exit $EXIT_STATUS
186