test_xsk.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Copyright(c) 2020 Intel Corporation, Weqaar Janjua <[email protected]>
  4. # AF_XDP selftests based on veth
  5. #
  6. # End-to-end AF_XDP over Veth test
  7. #
  8. # Topology:
  9. # ---------
  10. # -----------
  11. # _ | Process | _
  12. # / ----------- \
  13. # / | \
  14. # / | \
  15. # ----------- | -----------
  16. # | Thread1 | | | Thread2 |
  17. # ----------- | -----------
  18. # | | |
  19. # ----------- | -----------
  20. # | xskX | | | xskY |
  21. # ----------- | -----------
  22. # | | |
  23. # ----------- | ----------
  24. # | vethX | --------- | vethY |
  25. # ----------- peer ----------
  26. # | | |
  27. # namespaceX | namespaceY
  28. #
  29. # AF_XDP is an address family optimized for high performance packet processing,
  30. # it is XDP’s user-space interface.
  31. #
  32. # An AF_XDP socket is linked to a single UMEM which is a region of virtual
  33. # contiguous memory, divided into equal-sized frames.
  34. #
  35. # Refer to AF_XDP Kernel Documentation for detailed information:
  36. # https://www.kernel.org/doc/html/latest/networking/af_xdp.html
  37. #
  38. # Prerequisites setup by script:
  39. #
  40. # Set up veth interfaces as per the topology shown ^^:
  41. # * setup two veth interfaces and one namespace
  42. # ** veth<xxxx> in root namespace
  43. # ** veth<yyyy> in af_xdp<xxxx> namespace
  44. # ** namespace af_xdp<xxxx>
  45. # *** xxxx and yyyy are randomly generated 4 digit numbers used to avoid
  46. # conflict with any existing interface
  47. # * tests the veth and xsk layers of the topology
  48. #
  49. # See the source xskxceiver.c for information on each test
  50. #
  51. # Kernel configuration:
  52. # ---------------------
  53. # See "config" file for recommended kernel config options.
  54. #
  55. # Turn on XDP sockets and veth support when compiling i.e.
  56. # Networking support -->
  57. # Networking options -->
  58. # [ * ] XDP sockets
  59. #
  60. # Executing Tests:
  61. # ----------------
  62. # Must run with CAP_NET_ADMIN capability.
  63. #
  64. # Run:
  65. # sudo ./test_xsk.sh
  66. #
  67. # If running from kselftests:
  68. # sudo make run_tests
  69. #
  70. # Run with verbose output:
  71. # sudo ./test_xsk.sh -v
  72. #
  73. # Run and dump packet contents:
  74. # sudo ./test_xsk.sh -D
  75. #
  76. # Run test suite for physical device in loopback mode
  77. # sudo ./test_xsk.sh -i IFACE
  78. . xsk_prereqs.sh
  79. ETH=""
  80. while getopts "vDi:" flag
  81. do
  82. case "${flag}" in
  83. v) verbose=1;;
  84. D) dump_pkts=1;;
  85. i) ETH=${OPTARG};;
  86. esac
  87. done
  88. TEST_NAME="PREREQUISITES"
  89. URANDOM=/dev/urandom
  90. [ ! -e "${URANDOM}" ] && { echo "${URANDOM} not found. Skipping tests."; test_exit $ksft_fail; }
  91. VETH0_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
  92. VETH0=ve${VETH0_POSTFIX}
  93. VETH1_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
  94. VETH1=ve${VETH1_POSTFIX}
  95. NS0=root
  96. NS1=af_xdp${VETH1_POSTFIX}
  97. MTU=1500
  98. trap ctrl_c INT
  99. function ctrl_c() {
  100. cleanup_exit ${VETH0} ${VETH1} ${NS1}
  101. exit 1
  102. }
  103. setup_vethPairs() {
  104. if [[ $verbose -eq 1 ]]; then
  105. echo "setting up ${VETH0}: namespace: ${NS0}"
  106. fi
  107. ip netns add ${NS1}
  108. ip link add ${VETH0} numtxqueues 4 numrxqueues 4 type veth peer name ${VETH1} numtxqueues 4 numrxqueues 4
  109. if [ -f /proc/net/if_inet6 ]; then
  110. echo 1 > /proc/sys/net/ipv6/conf/${VETH0}/disable_ipv6
  111. echo 1 > /proc/sys/net/ipv6/conf/${VETH1}/disable_ipv6
  112. fi
  113. if [[ $verbose -eq 1 ]]; then
  114. echo "setting up ${VETH1}: namespace: ${NS1}"
  115. fi
  116. if [[ $busy_poll -eq 1 ]]; then
  117. echo 2 > /sys/class/net/${VETH0}/napi_defer_hard_irqs
  118. echo 200000 > /sys/class/net/${VETH0}/gro_flush_timeout
  119. echo 2 > /sys/class/net/${VETH1}/napi_defer_hard_irqs
  120. echo 200000 > /sys/class/net/${VETH1}/gro_flush_timeout
  121. fi
  122. ip link set ${VETH1} netns ${NS1}
  123. ip netns exec ${NS1} ip link set ${VETH1} mtu ${MTU}
  124. ip link set ${VETH0} mtu ${MTU}
  125. ip netns exec ${NS1} ip link set ${VETH1} up
  126. ip netns exec ${NS1} ip link set dev lo up
  127. ip link set ${VETH0} up
  128. }
  129. if [ ! -z $ETH ]; then
  130. VETH0=${ETH}
  131. VETH1=${ETH}
  132. NS1=""
  133. else
  134. validate_root_exec
  135. validate_veth_support ${VETH0}
  136. validate_ip_utility
  137. setup_vethPairs
  138. retval=$?
  139. if [ $retval -ne 0 ]; then
  140. test_status $retval "${TEST_NAME}"
  141. cleanup_exit ${VETH0} ${VETH1} ${NS1}
  142. exit $retval
  143. fi
  144. fi
  145. if [[ $verbose -eq 1 ]]; then
  146. ARGS+="-v "
  147. fi
  148. if [[ $dump_pkts -eq 1 ]]; then
  149. ARGS="-D "
  150. fi
  151. retval=$?
  152. test_status $retval "${TEST_NAME}"
  153. ## START TESTS
  154. statusList=()
  155. TEST_NAME="XSK_SELFTESTS_${VETH0}_SOFTIRQ"
  156. exec_xskxceiver
  157. if [ -z $ETH ]; then
  158. cleanup_exit ${VETH0} ${VETH1} ${NS1}
  159. fi
  160. TEST_NAME="XSK_SELFTESTS_${VETH0}_BUSY_POLL"
  161. busy_poll=1
  162. if [ -z $ETH ]; then
  163. setup_vethPairs
  164. fi
  165. exec_xskxceiver
  166. ## END TESTS
  167. if [ -z $ETH ]; then
  168. cleanup_exit ${VETH0} ${VETH1} ${NS1}
  169. fi
  170. failures=0
  171. echo -e "\nSummary:"
  172. for i in "${!statusList[@]}"
  173. do
  174. if [ ${statusList[$i]} -ne 0 ]; then
  175. test_status ${statusList[$i]} ${nameList[$i]}
  176. failures=1
  177. fi
  178. done
  179. if [ $failures -eq 0 ]; then
  180. echo "All tests successful!"
  181. fi