1#!/bin/sh
2
3################################################################################
4##                                                                            ##
5## Copyright (c) International Business Machines  Corp., 2006                 ##
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#   route4-change-if
26#
27# Description:
28#   Verify the kernel is not crashed when the interface of an IPv4 route is
29#   changed frequently
30#    test01 - by route command
31#    test02 - by ip command
32#
33# Setup:
34#   See testcases/network/stress/README
35#
36# Author:
37#   Mitsuru Chinen <mitch@jp.ibm.com>
38#
39# History:
40#	Mar 17 2006 - Created (Mitsuru Chinen)
41#
42#-----------------------------------------------------------------------
43# Uncomment line below for debug output.
44#trace_logic=${trace_logic:-"set -x"}
45$trace_logic
46
47# Make sure the value of LTPROOT
48LTPROOT=${LTPROOT:-`(cd ../../../../ ; pwd)`}
49export LTPROOT
50
51# Total number of the test case
52TST_TOTAL=2
53export TST_TOTAL
54
55# Default of the test case ID and the test case count
56TCID=route4-change-if
57TST_COUNT=0
58export TCID
59export TST_COUNT
60
61# Check the environmanet variable
62. check_envval || exit $TST_TOTAL
63
64# The number of times where route is changed
65NS_TIMES=${NS_TIMES:-10000}
66
67# The first 2 ocnted of the Network portion of the gateway address
68IPV4_NETWORK_PRE=${IPV4_NETWORK_PRE:-"10.0"}
69
70# Netmask of for the gateway
71IPV4_NETMASK_NUM=24
72
73# Host portion of the IPv4 address
74LHOST_IPV4_HOST=${LHOST_IPV4_HOST:-"2"}	# src
75RHOST_IPV4_HOST=${RHOST_IPV4_HOST:-"1"}	# gateway
76
77# The destination network
78DST_NETWORK="10.10.0"	# destination network would be 10.10.0.0/24
79DST_HOST="5"
80DST_PORT="7"
81
82
83#-----------------------------------------------------------------------
84#
85# NAME:
86#   do_setup
87#
88# DESCRIPTION:
89#   Make a IPv4 connectivity
90#
91# SET VALUES:
92#   rhost_ipv4addr	- IPv4 Address of the remote host
93#   lhost_ifname	- Interface name of the local host
94#   rhost_ifname	- Interface name of the remote host
95#
96#-----------------------------------------------------------------------
97do_setup()
98{
99    TCID=route4-change-if
100    TST_COUNT=0
101
102    # Get the number of the test links
103    link_total=`echo $LHOST_HWADDRS | wc -w`
104    rhost_link_total=`echo $RHOST_HWADDRS | wc -w`
105    if [ $link_total -ne $rhost_link_total ]; then
106	tst_resm TBROK "The number of element in LHOST_HWADDRS differs from RHOST_HWADDRS"
107	exit $TST_TOTAL
108    fi
109    if [ $link_total -lt 2 ]; then
110	tst_resm TBROK "This test case requires plural Test Links"
111	exit $TST_TOTAL
112    fi
113
114    lhost_ifname_array=""
115    rhost_ifname_array=""
116    link_num=0
117    while [ $link_num -lt $link_total ]; do
118	# Get the Interface names of the local host
119	lhost_ifname=`get_ifname lhost ${link_num}`
120	if [ $? -ne 0 ]; then
121	    tst_resm TBROK "Failed to get the interface name at the local host"
122	    exit $TST_TOTAL
123	fi
124	lhost_ifname_array="$lhost_ifname_array $lhost_ifname"
125
126	# Get the Interface names of the remote host
127	rhost_ifname=`get_ifname rhost ${link_num}`
128	if [ $? -ne 0 ]; then
129	    tst_resm TBROK "Failed to get the interface name at the remote host"
130	    exit $TST_TOTAL
131	fi
132	rhost_ifname_array="$rhost_ifname_array $rhost_ifname"
133
134	# Initialize the interfaces of the remote host
135	initialize_if rhost ${link_num}
136
137	# Set IPv4 address to the interface of the remote host
138	set_ipv4addr rhost ${link_num} "${IPV4_NETWORK_PRE}.${link_num}" ${RHOST_IPV4_HOST}
139	if [ $? -ne 0 ]; then
140	    tst_resm TBROK "Failed to assign IP address to the interface $rhost_ifname at the remote host"
141	    exit $TST_TOTAL
142	fi
143
144	link_num=`expr $link_num + 1`
145    done
146}
147
148
149
150
151#-----------------------------------------------------------------------
152#
153# NAME:
154#   do_cleanup
155#
156# DESCRIPTION:
157#   Recover the tested interfaces
158#
159#-----------------------------------------------------------------------
160do_cleanup()
161{
162    # Make sure to kill the udp datagram sender
163    killall -SIGHUP ns-udpsender >/dev/null 2>&1
164
165    # Initialize the interfaces
166    link_num=0
167    while [ $link_num -lt $link_total ]; do
168	initialize_if lhost ${link_num}
169	initialize_if rhost ${link_num}
170	link_num=`expr $link_num + 1`
171    done
172}
173
174
175#-----------------------------------------------------------------------
176#
177# FUNCTION:
178#   test_body
179#
180# DESCRIPTION:
181#   main code of the test
182#
183# Arguments:
184#   $1: define the test type
185#       1 - route command case
186#       2 - ip command case
187#
188#-----------------------------------------------------------------------
189test_body()
190{
191    test_type=$1
192
193    TCID=route4-change-if0${test_type}
194    TST_COUNT=$test_type
195
196    case $test_type in
197	1)
198	test_command="route"
199	;;
200	2)
201	test_command="ip"
202	;;
203	*)
204	tst_resm TBROK "unspecified case"
205	return 1
206	;;
207    esac
208
209    tst_resm TINFO "Verify the kernel is not crashed when the interface of an IPv4 route is changed frequently by $test_command command in $NS_TIMES times"
210
211    link_num=0
212    while [ $link_num -lt $link_total ]; do
213	# Initialize the interface of the local host
214	initialize_if lhost ${link_num}
215
216	# Assign IPv4 address to the interface of the local host
217	set_ipv4addr lhost ${link_num} "${IPV4_NETWORK_PRE}.${link_num}" ${LHOST_IPV4_HOST}
218	if [ $? -ne 0 ]; then
219	    tst_resm TBROK "Failed to assign an IPv4 address at the local host"
220	    return 1
221	fi
222
223	# Check the connectivity to the gateway
224	field=`expr $link_num + 1`
225	lhost_ifname=`echo $lhost_ifname_array | cut -d ' ' -f $field`
226	check_icmpv4_connectivity $lhost_ifname "${IPV4_NETWORK_PRE}.${link_num}.${LHOST_IPV4_HOST}"
227	if [ $? -ne 0 ]; then
228	    tst_resm TBROK "Test Link $link_num is somthing wrong."
229	    return 1
230	fi
231	link_num=`expr $link_num + 1`
232    done
233
234    # Set the variables regarding the destination host
235    dst_addr=${DST_NETWORK}.${DST_HOST}
236    dst_network=${DST_NETWORK}.0
237
238    # Set the first route
239    link_num=0
240    field=`expr $link_num + 1`
241    lhost_ifname=`echo $lhost_ifname_array | cut -d ' ' -f $field`
242    gateway="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
243    case $test_type in
244	1)
245	route add -net $dst_network netmask 255.255.255.0 gw $gateway dev $lhost_ifname
246	;;
247	2)
248	ip route add ${dst_network}/24 via $gateway dev $lhost_ifname
249	;;
250    esac
251
252    # Load the route with UDP traffic
253    ns-udpsender -f 4 -D $dst_addr -p $DST_PORT -b -s 1472
254    if [ $? -ne 0 ]; then
255	tst_resm TFAIL "Failed to run a UDP datagram sender"
256	return 1
257    fi
258
259    # Loop for changing the route
260    cnt=0
261    while [ $cnt -lt $NS_TIMES ]; do
262	link_num=`expr $link_num + 1`
263	if [ $link_num -ge $link_total ]; then
264	    link_num=0
265	fi
266
267	pre_lhost_ifname=$lhost_ifname
268	pre_gateway=$gateway
269
270	field=`expr $link_num + 1`
271	lhost_ifname=`echo $lhost_ifname_array | cut -d ' ' -f $field`
272	gateway="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
273
274	case $test_type in
275	    1)
276	    route add -net $dst_network netmask 255.255.255.0 gw $gateway dev $lhost_ifname
277	    route del -net $dst_network netmask 255.255.255.0 gw $pre_gateway dev $pre_lhost_ifname
278	    ;;
279	    2)
280	    ip route change ${dst_network}/24 via $gateway dev $lhost_ifname
281	    ;;
282	esac
283	if [ $? -ne 0 ]; then
284	    tst_resm TFAIL "Failed to change the gateway to $gateway"
285	    return 1
286	fi
287
288	# Rerun if udp datagram sender is dead
289	ps auxw | fgrep -v grep | grep ns-udpsender > /dev/null
290	if [ $? -ne 0 ]; then
291	    ns-udpsender -f 4 -D $dst_addr -p $DST_PORT -b -s 1472
292	    if [ $? -ne 0 ]; then
293		tst_resm TFAIL "Failed to run a UDP datagram sender"
294		return 1
295	    fi
296	fi
297
298	cnt=`expr $cnt + 1`
299    done
300
301    # Kill the udp datagram sender
302    killall -SIGHUP ns-udpsender
303
304    tst_resm TPASS "Test is finished correctly."
305    return 0
306}
307
308
309#-----------------------------------------------------------------------
310#
311# Main
312#
313# Exit Value:
314#   The number of the failure
315#
316#-----------------------------------------------------------------------
317
318RC=0
319do_setup
320test_body 1 || RC=`expr $RC + 1`      # Case of route command
321test_body 2 || RC=`expr $RC + 1`      # Case of ip command
322do_cleanup
323
324exit $RC
325