psample.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # This test is for checking the psample module. It makes use of netdevsim
  5. # which periodically generates "sampled" packets.
  6. lib_dir=$(dirname $0)/../../../net/forwarding
  7. ALL_TESTS="
  8. psample_enable_test
  9. psample_group_num_test
  10. psample_md_test
  11. "
  12. NETDEVSIM_PATH=/sys/bus/netdevsim/
  13. DEV_ADDR=1337
  14. DEV=netdevsim${DEV_ADDR}
  15. SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV/net/
  16. PSAMPLE_DIR=/sys/kernel/debug/netdevsim/$DEV/psample/
  17. CAPTURE_FILE=$(mktemp)
  18. NUM_NETIFS=0
  19. source $lib_dir/lib.sh
  20. DEVLINK_DEV=
  21. source $lib_dir/devlink_lib.sh
  22. DEVLINK_DEV=netdevsim/${DEV}
  23. # Available at https://github.com/Mellanox/libpsample
  24. require_command psample
  25. psample_capture()
  26. {
  27. rm -f $CAPTURE_FILE
  28. timeout 2 ip netns exec testns1 psample &> $CAPTURE_FILE
  29. }
  30. psample_enable_test()
  31. {
  32. RET=0
  33. echo 1 > $PSAMPLE_DIR/enable
  34. check_err $? "Failed to enable sampling when should not"
  35. echo 1 > $PSAMPLE_DIR/enable 2>/dev/null
  36. check_fail $? "Sampling enablement succeeded when should fail"
  37. psample_capture
  38. if [ $(cat $CAPTURE_FILE | wc -l) -eq 0 ]; then
  39. check_err 1 "Failed to capture sampled packets"
  40. fi
  41. echo 0 > $PSAMPLE_DIR/enable
  42. check_err $? "Failed to disable sampling when should not"
  43. echo 0 > $PSAMPLE_DIR/enable 2>/dev/null
  44. check_fail $? "Sampling disablement succeeded when should fail"
  45. psample_capture
  46. if [ $(cat $CAPTURE_FILE | wc -l) -ne 0 ]; then
  47. check_err 1 "Captured sampled packets when should not"
  48. fi
  49. log_test "psample enable / disable"
  50. }
  51. psample_group_num_test()
  52. {
  53. RET=0
  54. echo 1234 > $PSAMPLE_DIR/group_num
  55. echo 1 > $PSAMPLE_DIR/enable
  56. psample_capture
  57. grep -q -e "group 1234" $CAPTURE_FILE
  58. check_err $? "Sampled packets reported with wrong group number"
  59. # New group number should only be used after disable / enable.
  60. echo 4321 > $PSAMPLE_DIR/group_num
  61. psample_capture
  62. grep -q -e "group 4321" $CAPTURE_FILE
  63. check_fail $? "Group number changed while sampling is active"
  64. echo 0 > $PSAMPLE_DIR/enable && echo 1 > $PSAMPLE_DIR/enable
  65. psample_capture
  66. grep -q -e "group 4321" $CAPTURE_FILE
  67. check_err $? "Group number did not change after restarting sampling"
  68. log_test "psample group number"
  69. echo 0 > $PSAMPLE_DIR/enable
  70. }
  71. psample_md_test()
  72. {
  73. RET=0
  74. echo 1 > $PSAMPLE_DIR/enable
  75. echo 1234 > $PSAMPLE_DIR/in_ifindex
  76. echo 4321 > $PSAMPLE_DIR/out_ifindex
  77. psample_capture
  78. grep -q -e "in-ifindex 1234" $CAPTURE_FILE
  79. check_err $? "Sampled packets reported with wrong in-ifindex"
  80. grep -q -e "out-ifindex 4321" $CAPTURE_FILE
  81. check_err $? "Sampled packets reported with wrong out-ifindex"
  82. echo 5 > $PSAMPLE_DIR/out_tc
  83. psample_capture
  84. grep -q -e "out-tc 5" $CAPTURE_FILE
  85. check_err $? "Sampled packets reported with wrong out-tc"
  86. echo $((2**16 - 1)) > $PSAMPLE_DIR/out_tc
  87. psample_capture
  88. grep -q -e "out-tc " $CAPTURE_FILE
  89. check_fail $? "Sampled packets reported with out-tc when should not"
  90. echo 1 > $PSAMPLE_DIR/out_tc
  91. echo 10000 > $PSAMPLE_DIR/out_tc_occ_max
  92. psample_capture
  93. grep -q -e "out-tc-occ " $CAPTURE_FILE
  94. check_err $? "Sampled packets not reported with out-tc-occ when should"
  95. echo 0 > $PSAMPLE_DIR/out_tc_occ_max
  96. psample_capture
  97. grep -q -e "out-tc-occ " $CAPTURE_FILE
  98. check_fail $? "Sampled packets reported with out-tc-occ when should not"
  99. echo 10000 > $PSAMPLE_DIR/latency_max
  100. psample_capture
  101. grep -q -e "latency " $CAPTURE_FILE
  102. check_err $? "Sampled packets not reported with latency when should"
  103. echo 0 > $PSAMPLE_DIR/latency_max
  104. psample_capture
  105. grep -q -e "latency " $CAPTURE_FILE
  106. check_fail $? "Sampled packets reported with latency when should not"
  107. log_test "psample metadata"
  108. echo 0 > $PSAMPLE_DIR/enable
  109. }
  110. setup_prepare()
  111. {
  112. modprobe netdevsim &> /dev/null
  113. echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
  114. while [ ! -d $SYSFS_NET_DIR ] ; do :; done
  115. set -e
  116. ip netns add testns1
  117. devlink dev reload $DEVLINK_DEV netns testns1
  118. set +e
  119. }
  120. cleanup()
  121. {
  122. pre_cleanup
  123. rm -f $CAPTURE_FILE
  124. ip netns del testns1
  125. echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
  126. modprobe -r netdevsim &> /dev/null
  127. }
  128. trap cleanup EXIT
  129. setup_prepare
  130. tests_run
  131. exit $EXIT_STATUS