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"
25 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
27 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
28 #define BLK_MIG_FLAG_EOS 0x02
29 #define BLK_MIG_FLAG_PROGRESS 0x04
31 #define MAX_IS_ALLOCATED_SEARCH 65536
33 //#define DEBUG_BLK_MIGRATION
35 #ifdef DEBUG_BLK_MIGRATION
36 #define DPRINTF(fmt, ...) \
37 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
39 #define DPRINTF(fmt, ...) \
43 typedef struct BlkMigDevState
{
49 int64_t completed_sectors
;
50 int64_t total_sectors
;
52 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
53 unsigned long *aio_bitmap
;
56 typedef struct BlkMigBlock
{
63 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 total_time
;
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 inline void add_avg_read_time(int64_t time
)
136 block_mig_state
.reads
++;
137 block_mig_state
.total_time
+= time
;
140 static inline long double compute_read_bwidth(void)
142 assert(block_mig_state
.total_time
!= 0);
143 return (block_mig_state
.reads
* BLOCK_SIZE
)/ block_mig_state
.total_time
;
146 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
148 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
150 if ((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
];
174 val
&= ~(1UL << bit
);
176 bmds
->aio_bitmap
[idx
] = val
;
180 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
182 BlockDriverState
*bs
= bmds
->bs
;
185 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
186 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
187 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
189 bmds
->aio_bitmap
= qemu_mallocz(bitmap_size
);
192 static void blk_mig_read_cb(void *opaque
, int ret
)
194 BlkMigBlock
*blk
= opaque
;
198 blk
->time
= qemu_get_clock_ns(rt_clock
) - blk
->time
;
200 add_avg_read_time(blk
->time
);
202 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
203 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
205 block_mig_state
.submitted
--;
206 block_mig_state
.read_done
++;
207 assert(block_mig_state
.submitted
>= 0);
210 static int mig_save_device_bulk(Monitor
*mon
, QEMUFile
*f
,
211 BlkMigDevState
*bmds
)
213 int64_t total_sectors
= bmds
->total_sectors
;
214 int64_t cur_sector
= bmds
->cur_sector
;
215 BlockDriverState
*bs
= bmds
->bs
;
219 if (bmds
->shared_base
) {
220 while (cur_sector
< total_sectors
&&
221 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
223 cur_sector
+= nr_sectors
;
227 if (cur_sector
>= total_sectors
) {
228 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
232 bmds
->completed_sectors
= cur_sector
;
234 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
236 /* we are going to transfer a full block even if it is not allocated */
237 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
239 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
240 nr_sectors
= total_sectors
- cur_sector
;
243 blk
= qemu_malloc(sizeof(BlkMigBlock
));
244 blk
->buf
= qemu_malloc(BLOCK_SIZE
);
246 blk
->sector
= cur_sector
;
247 blk
->nr_sectors
= nr_sectors
;
249 blk
->iov
.iov_base
= blk
->buf
;
250 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
251 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
253 blk
->time
= qemu_get_clock_ns(rt_clock
);
255 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
256 nr_sectors
, blk_mig_read_cb
, blk
);
260 block_mig_state
.submitted
++;
262 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
263 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
265 return (bmds
->cur_sector
>= total_sectors
);
268 monitor_printf(mon
, "Error reading sector %" PRId64
"\n", cur_sector
);
269 qemu_file_set_error(f
);
275 static void set_dirty_tracking(int enable
)
277 BlkMigDevState
*bmds
;
279 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
280 bdrv_set_dirty_tracking(bmds
->bs
, enable
);
284 static void init_blk_migration_it(void *opaque
, BlockDriverState
*bs
)
286 Monitor
*mon
= opaque
;
287 BlkMigDevState
*bmds
;
290 if (!bdrv_is_read_only(bs
)) {
291 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
296 bmds
= qemu_mallocz(sizeof(BlkMigDevState
));
298 bmds
->bulk_completed
= 0;
299 bmds
->total_sectors
= sectors
;
300 bmds
->completed_sectors
= 0;
301 bmds
->shared_base
= block_mig_state
.shared_base
;
302 alloc_aio_bitmap(bmds
);
303 drive_get_ref(drive_get_by_blockdev(bs
));
304 bdrv_set_in_use(bs
, 1);
306 block_mig_state
.total_sector_sum
+= sectors
;
308 if (bmds
->shared_base
) {
309 monitor_printf(mon
, "Start migration for %s with shared base "
313 monitor_printf(mon
, "Start full migration for %s\n",
317 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
321 static void init_blk_migration(Monitor
*mon
, QEMUFile
*f
)
323 block_mig_state
.submitted
= 0;
324 block_mig_state
.read_done
= 0;
325 block_mig_state
.transferred
= 0;
326 block_mig_state
.total_sector_sum
= 0;
327 block_mig_state
.prev_progress
= -1;
328 block_mig_state
.bulk_completed
= 0;
329 block_mig_state
.total_time
= 0;
330 block_mig_state
.reads
= 0;
332 bdrv_iterate(init_blk_migration_it
, mon
);
335 static int blk_mig_save_bulked_block(Monitor
*mon
, QEMUFile
*f
)
337 int64_t completed_sector_sum
= 0;
338 BlkMigDevState
*bmds
;
342 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
343 if (bmds
->bulk_completed
== 0) {
344 if (mig_save_device_bulk(mon
, f
, bmds
) == 1) {
345 /* completed bulk section for this device */
346 bmds
->bulk_completed
= 1;
348 completed_sector_sum
+= bmds
->completed_sectors
;
352 completed_sector_sum
+= bmds
->completed_sectors
;
356 if (block_mig_state
.total_sector_sum
!= 0) {
357 progress
= completed_sector_sum
* 100 /
358 block_mig_state
.total_sector_sum
;
362 if (progress
!= block_mig_state
.prev_progress
) {
363 block_mig_state
.prev_progress
= progress
;
364 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
365 | BLK_MIG_FLAG_PROGRESS
);
366 monitor_printf(mon
, "Completed %d %%\r", progress
);
373 static void blk_mig_reset_dirty_cursor(void)
375 BlkMigDevState
*bmds
;
377 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
382 static int mig_save_device_dirty(Monitor
*mon
, QEMUFile
*f
,
383 BlkMigDevState
*bmds
, int is_async
)
386 int64_t total_sectors
= bmds
->total_sectors
;
390 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
391 if (bmds_aio_inflight(bmds
, sector
)) {
394 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
396 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
397 nr_sectors
= total_sectors
- sector
;
399 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
401 blk
= qemu_malloc(sizeof(BlkMigBlock
));
402 blk
->buf
= qemu_malloc(BLOCK_SIZE
);
404 blk
->sector
= sector
;
405 blk
->nr_sectors
= nr_sectors
;
408 blk
->iov
.iov_base
= blk
->buf
;
409 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
410 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
412 blk
->time
= qemu_get_clock_ns(rt_clock
);
414 blk
->aiocb
= bdrv_aio_readv(bmds
->bs
, sector
, &blk
->qiov
,
415 nr_sectors
, blk_mig_read_cb
, blk
);
419 block_mig_state
.submitted
++;
420 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
422 if (bdrv_read(bmds
->bs
, sector
, blk
->buf
,
432 bdrv_reset_dirty(bmds
->bs
, sector
, nr_sectors
);
435 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
436 bmds
->cur_dirty
= sector
;
439 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
442 monitor_printf(mon
, "Error reading sector %" PRId64
"\n", sector
);
443 qemu_file_set_error(f
);
449 static int blk_mig_save_dirty_block(Monitor
*mon
, QEMUFile
*f
, int is_async
)
451 BlkMigDevState
*bmds
;
454 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
455 if (mig_save_device_dirty(mon
, f
, bmds
, is_async
) == 0) {
464 static void flush_blks(QEMUFile
* f
)
468 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
469 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
470 block_mig_state
.transferred
);
472 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
473 if (qemu_file_rate_limit(f
)) {
477 qemu_file_set_error(f
);
482 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
486 block_mig_state
.read_done
--;
487 block_mig_state
.transferred
++;
488 assert(block_mig_state
.read_done
>= 0);
491 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
492 block_mig_state
.submitted
, block_mig_state
.read_done
,
493 block_mig_state
.transferred
);
496 static int64_t get_remaining_dirty(void)
498 BlkMigDevState
*bmds
;
501 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
502 dirty
+= bdrv_get_dirty_count(bmds
->bs
);
505 return dirty
* BLOCK_SIZE
;
508 static int is_stage2_completed(void)
510 int64_t remaining_dirty
;
513 if (block_mig_state
.bulk_completed
== 1) {
515 remaining_dirty
= get_remaining_dirty();
516 if (remaining_dirty
== 0) {
520 bwidth
= compute_read_bwidth();
522 if ((remaining_dirty
/ bwidth
) <=
523 migrate_max_downtime()) {
524 /* finish stage2 because we think that we can finish remaing work
525 below max_downtime */
534 static void blk_mig_cleanup(Monitor
*mon
)
536 BlkMigDevState
*bmds
;
539 set_dirty_tracking(0);
541 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
542 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
543 bdrv_set_in_use(bmds
->bs
, 0);
544 drive_put_ref(drive_get_by_blockdev(bmds
->bs
));
545 qemu_free(bmds
->aio_bitmap
);
549 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
550 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
555 monitor_printf(mon
, "\n");
558 static int block_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
560 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
561 stage
, block_mig_state
.submitted
, block_mig_state
.transferred
);
564 blk_mig_cleanup(mon
);
568 if (block_mig_state
.blk_enable
!= 1) {
569 /* no need to migrate storage */
570 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
575 init_blk_migration(mon
, f
);
577 /* start track dirty blocks */
578 set_dirty_tracking(1);
583 if (qemu_file_has_error(f
)) {
584 blk_mig_cleanup(mon
);
588 blk_mig_reset_dirty_cursor();
591 /* control the rate of transfer */
592 while ((block_mig_state
.submitted
+
593 block_mig_state
.read_done
) * BLOCK_SIZE
<
594 qemu_file_get_rate_limit(f
)) {
595 if (block_mig_state
.bulk_completed
== 0) {
596 /* first finish the bulk phase */
597 if (blk_mig_save_bulked_block(mon
, f
) == 0) {
598 /* finished saving bulk on all devices */
599 block_mig_state
.bulk_completed
= 1;
602 if (blk_mig_save_dirty_block(mon
, f
, 1) == 0) {
603 /* no more dirty blocks */
611 if (qemu_file_has_error(f
)) {
612 blk_mig_cleanup(mon
);
618 /* we know for sure that save bulk is completed and
619 all async read completed */
620 assert(block_mig_state
.submitted
== 0);
622 while (blk_mig_save_dirty_block(mon
, f
, 0) != 0);
623 blk_mig_cleanup(mon
);
625 /* report completion */
626 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
628 if (qemu_file_has_error(f
)) {
632 monitor_printf(mon
, "Block migration completed\n");
635 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
637 return ((stage
== 2) && is_stage2_completed());
640 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
642 static int banner_printed
;
644 char device_name
[256];
646 BlockDriverState
*bs
, *bs_prev
= NULL
;
648 int64_t total_sectors
= 0;
652 addr
= qemu_get_be64(f
);
654 flags
= addr
& ~BDRV_SECTOR_MASK
;
655 addr
>>= BDRV_SECTOR_BITS
;
657 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
659 /* get device name */
660 len
= qemu_get_byte(f
);
661 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
662 device_name
[len
] = '\0';
664 bs
= bdrv_find(device_name
);
666 fprintf(stderr
, "Error unknown block device %s\n",
673 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
674 if (total_sectors
<= 0) {
675 error_report("Error getting length of block device %s\n",
681 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
682 nr_sectors
= total_sectors
- addr
;
684 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
687 buf
= qemu_malloc(BLOCK_SIZE
);
689 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
690 ret
= bdrv_write(bs
, addr
, buf
, nr_sectors
);
696 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
697 if (!banner_printed
) {
698 printf("Receiving block device images\n");
701 printf("Completed %d %%%c", (int)addr
,
702 (addr
== 100) ? '\n' : '\r');
704 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
705 fprintf(stderr
, "Unknown flags\n");
708 if (qemu_file_has_error(f
)) {
711 } while (!(flags
& BLK_MIG_FLAG_EOS
));
716 static void block_set_params(int blk_enable
, int shared_base
, void *opaque
)
718 block_mig_state
.blk_enable
= blk_enable
;
719 block_mig_state
.shared_base
= shared_base
;
721 /* shared base means that blk_enable = 1 */
722 block_mig_state
.blk_enable
|= shared_base
;
725 void blk_mig_init(void)
727 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
728 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
730 register_savevm_live(NULL
, "block", 0, 1, block_set_params
,
731 block_save_live
, NULL
, block_load
, &block_mig_state
);