test/vhost: Make sure all TCP ports allocated for QEMU are available

This may become problematic in case of bigger number of VMs. In
particular, it was noticed that the vnc port may overlap with ssh's
X forwarding ports (starting at 6010). To make sure QEMU does not
fail while attempting to bind to already existing port, we first
check if target port is in use.

Signed-off-by: Michal Berger <michal.berger@intel.com>
Change-Id: I525aa2a1cc52c6aa1d8d4ade8924ad684fe8af50
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/16337
Reviewed-by: Shuhei Matsumoto <smatsumoto@nvidia.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Karol Latecki <karol.latecki@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Michal Berger 2023-01-18 18:05:20 +01:00 committed by David Ko
parent 8c5a2c051d
commit 196978565e

View File

@ -673,6 +673,14 @@ function vm_setup() {
queue_number=$cpu_num queue_number=$cpu_num
fi fi
# Normalize tcp ports to make sure they are available
ssh_socket=$(get_free_tcp_port "$ssh_socket")
fio_socket=$(get_free_tcp_port "$fio_socket")
monitor_port=$(get_free_tcp_port "$monitor_port")
migration_port=$(get_free_tcp_port "$migration_port")
gdbserver_socket=$(get_free_tcp_port "$gdbserver_socket")
vnc_socket=$(get_free_tcp_port "$vnc_socket")
xtrace_restore xtrace_restore
local node_num=${!qemu_numa_node_param} local node_num=${!qemu_numa_node_param}
@ -1384,3 +1392,21 @@ function get_from_fio() {
awk -F= "/^$opt/{print \$2}" "$conf" awk -F= "/^$opt/{print \$2}" "$conf"
} }
function get_free_tcp_port() {
local port=$1 to=${2:-1} sockets=()
mapfile -t sockets < /proc/net/tcp
# If there's a TCP socket in a listening state keep incrementing $port until
# we find one that's not used. $to determines how long should we look for:
# 0: don't increment, just check if given $port is in use
# >0: increment $to times
# <0: no increment limit
while [[ ${sockets[*]} == *":$(printf '%04X' "$port") 00000000:0000 0A"* ]]; do
((to-- && ++port <= 65535)) || return 1
done
echo "$port"
}