t_bridge.sh revision 1.3
1#! /usr/bin/atf-sh
2#	$NetBSD: t_bridge.sh,v 1.3 2015/01/08 06:33:11 ozaki-r Exp $
3#
4# Copyright (c) 2014 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27#
28
29inetserver="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_bridge -lrumpnet_shmif"
30inet6server="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_netinet6 -lrumpnet_bridge -lrumpnet_shmif"
31
32SOCK1=unix://commsock1
33SOCK2=unix://commsock2
34SOCK3=unix://commsock3
35IP1=10.0.0.1
36IP2=10.0.0.2
37IP61=fc00::1
38IP62=fc00::2
39
40atf_test_case basic cleanup
41atf_test_case basic6 cleanup
42atf_test_case rtable cleanup
43
44basic_head()
45{
46	atf_set "descr" "Does simple if_bridge tests"
47	atf_set "require.progs" "rump_server"
48}
49
50basic6_head()
51{
52	atf_set "descr" "Does simple if_bridge tests (IPv6)"
53	atf_set "require.progs" "rump_server"
54}
55
56rtable_head()
57{
58	atf_set "descr" "Tests route table operations of if_bridge"
59	atf_set "require.progs" "rump_server"
60}
61
62setup_endpoint()
63{
64	sock=${1}
65	addr=${2}
66	bus=${3}
67	mode=${4}
68
69	export RUMP_SERVER=${sock}
70	atf_check -s exit:0 rump.ifconfig shmif0 create
71	atf_check -s exit:0 rump.ifconfig shmif0 linkstr ${bus}
72	if [ $mode = "ipv6" ]; then
73		atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${addr}
74	else
75		atf_check -s exit:0 rump.ifconfig shmif0 inet ${addr} netmask 0xffffff00
76	fi
77
78	atf_check -s exit:0 rump.ifconfig shmif0 up
79	rump.ifconfig shmif0
80}
81
82test_endpoint()
83{
84	sock=${1}
85	addr=${2}
86	bus=${3}
87	mode=${4}
88
89	export RUMP_SERVER=${sock}
90	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
91	if [ $mode = "ipv6" ]; then
92		export LD_PRELOAD=/usr/lib/librumphijack.so
93		atf_check -s exit:0 -o ignore ping6 -n -c 1 ${addr}
94		unset LD_PRELOAD
95	else
96		atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 ${addr}
97	fi
98}
99
100show_endpoint()
101{
102	sock=${1}
103
104	export RUMP_SERVER=${sock}
105	rump.ifconfig -v shmif0
106}
107
108test_setup()
109{
110	test_endpoint $SOCK1 $IP1 bus1 ipv4
111	test_endpoint $SOCK3 $IP2 bus2 ipv4
112
113	export RUMP_SERVER=$SOCK2
114	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
115	atf_check -s exit:0 -o match:shmif1 rump.ifconfig
116}
117
118test_setup6()
119{
120	test_endpoint $SOCK1 $IP61 bus1 ipv6
121	test_endpoint $SOCK3 $IP62 bus2 ipv6
122
123	export RUMP_SERVER=$SOCK2
124	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
125	atf_check -s exit:0 -o match:shmif1 rump.ifconfig
126}
127
128setup_bridge_server()
129{
130	export RUMP_SERVER=$SOCK2
131	atf_check -s exit:0 rump.ifconfig shmif0 create
132	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
133	atf_check -s exit:0 rump.ifconfig shmif0 up
134
135	atf_check -s exit:0 rump.ifconfig shmif1 create
136	atf_check -s exit:0 rump.ifconfig shmif1 linkstr bus2
137	atf_check -s exit:0 rump.ifconfig shmif1 up
138}
139
140setup()
141{
142	atf_check -s exit:0 ${inetserver} $SOCK1
143	atf_check -s exit:0 ${inetserver} $SOCK2
144	atf_check -s exit:0 ${inetserver} $SOCK3
145
146	setup_endpoint $SOCK1 $IP1 bus1 ipv4
147	setup_endpoint $SOCK3 $IP2 bus2 ipv4
148	setup_bridge_server
149}
150
151setup6()
152{
153	atf_check -s exit:0 ${inet6server} $SOCK1
154	atf_check -s exit:0 ${inet6server} $SOCK2
155	atf_check -s exit:0 ${inet6server} $SOCK3
156
157	setup_endpoint $SOCK1 $IP61 bus1 ipv6
158	setup_endpoint $SOCK3 $IP62 bus2 ipv6
159	setup_bridge_server
160}
161
162setup_bridge()
163{
164	export RUMP_SERVER=$SOCK2
165	atf_check -s exit:0 rump.ifconfig bridge0 create
166	atf_check -s exit:0 rump.ifconfig bridge0 up
167
168	export LD_PRELOAD=/usr/lib/librumphijack.so
169	atf_check -s exit:0 /sbin/brconfig bridge0 add shmif0
170	atf_check -s exit:0 /sbin/brconfig bridge0 add shmif1
171	/sbin/brconfig bridge0
172	unset LD_PRELOAD
173	rump.ifconfig shmif0
174	rump.ifconfig shmif1
175}
176
177teardown_bridge()
178{
179	export RUMP_SERVER=$SOCK2
180	export LD_PRELOAD=/usr/lib/librumphijack.so
181	/sbin/brconfig bridge0
182	atf_check -s exit:0 /sbin/brconfig bridge0 delete shmif0
183	atf_check -s exit:0 /sbin/brconfig bridge0 delete shmif1
184	/sbin/brconfig bridge0
185	unset LD_PRELOAD
186	rump.ifconfig shmif0
187	rump.ifconfig shmif1
188}
189
190test_setup_bridge()
191{
192	export RUMP_SERVER=$SOCK2
193	export LD_PRELOAD=/usr/lib/librumphijack.so
194	atf_check -s exit:0 -o match:shmif0 /sbin/brconfig bridge0
195	atf_check -s exit:0 -o match:shmif1 /sbin/brconfig bridge0
196	/sbin/brconfig bridge0
197	unset LD_PRELOAD
198}
199
200cleanup()
201{
202	env RUMP_SERVER=$SOCK1 rump.halt
203	env RUMP_SERVER=$SOCK2 rump.halt
204	env RUMP_SERVER=$SOCK3 rump.halt
205}
206
207dump_bus()
208{
209	/usr/bin/shmif_dumpbus -p - bus1 2>/dev/null| /usr/sbin/tcpdump -n -e -r -
210	/usr/bin/shmif_dumpbus -p - bus2 2>/dev/null| /usr/sbin/tcpdump -n -e -r -
211}
212
213down_up_interfaces()
214{
215	export RUMP_SERVER=$SOCK1
216	rump.ifconfig shmif0 down
217	rump.ifconfig shmif0 up
218	export RUMP_SERVER=$SOCK3
219	rump.ifconfig shmif0 down
220	rump.ifconfig shmif0 up
221}
222
223test_ping_failure()
224{
225	export RUMP_SERVER=$SOCK1
226	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP2
227	export RUMP_SERVER=$SOCK3
228	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP1
229}
230
231test_ping_success()
232{
233	export RUMP_SERVER=$SOCK1
234	rump.ifconfig -v shmif0
235	atf_check -s exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP2
236	rump.ifconfig -v shmif0
237
238	export RUMP_SERVER=$SOCK3
239	rump.ifconfig -v shmif0
240	atf_check -s exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP1
241	rump.ifconfig -v shmif0
242}
243
244test_ping6_failure()
245{
246	export LD_PRELOAD=/usr/lib/librumphijack.so
247	export RUMP_SERVER=$SOCK1
248	atf_check -s not-exit:0 -o ignore ping6 -q -n -c 1 $IP62
249	export RUMP_SERVER=$SOCK3
250	atf_check -s not-exit:0 -o ignore ping6 -q -n -c 1 $IP61
251	unset LD_PRELOAD
252}
253
254test_ping6_success()
255{
256	export RUMP_SERVER=$SOCK1
257	rump.ifconfig -v shmif0
258	export LD_PRELOAD=/usr/lib/librumphijack.so
259	atf_check -s exit:0 -o ignore ping6 -q -n -c 1 $IP62
260	unset LD_PRELOAD
261	rump.ifconfig -v shmif0
262
263	export RUMP_SERVER=$SOCK3
264	rump.ifconfig -v shmif0
265	export LD_PRELOAD=/usr/lib/librumphijack.so
266	atf_check -s exit:0 -o ignore ping6 -q -n -c 1 $IP61
267	unset LD_PRELOAD
268	rump.ifconfig -v shmif0
269}
270
271get_number_of_caches()
272{
273	export RUMP_SERVER=$SOCK2
274	export LD_PRELOAD=/usr/lib/librumphijack.so
275	echo $(($(/sbin/brconfig bridge0 |grep -A 100 "Address cache" |wc -l) - 1))
276	unset LD_PRELOAD
277}
278
279test_brconfig_maxaddr()
280{
281	addr1= addr3= n=
282
283	# Get MAC addresses of the endpoints.
284	export RUMP_SERVER=$SOCK1
285	addr1=$(rump.ifconfig shmif0 |awk '/address:/ { print $2;}')
286	export RUMP_SERVER=$SOCK3
287	addr3=$(rump.ifconfig shmif0 |awk '/address:/ { print $2;}')
288	unset RUMP_SERVER
289
290	# Refill the MAC addresses of the endpoints.
291	export RUMP_SERVER=$SOCK1
292	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 $IP2
293	export RUMP_SERVER=$SOCK2
294	export LD_PRELOAD=/usr/lib/librumphijack.so
295	/sbin/brconfig bridge0
296	atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
297	atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
298
299	# Check the default # of caches is 100
300	atf_check -s exit:0 -o match:"max cache: 100" /sbin/brconfig bridge0
301
302	# Test two MAC addresses are cached
303	n=$(get_number_of_caches)
304	atf_check_equal $n 2
305
306	# Limit # of caches to one
307	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 maxaddr 1
308	atf_check -s exit:0 -o match:"max cache: 1" /sbin/brconfig bridge0
309	/sbin/brconfig bridge0
310
311	# Test just one address is cached
312	n=$(get_number_of_caches)
313	atf_check_equal $n 1
314
315	# Increase # of caches to two
316	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 maxaddr 2
317	atf_check -s exit:0 -o match:"max cache: 2" /sbin/brconfig bridge0
318	unset LD_PRELOAD
319
320	# Test we can cache two addresses again
321	export RUMP_SERVER=$SOCK1
322	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 $IP2
323	export RUMP_SERVER=$SOCK2
324	export LD_PRELOAD=/usr/lib/librumphijack.so
325	/sbin/brconfig bridge0
326	atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
327	atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
328	unset LD_PRELOAD
329}
330
331basic_body()
332{
333	setup
334	test_setup
335
336	setup_bridge
337	test_setup_bridge
338
339	test_ping_success
340
341	teardown_bridge
342	test_ping_failure
343}
344
345basic6_body()
346{
347	setup6
348	test_setup6
349
350	# TODO: enable once ping6 implements timeout feature
351	#test_ping6_failure
352
353	setup_bridge
354	test_setup_bridge
355
356	test_ping6_success
357
358	teardown_bridge
359	# TODO: enable once ping6 implements timeout feature
360	#test_ping6_failure
361}
362
363rtable_body()
364{
365	addr1= addr3=
366
367	setup
368	setup_bridge
369
370	# Get MAC addresses of the endpoints.
371	export RUMP_SERVER=$SOCK1
372	addr1=$(rump.ifconfig shmif0 |awk '/address:/ { print $2;}')
373	export RUMP_SERVER=$SOCK3
374	addr3=$(rump.ifconfig shmif0 |awk '/address:/ { print $2;}')
375	unset RUMP_SERVER
376
377	# Confirm there is no MAC address caches.
378	export RUMP_SERVER=$SOCK2
379	export LD_PRELOAD=/usr/lib/librumphijack.so
380	/sbin/brconfig bridge0
381	atf_check -s exit:0 -o not-match:"$addr1" /sbin/brconfig bridge0
382	atf_check -s exit:0 -o not-match:"$addr3" /sbin/brconfig bridge0
383	unset LD_PRELOAD
384
385	# Make the bridge learn the MAC addresses of the endpoints.
386	export RUMP_SERVER=$SOCK1
387	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 $IP2
388	unset RUMP_SERVER
389
390	# Tests the addresses are in the cache.
391	export RUMP_SERVER=$SOCK2
392	export LD_PRELOAD=/usr/lib/librumphijack.so
393	/sbin/brconfig bridge0
394	atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
395	atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
396
397	# Tests brconfig deladdr
398	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 deladdr "$addr1"
399	atf_check -s exit:0 -o not-match:"$addr1 shmif0" /sbin/brconfig bridge0
400	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 deladdr "$addr3"
401	atf_check -s exit:0 -o not-match:"$addr3 shmif1" /sbin/brconfig bridge0
402	unset LD_PRELOAD
403
404	# Refill the MAC addresses of the endpoints.
405	export RUMP_SERVER=$SOCK1
406	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 $IP2
407	unset RUMP_SERVER
408	export RUMP_SERVER=$SOCK2
409	export LD_PRELOAD=/usr/lib/librumphijack.so
410	/sbin/brconfig bridge0
411	atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
412	atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
413
414	# Tests brconfig flush.
415	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 flush
416	atf_check -s exit:0 -o not-match:"$addr1 shmif0" /sbin/brconfig bridge0
417	atf_check -s exit:0 -o not-match:"$addr3 shmif1" /sbin/brconfig bridge0
418	unset LD_PRELOAD
419
420	# Tests brconfig timeout.
421	export RUMP_SERVER=$SOCK2
422	export LD_PRELOAD=/usr/lib/librumphijack.so
423	atf_check -s exit:0 -o match:"timeout: 1200" /sbin/brconfig bridge0
424	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 timeout 10
425	atf_check -s exit:0 -o match:"timeout: 10" /sbin/brconfig bridge0
426	unset LD_PRELOAD
427
428	# Tests brconfig maxaddr.
429	test_brconfig_maxaddr
430
431	# TODO: brconfig static/flushall/discover/learn
432	# TODO: cache expiration; it takes 5 minutes at least and we want to
433	#       wait here so long. Should we have a sysctl to change the period?
434}
435
436basic_cleanup()
437{
438	dump_bus
439	cleanup
440}
441
442basic6_cleanup()
443{
444	dump_bus
445	cleanup
446}
447
448rtable_cleanup()
449{
450	dump_bus
451	cleanup
452}
453
454atf_init_test_cases()
455{
456	atf_add_test_case basic
457	atf_add_test_case basic6
458	atf_add_test_case rtable
459}
460