225 lines · bash
1#! /bin/bash2# SPDX-License-Identifier: GPL-2.03 4set -e5set -u6 7unset KBUILD_OUTPUT8 9current_dir="$(realpath "$(dirname "$0")")"10build_dir="$current_dir"11 12build_include="$current_dir/include.sh"13if test -f "$build_include"; then14 # this include will define "$mk_build_dir" as the location the test was15 # built. We will need this if the tests are installed in a location16 # other than the kernel source17 18 source "$build_include"19 build_dir="$mk_build_dir"20fi21 22# This test requires kernel source and the *.gcda data therein23# Locate the top level of the kernel source, and the net/rds24# subfolder with the appropriate *.gcno object files25ksrc_dir="$(realpath "$build_dir"/../../../../../)"26kconfig="$ksrc_dir/.config"27obj_dir="$ksrc_dir/net/rds"28 29GCOV_CMD=gcov30 31#check to see if the host has the required packages to generate a gcov report32check_gcov_env()33{34 if ! which "$GCOV_CMD" > /dev/null 2>&1; then35 echo "Warning: Could not find gcov. "36 GENERATE_GCOV_REPORT=037 return38 fi39 40 # the gcov version must match the gcc version41 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" ]; then44 #attempt to find a matching gcov version45 GCOV_CMD=gcov-$(gcc -dumpversion)46 47 if ! which "$GCOV_CMD" > /dev/null 2>&1; then48 echo "Warning: Could not find an appropriate gcov installation. \49 gcov version must match gcc version"50 GENERATE_GCOV_REPORT=051 return52 fi53 54 #recheck version number of found gcov executable55 GCOV_VER=$($GCOV_CMD -v | grep gcov | awk '{print $3}'| \56 awk 'BEGIN {FS="-"}{print $1}')57 if [ "$GCOV_VER" != "$GCC_VER" ]; then58 echo "Warning: Could not find an appropriate gcov installation. \59 gcov version must match gcc version"60 GENERATE_GCOV_REPORT=061 else62 echo "Warning: Mismatched gcc and gcov detected. Using $GCOV_CMD"63 fi64 fi65}66 67# Check to see if the kconfig has the required configs to generate a coverage report68check_gcov_conf()69{70 if ! grep -x "CONFIG_GCOV_PROFILE_RDS=y" "$kconfig" > /dev/null 2>&1; then71 echo "INFO: CONFIG_GCOV_PROFILE_RDS should be enabled for coverage reports"72 GENERATE_GCOV_REPORT=073 fi74 if ! grep -x "CONFIG_GCOV_KERNEL=y" "$kconfig" > /dev/null 2>&1; then75 echo "INFO: CONFIG_GCOV_KERNEL should be enabled for coverage reports"76 GENERATE_GCOV_REPORT=077 fi78 if grep -x "CONFIG_GCOV_PROFILE_ALL=y" "$kconfig" > /dev/null 2>&1; then79 echo "INFO: CONFIG_GCOV_PROFILE_ALL should be disabled for coverage reports"80 GENERATE_GCOV_REPORT=081 fi82 83 if [ "$GENERATE_GCOV_REPORT" -eq 0 ]; then84 echo "To enable gcov reports, please run "\85 "\"tools/testing/selftests/net/rds/config.sh -g\" and rebuild the kernel"86 else87 # if we have the required kernel configs, proceed to check the environment to88 # ensure we have the required gcov packages89 check_gcov_env90 fi91}92 93# Kselftest framework requirement - SKIP code is 4.94check_conf_enabled() {95 if ! grep -x "$1=y" "$kconfig" > /dev/null 2>&1; then96 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 499 fi100}101check_conf_disabled() {102 if grep -x "$1=y" "$kconfig" > /dev/null 2>&1; then103 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 4106 fi107}108check_conf() {109 check_conf_enabled CONFIG_NET_SCH_NETEM110 check_conf_enabled CONFIG_VETH111 check_conf_enabled CONFIG_NET_NS112 check_conf_enabled CONFIG_RDS_TCP113 check_conf_enabled CONFIG_RDS114 check_conf_disabled CONFIG_MODULES115}116 117check_env()118{119 if ! test -d "$obj_dir"; then120 echo "selftests: [SKIP] This test requires a kernel source tree"121 exit 4122 fi123 if ! test -e "$kconfig"; then124 echo "selftests: [SKIP] This test requires a configured kernel source tree"125 exit 4126 fi127 if ! which strace > /dev/null 2>&1; then128 echo "selftests: [SKIP] Could not run test without strace"129 exit 4130 fi131 if ! which tcpdump > /dev/null 2>&1; then132 echo "selftests: [SKIP] Could not run test without tcpdump"133 exit 4134 fi135 136 if ! which python3 > /dev/null 2>&1; then137 echo "selftests: [SKIP] Could not run test without python3"138 exit 4139 fi140 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 ) ]] ; then144 echo "selftests: [SKIP] Could not run test without at least python3.9"145 python3 -V146 exit 4147 fi148}149 150LOG_DIR="$current_dir"/rds_logs151PLOSS=0152PCORRUPT=0153PDUP=0154GENERATE_GCOV_REPORT=1155while getopts "d:l:c:u:" opt; do156 case ${opt} in157 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 1173 ;;174 ?)175 echo "Invalid option: -${OPTARG}."176 exit 1177 ;;178 esac179done180 181 182check_env183check_conf184check_gcov_conf185 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 +e194echo 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 ]; then204 echo saving coverage data...205 (set +x; cd /sys/kernel/debug/gcov; find ./* -name '*.gcda' | \206 while read -r f207 do208 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/"214else215 echo "Coverage report will be skipped"216fi217 218if [ "$test_rc" -eq 0 ]; then219 echo "PASS: Test completed successfully"220else221 echo "FAIL: Test failed"222fi223 224exit "$test_rc"225