]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: Do sensible auto allocation of SPICE port numbers
authorPeter Krempa <pkrempa@redhat.com>
Tue, 23 Apr 2013 05:01:38 +0000 (07:01 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 24 Apr 2013 12:37:20 +0000 (14:37 +0200)
With this patch, if the autoport attribute is used, the code will
sensibly auto allocate the ports only if needed.

docs/formatdomain.html.in
src/qemu/qemu_process.c

index 888c005982da044bc5e0d4ac669910b2bb3a2f96..bb75943bc4f33a2152cc39006510932b2dc67bac 100644 (file)
@@ -3470,7 +3470,7 @@ qemu-kvm -net nic,model=? /dev/null
               while <code>tlsPort</code> gives an alternative secure
               port number. The <code>autoport</code> attribute is the
               new preferred syntax for indicating autoallocation of
-              both port numbers.  The <code>listen</code> attribute is
+              needed port numbers.  The <code>listen</code> attribute is
               an IP address for the server to listen
               on. The <code>passwd</code> attribute provides a SPICE
               password in clear text. The <code>keymap</code>
index b17f9b2becfbff92a444f56d065ca6d769ff7687..925939dd9e098e5a61d1283065147f7baee39a8d 100644 (file)
@@ -3275,44 +3275,80 @@ qemuProcessSPICEAllocatePorts(virQEMUDriverPtr driver,
                               virQEMUDriverConfigPtr cfg,
                               virDomainGraphicsDefPtr graphics)
 {
-    int ret = -1;
     unsigned short port = 0;
     unsigned short tlsPort;
+    int i;
+    int defaultMode = graphics->data.spice.defaultMode;
+
+    bool needTLSPort = false;
+    bool needPort = false;
+
+    if (graphics->data.spice.autoport) {
+        /* check if tlsPort or port need allocation */
+        for (i = 0 ; i < VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_LAST ; i++) {
+            switch (graphics->data.spice.channels[i]) {
+            case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_SECURE:
+                needTLSPort = true;
+                break;
+
+            case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_INSECURE:
+                needPort = true;
+                break;
+
+            case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_ANY:
+                switch (defaultMode) {
+                case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_SECURE:
+                    needTLSPort = true;
+                    break;
 
-    if (graphics->data.spice.autoport ||
-        graphics->data.spice.port == -1) {
+                case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_INSECURE:
+                    needPort = true;
+                    break;
+
+                case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_ANY:
+                    needTLSPort = true;
+                    needPort = true;
+                    break;
+                }
+                break;
+            }
+        }
+    }
+
+    if (needPort || graphics->data.spice.port == -1) {
         if (virPortAllocatorAcquire(driver->remotePorts, &port) < 0)
-            goto cleanup;
+            goto error;
 
         if (port == 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("Unable to find an unused port for SPICE"));
-            goto cleanup;
+            goto error;
         }
 
         graphics->data.spice.port = port;
     }
 
     if (cfg->spiceTLS &&
-        (graphics->data.spice.autoport ||
-         graphics->data.spice.tlsPort == -1)) {
+        (needTLSPort || graphics->data.spice.tlsPort == -1)) {
         if (virPortAllocatorAcquire(driver->remotePorts, &tlsPort) < 0)
-            goto cleanup;
+            goto error;
 
         if (tlsPort == 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           "%s", _("Unable to find an unused port for SPICE TLS"));
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("Unable to find an unused port for SPICE TLS"));
             virPortAllocatorRelease(driver->remotePorts, port);
-            goto cleanup;
+            goto error;
         }
 
         graphics->data.spice.tlsPort = tlsPort;
     }
 
-    ret = 0;
+    return 0;
 
-cleanup:
-    return ret;
+error:
+    if (port)
+        virPortAllocatorRelease(driver->remotePorts, port);
+    return -1;
 }