tst_net_stress.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) 2017-2018 Petr Vorel <pvorel@suse.cz>
  4. # Copyright (c) 2015-2017 Oracle and/or its affiliates. All Rights Reserved.
  5. # Copyright (c) International Business Machines Corp., 2006
  6. # Author: Petr Vorel <pvorel@suse.cz>
  7. # Author: Alexey Kodanev <alexey.kodanev@oracle.com>
  8. #
  9. # Library for all network/stress/ tests.
  10. # NOTE: More information about network variables can be found
  11. # in tst_net.sh and testcases/network/stress/README.
  12. . tst_net.sh
  13. # Netmask of for the tested network
  14. IPV4_NETMASK="255.255.255.0"
  15. IPV4_NETMASK_NUM=24
  16. # Multicast address and it's prefix
  17. MCAST_IPV4_ADDR_PREFIX="224.10"
  18. MCAST_IPV4_ADDR="${MCAST_IPV4_ADDR_PREFIX}.10.1"
  19. MCAST_IPV6_ADDR_PREFIX="ff0e::1111"
  20. MCAST_IPV6_ADDR="${MCAST_IPV6_ADDR_PREFIX}:1"
  21. # Setup for tests using netstress.
  22. netstress_setup()
  23. {
  24. TST_NEEDS_ROOT=1
  25. tst_require_cmds pgrep pkill
  26. }
  27. # Cleanup for tests using netstress.
  28. netstress_cleanup()
  29. {
  30. # Stop the background TCP traffic
  31. pkill -13 -x netstress
  32. tst_rhost_run -c "pkill -13 -x netstress"
  33. }
  34. # restore_ipaddr [TYPE] [LINK] [LOCAL_IFACE] [REMOTE_IFACE]
  35. # TYPE: { lhost | rhost }; Default value is 'lhost'.
  36. # LINK: link number starting from 0. Default value is '0'.
  37. # LOCAL_IFACE: local iface name.
  38. # REMOTE_IFACE: local iface name.
  39. restore_ipaddr()
  40. {
  41. local type="${1:-lhost}"
  42. local link_num="${2:-0}"
  43. local iface_loc=${3:-$(tst_iface lhost $link_num)}
  44. local iface_rmt=${4:-$(tst_iface rhost $link_num)}
  45. tst_restore_ipaddr $type $link_num || return $?
  46. [ $type = "lhost" ] && tst_wait_ipv6_dad $iface_loc $iface_rmt
  47. }
  48. # Check connectivity with tst_ping.
  49. # check_connectivity SRC_IFACE DST_ADDR [CNT]
  50. # SRC_IFACE: source interface name.
  51. # DST_ADDR: destination IPv4 or IPv6 address.
  52. # CNT: loop step.
  53. check_connectivity()
  54. {
  55. local src_iface="${1}"
  56. local dst_addr="${2}"
  57. local cnt="${3:-}"
  58. local cnt_msg
  59. [ -n "$cnt" ] && cnt_msg=" (step $cnt)"
  60. tst_res TINFO "ping through $src_iface iface to ${dst_addr}$cnt_msg"
  61. tst_ping $src_iface $dst_addr
  62. }
  63. # check_connectivity_interval CNT [RESTORE] [SRC_IFACE] [DST_ADDR]
  64. # CNT: loop step.
  65. # RESTORE: whether restore ip addr.
  66. # SRC_IFACE: source interface name.
  67. # DST_ADDR: destination IPv4 or IPv6 address.
  68. check_connectivity_interval()
  69. {
  70. local cnt="$1"
  71. local restore="${2:-false}"
  72. local src_iface="${3:-$(tst_iface)}"
  73. local dst_addr="${4:-$(tst_ipaddr rhost)}"
  74. [ $CHECK_INTERVAL -eq 0 ] && return
  75. [ $(($cnt % $CHECK_INTERVAL)) -ne 0 ] && return
  76. [ "$restore" != "false" ] && restore_ipaddr
  77. check_connectivity $src_iface $dst_addr $cnt
  78. }
  79. # Run netstress process on both lhost and rhost.
  80. # make_background_tcp_traffic [IP]
  81. # IP: server IP; Default value is $(tst_ipaddr).
  82. make_background_tcp_traffic()
  83. {
  84. pgrep -x netstress > /dev/null && return
  85. local ip="${1:-$(tst_ipaddr)}"
  86. local port=$(tst_get_unused_port ipv${TST_IPVER} stream)
  87. netstress -R 3 -g $port > /dev/null 2>&1 &
  88. tst_rhost_run -b -c "netstress -l -H $ip -g $port"
  89. }
  90. test_if_ip()
  91. {
  92. case $1 in
  93. 1) test_body 'if_cmd';;
  94. 2) test_body 'ip_cmd';;
  95. esac
  96. }
  97. test_rt_ip()
  98. {
  99. case $1 in
  100. 1) test_body 'rt_cmd';;
  101. 2) test_body 'ip_cmd';;
  102. esac
  103. }