qtest: Use strtoull() for uint64_t
[qemu/pbrook.git] / block-migration.c
blobbcd00390257a0d2d6179f34f987644b34597c1ad
1 /*
2 * QEMU live block migration
4 * Copyright IBM, Corp. 2009
6 * Authors:
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.
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"
18 #include "hw/hw.h"
19 #include "qemu/queue.h"
20 #include "qemu/timer.h"
21 #include "migration/block.h"
22 #include "migration/migration.h"
23 #include "sysemu/blockdev.h"
24 #include <assert.h>
26 #define BLOCK_SIZE (1 << 20)
27 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> 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)
40 #else
41 #define DPRINTF(fmt, ...) \
42 do { } while (0)
43 #endif
45 typedef struct BlkMigDevState {
46 BlockDriverState *bs;
47 int bulk_completed;
48 int shared_base;
49 int64_t cur_sector;
50 int64_t cur_dirty;
51 int64_t completed_sectors;
52 int64_t total_sectors;
53 int64_t dirty;
54 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
55 unsigned long *aio_bitmap;
56 } BlkMigDevState;
58 typedef struct BlkMigBlock {
59 uint8_t *buf;
60 BlkMigDevState *bmds;
61 int64_t sector;
62 int nr_sectors;
63 struct iovec iov;
64 QEMUIOVector qiov;
65 BlockDriverAIOCB *aiocb;
66 int ret;
67 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
68 } BlkMigBlock;
70 typedef struct BlkMigState {
71 int blk_enable;
72 int shared_base;
73 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
74 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
75 int submitted;
76 int read_done;
77 int transferred;
78 int64_t total_sector_sum;
79 int prev_progress;
80 int bulk_completed;
81 long double prev_time_offset;
82 } BlkMigState;
84 static BlkMigState block_mig_state;
86 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
88 int len;
90 /* sector number and flags */
91 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
92 | BLK_MIG_FLAG_DEVICE_BLOCK);
94 /* device name */
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;
110 uint64_t sum = 0;
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;
126 uint64_t sum = 0;
128 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
129 sum += bmds->total_sectors;
131 return sum << BDRV_SECTOR_BITS;
134 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
136 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
138 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
139 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
140 (1UL << (chunk % (sizeof(unsigned long) * 8))));
141 } else {
142 return 0;
146 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
147 int nb_sectors, int set)
149 int64_t start, end;
150 unsigned long val, idx, bit;
152 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
153 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
155 for (; start <= end; start++) {
156 idx = start / (sizeof(unsigned long) * 8);
157 bit = start % (sizeof(unsigned long) * 8);
158 val = bmds->aio_bitmap[idx];
159 if (set) {
160 val |= 1UL << bit;
161 } else {
162 val &= ~(1UL << bit);
164 bmds->aio_bitmap[idx] = val;
168 static void alloc_aio_bitmap(BlkMigDevState *bmds)
170 BlockDriverState *bs = bmds->bs;
171 int64_t bitmap_size;
173 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
174 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
175 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
177 bmds->aio_bitmap = g_malloc0(bitmap_size);
180 static void blk_mig_read_cb(void *opaque, int ret)
182 long double curr_time = qemu_get_clock_ns(rt_clock);
183 BlkMigBlock *blk = opaque;
185 blk->ret = ret;
187 block_mig_state.prev_time_offset = curr_time;
189 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
190 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
192 block_mig_state.submitted--;
193 block_mig_state.read_done++;
194 assert(block_mig_state.submitted >= 0);
197 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
199 int64_t total_sectors = bmds->total_sectors;
200 int64_t cur_sector = bmds->cur_sector;
201 BlockDriverState *bs = bmds->bs;
202 BlkMigBlock *blk;
203 int nr_sectors;
205 if (bmds->shared_base) {
206 while (cur_sector < total_sectors &&
207 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
208 &nr_sectors)) {
209 cur_sector += nr_sectors;
213 if (cur_sector >= total_sectors) {
214 bmds->cur_sector = bmds->completed_sectors = total_sectors;
215 return 1;
218 bmds->completed_sectors = cur_sector;
220 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
222 /* we are going to transfer a full block even if it is not allocated */
223 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
225 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
226 nr_sectors = total_sectors - cur_sector;
229 blk = g_malloc(sizeof(BlkMigBlock));
230 blk->buf = g_malloc(BLOCK_SIZE);
231 blk->bmds = bmds;
232 blk->sector = cur_sector;
233 blk->nr_sectors = nr_sectors;
235 blk->iov.iov_base = blk->buf;
236 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
237 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
239 if (block_mig_state.submitted == 0) {
240 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
243 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
244 nr_sectors, blk_mig_read_cb, blk);
245 block_mig_state.submitted++;
247 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
248 bmds->cur_sector = cur_sector + nr_sectors;
250 return (bmds->cur_sector >= total_sectors);
253 static void set_dirty_tracking(int enable)
255 BlkMigDevState *bmds;
257 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
258 bdrv_set_dirty_tracking(bmds->bs, enable ? BLOCK_SIZE : 0);
262 static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
264 BlkMigDevState *bmds;
265 int64_t sectors;
267 if (!bdrv_is_read_only(bs)) {
268 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
269 if (sectors <= 0) {
270 return;
273 bmds = g_malloc0(sizeof(BlkMigDevState));
274 bmds->bs = bs;
275 bmds->bulk_completed = 0;
276 bmds->total_sectors = sectors;
277 bmds->completed_sectors = 0;
278 bmds->shared_base = block_mig_state.shared_base;
279 alloc_aio_bitmap(bmds);
280 drive_get_ref(drive_get_by_blockdev(bs));
281 bdrv_set_in_use(bs, 1);
283 block_mig_state.total_sector_sum += sectors;
285 if (bmds->shared_base) {
286 DPRINTF("Start migration for %s with shared base image\n",
287 bs->device_name);
288 } else {
289 DPRINTF("Start full migration for %s\n", bs->device_name);
292 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
296 static void init_blk_migration(QEMUFile *f)
298 block_mig_state.submitted = 0;
299 block_mig_state.read_done = 0;
300 block_mig_state.transferred = 0;
301 block_mig_state.total_sector_sum = 0;
302 block_mig_state.prev_progress = -1;
303 block_mig_state.bulk_completed = 0;
305 bdrv_iterate(init_blk_migration_it, NULL);
308 static int blk_mig_save_bulked_block(QEMUFile *f)
310 int64_t completed_sector_sum = 0;
311 BlkMigDevState *bmds;
312 int progress;
313 int ret = 0;
315 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
316 if (bmds->bulk_completed == 0) {
317 if (mig_save_device_bulk(f, bmds) == 1) {
318 /* completed bulk section for this device */
319 bmds->bulk_completed = 1;
321 completed_sector_sum += bmds->completed_sectors;
322 ret = 1;
323 break;
324 } else {
325 completed_sector_sum += bmds->completed_sectors;
329 if (block_mig_state.total_sector_sum != 0) {
330 progress = completed_sector_sum * 100 /
331 block_mig_state.total_sector_sum;
332 } else {
333 progress = 100;
335 if (progress != block_mig_state.prev_progress) {
336 block_mig_state.prev_progress = progress;
337 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
338 | BLK_MIG_FLAG_PROGRESS);
339 DPRINTF("Completed %d %%\r", progress);
342 return ret;
345 static void blk_mig_reset_dirty_cursor(void)
347 BlkMigDevState *bmds;
349 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
350 bmds->cur_dirty = 0;
354 static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
355 int is_async)
357 BlkMigBlock *blk;
358 int64_t total_sectors = bmds->total_sectors;
359 int64_t sector;
360 int nr_sectors;
361 int ret = -EIO;
363 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
364 if (bmds_aio_inflight(bmds, sector)) {
365 bdrv_drain_all();
367 if (bdrv_get_dirty(bmds->bs, sector)) {
369 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
370 nr_sectors = total_sectors - sector;
371 } else {
372 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
374 blk = g_malloc(sizeof(BlkMigBlock));
375 blk->buf = g_malloc(BLOCK_SIZE);
376 blk->bmds = bmds;
377 blk->sector = sector;
378 blk->nr_sectors = nr_sectors;
380 if (is_async) {
381 blk->iov.iov_base = blk->buf;
382 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
383 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
385 if (block_mig_state.submitted == 0) {
386 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
389 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
390 nr_sectors, blk_mig_read_cb, blk);
391 block_mig_state.submitted++;
392 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
393 } else {
394 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
395 if (ret < 0) {
396 goto error;
398 blk_send(f, blk);
400 g_free(blk->buf);
401 g_free(blk);
404 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
405 break;
407 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
408 bmds->cur_dirty = sector;
411 return (bmds->cur_dirty >= bmds->total_sectors);
413 error:
414 DPRINTF("Error reading sector %" PRId64 "\n", sector);
415 g_free(blk->buf);
416 g_free(blk);
417 return ret;
420 /* return value:
421 * 0: too much data for max_downtime
422 * 1: few enough data for max_downtime
424 static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
426 BlkMigDevState *bmds;
427 int ret = 1;
429 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
430 ret = mig_save_device_dirty(f, bmds, is_async);
431 if (ret <= 0) {
432 break;
436 return ret;
439 static int flush_blks(QEMUFile *f)
441 BlkMigBlock *blk;
442 int ret = 0;
444 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
445 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
446 block_mig_state.transferred);
448 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
449 if (qemu_file_rate_limit(f)) {
450 break;
452 if (blk->ret < 0) {
453 ret = blk->ret;
454 break;
456 blk_send(f, blk);
458 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
459 g_free(blk->buf);
460 g_free(blk);
462 block_mig_state.read_done--;
463 block_mig_state.transferred++;
464 assert(block_mig_state.read_done >= 0);
467 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
468 block_mig_state.submitted, block_mig_state.read_done,
469 block_mig_state.transferred);
470 return ret;
473 static int64_t get_remaining_dirty(void)
475 BlkMigDevState *bmds;
476 int64_t dirty = 0;
478 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
479 dirty += bdrv_get_dirty_count(bmds->bs);
482 return dirty << BDRV_SECTOR_BITS;
485 static void blk_mig_cleanup(void)
487 BlkMigDevState *bmds;
488 BlkMigBlock *blk;
490 bdrv_drain_all();
492 set_dirty_tracking(0);
494 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
495 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
496 bdrv_set_in_use(bmds->bs, 0);
497 drive_put_ref(drive_get_by_blockdev(bmds->bs));
498 g_free(bmds->aio_bitmap);
499 g_free(bmds);
502 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
503 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
504 g_free(blk->buf);
505 g_free(blk);
509 static void block_migration_cancel(void *opaque)
511 blk_mig_cleanup();
514 static int block_save_setup(QEMUFile *f, void *opaque)
516 int ret;
518 DPRINTF("Enter save live setup submitted %d transferred %d\n",
519 block_mig_state.submitted, block_mig_state.transferred);
521 init_blk_migration(f);
523 /* start track dirty blocks */
524 set_dirty_tracking(1);
526 ret = flush_blks(f);
527 if (ret) {
528 blk_mig_cleanup();
529 return ret;
532 blk_mig_reset_dirty_cursor();
534 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
536 return 0;
539 static int block_save_iterate(QEMUFile *f, void *opaque)
541 int ret;
543 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
544 block_mig_state.submitted, block_mig_state.transferred);
546 ret = flush_blks(f);
547 if (ret) {
548 blk_mig_cleanup();
549 return ret;
552 blk_mig_reset_dirty_cursor();
554 /* control the rate of transfer */
555 while ((block_mig_state.submitted +
556 block_mig_state.read_done) * BLOCK_SIZE <
557 qemu_file_get_rate_limit(f)) {
558 if (block_mig_state.bulk_completed == 0) {
559 /* first finish the bulk phase */
560 if (blk_mig_save_bulked_block(f) == 0) {
561 /* finished saving bulk on all devices */
562 block_mig_state.bulk_completed = 1;
564 } else {
565 ret = blk_mig_save_dirty_block(f, 1);
566 if (ret != 0) {
567 /* no more dirty blocks */
568 break;
572 if (ret < 0) {
573 blk_mig_cleanup();
574 return ret;
577 ret = flush_blks(f);
578 if (ret) {
579 blk_mig_cleanup();
580 return ret;
583 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
585 /* Complete when bulk transfer is done and all dirty blocks have been
586 * transferred.
588 return block_mig_state.bulk_completed &&
589 block_mig_state.submitted == 0 &&
590 block_mig_state.read_done == 0;
593 static int block_save_complete(QEMUFile *f, void *opaque)
595 int ret;
597 DPRINTF("Enter save live complete submitted %d transferred %d\n",
598 block_mig_state.submitted, block_mig_state.transferred);
600 ret = flush_blks(f);
601 if (ret) {
602 blk_mig_cleanup();
603 return ret;
606 blk_mig_reset_dirty_cursor();
608 /* we know for sure that save bulk is completed and
609 all async read completed */
610 assert(block_mig_state.submitted == 0);
612 do {
613 ret = blk_mig_save_dirty_block(f, 0);
614 } while (ret == 0);
616 blk_mig_cleanup();
617 if (ret < 0) {
618 return ret;
620 /* report completion */
621 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
623 DPRINTF("Block migration completed\n");
625 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
627 return 0;
630 static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
633 DPRINTF("Enter save live pending %ld\n", get_remaining_dirty());
635 return get_remaining_dirty();
638 static int block_load(QEMUFile *f, void *opaque, int version_id)
640 static int banner_printed;
641 int len, flags;
642 char device_name[256];
643 int64_t addr;
644 BlockDriverState *bs, *bs_prev = NULL;
645 uint8_t *buf;
646 int64_t total_sectors = 0;
647 int nr_sectors;
648 int ret;
650 do {
651 addr = qemu_get_be64(f);
653 flags = addr & ~BDRV_SECTOR_MASK;
654 addr >>= BDRV_SECTOR_BITS;
656 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
657 /* get device name */
658 len = qemu_get_byte(f);
659 qemu_get_buffer(f, (uint8_t *)device_name, len);
660 device_name[len] = '\0';
662 bs = bdrv_find(device_name);
663 if (!bs) {
664 fprintf(stderr, "Error unknown block device %s\n",
665 device_name);
666 return -EINVAL;
669 if (bs != bs_prev) {
670 bs_prev = bs;
671 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
672 if (total_sectors <= 0) {
673 error_report("Error getting length of block device %s",
674 device_name);
675 return -EINVAL;
679 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
680 nr_sectors = total_sectors - addr;
681 } else {
682 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
685 buf = g_malloc(BLOCK_SIZE);
687 qemu_get_buffer(f, buf, BLOCK_SIZE);
688 ret = bdrv_write(bs, addr, buf, nr_sectors);
690 g_free(buf);
691 if (ret < 0) {
692 return ret;
694 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
695 if (!banner_printed) {
696 printf("Receiving block device images\n");
697 banner_printed = 1;
699 printf("Completed %d %%%c", (int)addr,
700 (addr == 100) ? '\n' : '\r');
701 fflush(stdout);
702 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
703 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
704 return -EINVAL;
706 ret = qemu_file_get_error(f);
707 if (ret != 0) {
708 return ret;
710 } while (!(flags & BLK_MIG_FLAG_EOS));
712 return 0;
715 static void block_set_params(const MigrationParams *params, void *opaque)
717 block_mig_state.blk_enable = params->blk;
718 block_mig_state.shared_base = params->shared;
720 /* shared base means that blk_enable = 1 */
721 block_mig_state.blk_enable |= params->shared;
724 static bool block_is_active(void *opaque)
726 return block_mig_state.blk_enable == 1;
729 SaveVMHandlers savevm_block_handlers = {
730 .set_params = block_set_params,
731 .save_live_setup = block_save_setup,
732 .save_live_iterate = block_save_iterate,
733 .save_live_complete = block_save_complete,
734 .save_live_pending = block_save_pending,
735 .load_state = block_load,
736 .cancel = block_migration_cancel,
737 .is_active = block_is_active,
740 void blk_mig_init(void)
742 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
743 QSIMPLEQ_INIT(&block_mig_state.blk_list);
745 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
746 &block_mig_state);