gre_gso.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # This test is for checking GRE GSO.
  4. ret=0
  5. # Kselftest framework requirement - SKIP code is 4.
  6. ksft_skip=4
  7. # all tests in this script. Can be overridden with -t option
  8. TESTS="gre_gso"
  9. VERBOSE=0
  10. PAUSE_ON_FAIL=no
  11. PAUSE=no
  12. IP="ip -netns ns1"
  13. NS_EXEC="ip netns exec ns1"
  14. TMPFILE=`mktemp`
  15. PID=
  16. log_test()
  17. {
  18. local rc=$1
  19. local expected=$2
  20. local msg="$3"
  21. if [ ${rc} -eq ${expected} ]; then
  22. printf " TEST: %-60s [ OK ]\n" "${msg}"
  23. nsuccess=$((nsuccess+1))
  24. else
  25. ret=1
  26. nfail=$((nfail+1))
  27. printf " TEST: %-60s [FAIL]\n" "${msg}"
  28. if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
  29. echo
  30. echo "hit enter to continue, 'q' to quit"
  31. read a
  32. [ "$a" = "q" ] && exit 1
  33. fi
  34. fi
  35. if [ "${PAUSE}" = "yes" ]; then
  36. echo
  37. echo "hit enter to continue, 'q' to quit"
  38. read a
  39. [ "$a" = "q" ] && exit 1
  40. fi
  41. }
  42. setup()
  43. {
  44. set -e
  45. ip netns add ns1
  46. ip netns set ns1 auto
  47. $IP link set dev lo up
  48. ip link add veth0 type veth peer name veth1
  49. ip link set veth0 up
  50. ip link set veth1 netns ns1
  51. $IP link set veth1 name veth0
  52. $IP link set veth0 up
  53. dd if=/dev/urandom of=$TMPFILE bs=1024 count=2048 &>/dev/null
  54. set +e
  55. }
  56. cleanup()
  57. {
  58. rm -rf $TMPFILE
  59. [ -n "$PID" ] && kill $PID
  60. ip link del dev gre1 &> /dev/null
  61. ip link del dev veth0 &> /dev/null
  62. ip netns del ns1
  63. }
  64. get_linklocal()
  65. {
  66. local dev=$1
  67. local ns=$2
  68. local addr
  69. [ -n "$ns" ] && ns="-netns $ns"
  70. addr=$(ip -6 -br $ns addr show dev ${dev} | \
  71. awk '{
  72. for (i = 3; i <= NF; ++i) {
  73. if ($i ~ /^fe80/)
  74. print $i
  75. }
  76. }'
  77. )
  78. addr=${addr/\/*}
  79. [ -z "$addr" ] && return 1
  80. echo $addr
  81. return 0
  82. }
  83. gre_create_tun()
  84. {
  85. local a1=$1
  86. local a2=$2
  87. local mode
  88. [[ $a1 =~ ^[0-9.]*$ ]] && mode=gre || mode=ip6gre
  89. ip tunnel add gre1 mode $mode local $a1 remote $a2 dev veth0
  90. ip link set gre1 up
  91. $IP tunnel add gre1 mode $mode local $a2 remote $a1 dev veth0
  92. $IP link set gre1 up
  93. }
  94. gre_gst_test_checks()
  95. {
  96. local name=$1
  97. local addr=$2
  98. local proto=$3
  99. [ "$proto" == 6 ] && addr="[$addr]"
  100. $NS_EXEC socat - tcp${proto}-listen:$port,reuseaddr,fork >/dev/null &
  101. PID=$!
  102. while ! $NS_EXEC ss -ltn | grep -q $port; do ((i++)); sleep 0.01; done
  103. cat $TMPFILE | timeout 1 socat -u STDIN TCP:$addr:$port
  104. log_test $? 0 "$name - copy file w/ TSO"
  105. ethtool -K veth0 tso off
  106. cat $TMPFILE | timeout 1 socat -u STDIN TCP:$addr:$port
  107. log_test $? 0 "$name - copy file w/ GSO"
  108. ethtool -K veth0 tso on
  109. kill $PID
  110. PID=
  111. }
  112. gre6_gso_test()
  113. {
  114. local port=7777
  115. setup
  116. a1=$(get_linklocal veth0)
  117. a2=$(get_linklocal veth0 ns1)
  118. gre_create_tun $a1 $a2
  119. ip addr add 172.16.2.1/24 dev gre1
  120. $IP addr add 172.16.2.2/24 dev gre1
  121. ip -6 addr add 2001:db8:1::1/64 dev gre1 nodad
  122. $IP -6 addr add 2001:db8:1::2/64 dev gre1 nodad
  123. sleep 2
  124. gre_gst_test_checks GREv6/v4 172.16.2.2 4
  125. gre_gst_test_checks GREv6/v6 2001:db8:1::2 6
  126. cleanup
  127. }
  128. gre_gso_test()
  129. {
  130. gre6_gso_test
  131. }
  132. ################################################################################
  133. # usage
  134. usage()
  135. {
  136. cat <<EOF
  137. usage: ${0##*/} OPTS
  138. -t <test> Test(s) to run (default: all)
  139. (options: $TESTS)
  140. -p Pause on fail
  141. -P Pause after each test before cleanup
  142. -v verbose mode (show commands and output)
  143. EOF
  144. }
  145. ################################################################################
  146. # main
  147. while getopts :t:pPhv o
  148. do
  149. case $o in
  150. t) TESTS=$OPTARG;;
  151. p) PAUSE_ON_FAIL=yes;;
  152. P) PAUSE=yes;;
  153. v) VERBOSE=$(($VERBOSE + 1));;
  154. h) usage; exit 0;;
  155. *) usage; exit 1;;
  156. esac
  157. done
  158. PEER_CMD="ip netns exec ${PEER_NS}"
  159. # make sure we don't pause twice
  160. [ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
  161. if [ "$(id -u)" -ne 0 ];then
  162. echo "SKIP: Need root privileges"
  163. exit $ksft_skip;
  164. fi
  165. if [ ! -x "$(command -v ip)" ]; then
  166. echo "SKIP: Could not run test without ip tool"
  167. exit $ksft_skip
  168. fi
  169. if [ ! -x "$(command -v socat)" ]; then
  170. echo "SKIP: Could not run test without socat tool"
  171. exit $ksft_skip
  172. fi
  173. # start clean
  174. cleanup &> /dev/null
  175. for t in $TESTS
  176. do
  177. case $t in
  178. gre_gso) gre_gso_test;;
  179. help) echo "Test names: $TESTS"; exit 0;;
  180. esac
  181. done
  182. if [ "$TESTS" != "none" ]; then
  183. printf "\nTests passed: %3d\n" ${nsuccess}
  184. printf "Tests failed: %3d\n" ${nfail}
  185. fi
  186. exit $ret