1#!/bin/sh
2# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
3# Copyright (c) International Business Machines  Corp., 2005
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License as
7# published by the Free Software Foundation; either version 2 of
8# the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it would be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17#
18# Author: Mitsuru Chinen <mitch@jp.ibm.com>
19
20TST_TOTAL=2
21TCID=if-addr-adddel
22
23. if-lib.sh
24
25# The interval of the check interface activity (check ten times)
26CHECK_INTERVAL=${CHECK_INTERVAL:-$(($NS_TIMES / 100))}
27
28test_body()
29{
30	local cmd_type=$1
31
32	case $cmd_type in
33	if_cmd) local cmd_name='ifconfig' ;;
34	ip_cmd) local cmd_name='ip' ;;
35	*) tst_brkm TBROK "Unknown test parameter '$cmd_type'"
36	esac
37
38	local num=$(($(od -A n -d -N 1 /dev/random) * 253 / 255 + 2 ))
39	local iface=$(tst_iface)
40	if [ "$TST_IPV6" ]; then
41		local new_ip=${IPV6_NET32_UNUSED}::$num
42		local netmask=64
43	else
44		local new_ip=${IPV4_NET16_UNUSED}.1.$num
45		local netmask=24
46	fi
47
48	tst_resm TINFO "'$cmd_name' add/del IPv$ipver '$new_ip' $NS_TIMES times"
49
50	tst_restore_ipaddr || \
51		tst_resm TBROK "Failed to set default IP addresses"
52
53	make_background_tcp_traffic
54
55	local cnt=1
56	while [ $cnt -le $NS_TIMES ]; do
57
58		case $cmd_type in
59		if_cmd)
60			if [ "$TST_IPV6" ]; then
61				ifconfig $iface add $new_ip/$netmask
62			else
63				ifconfig $iface:1 $new_ip netmask 255.255.255.0
64			fi
65		;;
66		ip_cmd) ip addr add $new_ip/$netmask dev $iface ;;
67		esac
68
69		if [ $? -ne 0 ]; then
70			tst_resm TFAIL "command failed to add $new_ip to $iface"
71			return
72		fi
73
74		ip addr show $iface | grep -q $new_ip
75		if [ $? -ne 0 ]; then
76			ip addr show $iface
77			tst_resm TFAIL "$new_ip not configured"
78			return
79		fi
80
81		check_connectivity $cnt || return
82
83		cnt=$(($cnt + 1))
84
85		# Check the background TCP traffic
86		pgrep -x tcp_fastopen > /dev/null || make_background_tcp_traffic
87
88		case $cmd_type in
89		if_cmd)
90			if [ "$TST_IPV6" ]; then
91				ifconfig $iface del $new_ip/$netmask
92			else
93				ifconfig $iface:1 down
94			fi
95		;;
96		ip_cmd) ip addr del $new_ip/$netmask dev $iface ;;
97		esac
98
99		if [ $? -ne 0 ]; then
100			tst_resm TFAIL " delete command failed".
101			return
102		fi
103
104		ip addr show $iface | grep -q $new_ip
105		if [ $? -eq 0 ]; then
106			ip addr show $iface
107			tst_resm TFAIL "Failed to remove '$new_ip' address"
108			return
109		fi
110	done
111
112	tst_resm TPASS "Test is finished correctly"
113}
114
115setup
116
117tst_check_cmds ifconfig
118
119test_body 'if_cmd'
120test_body 'ip_cmd'
121
122tst_exit
123