#!/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-adddel
#
# Description:
#  Verify the IPv6 connectivity isn't broken with adding and deleting an
#  IPv6 address many times
#    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-adddel
TST_COUNT=0
export TCID
export TST_COUNT

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

# The number of the add/delete IPv6 address
NS_TIMES=${NS_TIMES:-10000}

# The interval of the check interface activity
CHECK_INTERVAL=${CHECK_INTERVAL:-`expr $NS_TIMES \/ 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 of the remote host
RHOST_IPV6_HOST=":1"

# Host portion of a IPv6 address of the local host for TCP traffic
LHOST_IPV6_HOST_FIXED=":99"


#-----------------------------------------------------------------------
#
# NAME:
#   do_setup
#
# DESCRIPTION:
#   Preparation
#
# SET VALUES:
#   rhost_ipv6addr	- IPv6 Address of the remote host
#   lhost_ifname	- Interface name of the local host
#   rhost_ifname	- Interface name of the remote host
#
#-----------------------------------------------------------------------
do_setup()
{
    TCID=if6-addr-adddel
    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"
    fi

    # IPv6 address of the remote host
    rhost_ipv6addr="${IPV6_NETWORK}:${RHOST_IPV6_HOST}"

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



#-----------------------------------------------------------------------
#
# 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-adddel0$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 connectivty is not broken when the $test_command command adds, then deletes an IPv6 address $NS_TIMES times"

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

    # IPv6 address of the localhost
    lhost_ipv6addr=${IPV6_NETWORK}::${test_type}${test_type}

    # Add an IPv6 address for TCP traffic
    add_ipv6addr lhost ${LINK_NUM} ${IPV6_NETWORK} ${LHOST_IPV6_HOST_FIXED}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to assign an IPv6 address at the local host"
	return 1
    fi
    lhost_fixed_ipv6addr="${IPV6_NETWORK}:${LHOST_IPV6_HOST_FIXED}"

    # Check the connctivity
    ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv6_connectivity $rhost_ifname $lhost_fixed_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 non-alias address
    server_pid=`bg_tcp_traffic make ${lhost_fixed_ipv6addr}`

    # Start the loop
    cnt=0
    while [ $cnt -lt $NS_TIMES ]; do
	# Add
	case $test_type in
	    1)
	    ifconfig $lhost_ifname add ${lhost_ipv6addr}/${IPV6_NETMASK_NUM}
	    ;;
	    2)
	    ip addr add ${lhost_ipv6addr}/${IPV6_NETMASK_NUM} dev $lhost_ifname
	    ;;
	esac
	if [ $? -ne 0 ]; then
	    tst_resm TFAIL "Failed to add an IPv6 address at $cnt times"
	    return 1
	fi

	# Check the connectivity
	cnt=`expr $cnt + 1`
	if [ $CHECK_INTERVAL -ne 0 ]; then
	    if [ `expr $cnt % ${CHECK_INTERVAL}` -eq 0 ]; then
		check_icmpv6_connectivity $lhost_ifname $rhost_ipv6addr
		if [ $? -ne 0 ]; then
		    tst_resm TFAIL "$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_fixed_ipv6addr}`
	fi

	# Delete
	case $test_type in
	    1)
	    ifconfig $lhost_ifname del ${lhost_ipv6addr}/${IPV6_NETMASK_NUM}
	    ;;
	    2)
	    ip addr del ${lhost_ipv6addr}/${IPV6_NETMASK_NUM} dev $lhost_ifname
	    ;;
	esac
	if [ $? -ne 0 ]; then
	    tst_resm TFAIL "Failed to del an IPv6 addrss at $cnt times"
	    return 1
	fi
    done

    # 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
