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.
14 #include "qemu-common.h"
15 #include "block_int.h"
17 #include "qemu-queue.h"
19 #include "block-migration.h"
22 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
24 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
25 #define BLK_MIG_FLAG_EOS 0x02
26 #define BLK_MIG_FLAG_PROGRESS 0x04
28 #define MAX_IS_ALLOCATED_SEARCH 65536
30 //#define DEBUG_BLK_MIGRATION
32 #ifdef DEBUG_BLK_MIGRATION
33 #define DPRINTF(fmt, ...) \
34 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
36 #define DPRINTF(fmt, ...) \
40 typedef struct BlkMigDevState
{
45 int64_t completed_sectors
;
46 int64_t total_sectors
;
48 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
51 typedef struct BlkMigBlock
{
57 BlockDriverAIOCB
*aiocb
;
59 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
62 typedef struct BlkMigState
{
65 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
66 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
70 int64_t total_sector_sum
;
75 static BlkMigState block_mig_state
;
77 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
81 /* sector number and flags */
82 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
83 | BLK_MIG_FLAG_DEVICE_BLOCK
);
86 len
= strlen(blk
->bmds
->bs
->device_name
);
87 qemu_put_byte(f
, len
);
88 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
90 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
93 int blk_mig_active(void)
95 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
98 uint64_t blk_mig_bytes_transferred(void)
100 BlkMigDevState
*bmds
;
103 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
104 sum
+= bmds
->completed_sectors
;
106 return sum
<< BDRV_SECTOR_BITS
;
109 uint64_t blk_mig_bytes_remaining(void)
111 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
114 uint64_t blk_mig_bytes_total(void)
116 BlkMigDevState
*bmds
;
119 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
120 sum
+= bmds
->total_sectors
;
122 return sum
<< BDRV_SECTOR_BITS
;
125 static void blk_mig_read_cb(void *opaque
, int ret
)
127 BlkMigBlock
*blk
= opaque
;
131 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
133 block_mig_state
.submitted
--;
134 block_mig_state
.read_done
++;
135 assert(block_mig_state
.submitted
>= 0);
138 static int mig_save_device_bulk(Monitor
*mon
, QEMUFile
*f
,
139 BlkMigDevState
*bmds
)
141 int64_t total_sectors
= bmds
->total_sectors
;
142 int64_t cur_sector
= bmds
->cur_sector
;
143 BlockDriverState
*bs
= bmds
->bs
;
147 if (bmds
->shared_base
) {
148 while (cur_sector
< total_sectors
&&
149 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
151 cur_sector
+= nr_sectors
;
155 if (cur_sector
>= total_sectors
) {
156 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
160 bmds
->completed_sectors
= cur_sector
;
162 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
164 /* we are going to transfer a full block even if it is not allocated */
165 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
167 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
168 nr_sectors
= total_sectors
- cur_sector
;
171 blk
= qemu_malloc(sizeof(BlkMigBlock
));
172 blk
->buf
= qemu_malloc(BLOCK_SIZE
);
174 blk
->sector
= cur_sector
;
176 blk
->iov
.iov_base
= blk
->buf
;
177 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
178 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
180 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
181 nr_sectors
, blk_mig_read_cb
, blk
);
185 block_mig_state
.submitted
++;
186 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
187 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
189 return (bmds
->cur_sector
>= total_sectors
);
192 monitor_printf(mon
, "Error reading sector %" PRId64
"\n", cur_sector
);
193 qemu_file_set_error(f
);
199 static void set_dirty_tracking(int enable
)
201 BlkMigDevState
*bmds
;
203 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
204 bdrv_set_dirty_tracking(bmds
->bs
, enable
);
208 static void init_blk_migration(Monitor
*mon
, QEMUFile
*f
)
210 BlkMigDevState
*bmds
;
211 BlockDriverState
*bs
;
214 block_mig_state
.submitted
= 0;
215 block_mig_state
.read_done
= 0;
216 block_mig_state
.transferred
= 0;
217 block_mig_state
.total_sector_sum
= 0;
218 block_mig_state
.prev_progress
= -1;
219 block_mig_state
.bulk_completed
= 0;
221 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
222 if (bs
->type
== BDRV_TYPE_HD
) {
223 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
228 bmds
= qemu_mallocz(sizeof(BlkMigDevState
));
230 bmds
->bulk_completed
= 0;
231 bmds
->total_sectors
= sectors
;
232 bmds
->completed_sectors
= 0;
233 bmds
->shared_base
= block_mig_state
.shared_base
;
235 block_mig_state
.total_sector_sum
+= sectors
;
237 if (bmds
->shared_base
) {
238 monitor_printf(mon
, "Start migration for %s with shared base "
242 monitor_printf(mon
, "Start full migration for %s\n",
246 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
251 static int blk_mig_save_bulked_block(Monitor
*mon
, QEMUFile
*f
)
253 int64_t completed_sector_sum
= 0;
254 BlkMigDevState
*bmds
;
258 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
259 if (bmds
->bulk_completed
== 0) {
260 if (mig_save_device_bulk(mon
, f
, bmds
) == 1) {
261 /* completed bulk section for this device */
262 bmds
->bulk_completed
= 1;
264 completed_sector_sum
+= bmds
->completed_sectors
;
268 completed_sector_sum
+= bmds
->completed_sectors
;
272 progress
= completed_sector_sum
* 100 / block_mig_state
.total_sector_sum
;
273 if (progress
!= block_mig_state
.prev_progress
) {
274 block_mig_state
.prev_progress
= progress
;
275 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
276 | BLK_MIG_FLAG_PROGRESS
);
277 monitor_printf(mon
, "Completed %d %%\r", progress
);
284 #define MAX_NUM_BLOCKS 4
286 static void blk_mig_save_dirty_blocks(Monitor
*mon
, QEMUFile
*f
)
288 BlkMigDevState
*bmds
;
292 blk
.buf
= qemu_malloc(BLOCK_SIZE
);
294 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
295 for (sector
= 0; sector
< bmds
->cur_sector
;) {
296 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
297 if (bdrv_read(bmds
->bs
, sector
, blk
.buf
,
298 BDRV_SECTORS_PER_DIRTY_CHUNK
) < 0) {
299 monitor_printf(mon
, "Error reading sector %" PRId64
"\n",
301 qemu_file_set_error(f
);
309 bdrv_reset_dirty(bmds
->bs
, sector
,
310 BDRV_SECTORS_PER_DIRTY_CHUNK
);
312 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
319 static void flush_blks(QEMUFile
* f
)
323 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
324 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
325 block_mig_state
.transferred
);
327 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
328 if (qemu_file_rate_limit(f
)) {
332 qemu_file_set_error(f
);
337 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
341 block_mig_state
.read_done
--;
342 block_mig_state
.transferred
++;
343 assert(block_mig_state
.read_done
>= 0);
346 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
347 block_mig_state
.submitted
, block_mig_state
.read_done
,
348 block_mig_state
.transferred
);
351 static int is_stage2_completed(void)
353 return (block_mig_state
.submitted
== 0 && block_mig_state
.bulk_completed
);
356 static void blk_mig_cleanup(Monitor
*mon
)
358 BlkMigDevState
*bmds
;
361 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
362 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
366 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
367 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
372 set_dirty_tracking(0);
374 monitor_printf(mon
, "\n");
377 static int block_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
379 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
380 stage
, block_mig_state
.submitted
, block_mig_state
.transferred
);
383 blk_mig_cleanup(mon
);
387 if (block_mig_state
.blk_enable
!= 1) {
388 /* no need to migrate storage */
389 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
394 init_blk_migration(mon
, f
);
396 /* start track dirty blocks */
397 set_dirty_tracking(1);
402 if (qemu_file_has_error(f
)) {
403 blk_mig_cleanup(mon
);
407 /* control the rate of transfer */
408 while ((block_mig_state
.submitted
+
409 block_mig_state
.read_done
) * BLOCK_SIZE
<
410 qemu_file_get_rate_limit(f
)) {
411 if (blk_mig_save_bulked_block(mon
, f
) == 0) {
412 /* finished saving bulk on all devices */
413 block_mig_state
.bulk_completed
= 1;
420 if (qemu_file_has_error(f
)) {
421 blk_mig_cleanup(mon
);
426 /* we know for sure that save bulk is completed */
428 blk_mig_save_dirty_blocks(mon
, f
);
429 blk_mig_cleanup(mon
);
431 /* report completion */
432 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
434 if (qemu_file_has_error(f
)) {
438 monitor_printf(mon
, "Block migration completed\n");
441 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
443 return ((stage
== 2) && is_stage2_completed());
446 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
448 static int banner_printed
;
450 char device_name
[256];
452 BlockDriverState
*bs
;
456 addr
= qemu_get_be64(f
);
458 flags
= addr
& ~BDRV_SECTOR_MASK
;
459 addr
>>= BDRV_SECTOR_BITS
;
461 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
462 /* get device name */
463 len
= qemu_get_byte(f
);
464 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
465 device_name
[len
] = '\0';
467 bs
= bdrv_find(device_name
);
469 fprintf(stderr
, "Error unknown block device %s\n",
474 buf
= qemu_malloc(BLOCK_SIZE
);
476 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
477 bdrv_write(bs
, addr
, buf
, BDRV_SECTORS_PER_DIRTY_CHUNK
);
480 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
481 if (!banner_printed
) {
482 printf("Receiving block device images\n");
485 printf("Completed %d %%%c", (int)addr
,
486 (addr
== 100) ? '\n' : '\r');
488 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
489 fprintf(stderr
, "Unknown flags\n");
492 if (qemu_file_has_error(f
)) {
495 } while (!(flags
& BLK_MIG_FLAG_EOS
));
500 static void block_set_params(int blk_enable
, int shared_base
, void *opaque
)
502 block_mig_state
.blk_enable
= blk_enable
;
503 block_mig_state
.shared_base
= shared_base
;
505 /* shared base means that blk_enable = 1 */
506 block_mig_state
.blk_enable
|= shared_base
;
509 void blk_mig_init(void)
511 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
512 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
514 register_savevm_live("block", 0, 1, block_set_params
, block_save_live
,
515 NULL
, block_load
, &block_mig_state
);