Blame 0019-spice-live-migration-wip.patch

Justin M. Forbes a81953
From 80b1dac2be1487d31e6766abe2359fcff1bf0481 Mon Sep 17 00:00:00 2001
Justin M. Forbes a81953
From: Gerd Hoffmann <kraxel@redhat.com>
Justin M. Forbes a81953
Date: Fri, 23 Apr 2010 13:28:21 +0200
Justin M. Forbes a81953
Subject: [PATCH 19/39] spice: live migration (wip).
Justin M. Forbes a81953
Justin M. Forbes a81953
Handle spice client migration, i.e. inform a spice client connected
Justin M. Forbes a81953
about the new host and connection parameters, so it can move over the
Justin M. Forbes a81953
connection automatically.
Justin M. Forbes a81953
---
Justin M. Forbes a81953
 monitor.c       |    1 +
Justin M. Forbes a81953
 qemu-monitor.hx |   11 +++++++
Justin M. Forbes a81953
 qemu-spice.h    |    2 +
Justin M. Forbes a81953
 spice.c         |   87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
Justin M. Forbes a81953
 4 files changed, 101 insertions(+), 0 deletions(-)
Justin M. Forbes a81953
Justin M. Forbes a81953
diff --git a/monitor.c b/monitor.c
Justin M. Forbes a81953
index e51df62..6674a8c 100644
Justin M. Forbes a81953
--- a/monitor.c
Justin M. Forbes a81953
+++ b/monitor.c
Justin M. Forbes a81953
@@ -57,6 +57,7 @@
Justin M. Forbes a81953
 #include "osdep.h"
Justin M. Forbes a81953
 #include "exec-all.h"
Justin M. Forbes a81953
 #include "qemu-kvm.h"
Justin M. Forbes a81953
+#include "qemu-spice.h"
Justin M. Forbes a81953
Justin M. Forbes a81953
 //#define DEBUG
Justin M. Forbes a81953
 //#define DEBUG_COMPLETION
Justin M. Forbes a81953
diff --git a/qemu-monitor.hx b/qemu-monitor.hx
Justin M. Forbes a81953
index da7b796..c2570d9 100644
Justin M. Forbes a81953
--- a/qemu-monitor.hx
Justin M. Forbes a81953
+++ b/qemu-monitor.hx
Justin M. Forbes a81953
@@ -2510,6 +2510,17 @@ ETEXI
Justin M. Forbes a81953
Justin M. Forbes a81953
 HXCOMM DO NOT add new commands after 'info', move your addition before it!
Justin M. Forbes a81953
Justin M. Forbes a81953
+#if defined(CONFIG_SPICE)
Justin M. Forbes a81953
+    {
Justin M. Forbes a81953
+        .name       = "spice_migrate_info",
Justin M. Forbes a81953
+        .args_type  = "hostname:s,port:i?,tls-port:i?,cert-subject:s?",
Justin M. Forbes a81953
+        .params     = "hostname port tls-port cert-subject",
Justin M. Forbes a81953
+        .help       = "send migration info to spice client",
Justin M. Forbes a81953
+	.user_print = monitor_user_noop,
Justin M. Forbes a81953
+        .mhandler.cmd_new = mon_spice_migrate,
Justin M. Forbes a81953
+    },
Justin M. Forbes a81953
+#endif
Justin M. Forbes a81953
+
Justin M. Forbes a81953
 STEXI
Justin M. Forbes a81953
 @end table
Justin M. Forbes a81953
 ETEXI
Justin M. Forbes a81953
diff --git a/qemu-spice.h b/qemu-spice.h
Justin M. Forbes a81953
index 6f19ba7..3c8e959 100644
Justin M. Forbes a81953
--- a/qemu-spice.h
Justin M. Forbes a81953
+++ b/qemu-spice.h
Justin M. Forbes a81953
@@ -16,6 +16,8 @@ void qemu_spice_input_init(void);
Justin M. Forbes a81953
 void qemu_spice_audio_init(void);
Justin M. Forbes a81953
 void qemu_spice_display_init(DisplayState *ds);
Justin M. Forbes a81953
Justin M. Forbes a81953
+int mon_spice_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data);
Justin M. Forbes a81953
+
Justin M. Forbes a81953
 #else  /* CONFIG_SPICE */
Justin M. Forbes a81953
Justin M. Forbes a81953
 #define using_spice 0
Justin M. Forbes a81953
diff --git a/spice.c b/spice.c
Justin M. Forbes a81953
index fc76ef7..1109b4f 100644
Justin M. Forbes a81953
--- a/spice.c
Justin M. Forbes a81953
+++ b/spice.c
Justin M. Forbes a81953
@@ -11,6 +11,7 @@
Justin M. Forbes a81953
 #include "qemu-queue.h"
Justin M. Forbes a81953
 #include "qemu-x509.h"
Justin M. Forbes a81953
 #include "monitor.h"
Justin M. Forbes a81953
+#include "hw/hw.h"
Justin M. Forbes a81953
Justin M. Forbes a81953
 /* core bits */
Justin M. Forbes a81953
Justin M. Forbes a81953
@@ -122,8 +123,90 @@ static SpiceCoreInterface core_interface = {
Justin M. Forbes a81953
     .watch_remove       = watch_remove,
Justin M. Forbes a81953
 };
