9ae3a8
From 714d313805542063ea68e26a0d47b5059ee9952e Mon Sep 17 00:00:00 2001
9ae3a8
From: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
Date: Tue, 13 Aug 2013 09:06:36 +0200
9ae3a8
Subject: dataplane: enable virtio-blk x-data-plane=on live migration
9ae3a8
9ae3a8
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
Message-id: <1376384797-4701-6-git-send-email-stefanha@redhat.com>
9ae3a8
Patchwork-id: 53210
9ae3a8
O-Subject: [PATCH v2 5/6] dataplane: enable virtio-blk x-data-plane=on live migration
9ae3a8
Bugzilla: 995030
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
9ae3a8
Although the dataplane thread does not cooperate with dirty memory
9ae3a8
logging yet it's fairly easy to temporarily disable dataplane during
9ae3a8
live migration.  This way virtio-blk can live migrate when
9ae3a8
x-data-plane=on.
9ae3a8
9ae3a8
The dataplane thread will restart after migration is cancelled or if the
9ae3a8
guest resuming virtio-blk operation after migration completes.
9ae3a8
9ae3a8
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
(cherry picked from commit 84db52d059f3296abf7783968645c4a96d21b099)
9ae3a8
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
diff --git a/hw/block/dataplane/virtio-blk.c b/hw/block/dataplane/virtio-blk.c
9ae3a8
index 2faed43..63c3ffa 100644
9ae3a8
--- a/hw/block/dataplane/virtio-blk.c
9ae3a8
+++ b/hw/block/dataplane/virtio-blk.c
9ae3a8
@@ -18,7 +18,6 @@
9ae3a8
 #include "qemu/error-report.h"
9ae3a8
 #include "hw/virtio/dataplane/vring.h"
9ae3a8
 #include "ioq.h"
9ae3a8
-#include "migration/migration.h"
9ae3a8
 #include "block/block.h"
9ae3a8
 #include "hw/virtio/virtio-blk.h"
9ae3a8
 #include "virtio-blk.h"
9ae3a8
@@ -69,8 +68,6 @@ struct VirtIOBlockDataPlane {
9ae3a8
                                              queue */
9ae3a8
 
9ae3a8
     unsigned int num_reqs;
9ae3a8
-
9ae3a8
-    Error *migration_blocker;
9ae3a8
 };
9ae3a8
 
9ae3a8
 /* Raise an interrupt to signal guest, if necessary */
9ae3a8
@@ -433,10 +430,6 @@ bool virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *blk,
9ae3a8
     /* Prevent block operations that conflict with data plane thread */
9ae3a8
     bdrv_set_in_use(blk->conf.bs, 1);
9ae3a8
 
9ae3a8
-    error_setg(&s->migration_blocker,
9ae3a8
-            "x-data-plane does not support migration");
9ae3a8
-    migrate_add_blocker(s->migration_blocker);
9ae3a8
-
9ae3a8
     *dataplane = s;
9ae3a8
     return true;
9ae3a8
 }
9ae3a8
@@ -448,8 +441,6 @@ void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
9ae3a8
     }
9ae3a8
 
9ae3a8
     virtio_blk_data_plane_stop(s);
9ae3a8
-    migrate_del_blocker(s->migration_blocker);
9ae3a8
-    error_free(s->migration_blocker);
9ae3a8
     bdrv_set_in_use(s->blk->conf.bs, 0);
9ae3a8
     g_free(s);
9ae3a8
 }
9ae3a8
diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
9ae3a8
index cf12469..cca0c77 100644
9ae3a8
--- a/hw/block/virtio-blk.c
9ae3a8
+++ b/hw/block/virtio-blk.c
9ae3a8
@@ -19,6 +19,7 @@
9ae3a8
 #include "hw/virtio/virtio-blk.h"
9ae3a8
 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
9ae3a8
 # include "dataplane/virtio-blk.h"
