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