xref: /linux/tools/testing/selftests/net/rds/run.sh (revision 9410645520e9b820069761f3450ef6661418e279)
1#! /bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4set -e
5set -u
6
7unset KBUILD_OUTPUT
8
9current_dir="$(realpath "$(dirname "$0")")"
10build_dir="$current_dir"
11
12build_include="$current_dir/include.sh"
13if test -f "$build_include"; then
14	# this include will define "$mk_build_dir" as the location the test was
15	# built.  We will need this if the tests are installed in a location
16	# other than the kernel source
17
18	source "$build_include"
19	build_dir="$mk_build_dir"
20fi
21
22# This test requires kernel source and the *.gcda data therein
23# Locate the top level of the kernel source, and the net/rds
24# subfolder with the appropriate *.gcno object files
25ksrc_dir="$(realpath "$build_dir"/../../../../../)"
26kconfig="$ksrc_dir/.config"
27obj_dir="$ksrc_dir/net/rds"
28
29GCOV_CMD=gcov
30
31#check to see if the host has the required packages to generate a gcov report
32check_gcov_env()
33{
34	if ! which "$GCOV_CMD" > /dev/null 2>&1; then
35		echo "Warning: Could not find gcov. "
36		GENERATE_GCOV_REPORT=0
37		return
38	fi
39
40	# the gcov version must match the gcc version
41	GCC_VER=$(gcc -dumpfullversion)
42	GCOV_VER=$($GCOV_CMD -v | grep gcov | awk '{print $3}'| awk 'BEGIN {FS="-"}{print $1}')
43	if [ "$GCOV_VER" != "$GCC_VER" ]; then
44		#attempt to find a matching gcov version
45		GCOV_CMD=gcov-$(gcc -dumpversion)
46
47		if ! which "$GCOV_CMD" > /dev/null 2>&1; then
48			echo "Warning: Could not find an appropriate gcov installation. \
49				gcov version must match gcc version"
50			GENERATE_GCOV_REPORT=0
51			return
52		fi
53
54		#recheck version number of found gcov executable
55		GCOV_VER=$($GCOV_CMD -v | grep gcov | awk '{print $3}'| \
56			awk 'BEGIN {FS="-"}{print $1}')
57		if [ "$GCOV_VER" != "$GCC_VER" ]; then
58			echo "Warning: Could not find an appropriate gcov installation. \
59				gcov version must match gcc version"
60			GENERATE_GCOV_REPORT=0
61		else
62			echo "Warning: Mismatched gcc and gcov detected.  Using $GCOV_CMD"
63		fi
64	fi
65}
66
67# Check to see if the kconfig has the required configs to generate a coverage report
68check_gcov_conf()
69{
70	if ! grep -x "CONFIG_GCOV_PROFILE_RDS=y" "$kconfig" > /dev/null 2>&1; then
71		echo "INFO: CONFIG_GCOV_PROFILE_RDS should be enabled for coverage reports"
72		GENERATE_GCOV_REPORT=0
73	fi
74	if ! grep -x "CONFIG_GCOV_KERNEL=y" "$kconfig" > /dev/null 2>&1; then
75		echo "INFO: CONFIG_GCOV_KERNEL should be enabled for coverage reports"
76		GENERATE_GCOV_REPORT=0
77	fi
78	if grep -x "CONFIG_GCOV_PROFILE_ALL=y" "$kconfig" > /dev/null 2>&1; then
79		echo "INFO: CONFIG_GCOV_PROFILE_ALL should be disabled for coverage reports"
80		GENERATE_GCOV_REPORT=0
81	fi
82
83	if [ "$GENERATE_GCOV_REPORT" -eq 0 ]; then
84		echo "To enable gcov reports, please run "\
85			"\"tools/testing/selftests/net/rds/config.sh -g\" and rebuild the kernel"
86	else
87		# if we have the required kernel configs, proceed to check the environment to
88		# ensure we have the required gcov packages
89		check_gcov_env
90	fi
91}
92
93# Kselftest framework requirement - SKIP code is 4.
94check_conf_enabled() {
95	if ! grep -x "$1=y" "$kconfig" > /dev/null 2>&1; then
96		echo "selftests: [SKIP] This test requires $1 enabled"
97		echo "Please run tools/testing/selftests/net/rds/config.sh and rebuild the kernel"
98		exit 4
99	fi
100}
101check_conf_disabled() {
102	if grep -x "$1=y" "$kconfig" > /dev/null 2>&1; then
103		echo "selftests: [SKIP] This test requires $1 disabled"
104		echo "Please run tools/testing/selftests/net/rds/config.sh and rebuild the kernel"
105		exit 4
106	fi
107}
108check_conf() {
109	check_conf_enabled CONFIG_NET_SCH_NETEM
110	check_conf_enabled CONFIG_VETH
111	check_conf_enabled CONFIG_NET_NS
112	check_conf_enabled CONFIG_RDS_TCP
113	check_conf_enabled CONFIG_RDS
114	check_conf_disabled CONFIG_MODULES
115}
116
117check_env()
118{
119	if ! test -d "$obj_dir"; then
120		echo "selftests: [SKIP] This test requires a kernel source tree"
121		exit 4
122	fi
123	if ! test -e "$kconfig"; then
124		echo "selftests: [SKIP] This test requires a configured kernel source tree"
125		exit 4
126	fi
127	if ! which strace > /dev/null 2>&1; then
128		echo "selftests: [SKIP] Could not run test without strace"
129		exit 4
130	fi
131	if ! which tcpdump > /dev/null 2>&1; then
132		echo "selftests: [SKIP] Could not run test without tcpdump"
133		exit 4
134	fi
135
136	if ! which python3 > /dev/null 2>&1; then
137		echo "selftests: [SKIP] Could not run test without python3"
138		exit 4
139	fi
140
141	python_major=$(python3 -c "import sys; print(sys.version_info[0])")
142	python_minor=$(python3 -c "import sys; print(sys.version_info[1])")
143	if [[ python_major -lt 3 || ( python_major -eq 3 && python_minor -lt 9 ) ]] ; then
144		echo "selftests: [SKIP] Could not run test without at least python3.9"
145		python3 -V
146		exit 4
147	fi
148}
149
150LOG_DIR="$current_dir"/rds_logs
151PLOSS=0
152PCORRUPT=0
153PDUP=0
154GENERATE_GCOV_REPORT=1
155while getopts "d:l:c:u:" opt; do
156  case ${opt} in
157    d)
158      LOG_DIR=${OPTARG}
159      ;;
160    l)
161      PLOSS=${OPTARG}
162      ;;
163    c)
164      PCORRUPT=${OPTARG}
165      ;;
166    u)
167      PDUP=${OPTARG}
168      ;;
169    :)
170      echo "USAGE: run.sh [-d logdir] [-l packet_loss] [-c packet_corruption]" \
171           "[-u packet_duplcate] [-g]"
172      exit 1
173      ;;
174    ?)
175      echo "Invalid option: -${OPTARG}."
176      exit 1
177      ;;
178  esac
179done
180
181
182check_env
183check_conf
184check_gcov_conf
185
186
187rm -fr "$LOG_DIR"
188TRACE_FILE="${LOG_DIR}/rds-strace.txt"
189COVR_DIR="${LOG_DIR}/coverage/"
190mkdir -p  "$LOG_DIR"
191mkdir -p "$COVR_DIR"
192
193set +e
194echo running RDS tests...
195echo Traces will be logged to "$TRACE_FILE"
196rm -f "$TRACE_FILE"
197strace -T -tt -o "$TRACE_FILE" python3 "$(dirname "$0")/test.py" --timeout 400 -d "$LOG_DIR" \
198       -l "$PLOSS" -c "$PCORRUPT" -u "$PDUP"
199
200test_rc=$?
201dmesg > "${LOG_DIR}/dmesg.out"
202
203if [ "$GENERATE_GCOV_REPORT" -eq 1 ]; then
204       echo saving coverage data...
205       (set +x; cd /sys/kernel/debug/gcov; find ./* -name '*.gcda' | \
206       while read -r f
207       do
208               cat < "/sys/kernel/debug/gcov/$f" > "/$f"
209       done)
210
211       echo running gcovr...
212       gcovr -s --html-details --gcov-executable "$GCOV_CMD" --gcov-ignore-parse-errors \
213             -o "${COVR_DIR}/gcovr" "${ksrc_dir}/net/rds/"
214else
215       echo "Coverage report will be skipped"
216fi
217
218if [ "$test_rc" -eq 0 ]; then
219	echo "PASS: Test completed successfully"
220else
221	echo "FAIL: Test failed"
222fi
223
224exit "$test_rc"
225