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
;
53 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
54 unsigned long *aio_bitmap
;
57 typedef struct BlkMigBlock
{
64 BlockDriverAIOCB
*aiocb
;
66 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
69 typedef struct BlkMigState
{
72 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
73 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
77 int64_t total_sector_sum
;
82 static BlkMigState block_mig_state
;
84 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
88 /* sector number and flags */
89 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
90 | BLK_MIG_FLAG_DEVICE_BLOCK
);
93 len
= strlen(blk
->bmds
->bs
->device_name
);
94 qemu_put_byte(f
, len
);
95 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
97 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
100 int blk_mig_active(void)
102 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
105 uint64_t blk_mig_bytes_transferred(void)
107 BlkMigDevState
*bmds
;
110 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
111 sum
+= bmds
->completed_sectors
;
113 return sum
<< BDRV_SECTOR_BITS
;
116 uint64_t blk_mig_bytes_remaining(void)
118 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
121 uint64_t blk_mig_bytes_total(void)
123 BlkMigDevState
*bmds
;
126 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
127 sum
+= bmds
->total_sectors
;
129 return sum
<< BDRV_SECTOR_BITS
;
132 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
134 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
136 if ((sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bmds
->bs
)) {
137 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
138 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
144 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
145 int nb_sectors
, int set
)
148 unsigned long val
, idx
, bit
;
150 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
151 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
153 for (; start
<= end
; start
++) {
154 idx
= start
/ (sizeof(unsigned long) * 8);
155 bit
= start
% (sizeof(unsigned long) * 8);
156 val
= bmds
->aio_bitmap
[idx
];
160 val
&= ~(1UL << bit
);
162 bmds
->aio_bitmap
[idx
] = val
;
166 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
168 BlockDriverState
*bs
= bmds
->bs
;
171 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
172 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
173 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
175 bmds
->aio_bitmap
= g_malloc0(bitmap_size
);
178 static void blk_mig_read_cb(void *opaque
, int ret
)
180 BlkMigBlock
*blk
= opaque
;
184 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
185 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
187 block_mig_state
.submitted
--;
188 block_mig_state
.read_done
++;
189 assert(block_mig_state
.submitted
>= 0);
192 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
194 int64_t total_sectors
= bmds
->total_sectors
;
195 int64_t cur_sector
= bmds
->cur_sector
;
196 BlockDriverState
*bs
= bmds
->bs
;
200 if (bmds
->shared_base
) {
201 while (cur_sector
< total_sectors
&&
202 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
204 cur_sector
+= nr_sectors
;
208 if (cur_sector
>= total_sectors
) {
209 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
213 bmds
->completed_sectors
= cur_sector
;
215 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
217 /* we are going to transfer a full block even if it is not allocated */
218 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
220 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
221 nr_sectors
= total_sectors
- cur_sector
;
224 blk
= g_malloc(sizeof(BlkMigBlock
));
225 blk
->buf
= g_malloc(BLOCK_SIZE
);
227 blk
->sector
= cur_sector
;
228 blk
->nr_sectors
= nr_sectors
;
230 blk
->iov
.iov_base
= blk
->buf
;
231 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
232 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
234 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
235 nr_sectors
, blk_mig_read_cb
, blk
);
236 block_mig_state
.submitted
++;
238 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
239 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
241 return (bmds
->cur_sector
>= total_sectors
);
244 static void set_dirty_tracking(int enable
)
246 BlkMigDevState
*bmds
;
248 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
249 bdrv_set_dirty_tracking(bmds
->bs
, enable
? BLOCK_SIZE
: 0);
253 static void init_blk_migration_it(void *opaque
, BlockDriverState
*bs
)
255 BlkMigDevState
*bmds
;
258 if (!bdrv_is_read_only(bs
)) {
259 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
264 bmds
= g_malloc0(sizeof(BlkMigDevState
));
266 bmds
->bulk_completed
= 0;
267 bmds
->total_sectors
= sectors
;
268 bmds
->completed_sectors
= 0;
269 bmds
->shared_base
= block_mig_state
.shared_base
;
270 alloc_aio_bitmap(bmds
);
271 drive_get_ref(drive_get_by_blockdev(bs
));
272 bdrv_set_in_use(bs
, 1);
274 block_mig_state
.total_sector_sum
+= sectors
;
276 if (bmds
->shared_base
) {
277 DPRINTF("Start migration for %s with shared base image\n",
280 DPRINTF("Start full migration for %s\n", bs
->device_name
);
283 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
287 static void init_blk_migration(QEMUFile
*f
)
289 block_mig_state
.submitted
= 0;
290 block_mig_state
.read_done
= 0;
291 block_mig_state
.transferred
= 0;
292 block_mig_state
.total_sector_sum
= 0;
293 block_mig_state
.prev_progress
= -1;
294 block_mig_state
.bulk_completed
= 0;
296 bdrv_iterate(init_blk_migration_it
, NULL
);
299 static int blk_mig_save_bulked_block(QEMUFile
*f
)
301 int64_t completed_sector_sum
= 0;
302 BlkMigDevState
*bmds
;
306 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
307 if (bmds
->bulk_completed
== 0) {
308 if (mig_save_device_bulk(f
, bmds
) == 1) {
309 /* completed bulk section for this device */
310 bmds
->bulk_completed
= 1;
312 completed_sector_sum
+= bmds
->completed_sectors
;
316 completed_sector_sum
+= bmds
->completed_sectors
;
320 if (block_mig_state
.total_sector_sum
!= 0) {
321 progress
= completed_sector_sum
* 100 /
322 block_mig_state
.total_sector_sum
;
326 if (progress
!= block_mig_state
.prev_progress
) {
327 block_mig_state
.prev_progress
= progress
;
328 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
329 | BLK_MIG_FLAG_PROGRESS
);
330 DPRINTF("Completed %d %%\r", progress
);
336 static void blk_mig_reset_dirty_cursor(void)
338 BlkMigDevState
*bmds
;
340 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
345 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
349 int64_t total_sectors
= bmds
->total_sectors
;
354 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
355 if (bmds_aio_inflight(bmds
, sector
)) {
358 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
360 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
361 nr_sectors
= total_sectors
- sector
;
363 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
365 blk
= g_malloc(sizeof(BlkMigBlock
));
366 blk
->buf
= g_malloc(BLOCK_SIZE
);
368 blk
->sector
= sector
;
369 blk
->nr_sectors
= nr_sectors
;
372 blk
->iov
.iov_base
= blk
->buf
;
373 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
374 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
376 blk
->aiocb
= bdrv_aio_readv(bmds
->bs
, sector
, &blk
->qiov
,
377 nr_sectors
, blk_mig_read_cb
, blk
);
378 block_mig_state
.submitted
++;
379 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
381 ret
= bdrv_read(bmds
->bs
, sector
, blk
->buf
, nr_sectors
);
391 bdrv_reset_dirty(bmds
->bs
, sector
, nr_sectors
);
394 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
395 bmds
->cur_dirty
= sector
;
398 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
401 DPRINTF("Error reading sector %" PRId64
"\n", sector
);
408 * 0: too much data for max_downtime
409 * 1: few enough data for max_downtime
411 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
413 BlkMigDevState
*bmds
;
416 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
417 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
426 static int flush_blks(QEMUFile
*f
)
431 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
432 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
433 block_mig_state
.transferred
);
435 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
436 if (qemu_file_rate_limit(f
)) {
445 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
449 block_mig_state
.read_done
--;
450 block_mig_state
.transferred
++;
451 assert(block_mig_state
.read_done
>= 0);
454 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
455 block_mig_state
.submitted
, block_mig_state
.read_done
,
456 block_mig_state
.transferred
);
460 static int64_t get_remaining_dirty(void)
462 BlkMigDevState
*bmds
;
465 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
466 dirty
+= bdrv_get_dirty_count(bmds
->bs
);
469 return dirty
<< BDRV_SECTOR_BITS
;
472 static void blk_mig_cleanup(void)
474 BlkMigDevState
*bmds
;
479 set_dirty_tracking(0);
481 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
482 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
483 bdrv_set_in_use(bmds
->bs
, 0);
484 drive_put_ref(drive_get_by_blockdev(bmds
->bs
));
485 g_free(bmds
->aio_bitmap
);
489 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
490 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
496 static void block_migration_cancel(void *opaque
)
501 static int block_save_setup(QEMUFile
*f
, void *opaque
)
505 DPRINTF("Enter save live setup submitted %d transferred %d\n",
506 block_mig_state
.submitted
, block_mig_state
.transferred
);
508 init_blk_migration(f
);
510 /* start track dirty blocks */
511 set_dirty_tracking(1);
514 blk_mig_reset_dirty_cursor();
515 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
520 static int block_save_iterate(QEMUFile
*f
, void *opaque
)
523 int64_t last_ftell
= qemu_ftell(f
);
525 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
526 block_mig_state
.submitted
, block_mig_state
.transferred
);
533 blk_mig_reset_dirty_cursor();
535 /* control the rate of transfer */
536 while ((block_mig_state
.submitted
+
537 block_mig_state
.read_done
) * BLOCK_SIZE
<
538 qemu_file_get_rate_limit(f
)) {
539 if (block_mig_state
.bulk_completed
== 0) {
540 /* first finish the bulk phase */
541 if (blk_mig_save_bulked_block(f
) == 0) {
542 /* finished saving bulk on all devices */
543 block_mig_state
.bulk_completed
= 1;
546 ret
= blk_mig_save_dirty_block(f
, 1);
551 /* no more dirty blocks */
562 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
564 return qemu_ftell(f
) - last_ftell
;
567 static int block_save_complete(QEMUFile
*f
, void *opaque
)
571 DPRINTF("Enter save live complete submitted %d transferred %d\n",
572 block_mig_state
.submitted
, block_mig_state
.transferred
);
579 blk_mig_reset_dirty_cursor();
581 /* we know for sure that save bulk is completed and
582 all async read completed */
583 assert(block_mig_state
.submitted
== 0);
586 ret
= blk_mig_save_dirty_block(f
, 0);
592 /* report completion */
593 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
595 DPRINTF("Block migration completed\n");
597 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
603 static uint64_t block_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
)
605 /* Estimate pending number of bytes to send */
606 uint64_t pending
= get_remaining_dirty() +
607 block_mig_state
.submitted
* BLOCK_SIZE
+
608 block_mig_state
.read_done
* BLOCK_SIZE
;
610 /* Report at least one block pending during bulk phase */
611 if (pending
== 0 && !block_mig_state
.bulk_completed
) {
612 pending
= BLOCK_SIZE
;
615 DPRINTF("Enter save live pending %" PRIu64
"\n", pending
);
619 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
621 static int banner_printed
;
623 char device_name
[256];
625 BlockDriverState
*bs
, *bs_prev
= NULL
;
627 int64_t total_sectors
= 0;
632 addr
= qemu_get_be64(f
);
634 flags
= addr
& ~BDRV_SECTOR_MASK
;
635 addr
>>= BDRV_SECTOR_BITS
;
637 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
638 /* get device name */
639 len
= qemu_get_byte(f
);
640 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
641 device_name
[len
] = '\0';
643 bs
= bdrv_find(device_name
);
645 fprintf(stderr
, "Error unknown block device %s\n",
652 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
653 if (total_sectors
<= 0) {
654 error_report("Error getting length of block device %s",
660 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
661 nr_sectors
= total_sectors
- addr
;
663 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
666 buf
= g_malloc(BLOCK_SIZE
);
668 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
669 ret
= bdrv_write(bs
, addr
, buf
, nr_sectors
);
675 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
676 if (!banner_printed
) {
677 printf("Receiving block device images\n");
680 printf("Completed %d %%%c", (int)addr
,
681 (addr
== 100) ? '\n' : '\r');
683 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
684 fprintf(stderr
, "Unknown block migration flags: %#x\n", flags
);
687 ret
= qemu_file_get_error(f
);
691 } while (!(flags
& BLK_MIG_FLAG_EOS
));
696 static void block_set_params(const MigrationParams
*params
, void *opaque
)
698 block_mig_state
.blk_enable
= params
->blk
;
699 block_mig_state
.shared_base
= params
->shared
;
701 /* shared base means that blk_enable = 1 */
702 block_mig_state
.blk_enable
|= params
->shared
;
705 static bool block_is_active(void *opaque
)
707 return block_mig_state
.blk_enable
== 1;
710 SaveVMHandlers savevm_block_handlers
= {
711 .set_params
= block_set_params
,
712 .save_live_setup
= block_save_setup
,
713 .save_live_iterate
= block_save_iterate
,
714 .save_live_complete
= block_save_complete
,
715 .save_live_pending
= block_save_pending
,
716 .load_state
= block_load
,
717 .cancel
= block_migration_cancel
,
718 .is_active
= block_is_active
,
721 void blk_mig_init(void)
723 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
724 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
726 register_savevm_live(NULL
, "block", 0, 1, &savevm_block_handlers
,