run.sh 6.0 KB

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