xref: /aosp_15_r20/external/ltp/testcases/network/stress/interface/if-addr-adddel.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2017-2022 Petr Vorel <[email protected]>
4# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
5# Copyright (c) International Business Machines  Corp., 2005
6# Author: Mitsuru Chinen <[email protected]>
7
8IF_CMD='ifconfig'
9
10test_body()
11{
12	local cmd="$CMD"
13	local num=$(($(od -A n -t u1 -N 1 /dev/random) * 253 / 255 + 2 ))
14	local iface=$(tst_iface)
15	if [ "$TST_IPV6" ]; then
16		local new_ip=${IPV6_NET32_UNUSED}::$num
17		local netmask=64
18	else
19		local new_ip=${IPV4_NET16_UNUSED}.1.$num
20		local netmask=24
21	fi
22
23	tst_res TINFO "'$cmd' add/del IPv$TST_IPVER '$new_ip' $NS_TIMES times"
24
25	if ! restore_ipaddr; then
26		tst_res TBROK "Failed to set default IP addresses"
27		return
28	fi
29
30	local cnt=1
31	while [ $cnt -le $NS_TIMES ]; do
32		make_background_tcp_traffic
33
34		case $cmd in
35		ifconfig)
36			if [ "$TST_IPV6" ]; then
37				ifconfig $iface add $new_ip/$netmask
38			else
39				ifconfig $iface:1 $new_ip netmask 255.255.255.0
40			fi
41		;;
42		ip) ip addr add $new_ip/$netmask dev $iface ;;
43		esac
44
45		if [ $? -ne 0 ]; then
46			tst_res TFAIL "command failed to add $new_ip to $iface"
47			return
48		fi
49
50		ip addr show $iface | grep -q $new_ip
51		if [ $? -ne 0 ]; then
52			ip addr show $iface
53			tst_res TFAIL "$new_ip not configured"
54			return
55		fi
56
57		check_connectivity_interval $cnt || return
58
59		cnt=$(($cnt + 1))
60
61		case $cmd in
62		ifconfig)
63			if [ "$TST_IPV6" ]; then
64				ifconfig $iface del $new_ip/$netmask
65			else
66				ifconfig $iface:1 down
67			fi
68		;;
69		ip) ip addr del $new_ip/$netmask dev $iface ;;
70		esac
71
72		if [ $? -ne 0 ]; then
73			tst_res TFAIL " delete command failed".
74			return
75		fi
76
77		ip addr show $iface | grep -q $new_ip
78		if [ $? -eq 0 ]; then
79			ip addr show $iface
80			tst_res TFAIL "Failed to remove '$new_ip' address"
81			return
82		fi
83	done
84
85	tst_res TPASS "Test is finished correctly"
86}
87
88. if-lib.sh
89
90# The interval of the check interface activity
91CHECK_INTERVAL=${CHECK_INTERVAL:-$(($NS_TIMES / 20))}
92
93tst_run
94