#!/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   tcp4-uni-basic01
#
# Description:
#   Verify that the kernel is not crashed with a connection to with
#   the following condition:
#     - The version of IP is IPv4
#     - Network is not delayed
#     - IPsec is not used
#     - Disable window scaling
#     - Enable Nagle algorithm
#     - Enable TCP Duplicate SACK support
#     - Enable SACK Support
#     - No packet are lost
#     - No packet are duplicated
#     - Disable TSO if it is avalable
#
#   *) This script may be read by the other test case
#
# Setup:
#   See 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

# The test case ID, the test case count and the total number of test case
TCID=${TCID:-tcp4-uni-basic01}
TST_TOTAL=1
TST_COUNT=1
export TCID
export TST_COUNT
export TST_TOTAL



# Test description
NON_BASIC=${NON_BASIC:-false}
$NON_BASIC || tst_resm TINFO "Verify that the kernel is not crashed by a TCP connection"

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

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

# Dulation of the test [sec]
NS_DURATION=${NS_DURATION:-3600}      # 1 hour

# The number of the test link where tests run
LINK_NUM=${LINK_NUM:-0}

# The version of IP
IP_VER=${IP_VER:-4}

# true, if ipsec is used
DO_IPSEC=${DO_IPSEC:-false}

# The value of SPI
SPI=${SPI:-1000}

# IPsec Protocol ( ah / esp / ipcomp )
IPSEC_PROTO=${IPSEC_PROTO:-ah}

# IPsec Mode ( transport / tunnel )
IPSEC_MODE=${IPSEC_MODE:-transport}

# true, if network is delayed
DO_NET_DELAY=${DO_NET_DELAY:-false}

# Amount of network delay [ms]
NET_DELAY=${NET_DELAY:-600}

# The deflection of network delay [ms]
NET_DELAY_DEFL=${NET_DELAY_DEFL:-200}

# true, if some packets are lost
DO_PACKET_LOSS=${DO_PACKET_LOSS:-false}

# Rate of packet loss [%]
PACKET_LOSS_RATE=${PACKET_LOSS_RATE:-8}

# true, if some packets are duplicated
DO_PACKET_DUP=${DO_PACKET_DUP:-false}

# Rate of packet dupulication [%]
PACKET_DUP_RATE=${PACKET_DUP_RATE:-1}

# true, if test is for small sending (Namely, disable NAGLE algorithm)
DO_SMALL_SEND=${DO_SMALL_SEND:-false}

# true, if test is for window scaling
DO_WINDOW_SCALING=${DO_WINDOW_SCALING:-false}

# true, if test is for DSACK
DO_DSACK=${DO_DSACK:-true}

# true, if test is for SACK
DO_SACK=${DO_SACK:-true}

# true, if test is for TSO
DO_TSO=${DO_TSO:-false}

#-----------------------------------------------------------------------
#
# Function: do_cleanup
#
# Description:
#   Recover the system configuration
#
#-----------------------------------------------------------------------
do_cleanup()
{
    # Kill the tcp traffic server
    killall_tcp_traffic

    # Enable window scaling
    sysctl -w net.ipv4.tcp_window_scaling=1 >/dev/null 2>&1

    # Enable TCP Duplicate SACK support
    sysctl -w net.ipv4.tcp_dsack=1 >/dev/null 2>&1

    # Enable SACK support
    sysctl -w net.ipv4.tcp_sack=1 >/dev/null 2>&1

    # Restore the tcp segmentation offload setting
    if [ x${tso_orig} != x ]; then
	ethtool -K $lhost_ifname tso $tso_orig
    fi

    # Unset SAD/SPD
    output_ipsec_conf flush | setkey -c >/dev/null 2>&1
    $LTP_RSH $RHOST ${LTPROOT}/'testcases/bin/output_ipsec_conf flush | PATH=/sbin:/usr/sbin:$PATH setkey -c' >/dev/null 2>&1

    # Disable network emulator
    if [ x$rhost_ifname = x ]; then
	rhost_ifname=`get_ifname rhost $LINK_NUM`
    fi
    $LTP_RSH $RHOST "PATH=/sbin:/usr/sbin:$PATH tc qdisc del dev $rhost_ifname root netem" >/dev/null 2>&1

    # Clean up each interface
    initialize_if lhost ${LINK_NUM} || exit 1
    initialize_if rhost ${LINK_NUM} || exit 1
}


