Home | History | Annotate | Line # | Download | only in net
net_common.sh revision 1.36
      1 #	$NetBSD: net_common.sh,v 1.36 2019/08/26 04:50:03 ozaki-r Exp $
      2 #
      3 # Copyright (c) 2016 Internet Initiative Japan 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 
     28 #
     29 # Common utility functions for tests/net
     30 #
     31 
     32 HIJACKING="env LD_PRELOAD=/usr/lib/librumphijack.so \
     33     RUMPHIJACK=path=/rump,socket=all:nolocal,sysctl=yes"
     34 ONEDAYISH="(23h5[0-9]m|1d0h0m)[0-9]+s ?"
     35 
     36 extract_new_packets()
     37 {
     38 	local bus=$1
     39 	local old=./.__old
     40 
     41 	if [ ! -f $old ]; then
     42 		old=/dev/null
     43 	fi
     44 
     45 	shmif_dumpbus -p - $bus 2>/dev/null |
     46 	    tcpdump -n -e -r - 2>/dev/null > ./.__new
     47 	diff -u $old ./.__new | grep '^+' | cut -d '+' -f 2   > ./.__diff
     48 	mv -f ./.__new ./.__old
     49 	cat ./.__diff
     50 }
     51 
     52 check_route()
     53 {
     54 	local target=$1
     55 	local gw=$2
     56 	local flags=${3:-\.\+}
     57 	local ifname=${4:-\.\+}
     58 
     59 	target=$(echo $target | sed 's/\./\\./g')
     60 	if [ "$gw" = "" ]; then
     61 		gw=".+"
     62 	else
     63 		gw=$(echo $gw | sed 's/\./\\./g')
     64 	fi
     65 
     66 	atf_check -s exit:0 -e ignore \
     67 	    -o match:"^$target +$gw +$flags +- +- +.+ +$ifname" \
     68 	    rump.netstat -rn
     69 }
     70 
     71 check_route_flags()
     72 {
     73 
     74 	check_route "$1" "" "$2" ""
     75 }
     76 
     77 check_route_gw()
     78 {
     79 
     80 	check_route "$1" "$2" "" ""
     81 }
     82 
     83 check_route_no_entry()
     84 {
     85 	local target=$(echo "$1" | sed 's/\./\\./g')
     86 
     87 	atf_check -s exit:0 -e ignore -o not-match:"^$target" rump.netstat -rn
     88 }
     89 
     90 get_linklocal_addr()
     91 {
     92 
     93 	RUMP_SERVER=${1} rump.ifconfig ${2} inet6 |
     94 	    awk "/fe80/ {sub(/%$2/, \"\"); sub(/\\/[0-9]*/, \"\"); print \$2;}"
     95 
     96 	return 0
     97 }
     98 
     99 get_macaddr()
    100 {
    101 
    102 	RUMP_SERVER=${1} rump.ifconfig ${2} | awk '/address/ {print $2;}'
    103 }
    104 
    105 HTTPD_PID=./.__httpd.pid
    106 start_httpd()
    107 {
    108 	local sock=$1
    109 	local ip=$2
    110 	local backup=$RUMP_SERVER
    111 
    112 	export RUMP_SERVER=$sock
    113 
    114 	# start httpd in daemon mode
    115 	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so \
    116 	    /usr/libexec/httpd -P $HTTPD_PID -i $ip -b -s $(pwd)
    117 
    118 	export RUMP_SERVER=$backup
    119 
    120 	sleep 3
    121 }
    122 
    123 stop_httpd()
    124 {
    125 
    126 	if [ -f $HTTPD_PID ]; then
    127 		kill -9 $(cat $HTTPD_PID)
    128 		rm -f $HTTPD_PID
    129 		sleep 1
    130 	fi
    131 }
    132 
    133 NC_PID=./.__nc.pid
    134 start_nc_server()
    135 {
    136 	local sock=$1
    137 	local port=$2
    138 	local outfile=$3
    139 	local proto=${4:-ipv4}
    140 	local backup=$RUMP_SERVER
    141 	local opts=
    142 
    143 	export RUMP_SERVER=$sock
    144 
    145 	if [ $proto = ipv4 ]; then
    146 		opts="-l -4"
    147 	else
    148 		opts="-l -6"
    149 	fi
    150 
    151 	env LD_PRELOAD=/usr/lib/librumphijack.so nc $opts $port > $outfile &
    152 	echo $! > $NC_PID
    153 
    154 	if [ $proto = ipv4 ]; then
    155 		$DEBUG && rump.netstat -a -f inet
    156 	else
    157 		$DEBUG && rump.netstat -a -f inet6
    158 	fi
    159 
    160 	export RUMP_SERVER=$backup
    161 
    162 	sleep 1
    163 }
    164 
    165 stop_nc_server()
    166 {
    167 
    168 	if [ -f $NC_PID ]; then
    169 		kill -9 $(cat $NC_PID)
    170 		rm -f $NC_PID
    171 		sleep 1
    172 	fi
    173 }
    174 
    175 BASIC_LIBS="-lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif"
    176 FS_LIBS="$BASIC_LIBS -lrumpdev -lrumpvfs -lrumpfs_ffs"
    177 CRYPTO_LIBS="$BASIC_LIBS -lrumpdev -lrumpdev_opencrypto \
    178     -lrumpkern_z -lrumpkern_crypto"
    179 NPF_LIBS="$BASIC_LIBS -lrumpdev -lrumpvfs -lrumpdev_bpf -lrumpnet_npf"
    180 CRYPTO_NPF_LIBS="$CRYPTO_LIBS -lrumpvfs -lrumpdev_bpf -lrumpnet_npf"
    181 
    182 # We cannot keep variables between test phases, so need to store in files
    183 _rump_server_socks=./.__socks
    184 _rump_server_ifaces=./.__ifaces
    185 _rump_server_buses=./.__buses
    186 
    187 DEBUG_SYSCTL_ENTRIES="net.inet.arp.debug net.inet6.icmp6.nd6_debug \
    188     net.inet.ipsec.debug"
    189 
    190 IPSEC_KEY_DEBUG=${IPSEC_KEY_DEBUG:-false}
    191 
    192 _rump_server_start_common()
    193 {
    194 	local sock=$1
    195 	local backup=$RUMP_SERVER
    196 
    197 	shift 1
    198 
    199 	atf_check -s exit:0 rump_server "$@" "$sock"
    200 
    201 	if $DEBUG; then
    202 		# Enable debugging features in the kernel
    203 		export RUMP_SERVER=$sock
    204 		for ent in $DEBUG_SYSCTL_ENTRIES; do
    205 			if rump.sysctl -q $ent; then
    206 				atf_check -s exit:0 rump.sysctl -q -w $ent=1
    207 			fi
    208 		done
    209 		export RUMP_SERVER=$backup
    210 	fi
    211 	if $IPSEC_KEY_DEBUG; then
    212 		# Enable debugging features in the kernel
    213 		export RUMP_SERVER=$sock
    214 		if rump.sysctl -q net.key.debug; then
    215 			atf_check -s exit:0 \
    216 			    rump.sysctl -q -w net.key.debug=0xffff
    217 		fi
    218 		export RUMP_SERVER=$backup
    219 	fi
    220 
    221 	echo $sock >> $_rump_server_socks
    222 	$DEBUG && cat $_rump_server_socks
    223 }
    224 
    225 rump_server_start()
    226 {
    227 	local sock=$1
    228 	local lib=
    229 	local libs="$BASIC_LIBS"
    230 
    231 	shift 1
    232 
    233 	for lib
    234 	do
    235 		libs="$libs -lrumpnet_$lib"
    236 	done
    237 
    238 	_rump_server_start_common $sock $libs
    239 
    240 	return 0
    241 }
    242 
    243 rump_server_fs_start()
    244 {
    245 	local sock=$1
    246 	local lib=
    247 	local libs="$FS_LIBS"
    248 
    249 	shift 1
    250 
    251 	for lib
    252 	do
    253 		libs="$libs -lrumpnet_$lib"
    254 	done
    255 
    256 	_rump_server_start_common $sock $libs
    257 
    258 	return 0
    259 }
    260 
    261 rump_server_crypto_start()
    262 {
    263 	local sock=$1
    264 	local lib=
    265 	local libs="$CRYPTO_LIBS"
    266 
    267 	shift 1
    268 
    269 	for lib
    270 	do
    271 		libs="$libs -lrumpnet_$lib"
    272 	done
    273 
    274 	_rump_server_start_common $sock $libs
    275 
    276 	return 0
    277 }
    278 
    279 rump_server_npf_start()
    280 {
    281 	local sock=$1
    282 	local lib=
    283 	local libs="$NPF_LIBS"
    284 
    285 	shift 1
    286 
    287 	for lib
    288 	do
    289 		libs="$libs -lrumpnet_$lib"
    290 	done
    291 
    292 	_rump_server_start_common $sock $libs
    293 
    294 	return 0
    295 }
    296 
    297 rump_server_crypto_npf_start()
    298 {
    299 	local sock=$1
    300 	local lib=
    301 	local libs="$CRYPTO_NPF_LIBS"
    302 
    303 	shift 1
    304 
    305 	for lib
    306 	do
    307 		libs="$libs -lrumpnet_$lib"
    308 	done
    309 
    310 	_rump_server_start_common $sock $libs
    311 
    312 	return 0
    313 }
    314 
    315 rump_server_add_iface()
    316 {
    317 	local sock=$1
    318 	local ifname=$2
    319 	local bus=$3
    320 	local backup=$RUMP_SERVER
    321 
    322 	export RUMP_SERVER=$sock
    323 	atf_check -s exit:0 rump.ifconfig $ifname create
    324 	if [ -n "$bus" ]; then
    325 		atf_check -s exit:0 rump.ifconfig $ifname linkstr $bus
    326 	fi
    327 	export RUMP_SERVER=$backup
    328 
    329 	echo $sock $ifname >> $_rump_server_ifaces
    330 	$DEBUG && cat $_rump_server_ifaces
    331 
    332 	echo $bus >> $_rump_server_buses
    333 	cat $_rump_server_buses |sort -u >./.__tmp
    334 	mv -f ./.__tmp $_rump_server_buses
    335 	$DEBUG && cat $_rump_server_buses
    336 
    337 	return 0
    338 }
    339 
    340 rump_server_check_poolleaks()
    341 {
    342 	local target=$1
    343 
    344 	# XXX rumphijack doesn't work with a binary with suid/sgid bits like
    345 	# vmstat.  Use a copied one to drop sgid bit as a workaround until
    346 	# vmstat stops using kvm(3) for /dev/kmem and the sgid bit.
    347 	cp /usr/bin/vmstat ./vmstat
    348 	reqs=$($HIJACKING ./vmstat -mv | awk "/$target/ {print \$3;}")
    349 	rels=$($HIJACKING ./vmstat -mv | awk "/$target/ {print \$5;}")
    350 	rm -f ./vmstat
    351 	atf_check_equal '$target$reqs' '$target$rels'
    352 }
    353 
    354 
    355 rump_server_check_memleaks()
    356 {
    357 
    358 	rump_server_check_poolleaks llentrypl
    359 	# This doesn't work for objects allocated through pool_cache
    360 	#rump_server_check_poolleaks mbpl
    361 	#rump_server_check_poolleaks mclpl
    362 	#rump_server_check_poolleaks socket
    363 }
    364 
    365 rump_server_destroy_ifaces()
    366 {
    367 	local backup=$RUMP_SERVER
    368 	local output=ignore
    369 	local reqs= rels=
    370 
    371 	$DEBUG && cat $_rump_server_ifaces
    372 
    373 	# Try to dump states before destroying interfaces
    374 	for sock in $(cat $_rump_server_socks); do
    375 		export RUMP_SERVER=$sock
    376 		if $DEBUG; then
    377 			output=save:/dev/stdout
    378 		fi
    379 		atf_check -s exit:0 -o $output rump.ifconfig
    380 		atf_check -s exit:0 -o $output rump.netstat -nr
    381 		# XXX still need hijacking
    382 		atf_check -s exit:0 -o $output $HIJACKING rump.netstat -nai
    383 		atf_check -s exit:0 -o $output rump.arp -na
    384 		atf_check -s exit:0 -o $output rump.ndp -na
    385 		atf_check -s exit:0 -o $output $HIJACKING ifmcstat
    386 	done
    387 
    388 	# XXX using pipe doesn't work. See PR bin/51667
    389 	#cat $_rump_server_ifaces | while read sock ifname; do
    390 	# Destroy interfaces in the reverse order
    391 	tac $_rump_server_ifaces > __ifaces
    392 	while read sock ifname; do
    393 		export RUMP_SERVER=$sock
    394 		if rump.ifconfig -l |grep -q $ifname; then
    395 			atf_check -s exit:0 rump.ifconfig $ifname destroy
    396 		fi
    397 		atf_check -s exit:0 -o ignore rump.ifconfig
    398 	done < __ifaces
    399 	rm -f __ifaces
    400 
    401 	for sock in $(cat $_rump_server_socks); do
    402 		export RUMP_SERVER=$sock
    403 		rump_server_check_memleaks
    404 	done
    405 
    406 	export RUMP_SERVER=$backup
    407 
    408 	return 0
    409 }
    410 
    411 rump_server_halt_servers()
    412 {
    413 	local backup=$RUMP_SERVER
    414 
    415 	$DEBUG && cat $_rump_server_socks
    416 	for sock in $(cat $_rump_server_socks); do
    417 		env RUMP_SERVER=$sock rump.halt
    418 	done
    419 	export RUMP_SERVER=$backup
    420 
    421 	return 0
    422 }
    423 
    424 extract_rump_server_core()
    425 {
    426 
    427 	if [ -f rump_server.core ]; then
    428 		gdb -ex bt /usr/bin/rump_server rump_server.core
    429 		# Extract kernel logs including a panic message
    430 		strings rump_server.core |grep -E '^\[.+\] '
    431 	fi
    432 }
    433 
    434 dump_kernel_stats()
    435 {
    436 	local sock=$1
    437 
    438 	echo "### Dumping $sock"
    439 	export RUMP_SERVER=$sock
    440 	rump.ifconfig -av
    441 	rump.netstat -nr
    442 	# XXX still need hijacking
    443 	$HIJACKING rump.netstat -nai
    444 	# XXX workaround for vmstat with the sgid bit
    445 	cp /usr/bin/vmstat ./vmstat
    446 	$HIJACKING ./vmstat -m
    447 	rm -f ./vmstat
    448 	rump.arp -na
    449 	rump.ndp -na
    450 	$HIJACKING ifmcstat
    451 	$HIJACKING dmesg
    452 }
    453 
    454 rump_server_dump_servers()
    455 {
    456 	local backup=$RUMP_SERVER
    457 
    458 	$DEBUG && cat $_rump_server_socks
    459 	for sock in $(cat $_rump_server_socks); do
    460 		dump_kernel_stats $sock
    461 	done
    462 	export RUMP_SERVER=$backup
    463 
    464 	extract_rump_server_core
    465 	return 0
    466 }
    467 
    468 rump_server_dump_buses()
    469 {
    470 
    471 	if [ ! -f $_rump_server_buses ]; then
    472 		return 0
    473 	fi
    474 
    475 	$DEBUG && cat $_rump_server_buses
    476 	for bus in $(cat $_rump_server_buses); do
    477 		echo "### Dumping $bus"
    478 		shmif_dumpbus -p - $bus 2>/dev/null| tcpdump -n -e -r -
    479 	done
    480 	return 0
    481 }
    482 
    483 cleanup()
    484 {
    485 
    486 	rump_server_halt_servers
    487 }
    488 
    489 dump()
    490 {
    491 
    492 	rump_server_dump_servers
    493 	rump_server_dump_buses
    494 }
    495 
    496 skip_if_qemu()
    497 {
    498 	if sysctl machdep.cpu_brand 2>/dev/null | grep QEMU >/dev/null 2>&1
    499 	then
    500 	    atf_skip "unreliable under qemu, skip until PR kern/43997 fixed"
    501 	fi
    502 }
    503 
    504 test_create_destroy_common()
    505 {
    506 	local sock=$1
    507 	local ifname=$2
    508 	local test_address=${3:-false}
    509 	local ipv4="10.0.0.1/24"
    510 	local ipv6="fc00::1"
    511 
    512 	export RUMP_SERVER=$sock
    513 
    514 	atf_check -s exit:0 rump.ifconfig $ifname create
    515 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    516 
    517 	atf_check -s exit:0 rump.ifconfig $ifname create
    518 	atf_check -s exit:0 rump.ifconfig $ifname up
    519 	atf_check -s exit:0 rump.ifconfig $ifname down
    520 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    521 
    522 	# Destroy while UP
    523 	atf_check -s exit:0 rump.ifconfig $ifname create
    524 	atf_check -s exit:0 rump.ifconfig $ifname up
    525 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    526 
    527 	if ! $test_address; then
    528 		return
    529 	fi
    530 
    531 	# With an IPv4 address
    532 	atf_check -s exit:0 rump.ifconfig $ifname create
    533 	atf_check -s exit:0 rump.ifconfig $ifname inet $ipv4
    534 	atf_check -s exit:0 rump.ifconfig $ifname up
    535 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    536 
    537 	# With an IPv6 address
    538 	atf_check -s exit:0 rump.ifconfig $ifname create
    539 	atf_check -s exit:0 rump.ifconfig $ifname inet6 $ipv6
    540 	atf_check -s exit:0 rump.ifconfig $ifname up
    541 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    542 
    543 	unset RUMP_SERVER
    544 }
    545