[libvirt] [PATCH v2.1 07/11] qemu_migration: Move port allocation to a separate func

Michal Privoznik mprivozn at redhat.com
Fri Jan 11 16:52:19 UTC 2013


There's a code snippet which allocates a port for incoming
migration. We will need this for allocating a port for
incoming disks as well. Hence, move this snippet into
a separate function called qemuMigrationNextPort().
---
 src/qemu/qemu_migration.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index ccf223e..367747c 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -1087,6 +1087,17 @@ error:
     return NULL;
 }
 
+static int
+qemuMigrationNextPort(void) {
+    static int port = 0;
+    int ret =  QEMUD_MIGRATION_FIRST_PORT + port++;
+
+    if (port == QEMUD_MIGRATION_NUM_PORTS)
+        port = 0;
+
+    return ret;
+}
+
 /**
  * qemuMigrationStartNBDServer:
  * @driver: qemu driver
@@ -1938,7 +1949,6 @@ qemuMigrationPrepareDirect(virQEMUDriverPtr driver,
                            const char *dom_xml,
                            unsigned long flags)
 {
-    static int port = 0;
     int this_port;
     char *hostname = NULL;
     char migrateFrom [64];
@@ -1963,8 +1973,7 @@ qemuMigrationPrepareDirect(virQEMUDriverPtr driver,
      * to be a correct hostname which refers to the target machine).
      */
     if (uri_in == NULL) {
-        this_port = QEMUD_MIGRATION_FIRST_PORT + port++;
-        if (port == QEMUD_MIGRATION_NUM_PORTS) port = 0;
+        this_port = qemuMigrationNextPort();
 
         /* Get hostname */
         if ((hostname = virGetHostname(NULL)) == NULL)
@@ -2003,9 +2012,7 @@ qemuMigrationPrepareDirect(virQEMUDriverPtr driver,
         p = strrchr(uri_in, ':');
         if (p == strchr(uri_in, ':')) {
             /* Generate a port */
-            this_port = QEMUD_MIGRATION_FIRST_PORT + port++;
-            if (port == QEMUD_MIGRATION_NUM_PORTS)
-                port = 0;
+            this_port = qemuMigrationNextPort();
 
             /* Caller frees */
             if (virAsprintf(uri_out, "%s:%d", uri_in, this_port) < 0) {
-- 
1.8.0.2




More information about the libvir-list mailing list