Home | History | Annotate | Line # | Download | only in net
net_common.sh revision 1.34
      1 #	$NetBSD: net_common.sh,v 1.34 2019/08/19 03:22:47 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 	reqs=$($HIJACKING vmstat -mv | awk "/$target/ {print \$3;}")
    345 	rels=$($HIJACKING vmstat -mv | awk "/$target/ {print \$5;}")
    346 	atf_check_equal '$target$reqs' '$target$rels'
    347 }
    348 
    349 
    350 rump_server_check_memleaks()
    351 {
    352 
    353 	rump_server_check_poolleaks llentrypl
    354 	# This doesn't work for objects allocated through pool_cache
    355 	#rump_server_check_poolleaks mbpl
    356 	#rump_server_check_poolleaks mclpl
    357 	#rump_server_check_poolleaks socket
    358 }
    359 
    360 rump_server_destroy_ifaces()
    361 {
    362 	local backup=$RUMP_SERVER
    363 	local output=ignore
    364 	local reqs= rels=
    365 
    366 	$DEBUG && cat $_rump_server_ifaces
    367 
    368 	# Try to dump states before destroying interfaces
    369 	for sock in $(cat $_rump_server_socks); do
    370 		export RUMP_SERVER=$sock
    371 		if $DEBUG; then
    372 			output=save:/dev/stdout
    373 		fi
    374 		atf_check -s exit:0 -o $output rump.ifconfig
    375 		atf_check -s exit:0 -o $output rump.netstat -nr
    376 		# XXX still need hijacking
    377 		atf_check -s exit:0 -o $output $HIJACKING rump.netstat -nai
    378 		atf_check -s exit:0 -o $output rump.arp -na
    379 		atf_check -s exit:0 -o $output rump.ndp -na
    380 		atf_check -s exit:0 -o $output $HIJACKING ifmcstat
    381 	done
    382 
    383 	# XXX using pipe doesn't work. See PR bin/51667
    384 	#cat $_rump_server_ifaces | while read sock ifname; do
    385 	# Destroy interfaces in the reverse order
    386 	tac $_rump_server_ifaces > __ifaces
    387 	while read sock ifname; do
    388 		export RUMP_SERVER=$sock
    389 		if rump.ifconfig -l |grep -q $ifname; then
    390 			atf_check -s exit:0 rump.ifconfig $ifname destroy
    391 		fi
    392 		atf_check -s exit:0 -o ignore rump.ifconfig
    393 	done < __ifaces
    394 	rm -f __ifaces
    395 
    396 	for sock in $(cat $_rump_server_socks); do
    397 		export RUMP_SERVER=$sock
    398 		rump_server_check_memleaks
    399 	done
    400 
    401 	export RUMP_SERVER=$backup
    402 
    403 	return 0
    404 }
    405 
    406 rump_server_halt_servers()
    407 {
    408 	local backup=$RUMP_SERVER
    409 
    410 	$DEBUG && cat $_rump_server_socks
    411 	for sock in $(cat $_rump_server_socks); do
    412 		env RUMP_SERVER=$sock rump.halt
    413 	done
    414 	export RUMP_SERVER=$backup
    415 
    416 	return 0
    417 }
    418 
    419 extract_rump_server_core()
    420 {
    421 
    422 	if [ -f rump_server.core ]; then
    423 		gdb -ex bt /usr/bin/rump_server rump_server.core
    424 		# Extract kernel logs including a panic message
    425 		strings rump_server.core |grep -E '^\[.+\] '
    426 	fi
    427 }
    428 
    429 dump_kernel_stats()
    430 {
    431 	local sock=$1
    432 
    433 	echo "### Dumping $sock"
    434 	export RUMP_SERVER=$sock
    435 	rump.ifconfig -av
    436 	rump.netstat -nr
    437 	# XXX still need hijacking
    438 	$HIJACKING rump.netstat -nai
    439 	$HIJACKING vmstat -m
    440 	rump.arp -na
    441 	rump.ndp -na
    442 	$HIJACKING ifmcstat
    443 	$HIJACKING dmesg
    444 }
    445 
    446 rump_server_dump_servers()
    447 {
    448 	local backup=$RUMP_SERVER
    449 
    450 	$DEBUG && cat $_rump_server_socks
    451 	for sock in $(cat $_rump_server_socks); do
    452 		dump_kernel_stats $sock
    453 	done
    454 	export RUMP_SERVER=$backup
    455 
    456 	extract_rump_server_core
    457 	return 0
    458 }
    459 
    460 rump_server_dump_buses()
    461 {
    462 
    463 	if [ ! -f $_rump_server_buses ]; then
    464 		return 0
    465 	fi
    466 
    467 	$DEBUG && cat $_rump_server_buses
    468 	for bus in $(cat $_rump_server_buses); do
    469 		echo "### Dumping $bus"
    470 		shmif_dumpbus -p - $bus 2>/dev/null| tcpdump -n -e -r -
    471 	done
    472 	return 0
    473 }
    474 
    475 cleanup()
    476 {
    477 
    478 	rump_server_halt_servers
    479 }
    480 
    481 dump()
    482 {
    483 
    484 	rump_server_dump_servers
    485 	rump_server_dump_buses
    486 }
    487 
    488 skip_if_qemu()
    489 {
    490 	if sysctl machdep.cpu_brand 2>/dev/null | grep QEMU >/dev/null 2>&1
    491 	then
    492 	    atf_skip "unreliable under qemu, skip until PR kern/43997 fixed"
    493 	fi
    494 }
    495 
    496 test_create_destroy_common()
    497 {
    498 	local sock=$1
    499 	local ifname=$2
    500 	local test_address=${3:-false}
    501 	local ipv4="10.0.0.1/24"
    502 	local ipv6="fc00::1"
    503 
    504 	export RUMP_SERVER=$sock
    505 
    506 	atf_check -s exit:0 rump.ifconfig $ifname create
    507 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    508 
    509 	atf_check -s exit:0 rump.ifconfig $ifname create
    510 	atf_check -s exit:0 rump.ifconfig $ifname up
    511 	atf_check -s exit:0 rump.ifconfig $ifname down
    512 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    513 
    514 	# Destroy while UP
    515 	atf_check -s exit:0 rump.ifconfig $ifname create
    516 	atf_check -s exit:0 rump.ifconfig $ifname up
    517 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    518 
    519 	if ! $test_address; then
    520 		return
    521 	fi
    522 
    523 	# With an IPv4 address
    524 	atf_check -s exit:0 rump.ifconfig $ifname create
    525 	atf_check -s exit:0 rump.ifconfig $ifname inet $ipv4
    526 	atf_check -s exit:0 rump.ifconfig $ifname up
    527 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    528 
    529 	# With an IPv6 address
    530 	atf_check -s exit:0 rump.ifconfig $ifname create
    531 	atf_check -s exit:0 rump.ifconfig $ifname inet6 $ipv6
    532 	atf_check -s exit:0 rump.ifconfig $ifname up
    533 	atf_check -s exit:0 rump.ifconfig $ifname destroy
    534 
    535 	unset RUMP_SERVER
    536 }
    537