#-----------------------------------------------------------------------
#
# Function: do_setup
#
# Description:
#   Setup the system for testing
#
#-----------------------------------------------------------------------
do_setup()
{
    # Output the informaion
    tst_resm TINFO "- Test duration is $NS_DURATION [sec]"
    tst_resm TINFO "- Version of IP is IPv${IP_VER}"

    # Addtional server option
    server_opt=""

    # Addtional client option
    client_opt=""

    # Original TSO parameter
    tso_orig=""

    # Check the remote host has netem functionality
    if $DO_NET_DELAY || $DO_PACKET_LOSS || $DO_PACKET_DUP ; then
	message=`check_netem`
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "$message"
	    exit 1
	fi
    fi

    $DO_NET_DELAY && tst_resm TINFO "- Network delay is ${NET_DELAY}ms +/- ${NET_DELAY_DEFL}ms"

    $DO_PACKET_LOSS && tst_resm TINFO "- Packet loss rate is ${PACKET_LOSS_RATE}%%"

    $DO_PACKET_DUP && tst_resm TINFO "- Packet duplication rate is ${PACKET_DUP_RATE}%%"

    # Check the setkey command is available
    if $DO_IPSEC ; then
	message=`check_setkey`
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "$message"
	    exit 1
	fi

	case $IPSEC_PROTO in
	    ah)
	    tst_resm TINFO "- IPsec [ AH / $IPSEC_MODE ]"
	    ;;
	    esp)
	    tst_resm TINFO "- IPsec [ ESP / $IPSEC_MODE ]"
	    ;;
	    ipcomp)
	    tst_resm TINFO "- IPcomp [ $IPSEC_MODE ]"
	    ;;
	esac
    fi

    # name of interface of the local/remote 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
    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

    # Initialize the system configuration
    do_cleanup

    # Call do_cleanup function before exit
    trap do_cleanup 0

    # Add option for small sending test
    if $DO_SMALL_SEND ; then
	server_opt="-s"
    fi

    # Configure window scaling parameter
    if $DO_WINDOW_SCALING ; then
	server_opt="-w"
	client_opt="-w"
	sysctl -w net.ipv4.tcp_window_scaling=1 >/dev/null
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to enable window scaling"
	    exit 1
	fi
    else
	sysctl -w net.ipv4.tcp_window_scaling=0 >/dev/null 2>&1
    fi

    # Configure DSACK parameter
    if $DO_DSACK ; then
	sysctl -w net.ipv4.tcp_dsack=1 >/dev/null
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to enable Duplicate SACK"
	    exit 1
	fi
    else
	sysctl -w net.ipv4.tcp_dsack=0 >/dev/null 2>&1
    fi

    # Configure SACK parameter
    if $DO_SACK ; then
	sysctl -w net.ipv4.tcp_sack=1 >/dev/null
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to enable SACK"
	    exit 1
	fi
    else
	sysctl -w net.ipv4.tcp_sack=0 >/dev/null 2>&1
    fi

    # Store the current TSO parameter, then configure it
    offload_info=`mktemp -p $TMPDIR`
    ethtool -k $lhost_ifname > $offload_info 2>/dev/null
    fgrep "tcp segmentation offload" $offload_info >/dev/null 2>&1
    if [ $? -ne 0 ]; then
	if $DO_TSO ; then
	    tst_resm TCONF "The device at $lhost_ifname does not support TSO."
	    rm -f $offload_info
	    exit 1
	fi
	tso_orig=`fgrep "tcp segmentation offload" $offload_info | sed -e 's/^.*: //'`
	if $DO_TSO ; then
	    server_opt="-w"
	    client_opt="-w"
	    ethtool -K $lhost_ifname tso on
	else
	    ethtool -K $lhost_ifname tso off
	fi
    fi
    rm -f $offload_info

    # Configure the network interface
    case $IP_VER in
	4)
	# Network portion of the IPv4 address
	network_part=${IPV4_NETWORK:-"10.0.0"}

	# Netmask of the IPv4 network
	network_mask=24

	# Host portion of the IPv4 address
	lhost_host_part=${LHOST_IPV4_HOST:-"2"}     # local host
	rhost_host_part=${RHOST_IPV4_HOST:-"1"}     # remote host

	# Set IPv4 addresses to the interfaces
	set_ipv4addr lhost $LINK_NUM $network_part $lhost_host_part
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to add any IP address at the local host"
	    exit 1
	fi

	set_ipv4addr rhost $LINK_NUM $network_part $rhost_host_part
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to add any IP address at the remote host"
	    exit 1
	fi

	# IPv4 address of the local/remote host
	lhost_addr="${network_part}.${lhost_host_part}"
	rhost_addr="${network_part}.${rhost_host_part}"
	;;

	6)
	# Network portion of the IPv6 address
	network_part="fd00:1:1:1"

	# Netmask of the IPv6 network
	network_mask=64

	# Host portion of the IPv6 address
	lhost_host_part=":2"     # local host
	rhost_host_part=":1"     # remote host

	# Set IPv6 addresses to the interfaces
	add_ipv6addr lhost $LINK_NUM $network_part $lhost_host_part
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to add any IP address at the local host"
	    exit 1
	fi

	add_ipv6addr rhost $LINK_NUM $network_part $rhost_host_part
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to add any IP address at the remote host"
	    exit 1
	fi

	# IPv6 address of the local/remote host
	lhost_addr="${network_part}:${lhost_host_part}"
	rhost_addr="${network_part}:${rhost_host_part}"
	;;

	*)
	tst_resm TBROK "Unknown IP version"
	;;
    esac

    netem_param=

    # Make the network delay
    if $DO_NET_DELAY ; then
	netem_param="delay ${NET_DELAY}ms ${NET_DELAY_DEFL}ms distribution normal"
	ret=`$LTP_RSH $RHOST 'PATH=/sbin:/usr/sbin:$PATH tc' qdisc add dev $rhost_ifname root netem $netem_param' ; echo $?'`
	if [ $ret -ne 0 ]; then
	    tst_resm TBROK "Failed to make the delayed network"
	    exit 1
	fi
    fi

    # Make some packets lost
    if $DO_PACKET_LOSS ; then
	tc_cmd="add"
	if [ x"$netem_param" != x ]; then
	    tc_cmd="change"
	fi
	netem_param="loss ${PACKET_LOSS_RATE}% $netem_param"
	ret=`$LTP_RSH $RHOST 'PATH=/sbin:/usr/sbin:$PATH tc' qdisc $tc_cmd dev $rhost_ifname root netem $netem_param' ; echo $?'`
	if [ $ret -ne 0 ]; then
	    tst_resm TBROK "Failed to use netem functionality"
	    exit 1
	fi
    fi

    # Make some packets duplicated
    if $DO_PACKET_DUP ; then
	tc_cmd="add"
	if [ x"$netem_param" != x ]; then
	    tc_cmd="change"
	fi
	netem_param="duplicate ${PACKET_DUP_RATE}% $netem_param"
	ret=`$LTP_RSH $RHOST 'PATH=/sbin:/usr/sbin:$PATH tc' qdisc $tc_cmd dev $rhost_ifname root netem $netem_param' ; echo $?'`
	if [ $ret -ne 0 ]; then
	    tst_resm TBROK "Failed to use netem functionality"
	    exit 1
	fi
    fi

    # Configure SAD/SPD
    if $DO_IPSEC ; then
	# Set SAD/SPD according to the variables
	ipsec_log=`mktemp -p $TMPDIR`
	output_ipsec_conf src \
	    $IPSEC_PROTO $IPSEC_MODE $SPI $lhost_addr $rhost_addr \
		|  setkey -c 2>&1 | tee $ipsec_log
	if [ $? -ne 0 -o -s $ipsec_log ]; then
	    tst_resm TBROK "Failed to configure SAD/SPD on the local host."
	    rm -f $ipsec_log
	    exit 1
	fi

	$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
	if [ $? -ne 0 -o -s $ipsec_log ]; then
	    tst_resm TBROK "Failed to configure SAD/SPD on the remote host."
	    rm -f $ipsec_log
	    exit 1
	fi
    fi

    # Make sure the connectvity
    case $IP_VER in
	4)
	ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv4_connectivity $rhost_ifname $lhost_addr' ; echo $?'`
	if [ $ret -ne 0 ]; then
	    tst_resm TBROK "There is no IPv4 connectivity."
	    exit 1
	fi
	;;

	6)
	ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/check_icmpv6_connectivity $rhost_ifname $lhost_addr' ; echo $?'`
	if [ $ret -ne 0 ]; then
	    tst_resm TBROK "There is no IPv6 connectivity."
	    exit 1
	fi
	;;
    esac

}

