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.
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"
23 #include "sysemu/blockdev.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)
41 #define DPRINTF(fmt, ...) \
45 typedef struct BlkMigDevState
{
46 /* Written during setup phase. Can be read without a lock. */
49 int64_t total_sectors
;
50 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
52 /* Only used by migration thread. Does not need a lock. */
57 /* Protected by block migration lock. */
58 unsigned long *aio_bitmap
;
59 int64_t completed_sectors
;
62 typedef struct BlkMigBlock
{
63 /* Only used by migration thread. */
70 BlockDriverAIOCB
*aiocb
;
72 /* Protected by block migration lock. */
74 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
77 typedef struct BlkMigState
{
78 /* Written during setup phase. Can be read without a lock. */
81 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
82 int64_t total_sector_sum
;
84 /* Protected by lock. */
85 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
89 /* Only used by migration thread. Does not need a lock. */
94 /* Lock must be taken _inside_ the iothread lock. */
98 static BlkMigState block_mig_state
;
100 static void blk_mig_lock(void)
102 qemu_mutex_lock(&block_mig_state
.lock
);
105 static void blk_mig_unlock(void)
107 qemu_mutex_unlock(&block_mig_state
.lock
);
110 /* Must run outside of the iothread lock during the bulk phase,
111 * or the VM will stall.
114 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
118 /* sector number and flags */
119 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
120 | BLK_MIG_FLAG_DEVICE_BLOCK
);
123 len
= strlen(blk
->bmds
->bs
->device_name
);
124 qemu_put_byte(f
, len
);
125 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
127 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
130 int blk_mig_active(void)
132 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
135 uint64_t blk_mig_bytes_transferred(void)
137 BlkMigDevState
*bmds
;
141 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
142 sum
+= bmds
->completed_sectors
;
145 return sum
<< BDRV_SECTOR_BITS
;
148 uint64_t blk_mig_bytes_remaining(void)
150 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
153 uint64_t blk_mig_bytes_total(void)
155 BlkMigDevState
*bmds
;
158 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
159 sum
+= bmds
->total_sectors
;
161 return sum
<< BDRV_SECTOR_BITS
;
165 /* Called with migration lock held. */
167 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
169 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
171 if ((sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bmds
->bs
)) {
172 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
173 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
179 /* Called with migration lock held. */
181 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
182 int nb_sectors
, int set
)
185 unsigned long val
, idx
, bit
;
187 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
188 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
190 for (; start
<= end
; start
++) {
191 idx
= start
/ (sizeof(unsigned long) * 8);
192 bit
= start
% (sizeof(unsigned long) * 8);
193 val
= bmds
->aio_bitmap
[idx
];
197 val
&= ~(1UL << bit
);
199 bmds
->aio_bitmap
[idx
] = val
;
203 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
205 BlockDriverState
*bs
= bmds
->bs
;
208 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
209 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
210 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
212 bmds
->aio_bitmap
= g_malloc0(bitmap_size
);
215 /* Never hold migration lock when yielding to the main loop! */
217 static void blk_mig_read_cb(void *opaque
, int ret
)
219 BlkMigBlock
*blk
= opaque
;
224 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
225 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
227 block_mig_state
.submitted
--;
228 block_mig_state
.read_done
++;
229 assert(block_mig_state
.submitted
>= 0);
233 /* Called with no lock taken. */
235 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
237 int64_t total_sectors
= bmds
->total_sectors
;
238 int64_t cur_sector
= bmds
->cur_sector
;
239 BlockDriverState
*bs
= bmds
->bs
;
243 if (bmds
->shared_base
) {
244 qemu_mutex_lock_iothread();
245 while (cur_sector
< total_sectors
&&
246 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
248 cur_sector
+= nr_sectors
;
250 qemu_mutex_unlock_iothread();
253 if (cur_sector
>= total_sectors
) {
254 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
258 bmds
->completed_sectors
= cur_sector
;
260 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
262 /* we are going to transfer a full block even if it is not allocated */
263 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
265 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
266 nr_sectors
= total_sectors
- cur_sector
;
269 blk
= g_malloc(sizeof(BlkMigBlock
));
270 blk
->buf
= g_malloc(BLOCK_SIZE
);
272 blk
->sector
= cur_sector
;
273 blk
->nr_sectors
= nr_sectors
;
275 blk
->iov
.iov_base
= blk
->buf
;
276 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
277 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
280 block_mig_state
.submitted
++;
283 qemu_mutex_lock_iothread();
284 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
285 nr_sectors
, blk_mig_read_cb
, blk
);
287 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
288 qemu_mutex_unlock_iothread();
290 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
291 return (bmds
->cur_sector
>= total_sectors
);
294 /* Called with iothread lock taken. */
296 static void set_dirty_tracking(int enable
)
298 BlkMigDevState
*bmds
;
300 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
301 bdrv_set_dirty_tracking(bmds
->bs
, enable
? BLOCK_SIZE
: 0);
305 static void init_blk_migration_it(void *opaque
, BlockDriverState
*bs
)
307 BlkMigDevState
*bmds
;
310 if (!bdrv_is_read_only(bs
)) {
311 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
316 bmds
= g_malloc0(sizeof(BlkMigDevState
));
318 bmds
->bulk_completed
= 0;
319 bmds
->total_sectors
= sectors
;
320 bmds
->completed_sectors
= 0;
321 bmds
->shared_base
= block_mig_state
.shared_base
;
322 alloc_aio_bitmap(bmds
);
323 drive_get_ref(drive_get_by_blockdev(bs
));
324 bdrv_set_in_use(bs
, 1);
326 block_mig_state
.total_sector_sum
+= sectors
;
328 if (bmds
->shared_base
) {
329 DPRINTF("Start migration for %s with shared base image\n",
332 DPRINTF("Start full migration for %s\n", bs
->device_name
);
335 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
339 static void init_blk_migration(QEMUFile
*f
)
341 block_mig_state
.submitted
= 0;
342 block_mig_state
.read_done
= 0;
343 block_mig_state
.transferred
= 0;
344 block_mig_state
.total_sector_sum
= 0;
345 block_mig_state
.prev_progress
= -1;
346 block_mig_state
.bulk_completed
= 0;
348 bdrv_iterate(init_blk_migration_it
, NULL
);
351 /* Called with no lock taken. */
353 static int blk_mig_save_bulked_block(QEMUFile
*f
)
355 int64_t completed_sector_sum
= 0;
356 BlkMigDevState
*bmds
;
360 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
361 if (bmds
->bulk_completed
== 0) {
362 if (mig_save_device_bulk(f
, bmds
) == 1) {
363 /* completed bulk section for this device */
364 bmds
->bulk_completed
= 1;
366 completed_sector_sum
+= bmds
->completed_sectors
;
370 completed_sector_sum
+= bmds
->completed_sectors
;
374 if (block_mig_state
.total_sector_sum
!= 0) {
375 progress
= completed_sector_sum
* 100 /
376 block_mig_state
.total_sector_sum
;
380 if (progress
!= block_mig_state
.prev_progress
) {
381 block_mig_state
.prev_progress
= progress
;
382 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
383 | BLK_MIG_FLAG_PROGRESS
);
384 DPRINTF("Completed %d %%\r", progress
);
390 static void blk_mig_reset_dirty_cursor(void)
392 BlkMigDevState
*bmds
;
394 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
399 /* Called with iothread lock taken. */
401 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
405 int64_t total_sectors
= bmds
->total_sectors
;
410 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
412 if (bmds_aio_inflight(bmds
, sector
)) {
418 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
420 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
421 nr_sectors
= total_sectors
- sector
;
423 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
425 blk
= g_malloc(sizeof(BlkMigBlock
));
426 blk
->buf
= g_malloc(BLOCK_SIZE
);
428 blk
->sector
= sector
;
429 blk
->nr_sectors
= nr_sectors
;
432 blk
->iov
.iov_base
= blk
->buf
;
433 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
434 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
436 blk
->aiocb
= bdrv_aio_readv(bmds
->bs
, sector
, &blk
->qiov
,
437 nr_sectors
, blk_mig_read_cb
, blk
);
440 block_mig_state
.submitted
++;
441 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
444 ret
= bdrv_read(bmds
->bs
, sector
, blk
->buf
, nr_sectors
);
454 bdrv_reset_dirty(bmds
->bs
, sector
, nr_sectors
);
457 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
458 bmds
->cur_dirty
= sector
;
461 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
464 DPRINTF("Error reading sector %" PRId64
"\n", sector
);
470 /* Called with iothread lock taken.
473 * 0: too much data for max_downtime
474 * 1: few enough data for max_downtime
476 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
478 BlkMigDevState
*bmds
;
481 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
482 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
491 /* Called with no locks taken. */
493 static int flush_blks(QEMUFile
*f
)
498 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
499 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
500 block_mig_state
.transferred
);
503 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
504 if (qemu_file_rate_limit(f
)) {
512 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
520 block_mig_state
.read_done
--;
521 block_mig_state
.transferred
++;
522 assert(block_mig_state
.read_done
>= 0);
526 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
527 block_mig_state
.submitted
, block_mig_state
.read_done
,
528 block_mig_state
.transferred
);
532 /* Called with iothread lock taken. */
534 static int64_t get_remaining_dirty(void)
536 BlkMigDevState
*bmds
;
539 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
540 dirty
+= bdrv_get_dirty_count(bmds
->bs
);
543 return dirty
<< BDRV_SECTOR_BITS
;
546 /* Called with iothread lock taken. */
548 static void blk_mig_cleanup(void)
550 BlkMigDevState
*bmds
;
555 set_dirty_tracking(0);
558 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
559 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
560 bdrv_set_in_use(bmds
->bs
, 0);
561 drive_put_ref(drive_get_by_blockdev(bmds
->bs
));
562 g_free(bmds
->aio_bitmap
);
566 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
567 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
574 static void block_migration_cancel(void *opaque
)
579 static int block_save_setup(QEMUFile
*f
, void *opaque
)
583 DPRINTF("Enter save live setup submitted %d transferred %d\n",
584 block_mig_state
.submitted
, block_mig_state
.transferred
);
586 qemu_mutex_lock_iothread();
587 init_blk_migration(f
);
589 /* start track dirty blocks */
590 set_dirty_tracking(1);
591 qemu_mutex_unlock_iothread();
594 blk_mig_reset_dirty_cursor();
595 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
600 static int block_save_iterate(QEMUFile
*f
, void *opaque
)
603 int64_t last_ftell
= qemu_ftell(f
);
605 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
606 block_mig_state
.submitted
, block_mig_state
.transferred
);
613 blk_mig_reset_dirty_cursor();
615 /* control the rate of transfer */
617 while ((block_mig_state
.submitted
+
618 block_mig_state
.read_done
) * BLOCK_SIZE
<
619 qemu_file_get_rate_limit(f
)) {
621 if (block_mig_state
.bulk_completed
== 0) {
622 /* first finish the bulk phase */
623 if (blk_mig_save_bulked_block(f
) == 0) {
624 /* finished saving bulk on all devices */
625 block_mig_state
.bulk_completed
= 1;
629 /* Always called with iothread lock taken for
630 * simplicity, block_save_complete also calls it.
632 qemu_mutex_lock_iothread();
633 ret
= blk_mig_save_dirty_block(f
, 1);
634 qemu_mutex_unlock_iothread();
641 /* no more dirty blocks */
652 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
653 return qemu_ftell(f
) - last_ftell
;
656 /* Called with iothread lock taken. */
658 static int block_save_complete(QEMUFile
*f
, void *opaque
)
662 DPRINTF("Enter save live complete submitted %d transferred %d\n",
663 block_mig_state
.submitted
, block_mig_state
.transferred
);
670 blk_mig_reset_dirty_cursor();
672 /* we know for sure that save bulk is completed and
673 all async read completed */
675 assert(block_mig_state
.submitted
== 0);
679 ret
= blk_mig_save_dirty_block(f
, 0);
685 /* report completion */
686 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
688 DPRINTF("Block migration completed\n");
690 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
696 static uint64_t block_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
)
698 /* Estimate pending number of bytes to send */
701 qemu_mutex_lock_iothread();
703 pending
= get_remaining_dirty() +
704 block_mig_state
.submitted
* BLOCK_SIZE
+
705 block_mig_state
.read_done
* BLOCK_SIZE
;
707 /* Report at least one block pending during bulk phase */
708 if (pending
== 0 && !block_mig_state
.bulk_completed
) {
709 pending
= BLOCK_SIZE
;
712 qemu_mutex_unlock_iothread();
714 DPRINTF("Enter save live pending %" PRIu64
"\n", pending
);
718 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
720 static int banner_printed
;
722 char device_name
[256];
724 BlockDriverState
*bs
, *bs_prev
= NULL
;
726 int64_t total_sectors
= 0;
731 addr
= qemu_get_be64(f
);
733 flags
= addr
& ~BDRV_SECTOR_MASK
;
734 addr
>>= BDRV_SECTOR_BITS
;
736 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
737 /* get device name */
738 len
= qemu_get_byte(f
);
739 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
740 device_name
[len
] = '\0';
742 bs
= bdrv_find(device_name
);
744 fprintf(stderr
, "Error unknown block device %s\n",
751 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
752 if (total_sectors
<= 0) {
753 error_report("Error getting length of block device %s",
759 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
760 nr_sectors
= total_sectors
- addr
;
762 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
765 buf
= g_malloc(BLOCK_SIZE
);
767 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
768 ret
= bdrv_write(bs
, addr
, buf
, nr_sectors
);
774 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
775 if (!banner_printed
) {
776 printf("Receiving block device images\n");
779 printf("Completed %d %%%c", (int)addr
,
780 (addr
== 100) ? '\n' : '\r');
782 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
783 fprintf(stderr
, "Unknown block migration flags: %#x\n", flags
);
786 ret
= qemu_file_get_error(f
);
790 } while (!(flags
& BLK_MIG_FLAG_EOS
));
795 static void block_set_params(const MigrationParams
*params
, void *opaque
)
797 block_mig_state
.blk_enable
= params
->blk
;
798 block_mig_state
.shared_base
= params
->shared
;
800 /* shared base means that blk_enable = 1 */
801 block_mig_state
.blk_enable
|= params
->shared
;
804 static bool block_is_active(void *opaque
)
806 return block_mig_state
.blk_enable
== 1;
809 SaveVMHandlers savevm_block_handlers
= {
810 .set_params
= block_set_params
,
811 .save_live_setup
= block_save_setup
,
812 .save_live_iterate
= block_save_iterate
,
813 .save_live_complete
= block_save_complete
,
814 .save_live_pending
= block_save_pending
,
815 .load_state
= block_load
,
816 .cancel
= block_migration_cancel
,
817 .is_active
= block_is_active
,
820 void blk_mig_init(void)
822 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
823 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
824 qemu_mutex_init(&block_mig_state
.lock
);
826 register_savevm_live(NULL
, "block", 0, 1, &savevm_block_handlers
,