diff --git a/test/vhost/common.sh b/test/vhost/common.sh index 4a7893fd8..a40f1430d 100644 --- a/test/vhost/common.sh +++ b/test/vhost/common.sh @@ -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" +}