9119d9
From 79e76b8ff1ee3b5b5cea58e896ea22480653bb8f Mon Sep 17 00:00:00 2001
9119d9
Message-Id: <79e76b8ff1ee3b5b5cea58e896ea22480653bb8f@dist-git>
9119d9
From: Jiri Denemark <jdenemar@redhat.com>
9119d9
Date: Tue, 23 Sep 2014 15:47:54 +0200
9119d9
Subject: [PATCH] qemu: Fix old tcp:host URIs more cleanly
9119d9
9119d9
For compatibility with old libvirt we need to support both tcp:host and
9119d9
tcp://host migration URIs. Let's make the code that parses them a bit
9119d9
cleaner.
9119d9
9119d9
https://bugzilla.redhat.com/show_bug.cgi?id=1013055
9119d9
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
(cherry picked from commit 1cffb25c165cc47a45380379146af526f0f87135)
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
---
9119d9
 src/qemu/qemu_migration.c | 73 +++++++++++++++++++++--------------------------
9119d9
 1 file changed, 32 insertions(+), 41 deletions(-)
9119d9
9119d9
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
9119d9
index 179af80..a66ea54 100644
9119d9
--- a/src/qemu/qemu_migration.c
9119d9
+++ b/src/qemu/qemu_migration.c
9119d9
@@ -2817,6 +2817,29 @@ qemuMigrationPrepareTunnel(virQEMUDriverPtr driver,
9119d9
 }
9119d9
 
9119d9
 
9119d9
+static virURIPtr
9119d9
+qemuMigrationParseURI(const char *uri, bool *wellFormed)
9119d9
+{
9119d9
+    char *tmp = NULL;
9119d9
+    virURIPtr parsed;
9119d9
+
9119d9
+    /* For compatibility reasons tcp://... URIs are sent as tcp:...
9119d9
+     * We need to transform them to a well-formed URI before parsing. */
9119d9
+    if (STRPREFIX(uri, "tcp:") && !STRPREFIX(uri + 4, "//")) {
9119d9
+        if (virAsprintf(&tmp, "tcp://%s", uri + 4) < 0)
9119d9
+            return NULL;
9119d9
+        uri = tmp;
9119d9
+    }
9119d9
+
9119d9
+    parsed = virURIParse(uri);
9119d9
+    if (parsed && wellFormed)
9119d9
+        *wellFormed = !tmp;
9119d9
+    VIR_FREE(tmp);
9119d9
+
9119d9
+    return parsed;
9119d9
+}
9119d9
+
9119d9
+
9119d9
 int
9119d9
 qemuMigrationPrepareDirect(virQEMUDriverPtr driver,
9119d9
                            virConnectPtr dconn,
9119d9
@@ -2834,11 +2857,8 @@ qemuMigrationPrepareDirect(virQEMUDriverPtr driver,
9119d9
     unsigned short port = 0;
9119d9
     bool autoPort = true;
9119d9
     char *hostname = NULL;
9119d9
-    const char *p;
9119d9
-    char *uri_str = NULL;
9119d9
     int ret = -1;
9119d9
     virURIPtr uri = NULL;
9119d9
-    bool well_formed_uri = true;
9119d9
     virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
9119d9
     const char *migrateHost = cfg->migrateHost;
9119d9
 
9119d9
@@ -2890,34 +2910,18 @@ qemuMigrationPrepareDirect(virQEMUDriverPtr driver,
9119d9
          * compatibility with old targets. We at least make the
9119d9
          * new targets accept both syntaxes though.
9119d9
          */
9119d9
-        /* Caller frees */
9119d9
         if (virAsprintf(uri_out, "tcp:%s:%d", hostname, port) < 0)
9119d9
             goto cleanup;
9119d9
     } else {
9119d9
-        /* Check the URI starts with "tcp:".  We will escape the
9119d9
-         * URI when passing it to the qemu monitor, so bad
9119d9
-         * characters in hostname part don't matter.
9119d9
-         */
9119d9
-        if (!(p = STRSKIP(uri_in, "tcp:"))) {
9119d9
-            virReportError(VIR_ERR_INVALID_ARG, "%s",
9119d9
-                           _("only tcp URIs are supported for KVM/QEMU"
9119d9
-                             " migrations"));
9119d9
+        bool well_formed_uri;
9119d9
+
9119d9
+        if (!(uri = qemuMigrationParseURI(uri_in, &well_formed_uri)))
9119d9
             goto cleanup;
9119d9
-        }
9119d9
 
9119d9
-        /* Convert uri_in to well-formed URI with // after tcp: */
9119d9
-        if (!(STRPREFIX(uri_in, "tcp://"))) {
9119d9
-            well_formed_uri = false;
9119d9
-            if (virAsprintf(&uri_str, "tcp://%s", p) < 0)
9119d9
-                goto cleanup;
9119d9
-        }
9119d9
-
9119d9
-        uri = virURIParse(uri_str ? uri_str : uri_in);
9119d9
-        VIR_FREE(uri_str);
9119d9
-
9119d9
-        if (uri == NULL) {
9119d9
-            virReportError(VIR_ERR_INVALID_ARG, _("unable to parse URI: %s"),
9119d9
-                           uri_in);
9119d9
+        if (STRNEQ(uri->scheme, "tcp")) {
9119d9
+            virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
9119d9
+                           _("unsupported scheme %s in migration URI %s"),
9119d9
+                           uri->scheme, uri_in);
9119d9
             goto cleanup;
9119d9
         }
9119d9
 
9119d9
@@ -2931,18 +2935,15 @@ qemuMigrationPrepareDirect(virQEMUDriverPtr driver,
9119d9
             if (virPortAllocatorAcquire(driver->migrationPorts, &port) < 0)
9119d9
                 goto cleanup;
9119d9
 
9119d9
+            /* Send well-formed URI only if uri_in was well-formed */
9119d9
             if (well_formed_uri) {
9119d9
                 uri->port = port;
9119d9
-
9119d9
-                /* Caller frees */
9119d9
                 if (!(*uri_out = virURIFormat(uri)))
9119d9
                     goto cleanup;
9119d9
             } else {
9119d9
-                /* Caller frees */
9119d9
                 if (virAsprintf(uri_out, "%s:%d", uri_in, port) < 0)
9119d9
                     goto cleanup;
9119d9
             }
9119d9
-
9119d9
         } else {
9119d9
             port = uri->port;
9119d9
             autoPort = false;
9119d9
@@ -3704,17 +3705,7 @@ static int doNativeMigrate(virQEMUDriverPtr driver,
9119d9
               cookieout, cookieoutlen, flags, resource,
9119d9
               NULLSTR(graphicsuri));
9119d9
 
9119d9
-    if (STRPREFIX(uri, "tcp:") && !STRPREFIX(uri, "tcp://")) {
9119d9
-        char *tmp;
9119d9
-        /* HACK: source host generates bogus URIs, so fix them up */
9119d9
-        if (virAsprintf(&tmp, "tcp://%s", uri + strlen("tcp:")) < 0)
9119d9
-            return -1;
9119d9
-        uribits = virURIParse(tmp);
9119d9
-        VIR_FREE(tmp);
9119d9
-    } else {
9119d9
-        uribits = virURIParse(uri);
9119d9
-    }
9119d9
-    if (!uribits)
9119d9
+    if (!(uribits = qemuMigrationParseURI(uri, NULL)))
9119d9
         return -1;
9119d9
 
9119d9
     if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_MIGRATE_QEMU_FD))
9119d9
-- 
9119d9
2.1.1
9119d9