[libvirt] [PATCH v3 1/4] qemu: Unify port-wise SPICE and VNC behavior

Martin Kletzander mkletzan at redhat.com
Mon Aug 13 13:21:22 UTC 2012


Port allocations for SPICE and VNC behave almost the same (with
default ports), but there is some mess in the code. This patch clears
these inconsistencies and makes sure the same behavior will be used
when ports for remote displays are changed.

Changes:
 - hard-coded number 5900 removed (handled elsewhere like with VNC)
 - reservedVNCPorts renamed to reservedRemotePorts (it's not just for
   VNC anymore)
 - QEMU_VNC_PORT_{MIN,MAX} renamed to QEMU_REMOTE_PORT_{MIN,MAX}
 - port allocation unified for VNC and SPICE
---
 src/conf/domain_conf.c  |    2 +-
 src/qemu/qemu_command.h |    4 +-
 src/qemu/qemu_conf.h    |    2 +-
 src/qemu/qemu_driver.c  |   11 ++-----
 src/qemu/qemu_process.c |   66 +++++++++++++++++++++++++---------------------
 5 files changed, 43 insertions(+), 42 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index d8c0969..24e4860 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -6244,7 +6244,7 @@ virDomainGraphicsDefParseXML(xmlNodePtr node,
             }
             VIR_FREE(port);
         } else {
-            def->data.spice.port = 5900;
+            def->data.spice.port = 0;
         }

         tlsPort = virXMLPropString(node, "tlsPort");
diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h
index e999bc7..14afd9b 100644
--- a/src/qemu/qemu_command.h
+++ b/src/qemu/qemu_command.h
@@ -37,8 +37,8 @@
 # define QEMU_VIRTIO_SERIAL_PREFIX "virtio-serial"
 # define QEMU_FSDEV_HOST_PREFIX "fsdev-"

