lib.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. ##############################################################################
  4. # Defines
  5. : "${WAIT_TIMEOUT:=20}"
  6. # Whether to pause on after a failure.
  7. : "${PAUSE_ON_FAIL:=no}"
  8. BUSYWAIT_TIMEOUT=$((WAIT_TIMEOUT * 1000)) # ms
  9. # Kselftest framework constants.
  10. ksft_pass=0
  11. ksft_fail=1
  12. ksft_xfail=2
  13. ksft_skip=4
  14. # namespace list created by setup_ns
  15. NS_LIST=()
  16. # Exit status to return at the end. Set in case one of the tests fails.
  17. EXIT_STATUS=0
  18. # Per-test return value. Clear at the beginning of each test.
  19. RET=0
  20. ##############################################################################
  21. # Helpers
  22. __ksft_status_merge()
  23. {
  24. local a=$1; shift
  25. local b=$1; shift
  26. local -A weights
  27. local weight=0
  28. local i
  29. for i in "$@"; do
  30. weights[$i]=$((weight++))
  31. done
  32. if [[ ${weights[$a]} > ${weights[$b]} ]]; then
  33. echo "$a"
  34. return 0
  35. else
  36. echo "$b"
  37. return 1
  38. fi
  39. }
  40. ksft_status_merge()
  41. {
  42. local a=$1; shift
  43. local b=$1; shift
  44. __ksft_status_merge "$a" "$b" \
  45. $ksft_pass $ksft_xfail $ksft_skip $ksft_fail
  46. }
  47. ksft_exit_status_merge()
  48. {
  49. local a=$1; shift
  50. local b=$1; shift
  51. __ksft_status_merge "$a" "$b" \
  52. $ksft_xfail $ksft_pass $ksft_skip $ksft_fail
  53. }
  54. loopy_wait()
  55. {
  56. local sleep_cmd=$1; shift
  57. local timeout_ms=$1; shift
  58. local start_time="$(date -u +%s%3N)"
  59. while true
  60. do
  61. local out
  62. if out=$("$@"); then
  63. echo -n "$out"
  64. return 0
  65. fi
  66. local current_time="$(date -u +%s%3N)"
  67. if ((current_time - start_time > timeout_ms)); then
  68. echo -n "$out"
  69. return 1
  70. fi
  71. $sleep_cmd
  72. done
  73. }
  74. busywait()
  75. {
  76. local timeout_ms=$1; shift
  77. loopy_wait : "$timeout_ms" "$@"
  78. }
  79. # timeout in seconds
  80. slowwait()
  81. {
  82. local timeout_sec=$1; shift
  83. loopy_wait "sleep 0.1" "$((timeout_sec * 1000))" "$@"
  84. }
  85. until_counter_is()
  86. {
  87. local expr=$1; shift
  88. local current=$("$@")
  89. echo $((current))
  90. ((current $expr))
  91. }
  92. busywait_for_counter()
  93. {
  94. local timeout=$1; shift
  95. local delta=$1; shift
  96. local base=$("$@")
  97. busywait "$timeout" until_counter_is ">= $((base + delta))" "$@"
  98. }
  99. slowwait_for_counter()
  100. {
  101. local timeout=$1; shift
  102. local delta=$1; shift
  103. local base=$("$@")
  104. slowwait "$timeout" until_counter_is ">= $((base + delta))" "$@"
  105. }
  106. # Check for existence of tools which are built as part of selftests
  107. # but may also already exist in $PATH
  108. check_gen_prog()
  109. {
  110. local prog_name=$1; shift
  111. if ! which $prog_name >/dev/null 2>/dev/null; then
  112. PATH=$PWD:$PATH
  113. if ! which $prog_name >/dev/null; then
  114. echo "'$prog_name' command not found; skipping tests"
  115. exit $ksft_skip
  116. fi
  117. fi
  118. }
  119. remove_ns_list()
  120. {
  121. local item=$1
  122. local ns
  123. local ns_list=("${NS_LIST[@]}")
  124. NS_LIST=()
  125. for ns in "${ns_list[@]}"; do
  126. if [ "${ns}" != "${item}" ]; then
  127. NS_LIST+=("${ns}")
  128. fi
  129. done
  130. }
  131. cleanup_ns()
  132. {
  133. local ns=""
  134. local ret=0
  135. for ns in "$@"; do
  136. [ -z "${ns}" ] && continue
  137. ip netns pids "${ns}" 2> /dev/null | xargs -r kill || true
  138. ip netns delete "${ns}" &> /dev/null || true
  139. if ! busywait $BUSYWAIT_TIMEOUT ip netns list \| grep -vq "^$ns$" &> /dev/null; then
  140. echo "Warn: Failed to remove namespace $ns"
  141. ret=1
  142. else
  143. remove_ns_list "${ns}"
  144. fi
  145. done
  146. return $ret
  147. }
  148. cleanup_all_ns()
  149. {
  150. cleanup_ns "${NS_LIST[@]}"
  151. }
  152. # setup netns with given names as prefix. e.g
  153. # setup_ns local remote
  154. setup_ns()
  155. {
  156. local ns_name=""
  157. local ns_list=()
  158. for ns_name in "$@"; do
  159. # avoid conflicts with local var: internal error
  160. if [ "${ns_name}" = "ns_name" ]; then
  161. echo "Failed to setup namespace '${ns_name}': invalid name"
  162. cleanup_ns "${ns_list[@]}"
  163. exit $ksft_fail
  164. fi
  165. # Some test may setup/remove same netns multi times
  166. if [ -z "${!ns_name}" ]; then
  167. eval "${ns_name}=${ns_name,,}-$(mktemp -u XXXXXX)"
  168. else
  169. cleanup_ns "${!ns_name}"
  170. fi
  171. if ! ip netns add "${!ns_name}"; then
  172. echo "Failed to create namespace $ns_name"
  173. cleanup_ns "${ns_list[@]}"
  174. return $ksft_skip
  175. fi
  176. ip -n "${!ns_name}" link set lo up
  177. ns_list+=("${!ns_name}")
  178. done
  179. NS_LIST+=("${ns_list[@]}")
  180. }
  181. tc_rule_stats_get()
  182. {
  183. local dev=$1; shift
  184. local pref=$1; shift
  185. local dir=${1:-ingress}; shift
  186. local selector=${1:-.packets}; shift
  187. tc -j -s filter show dev $dev $dir pref $pref \
  188. | jq ".[1].options.actions[].stats$selector"
  189. }
  190. tc_rule_handle_stats_get()
  191. {
  192. local id=$1; shift
  193. local handle=$1; shift
  194. local selector=${1:-.packets}; shift
  195. local netns=${1:-""}; shift
  196. tc $netns -j -s filter show $id \
  197. | jq ".[] | select(.options.handle == $handle) | \
  198. .options.actions[0].stats$selector"
  199. }
  200. ret_set_ksft_status()
  201. {
  202. local ksft_status=$1; shift
  203. local msg=$1; shift
  204. RET=$(ksft_status_merge $RET $ksft_status)
  205. if (( $? )); then
  206. retmsg=$msg
  207. fi
  208. }
  209. log_test_result()
  210. {
  211. local test_name=$1; shift
  212. local opt_str=$1; shift
  213. local result=$1; shift
  214. local retmsg=$1
  215. printf "TEST: %-60s [%s]\n" "$test_name $opt_str" "$result"
  216. if [[ $retmsg ]]; then
  217. printf "\t%s\n" "$retmsg"
  218. fi
  219. }
  220. pause_on_fail()
  221. {
  222. if [[ $PAUSE_ON_FAIL == yes ]]; then
  223. echo "Hit enter to continue, 'q' to quit"
  224. read a
  225. [[ $a == q ]] && exit 1
  226. fi
  227. }
  228. handle_test_result_pass()
  229. {
  230. local test_name=$1; shift
  231. local opt_str=$1; shift
  232. log_test_result "$test_name" "$opt_str" " OK "
  233. }
  234. handle_test_result_fail()
  235. {
  236. local test_name=$1; shift
  237. local opt_str=$1; shift
  238. log_test_result "$test_name" "$opt_str" FAIL "$retmsg"
  239. pause_on_fail
  240. }
  241. handle_test_result_xfail()
  242. {
  243. local test_name=$1; shift
  244. local opt_str=$1; shift
  245. log_test_result "$test_name" "$opt_str" XFAIL "$retmsg"
  246. pause_on_fail
  247. }
  248. handle_test_result_skip()
  249. {
  250. local test_name=$1; shift
  251. local opt_str=$1; shift
  252. log_test_result "$test_name" "$opt_str" SKIP "$retmsg"
  253. }
  254. log_test()
  255. {
  256. local test_name=$1
  257. local opt_str=$2
  258. if [[ $# -eq 2 ]]; then
  259. opt_str="($opt_str)"
  260. fi
  261. if ((RET == ksft_pass)); then
  262. handle_test_result_pass "$test_name" "$opt_str"
  263. elif ((RET == ksft_xfail)); then
  264. handle_test_result_xfail "$test_name" "$opt_str"
  265. elif ((RET == ksft_skip)); then
  266. handle_test_result_skip "$test_name" "$opt_str"
  267. else
  268. handle_test_result_fail "$test_name" "$opt_str"
  269. fi
  270. EXIT_STATUS=$(ksft_exit_status_merge $EXIT_STATUS $RET)
  271. return $RET
  272. }
  273. log_test_skip()
  274. {
  275. RET=$ksft_skip retmsg= log_test "$@"
  276. }
  277. log_test_xfail()
  278. {
  279. RET=$ksft_xfail retmsg= log_test "$@"
  280. }
  281. log_info()
  282. {
  283. local msg=$1
  284. echo "INFO: $msg"
  285. }