For QEMU migration via TCP, there's a bit of time between port
reservation and usage, because currently, the port needs to be
reserved before doing a fork, where the systemd scope needs to be set
up and swtpm might need to be started before the QEMU binary can be
invoked and actually use the port.

To improve the situation, get the latest port recorded in the
reservation file and start trying from the next port, wrapping around
when hitting the end. Drastically reduces the chances to run into a
conflict, because after a given port reservation, all other ports are
tried first before returning to that port.

Signed-off-by: Fiona Ebner <f.eb...@proxmox.com>
---
 src/PVE/Tools.pm | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 4d018e9..820229d 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -923,6 +923,11 @@ sub next_unused_port {
 
        my $ports = {};
 
+       # Avoid that bulk actions compete for the first few ports by detecting 
the latest
+       # (previously) used port and start checking from there when trying to 
get a reservation.
+       my $latest_timestamp = 0;
+       my $latest_port = $range_end - 1;
+
        if (my $fh = IO::File->new ($filename, "r")) {
            while (my $line = <$fh>) {
                if ($line =~ m/^(\d+)\s(\d+)$/) {
@@ -930,6 +935,14 @@ sub next_unused_port {
                    if (($timestamp + $expiretime) > $ctime) {
                        $ports->{$port} = $timestamp; # not expired
                    }
+                   if (
+                       $port >= $range_start
+                       && $port < $range_end
+                       && $timestamp > $latest_timestamp
+                   ) {
+                       $latest_timestamp = $timestamp;
+                       $latest_port = $port;
+                   }
                }
            }
        }
@@ -942,7 +955,11 @@ sub next_unused_port {
                        GetAddrInfoFlags => 0);
        $sockargs{LocalAddr} = $address if defined($address);
 
-       for (my $p = $range_start; $p < $range_end; $p++) {
+       my $range = $range_end - $range_start;
+       for (my $offset = 1; $offset <= $range; $offset++) {
+           my $p = $latest_port + $offset;
+           $p -= $range if $p >= $range_end; # wrap around
+
            next if $ports->{$p}; # reserved
 
            $sockargs{LocalPort} = $p;
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to