2 * QEMU live block migration
4 * Copyright IBM, Corp. 2009
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"
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 total_time;
81 long double prev_time_offset;
85 static BlkMigState block_mig_state;
87 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
91 /* sector number and flags */
92 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
93 | BLK_MIG_FLAG_DEVICE_BLOCK);
96 len = strlen(blk->bmds->bs->device_name);
97 qemu_put_byte(f, len);
98 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
100 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
103 int blk_mig_active(void)
105 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
108 uint64_t blk_mig_bytes_transferred(void)
110 BlkMigDevState *bmds;
113 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
114 sum += bmds->completed_sectors;
116 return sum << BDRV_SECTOR_BITS;
119 uint64_t blk_mig_bytes_remaining(void)
121 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
124 uint64_t blk_mig_bytes_total(void)
126 BlkMigDevState *bmds;
129 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
130 sum += bmds->total_sectors;
132 return sum << BDRV_SECTOR_BITS;
135 static inline long double compute_read_bwidth(void)
137 assert(block_mig_state.total_time != 0);
138 return (block_mig_state.reads / block_mig_state.total_time) * BLOCK_SIZE;
141 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
143 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
145 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
146 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
147 (1UL << (chunk % (sizeof(unsigned long) * 8))));
153 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
154 int nb_sectors, int set)
157 unsigned long val, idx, bit;
159 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
160 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
162 for (; start <= end; start++) {
163 idx = start / (sizeof(unsigned long) * 8);
164 bit = start % (sizeof(unsigned long) * 8);
165 val = bmds->aio_bitmap[idx];
169 val &= ~(1UL << bit);
171 bmds->aio_bitmap[idx] = val;
175 static void alloc_aio_bitmap(BlkMigDevState *bmds)
177 BlockDriverState *bs = bmds->bs;
180 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
181 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
182 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
184 bmds->aio_bitmap = g_malloc0(bitmap_size);
187 static void blk_mig_read_cb(void *opaque, int ret)
189 long double curr_time = qemu_get_clock_ns(rt_clock);
190 BlkMigBlock *blk = opaque;
194 block_mig_state.reads++;
195 block_mig_state.total_time += (curr_time - block_mig_state.prev_time_offset);
196 block_mig_state.prev_time_offset = curr_time;
198 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
199 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
201 block_mig_state.submitted--;
202 block_mig_state.read_done++;
203 assert(block_mig_state.submitted >= 0);
206 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
208 int64_t total_sectors = bmds->total_sectors;
209 int64_t cur_sector = bmds->cur_sector;
210 BlockDriverState *bs = bmds->bs;
214 if (bmds->shared_base) {
215 while (cur_sector < total_sectors &&
216 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
218 cur_sector += nr_sectors;
222 if (cur_sector >= total_sectors) {
223 bmds->cur_sector = bmds->completed_sectors = total_sectors;
227 bmds->completed_sectors = cur_sector;
229 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
231 /* we are going to transfer a full block even if it is not allocated */
232 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
234 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
235 nr_sectors = total_sectors - cur_sector;
238 blk = g_malloc(sizeof(BlkMigBlock));
239 blk->buf = g_malloc(BLOCK_SIZE);
241 blk->sector = cur_sector;
242 blk->nr_sectors = nr_sectors;
244 blk->iov.iov_base = blk->buf;
245 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
246 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
248 if (block_mig_state.submitted == 0) {
249 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
252 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
253 nr_sectors, blk_mig_read_cb, blk);
254 block_mig_state.submitted++;
256 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
257 bmds->cur_sector = cur_sector + nr_sectors;
259 return (bmds->cur_sector >= total_sectors);
262 static void set_dirty_tracking(int enable)
264 BlkMigDevState *bmds;
266 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
267 bdrv_set_dirty_tracking(bmds->bs, enable);
271 static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
273 BlkMigDevState *bmds;
276 if (!bdrv_is_read_only(bs)) {
277 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
282 bmds = g_malloc0(sizeof(BlkMigDevState));
284 bmds->bulk_completed = 0;
285 bmds->total_sectors = sectors;
286 bmds->completed_sectors = 0;
287 bmds->shared_base = block_mig_state.shared_base;
288 alloc_aio_bitmap(bmds);
289 drive_get_ref(drive_get_by_blockdev(bs));
290 bdrv_set_in_use(bs, 1);
292 block_mig_state.total_sector_sum += sectors;
294 if (bmds->shared_base) {
295 DPRINTF("Start migration for %s with shared base image\n",
298 DPRINTF("Start full migration for %s\n", bs->device_name);
301 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
305 static void init_blk_migration(QEMUFile *f)
307 block_mig_state.submitted = 0;
308 block_mig_state.read_done = 0;
309 block_mig_state.transferred = 0;
310 block_mig_state.total_sector_sum = 0;
311 block_mig_state.prev_progress = -1;
312 block_mig_state.bulk_completed = 0;
313 block_mig_state.total_time = 0;
314 block_mig_state.reads = 0;
316 bdrv_iterate(init_blk_migration_it, NULL);
319 static int blk_mig_save_bulked_block(QEMUFile *f)
321 int64_t completed_sector_sum = 0;
322 BlkMigDevState *bmds;
326 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
327 if (bmds->bulk_completed == 0) {
328 if (mig_save_device_bulk(f, bmds) == 1) {
329 /* completed bulk section for this device */
330 bmds->bulk_completed = 1;
332 completed_sector_sum += bmds->completed_sectors;
336 completed_sector_sum += bmds->completed_sectors;
340 if (block_mig_state.total_sector_sum != 0) {
341 progress = completed_sector_sum * 100 /
342 block_mig_state.total_sector_sum;
346 if (progress != block_mig_state.prev_progress) {
347 block_mig_state.prev_progress = progress;
348 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
349 | BLK_MIG_FLAG_PROGRESS);
350 DPRINTF("Completed %d %%\r", progress);
356 static void blk_mig_reset_dirty_cursor(void)
358 BlkMigDevState *bmds;
360 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
365 static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
369 int64_t total_sectors = bmds->total_sectors;
374 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
375 if (bmds_aio_inflight(bmds, sector)) {
378 if (bdrv_get_dirty(bmds->bs, sector)) {
380 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
381 nr_sectors = total_sectors - sector;
383 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
385 blk = g_malloc(sizeof(BlkMigBlock));
386 blk->buf = g_malloc(BLOCK_SIZE);
388 blk->sector = sector;
389 blk->nr_sectors = nr_sectors;
392 blk->iov.iov_base = blk->buf;
393 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
394 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
396 if (block_mig_state.submitted == 0) {
397 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
400 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
401 nr_sectors, blk_mig_read_cb, blk);
402 block_mig_state.submitted++;
403 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
405 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
415 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
418 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
419 bmds->cur_dirty = sector;
422 return (bmds->cur_dirty >= bmds->total_sectors);
425 DPRINTF("Error reading sector %" PRId64 "\n", sector);
432 * 0: too much data for max_downtime
433 * 1: few enough data for max_downtime
435 static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
437 BlkMigDevState *bmds;
440 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
441 ret = mig_save_device_dirty(f, bmds, is_async);
450 static int flush_blks(QEMUFile *f)
455 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
456 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
457 block_mig_state.transferred);
459 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
460 if (qemu_file_rate_limit(f)) {
469 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
473 block_mig_state.read_done--;
474 block_mig_state.transferred++;
475 assert(block_mig_state.read_done >= 0);
478 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
479 block_mig_state.submitted, block_mig_state.read_done,
480 block_mig_state.transferred);
484 static int64_t get_remaining_dirty(void)
486 BlkMigDevState *bmds;
489 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
490 dirty += bdrv_get_dirty_count(bmds->bs);
493 return dirty * BLOCK_SIZE;
496 static int is_stage2_completed(void)
498 int64_t remaining_dirty;
501 if (block_mig_state.bulk_completed == 1) {
503 remaining_dirty = get_remaining_dirty();
504 if (remaining_dirty == 0) {
508 bwidth = compute_read_bwidth();
510 if ((remaining_dirty / bwidth) <=
511 migrate_max_downtime()) {
512 /* finish stage2 because we think that we can finish remaining work
513 below max_downtime */
522 static void blk_mig_cleanup(void)
524 BlkMigDevState *bmds;
529 set_dirty_tracking(0);
531 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
532 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
533 bdrv_set_in_use(bmds->bs, 0);
534 drive_put_ref(drive_get_by_blockdev(bmds->bs));
535 g_free(bmds->aio_bitmap);
539 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
540 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
546 static void block_migration_cancel(void *opaque)
551 static int block_save_setup(QEMUFile *f, void *opaque)
555 DPRINTF("Enter save live setup submitted %d transferred %d\n",
556 block_mig_state.submitted, block_mig_state.transferred);
558 init_blk_migration(f);
560 /* start track dirty blocks */
561 set_dirty_tracking(1);
569 blk_mig_reset_dirty_cursor();
571 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
576 static int block_save_iterate(QEMUFile *f, void *opaque)
580 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
581 block_mig_state.submitted, block_mig_state.transferred);
589 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(f) == 0) {
598 /* finished saving bulk on all devices */
599 block_mig_state.bulk_completed = 1;
602 ret = blk_mig_save_dirty_block(f, 1);
604 /* no more dirty blocks */
620 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
622 return is_stage2_completed();
625 static int block_save_complete(QEMUFile *f, void *opaque)
629 DPRINTF("Enter save live complete submitted %d transferred %d\n",
630 block_mig_state.submitted, block_mig_state.transferred);
638 blk_mig_reset_dirty_cursor();
640 /* we know for sure that save bulk is completed and
641 all async read completed */
642 assert(block_mig_state.submitted == 0);
645 ret = blk_mig_save_dirty_block(f, 0);
652 /* report completion */
653 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
655 DPRINTF("Block migration completed\n");
657 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
662 static int block_load(QEMUFile *f, void *opaque, int version_id)
664 static int banner_printed;
666 char device_name[256];
668 BlockDriverState *bs, *bs_prev = NULL;
670 int64_t total_sectors = 0;
675 addr = qemu_get_be64(f);
677 flags = addr & ~BDRV_SECTOR_MASK;
678 addr >>= BDRV_SECTOR_BITS;
680 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
681 /* get device name */
682 len = qemu_get_byte(f);
683 qemu_get_buffer(f, (uint8_t *)device_name, len);
684 device_name[len] = '\0';
686 bs = bdrv_find(device_name);
688 fprintf(stderr, "Error unknown block device %s\n",
695 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
696 if (total_sectors <= 0) {
697 error_report("Error getting length of block device %s",
703 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
704 nr_sectors = total_sectors - addr;
706 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
709 buf = g_malloc(BLOCK_SIZE);
711 qemu_get_buffer(f, buf, BLOCK_SIZE);
712 ret = bdrv_write(bs, addr, buf, nr_sectors);
718 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
719 if (!banner_printed) {
720 printf("Receiving block device images\n");
723 printf("Completed %d %%%c", (int)addr,
724 (addr == 100) ? '\n' : '\r');
726 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
727 fprintf(stderr, "Unknown flags\n");
730 ret = qemu_file_get_error(f);
734 } while (!(flags & BLK_MIG_FLAG_EOS));
739 static void block_set_params(const MigrationParams *params, void *opaque)
741 block_mig_state.blk_enable = params->blk;
742 block_mig_state.shared_base = params->shared;
744 /* shared base means that blk_enable = 1 */
745 block_mig_state.blk_enable |= params->shared;
748 static bool block_is_active(void *opaque)
750 return block_mig_state.blk_enable == 1;
753 SaveVMHandlers savevm_block_handlers = {
754 .set_params = block_set_params,
755 .save_live_setup = block_save_setup,
756 .save_live_iterate = block_save_iterate,
757 .save_live_complete = block_save_complete,
758 .load_state = block_load,
759 .cancel = block_migration_cancel,
760 .is_active = block_is_active,
763 void blk_mig_init(void)
765 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
766 QSIMPLEQ_INIT(&block_mig_state.blk_list);
768 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,