Commit 905c4dcf authored by Michal Berger's avatar Michal Berger Committed by Jim Harris
Browse files

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: default avatarMichal Berger <michal.berger@intel.com>
Change-Id: I525aa2a1cc52c6aa1d8d4ade8924ad684fe8af50
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/16337


Reviewed-by: default avatarShuhei Matsumoto <smatsumoto@nvidia.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarKarol Latecki <karol.latecki@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
parent ce6550a9
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -673,6 +673,14 @@ function vm_setup() {
		queue_number=$cpu_num
	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

	local node_num=${!qemu_numa_node_param}
@@ -1384,3 +1392,21 @@ function get_from_fio() {

	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"
}