#!/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:
#   if6-addr-addlarge
#
# Description:
#   Verify the IPv6 connectivity is not broken with adding a large number
#   of IPv6 address
#    test01 - by ifconfig command
#    test02 - 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=2
export TST_TOTAL

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

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

# The number of the add IPv6 addr
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 IPv6 address
IPV6_NETWORK="fd00:1:1:1"

# Netmask of the tested network
IPV6_NETMASK_NUM=64

# Host portion of the IPv6 address
LHOST_IPV6_HOST=":2"
RHOST_IPV6_HOST=":1"



#-----------------------------------------------------------------------
#
# NAME:
#   do_setup
#
# DESCRIPTION:
#   Preparation
#
# SET VALUES:
#   lhost_ifname	- Interface name of the local host
#   rhost_ifname	- Interface name of the remote host
#   rhost_ipv6addr	- IP address of the remote host
#
#-----------------------------------------------------------------------
do_setup()
{
    TCID=if6-addr-addlarge
    TST_COUNT=0

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

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

    # Set IPv6 address to the interfaces
    add_ipv6addr rhost ${LINK_NUM} ${IPV6_NETWORK} ${RHOST_IPV6_HOST}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to add an IPv6 address at the remote host"
	exit $TST_TOTAL
    fi

    # Get the Interface name of the local host
    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

    # Get the Interface name of the remote host
    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

    # IPv6 address of the remote host
    rhost_ipv6addr="${IPV6_NETWORK}:${RHOST_IPV6_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:
#   test_body
#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   $1: define the test type
#       1 - ifconfig command case
#       2 - ip command case
#
#-----------------------------------------------------------------------
test_body()
{
    test_type=$1

    TCID=if6-addr-addlarge0$test_type
    TST_COUNT=$test_type

    case $test_type in
	1)
	test_command="ifconfig"
	;;
	2)
	test_command="ip"
	;;
	*)
	tst_resm TBROK "unspecified case"
	return 1
	;;
    esac

    tst_resm TINFO "Verify the IPv6 connectivity is not broken when the $test_command command adds $IP_TOTAL IPv6 addresses"

    # Make sure the interface is initialized
    initialize_if lhost ${LINK_NUM}

    # Set the fixed address
    add_ipv6addr lhost ${LINK_NUM} ${IPV6_NETWORK} ${LHOST_IPV6_HOST}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to add IPv6 address to the interface at the local host"
	return 1
    fi
    lhost_ipv6addr="${IPV6_NETWORK}:${LHOST_IPV6_HOST}"

    # Check the connctivity
    ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv6_connectivity $rhost_ifname $lhost_ipv6addr' ; 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 fixed address
    server_pid=`bg_tcp_traffic make $lhost_ipv6addr`

    # Start the loop
    cnt=0
    x=0 ; y=0 ; xymax=255
    while [ $cnt -lt $IP_TOTAL ]; do
	# Create
	hex_x=`printf '%02x' $x`
	hex_y=`printf '%02x' $y`
	case $test_type in
	    1)
	    add_ipv6addr="${IPV6_NETWORK}:1:${hex_x}:${hex_y}:1"
	    ifconfig ${lhost_ifname} add ${add_ipv6addr}/${IPV6_NETMASK_NUM}
	    ;;
	    2)
	    add_ipv6addr="${IPV6_NETWORK}:2:${hex_x}:${hex_y}:1"
	    ip addr add ${add_ipv6addr}/${IPV6_NETMASK_NUM} dev ${lhost_ifname}
	    ;;
	esac
	if [ $? -ne 0 ]; then
	    tst_resm TINFO "Failed to add an address. It seems to reach the max number."
	    break
	fi

	# Check the connectivity
	cnt=`expr $cnt + 1`
	if [ $CHECK_INTERVAL -ne 0 ]; then
	    if [ `expr $cnt % ${CHECK_INTERVAL}` -eq 0 ]; then
		# Do ping from the remote host to the new local host address
		ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv6_connectivity $rhost_ifname  ${add_ipv6addr}' ; echo $?'`
		if [ $ret -ne 0 ]; then
		    tst_resm TFAIL "The interface ${lhost_ifname} is broken at $cnt time."
		    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_ipv6addr`
	fi

	# Define next IP address to add
	y=`expr $y + 1`
	if [ $y -gt $xymax ]; then
	    y=0
	    x=`expr $x + 1`
	    if [ $x -gt $xymax ]; then
		tst_resm INFO "Too large times. Finished $cnt times."
		break
	    fi
	fi
    done

    # Check the connecitivy again
    check_icmpv6_connectivity $lhost_ifname $rhost_ipv6addr
    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
test_body 1 || RC=`expr $RC + 1`      # Case of ifconfig command
test_body 2 || RC=`expr $RC + 1`      # Case of ip command
do_cleanup

exit $RC
