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