#!/bin/sh
# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
# 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 would 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, see <http://www.gnu.org/licenses/>.
#
# Author: Mitsuru Chinen <mitch@jp.ibm.com>

TST_TOTAL=2

TST_CLEANUP="netstress_cleanup"

. test_net_stress.sh

# The interval of the check interface activity
CHECK_INTERVAL=${CHECK_INTERVAL:-$(($IP_TOTAL / 20))}

test_body()
{
	local cmd_type=$1

	case $cmd_type in
	if_cmd) local cmd_name='ifconfig' ;;
	ip_cmd) local cmd_name='ip' ;;
	*) tst_brkm TBROK "Unknown test parameter '$cmd_type'"
	esac

	local iface=$(tst_iface)
	[ "$TST_IPV6" ] && local netmask=64 || local netmask=16

	tst_resm TINFO "'$cmd_name' add $IP_TOTAL IPv$ipver addresses"
	tst_resm TINFO "check interval that $iface is working: $CHECK_INTERVAL"

	if ! restore_ipaddr; then
		tst_resm TBROK "Failed to set default IP addresses"
		return
	fi

	local x=1
	local y=1
	local cnt=1

	[ "$TST_IPV6" ] && local xymax=65535 || xymax=254

	if [ $IP_TOTAL -gt $((xymax * xymax)) ]; then
		tst_resm TWARN "set IP_TOTAL to $xymax * $xymax"
		IP_TOTAL=$((xymax * xymax))
	fi

	while [ $cnt -le $IP_TOTAL ]; do
		make_background_tcp_traffic

		if [ "$TST_IPV6" ]; then
			local hex_x=$(printf '%x' $x)
			local hex_y=$(printf '%x' $y)
			local new_ip=${IPV6_NET32_UNUSED}:1:1:1:$hex_x:$hex_y:1
		else
			local new_ip=${IPV4_NET16_UNUSED}.$x.$y
		fi

		case $cmd_type in
		if_cmd)
			if [ "$TST_IPV6" ]; then
				ifconfig $iface add $new_ip/$netmask
			else
				ifconfig $iface:$x:$y $new_ip netmask 255.255.0.0
			fi
		;;
		ip_cmd) ip addr add $new_ip/$netmask dev $iface ;;
		esac

		if [ $? -ne 0 ]; then
			tst_resm TFAIL "command failed to add $new_ip to $iface"
			return
		fi

		ip addr show $iface | grep -q $new_ip
		if [ $? -ne 0 ]; then
			ip addr show $iface
			tst_resm TFAIL "$new_ip not configured"
			return
		fi

		check_connectivity_interval $cnt || return

		case $cmd_type in
		if_cmd)
			if [ "$TST_IPV6" ]; then
				ifconfig $iface del $new_ip/$netmask
			else
				ifconfig $iface:$x:$y down
			fi
		;;
		ip_cmd) ip addr del $new_ip/$netmask dev $iface ;;
		esac

		if [ $? -ne 0 ]; then
			tst_resm TFAIL " delete command failed".
			return
		fi

		ip addr show $iface | grep -q $new_ip
		if [ $? -eq 0 ]; then
			ip addr show $iface
			tst_resm TFAIL "Failed to remove '$new_ip' address"
			return
		fi

		cnt=$(($cnt + 1))
		y=$(($y + 1))
		if [ $y -gt $xymax ]; then
			y=1
			x=$(($x + 1))
			if [ $x -gt $xymax ]; then
				tst_brkm TBROK "Too large $IP_TOTAL"
			fi
		fi
	done

	tst_resm TPASS "Test is finished correctly"
}

netstress_setup

tst_check_cmds ifconfig

test_body 'if_cmd'
test_body 'ip_cmd'

tst_exit