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