9ae3a8
+# include "migration/migration.h"
9ae3a8
 #endif
9ae3a8
 #include "block/scsi.h"
9ae3a8
 #ifdef __linux__
9ae3a8
@@ -628,6 +629,34 @@ void virtio_blk_set_conf(DeviceState *dev, VirtIOBlkConf *blk)
9ae3a8
     memcpy(&(s->blk), blk, sizeof(struct VirtIOBlkConf));
9ae3a8
 }
9ae3a8
 
9ae3a8
+#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
9ae3a8
+/* Disable dataplane thread during live migration since it does not
9ae3a8
+ * update the dirty memory bitmap yet.
9ae3a8
+ */
9ae3a8
+static void virtio_blk_migration_state_changed(Notifier *notifier, void *data)
9ae3a8
+{
9ae3a8
+    VirtIOBlock *s = container_of(notifier, VirtIOBlock,
9ae3a8
+                                  migration_state_notifier);
9ae3a8
+    MigrationState *mig = data;
9ae3a8
+
9ae3a8
+    if (migration_is_active(mig)) {
9ae3a8
+        if (!s->dataplane) {
9ae3a8
+            return;
9ae3a8
+        }
9ae3a8
+        virtio_blk_data_plane_destroy(s->dataplane);
9ae3a8
+        s->dataplane = NULL;
9ae3a8
+    } else if (migration_has_finished(mig) ||
9ae3a8
+               migration_has_failed(mig)) {
9ae3a8
+        if (s->dataplane) {
9ae3a8
+            return;
9ae3a8
+        }
9ae3a8
+        bdrv_drain_all(); /* complete in-flight non-dataplane requests */
9ae3a8
+        virtio_blk_data_plane_create(VIRTIO_DEVICE(s), &s->blk,
9ae3a8
+                                     &s->dataplane);
9ae3a8
+    }
9ae3a8
+}
9ae3a8
+#endif /* CONFIG_VIRTIO_BLK_DATA_PLANE */
9ae3a8
+
9ae3a8
 static int virtio_blk_device_init(VirtIODevice *vdev)
9ae3a8
 {
9ae3a8
     DeviceState *qdev = DEVICE(vdev);
9ae3a8
@@ -664,6 +693,8 @@ static int virtio_blk_device_init(VirtIODevice *vdev)
9ae3a8
         virtio_cleanup(vdev);
9ae3a8
         return -1;
9ae3a8
     }
9ae3a8
+    s->migration_state_notifier.notify = virtio_blk_migration_state_changed;
9ae3a8
+    add_migration_state_change_notifier(&s->migration_state_notifier);
9ae3a8
 #endif
9ae3a8
 
9ae3a8
     s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
9ae3a8
@@ -683,6 +714,7 @@ static int virtio_blk_device_exit(DeviceState *dev)
9ae3a8
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
9ae3a8
     VirtIOBlock *s = VIRTIO_BLK(dev);
9ae3a8
 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
9ae3a8
+    remove_migration_state_change_notifier(&s->migration_state_notifier);
9ae3a8
     virtio_blk_data_plane_destroy(s->dataplane);
9ae3a8
     s->dataplane = NULL;
9ae3a8
 #endif
9ae3a8
diff --git a/include/hw/virtio/virtio-blk.h b/include/hw/virtio/virtio-blk.h
9ae3a8
index fc71853..b87cf49 100644
9ae3a8
--- a/include/hw/virtio/virtio-blk.h
9ae3a8
+++ b/include/hw/virtio/virtio-blk.h
9ae3a8
@@ -125,6 +125,7 @@ typedef struct VirtIOBlock {
9ae3a8
     unsigned short sector_mask;
9ae3a8
     VMChangeStateEntry *change;
9ae3a8
 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
9ae3a8
+    Notifier migration_state_notifier;
9ae3a8
     struct VirtIOBlockDataPlane *dataplane;
9ae3a8
 #endif
9ae3a8
 } VirtIOBlock;