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
{
46 /* Written during setup phase. Can be read without a lock. */
49 int64_t total_sectors
;
50 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
52 /* Only used by migration thread. Does not need a lock. */
57 /* Protected by iothread lock. */
58 unsigned long *aio_bitmap
;
59 int64_t completed_sectors
;
62 typedef struct BlkMigBlock
{
63 /* Only used by migration thread. */
70 BlockDriverAIOCB
*aiocb
;
72 /* Protected by iothread lock. */
74 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
77 typedef struct BlkMigState
{
78 /* Written during setup phase. Can be read without a lock. */
81 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
82 int64_t total_sector_sum
;
84 /* Protected by iothread lock. */
85 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
89 /* Only used by migration thread. Does not need a lock. */
95 static BlkMigState block_mig_state
;
97 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
101 /* sector number and flags */
102 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
103 | BLK_MIG_FLAG_DEVICE_BLOCK
);
106 len
= strlen(blk
->bmds
->bs
->device_name
);
107 qemu_put_byte(f
, len
);
108 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
110 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
113 int blk_mig_active(void)
115 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
118 uint64_t blk_mig_bytes_transferred(void)
120 BlkMigDevState
*bmds
;
123 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
124 sum
+= bmds
->completed_sectors
;
126 return sum
<< BDRV_SECTOR_BITS
;
129 uint64_t blk_mig_bytes_remaining(void)
131 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
134 uint64_t blk_mig_bytes_total(void)
136 BlkMigDevState
*bmds
;
139 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
140 sum
+= bmds
->total_sectors
;
142 return sum
<< BDRV_SECTOR_BITS
;
145 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
147 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
149 if ((sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bmds
->bs
)) {
150 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
151 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
157 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
158 int nb_sectors
, int set
)
161 unsigned long val
, idx
, bit
;
163 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
164 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
166 for (; start
<= end
; start
++) {
167 idx
= start
/ (sizeof(unsigned long) * 8);
168 bit
= start
% (sizeof(unsigned long) * 8);
169 val
= bmds
->aio_bitmap
[idx
];
173 val
&= ~(1UL << bit
);
175 bmds
->aio_bitmap
[idx
] = val
;
179 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
181 BlockDriverState
*bs
= bmds
->bs
;
184 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
185 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
186 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
188 bmds
->aio_bitmap
= g_malloc0(bitmap_size
);
191 static void blk_mig_read_cb(void *opaque
, int ret
)
193 BlkMigBlock
*blk
= opaque
;
197 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
198 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
200 block_mig_state
.submitted
--;
201 block_mig_state
.read_done
++;
202 assert(block_mig_state
.submitted
>= 0);
205 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
207 int64_t total_sectors
= bmds
->total_sectors
;
208 int64_t cur_sector
= bmds
->cur_sector
;
209 BlockDriverState
*bs
= bmds
->bs
;
213 if (bmds
->shared_base
) {
214 while (cur_sector
< total_sectors
&&
215 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
217 cur_sector
+= nr_sectors
;
221 if (cur_sector
>= total_sectors
) {
222 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
226 bmds
->completed_sectors
= cur_sector
;
228 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
230 /* we are going to transfer a full block even if it is not allocated */
231 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
233 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
234 nr_sectors
= total_sectors
- cur_sector
;
237 blk
= g_malloc(sizeof(BlkMigBlock
));
238 blk
->buf
= g_malloc(BLOCK_SIZE
);
240 blk
->sector
= cur_sector
;
241 blk
->nr_sectors
= nr_sectors
;
243 blk
->iov
.iov_base
= blk
->buf
;
244 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
245 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
247 block_mig_state
.submitted
++;
249 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
250 nr_sectors
, blk_mig_read_cb
, blk
);
252 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
253 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
255 return (bmds
->cur_sector
>= total_sectors
);
258 static void set_dirty_tracking(int enable
)
260 BlkMigDevState
*bmds
;
262 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
263 bdrv_set_dirty_tracking(bmds
->bs
, enable
? BLOCK_SIZE
: 0);
267 static void init_blk_migration_it(void *opaque
, BlockDriverState
*bs
)
269 BlkMigDevState
*bmds
;
272 if (!bdrv_is_read_only(bs
)) {
273 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
278 bmds
= g_malloc0(sizeof(BlkMigDevState
));
280 bmds
->bulk_completed
= 0;
281 bmds
->total_sectors
= sectors
;
282 bmds
->completed_sectors
= 0;
283 bmds
->shared_base
= block_mig_state
.shared_base
;
284 alloc_aio_bitmap(bmds
);
285 drive_get_ref(drive_get_by_blockdev(bs
));
286 bdrv_set_in_use(bs
, 1);
288 block_mig_state
.total_sector_sum
+= sectors
;
290 if (bmds
->shared_base
) {
291 DPRINTF("Start migration for %s with shared base image\n",
294 DPRINTF("Start full migration for %s\n", bs
->device_name
);
297 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
301 static void init_blk_migration(QEMUFile
*f
)
303 block_mig_state
.submitted
= 0;
304 block_mig_state
.read_done
= 0;
305 block_mig_state
.transferred
= 0;
306 block_mig_state
.total_sector_sum
= 0;
307 block_mig_state
.prev_progress
= -1;
308 block_mig_state
.bulk_completed
= 0;
310 bdrv_iterate(init_blk_migration_it
, NULL
);
313 static int blk_mig_save_bulked_block(QEMUFile
*f
)
315 int64_t completed_sector_sum
= 0;
316 BlkMigDevState
*bmds
;
320 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
321 if (bmds
->bulk_completed
== 0) {
322 if (mig_save_device_bulk(f
, bmds
) == 1) {
323 /* completed bulk section for this device */
324 bmds
->bulk_completed
= 1;
326 completed_sector_sum
+= bmds
->completed_sectors
;
330 completed_sector_sum
+= bmds
->completed_sectors
;
334 if (block_mig_state
.total_sector_sum
!= 0) {
335 progress
= completed_sector_sum
* 100 /
336 block_mig_state
.total_sector_sum
;
340 if (progress
!= block_mig_state
.prev_progress
) {
341 block_mig_state
.prev_progress
= progress
;
342 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
343 | BLK_MIG_FLAG_PROGRESS
);
344 DPRINTF("Completed %d %%\r", progress
);
350 static void blk_mig_reset_dirty_cursor(void)
352 BlkMigDevState
*bmds
;
354 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
359 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
363 int64_t total_sectors
= bmds
->total_sectors
;
368 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
369 if (bmds_aio_inflight(bmds
, sector
)) {
372 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
374 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
375 nr_sectors
= total_sectors
- sector
;
377 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
379 blk
= g_malloc(sizeof(BlkMigBlock
));
380 blk
->buf
= g_malloc(BLOCK_SIZE
);
382 blk
->sector
= sector
;
383 blk
->nr_sectors
= nr_sectors
;
386 blk
->iov
.iov_base
= blk
->buf
;
387 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
388 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
390 blk
->aiocb
= bdrv_aio_readv(bmds
->bs
, sector
, &blk
->qiov
,
391 nr_sectors
, blk_mig_read_cb
, blk
);
392 block_mig_state
.submitted
++;
393 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
395 ret
= bdrv_read(bmds
->bs
, sector
, blk
->buf
, nr_sectors
);
405 bdrv_reset_dirty(bmds
->bs
, sector
, nr_sectors
);
408 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
409 bmds
->cur_dirty
= sector
;
412 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
415 DPRINTF("Error reading sector %" PRId64
"\n", sector
);
422 * 0: too much data for max_downtime
423 * 1: few enough data for max_downtime
425 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
427 BlkMigDevState
*bmds
;
430 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
431 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
440 static int flush_blks(QEMUFile
*f
)
445 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
446 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
447 block_mig_state
.transferred
);
449 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
450 if (qemu_file_rate_limit(f
)) {
458 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
464 block_mig_state
.read_done
--;
465 block_mig_state
.transferred
++;
466 assert(block_mig_state
.read_done
>= 0);
469 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
470 block_mig_state
.submitted
, block_mig_state
.read_done
,
471 block_mig_state
.transferred
);
475 static int64_t get_remaining_dirty(void)
477 BlkMigDevState
*bmds
;
480 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
481 dirty
+= bdrv_get_dirty_count(bmds
->bs
);
484 return dirty
<< BDRV_SECTOR_BITS
;
487 static void blk_mig_cleanup(void)
489 BlkMigDevState
*bmds
;
494 set_dirty_tracking(0);
496 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
497 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
498 bdrv_set_in_use(bmds
->bs
, 0);
499 drive_put_ref(drive_get_by_blockdev(bmds
->bs
));
500 g_free(bmds
->aio_bitmap
);
504 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
505 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
511 static void block_migration_cancel(void *opaque
)
516 static int block_save_setup(QEMUFile
*f
, void *opaque
)
520 DPRINTF("Enter save live setup submitted %d transferred %d\n",
521 block_mig_state
.submitted
, block_mig_state
.transferred
);
523 init_blk_migration(f
);
525 /* start track dirty blocks */
526 set_dirty_tracking(1);
529 blk_mig_reset_dirty_cursor();
530 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
535 static int block_save_iterate(QEMUFile
*f
, void *opaque
)
538 int64_t last_ftell
= qemu_ftell(f
);
540 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
541 block_mig_state
.submitted
, block_mig_state
.transferred
);
548 blk_mig_reset_dirty_cursor();
550 /* control the rate of transfer */
551 while ((block_mig_state
.submitted
+
552 block_mig_state
.read_done
) * BLOCK_SIZE
<
553 qemu_file_get_rate_limit(f
)) {
554 if (block_mig_state
.bulk_completed
== 0) {
555 /* first finish the bulk phase */
556 if (blk_mig_save_bulked_block(f
) == 0) {
557 /* finished saving bulk on all devices */
558 block_mig_state
.bulk_completed
= 1;
562 ret
= blk_mig_save_dirty_block(f
, 1);
568 /* no more dirty blocks */
578 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
579 return qemu_ftell(f
) - last_ftell
;
582 static int block_save_complete(QEMUFile
*f
, void *opaque
)
586 DPRINTF("Enter save live complete submitted %d transferred %d\n",
587 block_mig_state
.submitted
, block_mig_state
.transferred
);
594 blk_mig_reset_dirty_cursor();
596 /* we know for sure that save bulk is completed and
597 all async read completed */
598 assert(block_mig_state
.submitted
== 0);
601 ret
= blk_mig_save_dirty_block(f
, 0);
607 /* report completion */
608 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
610 DPRINTF("Block migration completed\n");
612 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
618 static uint64_t block_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
)
620 /* Estimate pending number of bytes to send */
623 pending
= get_remaining_dirty() +
624 block_mig_state
.submitted
* BLOCK_SIZE
+
625 block_mig_state
.read_done
* BLOCK_SIZE
;
627 /* Report at least one block pending during bulk phase */
628 if (pending
== 0 && !block_mig_state
.bulk_completed
) {
629 pending
= BLOCK_SIZE
;
632 DPRINTF("Enter save live pending %" PRIu64
"\n", pending
);
636 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
638 static int banner_printed
;
640 char device_name
[256];
642 BlockDriverState
*bs
, *bs_prev
= NULL
;
644 int64_t total_sectors
= 0;
649 addr
= qemu_get_be64(f
);
651 flags
= addr
& ~BDRV_SECTOR_MASK
;
652 addr
>>= BDRV_SECTOR_BITS
;
654 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
655 /* get device name */
656 len
= qemu_get_byte(f
);
657 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
658 device_name
[len
] = '\0';
660 bs
= bdrv_find(device_name
);
662 fprintf(stderr
, "Error unknown block device %s\n",
669 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
670 if (total_sectors
<= 0) {
671 error_report("Error getting length of block device %s",
677 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
678 nr_sectors
= total_sectors
- addr
;
680 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
683 buf
= g_malloc(BLOCK_SIZE
);
685 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
686 ret
= bdrv_write(bs
, addr
, buf
, nr_sectors
);
692 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
693 if (!banner_printed
) {
694 printf("Receiving block device images\n");
697 printf("Completed %d %%%c", (int)addr
,
698 (addr
== 100) ? '\n' : '\r');
700 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
701 fprintf(stderr
, "Unknown block migration flags: %#x\n", flags
);
704 ret
= qemu_file_get_error(f
);
708 } while (!(flags
& BLK_MIG_FLAG_EOS
));
713 static void block_set_params(const MigrationParams
*params
, void *opaque
)
715 block_mig_state
.blk_enable
= params
->blk
;
716 block_mig_state
.shared_base
= params
->shared
;
718 /* shared base means that blk_enable = 1 */
719 block_mig_state
.blk_enable
|= params
->shared
;
722 static bool block_is_active(void *opaque
)
724 return block_mig_state
.blk_enable
== 1;
727 SaveVMHandlers savevm_block_handlers
= {
728 .set_params
= block_set_params
,
729 .save_live_setup
= block_save_setup
,
730 .save_live_iterate
= block_save_iterate
,
731 .save_live_complete
= block_save_complete
,
732 .save_live_pending
= block_save_pending
,
733 .load_state
= block_load
,
734 .cancel
= block_migration_cancel
,
735 .is_active
= block_is_active
,
738 void blk_mig_init(void)
740 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
741 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
743 register_savevm_live(NULL
, "block", 0, 1, &savevm_block_handlers
,