drop_monitor_tests.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # This test is for checking drop monitor functionality.
  4. source lib.sh
  5. ret=0
  6. # all tests in this script. Can be overridden with -t option
  7. TESTS="
  8. sw_drops
  9. hw_drops
  10. "
  11. NETDEVSIM_PATH=/sys/bus/netdevsim/
  12. DEV_ADDR=1337
  13. DEV=netdevsim${DEV_ADDR}
  14. DEVLINK_DEV=netdevsim/${DEV}
  15. log_test()
  16. {
  17. local rc=$1
  18. local expected=$2
  19. local msg="$3"
  20. if [ ${rc} -eq ${expected} ]; then
  21. printf " TEST: %-60s [ OK ]\n" "${msg}"
  22. nsuccess=$((nsuccess+1))
  23. else
  24. ret=1
  25. nfail=$((nfail+1))
  26. printf " TEST: %-60s [FAIL]\n" "${msg}"
  27. fi
  28. }
  29. setup()
  30. {
  31. modprobe netdevsim &> /dev/null
  32. set -e
  33. setup_ns NS1
  34. $IP link add dummy10 up type dummy
  35. $NS_EXEC echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
  36. udevadm settle
  37. local netdev=$($NS_EXEC ls ${NETDEVSIM_PATH}/devices/${DEV}/net/)
  38. $IP link set dev $netdev up
  39. set +e
  40. }
  41. cleanup()
  42. {
  43. $NS_EXEC echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
  44. cleanup_ns ${NS1}
  45. }
  46. sw_drops_test()
  47. {
  48. echo
  49. echo "Software drops test"
  50. setup
  51. local dir=$(mktemp -d)
  52. $TC qdisc add dev dummy10 clsact
  53. $TC filter add dev dummy10 egress pref 1 handle 101 proto ip \
  54. flower dst_ip 192.0.2.10 action drop
  55. $NS_EXEC mausezahn dummy10 -a 00:11:22:33:44:55 -b 00:aa:bb:cc:dd:ee \
  56. -A 192.0.2.1 -B 192.0.2.10 -t udp sp=12345,dp=54321 -c 0 -q \
  57. -d 100msec &
  58. timeout 5 dwdump -o sw -w ${dir}/packets.pcap
  59. (( $(tshark -r ${dir}/packets.pcap \
  60. -Y 'ip.dst == 192.0.2.10' 2> /dev/null | wc -l) != 0))
  61. log_test $? 0 "Capturing active software drops"
  62. rm ${dir}/packets.pcap
  63. kill_process %%
  64. timeout 5 dwdump -o sw -w ${dir}/packets.pcap
  65. (( $(tshark -r ${dir}/packets.pcap \
  66. -Y 'ip.dst == 192.0.2.10' 2> /dev/null | wc -l) == 0))
  67. log_test $? 0 "Capturing inactive software drops"
  68. rm -r $dir
  69. cleanup
  70. }
  71. hw_drops_test()
  72. {
  73. echo
  74. echo "Hardware drops test"
  75. setup
  76. local dir=$(mktemp -d)
  77. $DEVLINK trap set $DEVLINK_DEV trap blackhole_route action trap
  78. timeout 5 dwdump -o hw -w ${dir}/packets.pcap
  79. (( $(tshark -r ${dir}/packets.pcap \
  80. -Y 'net_dm.hw_trap_name== blackhole_route' 2> /dev/null \
  81. | wc -l) != 0))
  82. log_test $? 0 "Capturing active hardware drops"
  83. rm ${dir}/packets.pcap
  84. $DEVLINK trap set $DEVLINK_DEV trap blackhole_route action drop
  85. timeout 5 dwdump -o hw -w ${dir}/packets.pcap
  86. (( $(tshark -r ${dir}/packets.pcap \
  87. -Y 'net_dm.hw_trap_name== blackhole_route' 2> /dev/null \
  88. | wc -l) == 0))
  89. log_test $? 0 "Capturing inactive hardware drops"
  90. rm -r $dir
  91. cleanup
  92. }
  93. ################################################################################
  94. # usage
  95. usage()
  96. {
  97. cat <<EOF
  98. usage: ${0##*/} OPTS
  99. -t <test> Test(s) to run (default: all)
  100. (options: $TESTS)
  101. EOF
  102. }
  103. ################################################################################
  104. # main
  105. while getopts ":t:h" opt; do
  106. case $opt in
  107. t) TESTS=$OPTARG;;
  108. h) usage; exit 0;;
  109. *) usage; exit 1;;
  110. esac
  111. done
  112. if [ "$(id -u)" -ne 0 ];then
  113. echo "SKIP: Need root privileges"
  114. exit $ksft_skip;
  115. fi
  116. if [ ! -x "$(command -v ip)" ]; then
  117. echo "SKIP: Could not run test without ip tool"
  118. exit $ksft_skip
  119. fi
  120. if [ ! -x "$(command -v devlink)" ]; then
  121. echo "SKIP: Could not run test without devlink tool"
  122. exit $ksft_skip
  123. fi
  124. if [ ! -x "$(command -v tshark)" ]; then
  125. echo "SKIP: Could not run test without tshark tool"
  126. exit $ksft_skip
  127. fi
  128. if [ ! -x "$(command -v dwdump)" ]; then
  129. echo "SKIP: Could not run test without dwdump tool"
  130. exit $ksft_skip
  131. fi
  132. if [ ! -x "$(command -v udevadm)" ]; then
  133. echo "SKIP: Could not run test without udevadm tool"
  134. exit $ksft_skip
  135. fi
  136. if [ ! -x "$(command -v timeout)" ]; then
  137. echo "SKIP: Could not run test without timeout tool"
  138. exit $ksft_skip
  139. fi
  140. if [ ! -x "$(command -v mausezahn)" ]; then
  141. echo "SKIP: Could not run test without mausezahn tool"
  142. exit $ksft_skip
  143. fi
  144. tshark -G fields 2> /dev/null | grep -q net_dm
  145. if [ $? -ne 0 ]; then
  146. echo "SKIP: tshark too old, missing net_dm dissector"
  147. exit $ksft_skip
  148. fi
  149. # create netns first so we can get the namespace name
  150. setup_ns NS1
  151. cleanup &> /dev/null
  152. trap cleanup EXIT
  153. IP="ip -netns ${NS1}"
  154. TC="tc -netns ${NS1}"
  155. DEVLINK="devlink -N ${NS1}"
  156. NS_EXEC="ip netns exec ${NS1}"
  157. for t in $TESTS
  158. do
  159. case $t in
  160. sw_drops|sw) sw_drops_test;;
  161. hw_drops|hw) hw_drops_test;;
  162. help) echo "Test names: $TESTS"; exit 0;;
  163. esac
  164. done
  165. if [ "$TESTS" != "none" ]; then
  166. printf "\nTests passed: %3d\n" ${nsuccess}
  167. printf "Tests failed: %3d\n" ${nfail}
  168. fi
  169. exit $ret