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