Home | History | Annotate | Line # | Download | only in net
net_common.sh revision 1.39
      1 #	$NetBSD: net_common.sh,v 1.39 2020/02/20 08:02:26 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 _rump_server_macaddrs=./.__macaddrs
    187 
    188 DEBUG_SYSCTL_ENTRIES="net.inet.arp.debug net.inet6.icmp6.nd6_debug \
    189     net.inet.ipsec.debug"
    190 
    191 IPSEC_KEY_DEBUG=${IPSEC_KEY_DEBUG:-false}
    192 
    193 _rump_server_start_common()
    194 {
    195 	local sock=$1
    196 	local backup=$RUMP_SERVER
    197 
    198 	shift 1
    199 
    200 	atf_check -s exit:0 rump_server "$@" "$sock"
    201 
    202 	if $DEBUG; then
    203 		# Enable debugging features in the kernel
    204 		export RUMP_SERVER=$sock
    205 		for ent in $DEBUG_SYSCTL_ENTRIES; do
    206 			if rump.sysctl -q $ent; then
    207 				atf_check -s exit:0 rump.sysctl -q -w $ent=1
    208 			fi
    209 		done
    210 		export RUMP_SERVER=$backup
    211 	fi
    212 	if $IPSEC_KEY_DEBUG; then
    213 		# Enable debugging features in the kernel
    214 		export RUMP_SERVER=$sock
    215 		if rump.sysctl -q net.key.debug; then
    216 			atf_check -s exit:0 \
    217 			    rump.sysctl -q -w net.key.debug=0xffff
    218 		fi
    219 		export RUMP_SERVER=$backup
    220 	fi
    221 
    222 	echo $sock >> $_rump_server_socks
    223 	$DEBUG && cat $_rump_server_socks
    224 }
    225 
    226 rump_server_start()
    227 {
    228 	local sock=$1
    229 	local lib=
    230 	local libs="$BASIC_LIBS"
    231 
    232 	shift 1
    233 
    234 	for lib
    235 	do
    236 		libs="$libs -lrumpnet_$lib"
    237 	done
    238 
    239 	_rump_server_start_common $sock $libs
    240 
    241 	return 0
    242 }
    243 
    244 rump_server_fs_start()
    245 {
    246 	local sock=$1
    247 	local lib=
    248 	local libs="$FS_LIBS"
    249 
    250 	shift 1
    251 
    252 	for lib
    253 	do
    254 		libs="$libs -lrumpnet_$lib"
    255 	done
    256 
    257 	_rump_server_start_common $sock $libs
    258 
    259 	return 0
    260 }
    261 
    262 rump_server_crypto_start()
    263 {
    264 	local sock=$1
    265 	local lib=
    266 	local libs="$CRYPTO_LIBS"
    267 
    268 	shift 1
    269 
    270 	for lib
    271 	do
    272 		libs="$libs -lrumpnet_$lib"
    273 	done
    274 
    275 	_rump_server_start_common $sock $libs
    276 
    277 	return 0
    278 }
    279 
    280 rump_server_npf_start()
    281 {
    282 	local sock=$1
    283 	local lib=
    284 	local libs="$NPF_LIBS"
    285 
    286 	shift 1
    287 
    288 	for lib
    289 	do
    290 		libs="$libs -lrumpnet_$lib"
    291 	done
    292 
    293 	_rump_server_start_common $sock $libs
    294 
    295 	return 0
    296 }
    297 
    298 rump_server_crypto_npf_start()
    299 {
    300 	local sock=$1
    301 	local lib=
    302 	local libs="$CRYPTO_NPF_LIBS"
    303 
    304 	shift 1
    305 
    306 	for lib
    307 	do
    308 		libs="$libs -lrumpnet_$lib"
    309 	done
    310 
    311 	_rump_server_start_common $sock $libs
    312 
    313 	return 0
    314 }
    315 
    316 rump_server_add_iface()
    317 {
    318 	local sock=$1
    319 	local ifname=$2
    320 	local bus=$3
    321 	local backup=$RUMP_SERVER
    322 	local macaddr=
    323 
    324 	export RUMP_SERVER=$sock
    325 	atf_check -s exit:0 rump.ifconfig $ifname create
    326 	if [ -n "$bus" ]; then
    327 		atf_check -s exit:0 rump.ifconfig $ifname linkstr $bus
    328 	fi
    329 	macaddr=$(get_macaddr $sock $ifname)
    330 	export RUMP_SERVER=$backup
    331 
    332 	if [ -f $_rump_server_macaddrs ]; then
    333 		atf_check -s not-exit:0 grep -q $macaddr $_rump_server_macaddrs
    334 	fi
    335 	echo $macaddr >> $_rump_server_macaddrs
    336 
    337 	echo $sock $ifname >> $_rump_server_ifaces
    338 	$DEBUG && cat $_rump_server_ifaces
    339 
    340 	echo $bus >> $_rump_server_buses
    341 	cat $_rump_server_buses |sort -u >./.__tmp
    342 	mv -f ./.__tmp $_rump_server_buses
    343 	$DEBUG && cat $_rump_server_buses
    344 
    345 	return 0
    346 }
    347 
    348 rump_server_check_poolleaks()
    349 {
    350 	local target=$1
    351 
    352 	# XXX rumphijack doesn't work with a binary with suid/sgid bits like
    353 	# vmstat.  Use a copied one to drop sgid bit as a workaround until
    354 	# vmstat stops using kvm(3) for /dev/kmem and the sgid bit.
    355 	cp /usr/bin/vmstat ./vmstat
    356 	reqs=$($HIJACKING ./vmstat -mv | awk "/$target/ {print \$3;}")
    357 	rels=$($HIJACKING ./vmstat -mv | awk "/$target/ {print \$5;}")
    358 	rm -f ./vmstat
    359 	atf_check_equal '$target$reqs' '$target$rels'
    360 }
    361 
    362 #
    363 # rump_server_check_memleaks detects memory leaks.  It can detect leaks of pool
    364 # objects that are guaranteed to be all deallocated at this point, i.e., all
    365 # created interfaces are destroyed.  Currently only llentpl satisfies this
    366 # constraint.  This mechanism can't be applied to objects allocated through
    367 # pool_cache(9) because it doesn't track released objects explicitly.
    368 #
    369 rump_server_check_memleaks()
    370 {
    371 
    372 	rump_server_check_poolleaks llentrypl
    373 	# This doesn't work for objects allocated through pool_cache
    374 	#rump_server_check_poolleaks mbpl
    375 	#rump_server_check_poolleaks mclpl
    376 	#rump_server_check_poolleaks socket
    377 }
    378 
    379 rump_server_destroy_ifaces()
    380 {
    381 	local backup=$RUMP_SERVER
    382 	local output=ignore
    383 	local reqs= rels=
    384 
    385 	$DEBUG && cat $_rump_server_ifaces
    386 
    387 	# Try to dump states before destroying interfaces
    388 	for sock in $(cat $_rump_server_socks); do
    389 		export RUMP_SERVER=$sock
    390 		if $DEBUG; then
    391 			output=save:/dev/stdout
    392 		fi
    393 		atf_check -s exit:0 -o $output rump.ifconfig
    394 		atf_check -s exit:0 -o $output rump.netstat -nr
    395 		# XXX still need hijacking
    396 		atf_check -s exit:0 -o $output $HIJACKING rump.netstat -nai
    397 		atf_check -s exit:0 -o $output rump.arp -na
    398 		atf_check -s exit:0 -o $output rump.ndp -na
    399 		atf_check -s exit:0 -o $output $HIJACKING ifmcstat
    400 	done
    401 
    402 	# XXX using pipe doesn't work. See PR bin/51667
    403 	#cat $_rump_server_ifaces | while read sock ifname; do
    404 	# Destroy interfaces in the reverse order
    405 	tac $_rump_server_ifaces > __ifaces
    406 	while read sock ifname; do
    407 		export RUMP_SERVER=$sock
    408 		if rump.ifconfig -l |grep -q $ifname; then
    409 			if $DEBUG; then
    410 				rump.ifconfig -v $ifname
    411 			fi
    412 			atf_check -s exit:0 rump.ifconfig $ifname destroy
    413 		fi
    414 		atf_check -s exit:0 -o ignore rump.ifconfig
    415 	done < __ifaces
    416 	rm -f __ifaces
    417 
    418 	for sock in $(cat $_rump_server_socks); do
    419 		export RUMP_SERVER=$sock
    420 		rump_server_check_memleaks
    421 	done
    422 
    423 	export RUMP_SERVER=$backup
    424 
    425 	return 0
    426 }
    427 
    428 rump_server_halt_servers()
    429 {
    430 	local backup=$RUMP_SERVER
    431 
    432 	$DEBUG && cat $_rump_server_socks
    433 	for sock in $(cat $_rump_server_socks); do
    434 		env RUMP_SERVER=$sock rump.halt
    435 	done
    436 	export RUMP_SERVER=$backup
    437 
    438 	return 0
    439 }
    440 
    441 extract_rump_server_core()
    442 {
    443 
    444 	if [ -f rump_server.core ]; then
    445 		gdb -ex bt /usr/bin/rump_server rump_server.core
    446 		# Extract kernel logs including a panic message
    447 		strings rump_server.core |grep -E '^\[.+\] '
    448 	fi
    449 }
    450 
    451 dump_kernel_stats()
    452 {
    453 	local sock=$1
    454 
    455 	echo "### Dumping $sock"
    456 	export RUMP_SERVER=$sock
    457 	rump.ifconfig -av
    458 	rump.netstat -nr
    459 	# XXX still need hijacking
    460 	$HIJACKING rump.netstat -nai
    461 	# XXX workaround for vmstat with the sgid bit
    462 	cp /usr/bin/vmstat ./vmstat
    463 	$HIJACKING ./vmstat -m
    464 	rm -f ./vmstat
    465 	rump.arp -na
    466 	rump.ndp -na
    467 	$HIJACKING ifmcstat
    468 	$HIJACKING dmesg
    469 }
    470 
    471 rump_server_dump_servers()
    472 {
    473 	local backup=$RUMP_SERVER
    474 
    475 	$DEBUG && cat $_rump_server_socks
    476 	for sock in $(cat $_rump_server_socks); do
    477 		dump_kernel_stats $sock
    478 	done
    479 	export RUMP_SERVER=$backup
    480 
    481 	extract_rump_server_core
    482 	return 0
    483 }
    484 
    485 rump_server_dump_buses()
    486 {
    487 
    488 	if [ ! -f $_rump_server_buses ]; then
    489 		return 0
    490 	fi
    491 
    492 	$DEBUG && cat $_rump_server_buses
    493 	for bus in $(cat $_rump_server_buses); do
    494 		echo "### Dumping $bus"
    495 		shmif_dumpbus -p - $bus 2>/dev/null| tcpdump -n -e -r -
    496 	done
    497 	return 0
    498 }
    499 
    500 cleanup()
    501 {
    502 
    503 	rump_server_halt_servers
    504 }
    505 
    506 dump()
    507 {
    508 
    509 	rump_server_dump_servers
    510 	rump_server_dump_buses
    511 }
    512 
    513 skip_if_qemu()
    514 {
    515 	if sysctl machdep.cpu_brand 2>/dev/null | grep QEMU >/dev/null 2>&1
    516 	then
    517 	    atf_skip "unreliable under qemu, skip until PR kern/43997 fixed"
    518 	fi
    519 }
    520 
    521 test_create_destroy_common()
    522 {
    523 	local sock=$1
    524 	local ifname=$2
    525 	local test_address=${3:-false}
    526 	local ipv4="10.0.0.1/24"
    527 	local ipv6="fc00::1"
    528 
    529 	export RUMP_SERVER=$sock
    530 
    531 	atf_check -s exit:0 rump.ifconfig $ifname create
    532 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    533 
    534 	atf_check -s exit:0 rump.ifconfig $ifname create
    535 	atf_check -s exit:0 rump.ifconfig $ifname up
    536 	atf_check -s exit:0 rump.ifconfig $ifname down
    537 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    538 
    539 	# Destroy while UP
    540 	atf_check -s exit:0 rump.ifconfig $ifname create
    541 	atf_check -s exit:0 rump.ifconfig $ifname up
    542 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    543 
    544 	if ! $test_address; then
    545 		return
    546 	fi
    547 
    548 	# With an IPv4 address
    549 	atf_check -s exit:0 rump.ifconfig $ifname create
    550 	atf_check -s exit:0 rump.ifconfig $ifname inet $ipv4
    551 	atf_check -s exit:0 rump.ifconfig $ifname up
    552 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    553 
    554 	# With an IPv6 address
    555 	atf_check -s exit:0 rump.ifconfig $ifname create
    556 	atf_check -s exit:0 rump.ifconfig $ifname inet6 $ipv6
    557 	atf_check -s exit:0 rump.ifconfig $ifname up
    558 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    559 
    560 	unset RUMP_SERVER
    561 }
    562