xref: /freebsd/tests/sys/net/if_bridge_test.sh (revision 580744621f33383027108364dcadad718df46ffe)
1# $FreeBSD$
2#
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2020 The FreeBSD Foundation
6#
7# This software was developed by Kristof Provost under sponsorship
8# from the FreeBSD Foundation.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29# SUCH DAMAGE.
30
31. $(atf_get_srcdir)/../common/vnet.subr
32
33atf_test_case "bridge_transmit_ipv4_unicast" "cleanup"
34bridge_transmit_ipv4_unicast_head()
35{
36	atf_set descr 'bridge_transmit_ipv4_unicast bridging test'
37	atf_set require.user root
38}
39
40bridge_transmit_ipv4_unicast_body()
41{
42	vnet_init
43
44	epair_alcatraz=$(vnet_mkepair)
45	epair_singsing=$(vnet_mkepair)
46
47	vnet_mkjail alcatraz ${epair_alcatraz}b
48	vnet_mkjail singsing ${epair_singsing}b
49
50	jexec alcatraz ifconfig ${epair_alcatraz}b 192.0.2.1/24 up
51	jexec singsing ifconfig ${epair_singsing}b 192.0.2.2/24 up
52
53	bridge=$(vnet_mkbridge)
54
55	ifconfig ${bridge} up
56	ifconfig ${epair_alcatraz}a up
57	ifconfig ${epair_singsing}a up
58	ifconfig ${bridge} addm ${epair_alcatraz}a
59	ifconfig ${bridge} addm ${epair_singsing}a
60
61	atf_check -s exit:0 -o ignore jexec alcatraz ping -c 3 -t 1 192.0.2.2
62	atf_check -s exit:0 -o ignore jexec singsing ping -c 3 -t 1 192.0.2.1
63}
64
65bridge_transmit_ipv4_unicast_cleanup()
66{
67	vnet_cleanup
68}
69
70atf_test_case "stp" "cleanup"
71stp_head()
72{
73	atf_set descr 'Spanning tree test'
74	atf_set require.user root
75	atf_set require.progs jq
76}
77
78stp_body()
79{
80	vnet_init
81
82	epair_one=$(vnet_mkepair)
83	epair_two=$(vnet_mkepair)
84	bridge_a=$(vnet_mkbridge)
85	bridge_b=$(vnet_mkbridge)
86
87	vnet_mkjail a ${bridge_a} ${epair_one}a ${epair_two}a
88	vnet_mkjail b ${bridge_b} ${epair_one}b ${epair_two}b
89
90	jexec a ifconfig ${bridge_a} up
91	jexec a ifconfig ${epair_one}a up
92	jexec a ifconfig ${epair_two}a up
93	jexec a ifconfig ${bridge_a} addm ${epair_one}a
94	jexec a ifconfig ${bridge_a} addm ${epair_two}a
95
96	jexec b ifconfig ${bridge_b} up
97	jexec b ifconfig ${epair_one}b up
98	jexec b ifconfig ${epair_two}b up
99	jexec b ifconfig ${bridge_b} addm ${epair_one}b
100	jexec b ifconfig ${bridge_b} addm ${epair_two}b
101
102	jexec a ifconfig ${bridge_a} 192.0.2.1/24
103
104	# Give the interfaces some time to come up and pass some traffic
105	sleep 5
106
107	# Confirm that there's looping traffic
108	nbr=$(jexec a netstat -I ${bridge_a} --libxo json \
109		| jq ".statistics.interface[0].\"received-packets\"")
110	if [ ${nbr} -lt 100 ]
111	then
112		atf_fail "Expected bridging loop, but found very few packets."
113	fi
114
115	# Enable spanning tree
116	jexec a ifconfig ${bridge_a} stp ${epair_one}a
117	jexec a ifconfig ${bridge_a} stp ${epair_two}a
118	jexec b ifconfig ${bridge_b} stp ${epair_one}b
119	jexec b ifconfig ${bridge_b} stp ${epair_two}b
120
121	# Give STP time to do its thing
122	sleep 5
123
124	a_discard=$(jexec a ifconfig ${bridge_a} | grep discarding)
125	b_discard=$(jexec b ifconfig ${bridge_b} | grep discarding)
126
127	if [ -z "${a_discard}" ] && [ -z "${b_discard}" ]
128	then
129		atf_fail "STP failed to detect bridging loop"
130	fi
131}
132
133stp_cleanup()
134{
135	vnet_cleanup
136}
137
138atf_test_case "static" "cleanup"
139static_head()
140{
141	atf_set descr 'Bridge static address test'
142	atf_set require.user root
143}
144
145static_body()
146{
147	vnet_init
148
149	epair=$(vnet_mkepair)
150	bridge=$(vnet_mkbridge)
151
152	vnet_mkjail one ${bridge} ${epair}a
153
154	ifconfig ${epair}b up
155
156	jexec one ifconfig ${bridge} up
157	jexec one ifconfig ${epair}a up
158	jexec one ifconfig ${bridge} addm ${epair}a
159
160	# Wrong interface
161	atf_check -s exit:1 -o ignore -e ignore \
162	    jexec one ifconfig ${bridge} static ${epair}b 00:01:02:03:04:05
163
164	# Bad address format
165	atf_check -s exit:1 -o ignore -e ignore \
166	    jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04
167
168	# Correct add
169	atf_check -s exit:0 -o ignore \
170	    jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04:05
171
172	# List addresses
173	atf_check -s exit:0 -o ignore \
174	    jexec one ifconfig ${bridge} addr
175
176	# Delete with bad address format
177	atf_check -s exit:1 -o ignore -e ignore \
178	    jexec one ifconfig ${bridge} deladdr 00:01:02:03:04
179
180	# Delete with unlisted address
181	atf_check -s exit:1 -o ignore -e ignore \
182	    jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:06
183
184	# Correct delete
185	atf_check -s exit:0 -o ignore \
186	    jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:05
187}
188
189static_cleanup()
190{
191	vnet_cleanup
192}
193
194atf_test_case "span" "cleanup"
195span_head()
196{
197	atf_set descr 'Bridge span test'
198	atf_set require.user root
199}
200
201span_body()
202{
203	set -x
204	vnet_init
205
206	epair=$(vnet_mkepair)
207	epair_span=$(vnet_mkepair)
208	bridge=$(vnet_mkbridge)
209
210	vnet_mkjail one ${bridge} ${epair}a ${epair_span}a
211
212	ifconfig ${epair}b up
213	ifconfig ${epair_span}b up
214
215	jexec one ifconfig ${bridge} up
216	jexec one ifconfig ${epair}a up
217	jexec one ifconfig ${epair_span}a up
218	jexec one ifconfig ${bridge} addm ${epair}a
219
220	jexec one ifconfig ${bridge} span ${epair_span}a
221	jexec one ifconfig ${bridge} 192.0.2.1/24
222
223	# Send some traffic through the span
224	jexec one ping -c 1 -t 1 192.0.2.2
225
226	# Check that we see the traffic on the span interface
227	atf_check -s exit:0 \
228		$(atf_get_srcdir)/../netpfil/common/pft_ping.py \
229		--sendif ${epair}b \
230		--to 192.0.2.2 \
231		--recvif ${epair_span}b
232
233	jexec one ifconfig ${bridge} -span ${epair_span}a
234
235	# And no more traffic after we remove the span
236	atf_check -s exit:1 \
237		$(atf_get_srcdir)/../netpfil/common/pft_ping.py \
238		--sendif ${epair}b \
239		--to 192.0.2.2 \
240		--recvif ${epair_span}b
241}
242
243span_cleanup()
244{
245	vnet_cleanup
246}
247
248atf_test_case "delete_with_members" "cleanup"
249delete_with_members_head()
250{
251	atf_set descr 'Delete a bridge which still has member interfaces'
252	atf_set require.user root
253}
254
255delete_with_members_body()
256{
257	vnet_init
258
259	bridge=$(vnet_mkbridge)
260	epair=$(vnet_mkepair)
261
262	ifconfig ${bridge} 192.0.2.1/24 up
263	ifconfig ${epair}a up
264	ifconfig ${bridge} addm ${epair}a
265
266	ifconfig ${bridge} destroy
267}
268
269delete_with_members_cleanup()
270{
271	vnet_cleanup
272}
273
274atf_test_case "mac_conflict" "cleanup"
275mac_conflict_head()
276{
277	atf_set descr 'Ensure that bridges in different jails get different mac addresses'
278	atf_set require.user root
279}
280
281mac_conflict_body()
282{
283	vnet_init
284
285	epair=$(vnet_mkepair)
286
287	# Ensure the bridge module is loaded so jails can use it.
288	tmpbridge=$(vnet_mkbridge)
289
290	vnet_mkjail bridge_mac_conflict_one ${epair}a
291	vnet_mkjail bridge_mac_conflict_two ${epair}b
292
293	jexec bridge_mac_conflict_one ifconfig bridge create
294	jexec bridge_mac_conflict_one ifconfig bridge0 192.0.2.1/24 up \
295	    addm ${epair}a
296	jexec bridge_mac_conflict_one ifconfig ${epair}a up
297
298	jexec bridge_mac_conflict_two ifconfig bridge create
299	jexec bridge_mac_conflict_two ifconfig bridge0 192.0.2.2/24 up \
300	    addm ${epair}b
301	jexec bridge_mac_conflict_two ifconfig ${epair}b up
302
303	atf_check -s exit:0 -o ignore \
304	    jexec bridge_mac_conflict_one ping -c 3 192.0.2.2
305}
306
307mac_conflict_cleanup()
308{
309	vnet_cleanup
310}
311
312atf_test_case "inherit_mac" "cleanup"
313inherit_mac_head()
314{
315	atf_set descr 'Bridge inherit_mac test, #216510'
316	atf_set require.user root
317}
318
319inherit_mac_body()
320{
321	vnet_init
322
323	bridge=$(vnet_mkbridge)
324	epair=$(vnet_mkepair)
325	vnet_mkjail one ${bridge} ${epair}a
326
327	jexec one sysctl net.link.bridge.inherit_mac=1
328
329	# Attempt to provoke the panic described in #216510
330	jexec one ifconfig ${bridge} 192.0.0.1/24 up
331	jexec one ifconfig ${bridge} addm ${epair}a
332}
333
334inherit_mac_cleanup()
335{
336	vnet_cleanup
337}
338
339atf_init_test_cases()
340{
341	atf_add_test_case "bridge_transmit_ipv4_unicast"
342	atf_add_test_case "stp"
343	atf_add_test_case "static"
344	atf_add_test_case "span"
345	atf_add_test_case "inherit_mac"
346	atf_add_test_case "delete_with_members"
347	atf_add_test_case "mac_conflict"
348}
349