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
33 #define MAX_IS_ALLOCATED_SEARCH 65536
35 //#define DEBUG_BLK_MIGRATION
37 #ifdef DEBUG_BLK_MIGRATION
38 #define DPRINTF(fmt, ...) \
39 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
41 #define DPRINTF(fmt, ...) \
45 typedef struct BlkMigDevState
{
51 int64_t completed_sectors
;
52 int64_t total_sectors
;
54 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
55 unsigned long *aio_bitmap
;
58 typedef struct BlkMigBlock
{
65 BlockDriverAIOCB
*aiocb
;
67 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
70 typedef struct BlkMigState
{
73 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
74 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
78 int64_t total_sector_sum
;
81 long double prev_time_offset
;
84 static BlkMigState block_mig_state
;
86 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
90 /* sector number and flags */
91 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
92 | BLK_MIG_FLAG_DEVICE_BLOCK
);
95 len
= strlen(blk
->bmds
->bs
->device_name
);
96 qemu_put_byte(f
, len
);
97 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
99 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
102 int blk_mig_active(void)
104 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
107 uint64_t blk_mig_bytes_transferred(void)
109 BlkMigDevState
*bmds
;
112 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
113 sum
+= bmds
->completed_sectors
;
115 return sum
<< BDRV_SECTOR_BITS
;
118 uint64_t blk_mig_bytes_remaining(void)
120 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
123 uint64_t blk_mig_bytes_total(void)
125 BlkMigDevState
*bmds
;
128 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
129 sum
+= bmds
->total_sectors
;
131 return sum
<< BDRV_SECTOR_BITS
;
134 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
136 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
138 if ((sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bmds
->bs
)) {
139 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
140 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
146 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
147 int nb_sectors
, int set
)
150 unsigned long val
, idx
, bit
;
152 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
153 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
155 for (; start
<= end
; start
++) {
156 idx
= start
/ (sizeof(unsigned long) * 8);
157 bit
= start
% (sizeof(unsigned long) * 8);
158 val
= bmds
->aio_bitmap
[idx
];
162 val
&= ~(1UL << bit
);
164 bmds
->aio_bitmap
[idx
] = val
;
168 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
170 BlockDriverState
*bs
= bmds
->bs
;
173 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
174 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
175 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
177 bmds
->aio_bitmap
= g_malloc0(bitmap_size
);
180 static void blk_mig_read_cb(void *opaque
, int ret
)
182 long double curr_time
= qemu_get_clock_ns(rt_clock
);
183 BlkMigBlock
*blk
= opaque
;
187 block_mig_state
.prev_time_offset
= curr_time
;
189 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
190 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
192 block_mig_state
.submitted
--;
193 block_mig_state
.read_done
++;
194 assert(block_mig_state
.submitted
>= 0);
197 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
199 int64_t total_sectors
= bmds
->total_sectors
;
200 int64_t cur_sector
= bmds
->cur_sector
;
201 BlockDriverState
*bs
= bmds
->bs
;
205 if (bmds
->shared_base
) {
206 while (cur_sector
< total_sectors
&&
207 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
209 cur_sector
+= nr_sectors
;
213 if (cur_sector
>= total_sectors
) {
214 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
218 bmds
->completed_sectors
= cur_sector
;
220 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
222 /* we are going to transfer a full block even if it is not allocated */
223 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
225 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
226 nr_sectors
= total_sectors
- cur_sector
;
229 blk
= g_malloc(sizeof(BlkMigBlock
));
230 blk
->buf
= g_malloc(BLOCK_SIZE
);
232 blk
->sector
= cur_sector
;
233 blk
->nr_sectors
= nr_sectors
;
235 blk
->iov
.iov_base
= blk
->buf
;
236 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
237 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
239 if (block_mig_state
.submitted
== 0) {
240 block_mig_state
.prev_time_offset
= qemu_get_clock_ns(rt_clock
);
243 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
244 nr_sectors
, blk_mig_read_cb
, blk
);
245 block_mig_state
.submitted
++;
247 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
248 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
250 return (bmds
->cur_sector
>= total_sectors
);
253 static void set_dirty_tracking(int enable
)
255 BlkMigDevState
*bmds
;
257 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
258 bdrv_set_dirty_tracking(bmds
->bs
, enable
? BLOCK_SIZE
: 0);
262 static void init_blk_migration_it(void *opaque
, BlockDriverState
*bs
)
264 BlkMigDevState
*bmds
;
267 if (!bdrv_is_read_only(bs
)) {
268 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
273 bmds
= g_malloc0(sizeof(BlkMigDevState
));
275 bmds
->bulk_completed
= 0;
276 bmds
->total_sectors
= sectors
;
277 bmds
->completed_sectors
= 0;
278 bmds
->shared_base
= block_mig_state
.shared_base
;
279 alloc_aio_bitmap(bmds
);
280 drive_get_ref(drive_get_by_blockdev(bs
));
281 bdrv_set_in_use(bs
, 1);
283 block_mig_state
.total_sector_sum
+= sectors
;
285 if (bmds
->shared_base
) {
286 DPRINTF("Start migration for %s with shared base image\n",
289 DPRINTF("Start full migration for %s\n", bs
->device_name
);
292 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
296 static void init_blk_migration(QEMUFile
*f
)
298 block_mig_state
.submitted
= 0;
299 block_mig_state
.read_done
= 0;
300 block_mig_state
.transferred
= 0;
301 block_mig_state
.total_sector_sum
= 0;
302 block_mig_state
.prev_progress
= -1;
303 block_mig_state
.bulk_completed
= 0;
305 bdrv_iterate(init_blk_migration_it
, NULL
);
308 static int blk_mig_save_bulked_block(QEMUFile
*f
)
310 int64_t completed_sector_sum
= 0;
311 BlkMigDevState
*bmds
;
315 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
316 if (bmds
->bulk_completed
== 0) {
317 if (mig_save_device_bulk(f
, bmds
) == 1) {
318 /* completed bulk section for this device */
319 bmds
->bulk_completed
= 1;
321 completed_sector_sum
+= bmds
->completed_sectors
;
325 completed_sector_sum
+= bmds
->completed_sectors
;
329 if (block_mig_state
.total_sector_sum
!= 0) {
330 progress
= completed_sector_sum
* 100 /
331 block_mig_state
.total_sector_sum
;
335 if (progress
!= block_mig_state
.prev_progress
) {
336 block_mig_state
.prev_progress
= progress
;
337 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
338 | BLK_MIG_FLAG_PROGRESS
);
339 DPRINTF("Completed %d %%\r", progress
);
345 static void blk_mig_reset_dirty_cursor(void)
347 BlkMigDevState
*bmds
;
349 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
354 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
358 int64_t total_sectors
= bmds
->total_sectors
;
363 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
364 if (bmds_aio_inflight(bmds
, sector
)) {
367 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
369 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
370 nr_sectors
= total_sectors
- sector
;
372 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
374 blk
= g_malloc(sizeof(BlkMigBlock
));
375 blk
->buf
= g_malloc(BLOCK_SIZE
);
377 blk
->sector
= sector
;
378 blk
->nr_sectors
= nr_sectors
;
381 blk
->iov
.iov_base
= blk
->buf
;
382 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
383 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
385 if (block_mig_state
.submitted
== 0) {
386 block_mig_state
.prev_time_offset
= qemu_get_clock_ns(rt_clock
);
389 blk
->aiocb
= bdrv_aio_readv(bmds
->bs
, sector
, &blk
->qiov
,
390 nr_sectors
, blk_mig_read_cb
, blk
);
391 block_mig_state
.submitted
++;
392 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
394 ret
= bdrv_read(bmds
->bs
, sector
, blk
->buf
, nr_sectors
);
404 bdrv_reset_dirty(bmds
->bs
, sector
, nr_sectors
);
407 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
408 bmds
->cur_dirty
= sector
;
411 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
414 DPRINTF("Error reading sector %" PRId64
"\n", sector
);
421 * 0: too much data for max_downtime
422 * 1: few enough data for max_downtime
424 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
426 BlkMigDevState
*bmds
;
429 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
430 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
439 static int flush_blks(QEMUFile
*f
)
444 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
445 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
446 block_mig_state
.transferred
);
448 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
449 if (qemu_file_rate_limit(f
)) {
458 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
462 block_mig_state
.read_done
--;
463 block_mig_state
.transferred
++;
464 assert(block_mig_state
.read_done
>= 0);
467 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
468 block_mig_state
.submitted
, block_mig_state
.read_done
,
469 block_mig_state
.transferred
);
473 static int64_t get_remaining_dirty(void)
475 BlkMigDevState
*bmds
;
478 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
479 dirty
+= bdrv_get_dirty_count(bmds
->bs
);
482 return dirty
<< BDRV_SECTOR_BITS
;
485 static void blk_mig_cleanup(void)
487 BlkMigDevState
*bmds
;
492 set_dirty_tracking(0);
494 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
495 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
496 bdrv_set_in_use(bmds
->bs
, 0);
497 drive_put_ref(drive_get_by_blockdev(bmds
->bs
));
498 g_free(bmds
->aio_bitmap
);
502 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
503 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
509 static void block_migration_cancel(void *opaque
)
514 static int block_save_setup(QEMUFile
*f
, void *opaque
)
518 DPRINTF("Enter save live setup submitted %d transferred %d\n",
519 block_mig_state
.submitted
, block_mig_state
.transferred
);
521 init_blk_migration(f
);
523 /* start track dirty blocks */
524 set_dirty_tracking(1);
532 blk_mig_reset_dirty_cursor();
534 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
539 static int block_save_iterate(QEMUFile
*f
, void *opaque
)
542 int64_t last_ftell
= qemu_ftell(f
);
544 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
545 block_mig_state
.submitted
, block_mig_state
.transferred
);
553 blk_mig_reset_dirty_cursor();
555 /* control the rate of transfer */
556 while ((block_mig_state
.submitted
+
557 block_mig_state
.read_done
) * BLOCK_SIZE
<
558 qemu_file_get_rate_limit(f
)) {
559 if (block_mig_state
.bulk_completed
== 0) {
560 /* first finish the bulk phase */
561 if (blk_mig_save_bulked_block(f
) == 0) {
562 /* finished saving bulk on all devices */
563 block_mig_state
.bulk_completed
= 1;
566 ret
= blk_mig_save_dirty_block(f
, 1);
568 /* no more dirty blocks */
584 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
586 return qemu_ftell(f
) - last_ftell
;
589 static int block_save_complete(QEMUFile
*f
, void *opaque
)
593 DPRINTF("Enter save live complete submitted %d transferred %d\n",
594 block_mig_state
.submitted
, block_mig_state
.transferred
);
602 blk_mig_reset_dirty_cursor();
604 /* we know for sure that save bulk is completed and
605 all async read completed */
606 assert(block_mig_state
.submitted
== 0);
609 ret
= blk_mig_save_dirty_block(f
, 0);
616 /* report completion */
617 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
619 DPRINTF("Block migration completed\n");
621 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
626 static uint64_t block_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
)
628 /* Estimate pending number of bytes to send */
629 uint64_t pending
= get_remaining_dirty() +
630 block_mig_state
.submitted
* BLOCK_SIZE
+
631 block_mig_state
.read_done
* BLOCK_SIZE
;
633 /* Report at least one block pending during bulk phase */
634 if (pending
== 0 && !block_mig_state
.bulk_completed
) {
635 pending
= BLOCK_SIZE
;
638 DPRINTF("Enter save live pending %" PRIu64
"\n", pending
);
642 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
644 static int banner_printed
;
646 char device_name
[256];
648 BlockDriverState
*bs
, *bs_prev
= NULL
;
650 int64_t total_sectors
= 0;
655 addr
= qemu_get_be64(f
);
657 flags
= addr
& ~BDRV_SECTOR_MASK
;
658 addr
>>= BDRV_SECTOR_BITS
;
660 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
661 /* get device name */
662 len
= qemu_get_byte(f
);
663 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
664 device_name
[len
] = '\0';
666 bs
= bdrv_find(device_name
);
668 fprintf(stderr
, "Error unknown block device %s\n",
675 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
676 if (total_sectors
<= 0) {
677 error_report("Error getting length of block device %s",
683 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
684 nr_sectors
= total_sectors
- addr
;
686 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
689 buf
= g_malloc(BLOCK_SIZE
);
691 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
692 ret
= bdrv_write(bs
, addr
, buf
, nr_sectors
);
698 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
699 if (!banner_printed
) {
700 printf("Receiving block device images\n");
703 printf("Completed %d %%%c", (int)addr
,
704 (addr
== 100) ? '\n' : '\r');
706 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
707 fprintf(stderr
, "Unknown block migration flags: %#x\n", flags
);
710 ret
= qemu_file_get_error(f
);
714 } while (!(flags
& BLK_MIG_FLAG_EOS
));
719 static void block_set_params(const MigrationParams
*params
, void *opaque
)
721 block_mig_state
.blk_enable
= params
->blk
;
722 block_mig_state
.shared_base
= params
->shared
;
724 /* shared base means that blk_enable = 1 */
725 block_mig_state
.blk_enable
|= params
->shared
;
728 static bool block_is_active(void *opaque
)
730 return block_mig_state
.blk_enable
== 1;
733 SaveVMHandlers savevm_block_handlers
= {
734 .set_params
= block_set_params
,
735 .save_live_setup
= block_save_setup
,
736 .save_live_iterate
= block_save_iterate
,
737 .save_live_complete
= block_save_complete
,
738 .save_live_pending
= block_save_pending
,
739 .load_state
= block_load
,
740 .cancel
= block_migration_cancel
,
741 .is_active
= block_is_active
,
744 void blk_mig_init(void)
746 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
747 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
749 register_savevm_live(NULL
, "block", 0, 1, &savevm_block_handlers
,