[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

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



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 |   65 +++++++++++++++++++++++++---------------------
 5 files changed, 42 insertions(+), 42 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 8d80f3b..7bd61a5 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -6210,7 +6210,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 1eafeb3..2713e0e 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 482e6d3..1d8653d 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 32819c3..a445c2f 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -486,8 +486,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 */
@@ -842,7 +842,7 @@ qemudShutdown(void) {
     virCapabilitiesFree(qemu_driver->caps);

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

     virSysinfoDefFree(qemu_driver->hostsysinfo);

@@ -4723,11 +4723,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,
                                    NULL,
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 1df3637..996e10e 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -2426,15 +2426,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;
@@ -2455,10 +2455,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;
         }
@@ -2479,11 +2479,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);
 }

@@ -3392,41 +3392,46 @@ 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) {
                 qemuReportError(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) {
-                    qemuReportError(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) {
+                qemuReportError(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) {
                     qemuReportError(VIR_ERR_INTERNAL_ERROR,
                                     "%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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]