gre_gso.sh 4.0 KB

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