-# define QEMU_VNC_PORT_MIN  5900
-# define QEMU_VNC_PORT_MAX  65535
+# define QEMU_REMOTE_PORT_MIN  5900
+# define QEMU_REMOTE_PORT_MAX  65535


 virCommandPtr qemuBuildCommandLine(virConnectPtr conn,
diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h
index 92e4968..3895889 100644
--- a/src/qemu/qemu_conf.h
+++ b/src/qemu/qemu_conf.h
@@ -135,7 +135,7 @@ struct qemud_driver {
     /* The devices which is are not in use by the host or any guest. */
     pciDeviceList *inactivePciHostdevs;

-    virBitmapPtr reservedVNCPorts;
+    virBitmapPtr reservedRemotePorts;

     virSysinfoDefPtr hostsysinfo;

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 9bf89bb..0d886c1 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -521,8 +521,8 @@ qemudStartup(int privileged) {
         goto error;

     /* Allocate bitmap for vnc port reservation */
-    if ((qemu_driver->reservedVNCPorts =
-         virBitmapAlloc(QEMU_VNC_PORT_MAX - QEMU_VNC_PORT_MIN)) == NULL)
+    if ((qemu_driver->reservedRemotePorts =
+         virBitmapAlloc(QEMU_REMOTE_PORT_MAX - QEMU_REMOTE_PORT_MIN)) == NULL)
         goto out_of_memory;

     /* read the host sysinfo */
@@ -880,7 +880,7 @@ qemudShutdown(void) {
     virCapabilitiesFree(qemu_driver->caps);

     virDomainObjListDeinit(&qemu_driver->domains);
-    virBitmapFree(qemu_driver->reservedVNCPorts);
+    virBitmapFree(qemu_driver->reservedRemotePorts);

     virSysinfoDefFree(qemu_driver->hostsysinfo);

@@ -4785,11 +4785,6 @@ static char *qemuDomainXMLToNative(virConnectPtr conn,
         }
         net->info.bootIndex = bootIndex;
     }
-    for (i = 0 ; i < def->ngraphics ; i++) {
-        if (def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
-            def->graphics[i]->data.vnc.autoport)
-            def->graphics[i]->data.vnc.port = QEMU_VNC_PORT_MIN;
-    }

     if (qemuCapsExtractVersionInfo(def->emulator, def->os.arch,
                                    false,
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index df4a016..4c40b49 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -2454,15 +2454,15 @@ static int qemuProcessNextFreePort(struct qemud_driver *driver,
 {
     int i;

-    for (i = startPort ; i < QEMU_VNC_PORT_MAX; i++) {
+    for (i = startPort ; i < QEMU_REMOTE_PORT_MAX; i++) {
         int fd;
         int reuse = 1;
         struct sockaddr_in addr;
         bool used = false;

-        if (virBitmapGetBit(driver->reservedVNCPorts,
-                            i - QEMU_VNC_PORT_MIN, &used) < 0)
-            VIR_DEBUG("virBitmapGetBit failed on bit %d", i - QEMU_VNC_PORT_MIN);
+        if (virBitmapGetBit(driver->reservedRemotePorts,
+                            i - QEMU_REMOTE_PORT_MIN, &used) < 0)
+            VIR_DEBUG("virBitmapGetBit failed on bit %d", i - QEMU_REMOTE_PORT_MIN);

         if (used)
             continue;
@@ -2483,10 +2483,10 @@ static int qemuProcessNextFreePort(struct qemud_driver *driver,
             /* Not in use, lets grab it */
             VIR_FORCE_CLOSE(fd);
             /* Add port to bitmap of reserved ports */
-            if (virBitmapSetBit(driver->reservedVNCPorts,
-                                i - QEMU_VNC_PORT_MIN) < 0) {
+            if (virBitmapSetBit(driver->reservedRemotePorts,
+                                i - QEMU_REMOTE_PORT_MIN) < 0) {
                 VIR_DEBUG("virBitmapSetBit failed on bit %d",
-                          i - QEMU_VNC_PORT_MIN);
+                          i - QEMU_REMOTE_PORT_MIN);
             }
             return i;
         }
@@ -2507,11 +2507,11 @@ static void
 qemuProcessReturnPort(struct qemud_driver *driver,
                       int port)
 {
-    if (port < QEMU_VNC_PORT_MIN)
+    if (port < QEMU_REMOTE_PORT_MIN)
         return;

-    if (virBitmapClearBit(driver->reservedVNCPorts,
-                          port - QEMU_VNC_PORT_MIN) < 0)
+    if (virBitmapClearBit(driver->reservedRemotePorts,
+                          port - QEMU_REMOTE_PORT_MIN) < 0)
         VIR_DEBUG("Could not mark port %d as unused", port);
 }

@@ -3417,41 +3417,47 @@ int qemuProcessStart(virConnectPtr conn,
         if (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
             !vm->def->graphics[0]->data.vnc.socket &&
             vm->def->graphics[0]->data.vnc.autoport) {
-            int port = qemuProcessNextFreePort(driver, QEMU_VNC_PORT_MIN);
+            int port;
+            if (vm->def->graphics[0]->data.vnc.port > 0)
+                port = qemuProcessNextFreePort(driver, vm->def->graphics[0]->data.vnc.port);
+            else
+                port = qemuProcessNextFreePort(driver, QEMU_REMOTE_PORT_MIN);
+
             if (port < 0) {
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                "%s", _("Unable to find an unused VNC port"));
                 goto cleanup;
             }
             vm->def->graphics[0]->data.vnc.port = port;
-        } else if (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SPICE) {
-            int port = -1;
-            if (vm->def->graphics[0]->data.spice.autoport ||
-                vm->def->graphics[0]->data.spice.port == -1) {
-                port = qemuProcessNextFreePort(driver, QEMU_VNC_PORT_MIN);

-                if (port < 0) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   "%s", _("Unable to find an unused SPICE port"));
-                    goto cleanup;
-                }
+        } else if (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SPICE &&
+                   vm->def->graphics[0]->data.spice.autoport) {
+            int port;
+            if (vm->def->graphics[0]->data.spice.port > 0)
+                port = qemuProcessNextFreePort(driver, vm->def->graphics[0]->data.spice.port);
+            else
+                port = qemuProcessNextFreePort(driver, QEMU_REMOTE_PORT_MIN);

-                vm->def->graphics[0]->data.spice.port = port;
+            if (port < 0) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                                "%s", _("Unable to find an unused SPICE port"));
+                goto cleanup;
             }
+            vm->def->graphics[0]->data.spice.port = port;

-            if (driver->spiceTLS &&
-                (vm->def->graphics[0]->data.spice.autoport ||
-                 vm->def->graphics[0]->data.spice.tlsPort == -1)) {
-                int tlsPort = qemuProcessNextFreePort(driver,
-                                                      vm->def->graphics[0]->data.spice.port + 1);
-                if (tlsPort < 0) {
+            if (driver->spiceTLS) {
+                if (vm->def->graphics[0]->data.spice.tlsPort > 0)
+                    port = qemuProcessNextFreePort(driver, vm->def->graphics[0]->data.spice.tlsPort);
+                else
+                    port = qemuProcessNextFreePort(driver, vm->def->graphics[0]->data.spice.port + 1);
+                if (port < 0) {
                     virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   "%s", _("Unable to find an unused SPICE TLS port"));
+                                    "%s", _("Unable to find an unused SPICE TLS port"));
                     qemuProcessReturnPort(driver, port);
                     goto cleanup;
                 }

-                vm->def->graphics[0]->data.spice.tlsPort = tlsPort;
+                vm->def->graphics[0]->data.spice.tlsPort = port;
             }
         }

-- 
1.7.8.6




More information about the libvir-list mailing list