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.
14 #include "qemu-common.h"
15 #include "block_int.h"
17 #include "qemu-queue.h"
18 #include "qemu-timer.h"
20 #include "block-migration.h"
21 #include "migration.h"
24 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
26 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
27 #define BLK_MIG_FLAG_EOS 0x02
28 #define BLK_MIG_FLAG_PROGRESS 0x04
30 #define MAX_IS_ALLOCATED_SEARCH 65536
32 //#define DEBUG_BLK_MIGRATION
34 #ifdef DEBUG_BLK_MIGRATION
35 #define DPRINTF(fmt, ...) \
36 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
38 #define DPRINTF(fmt, ...) \
42 typedef struct BlkMigDevState
{
48 int64_t completed_sectors
;
49 int64_t total_sectors
;
51 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
52 unsigned long *aio_bitmap
;
55 typedef struct BlkMigBlock
{
62 BlockDriverAIOCB
*aiocb
;
65 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
68 typedef struct BlkMigState
{
71 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
72 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
76 int64_t total_sector_sum
;
79 long double total_time
;
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 inline void add_avg_read_time(int64_t time
)
135 block_mig_state
.reads
++;
136 block_mig_state
.total_time
+= time
;
139 static inline long double compute_read_bwidth(void)
141 assert(block_mig_state
.total_time
!= 0);
142 return (block_mig_state
.reads
* BLOCK_SIZE
)/ block_mig_state
.total_time
;
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 (bmds
->aio_bitmap
&&
150 (sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bmds
->bs
)) {
151 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
152 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
158 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
159 int nb_sectors
, int set
)
162 unsigned long val
, idx
, bit
;
164 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
165 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
167 for (; start
<= end
; start
++) {
168 idx
= start
/ (sizeof(unsigned long) * 8);
169 bit
= start
% (sizeof(unsigned long) * 8);
170 val
= bmds
->aio_bitmap
[idx
];
172 if (!(val
& (1UL << bit
))) {
176 if (val
& (1UL << bit
)) {
177 val
&= ~(1UL << bit
);
180 bmds
->aio_bitmap
[idx
] = val
;
184 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
186 BlockDriverState
*bs
= bmds
->bs
;
189 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
190 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
191 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
193 bmds
->aio_bitmap
= qemu_mallocz(bitmap_size
);
196 static void blk_mig_read_cb(void *opaque
, int ret
)
198 BlkMigBlock
*blk
= opaque
;
202 blk
->time
= qemu_get_clock_ns(rt_clock
) - blk
->time
;
204 add_avg_read_time(blk
->time
);
206 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
207 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
209 block_mig_state
.submitted
--;
210 block_mig_state
.read_done
++;
211 assert(block_mig_state
.submitted
>= 0);
214 static int mig_save_device_bulk(Monitor
*mon
, QEMUFile
*f
,
215 BlkMigDevState
*bmds
)
217 int64_t total_sectors
= bmds
->total_sectors
;
218 int64_t cur_sector
= bmds
->cur_sector
;
219 BlockDriverState
*bs
= bmds
->bs
;
223 if (bmds
->shared_base
) {
224 while (cur_sector
< total_sectors
&&
225 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
227 cur_sector
+= nr_sectors
;
231 if (cur_sector
>= total_sectors
) {
232 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
236 bmds
->completed_sectors
= cur_sector
;
238 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
240 /* we are going to transfer a full block even if it is not allocated */
241 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
243 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
244 nr_sectors
= total_sectors
- cur_sector
;
247 blk
= qemu_malloc(sizeof(BlkMigBlock
));
248 blk
->buf
= qemu_malloc(BLOCK_SIZE
);
250 blk
->sector
= cur_sector
;
251 blk
->nr_sectors
= nr_sectors
;
253 blk
->iov
.iov_base
= blk
->buf
;
254 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
255 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
257 blk
->time
= qemu_get_clock_ns(rt_clock
);
259 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
260 nr_sectors
, blk_mig_read_cb
, blk
);
264 block_mig_state
.submitted
++;
266 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
267 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
269 return (bmds
->cur_sector
>= total_sectors
);
272 monitor_printf(mon
, "Error reading sector %" PRId64
"\n", cur_sector
);
273 qemu_file_set_error(f
);
279 static void set_dirty_tracking(int enable
)
281 BlkMigDevState
*bmds
;
283 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
284 bdrv_set_dirty_tracking(bmds
->bs
, enable
);
288 static void init_blk_migration_it(void *opaque
, BlockDriverState
*bs
)
290 Monitor
*mon
= opaque
;
291 BlkMigDevState
*bmds
;
294 if (!bdrv_is_read_only(bs
)) {
295 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
300 bmds
= qemu_mallocz(sizeof(BlkMigDevState
));
302 bmds
->bulk_completed
= 0;
303 bmds
->total_sectors
= sectors
;
304 bmds
->completed_sectors
= 0;
305 bmds
->shared_base
= block_mig_state
.shared_base
;
306 alloc_aio_bitmap(bmds
);
308 block_mig_state
.total_sector_sum
+= sectors
;
310 if (bmds
->shared_base
) {
311 monitor_printf(mon
, "Start migration for %s with shared base "
315 monitor_printf(mon
, "Start full migration for %s\n",
319 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
323 static void init_blk_migration(Monitor
*mon
, QEMUFile
*f
)
325 block_mig_state
.submitted
= 0;
326 block_mig_state
.read_done
= 0;
327 block_mig_state
.transferred
= 0;
328 block_mig_state
.total_sector_sum
= 0;
329 block_mig_state
.prev_progress
= -1;
330 block_mig_state
.bulk_completed
= 0;
331 block_mig_state
.total_time
= 0;
332 block_mig_state
.reads
= 0;
334 bdrv_iterate(init_blk_migration_it
, mon
);
337 static int blk_mig_save_bulked_block(Monitor
*mon
, QEMUFile
*f
)
339 int64_t completed_sector_sum
= 0;
340 BlkMigDevState
*bmds
;
344 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
345 if (bmds
->bulk_completed
== 0) {
346 if (mig_save_device_bulk(mon
, f
, bmds
) == 1) {
347 /* completed bulk section for this device */
348 bmds
->bulk_completed
= 1;
350 completed_sector_sum
+= bmds
->completed_sectors
;
354 completed_sector_sum
+= bmds
->completed_sectors
;
358 progress
= completed_sector_sum
* 100 / block_mig_state
.total_sector_sum
;
359 if (progress
!= block_mig_state
.prev_progress
) {
360 block_mig_state
.prev_progress
= progress
;
361 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
362 | BLK_MIG_FLAG_PROGRESS
);
363 monitor_printf(mon
, "Completed %d %%\r", progress
);
370 static void blk_mig_reset_dirty_cursor(void)
372 BlkMigDevState
*bmds
;
374 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
379 static int mig_save_device_dirty(Monitor
*mon
, QEMUFile
*f
,
380 BlkMigDevState
*bmds
, int is_async
)
383 int64_t total_sectors
= bmds
->total_sectors
;
387 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
388 if (bmds_aio_inflight(bmds
, sector
))
390 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
392 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
393 nr_sectors
= total_sectors
- sector
;
395 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
397 blk
= qemu_malloc(sizeof(BlkMigBlock
));
398 blk
->buf
= qemu_malloc(BLOCK_SIZE
);
400 blk
->sector
= sector
;
401 blk
->nr_sectors
= nr_sectors
;
404 blk
->iov
.iov_base
= blk
->buf
;
405 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
406 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
408 blk
->time
= qemu_get_clock_ns(rt_clock
);
410 blk
->aiocb
= bdrv_aio_readv(bmds
->bs
, sector
, &blk
->qiov
,
411 nr_sectors
, blk_mig_read_cb
, blk
);
415 block_mig_state
.submitted
++;
416 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
418 if (bdrv_read(bmds
->bs
, sector
, blk
->buf
,
428 bdrv_reset_dirty(bmds
->bs
, sector
, nr_sectors
);
431 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
432 bmds
->cur_dirty
= sector
;
435 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
438 monitor_printf(mon
, "Error reading sector %" PRId64
"\n", sector
);
439 qemu_file_set_error(f
);
445 static int blk_mig_save_dirty_block(Monitor
*mon
, QEMUFile
*f
, int is_async
)
447 BlkMigDevState
*bmds
;
450 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
451 if (mig_save_device_dirty(mon
, f
, bmds
, is_async
) == 0) {
460 static void flush_blks(QEMUFile
* f
)
464 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
465 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
466 block_mig_state
.transferred
);
468 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
469 if (qemu_file_rate_limit(f
)) {
473 qemu_file_set_error(f
);
478 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
482 block_mig_state
.read_done
--;
483 block_mig_state
.transferred
++;
484 assert(block_mig_state
.read_done
>= 0);
487 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
488 block_mig_state
.submitted
, block_mig_state
.read_done
,
489 block_mig_state
.transferred
);
492 static int64_t get_remaining_dirty(void)
494 BlkMigDevState
*bmds
;
497 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
498 dirty
+= bdrv_get_dirty_count(bmds
->bs
);
501 return dirty
* BLOCK_SIZE
;
504 static int is_stage2_completed(void)
506 int64_t remaining_dirty
;
509 if (block_mig_state
.bulk_completed
== 1) {
511 remaining_dirty
= get_remaining_dirty();
512 if (remaining_dirty
== 0) {
516 bwidth
= compute_read_bwidth();
518 if ((remaining_dirty
/ bwidth
) <=
519 migrate_max_downtime()) {
520 /* finish stage2 because we think that we can finish remaing work
521 below max_downtime */
530 static void blk_mig_cleanup(Monitor
*mon
)
532 BlkMigDevState
*bmds
;
535 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
536 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
537 qemu_free(bmds
->aio_bitmap
);
541 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
542 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
547 set_dirty_tracking(0);
549 monitor_printf(mon
, "\n");
552 static int block_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
554 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
555 stage
, block_mig_state
.submitted
, block_mig_state
.transferred
);
558 blk_mig_cleanup(mon
);
562 if (block_mig_state
.blk_enable
!= 1) {
563 /* no need to migrate storage */
564 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
569 init_blk_migration(mon
, f
);
571 /* start track dirty blocks */
572 set_dirty_tracking(1);
577 if (qemu_file_has_error(f
)) {
578 blk_mig_cleanup(mon
);
582 blk_mig_reset_dirty_cursor();
585 /* control the rate of transfer */
586 while ((block_mig_state
.submitted
+
587 block_mig_state
.read_done
) * BLOCK_SIZE
<
588 qemu_file_get_rate_limit(f
)) {
589 if (block_mig_state
.bulk_completed
== 0) {
590 /* first finish the bulk phase */
591 if (blk_mig_save_bulked_block(mon
, f
) == 0) {
592 /* finished saving bulk on all devices */
593 block_mig_state
.bulk_completed
= 1;
596 if (blk_mig_save_dirty_block(mon
, f
, 1) == 0) {
597 /* no more dirty blocks */
605 if (qemu_file_has_error(f
)) {
606 blk_mig_cleanup(mon
);
612 /* we know for sure that save bulk is completed and
613 all async read completed */
614 assert(block_mig_state
.submitted
== 0);
616 while (blk_mig_save_dirty_block(mon
, f
, 0) != 0);
617 blk_mig_cleanup(mon
);
619 /* report completion */
620 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
622 if (qemu_file_has_error(f
)) {
626 monitor_printf(mon
, "Block migration completed\n");
629 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
631 return ((stage
== 2) && is_stage2_completed());
634 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
636 static int banner_printed
;
638 char device_name
[256];
640 BlockDriverState
*bs
;
644 addr
= qemu_get_be64(f
);
646 flags
= addr
& ~BDRV_SECTOR_MASK
;
647 addr
>>= BDRV_SECTOR_BITS
;
649 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",
663 buf
= qemu_malloc(BLOCK_SIZE
);
665 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
666 ret
= bdrv_write(bs
, addr
, buf
, BDRV_SECTORS_PER_DIRTY_CHUNK
);
672 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
673 if (!banner_printed
) {
674 printf("Receiving block device images\n");
677 printf("Completed %d %%%c", (int)addr
,
678 (addr
== 100) ? '\n' : '\r');
680 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
681 fprintf(stderr
, "Unknown flags\n");
684 if (qemu_file_has_error(f
)) {
687 } while (!(flags
& BLK_MIG_FLAG_EOS
));
692 static void block_set_params(int blk_enable
, int shared_base
, void *opaque
)
694 block_mig_state
.blk_enable
= blk_enable
;
695 block_mig_state
.shared_base
= shared_base
;
697 /* shared base means that blk_enable = 1 */
698 block_mig_state
.blk_enable
|= shared_base
;
701 void blk_mig_init(void)
703 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
704 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
706 register_savevm_live(NULL
, "block", 0, 1, block_set_params
,
707 block_save_live
, NULL
, block_load
, &block_mig_state
);