2 * QEMU live block migration
4 * Copyright IBM, Corp. 2009
7 * Liran Schour <lirans@il.ibm.com>
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_int.h"
19 #include "qemu/queue.h"
20 #include "qemu/timer.h"
21 #include "migration/block.h"
22 #include "migration/migration.h"
23 #include "sysemu/blockdev.h"
26 #define BLOCK_SIZE (1 << 20)
27 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
29 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
30 #define BLK_MIG_FLAG_EOS 0x02
31 #define BLK_MIG_FLAG_PROGRESS 0x04
32 #define BLK_MIG_FLAG_ZERO_BLOCK 0x08
34 #define MAX_IS_ALLOCATED_SEARCH 65536
36 //#define DEBUG_BLK_MIGRATION
38 #ifdef DEBUG_BLK_MIGRATION
39 #define DPRINTF(fmt, ...) \
40 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
42 #define DPRINTF(fmt, ...) \
46 typedef struct BlkMigDevState
{
47 /* Written during setup phase. Can be read without a lock. */
50 int64_t total_sectors
;
51 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
53 /* Only used by migration thread. Does not need a lock. */
58 /* Protected by block migration lock. */
59 unsigned long *aio_bitmap
;
60 int64_t completed_sectors
;
61 BdrvDirtyBitmap
*dirty_bitmap
;
64 typedef struct BlkMigBlock
{
65 /* Only used by migration thread. */
72 BlockDriverAIOCB
*aiocb
;
74 /* Protected by block migration lock. */
76 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
79 typedef struct BlkMigState
{
80 /* Written during setup phase. Can be read without a lock. */
83 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
84 int64_t total_sector_sum
;
87 /* Protected by lock. */
88 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
92 /* Only used by migration thread. Does not need a lock. */
97 /* Lock must be taken _inside_ the iothread lock. */
101 static BlkMigState block_mig_state
;
103 static void blk_mig_lock(void)
105 qemu_mutex_lock(&block_mig_state
.lock
);
108 static void blk_mig_unlock(void)
110 qemu_mutex_unlock(&block_mig_state
.lock
);
113 /* Must run outside of the iothread lock during the bulk phase,
114 * or the VM will stall.
117 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
120 uint64_t flags
= BLK_MIG_FLAG_DEVICE_BLOCK
;
122 if (block_mig_state
.zero_blocks
&&
123 buffer_is_zero(blk
->buf
, BLOCK_SIZE
)) {
124 flags
|= BLK_MIG_FLAG_ZERO_BLOCK
;
127 /* sector number and flags */
128 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
132 len
= strlen(blk
->bmds
->bs
->device_name
);
133 qemu_put_byte(f
, len
);
134 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
136 /* if a block is zero we need to flush here since the network
137 * bandwidth is now a lot higher than the storage device bandwidth.
138 * thus if we queue zero blocks we slow down the migration */
139 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
144 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
147 int blk_mig_active(void)
149 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
152 uint64_t blk_mig_bytes_transferred(void)
154 BlkMigDevState
*bmds
;
158 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
159 sum
+= bmds
->completed_sectors
;
162 return sum
<< BDRV_SECTOR_BITS
;
165 uint64_t blk_mig_bytes_remaining(void)
167 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
170 uint64_t blk_mig_bytes_total(void)
172 BlkMigDevState
*bmds
;
175 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
176 sum
+= bmds
->total_sectors
;
178 return sum
<< BDRV_SECTOR_BITS
;
182 /* Called with migration lock held. */
184 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
186 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
188 if ((sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bmds
->bs
)) {
189 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
190 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
196 /* Called with migration lock held. */
198 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
199 int nb_sectors
, int set
)
202 unsigned long val
, idx
, bit
;
204 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
205 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
207 for (; start
<= end
; start
++) {
208 idx
= start
/ (sizeof(unsigned long) * 8);
209 bit
= start
% (sizeof(unsigned long) * 8);
210 val
= bmds
->aio_bitmap
[idx
];
214 val
&= ~(1UL << bit
);
216 bmds
->aio_bitmap
[idx
] = val
;
220 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
222 BlockDriverState
*bs
= bmds
->bs
;
225 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
226 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
227 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
229 bmds
->aio_bitmap
= g_malloc0(bitmap_size
);
232 /* Never hold migration lock when yielding to the main loop! */
234 static void blk_mig_read_cb(void *opaque
, int ret
)
236 BlkMigBlock
*blk
= opaque
;
241 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
242 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
244 block_mig_state
.submitted
--;
245 block_mig_state
.read_done
++;
246 assert(block_mig_state
.submitted
>= 0);
250 /* Called with no lock taken. */
252 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
254 int64_t total_sectors
= bmds
->total_sectors
;
255 int64_t cur_sector
= bmds
->cur_sector
;
256 BlockDriverState
*bs
= bmds
->bs
;
260 if (bmds
->shared_base
) {
261 qemu_mutex_lock_iothread();
262 while (cur_sector
< total_sectors
&&
263 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
265 cur_sector
+= nr_sectors
;
267 qemu_mutex_unlock_iothread();
270 if (cur_sector
>= total_sectors
) {
271 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
275 bmds
->completed_sectors
= cur_sector
;
277 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
279 /* we are going to transfer a full block even if it is not allocated */
280 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
282 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
283 nr_sectors
= total_sectors
- cur_sector
;
286 blk
= g_malloc(sizeof(BlkMigBlock
));
287 blk
->buf
= g_malloc(BLOCK_SIZE
);
289 blk
->sector
= cur_sector
;
290 blk
->nr_sectors
= nr_sectors
;
292 blk
->iov
.iov_base
= blk
->buf
;
293 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
294 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
297 block_mig_state
.submitted
++;
300 qemu_mutex_lock_iothread();
301 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
302 nr_sectors
, blk_mig_read_cb
, blk
);
304 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
305 qemu_mutex_unlock_iothread();
307 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
308 return (bmds
->cur_sector
>= total_sectors
);
311 /* Called with iothread lock taken. */
313 static void set_dirty_tracking(void)
315 BlkMigDevState
*bmds
;
317 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
318 bmds
->dirty_bitmap
= bdrv_create_dirty_bitmap(bmds
->bs
, BLOCK_SIZE
);
322 static void unset_dirty_tracking(void)
324 BlkMigDevState
*bmds
;
326 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
327 bdrv_release_dirty_bitmap(bmds
->bs
, bmds
->dirty_bitmap
);
331 static void init_blk_migration_it(void *opaque
, BlockDriverState
*bs
)
333 BlkMigDevState
*bmds
;
336 if (!bdrv_is_read_only(bs
)) {
337 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
342 bmds
= g_malloc0(sizeof(BlkMigDevState
));
344 bmds
->bulk_completed
= 0;
345 bmds
->total_sectors
= sectors
;
346 bmds
->completed_sectors
= 0;
347 bmds
->shared_base
= block_mig_state
.shared_base
;
348 alloc_aio_bitmap(bmds
);
349 bdrv_set_in_use(bs
, 1);
352 block_mig_state
.total_sector_sum
+= sectors
;
354 if (bmds
->shared_base
) {
355 DPRINTF("Start migration for %s with shared base image\n",
358 DPRINTF("Start full migration for %s\n", bs
->device_name
);
361 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
365 static void init_blk_migration(QEMUFile
*f
)
367 block_mig_state
.submitted
= 0;
368 block_mig_state
.read_done
= 0;
369 block_mig_state
.transferred
= 0;
370 block_mig_state
.total_sector_sum
= 0;
371 block_mig_state
.prev_progress
= -1;
372 block_mig_state
.bulk_completed
= 0;
373 block_mig_state
.zero_blocks
= migrate_zero_blocks();
375 bdrv_iterate(init_blk_migration_it
, NULL
);
378 /* Called with no lock taken. */
380 static int blk_mig_save_bulked_block(QEMUFile
*f
)
382 int64_t completed_sector_sum
= 0;
383 BlkMigDevState
*bmds
;
387 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
388 if (bmds
->bulk_completed
== 0) {
389 if (mig_save_device_bulk(f
, bmds
) == 1) {
390 /* completed bulk section for this device */
391 bmds
->bulk_completed
= 1;
393 completed_sector_sum
+= bmds
->completed_sectors
;
397 completed_sector_sum
+= bmds
->completed_sectors
;
401 if (block_mig_state
.total_sector_sum
!= 0) {
402 progress
= completed_sector_sum
* 100 /
403 block_mig_state
.total_sector_sum
;
407 if (progress
!= block_mig_state
.prev_progress
) {
408 block_mig_state
.prev_progress
= progress
;
409 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
410 | BLK_MIG_FLAG_PROGRESS
);
411 DPRINTF("Completed %d %%\r", progress
);
417 static void blk_mig_reset_dirty_cursor(void)
419 BlkMigDevState
*bmds
;
421 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
426 /* Called with iothread lock taken. */
428 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
432 int64_t total_sectors
= bmds
->total_sectors
;
437 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
439 if (bmds_aio_inflight(bmds
, sector
)) {
445 if (bdrv_get_dirty(bmds
->bs
, bmds
->dirty_bitmap
, sector
)) {
447 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
448 nr_sectors
= total_sectors
- sector
;
450 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
452 blk
= g_malloc(sizeof(BlkMigBlock
));
453 blk
->buf
= g_malloc(BLOCK_SIZE
);
455 blk
->sector
= sector
;
456 blk
->nr_sectors
= nr_sectors
;
459 blk
->iov
.iov_base
= blk
->buf
;
460 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
461 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
463 blk
->aiocb
= bdrv_aio_readv(bmds
->bs
, sector
, &blk
->qiov
,
464 nr_sectors
, blk_mig_read_cb
, blk
);
467 block_mig_state
.submitted
++;
468 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
471 ret
= bdrv_read(bmds
->bs
, sector
, blk
->buf
, nr_sectors
);
481 bdrv_reset_dirty(bmds
->bs
, sector
, nr_sectors
);
484 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
485 bmds
->cur_dirty
= sector
;
488 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
491 DPRINTF("Error reading sector %" PRId64
"\n", sector
);
497 /* Called with iothread lock taken.
500 * 0: too much data for max_downtime
501 * 1: few enough data for max_downtime
503 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
505 BlkMigDevState
*bmds
;
508 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
509 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
518 /* Called with no locks taken. */
520 static int flush_blks(QEMUFile
*f
)
525 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
526 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
527 block_mig_state
.transferred
);
530 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
531 if (qemu_file_rate_limit(f
)) {
539 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
547 block_mig_state
.read_done
--;
548 block_mig_state
.transferred
++;
549 assert(block_mig_state
.read_done
>= 0);
553 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
554 block_mig_state
.submitted
, block_mig_state
.read_done
,
555 block_mig_state
.transferred
);
559 /* Called with iothread lock taken. */
561 static int64_t get_remaining_dirty(void)
563 BlkMigDevState
*bmds
;
566 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
567 dirty
+= bdrv_get_dirty_count(bmds
->bs
, bmds
->dirty_bitmap
);
570 return dirty
<< BDRV_SECTOR_BITS
;
573 /* Called with iothread lock taken. */
575 static void blk_mig_cleanup(void)
577 BlkMigDevState
*bmds
;
582 unset_dirty_tracking();
585 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
586 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
587 bdrv_set_in_use(bmds
->bs
, 0);
588 bdrv_unref(bmds
->bs
);
589 g_free(bmds
->aio_bitmap
);
593 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
594 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
601 static void block_migration_cancel(void *opaque
)
606 static int block_save_setup(QEMUFile
*f
, void *opaque
)
610 DPRINTF("Enter save live setup submitted %d transferred %d\n",
611 block_mig_state
.submitted
, block_mig_state
.transferred
);
613 qemu_mutex_lock_iothread();
614 init_blk_migration(f
);
616 /* start track dirty blocks */
617 set_dirty_tracking();
618 qemu_mutex_unlock_iothread();
621 blk_mig_reset_dirty_cursor();
622 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
627 static int block_save_iterate(QEMUFile
*f
, void *opaque
)
630 int64_t last_ftell
= qemu_ftell(f
);
632 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
633 block_mig_state
.submitted
, block_mig_state
.transferred
);
640 blk_mig_reset_dirty_cursor();
642 /* control the rate of transfer */
644 while ((block_mig_state
.submitted
+
645 block_mig_state
.read_done
) * BLOCK_SIZE
<
646 qemu_file_get_rate_limit(f
)) {
648 if (block_mig_state
.bulk_completed
== 0) {
649 /* first finish the bulk phase */
650 if (blk_mig_save_bulked_block(f
) == 0) {
651 /* finished saving bulk on all devices */
652 block_mig_state
.bulk_completed
= 1;
656 /* Always called with iothread lock taken for
657 * simplicity, block_save_complete also calls it.
659 qemu_mutex_lock_iothread();
660 ret
= blk_mig_save_dirty_block(f
, 1);
661 qemu_mutex_unlock_iothread();
668 /* no more dirty blocks */
679 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
680 return qemu_ftell(f
) - last_ftell
;
683 /* Called with iothread lock taken. */
685 static int block_save_complete(QEMUFile
*f
, void *opaque
)
689 DPRINTF("Enter save live complete submitted %d transferred %d\n",
690 block_mig_state
.submitted
, block_mig_state
.transferred
);
697 blk_mig_reset_dirty_cursor();
699 /* we know for sure that save bulk is completed and
700 all async read completed */
702 assert(block_mig_state
.submitted
== 0);
706 ret
= blk_mig_save_dirty_block(f
, 0);
712 /* report completion */
713 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
715 DPRINTF("Block migration completed\n");
717 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
723 static uint64_t block_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
)
725 /* Estimate pending number of bytes to send */
728 qemu_mutex_lock_iothread();
730 pending
= get_remaining_dirty() +
731 block_mig_state
.submitted
* BLOCK_SIZE
+
732 block_mig_state
.read_done
* BLOCK_SIZE
;
734 /* Report at least one block pending during bulk phase */
735 if (pending
== 0 && !block_mig_state
.bulk_completed
) {
736 pending
= BLOCK_SIZE
;
739 qemu_mutex_unlock_iothread();
741 DPRINTF("Enter save live pending %" PRIu64
"\n", pending
);
745 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
747 static int banner_printed
;
749 char device_name
[256];
751 BlockDriverState
*bs
, *bs_prev
= NULL
;
753 int64_t total_sectors
= 0;
758 addr
= qemu_get_be64(f
);
760 flags
= addr
& ~BDRV_SECTOR_MASK
;
761 addr
>>= BDRV_SECTOR_BITS
;
763 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
764 /* get device name */
765 len
= qemu_get_byte(f
);
766 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
767 device_name
[len
] = '\0';
769 bs
= bdrv_find(device_name
);
771 fprintf(stderr
, "Error unknown block device %s\n",
778 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
779 if (total_sectors
<= 0) {
780 error_report("Error getting length of block device %s",
786 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
787 nr_sectors
= total_sectors
- addr
;
789 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
792 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
793 ret
= bdrv_write_zeroes(bs
, addr
, nr_sectors
,
796 buf
= g_malloc(BLOCK_SIZE
);
797 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
798 ret
= bdrv_write(bs
, addr
, buf
, nr_sectors
);
805 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
806 if (!banner_printed
) {
807 printf("Receiving block device images\n");
810 printf("Completed %d %%%c", (int)addr
,
811 (addr
== 100) ? '\n' : '\r');
813 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
814 fprintf(stderr
, "Unknown block migration flags: %#x\n", flags
);
817 ret
= qemu_file_get_error(f
);
821 } while (!(flags
& BLK_MIG_FLAG_EOS
));
826 static void block_set_params(const MigrationParams
*params
, void *opaque
)
828 block_mig_state
.blk_enable
= params
->blk
;
829 block_mig_state
.shared_base
= params
->shared
;
831 /* shared base means that blk_enable = 1 */
832 block_mig_state
.blk_enable
|= params
->shared
;
835 static bool block_is_active(void *opaque
)
837 return block_mig_state
.blk_enable
== 1;
840 SaveVMHandlers savevm_block_handlers
= {
841 .set_params
= block_set_params
,
842 .save_live_setup
= block_save_setup
,
843 .save_live_iterate
= block_save_iterate
,
844 .save_live_complete
= block_save_complete
,
845 .save_live_pending
= block_save_pending
,
846 .load_state
= block_load
,
847 .cancel
= block_migration_cancel
,
848 .is_active
= block_is_active
,
851 void blk_mig_init(void)
853 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
854 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
855 qemu_mutex_init(&block_mig_state
.lock
);
857 register_savevm_live(NULL
, "block", 0, 1, &savevm_block_handlers
,