Home | History | Annotate | Download | only in lib
      1 #!/bin/sh
      2 # Copyright (c) 2014-2015 Oracle and/or its affiliates. All Rights Reserved.
      3 #
      4 # This program is free software; you can redistribute it and/or
      5 # modify it under the terms of the GNU General Public License as
      6 # published by the Free Software Foundation; either version 2 of
      7 # the License, or (at your option) any later version.
      8 #
      9 # This program is distributed in the hope that it would be useful,
     10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 # GNU General Public License for more details.
     13 #
     14 # You should have received a copy of the GNU General Public License
     15 # along with this program; if not, write the Free Software Foundation,
     16 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     17 #
     18 # Author: Alexey Kodanev <alexey.kodanev (at] oracle.com>
     19 #
     20 
     21 [ -z "$TST_LIB_LOADED" ] && . test.sh
     22 
     23 # Run command on remote host.
     24 # Options:
     25 # -b run in background
     26 # -s safe option, if something goes wrong, will exit with TBROK
     27 # -c specify command to run
     28 
     29 tst_rhost_run()
     30 {
     31 	local pre_cmd=
     32 	local post_cmd=' || echo RTERR'
     33 	local out=
     34 	local user="root"
     35 	local cmd=
     36 	local safe=0
     37 
     38 	OPTIND=0
     39 
     40 	while getopts :bsc:u: opt; do
     41 		case "$opt" in
     42 		b)
     43 			pre_cmd="nohup"
     44 			post_cmd=" > /dev/null 2>&1 &"
     45 			out="1> /dev/null"
     46 		;;
     47 		s) safe=1 ;;
     48 		c) cmd=$OPTARG ;;
     49 		u) user=$OPTARG ;;
     50 		*)
     51 			tst_brkm TBROK "tst_rhost_run: unknown option: $OPTARG"
     52 		;;
     53 		esac
     54 	done
     55 
     56 	OPTIND=0
     57 
     58 	if [ -z "$cmd" ]; then
     59 		[ "$safe" -eq 1 ] && \
     60 			tst_brkm TBROK "tst_rhost_run: command not defined"
     61 		tst_resm TWARN "tst_rhost_run: command not defined"
     62 		return 1
     63 	fi
     64 
     65 	local output=
     66 	local ret=0
     67 	if [ -n "$TST_USE_SSH" ]; then
     68 		output=`ssh -n -q $user@$RHOST "sh -c \
     69 			'$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
     70 	else
     71 		output=`rsh -n -l $user $RHOST "sh -c \
     72 			'$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
     73 	fi
     74 	echo "$output" | grep -q 'RTERR$' && ret=1
     75 	if [ $ret -eq 1 ]; then
     76 		output=$(echo "$output" | sed 's/RTERR//')
     77 		[ "$safe" -eq 1 ] && \
     78 			tst_brkm TBROK "'$cmd' failed on '$RHOST': '$output'"
     79 	fi
     80 
     81 	[ -z "$out" -a -n "$output" ] && echo "$output"
     82 
     83 	return $ret
     84 }
     85 
     86 # Get test interface names for local/remote host.
     87 # tst_get_ifaces [TYPE]
     88 # TYPE: { lhost | rhost }; Default value is 'lhost'.
     89 tst_get_ifaces()
     90 {
     91 	local type=${1:-"lhost"}
     92 	if [ "$type" = "lhost" ]; then
     93 		echo "$LHOST_IFACES"
     94 	else
     95 		echo "$RHOST_IFACES"
     96 	fi
     97 }
     98 
     99 # Get HW addresses from defined test interface names.
    100 # tst_get_hwaddrs [TYPE]
    101 # TYPE: { lhost | rhost }; Default value is 'lhost'.
    102 tst_get_hwaddrs()
    103 {
    104 	local type=${1:-"lhost"}
    105 	local addr=
    106 	local list=
    107 
    108 	for eth in $(tst_get_ifaces $type); do
    109 
    110 		local addr_path="/sys/class/net/${eth}/address"
    111 
    112 		case $type in
    113 		lhost) addr=$(cat $addr_path) ;;
    114 		rhost) addr=$(tst_rhost_run -s -c "cat $addr_path")
    115 		esac
    116 
    117 		[ -z "$list" ] && list="$addr" || list="$list $addr"
    118 	done
    119 	echo "$list"
    120 }
    121 
    122 # Get test HW address.
    123 # tst_hwaddr [TYPE] [LINK]
    124 # TYPE: { lhost | rhost }; Default value is 'lhost'.
    125 # LINK: link number starting from 0. Default value is '0'.
    126 tst_hwaddr()
    127 {
    128 	local type=${1:-"lhost"}
    129 	local link_num=${2:-"0"}
    130 	local hwaddrs=
    131 	link_num=$(( $link_num + 1 ))
    132 	[ "$type" = "lhost" ] && hwaddrs=$LHOST_HWADDRS || hwaddrs=$RHOST_HWADDRS
    133 	echo "$hwaddrs" | awk '{ print $'"$link_num"' }'
    134 }
    135 
    136 # Get test interface name.
    137 # tst_iface [TYPE] [LINK]
    138 # TYPE: { lhost | rhost }; Default value is 'lhost'.
    139 # LINK: link number starting from 0. Default value is '0'.
    140 tst_iface()
    141 {
    142 	local type=${1:-"lhost"}
    143 	local link_num=${2:-"0"}
    144 	link_num=$(( $link_num + 1 ))
    145 	echo "$(tst_get_ifaces $type)" | awk '{ print $'"$link_num"' }'
    146 }
    147 
    148 # Blank for an IPV4 test; 6 for an IPV6 test.
    149 TST_IPV6=
    150 
    151 tst_read_opts()
    152 {
    153 	OPTIND=0
    154 	while getopts ":6" opt; do
    155 		case "$opt" in
    156 		6)
    157 			TST_IPV6=6;;
    158 		esac
    159 	done
    160 	OPTIND=0
    161 }
    162 
    163 tst_read_opts $*
    164 
    165 # Get IP address
    166 # tst_ipaddr [TYPE]
    167 # TYPE: { lhost | rhost }; Default value is 'lhost'.
    168 tst_ipaddr()
    169 {
    170 	local type=${1:-"lhost"}
    171 	local ipv=${TST_IPV6:-"4"}
    172 	local tst_host=
    173 
    174 	if [ "$type" = "lhost" ]; then
    175 		eval "tst_host=\$LHOST_IPV${ipv}_HOST"
    176 	else
    177 		eval "tst_host=\$RHOST_IPV${ipv}_HOST"
    178 	fi
    179 
    180 	if [ "$TST_IPV6" ]; then
    181 		echo "${IPV6_NETWORK}:${tst_host}"
    182 	else
    183 		echo "${IPV4_NETWORK}.${tst_host}"
    184 	fi
    185 }
    186 
    187 # tst_init_iface [TYPE] [LINK]
    188 # TYPE: { lhost | rhost }; Default value is 'lhost'.
    189 # LINK: link number starting from 0. Default value is '0'.
    190 tst_init_iface()
    191 {
    192 	local type=${1:-"lhost"}
    193 	local link_num=${2:-"0"}
    194 	local iface=$(tst_iface $type $link_num)
    195 	tst_resm TINFO "initialize '$type' '$iface' interface"
    196 
    197 	if [ "$type" = "lhost" ]; then
    198 		ip link set $iface down || return $?
    199 		ip route flush dev $iface || return $?
    200 		ip addr flush dev $iface || return $?
    201 		ip link set $iface up
    202 		return $?
    203 	fi
    204 
    205 	tst_rhost_run -c "ip link set $iface down" || return $?
    206 	tst_rhost_run -c "ip route flush dev $iface" || return $?
    207 	tst_rhost_run -c "ip addr flush dev $iface" || return $?
    208 	tst_rhost_run -c "ip link set $iface up"
    209 }
    210 
    211 # tst_add_ipaddr [TYPE] [LINK]
    212 # TYPE: { lhost | rhost }; Default value is 'lhost'.
    213 # LINK: link number starting from 0. Default value is '0'.
    214 tst_add_ipaddr()
    215 {
    216 	local type=${1:-"lhost"}
    217 	local link_num=${2:-"0"}
    218 
    219 	local mask=24
    220 	[ "$TST_IPV6" ] && mask=64
    221 
    222 	local iface=$(tst_iface $type $link_num)
    223 
    224 	if [ $type = "lhost" ]; then
    225 		tst_resm TINFO "set local addr $(tst_ipaddr)/$mask"
    226 		ip addr add $(tst_ipaddr)/$mask dev $iface
    227 		return $?
    228 	fi
    229 
    230 	tst_resm TINFO "set remote addr $(tst_ipaddr rhost)/$mask"
    231 	tst_rhost_run -c "ip addr add $(tst_ipaddr rhost)/$mask dev $iface"
    232 }
    233 
    234 # tst_restore_ipaddr [TYPE] [LINK]
    235 # Restore default ip addresses defined in network.sh
    236 # TYPE: { lhost | rhost }; Default value is 'lhost'.
    237 # LINK: link number starting from 0. Default value is '0'.
    238 tst_restore_ipaddr()
    239 {
    240 	local type=${1:-"lhost"}
    241 	local link_num=${2:-"0"}
    242 
    243 	tst_init_iface $type $link_num || return $?
    244 
    245 	local ret=0
    246 	local backup_tst_ipv6=$TST_IPV6
    247 	TST_IPV6= tst_add_ipaddr $type $link_num || ret=$?
    248 	TST_IPV6=6 tst_add_ipaddr $type $link_num || ret=$?
    249 	TST_IPV6=$backup_tst_ipv6
    250 
    251 	return $ret
    252 }
    253 
    254 # tst_netload ADDR [FILE] [TYPE] [OPTS]
    255 # Run network load test
    256 # ADDR: IP address
    257 # FILE: file with result time
    258 # TYPE: PING or TFO (TCP traffic)
    259 # OPTS: additional options
    260 tst_netload()
    261 {
    262 	local ip_addr="$1"
    263 	local rfile=${2:-"netload.res"}
    264 	local type=${3:-"TFO"}
    265 	local addopts=${@:4}
    266 	local ret=0
    267 	clients_num=${clients_num:-"2"}
    268 	client_requests=${client_requests:-"500000"}
    269 	max_requests=${max_requests:-"3"}
    270 
    271 	case "$type" in
    272 	PING)
    273 		local ipv6=
    274 		echo "$ip_addr" | grep ":" > /dev/null
    275 		[ $? -eq 0 ] && ipv6=6
    276 		tst_resm TINFO "run ping${ipv6} test with rhost '$ip_addr'..."
    277 		local res=
    278 		res=$(ping${ipv6} -f -c $client_requests $ip_addr -w 600 2>&1)
    279 		[ $? -ne 0 ] && return 1
    280 		echo $res | sed -nE 's/.*time ([0-9]+)ms.*/\1/p' > $rfile
    281 	;;
    282 	TFO)
    283 		local port=
    284 		port=$(tst_rhost_run -c 'tst_get_unused_port ipv6 stream')
    285 		[ $? -ne 0 ] && tst_brkm TBROK "failed to get unused port"
    286 
    287 		tst_resm TINFO "run tcp_fastopen with '$ip_addr', port '$port'"
    288 		tst_rhost_run -s -b -c "tcp_fastopen -R $max_requests \
    289 			-g $port $addopts"
    290 
    291 		# check that tcp_fastopen on rhost in 'Listening' state
    292 		local sec_waited=
    293 		for sec_waited in $(seq 1 60); do
    294 			tst_rhost_run -c "ss -ltn | grep -q $port" && break
    295 			if [ $sec_waited -eq 60 ]; then
    296 				tst_resm TINFO "rhost not in LISTEN state"
    297 				return 1
    298 			fi
    299 			sleep 1
    300 		done
    301 
    302 		# run local tcp client
    303 		tcp_fastopen -a $clients_num -r $client_requests -l -H $ip_addr\
    304 			 -g $port -d $rfile $addopts > /dev/null || ret=1
    305 
    306 		if [ $ret -eq 0 -a ! -f $rfile ]; then
    307 			tst_brkm TBROK "can't read $rfile"
    308 		fi
    309 
    310 		tst_rhost_run -c "pkill -9 tcp_fastopen\$"
    311 	;;
    312 	*) tst_brkm TBROK "invalid net_load type '$type'" ;;
    313 	esac
    314 
    315 	return $ret
    316 }
    317 
    318 # tst_ping [IFACE] [DST ADDR] [MESSAGE SIZE ARRAY]
    319 # Check icmp connectivity
    320 # IFACE: source interface name
    321 # DST ADDR: destination IPv4 or IPv6 address
    322 # MESSAGE SIZE ARRAY: message size array
    323 tst_ping()
    324 {
    325 	# The max number of ICMP echo request
    326 	PING_MAX=${PING_MAX:-"10"}
    327 
    328 	local src_iface=${1:-"$(tst_iface)"}
    329 	local dst_addr=${2:-"$(tst_ipaddr rhost)"}; shift 2
    330 	local msg_sizes=$@
    331 	local ret=0
    332 
    333 	# ping cmd use 56 as default message size
    334 	for size in ${msg_sizes:-"56"}; do
    335 		ping$TST_IPV6 -I $src_iface -c $PING_MAX $dst_addr \
    336 			-s $size > /dev/null 2>&1
    337 		ret=$?
    338 		if [ $ret -eq 0 ]; then
    339 			tst_resm TINFO "tst_ping IPv${TST_IPV6:-4} msg_size $size pass"
    340 		else
    341 			tst_resm TINFO "tst_ping IPv${TST_IPV6:-4} msg_size $size fail"
    342 			break
    343 		fi
    344 	done
    345 	return $ret
    346 }
    347 
    348 # Management Link
    349 [ -z "$RHOST" ] && tst_brkm TBROK "RHOST variable not defined"
    350 export RHOST="$RHOST"
    351 export PASSWD=${PASSWD:-""}
    352 # Don't use it in new tests, use tst_rhost_run() from test_net.sh instead.
    353 export LTP_RSH=${LTP_RSH:-"rsh -n"}
    354 
    355 # Test Links
    356 # Warning: make sure to set valid interface names and IP addresses below.
    357 # Set names for test interfaces, e.g. "eth0 eth1"
    358 export LHOST_IFACES=${LHOST_IFACES:-"eth0"}
    359 export RHOST_IFACES=${RHOST_IFACES:-"eth0"}
    360 
    361 # Set corresponding HW addresses, e.g. "00:00:00:00:00:01 00:00:00:00:00:02"
    362 export LHOST_HWADDRS=${LHOST_HWADDRS:-"$(tst_get_hwaddrs lhost)"}
    363 export RHOST_HWADDRS=${RHOST_HWADDRS:-"$(tst_get_hwaddrs rhost)"}
    364 
    365 # Set first three octets of the network address, default is '10.0.0'
    366 export IPV4_NETWORK=${IPV4_NETWORK:-"10.0.0"}
    367 # Set local host last octet, default is '2'
    368 export LHOST_IPV4_HOST=${LHOST_IPV4_HOST:-"2"}
    369 # Set remote host last octet, default is '1'
    370 export RHOST_IPV4_HOST=${RHOST_IPV4_HOST:-"1"}
    371 # Set the reverse of IPV4_NETWORK
    372 export IPV4_NET_REV=${IPV4_NET_REV:-"0.0.10"}
    373 # Set first three octets of the network address, default is 'fd00:1:1:1'
    374 export IPV6_NETWORK=${IPV6_NETWORK:-"fd00:1:1:1"}
    375 # Set local host last octet, default is '2'
    376 export LHOST_IPV6_HOST=${LHOST_IPV6_HOST:-":2"}
    377 # Set remote host last octet, default is '1'
    378 export RHOST_IPV6_HOST=${RHOST_IPV6_HOST:-":1"}
    379 # Reverse network portion of the IPv6 address
    380 export IPV6_NET_REV=${IPV6_NET_REV:-"1.0.0.0.1.0.0.0.1.0.0.0.0.0.d.f"}
    381 # Reverse host portion of the IPv6 address of the local host
    382 export LHOST_IPV6_REV=${LHOST_IPV6_REV:-"2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0"}
    383 # Reverse host portion of the IPv6 address of the remote host
    384 export RHOST_IPV6_REV=${RHOST_IPV6_REV:-"1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0"}
    385 
    386 # Networks that aren't reachable through the test links
    387 export IPV4_NET16_UNUSED=${IPV4_NET16_UNUSED:-"10.23"}
    388 export IPV6_NET32_UNUSED=${IPV6_NET32_UNUSED:-"fd00:23"}
    389 
    390 export HTTP_DOWNLOAD_DIR=${HTTP_DOWNLOAD_DIR:-"/var/www/html"}
    391 export FTP_DOWNLOAD_DIR=${FTP_DOWNLOAD_DIR:-"/var/ftp"}
    392 export FTP_UPLOAD_DIR=${FTP_UPLOAD_DIR:-"/var/ftp/pub"}
    393 export FTP_UPLOAD_URLDIR=${FTP_UPLOAD_URLDIR:-"pub"}
    394 
    395 # network/stress tests require additional parameters
    396 export NS_DURATION=${NS_DURATION:-"3600"}
    397 export NS_TIMES=${NS_TIMES:-"10000"}
    398 export CONNECTION_TOTAL=${CONNECTION_TOTAL:-"4000"}
    399 export IP_TOTAL=${IP_TOTAL:-"10000"}
    400 export IP_TOTAL_FOR_TCPIP=${IP_TOTAL_FOR_TCPIP:-"100"}
    401 export ROUTE_TOTAL=${ROUTE_TOTAL:-"10000"}
    402 export MTU_CHANGE_TIMES=${MTU_CHANGE_TIMES:-"1000"}
    403 export IF_UPDOWN_TIMES=${IF_UPDOWN_TIMES:-"10000"}
    404 export DOWNLOAD_BIGFILESIZE=${DOWNLOAD_BIGFILESIZE:-"2147483647"}
    405 export DOWNLOAD_REGFILESIZE=${DOWNLOAD_REGFILESIZE:-"1048576"}
    406 export UPLOAD_BIGFILESIZE=${UPLOAD_BIGFILESIZE:-"2147483647"}
    407 export UPLOAD_REGFILESIZE=${UPLOAD_REGFILESIZE:-"1024"}
    408 export MCASTNUM_NORMAL=${MCASTNUM_NORMAL:-"20"}
    409 export MCASTNUM_HEAVY=${MCASTNUM_HEAVY:-"40000"}
    410 
    411 # More information about network parameters can be found
    412 # in the following document: testcases/network/stress/README
    413