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
;
63 typedef struct BlkMigBlock
{
64 /* Only used by migration thread. */
71 BlockDriverAIOCB
*aiocb
;
73 /* Protected by block migration lock. */
75 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
78 typedef struct BlkMigState
{
79 /* Written during setup phase. Can be read without a lock. */
82 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
83 int64_t total_sector_sum
;
86 /* Protected by lock. */
87 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
91 /* Only used by migration thread. Does not need a lock. */
96 /* Lock must be taken _inside_ the iothread lock. */
100 static BlkMigState block_mig_state
;
102 static void blk_mig_lock(void)
104 qemu_mutex_lock(&block_mig_state
.lock
);
107 static void blk_mig_unlock(void)
109 qemu_mutex_unlock(&block_mig_state
.lock
);
112 /* Must run outside of the iothread lock during the bulk phase,
113 * or the VM will stall.
116 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
119 uint64_t flags
= BLK_MIG_FLAG_DEVICE_BLOCK
;
121 if (block_mig_state
.zero_blocks
&&
122 buffer_is_zero(blk
->buf
, BLOCK_SIZE
)) {
123 flags
|= BLK_MIG_FLAG_ZERO_BLOCK
;
126 /* sector number and flags */
127 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
131 len
= strlen(blk
->bmds
->bs
->device_name
);
132 qemu_put_byte(f
, len
);
133 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
135 /* if a block is zero we need to flush here since the network
136 * bandwidth is now a lot higher than the storage device bandwidth.
137 * thus if we queue zero blocks we slow down the migration */
138 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
143 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
146 int blk_mig_active(void)
148 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
151 uint64_t blk_mig_bytes_transferred(void)
153 BlkMigDevState
*bmds
;
157 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
158 sum
+= bmds
->completed_sectors
;
161 return sum
<< BDRV_SECTOR_BITS
;
164 uint64_t blk_mig_bytes_remaining(void)
166 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
169 uint64_t blk_mig_bytes_total(void)
171 BlkMigDevState
*bmds
;
174 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
175 sum
+= bmds
->total_sectors
;
177 return sum
<< BDRV_SECTOR_BITS
;
181 /* Called with migration lock held. */
183 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
185 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
187 if ((sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bmds
->bs
)) {
188 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
189 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
195 /* Called with migration lock held. */
197 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
198 int nb_sectors
, int set
)
201 unsigned long val
, idx
, bit
;
203 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
204 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
206 for (; start
<= end
; start
++) {
207 idx
= start
/ (sizeof(unsigned long) * 8);
208 bit
= start
% (sizeof(unsigned long) * 8);
209 val
= bmds
->aio_bitmap
[idx
];
213 val
&= ~(1UL << bit
);
215 bmds
->aio_bitmap
[idx
] = val
;
219 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
221 BlockDriverState
*bs
= bmds
->bs
;
224 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
225 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
226 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
228 bmds
->aio_bitmap
= g_malloc0(bitmap_size
);
231 /* Never hold migration lock when yielding to the main loop! */
233 static void blk_mig_read_cb(void *opaque
, int ret
)
235 BlkMigBlock
*blk
= opaque
;
240 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
241 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
243 block_mig_state
.submitted
--;
244 block_mig_state
.read_done
++;
245 assert(block_mig_state
.submitted
>= 0);
249 /* Called with no lock taken. */
251 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
253 int64_t total_sectors
= bmds
->total_sectors
;
254 int64_t cur_sector
= bmds
->cur_sector
;
255 BlockDriverState
*bs
= bmds
->bs
;
259 if (bmds
->shared_base
) {
260 qemu_mutex_lock_iothread();
261 while (cur_sector
< total_sectors
&&
262 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
264 cur_sector
+= nr_sectors
;
266 qemu_mutex_unlock_iothread();
269 if (cur_sector
>= total_sectors
) {
270 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
274 bmds
->completed_sectors
= cur_sector
;
276 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
278 /* we are going to transfer a full block even if it is not allocated */
279 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
281 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
282 nr_sectors
= total_sectors
- cur_sector
;
285 blk
= g_malloc(sizeof(BlkMigBlock
));
286 blk
->buf
= g_malloc(BLOCK_SIZE
);
288 blk
->sector
= cur_sector
;
289 blk
->nr_sectors
= nr_sectors
;
291 blk
->iov
.iov_base
= blk
->buf
;
292 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
293 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
296 block_mig_state
.submitted
++;
299 qemu_mutex_lock_iothread();
300 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
301 nr_sectors
, blk_mig_read_cb
, blk
);
303 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
304 qemu_mutex_unlock_iothread();
306 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
307 return (bmds
->cur_sector
>= total_sectors
);
310 /* Called with iothread lock taken. */
312 static void set_dirty_tracking(int enable
)
314 BlkMigDevState
*bmds
;
316 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
317 bdrv_set_dirty_tracking(bmds
->bs
, enable
? BLOCK_SIZE
: 0);
321 static void init_blk_migration_it(void *opaque
, BlockDriverState
*bs
)
323 BlkMigDevState
*bmds
;
326 if (!bdrv_is_read_only(bs
)) {
327 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
332 bmds
= g_malloc0(sizeof(BlkMigDevState
));
334 bmds
->bulk_completed
= 0;
335 bmds
->total_sectors
= sectors
;
336 bmds
->completed_sectors
= 0;
337 bmds
->shared_base
= block_mig_state
.shared_base
;
338 alloc_aio_bitmap(bmds
);
339 bdrv_set_in_use(bs
, 1);
342 block_mig_state
.total_sector_sum
+= sectors
;
344 if (bmds
->shared_base
) {
345 DPRINTF("Start migration for %s with shared base image\n",
348 DPRINTF("Start full migration for %s\n", bs
->device_name
);
351 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
355 static void init_blk_migration(QEMUFile
*f
)
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 bdrv_iterate(init_blk_migration_it
, NULL
);
368 /* Called with no lock taken. */
370 static int blk_mig_save_bulked_block(QEMUFile
*f
)
372 int64_t completed_sector_sum
= 0;
373 BlkMigDevState
*bmds
;
377 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
378 if (bmds
->bulk_completed
== 0) {
379 if (mig_save_device_bulk(f
, bmds
) == 1) {
380 /* completed bulk section for this device */
381 bmds
->bulk_completed
= 1;
383 completed_sector_sum
+= bmds
->completed_sectors
;
387 completed_sector_sum
+= bmds
->completed_sectors
;
391 if (block_mig_state
.total_sector_sum
!= 0) {
392 progress
= completed_sector_sum
* 100 /
393 block_mig_state
.total_sector_sum
;
397 if (progress
!= block_mig_state
.prev_progress
) {
398 block_mig_state
.prev_progress
= progress
;
399 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
400 | BLK_MIG_FLAG_PROGRESS
);
401 DPRINTF("Completed %d %%\r", progress
);
407 static void blk_mig_reset_dirty_cursor(void)
409 BlkMigDevState
*bmds
;
411 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
416 /* Called with iothread lock taken. */
418 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
422 int64_t total_sectors
= bmds
->total_sectors
;
427 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
429 if (bmds_aio_inflight(bmds
, sector
)) {
435 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
437 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
438 nr_sectors
= total_sectors
- sector
;
440 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
442 blk
= g_malloc(sizeof(BlkMigBlock
));
443 blk
->buf
= g_malloc(BLOCK_SIZE
);
445 blk
->sector
= sector
;
446 blk
->nr_sectors
= nr_sectors
;
449 blk
->iov
.iov_base
= blk
->buf
;
450 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
451 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
453 blk
->aiocb
= bdrv_aio_readv(bmds
->bs
, sector
, &blk
->qiov
,
454 nr_sectors
, blk_mig_read_cb
, blk
);
457 block_mig_state
.submitted
++;
458 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
461 ret
= bdrv_read(bmds
->bs
, sector
, blk
->buf
, nr_sectors
);
471 bdrv_reset_dirty(bmds
->bs
, sector
, nr_sectors
);
474 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
475 bmds
->cur_dirty
= sector
;
478 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
481 DPRINTF("Error reading sector %" PRId64
"\n", sector
);
487 /* Called with iothread lock taken.
490 * 0: too much data for max_downtime
491 * 1: few enough data for max_downtime
493 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
495 BlkMigDevState
*bmds
;
498 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
499 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
508 /* Called with no locks taken. */
510 static int flush_blks(QEMUFile
*f
)
515 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
516 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
517 block_mig_state
.transferred
);
520 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
521 if (qemu_file_rate_limit(f
)) {
529 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
537 block_mig_state
.read_done
--;
538 block_mig_state
.transferred
++;
539 assert(block_mig_state
.read_done
>= 0);
543 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
544 block_mig_state
.submitted
, block_mig_state
.read_done
,
545 block_mig_state
.transferred
);
549 /* Called with iothread lock taken. */
551 static int64_t get_remaining_dirty(void)
553 BlkMigDevState
*bmds
;
556 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
557 dirty
+= bdrv_get_dirty_count(bmds
->bs
);
560 return dirty
<< BDRV_SECTOR_BITS
;
563 /* Called with iothread lock taken. */
565 static void blk_mig_cleanup(void)
567 BlkMigDevState
*bmds
;
572 set_dirty_tracking(0);
575 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
576 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
577 bdrv_set_in_use(bmds
->bs
, 0);
578 bdrv_unref(bmds
->bs
);
579 g_free(bmds
->aio_bitmap
);
583 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
584 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
591 static void block_migration_cancel(void *opaque
)
596 static int block_save_setup(QEMUFile
*f
, void *opaque
)
600 DPRINTF("Enter save live setup submitted %d transferred %d\n",
601 block_mig_state
.submitted
, block_mig_state
.transferred
);
603 qemu_mutex_lock_iothread();
604 init_blk_migration(f
);
606 /* start track dirty blocks */
607 set_dirty_tracking(1);
608 qemu_mutex_unlock_iothread();
611 blk_mig_reset_dirty_cursor();
612 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
617 static int block_save_iterate(QEMUFile
*f
, void *opaque
)
620 int64_t last_ftell
= qemu_ftell(f
);
622 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
623 block_mig_state
.submitted
, block_mig_state
.transferred
);
630 blk_mig_reset_dirty_cursor();
632 /* control the rate of transfer */
634 while ((block_mig_state
.submitted
+
635 block_mig_state
.read_done
) * BLOCK_SIZE
<
636 qemu_file_get_rate_limit(f
)) {
638 if (block_mig_state
.bulk_completed
== 0) {
639 /* first finish the bulk phase */
640 if (blk_mig_save_bulked_block(f
) == 0) {
641 /* finished saving bulk on all devices */
642 block_mig_state
.bulk_completed
= 1;
646 /* Always called with iothread lock taken for
647 * simplicity, block_save_complete also calls it.
649 qemu_mutex_lock_iothread();
650 ret
= blk_mig_save_dirty_block(f
, 1);
651 qemu_mutex_unlock_iothread();
658 /* no more dirty blocks */
669 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
670 return qemu_ftell(f
) - last_ftell
;
673 /* Called with iothread lock taken. */
675 static int block_save_complete(QEMUFile
*f
, void *opaque
)
679 DPRINTF("Enter save live complete submitted %d transferred %d\n",
680 block_mig_state
.submitted
, block_mig_state
.transferred
);
687 blk_mig_reset_dirty_cursor();
689 /* we know for sure that save bulk is completed and
690 all async read completed */
692 assert(block_mig_state
.submitted
== 0);
696 ret
= blk_mig_save_dirty_block(f
, 0);
702 /* report completion */
703 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
705 DPRINTF("Block migration completed\n");
707 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
713 static uint64_t block_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
)
715 /* Estimate pending number of bytes to send */
718 qemu_mutex_lock_iothread();
720 pending
= get_remaining_dirty() +
721 block_mig_state
.submitted
* BLOCK_SIZE
+
722 block_mig_state
.read_done
* BLOCK_SIZE
;
724 /* Report at least one block pending during bulk phase */
725 if (pending
== 0 && !block_mig_state
.bulk_completed
) {
726 pending
= BLOCK_SIZE
;
729 qemu_mutex_unlock_iothread();
731 DPRINTF("Enter save live pending %" PRIu64
"\n", pending
);
735 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
737 static int banner_printed
;
739 char device_name
[256];
741 BlockDriverState
*bs
, *bs_prev
= NULL
;
743 int64_t total_sectors
= 0;
748 addr
= qemu_get_be64(f
);
750 flags
= addr
& ~BDRV_SECTOR_MASK
;
751 addr
>>= BDRV_SECTOR_BITS
;
753 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
754 /* get device name */
755 len
= qemu_get_byte(f
);
756 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
757 device_name
[len
] = '\0';
759 bs
= bdrv_find(device_name
);
761 fprintf(stderr
, "Error unknown block device %s\n",
768 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
769 if (total_sectors
<= 0) {
770 error_report("Error getting length of block device %s",
776 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
777 nr_sectors
= total_sectors
- addr
;
779 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
782 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
783 ret
= bdrv_write_zeroes(bs
, addr
, nr_sectors
);
785 buf
= g_malloc(BLOCK_SIZE
);
786 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
787 ret
= bdrv_write(bs
, addr
, buf
, nr_sectors
);
794 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
795 if (!banner_printed
) {
796 printf("Receiving block device images\n");
799 printf("Completed %d %%%c", (int)addr
,
800 (addr
== 100) ? '\n' : '\r');
802 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
803 fprintf(stderr
, "Unknown block migration flags: %#x\n", flags
);
806 ret
= qemu_file_get_error(f
);
810 } while (!(flags
& BLK_MIG_FLAG_EOS
));
815 static void block_set_params(const MigrationParams
*params
, void *opaque
)
817 block_mig_state
.blk_enable
= params
->blk
;
818 block_mig_state
.shared_base
= params
->shared
;
820 /* shared base means that blk_enable = 1 */
821 block_mig_state
.blk_enable
|= params
->shared
;
824 static bool block_is_active(void *opaque
)
826 return block_mig_state
.blk_enable
== 1;
829 SaveVMHandlers savevm_block_handlers
= {
830 .set_params
= block_set_params
,
831 .save_live_setup
= block_save_setup
,
832 .save_live_iterate
= block_save_iterate
,
833 .save_live_complete
= block_save_complete
,
834 .save_live_pending
= block_save_pending
,
835 .load_state
= block_load
,
836 .cancel
= block_migration_cancel
,
837 .is_active
= block_is_active
,
840 void blk_mig_init(void)
842 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
843 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
844 qemu_mutex_init(&block_mig_state
.lock
);
846 register_savevm_live(NULL
, "block", 0, 1, &savevm_block_handlers
,