xref: /linux/tools/testing/selftests/net/hsr/hsr_common.sh (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1# SPDX-License-Identifier: GPL-2.0
2# Common code for HSR testing scripts
3
4source ../lib.sh
5ret=0
6ksft_skip=4
7
8# $1: IP address
9is_v6()
10{
11	[ -z "${1##*:*}" ]
12}
13
14do_ping()
15{
16	local netns="$1"
17	local connect_addr="$2"
18	local ping_args="-q -c 2 -i 0.1"
19
20	if is_v6 "${connect_addr}"; then
21		$ipv6 || return 0
22		ping_args="${ping_args} -6"
23	fi
24
25	ip netns exec ${netns} ping ${ping_args} $connect_addr >/dev/null
26	if [ $? -ne 0 ] ; then
27		echo "$netns -> $connect_addr connectivity [ FAIL ]" 1>&2
28		ret=1
29		return 1
30	fi
31
32	return 0
33}
34
35do_ping_long()
36{
37	local netns="$1"
38	local connect_addr="$2"
39	local ping_args="-q -c 10 -i 0.1"
40
41	if is_v6 "${connect_addr}"; then
42		$ipv6 || return 0
43		ping_args="${ping_args} -6"
44	fi
45
46	OUT="$(LANG=C ip netns exec ${netns} ping ${ping_args} $connect_addr | grep received)"
47	if [ $? -ne 0 ] ; then
48		echo "$netns -> $connect_addr ping [ FAIL ]" 1>&2
49		ret=1
50		return 1
51	fi
52
53	VAL="$(echo $OUT | cut -d' ' -f1-8)"
54	SED_VAL="$(echo ${VAL} | sed -r -e 's/([0-9]{2}).*([0-9]{2}).*[[:space:]]([0-9]+%).*/\1 transmitted \2 received \3 loss/')"
55	if [ "${SED_VAL}" != "10 transmitted 10 received 0% loss" ]
56	then
57		echo "$netns -> $connect_addr ping TEST [ FAIL ]"
58		echo "Expect to send and receive 10 packets and no duplicates."
59		echo "Full message: ${OUT}."
60		ret=1
61		return 1
62	fi
63
64	return 0
65}
66
67stop_if_error()
68{
69	local msg="$1"
70
71	if [ ${ret} -ne 0 ]; then
72		echo "FAIL: ${msg}" 1>&2
73		exit ${ret}
74	fi
75}
76
77check_prerequisites()
78{
79	ip -Version > /dev/null 2>&1
80	if [ $? -ne 0 ];then
81		echo "SKIP: Could not run test without ip tool"
82		exit $ksft_skip
83	fi
84}
85