#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2006                 ##
##                                                                            ##
## 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:
#   mcast6-grpope04
#
# Description:
#   Verify that the kernel is not crashed when joining and leaving the same
#   IPv6 multicast group with the different source filter on multiple sockets
#   lots of times
#
# Setup:
#   See ltp-yyyymmdd/testcases/network/stress/README
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# History:
#	May 6 2006 - 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=mcast6-grpope04
TST_TOTAL=1
TST_COUNT=1
export TCID
export TST_COUNT
export TST_TOTAL

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

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

# Number of the socket
MCASTNUM_NORMAL=${MCASTNUM_NORMAL:-20}

# The number of the join/leave groups
NS_TIMES=${NS_TIMES:-10000}

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

# Network portion of the IPv6 address
NETWORK_PART="fec0:1:1:1"

# Host portion of the IPv6 address
LHOST_HOST_PART=":2"	# local host
RHOST_HOST_PART=":1"	# remote host

# Multicast Address
MCAST_ADDR=ff0e::1111:1

# Prefix of the filter souce adddress
SOURCE_ADDR_PREFIX="fec0:100:100:100"


#-----------------------------------------------------------------------
#
# Function: do_cleanup
#
# Description:
#   Recover the system configuration
#
#-----------------------------------------------------------------------
do_cleanup()
{
    # Make sure to kill the multicast receiver and sender
    killall -SIGHUP ns-mcast_join >/dev/null 2>&1
    $LTP_RSH $RHOST killall -SIGHUP ns-icmpv6_sender >/dev/null 2>&1
    
    # Clean up each interface
    initialize_if lhost ${LINK_NUM}
    initialize_if rhost ${LINK_NUM}
}


#-----------------------------------------------------------------------
#
# Function: do_setup
#
# Description:
#   Configure the ssystem for the test
#
#-----------------------------------------------------------------------
do_setup()
{
    # Initialize the system configuration
    do_cleanup

    # Call do_cleanup function before exit
    trap do_cleanup 0

    # 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

    # 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}"
    rhost_linklocal="fe80:${RHOST_HOST_PART}"

    # Make sure the connectvity
    check_icmpv6_connectivity $lhost_ifname $rhost_addr
    if [ $? -ne 0 ]; then
	tst_resm TBROK "There is no IPv6 connectivity."
	exit 1
    fi

    # Make sure the sysctl values
    sysctl -w net.ipv6.conf.all.force_mld_version=0 >/dev/null
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to set the sysctl value regarding multicast"
	exit $TST_TOTAL
    fi

    sysctl -w net.ipv6.conf.${lhost_ifname}.force_mld_version=0 >/dev/null
    sysctl -w net.ipv6.mld_max_msf=10 >/dev/null
}


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

# Test description
tst_resm TINFO "Verify that the kernel is not crashed when joining and leaving ame IPv6 multicast group with different source filters on $MCASTNUM_NORMAL sockets in $NS_TIMES times"

do_setup

# Send MLD General Query from the remote host
ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/ns-icmpv6_sender -I $rhost_ifname -S $rhost_linklocal -m -w 1000000000 -r 1000 -b' ; echo $?'`
if [ $ret -ne 0 ]; then
    tst_resm TBROK "Failed to start MLD querier"
    exit 1
fi

# Run a multicast join tool
num=0
while [ $num -lt $MCASTNUM_NORMAL ]; do
    # Define the source address
    if [ $num -gt 65535 ]; then
	tst_resm TINFO "The number of the connection is less than 65535"
	break
    fi
    num_hex=`printf "%x" $num`
    source_addr="${SOURCE_ADDR_PREFIX}::${num_hex}"

    if [ `expr $num % 5` -ne 2 ]; then
	filter="include"
    else
	filter="exclude"
    fi
    
    ns-mcast_join -f 6 -I $lhost_ifname -l $NS_TIMES -a $MCAST_ADDR -F $filter -s $source_addr &
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to start multicast joining tool Please check the environment"
	exit 1
    fi
    num=`expr $num + 1`
done

wait

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

tst_resm TPASS "Test is finished successfully."

exit 0
