t_bridge.sh revision 1.10
1#	$NetBSD: t_bridge.sh,v 1.10 2015/07/23 11:05:34 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
38IPBR1=10.0.0.11
39IPBR2=10.0.0.12
40IP6BR1=fc00::11
41IP6BR2=fc00::12
42
43TIMEOUT=5
44
45atf_test_case basic cleanup
46atf_test_case basic6 cleanup
47atf_test_case rtable cleanup
48atf_test_case member_ip cleanup
49atf_test_case member_ip6 cleanup
50
51basic_head()
52{
53	atf_set "descr" "Does simple if_bridge tests"
54	atf_set "require.progs" "rump_server"
55}
56
57basic6_head()
58{
59	atf_set "descr" "Does simple if_bridge tests (IPv6)"
60	atf_set "require.progs" "rump_server"
61}
62
63rtable_head()
64{
65	atf_set "descr" "Tests route table operations of if_bridge"
66	atf_set "require.progs" "rump_server"
67}
68
69member_ip_head()
70{
71	atf_set "descr" "Tests if_bridge with members with an IP address"
72	atf_set "require.progs" "rump_server"
73}
74
75member_ip6_head()
76{
77	atf_set "descr" "Tests if_bridge with members with an IP address (IPv6)"
78	atf_set "require.progs" "rump_server"
79}
80
81setup_endpoint()
82{
83	sock=${1}
84	addr=${2}
85	bus=${3}
86	mode=${4}
87
88	export RUMP_SERVER=${sock}
89	atf_check -s exit:0 rump.ifconfig shmif0 create
90	atf_check -s exit:0 rump.ifconfig shmif0 linkstr ${bus}
91	if [ $mode = "ipv6" ]; then
92		atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${addr}
93	else
94		atf_check -s exit:0 rump.ifconfig shmif0 inet ${addr} netmask 0xffffff00
95	fi
96
97	atf_check -s exit:0 rump.ifconfig shmif0 up
98	rump.ifconfig shmif0
99}
100
101test_endpoint()
102{
103	sock=${1}
104	addr=${2}
105	bus=${3}
106	mode=${4}
107
108	export RUMP_SERVER=${sock}
109	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
110	if [ $mode = "ipv6" ]; then
111		export LD_PRELOAD=/usr/lib/librumphijack.so
112		atf_check -s exit:0 -o ignore ping6 -n -c 1 -X $TIMEOUT ${addr}
113		unset LD_PRELOAD
114	else
115		atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 ${addr}
116	fi
117}
118
119show_endpoint()
120{
121	sock=${1}
122
123	export RUMP_SERVER=${sock}
124	rump.ifconfig -v shmif0
125}
126
127test_setup()
128{
129	test_endpoint $SOCK1 $IP1 bus1 ipv4
130	test_endpoint $SOCK3 $IP2 bus2 ipv4
131
132	export RUMP_SERVER=$SOCK2
133	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
134	atf_check -s exit:0 -o match:shmif1 rump.ifconfig
135}
136
137test_setup6()
138{
139	test_endpoint $SOCK1 $IP61 bus1 ipv6
140	test_endpoint $SOCK3 $IP62 bus2 ipv6
141
142	export RUMP_SERVER=$SOCK2
143	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
144	atf_check -s exit:0 -o match:shmif1 rump.ifconfig
145}
146
147setup_bridge_server()
148{
149	export RUMP_SERVER=$SOCK2
150	atf_check -s exit:0 rump.ifconfig shmif0 create
151	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
152	atf_check -s exit:0 rump.ifconfig shmif0 up
153
154	atf_check -s exit:0 rump.ifconfig shmif1 create
155	atf_check -s exit:0 rump.ifconfig shmif1 linkstr bus2
156	atf_check -s exit:0 rump.ifconfig shmif1 up
157}
158
159setup()
160{
161	atf_check -s exit:0 ${inetserver} $SOCK1
162	atf_check -s exit:0 ${inetserver} $SOCK2
163	atf_check -s exit:0 ${inetserver} $SOCK3
164
165	setup_endpoint $SOCK1 $IP1 bus1 ipv4
166	setup_endpoint $SOCK3 $IP2 bus2 ipv4
167	setup_bridge_server
168}
169
170setup6()
171{
172	atf_check -s exit:0 ${inet6server} $SOCK1
173	atf_check -s exit:0 ${inet6server} $SOCK2
174	atf_check -s exit:0 ${inet6server} $SOCK3
175
176	setup_endpoint $SOCK1 $IP61 bus1 ipv6
177	setup_endpoint $SOCK3 $IP62 bus2 ipv6
178	setup_bridge_server
179}
180
181setup_bridge()
182{
183	export RUMP_SERVER=$SOCK2
184	atf_check -s exit:0 rump.ifconfig bridge0 create
185	atf_check -s exit:0 rump.ifconfig bridge0 up
186
187	export LD_PRELOAD=/usr/lib/librumphijack.so
188	atf_check -s exit:0 /sbin/brconfig bridge0 add shmif0
189	atf_check -s exit:0 /sbin/brconfig bridge0 add shmif1
190	/sbin/brconfig bridge0
191	unset LD_PRELOAD
192	rump.ifconfig shmif0
193	rump.ifconfig shmif1
194}
195
196setup_member_ip()
197{
198	export RUMP_SERVER=$SOCK2
199	export LD_PRELOAD=/usr/lib/librumphijack.so
200	atf_check -s exit:0 rump.ifconfig shmif0 $IPBR1/24
201	atf_check -s exit:0 rump.ifconfig shmif1 $IPBR2/24
202	atf_check -s exit:0 rump.ifconfig -w 10
203	/sbin/brconfig bridge0
204	unset LD_PRELOAD
205	rump.ifconfig shmif0
206	rump.ifconfig shmif1
207}
208
209setup_member_ip6()
210{
211	export RUMP_SERVER=$SOCK2
212	export LD_PRELOAD=/usr/lib/librumphijack.so
213	atf_check -s exit:0 rump.ifconfig shmif0 inet6 $IP6BR1
214	atf_check -s exit:0 rump.ifconfig shmif1 inet6 $IP6BR2
215	atf_check -s exit:0 rump.ifconfig -w 10
216	/sbin/brconfig bridge0
217	unset LD_PRELOAD
218	rump.ifconfig shmif0
219	rump.ifconfig shmif1
220}
221
222teardown_bridge()
223{
224	export RUMP_SERVER=$SOCK2
225	export LD_PRELOAD=/usr/lib/librumphijack.so
226	/sbin/brconfig bridge0
227	atf_check -s exit:0 /sbin/brconfig bridge0 delete shmif0
228	atf_check -s exit:0 /sbin/brconfig bridge0 delete shmif1
229	/sbin/brconfig bridge0
230	unset LD_PRELOAD
231	rump.ifconfig shmif0
232	rump.ifconfig shmif1
233}
234
235test_setup_bridge()
236{
237	export RUMP_SERVER=$SOCK2
238	export LD_PRELOAD=/usr/lib/librumphijack.so
239	atf_check -s exit:0 -o match:shmif0 /sbin/brconfig bridge0
240	atf_check -s exit:0 -o match:shmif1 /sbin/brconfig bridge0
241	/sbin/brconfig bridge0
242	unset LD_PRELOAD
243}
244
245cleanup()
246{
247	env RUMP_SERVER=$SOCK1 rump.halt
248	env RUMP_SERVER=$SOCK2 rump.halt
249	env RUMP_SERVER=$SOCK3 rump.halt
250}
251
252dump_bus()
253{
254	/usr/bin/shmif_dumpbus -p - bus1 2>/dev/null| /usr/sbin/tcpdump -n -e -r -
255	/usr/bin/shmif_dumpbus -p - bus2 2>/dev/null| /usr/sbin/tcpdump -n -e -r -
256}
257
258down_up_interfaces()
259{
260	export RUMP_SERVER=$SOCK1
261	rump.ifconfig shmif0 down
262	rump.ifconfig shmif0 up
263	export RUMP_SERVER=$SOCK3
264	rump.ifconfig shmif0 down
265	rump.ifconfig shmif0 up
266}
267
268test_ping_failure()
269{
270	export RUMP_SERVER=$SOCK1
271	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP2
272	export RUMP_SERVER=$SOCK3
273	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP1
274}
275
276test_ping_success()
277{
278	export RUMP_SERVER=$SOCK1
279	rump.ifconfig -v shmif0
280	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP2
281	rump.ifconfig -v shmif0
282
283	export RUMP_SERVER=$SOCK3
284	rump.ifconfig -v shmif0
285	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP1
286	rump.ifconfig -v shmif0
287}
288
289test_ping6_failure()
290{
291	export LD_PRELOAD=/usr/lib/librumphijack.so
292	export RUMP_SERVER=$SOCK1
293	atf_check -s not-exit:0 -o ignore ping6 -q -n -c 1 -X $TIMEOUT $IP62
294	export RUMP_SERVER=$SOCK3
295	atf_check -s not-exit:0 -o ignore ping6 -q -n -c 1 -X $TIMEOUT $IP61
296	unset LD_PRELOAD
297}
298
299test_ping6_success()
300{
301	export RUMP_SERVER=$SOCK1
302	rump.ifconfig -v shmif0
303	export LD_PRELOAD=/usr/lib/librumphijack.so
304	atf_check -s exit:0 -o ignore ping6 -q -n -c 1 -X $TIMEOUT $IP62
305	unset LD_PRELOAD
306	rump.ifconfig -v shmif0
307
308	export RUMP_SERVER=$SOCK3
309	rump.ifconfig -v shmif0
310	export LD_PRELOAD=/usr/lib/librumphijack.so
311	atf_check -s exit:0 -o ignore ping6 -q -n -c 1 -X $TIMEOUT $IP61
312	unset LD_PRELOAD
313	rump.ifconfig -v shmif0
314}
315
316test_ping_member()
317{
318	export RUMP_SERVER=$SOCK1
319	rump.ifconfig -v shmif0
320	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IPBR1
321	rump.ifconfig -v shmif0
322	# Test for PR#48104
323	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IPBR2
324	rump.ifconfig -v shmif0
325
326	export RUMP_SERVER=$SOCK3
327	rump.ifconfig -v shmif0
328	# Test for PR#48104
329	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IPBR1
330	rump.ifconfig -v shmif0
331	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IPBR2
332	rump.ifconfig -v shmif0
333}
334
335test_ping6_member()
336{
337	export LD_PRELOAD=/usr/lib/librumphijack.so
338
339	export RUMP_SERVER=$SOCK1
340	rump.ifconfig -v shmif0
341	atf_check -s exit:0 -o ignore ping6 -q -n -X $TIMEOUT -c 1 $IP6BR1
342	rump.ifconfig -v shmif0
343	# Test for PR#48104
344	atf_check -s exit:0 -o ignore ping6 -q -n -X $TIMEOUT -c 1 $IP6BR2
345	rump.ifconfig -v shmif0
346
347	export RUMP_SERVER=$SOCK3
348	rump.ifconfig -v shmif0
349	# Test for PR#48104
350	atf_check -s exit:0 -o ignore ping6 -q -n -X $TIMEOUT -c 1 $IP6BR1
351	rump.ifconfig -v shmif0
352	atf_check -s exit:0 -o ignore ping6 -q -n -X $TIMEOUT -c 1 $IP6BR2
353	rump.ifconfig -v shmif0
354
355	unset LD_PRELOAD
356}
357
358get_number_of_caches()
359{
360	export RUMP_SERVER=$SOCK2
361	export LD_PRELOAD=/usr/lib/librumphijack.so
362	echo $(($(/sbin/brconfig bridge0 |grep -A 100 "Address cache" |wc -l) - 1))
363	unset LD_PRELOAD
364}
365
366test_brconfig_maxaddr()
367{
368	addr1= addr3= n=
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	# Refill the MAC addresses of the endpoints.
378	export RUMP_SERVER=$SOCK1
379	atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2
380	export RUMP_SERVER=$SOCK2
381	export LD_PRELOAD=/usr/lib/librumphijack.so
382	/sbin/brconfig bridge0
383	atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
384	atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
385
386	# Check the default # of caches is 100
387	atf_check -s exit:0 -o match:"max cache: 100" /sbin/brconfig bridge0
388
389	# Test two MAC addresses are cached
390	n=$(get_number_of_caches)
391	atf_check_equal $n 2
392
393	# Limit # of caches to one
394	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 maxaddr 1
395	atf_check -s exit:0 -o match:"max cache: 1" /sbin/brconfig bridge0
396	/sbin/brconfig bridge0
397
398	# Test just one address is cached
399	n=$(get_number_of_caches)
400	atf_check_equal $n 1
401
402	# Increase # of caches to two
403	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 maxaddr 2
404	atf_check -s exit:0 -o match:"max cache: 2" /sbin/brconfig bridge0
405	unset LD_PRELOAD
406
407	# Test we can cache two addresses again
408	export RUMP_SERVER=$SOCK1
409	atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2
410	export RUMP_SERVER=$SOCK2
411	export LD_PRELOAD=/usr/lib/librumphijack.so
412	/sbin/brconfig bridge0
413	atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
414	atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
415	unset LD_PRELOAD
416}
417
418basic_body()
419{
420	setup
421	test_setup
422
423	# Enable once PR kern/49219 is fixed
424	#test_ping_failure
425
426	setup_bridge
427	sleep 1
428	test_setup_bridge
429	test_ping_success
430
431	teardown_bridge
432	test_ping_failure
433}
434
435basic6_body()
436{
437	setup6
438	test_setup6
439
440	test_ping6_failure
441
442	setup_bridge
443	sleep 1
444	test_setup_bridge
445	test_ping6_success
446
447	teardown_bridge
448	test_ping6_failure
449}
450
451rtable_body()
452{
453	addr1= addr3=
454
455	setup
456	setup_bridge
457
458	# Get MAC addresses of the endpoints.
459	export RUMP_SERVER=$SOCK1
460	addr1=$(rump.ifconfig shmif0 |awk '/address:/ { print $2;}')
461	export RUMP_SERVER=$SOCK3
462	addr3=$(rump.ifconfig shmif0 |awk '/address:/ { print $2;}')
463	unset RUMP_SERVER
464
465	# Confirm there is no MAC address caches.
466	export RUMP_SERVER=$SOCK2
467	export LD_PRELOAD=/usr/lib/librumphijack.so
468	/sbin/brconfig bridge0
469	atf_check -s exit:0 -o not-match:"$addr1" /sbin/brconfig bridge0
470	atf_check -s exit:0 -o not-match:"$addr3" /sbin/brconfig bridge0
471	unset LD_PRELOAD
472
473	# Make the bridge learn the MAC addresses of the endpoints.
474	export RUMP_SERVER=$SOCK1
475	atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2
476	unset RUMP_SERVER
477
478	# Tests the addresses are in the cache.
479	export RUMP_SERVER=$SOCK2
480	export LD_PRELOAD=/usr/lib/librumphijack.so
481	/sbin/brconfig bridge0
482	atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
483	atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
484
485	# Tests brconfig deladdr
486	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 deladdr "$addr1"
487	atf_check -s exit:0 -o not-match:"$addr1 shmif0" /sbin/brconfig bridge0
488	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 deladdr "$addr3"
489	atf_check -s exit:0 -o not-match:"$addr3 shmif1" /sbin/brconfig bridge0
490	unset LD_PRELOAD
491
492	# Refill the MAC addresses of the endpoints.
493	export RUMP_SERVER=$SOCK1
494	atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2
495	unset RUMP_SERVER
496	export RUMP_SERVER=$SOCK2
497	export LD_PRELOAD=/usr/lib/librumphijack.so
498	/sbin/brconfig bridge0
499	atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
500	atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
501
502	# Tests brconfig flush.
503	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 flush
504	atf_check -s exit:0 -o not-match:"$addr1 shmif0" /sbin/brconfig bridge0
505	atf_check -s exit:0 -o not-match:"$addr3 shmif1" /sbin/brconfig bridge0
506	unset LD_PRELOAD
507
508	# Tests brconfig timeout.
509	export RUMP_SERVER=$SOCK2
510	export LD_PRELOAD=/usr/lib/librumphijack.so
511	atf_check -s exit:0 -o match:"timeout: 1200" /sbin/brconfig bridge0
512	atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 timeout 10
513	atf_check -s exit:0 -o match:"timeout: 10" /sbin/brconfig bridge0
514	unset LD_PRELOAD
515
516	# Tests brconfig maxaddr.
517	test_brconfig_maxaddr
518
519	# TODO: brconfig static/flushall/discover/learn
520	# TODO: cache expiration; it takes 5 minutes at least and we want to
521	#       wait here so long. Should we have a sysctl to change the period?
522}
523
524member_ip_body()
525{
526	setup
527	test_setup
528
529	# Enable once PR kern/49219 is fixed
530	#test_ping_failure
531
532	setup_bridge
533	sleep 1
534	test_setup_bridge
535	test_ping_success
536
537	setup_member_ip
538	test_ping_member
539
540	teardown_bridge
541	test_ping_failure
542}
543
544member_ip6_body()
545{
546	setup6
547	test_setup6
548
549	test_ping6_failure
550
551	setup_bridge
552	sleep 1
553	test_setup_bridge
554	test_ping6_success
555
556	setup_member_ip6
557	test_ping6_member
558
559	teardown_bridge
560	test_ping6_failure
561}
562
563basic_cleanup()
564{
565	dump_bus
566	cleanup
567}
568
569basic6_cleanup()
570{
571	dump_bus
572	cleanup
573}
574
575rtable_cleanup()
576{
577	dump_bus
578	cleanup
579}
580
581member_ip_cleanup()
582{
583	dump_bus
584	cleanup
585}
586
587member_ip6_cleanup()
588{
589	dump_bus
590	cleanup
591}
592
593atf_init_test_cases()
594{
595	atf_add_test_case basic
596	atf_add_test_case basic6
597	atf_add_test_case rtable
598	atf_add_test_case member_ip
599	atf_add_test_case member_ip6
600}
601