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