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 (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
28 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
29 #define BLK_MIG_FLAG_EOS 0x02
30 #define BLK_MIG_FLAG_PROGRESS 0x04
32 #define MAX_IS_ALLOCATED_SEARCH 65536
34 //#define DEBUG_BLK_MIGRATION
36 #ifdef DEBUG_BLK_MIGRATION
37 #define DPRINTF(fmt, ...) \
38 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
40 #define DPRINTF(fmt, ...) \
44 typedef struct BlkMigDevState
{
50 int64_t completed_sectors
;
51 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
;
80 long double prev_time_offset
;
83 static BlkMigState block_mig_state
;
85 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
89 /* sector number and flags */
90 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
91 | BLK_MIG_FLAG_DEVICE_BLOCK
);
94 len
= strlen(blk
->bmds
->bs
->device_name
);
95 qemu_put_byte(f
, len
);
96 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
98 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
101 int blk_mig_active(void)
103 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
106 uint64_t blk_mig_bytes_transferred(void)
108 BlkMigDevState
*bmds
;
111 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
112 sum
+= bmds
->completed_sectors
;
114 return sum
<< BDRV_SECTOR_BITS
;
117 uint64_t blk_mig_bytes_remaining(void)
119 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
122 uint64_t blk_mig_bytes_total(void)
124 BlkMigDevState
*bmds
;
127 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
128 sum
+= bmds
->total_sectors
;
130 return sum
<< BDRV_SECTOR_BITS
;
133 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
135 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
137 if ((sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bmds
->bs
)) {
138 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
139 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
145 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
146 int nb_sectors
, int set
)
149 unsigned long val
, idx
, bit
;
151 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
152 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
154 for (; start
<= end
; start
++) {
155 idx
= start
/ (sizeof(unsigned long) * 8);
156 bit
= start
% (sizeof(unsigned long) * 8);
157 val
= bmds
->aio_bitmap
[idx
];
161 val
&= ~(1UL << bit
);
163 bmds
->aio_bitmap
[idx
] = val
;
167 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
169 BlockDriverState
*bs
= bmds
->bs
;
172 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
173 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
174 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
176 bmds
->aio_bitmap
= g_malloc0(bitmap_size
);
179 static void blk_mig_read_cb(void *opaque
, int ret
)
181 long double curr_time
= qemu_get_clock_ns(rt_clock
);
182 BlkMigBlock
*blk
= opaque
;
186 block_mig_state
.prev_time_offset
= curr_time
;
188 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
189 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
191 block_mig_state
.submitted
--;
192 block_mig_state
.read_done
++;
193 assert(block_mig_state
.submitted
>= 0);
196 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
198 int64_t total_sectors
= bmds
->total_sectors
;
199 int64_t cur_sector
= bmds
->cur_sector
;
200 BlockDriverState
*bs
= bmds
->bs
;
204 if (bmds
->shared_base
) {
205 while (cur_sector
< total_sectors
&&
206 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
208 cur_sector
+= nr_sectors
;
212 if (cur_sector
>= total_sectors
) {
213 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
217 bmds
->completed_sectors
= cur_sector
;
219 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
221 /* we are going to transfer a full block even if it is not allocated */
222 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
224 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
225 nr_sectors
= total_sectors
- cur_sector
;
228 blk
= g_malloc(sizeof(BlkMigBlock
));
229 blk
->buf
= g_malloc(BLOCK_SIZE
);
231 blk
->sector
= cur_sector
;
232 blk
->nr_sectors
= nr_sectors
;
234 blk
->iov
.iov_base
= blk
->buf
;
235 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
236 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
238 if (block_mig_state
.submitted
== 0) {
239 block_mig_state
.prev_time_offset
= qemu_get_clock_ns(rt_clock
);
242 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
243 nr_sectors
, blk_mig_read_cb
, blk
);
244 block_mig_state
.submitted
++;
246 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
247 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
249 return (bmds
->cur_sector
>= total_sectors
);
252 static void set_dirty_tracking(int enable
)
254 BlkMigDevState
*bmds
;
256 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
257 bdrv_set_dirty_tracking(bmds
->bs
, enable
);
261 static void init_blk_migration_it(void *opaque
, BlockDriverState
*bs
)
263 BlkMigDevState
*bmds
;
266 if (!bdrv_is_read_only(bs
)) {
267 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
272 bmds
= g_malloc0(sizeof(BlkMigDevState
));
274 bmds
->bulk_completed
= 0;
275 bmds
->total_sectors
= sectors
;
276 bmds
->completed_sectors
= 0;
277 bmds
->shared_base
= block_mig_state
.shared_base
;
278 alloc_aio_bitmap(bmds
);
279 drive_get_ref(drive_get_by_blockdev(bs
));
280 bdrv_set_in_use(bs
, 1);
282 block_mig_state
.total_sector_sum
+= sectors
;
284 if (bmds
->shared_base
) {
285 DPRINTF("Start migration for %s with shared base image\n",
288 DPRINTF("Start full migration for %s\n", bs
->device_name
);
291 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
295 static void init_blk_migration(QEMUFile
*f
)
297 block_mig_state
.submitted
= 0;
298 block_mig_state
.read_done
= 0;
299 block_mig_state
.transferred
= 0;
300 block_mig_state
.total_sector_sum
= 0;
301 block_mig_state
.prev_progress
= -1;
302 block_mig_state
.bulk_completed
= 0;
304 bdrv_iterate(init_blk_migration_it
, NULL
);
307 static int blk_mig_save_bulked_block(QEMUFile
*f
)
309 int64_t completed_sector_sum
= 0;
310 BlkMigDevState
*bmds
;
314 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
315 if (bmds
->bulk_completed
== 0) {
316 if (mig_save_device_bulk(f
, bmds
) == 1) {
317 /* completed bulk section for this device */
318 bmds
->bulk_completed
= 1;
320 completed_sector_sum
+= bmds
->completed_sectors
;
324 completed_sector_sum
+= bmds
->completed_sectors
;
328 if (block_mig_state
.total_sector_sum
!= 0) {
329 progress
= completed_sector_sum
* 100 /
330 block_mig_state
.total_sector_sum
;
334 if (progress
!= block_mig_state
.prev_progress
) {
335 block_mig_state
.prev_progress
= progress
;
336 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
337 | BLK_MIG_FLAG_PROGRESS
);
338 DPRINTF("Completed %d %%\r", progress
);
344 static void blk_mig_reset_dirty_cursor(void)
346 BlkMigDevState
*bmds
;
348 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
353 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
357 int64_t total_sectors
= bmds
->total_sectors
;
362 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
363 if (bmds_aio_inflight(bmds
, sector
)) {
366 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
368 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
369 nr_sectors
= total_sectors
- sector
;
371 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
373 blk
= g_malloc(sizeof(BlkMigBlock
));
374 blk
->buf
= g_malloc(BLOCK_SIZE
);
376 blk
->sector
= sector
;
377 blk
->nr_sectors
= nr_sectors
;
380 blk
->iov
.iov_base
= blk
->buf
;
381 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
382 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
384 if (block_mig_state
.submitted
== 0) {
385 block_mig_state
.prev_time_offset
= qemu_get_clock_ns(rt_clock
);
388 blk
->aiocb
= bdrv_aio_readv(bmds
->bs
, sector
, &blk
->qiov
,
389 nr_sectors
, blk_mig_read_cb
, blk
);
390 block_mig_state
.submitted
++;
391 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
393 ret
= bdrv_read(bmds
->bs
, sector
, blk
->buf
, nr_sectors
);
403 bdrv_reset_dirty(bmds
->bs
, sector
, nr_sectors
);
406 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
407 bmds
->cur_dirty
= sector
;
410 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
413 DPRINTF("Error reading sector %" PRId64
"\n", sector
);
420 * 0: too much data for max_downtime
421 * 1: few enough data for max_downtime
423 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
425 BlkMigDevState
*bmds
;
428 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
429 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
438 static int flush_blks(QEMUFile
*f
)
443 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
444 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
445 block_mig_state
.transferred
);
447 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
448 if (qemu_file_rate_limit(f
)) {
457 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
461 block_mig_state
.read_done
--;
462 block_mig_state
.transferred
++;
463 assert(block_mig_state
.read_done
>= 0);
466 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
467 block_mig_state
.submitted
, block_mig_state
.read_done
,
468 block_mig_state
.transferred
);
472 static int64_t get_remaining_dirty(void)
474 BlkMigDevState
*bmds
;
477 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
478 dirty
+= bdrv_get_dirty_count(bmds
->bs
);
481 return dirty
* BLOCK_SIZE
;
484 static void blk_mig_cleanup(void)
486 BlkMigDevState
*bmds
;
491 set_dirty_tracking(0);
493 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
494 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
495 bdrv_set_in_use(bmds
->bs
, 0);
496 drive_put_ref(drive_get_by_blockdev(bmds
->bs
));
497 g_free(bmds
->aio_bitmap
);
501 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
502 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
508 static void block_migration_cancel(void *opaque
)
513 static int block_save_setup(QEMUFile
*f
, void *opaque
)
517 DPRINTF("Enter save live setup submitted %d transferred %d\n",
518 block_mig_state
.submitted
, block_mig_state
.transferred
);
520 init_blk_migration(f
);
522 /* start track dirty blocks */
523 set_dirty_tracking(1);
531 blk_mig_reset_dirty_cursor();
533 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
538 static int block_save_iterate(QEMUFile
*f
, void *opaque
)
542 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
543 block_mig_state
.submitted
, block_mig_state
.transferred
);
551 blk_mig_reset_dirty_cursor();
553 /* control the rate of transfer */
554 while ((block_mig_state
.submitted
+
555 block_mig_state
.read_done
) * BLOCK_SIZE
<
556 qemu_file_get_rate_limit(f
)) {
557 if (block_mig_state
.bulk_completed
== 0) {
558 /* first finish the bulk phase */
559 if (blk_mig_save_bulked_block(f
) == 0) {
560 /* finished saving bulk on all devices */
561 block_mig_state
.bulk_completed
= 1;
564 ret
= blk_mig_save_dirty_block(f
, 1);
566 /* no more dirty blocks */
582 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
587 static int block_save_complete(QEMUFile
*f
, void *opaque
)
591 DPRINTF("Enter save live complete submitted %d transferred %d\n",
592 block_mig_state
.submitted
, block_mig_state
.transferred
);
600 blk_mig_reset_dirty_cursor();
602 /* we know for sure that save bulk is completed and
603 all async read completed */
604 assert(block_mig_state
.submitted
== 0);
607 ret
= blk_mig_save_dirty_block(f
, 0);
614 /* report completion */
615 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
617 DPRINTF("Block migration completed\n");
619 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
624 static uint64_t block_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
)
627 DPRINTF("Enter save live pending %ld\n", get_remaining_dirty());
629 return get_remaining_dirty();
632 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
634 static int banner_printed
;
636 char device_name
[256];
638 BlockDriverState
*bs
, *bs_prev
= NULL
;
640 int64_t total_sectors
= 0;
645 addr
= qemu_get_be64(f
);
647 flags
= addr
& ~BDRV_SECTOR_MASK
;
648 addr
>>= BDRV_SECTOR_BITS
;
650 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
651 /* get device name */
652 len
= qemu_get_byte(f
);
653 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
654 device_name
[len
] = '\0';
656 bs
= bdrv_find(device_name
);
658 fprintf(stderr
, "Error unknown block device %s\n",
665 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
666 if (total_sectors
<= 0) {
667 error_report("Error getting length of block device %s",
673 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
674 nr_sectors
= total_sectors
- addr
;
676 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
679 buf
= g_malloc(BLOCK_SIZE
);
681 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
682 ret
= bdrv_write(bs
, addr
, buf
, nr_sectors
);
688 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
689 if (!banner_printed
) {
690 printf("Receiving block device images\n");
693 printf("Completed %d %%%c", (int)addr
,
694 (addr
== 100) ? '\n' : '\r');
696 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
697 fprintf(stderr
, "Unknown flags\n");
700 ret
= qemu_file_get_error(f
);
704 } while (!(flags
& BLK_MIG_FLAG_EOS
));
709 static void block_set_params(const MigrationParams
*params
, void *opaque
)
711 block_mig_state
.blk_enable
= params
->blk
;
712 block_mig_state
.shared_base
= params
->shared
;
714 /* shared base means that blk_enable = 1 */
715 block_mig_state
.blk_enable
|= params
->shared
;
718 static bool block_is_active(void *opaque
)
720 return block_mig_state
.blk_enable
== 1;
723 SaveVMHandlers savevm_block_handlers
= {
724 .set_params
= block_set_params
,
725 .save_live_setup
= block_save_setup
,
726 .save_live_iterate
= block_save_iterate
,
727 .save_live_complete
= block_save_complete
,
728 .save_live_pending
= block_save_pending
,
729 .load_state
= block_load
,
730 .cancel
= block_migration_cancel
,
731 .is_active
= block_is_active
,
734 void blk_mig_init(void)
736 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
737 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
739 register_savevm_live(NULL
, "block", 0, 1, &savevm_block_handlers
,