virt_lib.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) 2018-2019 Petr Vorel <pvorel@suse.cz>
  4. # Copyright (c) 2014-2017 Oracle and/or its affiliates. All Rights Reserved.
  5. # Author: Alexey Kodanev <alexey.kodanev@oracle.com>
  6. #
  7. # VxLAN
  8. # -----
  9. # Virtual eXtensible Local Area Network (VxLAN) provides L2 networks
  10. # over existed L3 networks. It is using UDP (port 8472) to encapsulate
  11. # data packets. More information:
  12. # http://tools.ietf.org/html/draft-mahalingam-dutt-dcops-vxlan-08
  13. #
  14. # Warning: Test assumes that machines don't have any existed VxLANs.
  15. # If machine has VxLANs, the test might fail or eventually delete
  16. # them in cleanup function. See "start_vni" variable which can
  17. # solve it.
  18. TST_SETUP="${TST_SETUP:-virt_lib_setup}"
  19. TST_CLEANUP="${TST_CLEANUP:-cleanup_vifaces}"
  20. virt_lib_usage()
  21. {
  22. echo "i n start ID to use"
  23. echo "d x VxLAN destination address, 'uni' or 'multi'"
  24. }
  25. virt_lib_parse_args()
  26. {
  27. case "$1" in
  28. i) start_id=$2 ;;
  29. d) vxlan_dst_addr=$2 ;;
  30. esac
  31. }
  32. virt_lib_setup()
  33. {
  34. case "$virt_type" in
  35. vxlan|geneve)
  36. if tst_kvcmp -lt "3.8"; then
  37. tst_brk TCONF "test must be run with kernel 3.8 or newer"
  38. fi
  39. if [ "$TST_IPV6" ] && tst_kvcmp -lt "3.12"; then
  40. tst_brk TCONF "test must be run with kernels >= 3.12"
  41. fi
  42. # newer versions of 'ip' complain if this option not set
  43. ip li add type vxlan help 2>&1 | grep -q dstport && vxlan_dstport=1
  44. ;;
  45. esac
  46. tst_require_cmds "ip"
  47. virt_add ltp_v0 || \
  48. tst_brk TCONF "iproute2 or kernel doesn't support $virt_type"
  49. ROD_SILENT "ip link delete ltp_v0"
  50. }
  51. TST_NEEDS_ROOT=1
  52. . tst_net.sh
  53. ip_virt_local="$(TST_IPV6= tst_ipaddr_un)"
  54. ip6_virt_local="$(TST_IPV6=6 tst_ipaddr_un)"
  55. ip_virt_remote="$(TST_IPV6= tst_ipaddr_un rhost)"
  56. ip6_virt_remote="$(TST_IPV6=6 tst_ipaddr_un rhost)"
  57. vxlan_dstport=0
  58. # Max performance loss (%) for virtual devices during network load
  59. VIRT_PERF_THRESHOLD=${VIRT_PERF_THRESHOLD:-80}
  60. if [ -n "$VIRT_PERF_THRESHOLD_MIN" ] && [ "$VIRT_PERF_THRESHOLD" -lt $VIRT_PERF_THRESHOLD_MIN ]; then
  61. VIRT_PERF_THRESHOLD="$VIRT_PERF_THRESHOLD_MIN"
  62. fi
  63. cleanup_vifaces()
  64. {
  65. tst_res TINFO "cleanup virtual interfaces..."
  66. local viface=`ip li | sed -nE 's/^[0-9]+: (ltp_v[0-9]+)[@:].+/\1/p'`
  67. for vx in $viface; do
  68. ip link delete $vx
  69. done
  70. }
  71. virt_cleanup_rmt()
  72. {
  73. cleanup_vifaces
  74. tst_rhost_run -c "ip link delete ltp_v0 2>/dev/null"
  75. if [ "$virt_tcp_syn" ]; then
  76. sysctl -q net.ipv4.tcp_syn_retries=$virt_tcp_syn
  77. virt_tcp_syn=
  78. fi
  79. }
  80. virt_cleanup()
  81. {
  82. virt_cleanup_rmt
  83. }
  84. virt_add()
  85. {
  86. local vname=$1
  87. shift
  88. local opt="$*"
  89. case $virt_type in
  90. vlan|vxlan)
  91. [ -z "$opt" ] && opt="id 4094"
  92. [ "$vxlan_dstport" -eq 1 ] && opt="dstport 0 $opt"
  93. [ "$virt_type" = "vxlan" ] && opt="$opt dev $(tst_iface)"
  94. ;;
  95. geneve)
  96. [ -z "$opt" ] && opt="id 4094 remote $(tst_ipaddr rhost)"
  97. ;;
  98. gre|ip6gre)
  99. [ -z "$opt" ] && \
  100. opt="remote $(tst_ipaddr rhost) dev $(tst_iface)"
  101. ;;
  102. sit)
  103. [ -z "$opt" ] && opt="remote $(tst_ipaddr rhost) local $(tst_ipaddr)"
  104. ;;
  105. esac
  106. case $virt_type in
  107. vxlan|geneve|sit)
  108. ip li add $vname type $virt_type $opt
  109. ;;
  110. gre|ip6gre)
  111. ip -f inet$TST_IPV6 tu add $vname mode $virt_type $opt
  112. ;;
  113. *)
  114. ip li add link $(tst_iface) $vname type $virt_type $opt
  115. ;;
  116. esac
  117. }
  118. virt_add_rhost()
  119. {
  120. local opt=""
  121. case $virt_type in
  122. vxlan|geneve)
  123. [ "$virt_type" = "vxlan" ] && opt="dev $(tst_iface rhost)"
  124. [ "$vxlan_dstport" -eq 1 ] && opt="$opt dstport 0"
  125. tst_rhost_run -s -c "ip li add ltp_v0 type $virt_type $@ $opt"
  126. ;;
  127. sit)
  128. tst_rhost_run -s -c "ip link add ltp_v0 type $virt_type $@"
  129. ;;
  130. gre|ip6gre)
  131. tst_rhost_run -s -c "ip -f inet$TST_IPV6 tu add ltp_v0 \
  132. mode $virt_type $@"
  133. ;;
  134. *)
  135. tst_rhost_run -s -c "ip li add link $(tst_iface rhost) ltp_v0 \
  136. type $virt_type $@"
  137. ;;
  138. esac
  139. }
  140. virt_multiple_add_test()
  141. {
  142. local opt="$@"
  143. local max=$(($start_id + $NS_TIMES - 1))
  144. local i
  145. tst_res TINFO "add $NS_TIMES $virt_type, then delete"
  146. for i in $(seq $start_id $max); do
  147. virt_add ltp_v$i id $i $opt || \
  148. tst_brk TFAIL "failed to create 'ltp_v0 $opt'"
  149. ROD_SILENT "ip link set ltp_v$i up"
  150. done
  151. for i in $(seq $start_id $max); do
  152. ROD_SILENT "ip link set ltp_v$i down"
  153. ROD_SILENT "ip link delete ltp_v$i"
  154. done
  155. tst_res TPASS "done"
  156. }
  157. virt_add_delete_test()
  158. {
  159. local opt="$@"
  160. local max=$(($NS_TIMES - 1))
  161. local i
  162. tst_res TINFO "add/del $virt_type $NS_TIMES times"
  163. for i in $(seq 0 $max); do
  164. virt_add ltp_v0 $opt || \
  165. tst_brk TFAIL "failed to create 'ltp_v0 $opt'"
  166. ROD_SILENT "ip link set ltp_v0 up"
  167. ROD_SILENT "ip link delete ltp_v0"
  168. done
  169. tst_res TPASS "done"
  170. }
  171. virt_setup()
  172. {
  173. local opt="$1"
  174. local opt_r="${2:-$1}"
  175. tst_res TINFO "setup local ${virt_type} with '$opt'"
  176. virt_add ltp_v0 $opt || \
  177. tst_brk TBROK "failed to create 'ltp_v0 $opt'"
  178. tst_res TINFO "setup rhost ${virt_type} with '$opt_r'"
  179. virt_add_rhost "$opt_r"
  180. ROD_SILENT "ip addr add ${ip6_virt_local}/64 dev ltp_v0 nodad"
  181. tst_rhost_run -s -c "ip ad add ${ip6_virt_remote}/64 dev ltp_v0 nodad"
  182. ROD_SILENT "ip addr add ${ip_virt_local}/24 dev ltp_v0"
  183. tst_rhost_run -s -c "ip addr add ${ip_virt_remote}/24 dev ltp_v0"
  184. ROD_SILENT "sysctl -q net.ipv6.conf.ltp_v0.accept_dad=0"
  185. tst_rhost_run -s -c "sysctl -q net.ipv6.conf.ltp_v0.accept_dad=0"
  186. ROD_SILENT "ip li set up ltp_v0"
  187. tst_rhost_run -s -c "ip li set up ltp_v0"
  188. }
  189. virt_tcp_syn=
  190. virt_minimize_timeout()
  191. {
  192. local mac_loc="$(cat /sys/class/net/ltp_v0/address)"
  193. local mac_rmt="$(tst_rhost_run -c 'cat /sys/class/net/ltp_v0/address')"
  194. ROD_SILENT "ip ne replace $ip_virt_remote lladdr \
  195. $mac_rmt nud permanent dev ltp_v0"
  196. tst_rhost_run -s -c "ip ne replace $ip_virt_local lladdr \
  197. $mac_loc nud permanent dev ltp_v0"
  198. virt_tcp_syn=$(sysctl -n net.ipv4.tcp_syn_retries)
  199. ROD sysctl -q net.ipv4.tcp_syn_retries=1
  200. }
  201. vxlan_setup_subnet_uni()
  202. {
  203. if tst_kvcmp -lt "3.10"; then
  204. tst_brk TCONF "test must be run with kernel 3.10 or newer"
  205. fi
  206. [ "$(ip li add type $virt_type help 2>&1 | grep remote)" ] || \
  207. tst_brk TCONF "iproute doesn't support remote unicast address"
  208. local opt="$1 remote $(tst_ipaddr rhost)"
  209. local opt_r="$2 remote $(tst_ipaddr)"
  210. virt_setup "$opt" "$opt_r"
  211. }
  212. vxlan_setup_subnet_multi()
  213. {
  214. tst_require_cmds "od"
  215. local b1=$(($(od -An -d -N1 /dev/urandom) % 254 + 1))
  216. local b2=$(($(od -An -d -N1 /dev/urandom) % 254 + 1))
  217. local b3=$(($(od -An -d -N1 /dev/urandom) % 254 + 1))
  218. local grp=
  219. if [ "$TST_IPV6" ]; then
  220. grp="group ff05::$(printf '%x:%x%x' $b1 $b2 $b3)"
  221. else
  222. grp="group 239.$b1.$b2.$b3"
  223. fi
  224. local opt="$1 $grp"
  225. local opt_r="$2 $grp"
  226. virt_setup "$opt" "$opt_r"
  227. }
  228. virt_compare_netperf()
  229. {
  230. local ret1="pass"
  231. local ret2="pass"
  232. local expect_res="${1:-pass}"
  233. local opts="$2"
  234. tst_netload -H $ip_virt_remote $opts -d res_ipv4 -e $expect_res \
  235. -D ltp_v0 || ret1="fail"
  236. tst_netload -H ${ip6_virt_remote} $opts -d res_ipv6 -e $expect_res \
  237. -D ltp_v0 || ret2="fail"
  238. [ "$ret1" = "fail" -o "$ret2" = "fail" ] && return
  239. local vt="$(cat res_ipv4)"
  240. local vt6="$(cat res_ipv6)"
  241. tst_netload -H $(tst_ipaddr rhost) $opts -d res_lan
  242. local lt="$(cat res_lan)"
  243. tst_res TINFO "time lan IPv${TST_IPVER}($lt) $virt_type IPv4($vt) and IPv6($vt6) ms"
  244. per=$(( $vt * 100 / $lt - 100 ))
  245. per6=$(( $vt6 * 100 / $lt - 100 ))
  246. case "$virt_type" in
  247. vxlan|geneve)
  248. tst_res TINFO "IP4 $virt_type over IP$TST_IPVER slower by $per %"
  249. tst_res TINFO "IP6 $virt_type over IP$TST_IPVER slower by $per6 %"
  250. ;;
  251. *)
  252. tst_res TINFO "IP4 $virt_type slower by $per %"
  253. tst_res TINFO "IP6 $virt_type slower by $per6 %"
  254. esac
  255. if [ "$per" -ge "$VIRT_PERF_THRESHOLD" -o \
  256. "$per6" -ge "$VIRT_PERF_THRESHOLD" ]; then
  257. tst_res TFAIL "Test failed, threshold: $VIRT_PERF_THRESHOLD %"
  258. else
  259. tst_res TPASS "Test passed, threshold: $VIRT_PERF_THRESHOLD %"
  260. fi
  261. }
  262. virt_check_cmd()
  263. {
  264. $@ > /dev/null 2>&1
  265. if [ $? -ne 0 ]; then
  266. tst_res TCONF "'$@' option(s) not supported, skipping it"
  267. return 1
  268. fi
  269. ROD_SILENT "ip li delete ltp_v0"
  270. return 0
  271. }
  272. virt_netperf_msg_sizes()
  273. {
  274. local sizes="100 1000 2000 $TST_NET_MAX_PKT"
  275. local s
  276. for s in $sizes; do
  277. virt_compare_netperf pass "-n $s -N $s"
  278. done
  279. }
  280. virt_netperf_rand_sizes()
  281. {
  282. local max_pkt_size="$TST_NET_MAX_PKT"
  283. local types="tcp udp udp_lite"
  284. for t in $types; do
  285. virt_compare_netperf pass "-A $max_pkt_size -T $t"
  286. done
  287. }
  288. # Check if we can create then delete virtual interface.
  289. virt_test_01()
  290. {
  291. start_id="${start_id:-1}"
  292. tst_res TINFO "add $virt_type with '$2'"
  293. virt_check_cmd virt_add ltp_v0 id 0 $2 || return
  294. virt_multiple_add_test "$2"
  295. }
  296. # Check if we can create then delete virtual interface.
  297. virt_test_02()
  298. {
  299. start_id="${start_id:-1}"
  300. tst_res TINFO "add and then delete $virt_type with '$2'"
  301. virt_check_cmd virt_add ltp_v0 $2 || return
  302. virt_add_delete_test "$2"
  303. start_id=$(($start_id + $NS_TIMES))
  304. }
  305. virt_gre_setup()
  306. {
  307. virt_type="gre"
  308. [ "$TST_IPV6" ] && virt_type="ip6gre"
  309. virt_lib_setup
  310. tst_res TINFO "test $virt_type"
  311. virt_setup "local $(tst_ipaddr) remote $(tst_ipaddr rhost) dev $(tst_iface)" \
  312. "local $(tst_ipaddr rhost) remote $(tst_ipaddr) dev $(tst_iface rhost)"
  313. }