1 #!/bin/sh 2 3 ################################################################################ 4 ## ## 5 ## Copyright (c) International Business Machines Corp., 2005 ## 6 ## ## 7 ## This program is free software; you can redistribute it and#or modify ## 8 ## it under the terms of the GNU General Public License as published by ## 9 ## the Free Software Foundation; either version 2 of the License, or ## 10 ## (at your option) any later version. ## 11 ## ## 12 ## This program is distributed in the hope that it will be useful, but ## 13 ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## 14 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ## 15 ## for more details. ## 16 ## ## 17 ## You should have received a copy of the GNU General Public License ## 18 ## along with this program; if not, write to the Free Software ## 19 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## 20 ## ## 21 ## ## 22 ################################################################################ 23 # 24 # File: 25 # bg_tcp_traffic 26 # 27 # Description: 28 # Control the background TCP traffic 29 # 30 # Author: 31 # Mitsuru Chinen <mitch (at] jp.ibm.com> 32 # 33 # Arguments: 34 # $1: command (make/check/kill/killall) 35 # $2: IP address when the command is make 36 # Process ID when the command is check or kill 37 # 38 # Outputs: 39 # Process ID of the TCP traffic server when the command is make 40 # 41 # Exit Value: 42 # 0: Exit normally 43 # >0: Exit abnormally 44 # 45 # History: 46 # Oct 19 2005 - Created (Mitsuru Chinen) 47 # 48 #----------------------------------------------------------------------- 49 #Uncomment line below for debug output. 50 #trace_logic=${trace_logic:-"set -x"} 51 $trace_logic 52 53 # Make sure the value of LTPROOT 54 LTPROOT=${LTPROOT:-`(cd ../../../../ ; pwd)`} 55 export LTPROOT 56 57 # Check the environmanet variable for the test 58 . check_envval || exit 1 59 60 # Timeout till client starts up [sec] 61 CLIENT_TIMEOUT=10 62 63 64 #----------------------------------------------------------------------- 65 # 66 # Function: usage 67 # 68 # Description: 69 # Print the usage of this script, then exit 70 # 71 #----------------------------------------------------------------------- 72 usage(){ 73 cat << EOD >&2 74 bg_tcp_traffic command [argument] 75 command: make / check / kill / killall 76 argument: IP address of the server (if command is make) 77 Process ID (if command is check/kill) 78 (none) (if command is killall) 79 EOD 80 81 exit 1 82 } 83 84 85 #----------------------------------------------------------------------- 86 # 87 # Function: make_traffic 88 # 89 # Description: 90 # Make a background tcp traffic. 91 # The local host will be a server, and the remote host will be a client 92 # 93 # Arguments: 94 # $1: IP address of the server 95 # 96 # Exit Value: 97 # 0: Success 98 # 1: Fail 99 # 100 #----------------------------------------------------------------------- 101 make_traffic() { 102 server_ipaddr=$1 103 104 # Identify the family is ipv4 or ipv6 105 family=0 106 echo $server_ipaddr | fgrep "." >/dev/null 2>&1 107 if [ $? -eq 0 ]; then 108 family=4 109 else 110 echo $server_ipaddr | fgrep ":" >/dev/null 2>&1 111 if [ $? -eq 0 ]; then 112 family=6 113 fi 114 fi 115 116 if [ $family -eq 0 ]; then 117 echo "The IP address of the server is something wrong." >&2 118 exit 1 119 fi 120 121 # Find the available consecutive ports 122 server_port=`find_portbundle tcp 1025 1` 123 if [ $? -ne 0 ]; then 124 echo "No port is available." >&2 125 exit 1 126 fi 127 128 # Start up a server 129 infofile=`mktemp -p $TMPDIR` 130 ns-tcpserver -b -f $family -p ${server_port} -o $infofile 131 132 while true ; do # Wait till ns-tcpserver outputs the information 133 if [ -s $infofile ]; then 134 break 135 fi 136 done 137 138 server_pid=`fgrep PID: $infofile | awk '{ print $2 }'` 139 140 rm -f $infofile 141 142 if [ x$server_pid = x ]; then 143 echo "TCP traffic server does not run" >&2 144 exit 1 145 fi 146 147 # Start up a client 148 $LTP_RSH $RHOST "${LTPROOT}/testcases/bin/ns-tcpclient -b -S $server_ipaddr -f $family -p $server_port" 149 150 count=0 151 while true ; do 152 $LTP_RSH $RHOST "ps auxw | fgrep -v grep | fgrep -l ns-tcpclient >/dev/null 2>&1" 153 if [ $? -ne 0 ]; then 154 if [ $count -lt $CLIENT_TIMEOUT ]; then 155 count=`expr $count + 1` 156 sleep 1 157 continue 158 else 159 echo "TCP traffic client does not run" >&2 160 kill -SIGHUP $server_pid 161 exit 1 162 fi 163 else 164 # Output the process ID of the server, the finished 165 echo $server_pid 166 exit 0 167 fi 168 done 169 } 170 171 172 #----------------------------------------------------------------------- 173 # 174 # Function: check_traffic 175 # 176 # Description: 177 # Check the TCP traffic exists 178 # 179 # Arguments: 180 # $1: Process ID of the TCP traffic server 181 # 182 # Exit Value: 183 # 0: The connectivity is good. 184 # 1: The connectivity is something wrong. 185 # 186 #----------------------------------------------------------------------- 187 check_traffic() 188 { 189 server_pid=$1 190 191 if [ ! -d /proc/$server_pid ]; then 192 echo "TCP traffic server has gone." >&2 193 exit 1 194 fi 195 exit 0 196 } 197 198 199 #----------------------------------------------------------------------- 200 # 201 # Function: kill_traffic 202 # 203 # Description: 204 # Kill the TCP traffic 205 # 206 # Arguments: 207 # $1: Process ID of the TCP traffic server 208 # 209 # Exit Value: 210 # Always 0 211 # 212 #----------------------------------------------------------------------- 213 kill_traffic() 214 { 215 server_pid=$1 216 217 kill -SIGHUP $server_pid >/dev/null 2>&1 218 exit 0 219 } 220 221 222 #----------------------------------------------------------------------- 223 # 224 # Function: killall_traffic 225 # 226 # Description: 227 # Kill all of the TCP traffic 228 # 229 # Exit Value: 230 # Always 0 231 # 232 #----------------------------------------------------------------------- 233 killall_traffic() 234 { 235 killall_tcp_traffic 236 } 237 238 # 239 # Main 240 # 241 command=$1 242 243 case $command in 244 make) 245 if [ $# -ne 2 ]; then 246 usage 247 fi 248 make_traffic $2 249 ;; 250 251 check) 252 if [ $# -ne 2 ]; then 253 usage 254 fi 255 check_traffic $2 256 ;; 257 258 kill) 259 if [ $# -ne 2 ]; then 260 usage 261 fi 262 kill_traffic $2 263 ;; 264 265 killall) 266 if [ $# -ne 1 ]; then 267 usage 268 fi 269 killall_traffic $2 270 ;; 271 272 *) 273 usage 274 ;; 275 esac 276