[PATCH v2 7/9] peer2peer migration: allow connecting to local sockets

Martin Kletzander mkletzan at redhat.com
Tue Sep 1 14:36:58 UTC 2020


Local socket connections were outright disabled because there was no "server"
part in the URI.  However, given how requirements and usage scenarios are
evolving, some management apps might need the source libvirt daemon to connect
to the destination daemon over a UNIX socket for peer2peer migration.  Since we
cannot know where the socket leads (whether the same daemon or not) let's decide
that based on whether the socket path is non-standard, or rather explicitly
specified in the URI.  Checking non-standard path would require to ask the
daemon for configuration and the only misuse that it would prevent would be a
pretty weird one.  And that's not worth it.  The assumption is that whenever
someone uses explicit UNIX socket paths in the URI for migration they better
know what they are doing.

Partially resolves: https://bugzilla.redhat.com/1638889

Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
---
 docs/manpages/virsh.rst    |  9 +++++++++
 src/libvirt-domain.c       |  8 +++++++-
 src/remote/remote_driver.c |  8 ++++++--
 src/util/viruri.c          | 30 ++++++++++++++++++++++++++++++
 src/util/viruri.h          |  2 ++
 tests/virmigtest.c         |  2 +-
 6 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst
index 12357ea4ee86..4d66019e750b 100644
--- a/docs/manpages/virsh.rst
+++ b/docs/manpages/virsh.rst
@@ -3235,6 +3235,15 @@ has different semantics:
 
 * peer2peer migration: the *desturi* is an address of the target host as seen from the source machine.
 
+In a special circumstance where you require a complete control of the connection
+and/or libvirt does not have network access to the remote side you can use a
+UNIX transport in the URI and specify a socket path in the query, for example
+with the qemu driver you could use this:
+
+.. code-block::
+
+      qemu+unix://?socket=/path/to/socket
+
 When *migrateuri* is not specified, libvirt will automatically determine the
 hypervisor specific URI.  Some hypervisors, including QEMU, have an optional
 "migration_host" configuration parameter (useful when the host has multiple
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index da5a21e4c4d4..cde86c77e892 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -3276,7 +3276,13 @@ virDomainMigrateCheckNotLocal(const char *dconnuri)
 
     if (!(tempuri = virURIParse(dconnuri)))
         return -1;
-    if (!tempuri->server || STRPREFIX(tempuri->server, "localhost")) {
+
+    /*
+     * If someone migrates explicitly to a unix socket, then they have to know
+     * what they are doing and it most probably was not a mistake.
+     */
+    if ((tempuri->server && STRPREFIX(tempuri->server, "localhost")) ||
+        (!tempuri->server && !virURICheckUnixSocket(tempuri))) {
         virReportInvalidArg(dconnuri, "%s",
                             _("Attempt to migrate guest to the same host"));
         return -1;
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 8654046b8ddb..7df995446a3c 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -1429,9 +1429,13 @@ remoteConnectOpen(virConnectPtr conn,
 
         /* If there's a driver registered we must defer to that.
          * If there isn't a driver, we must connect in "direct"
-         * mode - see doRemoteOpen */
+         * mode - see doRemoteOpen.
+         * One exception is if we are trying to connect to an
+         * unknown socket path as that might be proxied to remote
+         * host */
         if (!conn->uri->server &&
-            virHasDriverForURIScheme(driver)) {
+            virHasDriverForURIScheme(driver) &&
+            !virURICheckUnixSocket(conn->uri)) {
             ret = VIR_DRV_OPEN_DECLINED;
             goto cleanup;
         }
diff --git a/src/util/viruri.c b/src/util/viruri.c
index 0112186fdbc4..dd7559662bd2 100644
--- a/src/util/viruri.c
+++ b/src/util/viruri.c
@@ -393,3 +393,33 @@ virURIGetParam(virURIPtr uri, const char *name)
                    _("Missing URI parameter '%s'"), name);
     return NULL;
 }
+
+
+/**
+ * virURICheckUnixSocket:
+ * @uri: URI to check
+ *
+ * Check if the URI looks like it refers to a non-standard socket path.  In such
+ * scenario the socket might be proxied to a remote server even though the URI
+ * looks like it is only local.
+ *
+ * Returns: true if the URI might be proxied to a remote server
+ */
+bool
+virURICheckUnixSocket(virURIPtr uri)
+{
+    size_t i = 0;
+
+    if (!uri->scheme)
+        return false;
+
+    if (STRNEQ_NULLABLE(strchr(uri->scheme, '+'), "+unix"))
+        return false;
+
+    for (i = 0; i < uri->paramsCount; i++) {
+        if (STREQ(uri->params[i].name, "socket"))
+            return true;
+    }
+
+    return false;
+}
diff --git a/src/util/viruri.h b/src/util/viruri.h
index e607ecc109e7..dc4907b55059 100644
--- a/src/util/viruri.h
+++ b/src/util/viruri.h
@@ -62,4 +62,6 @@ int virURIResolveAlias(virConfPtr conf, const char *alias, char **uri);
 
 const char *virURIGetParam(virURIPtr uri, const char *name);
 
+bool virURICheckUnixSocket(virURIPtr uri);
+
 #define VIR_URI_SERVER(uri) ((uri) && (uri)->server ? (uri)->server : "localhost")
diff --git a/tests/virmigtest.c b/tests/virmigtest.c
index df8d51bab94b..af6643397e90 100644
--- a/tests/virmigtest.c
+++ b/tests/virmigtest.c
@@ -83,7 +83,7 @@ mymain(void)
 
     TEST("scheme://some.cryptorandom.fqdn.tld");
 
-    TEST_FAIL("hehe+unix:///?socket=/path/to/some-sock");
+    TEST("hehe+unix:///?socket=/path/to/some-sock");
 
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
-- 
2.28.0




More information about the libvir-list mailing list