#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   if4-alias-addlarge
#
# Description:
#   Verify the IPv4 connectivity isn't broken with creating a large number
#   of IPv4-aliases
#    test01 - by `ifconfig add' style
#    test02 - by `ifconfig ethn:n' style
#    test03 - by ip command
#
# Setup:
#   See ltp-yyyymmdd/testcases/network/stress/README
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# History:
#	Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
# Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# Make sure the value of LTPROOT
LTPROOT=${LTPROOT:-`(cd ../../../../ ; pwd)`}
export LTPROOT

# Total number of the test case
TST_TOTAL=3
export TST_TOTAL

# Default of the test case ID and the test case count
TCID=if4-alias-addlarge
TST_COUNT=0
export TCID
export TST_COUNT

# Check the environmanet variable
. check_envval || exit $TST_TOTAL

# The number of the add IPv4 alias
IP_TOTAL=${IP_TOTAL:-10000}

# The interval of the check interface activity
CHECK_INTERVAL=${CHECK_INTERVAL:-`expr $IP_TOTAL \/ 100`}

# The number of the test link where tests run
LINK_NUM=0

# Network portion of the IPv4 address
IPV4_NETWORK=${IPV4_NETWORK:-"10"}

# Netmask of for the tested network
IPV4_NETMASK="255.0.0.0"
IPV4_NETMASK_NUM=8

# Broadcast address of the tested network
IPV4_BROADCAST=${IPV4_NETWORK}.255.255.255

# Host portion of the IPv4 address
LHOST_IPV4_HOST=${LHOST_IPV4_HOST:-"0.0.2"}
RHOST_IPV4_HOST=${RHOST_IPV4_HOST:-"0.0.1"}



#-----------------------------------------------------------------------
#
# NAME:
#   do_setup
#
# DESCRIPTION:
#   Make a IPv4 connectivity
#
# SET VALUES:
#   lhost_ifname	- Interface name of the local host
#   rhost_ifname	- Interface name of the local host
#   rhost_ipv4addr	- IP address of the remote host
#
#-----------------------------------------------------------------------
do_setup()
{
    TCID=if4-alias-addlarge
    TST_COUNT=0

    # Make sure the TCP traffic server/client doesn't run
    bg_tcp_traffic killall

    # Initialize the interface at the remote host
    initialize_if rhost ${LINK_NUM}

    # Set IPv4 address to the interface at the remote host
    set_ipv4addr rhost ${LINK_NUM} ${IPV4_NETWORK} ${RHOST_IPV4_HOST}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to add an IPv4 address at the remote host"
	exit $TST_TOTAL
    fi

    # Get the Interface names
    lhost_ifname=`get_ifname lhost ${LINK_NUM}`
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to get the interface name at the local host"
	exit $TST_TOTAL
    fi
    rhost_ifname=`get_ifname rhost ${LINK_NUM}`
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to get the interface name at the remote host"
	exit $TST_TOTAL
    fi

    # IPv4 address of the remote host
    rhost_ipv4addr="${IPV4_NETWORK}.${RHOST_IPV4_HOST}"
}



#-----------------------------------------------------------------------
#
# NAME:
#   do_cleanup
#
# DESCRIPTION:
#   Recover the tested interfaces
#
#-----------------------------------------------------------------------
do_cleanup()
{
    # Make sure the TCP traffic server/client doesn't run
    bg_tcp_traffic killall

    # Initialize the interfaces
    initialize_if lhost ${LINK_NUM}
    initialize_if rhost ${LINK_NUM}
}



