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#   tcp4-multi-sameport01
26#
27# Description:
28#   Verify that the kernel is not crashed with multiple connection to the
29#   same port with the following condition:
30#     - The version of IP is IPv4
31#     - Network is not delayed
32#     - IPsec is not used
33#
34#   *) This script may be read by the other test case
35#
36# Setup:
37#   See testcases/network/stress/README
38#
39# Author:
40#   Mitsuru Chinen <mitch@jp.ibm.com>
41#
42# History:
43#	Oct 19 2005 - Created (Mitsuru Chinen)
44#
45#-----------------------------------------------------------------------
46# Uncomment line below for debug output.
47#trace_logic=${trace_logic:-"set -x"}
48$trace_logic
49
50# The test case ID, the test case count and the total number of test case
51TCID=${TCID:-tcp4-multi-sameport01}
52TST_TOTAL=1
53TST_COUNT=1
54export TCID
55export TST_COUNT
56export TST_TOTAL
57
58# Test description
59tst_resm TINFO "Verify that the kernel is not crashed with multiple connection to the same port."
60
61# Make sure the value of LTPROOT
62LTPROOT=${LTPROOT:-`(cd ../../../../.. ; pwd)`}
63export LTPROOT
64
65# Check the environmanet variable
66. check_envval || exit $TST_TOTAL
67
68# Dulation of the test [sec]
69NS_DURATION=${NS_DURATION:-3600}      # 1 hour
70
71# Quantity of the connection for multi connection test
72CONNECTION_TOTAL=${CONNECTION_TOTAL:-4000}
73
74#The number of the test link where tests run
75LINK_NUM=${LINK_NUM:-0}
76
77# The version of IP
78IP_VER=${IP_VER:-4}
79
80# true, if ipsec is used
81DO_IPSEC=${DO_IPSEC:-false}
82
83# The value of SPI
84SPI=${SPI:-1000}
85
86# IPsec Protocol ( ah / esp / ipcomp )
87IPSEC_PROTO=${IPSEC_PROTO:-ah}
88
89# IPsec Mode ( transport / tunnel )
90IPSEC_MODE=${IPSEC_MODE:-transport}
91
92# true, if network is delayed
93DO_NET_DELAY=${DO_NET_DELAY:-false}
94
95# Amount of network delay [ms]
96NET_DELAY=${NET_DELAY:-600}
97
98# The deflection of network delay [ms]
99NET_DELAY_DEFL=${NET_DELAY_DEFL:-200}
100
101
102#-----------------------------------------------------------------------
103#
104# Function: do_cleanup
105#
106# Description:
107#   Recover the system configuration
108#
109#-----------------------------------------------------------------------
110do_cleanup()
111{
112    # Unset SAD/SPD
113    output_ipsec_conf flush | setkey -c >/dev/null 2>&1
114    $LTP_RSH $RHOST ${LTPROOT}/'testcases/bin/output_ipsec_conf flush | PATH=/sbin:/usr/sbin:$PATH setkey -c' >/dev/null 2>&1
115
116    # Unset network delay
117    if [ x$rhost_ifname = x ]; then
118	rhost_ifname=`get_ifname rhost $LINK_NUM`
119    fi
120    $LTP_RSH $RHOST "PATH=/sbin:/usr/sbin:$PATH tc qdisc del dev $rhost_ifname root netem" >/dev/null 2>&1
121
122    # Clean up each interface
123    initialize_if lhost ${LINK_NUM}
124    initialize_if rhost ${LINK_NUM}
125
126    # Kill the tcp traffic server
127    killall_tcp_traffic
128}
129
130
131#-----------------------------------------------------------------------
132#
133# Setup
134#
135
136# Unset the maximum number of processes
137ulimit -u unlimited
138
139# Output the informaion
140tst_resm TINFO "- Test duration is $NS_DURATION [sec]"
141tst_resm TINFO "- Target number of the connection is $CONNECTION_TOTAL"
142tst_resm TINFO "- Version of IP is IPv${IP_VER}"
143
144if $DO_NET_DELAY ; then
145    message=`check_netem`
146    if [ $? -ne 0 ]; then
147	tst_resm TBROK "$message"
148	exit 1
149    fi
150    tst_resm TINFO "- Network delay is ${NET_DELAY}ms +/- ${NET_DELAY_DEFL}ms"
151fi
152
153if $DO_IPSEC ; then
154    message=`check_setkey`
155    if [ $? -ne 0 ]; then
156	tst_resm TBROK "$message"
157	exit 1
158    fi
159
160    case $IPSEC_PROTO in
161	ah)
162	tst_resm TINFO "- IPsec [ AH / $IPSEC_MODE ]"
163	;;
164	esp)
165	tst_resm TINFO "- IPsec [ ESP / $IPSEC_MODE ]"
166	;;
167	ipcomp)
168	tst_resm TINFO "- IPcomp [ $IPSEC_MODE ]"
169	;;
170    esac
171fi
172
173# name of interface of the local/remote host
174lhost_ifname=`get_ifname lhost $LINK_NUM`
175if [ $? -ne 0 ]; then
176    tst_resm TBROK "Failed to get the interface name at the local host"
177    exit $TST_TOTAL
178fi
179
180rhost_ifname=`get_ifname rhost $LINK_NUM`
181if [ $? -ne 0 ]; then
182    tst_resm TBROK "Failed to get the interface name at the remote host"
183    exit $TST_TOTAL
184fi
185
186# Initialize the system configuration
187do_cleanup
188
189# Call do_cleanup function before exit
190trap do_cleanup 0
191
192case $IP_VER in
193    4)
194    # Network portion of the IPv4 address
195    network_part=${IPV4_NETWORK:-"10.0.0"}
196
197    # Netmask of the IPv4 network
198    network_mask=24
199
200    # Host portion of the IPv4 address
201    lhost_host_part=${LHOST_IPV4_HOST:-"2"}     # local host
202    rhost_host_part=${RHOST_IPV4_HOST:-"1"}     # remote host
203
204    # Set IPv4 addresses to the interfaces
205    set_ipv4addr lhost $LINK_NUM $network_part $lhost_host_part
206    if [ $? -ne 0 ]; then
207	tst_resm TBROK "Failed to add any IP address at the local host"
208	exit 1
209    fi
210    set_ipv4addr rhost $LINK_NUM $network_part $rhost_host_part
211    if [ $? -ne 0 ]; then
212	tst_resm TBROK "Failed to add any IP address at the remote host"
213	exit 1
214    fi
215
216    # IPv4 address of the local/remote host
217    lhost_addr="${network_part}.${lhost_host_part}"
218    rhost_addr="${network_part}.${rhost_host_part}"
219    ;;
220
221    6)
222    # Network portion of the IPv6 address
223    network_part="fd00:1:1:1"
224
225    # Netmask of the IPv6 network
226    network_mask=64
227
228    # Host portion of the IPv6 address
229    lhost_host_part=":2"     # local host
230    rhost_host_part=":1"     # remote host
231
232    # Set IPv6 addresses to the interfaces
233    add_ipv6addr lhost $LINK_NUM $network_part $lhost_host_part
234    if [ $? -ne 0 ]; then
235	tst_resm TBROK "Failed to add any IP address at the local host"
236	exit 1
237    fi
238
239    add_ipv6addr rhost $LINK_NUM $network_part $rhost_host_part
240    if [ $? -ne 0 ]; then
241	tst_resm TBROK "Failed to add any IP address at the remote host"
242	exit 1
243    fi
244
245    # IPv6 address of the local/remote host
246    lhost_addr="${network_part}:${lhost_host_part}"
247    rhost_addr="${network_part}:${rhost_host_part}"
248    ;;
249
250    *)
251    tst_resm TBROK "Unknown IP version"
252    ;;
253esac
254
255# Make the network delay
256if $DO_NET_DELAY ; then
257    ret=`$LTP_RSH $RHOST 'PATH=/sbin:/usr/sbin:$PATH tc' qdisc add dev $rhost_ifname root netem delay ${NET_DELAY}ms ${NET_DELAY_DEFL}ms distribution normal' ; echo $?'`
258    if [ $ret -ne 0 ]; then
259	tst_resm TBROK "Failed to make the delayed network"
260	exit 1
261    fi
262fi
263
264# Configure SAD/SPD
265if $DO_IPSEC ; then
266    ipsec_log=`mktemp -p $TMPDIR`
267
268    output_ipsec_conf src \
269	$IPSEC_PROTO $IPSEC_MODE $SPI $lhost_addr $rhost_addr \
270	    |  setkey -c 2>&1 | tee $ipsec_log
271    if [ $? -ne 0 -o -s $ipsec_log ]; then
272	tst_resm TBROK "Failed to configure SAD/SPD on the local host."
273	rm -f $ipsec_log
274	exit 1
275    fi
276
277    $LTP_RSH $RHOST ${LTPROOT}/testcases/bin/output_ipsec_conf dst $IPSEC_PROTO $IPSEC_MODE $SPI $lhost_addr $rhost_addr' | PATH=/sbin:/usr/sbin:$PATH setkey -c' 2>&1 | tee $ipsec_log
278    if [ $? -ne 0 -o -s $ipsec_log ]; then
279	tst_resm TBROK "Failed to configure SAD/SPD on the remote host."
280	rm -f $ipsec_log
281	exit 1
282    fi
283    rm -f $ipsec_log
284fi
285
286# Make sure the connectvity
287case $IP_VER in
288    4)
289    ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv4_connectivity $rhost_ifname $lhost_addr' ; echo $?'`
290    if [ $ret -ne 0 ]; then
291	tst_resm TBROK "There is no IPv4 connectivity."
292	exit 1
293    fi
294    ;;
295
296    6)
297    ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv6_connectivity $rhost_ifname $lhost_addr' ; echo $?'`
298    if [ $ret -ne 0 ]; then
299	tst_resm TBROK "There is no IPv6 connectivity."
300	exit 1
301    fi
302    ;;
303esac
304
305
306#-----------------------------------------------------------------------
307#
308# Main
309#
310#
311
312# Find the available consecutive ports
313server_port=`find_portbundle tcp 1025 1`
314if [ $? -ne 0 ]; then
315    tst_resm TBROK "No port is available."
316    exit 1
317fi
318
319# Run a server
320info_file=`mktemp -p $TMPDIR`
321ns-tcpserver -b -c -f $IP_VER -o $info_file -p $server_port
322if [ $? -ne 0 ]; then
323    tst_resm TFAIL "Failed to run tcp traffic server."
324    rm -f $info_file
325    exit 1
326fi
327
328while true ; do
329    if [ -s $info_file ]; then
330	break
331    fi
332done
333
334server_pid=`grep PID: $info_file | cut -f 2 -d ' '`
335rm -f $info_file
336
337# Making connections
338connection_num=0
339while [ $connection_num -lt $CONNECTION_TOTAL ]; do
340    # Run a client
341    ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/ns-tcpclient -b -f $IP_VER -S $lhost_addr -p $server_port' ; echo $?'`
342    if [ $ret -ne 0 ]; then
343	# Failed to start any client
344	if [ $connection_num -eq 0 ]; then
345	    tst_resm TFAIL "Failed to run any client"
346	    exit 1
347	fi
348	# Failed to start a client
349	tst_resm TINFO "$connection_num seems the maximum number of the client"
350	break
351    fi
352    connection_num=`expr $connection_num + 1`
353done
354
355# Watch the TCP traffic server
356start_epoc=`date +%s`
357while true ; do
358    current_epoc=`date +%s`
359    elapse_epoc=`expr $current_epoc - $start_epoc`
360
361    if [ $elapse_epoc -ge $NS_DURATION ]; then
362	killall -SIGHUP ns-tcpserver
363	break
364    else
365	ps auxw | fgrep ns-tcpserver | fgrep -l $server_pid >/dev/null 2>&1
366	if [ $? -ne 0 ]; then
367	    tst_resm TFAIL "tcp traffic server is dead in $elapse_epoc [sec]"
368	    exit 1
369	fi
370    fi
371    sleep 1
372done
373
374
375#-----------------------------------------------------------------------
376#
377# Clean up
378#
379
380tst_resm TPASS "Test is finished successfully."
381exit 0
382