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.
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;
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;
80 long double prev_time_offset;
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 long double compute_read_bwidth(void)
136 assert(block_mig_state.total_time != 0);
137 return (block_mig_state.reads / block_mig_state.total_time) * BLOCK_SIZE;
140 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
142 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
144 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
145 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
146 (1UL << (chunk % (sizeof(unsigned long) * 8))));
152 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
153 int nb_sectors, int set)
156 unsigned long val, idx, bit;
158 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
159 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
161 for (; start <= end; start++) {
162 idx = start / (sizeof(unsigned long) * 8);
163 bit = start % (sizeof(unsigned long) * 8);
164 val = bmds->aio_bitmap[idx];
168 val &= ~(1UL << bit);
170 bmds->aio_bitmap[idx] = val;
174 static void alloc_aio_bitmap(BlkMigDevState *bmds)
176 BlockDriverState *bs = bmds->bs;
179 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
180 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
181 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
183 bmds->aio_bitmap = g_malloc0(bitmap_size);
186 static void blk_mig_read_cb(void *opaque, int ret)
188 long double curr_time = qemu_get_clock_ns(rt_clock);
189 BlkMigBlock *blk = opaque;
193 block_mig_state.reads++;
194 block_mig_state.total_time += (curr_time - block_mig_state.prev_time_offset);
195 block_mig_state.prev_time_offset = curr_time;
197 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
198 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
200 block_mig_state.submitted--;
201 block_mig_state.read_done++;
202 assert(block_mig_state.submitted >= 0);
205 static int mig_save_device_bulk(Monitor *mon, QEMUFile *f,
206 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 Monitor *mon = opaque;
274 BlkMigDevState *bmds;
277 if (!bdrv_is_read_only(bs)) {
278 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
283 bmds = g_malloc0(sizeof(BlkMigDevState));
285 bmds->bulk_completed = 0;
286 bmds->total_sectors = sectors;
287 bmds->completed_sectors = 0;
288 bmds->shared_base = block_mig_state.shared_base;
289 alloc_aio_bitmap(bmds);
290 drive_get_ref(drive_get_by_blockdev(bs));
291 bdrv_set_in_use(bs, 1);
293 block_mig_state.total_sector_sum += sectors;
295 if (bmds->shared_base) {
296 monitor_printf(mon, "Start migration for %s with shared base "
300 monitor_printf(mon, "Start full migration for %s\n",
304 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
308 static void init_blk_migration(Monitor *mon, QEMUFile *f)
310 block_mig_state.submitted = 0;
311 block_mig_state.read_done = 0;
312 block_mig_state.transferred = 0;
313 block_mig_state.total_sector_sum = 0;
314 block_mig_state.prev_progress = -1;
315 block_mig_state.bulk_completed = 0;
316 block_mig_state.total_time = 0;
317 block_mig_state.reads = 0;
319 bdrv_iterate(init_blk_migration_it, mon);
322 static int blk_mig_save_bulked_block(Monitor *mon, QEMUFile *f)
324 int64_t completed_sector_sum = 0;
325 BlkMigDevState *bmds;
329 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
330 if (bmds->bulk_completed == 0) {
331 if (mig_save_device_bulk(mon, f, bmds) == 1) {
332 /* completed bulk section for this device */
333 bmds->bulk_completed = 1;
335 completed_sector_sum += bmds->completed_sectors;
339 completed_sector_sum += bmds->completed_sectors;
343 if (block_mig_state.total_sector_sum != 0) {
344 progress = completed_sector_sum * 100 /
345 block_mig_state.total_sector_sum;
349 if (progress != block_mig_state.prev_progress) {
350 block_mig_state.prev_progress = progress;
351 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
352 | BLK_MIG_FLAG_PROGRESS);
353 monitor_printf(mon, "Completed %d %%\r", progress);
360 static void blk_mig_reset_dirty_cursor(void)
362 BlkMigDevState *bmds;
364 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
369 static int mig_save_device_dirty(Monitor *mon, QEMUFile *f,
370 BlkMigDevState *bmds, int is_async)
373 int64_t total_sectors = bmds->total_sectors;
378 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
379 if (bmds_aio_inflight(bmds, sector)) {
382 if (bdrv_get_dirty(bmds->bs, sector)) {
384 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
385 nr_sectors = total_sectors - sector;
387 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
389 blk = g_malloc(sizeof(BlkMigBlock));
390 blk->buf = g_malloc(BLOCK_SIZE);
392 blk->sector = sector;
393 blk->nr_sectors = nr_sectors;
396 blk->iov.iov_base = blk->buf;
397 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
398 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
400 if (block_mig_state.submitted == 0) {
401 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
404 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
405 nr_sectors, blk_mig_read_cb, blk);
406 block_mig_state.submitted++;
407 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
409 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
419 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
422 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
423 bmds->cur_dirty = sector;
426 return (bmds->cur_dirty >= bmds->total_sectors);
429 monitor_printf(mon, "Error reading sector %" PRId64 "\n", sector);
430 qemu_file_set_error(f, ret);
436 static int blk_mig_save_dirty_block(Monitor *mon, QEMUFile *f, int is_async)
438 BlkMigDevState *bmds;
441 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
442 if (mig_save_device_dirty(mon, f, bmds, is_async) == 0) {
451 static void 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)) {
464 qemu_file_set_error(f, blk->ret);
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);
483 static int64_t get_remaining_dirty(void)
485 BlkMigDevState *bmds;
488 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
489 dirty += bdrv_get_dirty_count(bmds->bs);
492 return dirty * BLOCK_SIZE;
495 static int is_stage2_completed(void)
497 int64_t remaining_dirty;
500 if (block_mig_state.bulk_completed == 1) {
502 remaining_dirty = get_remaining_dirty();
503 if (remaining_dirty == 0) {
507 bwidth = compute_read_bwidth();
509 if ((remaining_dirty / bwidth) <=
510 migrate_max_downtime()) {
511 /* finish stage2 because we think that we can finish remaining work
512 below max_downtime */
521 static void blk_mig_cleanup(Monitor *mon)
523 BlkMigDevState *bmds;
526 set_dirty_tracking(0);
528 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
529 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
530 bdrv_set_in_use(bmds->bs, 0);
531 drive_put_ref(drive_get_by_blockdev(bmds->bs));
532 g_free(bmds->aio_bitmap);
536 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
537 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
542 monitor_printf(mon, "\n");
545 static int block_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
549 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
550 stage, block_mig_state.submitted, block_mig_state.transferred);
553 blk_mig_cleanup(mon);
557 if (block_mig_state.blk_enable != 1) {
558 /* no need to migrate storage */
559 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
564 init_blk_migration(mon, f);
566 /* start track dirty blocks */
567 set_dirty_tracking(1);
572 ret = qemu_file_get_error(f);
574 blk_mig_cleanup(mon);
578 blk_mig_reset_dirty_cursor();
581 /* control the rate of transfer */
582 while ((block_mig_state.submitted +
583 block_mig_state.read_done) * BLOCK_SIZE <
584 qemu_file_get_rate_limit(f)) {
585 if (block_mig_state.bulk_completed == 0) {
586 /* first finish the bulk phase */
587 if (blk_mig_save_bulked_block(mon, f) == 0) {
588 /* finished saving bulk on all devices */
589 block_mig_state.bulk_completed = 1;
592 if (blk_mig_save_dirty_block(mon, f, 1) == 0) {
593 /* no more dirty blocks */
601 ret = qemu_file_get_error(f);
603 blk_mig_cleanup(mon);
609 /* we know for sure that save bulk is completed and
610 all async read completed */
611 assert(block_mig_state.submitted == 0);
613 while (blk_mig_save_dirty_block(mon, f, 0) != 0);
614 blk_mig_cleanup(mon);
616 /* report completion */
617 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
619 ret = qemu_file_get_error(f);
624 monitor_printf(mon, "Block migration completed\n");
627 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
629 return ((stage == 2) && is_stage2_completed());
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(int blk_enable, int shared_base, void *opaque)
711 block_mig_state.blk_enable = blk_enable;
712 block_mig_state.shared_base = shared_base;
714 /* shared base means that blk_enable = 1 */
715 block_mig_state.blk_enable |= shared_base;
718 void blk_mig_init(void)
720 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
721 QSIMPLEQ_INIT(&block_mig_state.blk_list);
723 register_savevm_live(NULL, "block", 0, 1, block_set_params,
724 block_save_live, NULL, block_load, &block_mig_state);