xref: /freebsd/tests/sys/net/if_bridge_test.sh (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
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	if [ "$(atf_config_get ci false)" = "true" ]; then
81		atf_skip "https://bugs.freebsd.org/246448"
82	fi
83
84	vnet_init
85
86	epair_one=$(vnet_mkepair)
87	epair_two=$(vnet_mkepair)
88	bridge_a=$(vnet_mkbridge)
89	bridge_b=$(vnet_mkbridge)
90
91	vnet_mkjail a ${bridge_a} ${epair_one}a ${epair_two}a
92	vnet_mkjail b ${bridge_b} ${epair_one}b ${epair_two}b
93
94	jexec a ifconfig ${bridge_a} up
95	jexec a ifconfig ${epair_one}a up
96	jexec a ifconfig ${epair_two}a up
97	jexec a ifconfig ${bridge_a} addm ${epair_one}a
98	jexec a ifconfig ${bridge_a} addm ${epair_two}a
99
100	jexec b ifconfig ${bridge_b} up
101	jexec b ifconfig ${epair_one}b up
102	jexec b ifconfig ${epair_two}b up
103	jexec b ifconfig ${bridge_b} addm ${epair_one}b
104	jexec b ifconfig ${bridge_b} addm ${epair_two}b
105
106	jexec a ifconfig ${bridge_a} 192.0.2.1/24
107
108	# Give the interfaces some time to come up and pass some traffic
109	sleep 5
110
111	# Confirm that there's looping traffic
112	nbr=$(jexec a netstat -I ${bridge_a} --libxo json \
113		| jq ".statistics.interface[0].\"received-packets\"")
114	if [ ${nbr} -lt 100 ]
115	then
116		atf_fail "Expected bridging loop, but found very few packets."
117	fi
118
119	# Enable spanning tree
120	jexec a ifconfig ${bridge_a} stp ${epair_one}a
121	jexec a ifconfig ${bridge_a} stp ${epair_two}a
122	jexec b ifconfig ${bridge_b} stp ${epair_one}b
123	jexec b ifconfig ${bridge_b} stp ${epair_two}b
124
125	# Give STP time to do its thing
126	sleep 5
127
128	a_discard=$(jexec a ifconfig ${bridge_a} | grep discarding)
129	b_discard=$(jexec b ifconfig ${bridge_b} | grep discarding)
130
131	if [ -z "${a_discard}" ] && [ -z "${b_discard}" ]
132	then
133		atf_fail "STP failed to detect bridging loop"
134	fi
135}
136
137stp_cleanup()
138{
139	vnet_cleanup
140}
141
142atf_test_case "static" "cleanup"
143static_head()
144{
145	atf_set descr 'Bridge static address test'
146	atf_set require.user root
147}
148
149static_body()
150{
151	vnet_init
152
153	epair=$(vnet_mkepair)
154	bridge=$(vnet_mkbridge)
155
156	vnet_mkjail one ${bridge} ${epair}a
157
158	ifconfig ${epair}b up
159
160	jexec one ifconfig ${bridge} up
161	jexec one ifconfig ${epair}a up
162	jexec one ifconfig ${bridge} addm ${epair}a
163
164	# Wrong interface
165	atf_check -s exit:1 -o ignore -e ignore \
166	    jexec one ifconfig ${bridge} static ${epair}b 00:01:02:03:04:05
167
168	# Bad address format
169	atf_check -s exit:1 -o ignore -e ignore \
170	    jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04
171
172	# Correct add
173	atf_check -s exit:0 -o ignore \
174	    jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04:05
175
176	# List addresses
177	atf_check -s exit:0 -o ignore \
178	    jexec one ifconfig ${bridge} addr
179
180	# Delete with bad address format
181	atf_check -s exit:1 -o ignore -e ignore \
182	    jexec one ifconfig ${bridge} deladdr 00:01:02:03:04
183
184	# Delete with unlisted address
185	atf_check -s exit:1 -o ignore -e ignore \
186	    jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:06
187
188	# Correct delete
189	atf_check -s exit:0 -o ignore \
190	    jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:05
191}
192
193static_cleanup()
194{
195	vnet_cleanup
196}
197
198atf_test_case "span" "cleanup"
199span_head()
200{
201	atf_set descr 'Bridge span test'
202	atf_set require.user root
203}
204
205span_body()
206{
207	set -x
208	vnet_init
209
210	epair=$(vnet_mkepair)
211	epair_span=$(vnet_mkepair)
212	bridge=$(vnet_mkbridge)
213
214	vnet_mkjail one ${bridge} ${epair}a ${epair_span}a
215
216	ifconfig ${epair}b up
217	ifconfig ${epair_span}b up
218
219	jexec one ifconfig ${bridge} up
220	jexec one ifconfig ${epair}a up
221	jexec one ifconfig ${epair_span}a up
222	jexec one ifconfig ${bridge} addm ${epair}a
223
224	jexec one ifconfig ${bridge} span ${epair_span}a
225	jexec one ifconfig ${bridge} 192.0.2.1/24
226
227	# Send some traffic through the span
228	jexec one ping -c 1 -t 1 192.0.2.2
229
230	# Check that we see the traffic on the span interface
231	atf_check -s exit:0 \
232		$(atf_get_srcdir)/../netpfil/common/pft_ping.py \
233		--sendif ${epair}b \
234		--to 192.0.2.2 \
235		--recvif ${epair_span}b
236
237	jexec one ifconfig ${bridge} -span ${epair_span}a
238
239	# And no more traffic after we remove the span
240	atf_check -s exit:1 \
241		$(atf_get_srcdir)/../netpfil/common/pft_ping.py \
242		--sendif ${epair}b \
243		--to 192.0.2.2 \
244		--recvif ${epair_span}b
245}
246
247span_cleanup()
248{
249	vnet_cleanup
250}
251
252atf_test_case "delete_with_members" "cleanup"
253delete_with_members_head()
254{
255	atf_set descr 'Delete a bridge which still has member interfaces'
256	atf_set require.user root
257}
258
259delete_with_members_body()
260{
261	vnet_init
262
263	bridge=$(vnet_mkbridge)
264	epair=$(vnet_mkepair)
265
266	ifconfig ${bridge} 192.0.2.1/24 up
267	ifconfig ${epair}a up
268	ifconfig ${bridge} addm ${epair}a
269
270	ifconfig ${bridge} destroy
271}
272
273delete_with_members_cleanup()
274{
275	vnet_cleanup
276}
277
278atf_test_case "mac_conflict" "cleanup"
279mac_conflict_head()
280{
281	atf_set descr 'Ensure that bridges in different jails get different mac addresses'
282	atf_set require.user root
283}
284
285mac_conflict_body()
286{
287	vnet_init
288
289	epair=$(vnet_mkepair)
290
291	# Ensure the bridge module is loaded so jails can use it.
292	tmpbridge=$(vnet_mkbridge)
293
294	vnet_mkjail bridge_mac_conflict_one ${epair}a
295	vnet_mkjail bridge_mac_conflict_two ${epair}b
296
297	jexec bridge_mac_conflict_one ifconfig bridge create
298	jexec bridge_mac_conflict_one ifconfig bridge0 192.0.2.1/24 up \
299	    addm ${epair}a
300	jexec bridge_mac_conflict_one ifconfig ${epair}a up
301
302	jexec bridge_mac_conflict_two ifconfig bridge create
303	jexec bridge_mac_conflict_two ifconfig bridge0 192.0.2.2/24 up \
304	    addm ${epair}b
305	jexec bridge_mac_conflict_two ifconfig ${epair}b up
306
307	atf_check -s exit:0 -o ignore \
308	    jexec bridge_mac_conflict_one ping -c 3 192.0.2.2
309}
310
311mac_conflict_cleanup()
312{
313	vnet_cleanup
314}
315
316atf_test_case "inherit_mac" "cleanup"
317inherit_mac_head()
318{
319	atf_set descr 'Bridge inherit_mac test, #216510'
320	atf_set require.user root
321}
322
323inherit_mac_body()
324{
325	vnet_init
326
327	bridge=$(vnet_mkbridge)
328	epair=$(vnet_mkepair)
329	vnet_mkjail one ${bridge} ${epair}a
330
331	jexec one sysctl net.link.bridge.inherit_mac=1
332
333	# Attempt to provoke the panic described in #216510
334	jexec one ifconfig ${bridge} 192.0.0.1/24 up
335	jexec one ifconfig ${bridge} addm ${epair}a
336}
337
338inherit_mac_cleanup()
339{
340	vnet_cleanup
341}
342
343atf_init_test_cases()
344{
345	atf_add_test_case "bridge_transmit_ipv4_unicast"
346	atf_add_test_case "stp"
347	atf_add_test_case "static"
348	atf_add_test_case "span"
349	atf_add_test_case "inherit_mac"
350	atf_add_test_case "delete_with_members"
351	atf_add_test_case "mac_conflict"
352}
353