xref: /aosp_15_r20/external/ltp/testcases/network/tcp_cmds/ipneigh/ipneigh01.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2018 SUSE Linux GmbH
4# Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
5# Copyright (c) International Business Machines Corp., 2000
6#
7# Test basic functionality of 'arp' and 'ip neigh'.
8
9NUMLOOPS=${NUMLOOPS:-50}
10TST_TESTFUNC=do_test
11TST_SETUP=do_setup
12TST_OPTS="c:"
13TST_PARSE_ARGS="parse_args"
14TST_USAGE="usage"
15TST_NEEDS_ROOT=1
16
17do_setup()
18{
19	case $CMD in
20	ip)
21		SHOW_CMD="ip neigh show"
22		DEL_CMD="ROD ip neigh del $(tst_ipaddr rhost) dev $(tst_iface)"
23		;;
24	arp)
25		if [ -n "$TST_IPV6" ]; then
26			tst_brk TCONF "'arp' doesn't support IPv6"
27		fi
28		SHOW_CMD="arp -an"
29		DEL_CMD="ROD arp -d $(tst_ipaddr rhost) -i $(tst_iface)"
30		;;
31	*)
32		tst_brk TBROK "unknown or missing command, use -c [ arp | ip ]"
33		;;
34	esac
35
36	tst_require_cmds $CMD ping$TST_IPV6
37}
38
39usage()
40{
41	echo "-c [ arp | ip ] Test command"
42}
43
44parse_args()
45{
46	case $1 in
47	c) CMD="$2" ;;
48	esac
49}
50
51do_test()
52{
53	local entry_name="ARP"
54	[ "$TST_IPV6" ] && entry_name="NDISC"
55
56	tst_res TINFO "stress auto-creation $entry_name cache entry deleted with '$CMD' $NUMLOOPS times"
57
58	for i in $(seq 1 $NUMLOOPS); do
59
60		if ! ping$TST_IPV6 -q -c1 $(tst_ipaddr rhost) -I $(tst_iface) > /dev/null; then
61			tst_res TFAIL "cannot ping $(tst_ipaddr rhost)"
62			return
63		fi
64
65		local k
66		local ret=1
67		for k in $(seq 1 30); do
68			$SHOW_CMD | grep -q $(tst_ipaddr rhost)
69			if [ $? -eq 0 ]; then
70				ret=0
71				break
72			fi
73			tst_sleep 100ms
74		done
75
76		if [ "$ret" -ne 0 ]; then
77			tst_res TFAIL "$entry_name entry '$(tst_ipaddr rhost)' not listed"
78			return
79		fi
80
81		$DEL_CMD
82
83		if $SHOW_CMD | grep -q "$(tst_ipaddr rhost).*$(tst_hwaddr rhost)"; then
84			tst_res TFAIL "'$DEL_CMD' failed, entry has $(tst_hwaddr rhost)' $i/$NUMLOOPS"
85			return
86		fi
87	done
88
89	tst_res TPASS "verified adding/removing $entry_name cache entry"
90}
91
92. tst_net.sh
93tst_run
94