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_int.h"
19 #include "qemu-queue.h"
20 #include "qemu-timer.h"
22 #include "block-migration.h"
23 #include "migration.h"
27 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << 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 {
51 int64_t completed_sectors;
52 int64_t total_sectors;
54 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
55 unsigned long *aio_bitmap;
58 typedef struct BlkMigBlock {
65 BlockDriverAIOCB *aiocb;
67 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
70 typedef struct BlkMigState {
73 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
74 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
78 int64_t total_sector_sum;
81 long double total_time;
82 long double prev_time_offset;
86 static BlkMigState block_mig_state;
88 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
92 /* sector number and flags */
93 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
94 | BLK_MIG_FLAG_DEVICE_BLOCK);
97 len = strlen(blk->bmds->bs->device_name);
98 qemu_put_byte(f, len);
99 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
101 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
104 int blk_mig_active(void)
106 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
109 uint64_t blk_mig_bytes_transferred(void)
111 BlkMigDevState *bmds;
114 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
115 sum += bmds->completed_sectors;
117 return sum << BDRV_SECTOR_BITS;
120 uint64_t blk_mig_bytes_remaining(void)
122 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
125 uint64_t blk_mig_bytes_total(void)
127 BlkMigDevState *bmds;
130 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
131 sum += bmds->total_sectors;
133 return sum << BDRV_SECTOR_BITS;
136 static inline long double compute_read_bwidth(void)
138 assert(block_mig_state.total_time != 0);
139 return (block_mig_state.reads / block_mig_state.total_time) * BLOCK_SIZE;
142 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
144 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
146 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
147 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
148 (1UL << (chunk % (sizeof(unsigned long) * 8))));
154 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
155 int nb_sectors, int set)
158 unsigned long val, idx, bit;
160 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
161 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
163 for (; start <= end; start++) {
164 idx = start / (sizeof(unsigned long) * 8);
165 bit = start % (sizeof(unsigned long) * 8);
166 val = bmds->aio_bitmap[idx];
170 val &= ~(1UL << bit);
172 bmds->aio_bitmap[idx] = val;
176 static void alloc_aio_bitmap(BlkMigDevState *bmds)
178 BlockDriverState *bs = bmds->bs;
181 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
182 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
183 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
185 bmds->aio_bitmap = g_malloc0(bitmap_size);
188 static void blk_mig_read_cb(void *opaque, int ret)
190 long double curr_time = qemu_get_clock_ns(rt_clock);
191 BlkMigBlock *blk = opaque;
195 block_mig_state.reads++;
196 block_mig_state.total_time += (curr_time - block_mig_state.prev_time_offset);
197 block_mig_state.prev_time_offset = curr_time;
199 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
200 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
202 block_mig_state.submitted--;
203 block_mig_state.read_done++;
204 assert(block_mig_state.submitted >= 0);
207 static int mig_save_device_bulk(Monitor *mon, QEMUFile *f,
208 BlkMigDevState *bmds)
210 int64_t total_sectors = bmds->total_sectors;
211 int64_t cur_sector = bmds->cur_sector;
212 BlockDriverState *bs = bmds->bs;
216 if (bmds->shared_base) {
217 while (cur_sector < total_sectors &&
218 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
220 cur_sector += nr_sectors;
224 if (cur_sector >= total_sectors) {
225 bmds->cur_sector = bmds->completed_sectors = total_sectors;
229 bmds->completed_sectors = cur_sector;
231 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
233 /* we are going to transfer a full block even if it is not allocated */
234 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
236 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
237 nr_sectors = total_sectors - cur_sector;
240 blk = g_malloc(sizeof(BlkMigBlock));
241 blk->buf = g_malloc(BLOCK_SIZE);
243 blk->sector = cur_sector;
244 blk->nr_sectors = nr_sectors;
246 blk->iov.iov_base = blk->buf;
247 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
248 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
250 if (block_mig_state.submitted == 0) {
251 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
254 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
255 nr_sectors, blk_mig_read_cb, blk);
256 block_mig_state.submitted++;
258 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
259 bmds->cur_sector = cur_sector + nr_sectors;
261 return (bmds->cur_sector >= total_sectors);
264 static void set_dirty_tracking(int enable)
266 BlkMigDevState *bmds;
268 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
269 bdrv_set_dirty_tracking(bmds->bs, enable);
273 static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
275 Monitor *mon = opaque;
276 BlkMigDevState *bmds;
279 if (!bdrv_is_read_only(bs)) {
280 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
285 bmds = g_malloc0(sizeof(BlkMigDevState));
287 bmds->bulk_completed = 0;
288 bmds->total_sectors = sectors;
289 bmds->completed_sectors = 0;
290 bmds->shared_base = block_mig_state.shared_base;
291 alloc_aio_bitmap(bmds);
292 drive_get_ref(drive_get_by_blockdev(bs));
293 bdrv_set_in_use(bs, 1);
295 block_mig_state.total_sector_sum += sectors;
297 if (bmds->shared_base) {
298 monitor_printf(mon, "Start migration for %s with shared base "
302 monitor_printf(mon, "Start full migration for %s\n",
306 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
310 static void init_blk_migration(Monitor *mon, QEMUFile *f)
312 block_mig_state.submitted = 0;
313 block_mig_state.read_done = 0;
314 block_mig_state.transferred = 0;
315 block_mig_state.total_sector_sum = 0;
316 block_mig_state.prev_progress = -1;
317 block_mig_state.bulk_completed = 0;
318 block_mig_state.total_time = 0;
319 block_mig_state.reads = 0;
321 bdrv_iterate(init_blk_migration_it, mon);
324 static int blk_mig_save_bulked_block(Monitor *mon, QEMUFile *f)
326 int64_t completed_sector_sum = 0;
327 BlkMigDevState *bmds;
331 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
332 if (bmds->bulk_completed == 0) {
333 if (mig_save_device_bulk(mon, f, bmds) == 1) {
334 /* completed bulk section for this device */
335 bmds->bulk_completed = 1;
337 completed_sector_sum += bmds->completed_sectors;
341 completed_sector_sum += bmds->completed_sectors;
345 if (block_mig_state.total_sector_sum != 0) {
346 progress = completed_sector_sum * 100 /
347 block_mig_state.total_sector_sum;
351 if (progress != block_mig_state.prev_progress) {
352 block_mig_state.prev_progress = progress;
353 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
354 | BLK_MIG_FLAG_PROGRESS);
355 monitor_printf(mon, "Completed %d %%\r", progress);
362 static void blk_mig_reset_dirty_cursor(void)
364 BlkMigDevState *bmds;
366 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
371 static int mig_save_device_dirty(Monitor *mon, QEMUFile *f,
372 BlkMigDevState *bmds, int is_async)
375 int64_t total_sectors = bmds->total_sectors;
380 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
381 if (bmds_aio_inflight(bmds, sector)) {
384 if (bdrv_get_dirty(bmds->bs, sector)) {
386 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
387 nr_sectors = total_sectors - sector;
389 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
391 blk = g_malloc(sizeof(BlkMigBlock));
392 blk->buf = g_malloc(BLOCK_SIZE);
394 blk->sector = sector;
395 blk->nr_sectors = nr_sectors;
398 blk->iov.iov_base = blk->buf;
399 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
400 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
402 if (block_mig_state.submitted == 0) {
403 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
406 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
407 nr_sectors, blk_mig_read_cb, blk);
408 block_mig_state.submitted++;
409 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
411 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
421 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
424 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
425 bmds->cur_dirty = sector;
428 return (bmds->cur_dirty >= bmds->total_sectors);
431 monitor_printf(mon, "Error reading sector %" PRId64 "\n", sector);
432 qemu_file_set_error(f, ret);
438 static int blk_mig_save_dirty_block(Monitor *mon, QEMUFile *f, int is_async)
440 BlkMigDevState *bmds;
443 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
444 if (mig_save_device_dirty(mon, f, bmds, is_async) == 0) {
453 static void flush_blks(QEMUFile* f)
457 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
458 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
459 block_mig_state.transferred);
461 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
462 if (qemu_file_rate_limit(f)) {
466 qemu_file_set_error(f, blk->ret);
471 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
475 block_mig_state.read_done--;
476 block_mig_state.transferred++;
477 assert(block_mig_state.read_done >= 0);
480 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
481 block_mig_state.submitted, block_mig_state.read_done,
482 block_mig_state.transferred);
485 static int64_t get_remaining_dirty(void)
487 BlkMigDevState *bmds;
490 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
491 dirty += bdrv_get_dirty_count(bmds->bs);
494 return dirty * BLOCK_SIZE;
497 static int is_stage2_completed(void)
499 int64_t remaining_dirty;
502 if (block_mig_state.bulk_completed == 1) {
504 remaining_dirty = get_remaining_dirty();
505 if (remaining_dirty == 0) {
509 bwidth = compute_read_bwidth();
511 if ((remaining_dirty / bwidth) <=
512 migrate_max_downtime()) {
513 /* finish stage2 because we think that we can finish remaining work
514 below max_downtime */
523 static void blk_mig_cleanup(Monitor *mon)
525 BlkMigDevState *bmds;
528 set_dirty_tracking(0);
530 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
531 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
532 bdrv_set_in_use(bmds->bs, 0);
533 drive_put_ref(drive_get_by_blockdev(bmds->bs));
534 g_free(bmds->aio_bitmap);
538 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
539 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
544 monitor_printf(mon, "\n");
547 static int block_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
551 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
552 stage, block_mig_state.submitted, block_mig_state.transferred);
555 blk_mig_cleanup(mon);
559 if (block_mig_state.blk_enable != 1) {
560 /* no need to migrate storage */
561 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
566 init_blk_migration(mon, f);
568 /* start track dirty blocks */
569 set_dirty_tracking(1);
574 ret = qemu_file_get_error(f);
576 blk_mig_cleanup(mon);
580 blk_mig_reset_dirty_cursor();
583 /* control the rate of transfer */
584 while ((block_mig_state.submitted +
585 block_mig_state.read_done) * BLOCK_SIZE <
586 qemu_file_get_rate_limit(f)) {
587 if (block_mig_state.bulk_completed == 0) {
588 /* first finish the bulk phase */
589 if (blk_mig_save_bulked_block(mon, f) == 0) {
590 /* finished saving bulk on all devices */
591 block_mig_state.bulk_completed = 1;
594 if (blk_mig_save_dirty_block(mon, f, 1) == 0) {
595 /* no more dirty blocks */
603 ret = qemu_file_get_error(f);
605 blk_mig_cleanup(mon);
611 /* we know for sure that save bulk is completed and
612 all async read completed */
613 assert(block_mig_state.submitted == 0);
615 while (blk_mig_save_dirty_block(mon, f, 0) != 0);
616 blk_mig_cleanup(mon);
618 /* report completion */
619 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
621 ret = qemu_file_get_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, *bs_prev = NULL;
642 int64_t total_sectors = 0;
647 addr = qemu_get_be64(f);
649 flags = addr & ~BDRV_SECTOR_MASK;
650 addr >>= BDRV_SECTOR_BITS;
652 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
653 /* get device name */
654 len = qemu_get_byte(f);
655 qemu_get_buffer(f, (uint8_t *)device_name, len);
656 device_name[len] = '\0';
658 bs = bdrv_find(device_name);
660 fprintf(stderr, "Error unknown block device %s\n",
667 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
668 if (total_sectors <= 0) {
669 error_report("Error getting length of block device %s",
675 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
676 nr_sectors = total_sectors - addr;
678 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
681 buf = g_malloc(BLOCK_SIZE);
683 qemu_get_buffer(f, buf, BLOCK_SIZE);
684 ret = bdrv_write(bs, addr, buf, nr_sectors);
690 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
691 if (!banner_printed) {
692 printf("Receiving block device images\n");
695 printf("Completed %d %%%c", (int)addr,
696 (addr == 100) ? '\n' : '\r');
698 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
699 fprintf(stderr, "Unknown flags\n");
702 ret = qemu_file_get_error(f);
706 } while (!(flags & BLK_MIG_FLAG_EOS));
711 static void block_set_params(int blk_enable, int shared_base, void *opaque)
713 block_mig_state.blk_enable = blk_enable;
714 block_mig_state.shared_base = shared_base;
716 /* shared base means that blk_enable = 1 */
717 block_mig_state.blk_enable |= shared_base;
720 void blk_mig_init(void)
722 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
723 QSIMPLEQ_INIT(&block_mig_state.blk_list);
725 register_savevm_live(NULL, "block", 0, 1, block_set_params,
726 block_save_live, NULL, block_load, &block_mig_state);