Home | History | Annotate | Line # | Download | only in net
net_common.sh revision 1.23
      1 #	$NetBSD: net_common.sh,v 1.23 2017/10/30 15:59:23 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" \
     88 	    rump.netstat -rn
     89 }
     90 
     91 get_linklocal_addr()
     92 {
     93 
     94 	export RUMP_SERVER=${1}
     95 	rump.ifconfig ${2} inet6 |
     96 	    awk "/fe80/ {sub(/%$2/, \"\"); sub(/\\/[0-9]*/, \"\"); print \$2;}"
     97 	unset RUMP_SERVER
     98 
     99 	return 0
    100 }
    101 
    102 get_macaddr()
    103 {
    104 
    105 	env RUMP_SERVER=${1} \
    106 	    rump.ifconfig ${2} |awk '/address/ {print $2;}'
    107 }
    108 
    109 HTTPD_PID=./.__httpd.pid
    110 start_httpd()
    111 {
    112 	local sock=$1
    113 	local ip=$2
    114 	local backup=$RUMP_SERVER
    115 
    116 	export RUMP_SERVER=$sock
    117 
    118 	# start httpd in daemon mode
    119 	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so \
    120 	    /usr/libexec/httpd -P $HTTPD_PID -i $ip -b -s $(pwd)
    121 
    122 	export RUMP_SERVER=$backup
    123 
    124 	sleep 3
    125 }
    126 
    127 stop_httpd()
    128 {
    129 
    130 	if [ -f $HTTPD_PID ]; then
    131 		kill -9 $(cat $HTTPD_PID)
    132 		rm -f $HTTPD_PID
    133 		sleep 1
    134 	fi
    135 }
    136 
    137 NC_PID=./.__nc.pid
    138 start_nc_server()
    139 {
    140 	local sock=$1
    141 	local port=$2
    142 	local outfile=$3
    143 	local proto=${4:-ipv4}
    144 	local backup=$RUMP_SERVER
    145 	local pid= opts=
    146 
    147 	export RUMP_SERVER=$sock
    148 
    149 	if [ $proto = ipv4 ]; then
    150 		opts="-l -4"
    151 	else
    152 		opts="-l -6"
    153 	fi
    154 
    155 	env LD_PRELOAD=/usr/lib/librumphijack.so \
    156 	    nc $opts $port > $outfile &
    157 	pid=$!
    158 	echo $pid > $NC_PID
    159 
    160 	if [ $proto = ipv4 ]; then
    161 		$DEBUG && rump.netstat -a -f inet
    162 	else
    163 		$DEBUG && rump.netstat -a -f inet6
    164 	fi
    165 
    166 	export RUMP_SERVER=$backup
    167 
    168 	sleep 1
    169 }
    170 
    171 stop_nc_server()
    172 {
    173 
    174 	if [ -f $NC_PID ]; then
    175 		kill -9 $(cat $NC_PID)
    176 		rm -f $NC_PID
    177 		sleep 1
    178 	fi
    179 }
    180 
    181 BASIC_LIBS="-lrumpnet -lrumpnet_net -lrumpnet_netinet \
    182     -lrumpnet_shmif -lrumpdev"
    183 FS_LIBS="$BASIC_LIBS -lrumpvfs -lrumpfs_ffs"
    184 CRYPTO_LIBS="$BASIC_LIBS -lrumpvfs -lrumpdev_opencrypto \
    185     -lrumpkern_z -lrumpkern_crypto"
    186 NPF_LIBS="$BASIC_LIBS -lrumpvfs -lrumpdev_bpf -lrumpnet_bpfjit -lrumpnet_npf"
    187 
    188 # We cannot keep variables between test phases, so need to store in files
    189 _rump_server_socks=./.__socks
    190 _rump_server_ifaces=./.__ifaces
    191 _rump_server_buses=./.__buses
    192 
    193 DEBUG_SYSCTL_ENTRIES="net.inet.arp.debug net.inet6.icmp6.nd6_debug \
    194     net.inet.ipsec.debug"
    195 
    196 IPSEC_KEY_DEBUG=${IPSEC_KEY_DEBUG:-false}
    197 
    198 _rump_server_start_common()
    199 {
    200 	local sock=$1
    201 	local libs=
    202 	local backup=$RUMP_SERVER
    203 
    204 	shift 1
    205 	libs="$*"
    206 
    207 	atf_check -s exit:0 rump_server $libs $sock
    208 
    209 	if $DEBUG; then
    210 		# Enable debugging features in the kernel
    211 		export RUMP_SERVER=$sock
    212 		for ent in $DEBUG_SYSCTL_ENTRIES; do
    213 			if rump.sysctl -q $ent; then
    214 				atf_check -s exit:0 rump.sysctl -q -w $ent=1
    215 			fi
    216 		done
    217 		export RUMP_SERVER=$backup
    218 	fi
    219 	if $IPSEC_KEY_DEBUG; then
    220 		# Enable debugging features in the kernel
    221 		export RUMP_SERVER=$sock
    222 		if rump.sysctl -q net.key.debug; then
    223 			atf_check -s exit:0 \
    224 			    rump.sysctl -q -w net.key.debug=0xffff
    225 		fi
    226 		export RUMP_SERVER=$backup
    227 	fi
    228 
    229 	echo $sock >> $_rump_server_socks
    230 	$DEBUG && cat $_rump_server_socks
    231 }
    232 
    233 rump_server_start()
    234 {
    235 	local sock=$1
    236 	local _libs=
    237 	local libs="$BASIC_LIBS"
    238 
    239 	shift 1
    240 	_libs="$*"
    241 
    242 	for lib in $_libs; do
    243 		libs="$libs -lrumpnet_$lib"
    244 	done
    245 
    246 	_rump_server_start_common $sock $libs
    247 
    248 	return 0
    249 }
    250 
    251 rump_server_fs_start()
    252 {
    253 	local sock=$1
    254 	local _libs=
    255 	local libs="$FS_LIBS"
    256 
    257 	shift 1
    258 	_libs="$*"
    259 
    260 	for lib in $_libs; do
    261 		libs="$libs -lrumpnet_$lib"
    262 	done
    263 
    264 	_rump_server_start_common $sock $libs
    265 
    266 	return 0
    267 }
    268 
    269 rump_server_crypto_start()
    270 {
    271 	local sock=$1
    272 	local _libs=
    273 	local libs="$CRYPTO_LIBS"
    274 
    275 	shift 1
    276 	_libs="$*"
    277 
    278 	for lib in $_libs; do
    279 		libs="$libs -lrumpnet_$lib"
    280 	done
    281 
    282 	_rump_server_start_common $sock $libs
    283 
    284 	return 0
    285 }
    286 
    287 rump_server_npf_start()
    288 {
    289 	local sock=$1
    290 	local _libs=
    291 	local libs="$NPF_LIBS"
    292 
    293 	shift 1
    294 	_libs="$*"
    295 
    296 	for lib in $_libs; do
    297 		libs="$libs -lrumpnet_$lib"
    298 	done
    299 
    300 	_rump_server_start_common $sock $libs
    301 
    302 	return 0
    303 }
    304 
    305 rump_server_add_iface()
    306 {
    307 	local sock=$1
    308 	local ifname=$2
    309 	local bus=$3
    310 	local backup=$RUMP_SERVER
    311 
    312 	export RUMP_SERVER=$sock
    313 	atf_check -s exit:0 rump.ifconfig $ifname create
    314 	atf_check -s exit:0 rump.ifconfig $ifname linkstr $bus
    315 	export RUMP_SERVER=$backup
    316 
    317 	echo $sock $ifname >> $_rump_server_ifaces
    318 	$DEBUG && cat $_rump_server_ifaces
    319 
    320 	echo $bus >> $_rump_server_buses
    321 	cat $_rump_server_buses |sort -u >./.__tmp
    322 	mv -f ./.__tmp $_rump_server_buses
    323 	$DEBUG && cat $_rump_server_buses
    324 
    325 	return 0
    326 }
    327 
    328 rump_server_destroy_ifaces()
    329 {
    330 	local backup=$RUMP_SERVER
    331 
    332 	$DEBUG && cat $_rump_server_ifaces
    333 
    334 	# Try to dump states before destroying interfaces
    335 	for sock in $(cat $_rump_server_socks); do
    336 		export RUMP_SERVER=$sock
    337 		atf_check -s exit:0 -o ignore rump.ifconfig
    338 		atf_check -s exit:0 -o ignore rump.netstat -nr
    339 		# XXX still need hijacking
    340 		atf_check -s exit:0 -o ignore $HIJACKING rump.netstat -nai
    341 		atf_check -s exit:0 -o ignore rump.arp -na
    342 		atf_check -s exit:0 -o ignore rump.ndp -na
    343 		atf_check -s exit:0 -o ignore $HIJACKING ifmcstat
    344 	done
    345 
    346 	# XXX using pipe doesn't work. See PR bin/51667
    347 	#cat $_rump_server_ifaces | while read sock ifname; do
    348 	while read sock ifname; do
    349 		export RUMP_SERVER=$sock
    350 		if rump.ifconfig -l |grep -q $ifname; then
    351 			atf_check -s exit:0 rump.ifconfig $ifname destroy
    352 		fi
    353 		atf_check -s exit:0 -o ignore rump.ifconfig
    354 	done < $_rump_server_ifaces
    355 	export RUMP_SERVER=$backup
    356 
    357 	return 0
    358 }
    359 
    360 rump_server_halt_servers()
    361 {
    362 	local backup=$RUMP_SERVER
    363 
    364 	$DEBUG && cat $_rump_server_socks
    365 	for sock in $(cat $_rump_server_socks); do
    366 		env RUMP_SERVER=$sock rump.halt
    367 	done
    368 	export RUMP_SERVER=$backup
    369 
    370 	return 0
    371 }
    372 
    373 rump_server_dump_servers()
    374 {
    375 	local backup=$RUMP_SERVER
    376 
    377 	$DEBUG && cat $_rump_server_socks
    378 	for sock in $(cat $_rump_server_socks); do
    379 		echo "### Dumping $sock"
    380 		export RUMP_SERVER=$sock
    381 		rump.ifconfig -av
    382 		rump.netstat -nr
    383 		# XXX still need hijacking
    384 		$HIJACKING rump.netstat -nai
    385 		rump.arp -na
    386 		rump.ndp -na
    387 		$HIJACKING ifmcstat
    388 		$HIJACKING dmesg
    389 	done
    390 	export RUMP_SERVER=$backup
    391 
    392 	if [ -f rump_server.core ]; then
    393 		gdb -ex bt /usr/bin/rump_server rump_server.core
    394 		strings rump_server.core |grep panic
    395 	fi
    396 	return 0
    397 }
    398 
    399 rump_server_dump_buses()
    400 {
    401 
    402 	if [ ! -f $_rump_server_buses ]; then
    403 		return 0
    404 	fi
    405 
    406 	$DEBUG && cat $_rump_server_buses
    407 	for bus in $(cat $_rump_server_buses); do
    408 		echo "### Dumping $bus"
    409 		shmif_dumpbus -p - $bus 2>/dev/null| tcpdump -n -e -r -
    410 	done
    411 	return 0
    412 }
    413 
    414 cleanup()
    415 {
    416 
    417 	rump_server_halt_servers
    418 }
    419 
    420 dump()
    421 {
    422 
    423 	rump_server_dump_servers
    424 	rump_server_dump_buses
    425 }
    426 
    427 skip_if_qemu()
    428 {
    429 	if sysctl machdep.cpu_brand 2>/dev/null | grep QEMU >/dev/null 2>&1
    430 	then
    431 	    atf_skip "unreliable under qemu, skip until PR kern/43997 fixed"
    432 	fi
    433 }
    434