Justin M. Forbes a81953
Justin M. Forbes a81953
+/* handle client migration */
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+static int spice_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    static int last_stage;
Justin M. Forbes a81953
+    static int migrate_client, client_connected;
Justin M. Forbes a81953
+    int ret = 1;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    if (last_stage != stage) {
Justin M. Forbes a81953
+        last_stage = stage;
Justin M. Forbes a81953
+        fprintf(stderr, "%s: stage %d\n", __FUNCTION__, stage);
Justin M. Forbes a81953
+    } else {
Justin M. Forbes a81953
+        fprintf(stderr, ".");
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    switch (stage) {
Justin M. Forbes a81953
+    case 1:
Justin M. Forbes a81953
+        migrate_client = 1;
Justin M. Forbes a81953
+        client_connected = 0;
Justin M. Forbes a81953
+        fprintf(stderr, "%s: start client migration\n", __FUNCTION__);
Justin M. Forbes a81953
+        if (spice_server_migrate_start(spice_server) != 0) {
Justin M. Forbes a81953
+            fprintf(stderr, "%s: fail -> no client migration\n", __FUNCTION__);
Justin M. Forbes a81953
+            migrate_client = 0;
Justin M. Forbes a81953
+        }
Justin M. Forbes a81953
+        break;
Justin M. Forbes a81953
+    case 2:
Justin M. Forbes a81953
+        if (!migrate_client)
Justin M. Forbes a81953
+            break;
Justin M. Forbes a81953
+        switch (spice_server_migrate_client_state(spice_server)) {
Justin M. Forbes a81953
+        case SPICE_MIGRATE_CLIENT_NONE:
Justin M. Forbes a81953
+            fprintf(stderr, "%s: no client connected\n", __FUNCTION__);
Justin M. Forbes a81953
+            migrate_client = 0;
Justin M. Forbes a81953
+            break;
Justin M. Forbes a81953
+        case SPICE_MIGRATE_CLIENT_WAITING:
Justin M. Forbes a81953
+            ret = 0;
Justin M. Forbes a81953
+            break;
Justin M. Forbes a81953
+        case SPICE_MIGRATE_CLIENT_READY:
Justin M. Forbes a81953
+            if (!client_connected) {
Justin M. Forbes a81953
+                fprintf(stderr, "%s: client connected to target\n", __FUNCTION__);
Justin M. Forbes a81953
+                client_connected = 1;
Justin M. Forbes a81953
+            }
Justin M. Forbes a81953
+            break;
Justin M. Forbes a81953
+        }
Justin M. Forbes a81953
+        break;
Justin M. Forbes a81953
+    case 3:
Justin M. Forbes a81953
+        if (migrate_client && client_connected) {
Justin M. Forbes a81953
+            fprintf(stderr, "%s: finish client migration\n", __FUNCTION__);
Justin M. Forbes a81953
+            spice_server_migrate_end(spice_server, 1);
Justin M. Forbes a81953
+        }
Justin M. Forbes a81953
+        break;
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+    return ret;
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+static void spice_save(QEMUFile *f, void *opaque)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    fprintf(stderr, "%s:\n", __FUNCTION__);
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+static int spice_load(QEMUFile *f, void *opaque, int version_id)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    fprintf(stderr, "%s:\n", __FUNCTION__);
Justin M. Forbes a81953
+    return 0;
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
 /* functions for the rest of qemu */
Justin M. Forbes a81953
Justin M. Forbes a81953
+int mon_spice_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    const char *hostname = qdict_get_str(qdict, "hostname");
Justin M. Forbes a81953
+    const char *subject  = qdict_get_try_str(qdict, "cert-subject");
Justin M. Forbes a81953
+    int port             = qdict_get_try_int(qdict, "port", -1);
Justin M. Forbes a81953
+    int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    if (!spice_server) {
Justin M. Forbes a81953
+        qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
Justin M. Forbes a81953
+        return -1;
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    /* TODO: Convert to QError */
Justin M. Forbes a81953
+    return spice_server_migrate_info(spice_server, hostname,
Justin M. Forbes a81953
+                                     port, tls_port, subject);
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
 void qemu_spice_init(void)
Justin M. Forbes a81953
 {
Justin M. Forbes a81953
     QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
Justin M. Forbes a81953
@@ -206,6 +289,10 @@ void qemu_spice_init(void)
Justin M. Forbes a81953
     qemu_spice_input_init();
Justin M. Forbes a81953
     qemu_spice_audio_init();
Justin M. Forbes a81953
Justin M. Forbes a81953
+    register_savevm_live(NULL, "spice", -1, 1, NULL,
Justin M. Forbes a81953
+                         spice_live, spice_save, spice_load,
Justin M. Forbes a81953
+                         spice_server);
Justin M. Forbes a81953
+
Justin M. Forbes a81953
     qemu_free(x509_key_file);
Justin M. Forbes a81953
     qemu_free(x509_cert_file);
Justin M. Forbes a81953
     qemu_free(x509_cacert_file);
Justin M. Forbes a81953
-- 
Justin M. Forbes a81953
1.7.2.3
Justin M. Forbes a81953