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
;
65 typedef struct BlkMigBlock
{
66 /* Only used by migration thread. */
73 BlockDriverAIOCB
*aiocb
;
75 /* Protected by block migration lock. */
77 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
80 typedef struct BlkMigState
{
81 /* Written during setup phase. Can be read without a lock. */
84 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
85 int64_t total_sector_sum
;
88 /* Protected by lock. */
89 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
93 /* Only used by migration thread. Does not need a lock. */
98 /* Lock must be taken _inside_ the iothread lock. */
102 static BlkMigState block_mig_state
;
104 static void blk_mig_lock(void)
106 qemu_mutex_lock(&block_mig_state
.lock
);
109 static void blk_mig_unlock(void)
111 qemu_mutex_unlock(&block_mig_state
.lock
);
114 /* Must run outside of the iothread lock during the bulk phase,
115 * or the VM will stall.
118 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
121 uint64_t flags
= BLK_MIG_FLAG_DEVICE_BLOCK
;
123 if (block_mig_state
.zero_blocks
&&
124 buffer_is_zero(blk
->buf
, BLOCK_SIZE
)) {
125 flags
|= BLK_MIG_FLAG_ZERO_BLOCK
;
128 /* sector number and flags */
129 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
133 len
= strlen(blk
->bmds
->bs
->device_name
);
134 qemu_put_byte(f
, len
);
135 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
137 /* if a block is zero we need to flush here since the network
138 * bandwidth is now a lot higher than the storage device bandwidth.
139 * thus if we queue zero blocks we slow down the migration */
140 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
145 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
148 int blk_mig_active(void)
150 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
153 uint64_t blk_mig_bytes_transferred(void)
155 BlkMigDevState
*bmds
;
159 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
160 sum
+= bmds
->completed_sectors
;
163 return sum
<< BDRV_SECTOR_BITS
;
166 uint64_t blk_mig_bytes_remaining(void)
168 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
171 uint64_t blk_mig_bytes_total(void)
173 BlkMigDevState
*bmds
;
176 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
177 sum
+= bmds
->total_sectors
;
179 return sum
<< BDRV_SECTOR_BITS
;
183 /* Called with migration lock held. */
185 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
187 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
189 if (sector
< bdrv_nb_sectors(bmds
->bs
)) {
190 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
191 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
197 /* Called with migration lock held. */
199 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
200 int nb_sectors
, int set
)
203 unsigned long val
, idx
, bit
;
205 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
206 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
208 for (; start
<= end
; start
++) {
209 idx
= start
/ (sizeof(unsigned long) * 8);
210 bit
= start
% (sizeof(unsigned long) * 8);
211 val
= bmds
->aio_bitmap
[idx
];
215 val
&= ~(1UL << bit
);
217 bmds
->aio_bitmap
[idx
] = val
;
221 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
223 BlockDriverState
*bs
= bmds
->bs
;
226 bitmap_size
= bdrv_nb_sectors(bs
) + 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 int set_dirty_tracking(void)
315 BlkMigDevState
*bmds
;
318 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
319 bmds
->dirty_bitmap
= bdrv_create_dirty_bitmap(bmds
->bs
, BLOCK_SIZE
,
321 if (!bmds
->dirty_bitmap
) {
329 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
330 if (bmds
->dirty_bitmap
) {
331 bdrv_release_dirty_bitmap(bmds
->bs
, bmds
->dirty_bitmap
);
337 static void unset_dirty_tracking(void)
339 BlkMigDevState
*bmds
;
341 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
342 bdrv_release_dirty_bitmap(bmds
->bs
, bmds
->dirty_bitmap
);
346 static void init_blk_migration_it(void *opaque
, BlockDriverState
*bs
)
348 BlkMigDevState
*bmds
;
351 if (!bdrv_is_read_only(bs
)) {
352 sectors
= bdrv_nb_sectors(bs
);
357 bmds
= g_malloc0(sizeof(BlkMigDevState
));
359 bmds
->bulk_completed
= 0;
360 bmds
->total_sectors
= sectors
;
361 bmds
->completed_sectors
= 0;
362 bmds
->shared_base
= block_mig_state
.shared_base
;
363 alloc_aio_bitmap(bmds
);
364 error_setg(&bmds
->blocker
, "block device is in use by migration");
365 bdrv_op_block_all(bs
, bmds
->blocker
);
368 block_mig_state
.total_sector_sum
+= sectors
;
370 if (bmds
->shared_base
) {
371 DPRINTF("Start migration for %s with shared base image\n",
374 DPRINTF("Start full migration for %s\n", bs
->device_name
);
377 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
381 static void init_blk_migration(QEMUFile
*f
)
383 block_mig_state
.submitted
= 0;
384 block_mig_state
.read_done
= 0;
385 block_mig_state
.transferred
= 0;
386 block_mig_state
.total_sector_sum
= 0;
387 block_mig_state
.prev_progress
= -1;
388 block_mig_state
.bulk_completed
= 0;
389 block_mig_state
.zero_blocks
= migrate_zero_blocks();
391 bdrv_iterate(init_blk_migration_it
, NULL
);
394 /* Called with no lock taken. */
396 static int blk_mig_save_bulked_block(QEMUFile
*f
)
398 int64_t completed_sector_sum
= 0;
399 BlkMigDevState
*bmds
;
403 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
404 if (bmds
->bulk_completed
== 0) {
405 if (mig_save_device_bulk(f
, bmds
) == 1) {
406 /* completed bulk section for this device */
407 bmds
->bulk_completed
= 1;
409 completed_sector_sum
+= bmds
->completed_sectors
;
413 completed_sector_sum
+= bmds
->completed_sectors
;
417 if (block_mig_state
.total_sector_sum
!= 0) {
418 progress
= completed_sector_sum
* 100 /
419 block_mig_state
.total_sector_sum
;
423 if (progress
!= block_mig_state
.prev_progress
) {
424 block_mig_state
.prev_progress
= progress
;
425 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
426 | BLK_MIG_FLAG_PROGRESS
);
427 DPRINTF("Completed %d %%\r", progress
);
433 static void blk_mig_reset_dirty_cursor(void)
435 BlkMigDevState
*bmds
;
437 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
442 /* Called with iothread lock taken. */
444 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
448 int64_t total_sectors
= bmds
->total_sectors
;
453 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
455 if (bmds_aio_inflight(bmds
, sector
)) {
461 if (bdrv_get_dirty(bmds
->bs
, bmds
->dirty_bitmap
, sector
)) {
463 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
464 nr_sectors
= total_sectors
- sector
;
466 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
468 blk
= g_malloc(sizeof(BlkMigBlock
));
469 blk
->buf
= g_malloc(BLOCK_SIZE
);
471 blk
->sector
= sector
;
472 blk
->nr_sectors
= nr_sectors
;
475 blk
->iov
.iov_base
= blk
->buf
;
476 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
477 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
479 blk
->aiocb
= bdrv_aio_readv(bmds
->bs
, sector
, &blk
->qiov
,
480 nr_sectors
, blk_mig_read_cb
, blk
);
483 block_mig_state
.submitted
++;
484 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
487 ret
= bdrv_read(bmds
->bs
, sector
, blk
->buf
, nr_sectors
);
497 bdrv_reset_dirty(bmds
->bs
, sector
, nr_sectors
);
500 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
501 bmds
->cur_dirty
= sector
;
504 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
507 DPRINTF("Error reading sector %" PRId64
"\n", sector
);
513 /* Called with iothread lock taken.
516 * 0: too much data for max_downtime
517 * 1: few enough data for max_downtime
519 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
521 BlkMigDevState
*bmds
;
524 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
525 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
534 /* Called with no locks taken. */
536 static int flush_blks(QEMUFile
*f
)
541 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
542 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
543 block_mig_state
.transferred
);
546 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
547 if (qemu_file_rate_limit(f
)) {
555 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
563 block_mig_state
.read_done
--;
564 block_mig_state
.transferred
++;
565 assert(block_mig_state
.read_done
>= 0);
569 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
570 block_mig_state
.submitted
, block_mig_state
.read_done
,
571 block_mig_state
.transferred
);
575 /* Called with iothread lock taken. */
577 static int64_t get_remaining_dirty(void)
579 BlkMigDevState
*bmds
;
582 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
583 dirty
+= bdrv_get_dirty_count(bmds
->bs
, bmds
->dirty_bitmap
);
586 return dirty
<< BDRV_SECTOR_BITS
;
589 /* Called with iothread lock taken. */
591 static void blk_mig_cleanup(void)
593 BlkMigDevState
*bmds
;
598 unset_dirty_tracking();
601 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
602 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
603 bdrv_op_unblock_all(bmds
->bs
, bmds
->blocker
);
604 error_free(bmds
->blocker
);
605 bdrv_unref(bmds
->bs
);
606 g_free(bmds
->aio_bitmap
);
610 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
611 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
618 static void block_migration_cancel(void *opaque
)
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
);
655 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
656 block_mig_state
.submitted
, block_mig_state
.transferred
);
663 blk_mig_reset_dirty_cursor();
665 /* control the rate of transfer */
667 while ((block_mig_state
.submitted
+
668 block_mig_state
.read_done
) * BLOCK_SIZE
<
669 qemu_file_get_rate_limit(f
)) {
671 if (block_mig_state
.bulk_completed
== 0) {
672 /* first finish the bulk phase */
673 if (blk_mig_save_bulked_block(f
) == 0) {
674 /* finished saving bulk on all devices */
675 block_mig_state
.bulk_completed
= 1;
679 /* Always called with iothread lock taken for
680 * simplicity, block_save_complete also calls it.
682 qemu_mutex_lock_iothread();
683 ret
= blk_mig_save_dirty_block(f
, 1);
684 qemu_mutex_unlock_iothread();
691 /* no more dirty blocks */
702 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
703 return qemu_ftell(f
) - last_ftell
;
706 /* Called with iothread lock taken. */
708 static int block_save_complete(QEMUFile
*f
, void *opaque
)
712 DPRINTF("Enter save live complete submitted %d transferred %d\n",
713 block_mig_state
.submitted
, block_mig_state
.transferred
);
720 blk_mig_reset_dirty_cursor();
722 /* we know for sure that save bulk is completed and
723 all async read completed */
725 assert(block_mig_state
.submitted
== 0);
729 ret
= blk_mig_save_dirty_block(f
, 0);
735 /* report completion */
736 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
738 DPRINTF("Block migration completed\n");
740 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
746 static uint64_t block_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
)
748 /* Estimate pending number of bytes to send */
751 qemu_mutex_lock_iothread();
753 pending
= get_remaining_dirty() +
754 block_mig_state
.submitted
* BLOCK_SIZE
+
755 block_mig_state
.read_done
* BLOCK_SIZE
;
757 /* Report at least one block pending during bulk phase */
758 if (pending
== 0 && !block_mig_state
.bulk_completed
) {
759 pending
= BLOCK_SIZE
;
762 qemu_mutex_unlock_iothread();
764 DPRINTF("Enter save live pending %" PRIu64
"\n", pending
);
768 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
770 static int banner_printed
;
772 char device_name
[256];
774 BlockDriverState
*bs
, *bs_prev
= NULL
;
776 int64_t total_sectors
= 0;
781 addr
= qemu_get_be64(f
);
783 flags
= addr
& ~BDRV_SECTOR_MASK
;
784 addr
>>= BDRV_SECTOR_BITS
;
786 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
787 /* get device name */
788 len
= qemu_get_byte(f
);
789 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
790 device_name
[len
] = '\0';
792 bs
= bdrv_find(device_name
);
794 fprintf(stderr
, "Error unknown block device %s\n",
801 total_sectors
= bdrv_nb_sectors(bs
);
802 if (total_sectors
<= 0) {
803 error_report("Error getting length of block device %s",
809 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
810 nr_sectors
= total_sectors
- addr
;
812 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
815 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
816 ret
= bdrv_write_zeroes(bs
, addr
, nr_sectors
,
819 buf
= g_malloc(BLOCK_SIZE
);
820 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
821 ret
= bdrv_write(bs
, addr
, buf
, nr_sectors
);
828 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
829 if (!banner_printed
) {
830 printf("Receiving block device images\n");
833 printf("Completed %d %%%c", (int)addr
,
834 (addr
== 100) ? '\n' : '\r');
836 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
837 fprintf(stderr
, "Unknown block migration flags: %#x\n", flags
);
840 ret
= qemu_file_get_error(f
);
844 } while (!(flags
& BLK_MIG_FLAG_EOS
));
849 static void block_set_params(const MigrationParams
*params
, void *opaque
)
851 block_mig_state
.blk_enable
= params
->blk
;
852 block_mig_state
.shared_base
= params
->shared
;
854 /* shared base means that blk_enable = 1 */
855 block_mig_state
.blk_enable
|= params
->shared
;
858 static bool block_is_active(void *opaque
)
860 return block_mig_state
.blk_enable
== 1;
863 static SaveVMHandlers savevm_block_handlers
= {
864 .set_params
= block_set_params
,
865 .save_live_setup
= block_save_setup
,
866 .save_live_iterate
= block_save_iterate
,
867 .save_live_complete
= block_save_complete
,
868 .save_live_pending
= block_save_pending
,
869 .load_state
= block_load
,
870 .cancel
= block_migration_cancel
,
871 .is_active
= block_is_active
,
874 void blk_mig_init(void)
876 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
877 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
878 qemu_mutex_init(&block_mig_state
.lock
);
880 register_savevm_live(NULL
, "block", 0, 1, &savevm_block_handlers
,