2 * QEMU live block migration
4 * Copyright IBM, Corp. 2009
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu-common.h"
17 #include "block/block.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
21 #include "qemu/queue.h"
22 #include "qemu/timer.h"
23 #include "migration/block.h"
24 #include "migration/migration.h"
25 #include "sysemu/blockdev.h"
26 #include "sysemu/block-backend.h"
29 #define BLOCK_SIZE (1 << 20)
30 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
32 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
33 #define BLK_MIG_FLAG_EOS 0x02
34 #define BLK_MIG_FLAG_PROGRESS 0x04
35 #define BLK_MIG_FLAG_ZERO_BLOCK 0x08
37 #define MAX_IS_ALLOCATED_SEARCH 65536
39 #define MAX_INFLIGHT_IO 512
41 //#define DEBUG_BLK_MIGRATION
43 #ifdef DEBUG_BLK_MIGRATION
44 #define DPRINTF(fmt, ...) \
45 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
47 #define DPRINTF(fmt, ...) \
51 typedef struct BlkMigDevState {
52 /* Written during setup phase. Can be read without a lock. */
55 int64_t total_sectors;
56 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
58 /* Only used by migration thread. Does not need a lock. */
63 /* Protected by block migration lock. */
64 unsigned long *aio_bitmap;
65 int64_t completed_sectors;
66 BdrvDirtyBitmap *dirty_bitmap;
70 typedef struct BlkMigBlock {
71 /* Only used by migration thread. */
80 /* Protected by block migration lock. */
82 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
85 typedef struct BlkMigState {
86 /* Written during setup phase. Can be read without a lock. */
89 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
90 int64_t total_sector_sum;
93 /* Protected by lock. */
94 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
98 /* Only used by migration thread. Does not need a lock. */
103 /* Lock must be taken _inside_ the iothread lock. */
107 static BlkMigState block_mig_state;
109 static void blk_mig_lock(void)
111 qemu_mutex_lock(&block_mig_state.lock);
114 static void blk_mig_unlock(void)
116 qemu_mutex_unlock(&block_mig_state.lock);
119 /* Must run outside of the iothread lock during the bulk phase,
120 * or the VM will stall.
123 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
126 uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK;
128 if (block_mig_state.zero_blocks &&
129 buffer_is_zero(blk->buf, BLOCK_SIZE)) {
130 flags |= BLK_MIG_FLAG_ZERO_BLOCK;
133 /* sector number and flags */
134 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
138 len = strlen(bdrv_get_device_name(blk->bmds->bs));
139 qemu_put_byte(f, len);
140 qemu_put_buffer(f, (uint8_t *)bdrv_get_device_name(blk->bmds->bs), len);
142 /* if a block is zero we need to flush here since the network
143 * bandwidth is now a lot higher than the storage device bandwidth.
144 * thus if we queue zero blocks we slow down the migration */
145 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
150 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
153 int blk_mig_active(void)
155 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
158 uint64_t blk_mig_bytes_transferred(void)
160 BlkMigDevState *bmds;
164 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
165 sum += bmds->completed_sectors;
168 return sum << BDRV_SECTOR_BITS;
171 uint64_t blk_mig_bytes_remaining(void)
173 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
176 uint64_t blk_mig_bytes_total(void)
178 BlkMigDevState *bmds;
181 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
182 sum += bmds->total_sectors;
184 return sum << BDRV_SECTOR_BITS;
188 /* Called with migration lock held. */
190 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
192 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
194 if (sector < bdrv_nb_sectors(bmds->bs)) {
195 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
196 (1UL << (chunk % (sizeof(unsigned long) * 8))));
202 /* Called with migration lock held. */
204 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
205 int nb_sectors, int set)
208 unsigned long val, idx, bit;
210 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
211 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
213 for (; start <= end; start++) {
214 idx = start / (sizeof(unsigned long) * 8);
215 bit = start % (sizeof(unsigned long) * 8);
216 val = bmds->aio_bitmap[idx];
220 val &= ~(1UL << bit);
222 bmds->aio_bitmap[idx] = val;
226 static void alloc_aio_bitmap(BlkMigDevState *bmds)
228 BlockDriverState *bs = bmds->bs;
231 bitmap_size = bdrv_nb_sectors(bs) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
232 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
234 bmds->aio_bitmap = g_malloc0(bitmap_size);
237 /* Never hold migration lock when yielding to the main loop! */
239 static void blk_mig_read_cb(void *opaque, int ret)
241 BlkMigBlock *blk = opaque;
246 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
247 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
249 block_mig_state.submitted--;
250 block_mig_state.read_done++;
251 assert(block_mig_state.submitted >= 0);
255 /* Called with no lock taken. */
257 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
259 int64_t total_sectors = bmds->total_sectors;
260 int64_t cur_sector = bmds->cur_sector;
261 BlockDriverState *bs = bmds->bs;
265 if (bmds->shared_base) {
266 qemu_mutex_lock_iothread();
267 while (cur_sector < total_sectors &&
268 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
270 cur_sector += nr_sectors;
272 qemu_mutex_unlock_iothread();
275 if (cur_sector >= total_sectors) {
276 bmds->cur_sector = bmds->completed_sectors = total_sectors;
280 bmds->completed_sectors = cur_sector;
282 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
284 /* we are going to transfer a full block even if it is not allocated */
285 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
287 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
288 nr_sectors = total_sectors - cur_sector;
291 blk = g_new(BlkMigBlock, 1);
292 blk->buf = g_malloc(BLOCK_SIZE);
294 blk->sector = cur_sector;
295 blk->nr_sectors = nr_sectors;
297 blk->iov.iov_base = blk->buf;
298 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
299 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
302 block_mig_state.submitted++;
305 qemu_mutex_lock_iothread();
306 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
307 nr_sectors, blk_mig_read_cb, blk);
309 bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector, nr_sectors);
310 qemu_mutex_unlock_iothread();
312 bmds->cur_sector = cur_sector + nr_sectors;
313 return (bmds->cur_sector >= total_sectors);
316 /* Called with iothread lock taken. */
318 static int set_dirty_tracking(void)
320 BlkMigDevState *bmds;
323 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
324 bmds->dirty_bitmap = bdrv_create_dirty_bitmap(bmds->bs, BLOCK_SIZE,
326 if (!bmds->dirty_bitmap) {
334 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
335 if (bmds->dirty_bitmap) {
336 bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
342 static void unset_dirty_tracking(void)
344 BlkMigDevState *bmds;
346 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
347 bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
351 static void init_blk_migration(QEMUFile *f)
353 BlockDriverState *bs;
354 BlkMigDevState *bmds;
357 block_mig_state.submitted = 0;
358 block_mig_state.read_done = 0;
359 block_mig_state.transferred = 0;
360 block_mig_state.total_sector_sum = 0;
361 block_mig_state.prev_progress = -1;
362 block_mig_state.bulk_completed = 0;
363 block_mig_state.zero_blocks = migrate_zero_blocks();
365 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
366 if (bdrv_is_read_only(bs)) {
370 sectors = bdrv_nb_sectors(bs);
375 bmds = g_new0(BlkMigDevState, 1);
377 bmds->bulk_completed = 0;
378 bmds->total_sectors = sectors;
379 bmds->completed_sectors = 0;
380 bmds->shared_base = block_mig_state.shared_base;
381 alloc_aio_bitmap(bmds);
382 error_setg(&bmds->blocker, "block device is in use by migration");
383 bdrv_op_block_all(bs, bmds->blocker);
386 block_mig_state.total_sector_sum += sectors;
388 if (bmds->shared_base) {
389 DPRINTF("Start migration for %s with shared base image\n",
390 bdrv_get_device_name(bs));
392 DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs));
395 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
399 /* Called with no lock taken. */
401 static int blk_mig_save_bulked_block(QEMUFile *f)
403 int64_t completed_sector_sum = 0;
404 BlkMigDevState *bmds;
408 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
409 if (bmds->bulk_completed == 0) {
410 if (mig_save_device_bulk(f, bmds) == 1) {
411 /* completed bulk section for this device */
412 bmds->bulk_completed = 1;
414 completed_sector_sum += bmds->completed_sectors;
418 completed_sector_sum += bmds->completed_sectors;
422 if (block_mig_state.total_sector_sum != 0) {
423 progress = completed_sector_sum * 100 /
424 block_mig_state.total_sector_sum;
428 if (progress != block_mig_state.prev_progress) {
429 block_mig_state.prev_progress = progress;
430 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
431 | BLK_MIG_FLAG_PROGRESS);
432 DPRINTF("Completed %d %%\r", progress);
438 static void blk_mig_reset_dirty_cursor(void)
440 BlkMigDevState *bmds;
442 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
447 /* Called with iothread lock taken. */
449 static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
453 int64_t total_sectors = bmds->total_sectors;
458 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
460 if (bmds_aio_inflight(bmds, sector)) {
462 bdrv_drain(bmds->bs);
466 if (bdrv_get_dirty(bmds->bs, bmds->dirty_bitmap, sector)) {
468 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
469 nr_sectors = total_sectors - sector;
471 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
473 blk = g_new(BlkMigBlock, 1);
474 blk->buf = g_malloc(BLOCK_SIZE);
476 blk->sector = sector;
477 blk->nr_sectors = nr_sectors;
480 blk->iov.iov_base = blk->buf;
481 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
482 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
484 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
485 nr_sectors, blk_mig_read_cb, blk);
488 block_mig_state.submitted++;
489 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
492 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
502 bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, sector, nr_sectors);
505 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
506 bmds->cur_dirty = sector;
509 return (bmds->cur_dirty >= bmds->total_sectors);
512 DPRINTF("Error reading sector %" PRId64 "\n", sector);
518 /* Called with iothread lock taken.
521 * 0: too much data for max_downtime
522 * 1: few enough data for max_downtime
524 static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
526 BlkMigDevState *bmds;
529 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
530 ret = mig_save_device_dirty(f, bmds, is_async);
539 /* Called with no locks taken. */
541 static int flush_blks(QEMUFile *f)
546 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
547 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
548 block_mig_state.transferred);
551 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
552 if (qemu_file_rate_limit(f)) {
560 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
568 block_mig_state.read_done--;
569 block_mig_state.transferred++;
570 assert(block_mig_state.read_done >= 0);
574 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
575 block_mig_state.submitted, block_mig_state.read_done,
576 block_mig_state.transferred);
580 /* Called with iothread lock taken. */
582 static int64_t get_remaining_dirty(void)
584 BlkMigDevState *bmds;
587 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
588 dirty += bdrv_get_dirty_count(bmds->dirty_bitmap);
591 return dirty << BDRV_SECTOR_BITS;
594 /* Called with iothread lock taken. */
596 static void block_migration_cleanup(void *opaque)
598 BlkMigDevState *bmds;
603 unset_dirty_tracking();
606 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
607 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
608 bdrv_op_unblock_all(bmds->bs, bmds->blocker);
609 error_free(bmds->blocker);
610 bdrv_unref(bmds->bs);
611 g_free(bmds->aio_bitmap);
615 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
616 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
623 static int block_save_setup(QEMUFile *f, void *opaque)
627 DPRINTF("Enter save live setup submitted %d transferred %d\n",
628 block_mig_state.submitted, block_mig_state.transferred);
630 qemu_mutex_lock_iothread();
631 init_blk_migration(f);
633 /* start track dirty blocks */
634 ret = set_dirty_tracking();
637 qemu_mutex_unlock_iothread();
641 qemu_mutex_unlock_iothread();
644 blk_mig_reset_dirty_cursor();
645 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
650 static int block_save_iterate(QEMUFile *f, void *opaque)
653 int64_t last_ftell = qemu_ftell(f);
656 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
657 block_mig_state.submitted, block_mig_state.transferred);
664 blk_mig_reset_dirty_cursor();
666 /* control the rate of transfer */
668 while ((block_mig_state.submitted +
669 block_mig_state.read_done) * BLOCK_SIZE <
670 qemu_file_get_rate_limit(f) &&
671 (block_mig_state.submitted +
672 block_mig_state.read_done) <
675 if (block_mig_state.bulk_completed == 0) {
676 /* first finish the bulk phase */
677 if (blk_mig_save_bulked_block(f) == 0) {
678 /* finished saving bulk on all devices */
679 block_mig_state.bulk_completed = 1;
683 /* Always called with iothread lock taken for
684 * simplicity, block_save_complete also calls it.
686 qemu_mutex_lock_iothread();
687 ret = blk_mig_save_dirty_block(f, 1);
688 qemu_mutex_unlock_iothread();
695 /* no more dirty blocks */
706 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
707 delta_ftell = qemu_ftell(f) - last_ftell;
708 if (delta_ftell > 0) {
710 } else if (delta_ftell < 0) {
717 /* Called with iothread lock taken. */
719 static int block_save_complete(QEMUFile *f, void *opaque)
723 DPRINTF("Enter save live complete submitted %d transferred %d\n",
724 block_mig_state.submitted, block_mig_state.transferred);
731 blk_mig_reset_dirty_cursor();
733 /* we know for sure that save bulk is completed and
734 all async read completed */
736 assert(block_mig_state.submitted == 0);
740 ret = blk_mig_save_dirty_block(f, 0);
746 /* report completion */
747 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
749 DPRINTF("Block migration completed\n");
751 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
756 static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
757 uint64_t *non_postcopiable_pending,
758 uint64_t *postcopiable_pending)
760 /* Estimate pending number of bytes to send */
763 qemu_mutex_lock_iothread();
765 pending = get_remaining_dirty() +
766 block_mig_state.submitted * BLOCK_SIZE +
767 block_mig_state.read_done * BLOCK_SIZE;
769 /* Report at least one block pending during bulk phase */
770 if (pending <= max_size && !block_mig_state.bulk_completed) {
771 pending = max_size + BLOCK_SIZE;
774 qemu_mutex_unlock_iothread();
776 DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
777 /* We don't do postcopy */
778 *non_postcopiable_pending += pending;
781 static int block_load(QEMUFile *f, void *opaque, int version_id)
783 static int banner_printed;
785 char device_name[256];
787 BlockDriverState *bs, *bs_prev = NULL;
790 int64_t total_sectors = 0;
795 addr = qemu_get_be64(f);
797 flags = addr & ~BDRV_SECTOR_MASK;
798 addr >>= BDRV_SECTOR_BITS;
800 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
801 /* get device name */
802 len = qemu_get_byte(f);
803 qemu_get_buffer(f, (uint8_t *)device_name, len);
804 device_name[len] = '\0';
806 blk = blk_by_name(device_name);
808 fprintf(stderr, "Error unknown block device %s\n",
814 fprintf(stderr, "Block device %s has no medium\n",
821 total_sectors = bdrv_nb_sectors(bs);
822 if (total_sectors <= 0) {
823 error_report("Error getting length of block device %s",
829 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
830 nr_sectors = total_sectors - addr;
832 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
835 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
836 ret = bdrv_write_zeroes(bs, addr, nr_sectors,
839 buf = g_malloc(BLOCK_SIZE);
840 qemu_get_buffer(f, buf, BLOCK_SIZE);
841 ret = bdrv_write(bs, addr, buf, nr_sectors);
848 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
849 if (!banner_printed) {
850 printf("Receiving block device images\n");
853 printf("Completed %d %%%c", (int)addr,
854 (addr == 100) ? '\n' : '\r');
856 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
857 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
860 ret = qemu_file_get_error(f);
864 } while (!(flags & BLK_MIG_FLAG_EOS));
869 static void block_set_params(const MigrationParams *params, void *opaque)
871 block_mig_state.blk_enable = params->blk;
872 block_mig_state.shared_base = params->shared;
874 /* shared base means that blk_enable = 1 */
875 block_mig_state.blk_enable |= params->shared;
878 static bool block_is_active(void *opaque)
880 return block_mig_state.blk_enable == 1;
883 static SaveVMHandlers savevm_block_handlers = {
884 .set_params = block_set_params,
885 .save_live_setup = block_save_setup,
886 .save_live_iterate = block_save_iterate,
887 .save_live_complete_precopy = block_save_complete,
888 .save_live_pending = block_save_pending,
889 .load_state = block_load,
890 .cleanup = block_migration_cleanup,
891 .is_active = block_is_active,
894 void blk_mig_init(void)
896 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
897 QSIMPLEQ_INIT(&block_mig_state.blk_list);
898 qemu_mutex_init(&block_mig_state.lock);
900 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,