#-----------------------------------------------------------------------
#
# Main
#
#

do_setup

# Find the available consecutive ports
server_port=`find_portbundle tcp 1025 1`
if [ $? -ne 0 ]; then
    tst_resm TBROK "No port is available."
    exit 1
fi

# Run a tcp traffic server
info_file=`mktemp -p $TMPDIR`
ns-tcpserver -b -f $IP_VER -p $server_port -o $info_file $server_opt
if [ $? -ne 0 ]; then
    tst_resm TFAIL "Failed to run a tcp traffic server."
    rm -f $info_file
    exit 1
fi

while true ; do
    if [ -s $info_file ]; then
	break
    fi
done

server_pid=`grep PID: $info_file | cut -f 2 -d ' '`
rm -f $info_file

# Run a tcp taffic client
ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/ns-tcpclient -b -f $IP_VER -S $lhost_addr -p $server_port $client_opt' ; echo $?'`
if [ $ret -ne 0 ]; then
    tst_resm TFAIL "Failed to run a tcp traffic client"
    exit 1
fi

start_epoc=`date +%s`
while true ; do
    current_epoc=`date +%s`
    elapse_epoc=`expr $current_epoc - $start_epoc`

    if [ $elapse_epoc -ge $NS_DURATION ]; then
	killall -SIGHUP ns-tcpserver
	break
    fi

    # Watch the TCP traffic server
    ps auxw | fgrep ns-tcpserver | fgrep -l $server_pid >/dev/null 2>&1
    if [ $? -ne 0 ]; then
	tst_resm TFAIL "tcp traffic server is dead in $elapse_epoc [sec]"
	exit 1
    fi
    sleep 1
done


#-----------------------------------------------------------------------
#
# Clean up
#

tst_resm TPASS "Test is finished successfully."
exit 0