#-----------------------------------------------------------------------
#
# FUNCTION:
#   test01
#
# DESCRIPTION:
#   Creating a large number of IPv4-aliases by `ifconfig add'
#
#-----------------------------------------------------------------------
test01()
{
    TCID=if4-alias-addlarge01
    TST_COUNT=1
    tst_resm TINFO "Verify the IPv4 connectivity is not broken when 'ifconifg add' creates $IP_TOTAL IPv4-aliases."

    # Make sure that no alias exists.
    initialize_if lhost ${LINK_NUM}

    # Set the non-alias address
    set_ipv4addr lhost ${LINK_NUM} ${IPV4_NETWORK} ${LHOST_IPV4_HOST}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to add IPv4 address to the interface at the local host"
	return 1
    fi
    lhost_ipv4addr="${IPV4_NETWORK}.${LHOST_IPV4_HOST}"

    # Check the connctivity
    ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv4_connectivity $rhost_ifname $lhost_ipv4addr' ; echo $?'`
    if [ $ret -ne 0 ]; then
	tst_resm TBROK "Test Link $LINK_NUM is somthing wrong."
	return 1
    fi

    # Set up for the loop
    # Note: 2 colon aliases are added by `ifconfig ethn:x:y'
    #       `ifconfig ethn:x:y:... add ' runs 4 times per a pair of x,y
    cnt=0
    x=1 ; y=1 ; xymax=99
    a=1 ; amax=3
    ifalias="${lhost_ifname}:${x}:${y}"
    ifconfig $ifalias ${IPV4_NETWORK}.${x}.${y}.${a} \
	    netmask $IPV4_NETMASK broadcast $IPV4_BROADCAST

    # Make a background TCP traffic from the non-alias address
   server_pid=`bg_tcp_traffic make $lhost_ipv4addr`

    # Start the loop
    while [ $cnt -lt $IP_TOTAL ]; do
	if [ $a -gt $amax ]; then
	    a=1
	    y=`expr $y + 1`
	    if [ $y -gt $xymax ]; then
		y=1
		x=`expr $x + 1`
		if [ $x -gt $xymax ]; then
		    tst_resm TINFO "No more address could be assigned by this script. Finished $cnt times."
		    break
		fi
	    fi
	    ifalias="${lhost_ifname}:${x}:${y}"
	    ifconfig $ifalias ${IPV4_NETWORK}.${x}.${y}.${a} \
			netmask $IPV4_NETMASK broadcast $IPV4_BROADCAST
	fi

	# Create
	a=`expr $a + 1`
	ifconfig $ifalias add ${IPV4_NETWORK}.${x}.${y}.${a} \
		    netmask $IPV4_NETMASK broadcast $IPV4_BROADCAST
	if [ $? -ne 0 ]; then
	    tst_resm TINFO "It seems $cnt is limit."
	    break
	fi
	ifalias="${ifalias}:0"

	# Check
	cnt=`expr $cnt + 1`
	if [ $CHECK_INTERVAL -ne 0 ]; then
	    if [ `expr $cnt % $CHECK_INTERVAL` -eq 0 ]; then
		# Check the connectivity from the remote host to
		# the local host alias.
		ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv4_connectivity $rhost_ifname ${IPV4_NETWORK}.${x}.${y}.${a}' ; echo $?'`
		if [ $ret -ne 0 ]; then
		    tst_resm TFAIL "The interface ${lhost_ifname} is broken."
		    return 1
		fi
	    fi
	fi

	# Check the background TCP traffic
	bg_tcp_traffic check $server_pid
	if [ $? -ne 0 ]; then
	    server_pid=`bg_tcp_traffic make $lhost_ipv4addr`
	fi
    done

    # Stop the background TCP traffic
    bg_tcp_traffic killall

    # Check the connecitivy from the interface (non-alias)
    check_icmpv4_connectivity $lhost_ifname $rhost_ipv4addr
    if [ $? -ne 0 ]; then
	tst_resm TFAIL "The interface ${lhost_ifname} is broken."
	return 1
    fi

    tst_resm TPASS "Test is finished correctly."
    return 0
}


#-----------------------------------------------------------------------
#
# FUNCTION:
#   test02
#
# DESCRIPTION:
#   Creating a large number of IPv4-aliases by `ifconfig ethn:n'
#
#-----------------------------------------------------------------------
test02()
{
    TCID=if4-alias-addlarge02
    TST_COUNT=2
    tst_resm TINFO "Verify the IPv4 connectivity is not broken when 'ifconfig ethn:n' creates $IP_TOTAL IPv4-aliases'"

    # Make sure that no alias exists.
    initialize_if lhost ${LINK_NUM}

    # Set the non-alias address
    set_ipv4addr lhost ${LINK_NUM} ${IPV4_NETWORK} ${LHOST_IPV4_HOST}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to initialize the interface at the local host"
	return 1
    fi
    lhost_ipv4addr="${IPV4_NETWORK}.${LHOST_IPV4_HOST}"

    # Check the connctivity
    ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv4_connectivity $rhost_ifname $lhost_ipv4addr' ; echo $?'`
    if [ $ret -ne 0 ]; then
	tst_resm TBROK "Test Link $LINK_NUM is somthing wrong."
	return 1
    fi

    # Make a background TCP traffic from the non-alias address
    server_pid=`bg_tcp_traffic make $lhost_ipv4addr`

    # Start the loop
    cnt=0
    x=1 ; y=1 ; xymax=254
    while [ $cnt -lt $IP_TOTAL ]; do
	# Create
	ifalias="${lhost_ifname}:${x}:${y}"
	ifconfig $ifalias ${IPV4_NETWORK}.0.${x}.${y} \
		netmask $IPV4_NETMASK broadcast $IPV4_BROADCAST
	if [ $? -ne 0 ]; then
	    tst_resm TINFO "It seems $cnt is limit."
	    break
	fi


	# Check the connectivity
	cnt=`expr $cnt + 1`
	if [ $CHECK_INTERVAL -ne 0 ]; then
	    if [ `expr $cnt % ${CHECK_INTERVAL}` -eq 0 ]; then
		# Check the connectivity from the remote host to
		# the local host alias.
		ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv4_connectivity $rhost_ifname ${IPV4_NETWORK}.0.${x}.${y}'; echo $?'`
		if [ $ret -ne 0 ]; then
		    tst_resm TFAIL "The interface ${lhost_ifname} is broken."
		    return 1
		fi
	    fi
	fi

	# Check the background TCP traffic
	bg_tcp_traffic check $server_pid
	if [ $? -ne 0 ]; then
	    server_pid=`bg_tcp_traffic make $lhost_ipv4addr`
	fi

	# Define next IP and alias
	y=`expr $y + 1`
	if [ $y -gt $xymax ]; then
	    y=1
	    x=`expr $x + 1`
	    if [ $x -gt $xymax ]; then
		tst_resm TINFO "No more address could be assigned by this script. Finished $cnt times."
		break
	    fi
	fi
    done

    # Stop the background TCP traffic
    bg_tcp_traffic killall

    # Check the connecitivy from the interface (non-alias)
    check_icmpv4_connectivity $lhost_ifname $rhost_ipv4addr
    if [ $? -ne 0 ]; then
	tst_resm TFAIL "The interface ${lhost_ifname} is broken."
	return 1
    fi

    tst_resm TPASS "Test is finished correctly."
    return 0
}



#-----------------------------------------------------------------------
#
# FUNCTION:
#   test03
#
# DESCRIPTION:
#   Creating a large number of IPv4-aliases by ip command
#
#-----------------------------------------------------------------------
test03()
{
    TCID=if4-alias-addlarge03
    TST_COUNT=3
    tst_resm TINFO "Verify the IPv4 connectivity is not broken when ip command creates $IP_TOTAL IPv4-aliases"

    # Make sure that no alias exists.
    initialize_if lhost ${LINK_NUM}

    # Set the non-alias address
    set_ipv4addr lhost ${LINK_NUM} ${IPV4_NETWORK} ${LHOST_IPV4_HOST}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to initialize the interface at the local host"
	return 1
    fi
    lhost_ipv4addr="${IPV4_NETWORK}.${LHOST_IPV4_HOST}"

    # Check the connctivity
    ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv4_connectivity $rhost_ifname $lhost_ipv4addr' ; echo $?'`
    if [ $ret -ne 0 ]; then
	tst_resm TBROK "Test Link $LINK_NUM is somthing wrong."
	return 1
    fi

    # Make a background TCP traffic from the non-alias address
    server_pid=`bg_tcp_traffic make $lhost_ipv4addr`

    # Start the loop
    cnt=0
    x=1 ; y=1 ; xymax=254
    while [ $cnt -lt $IP_TOTAL ]; do
	# Create
	ip addr add ${IPV4_NETWORK}.1.${x}.${y}/${IPV4_NETMASK_NUM} \
		broadcast $IPV4_BROADCAST dev ${lhost_ifname}
	if [ $? -ne 0 ]; then
	    tst_resm TINFO "It seems $cnt is limit."
	    break
	fi

	# Check the connectivity
	cnt=`expr $cnt + 1`
	if [ $CHECK_INTERVAL -ne 0 ]; then
	    if [ `expr $cnt % ${CHECK_INTERVAL}` -eq 0 ]; then
		# Check the connectivity from the remote host to
		# the local host alias.
		ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv4_connectivity $rhost_ifname ${IPV4_NETWORK}.1.${x}.${y}' ; echo $?'`
		if [ $ret -ne 0 ]; then
		    tst_resm TFAIL "The interface ${lhost_ifname} is broken."
		    return 1
		fi
	    fi
	fi

	# Check the background TCP traffic
	bg_tcp_traffic check $server_pid
	if [ $? -ne 0 ]; then
	    server_pid=`bg_tcp_traffic make $lhost_ipv4addr`
	fi

	# Define next IP and alias
	y=`expr $y + 1`
	if [ $y -gt $xymax ]; then
	    y=1
	    x=`expr $x + 1`
	    if [ $x -gt $xymax ]; then
		tst_resm TINFO "No more address could be assigned by this script. Finished $cnt times."
		break
	    fi
	fi
    done

    # Check the connecitivy from the interface (non-alias)
    check_icmpv4_connectivity $lhost_ifname $rhost_ipv4addr
    if [ $? -ne 0 ]; then
	tst_resm TFAIL "The interface ${lhost_ifname} is broken."
	return 1
    fi

    # Stop the background TCP traffic
    bg_tcp_traffic killall

    tst_resm TPASS "Test is finished correctly."
    return 0
}



#-----------------------------------------------------------------------
#
# Main
#
# Exit Value:
#   The number of the failure
#
#-----------------------------------------------------------------------

RC=0
do_setup
test01 || RC=`expr $RC + 1`
test02 || RC=`expr $RC + 1`
test03 || RC=`expr $RC + 1`
do_cleanup

exit $RC
