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
29 #define MAX_BLOCKS_READ 10000
30 #define BLOCKS_READ_CHANGE 100
31 #define INITIAL_BLOCKS_READ 100
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
{
48 int64_t completed_sectors
;
49 int64_t total_sectors
;
51 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
54 typedef struct BlkMigBlock
{
60 BlockDriverAIOCB
*aiocb
;
62 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
65 typedef struct BlkMigState
{
68 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
69 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
73 int64_t total_sector_sum
;
77 static BlkMigState block_mig_state
;
79 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
83 /* sector number and flags */
84 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
85 | BLK_MIG_FLAG_DEVICE_BLOCK
);
88 len
= strlen(blk
->bmds
->bs
->device_name
);
89 qemu_put_byte(f
, len
);
90 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
92 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
95 int blk_mig_active(void)
97 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
100 uint64_t blk_mig_bytes_transferred(void)
102 BlkMigDevState
*bmds
;
105 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
106 sum
+= bmds
->completed_sectors
;
108 return sum
<< BDRV_SECTOR_BITS
;
111 uint64_t blk_mig_bytes_remaining(void)
113 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
116 uint64_t blk_mig_bytes_total(void)
118 BlkMigDevState
*bmds
;
121 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
122 sum
+= bmds
->total_sectors
;
124 return sum
<< BDRV_SECTOR_BITS
;
127 static void blk_mig_read_cb(void *opaque
, int ret
)
129 BlkMigBlock
*blk
= opaque
;
133 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
135 block_mig_state
.submitted
--;
136 block_mig_state
.read_done
++;
137 assert(block_mig_state
.submitted
>= 0);
140 static int mig_save_device_bulk(Monitor
*mon
, QEMUFile
*f
,
141 BlkMigDevState
*bmds
, int is_async
)
143 int64_t total_sectors
= bmds
->total_sectors
;
144 int64_t cur_sector
= bmds
->cur_sector
;
145 BlockDriverState
*bs
= bmds
->bs
;
149 if (bmds
->shared_base
) {
150 while (cur_sector
< total_sectors
&&
151 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
153 cur_sector
+= nr_sectors
;
157 if (cur_sector
>= total_sectors
) {
158 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
162 bmds
->completed_sectors
= cur_sector
;
164 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
166 /* we are going to transfer a full block even if it is not allocated */
167 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
169 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
170 nr_sectors
= total_sectors
- cur_sector
;
173 blk
= qemu_malloc(sizeof(BlkMigBlock
));
174 blk
->buf
= qemu_malloc(BLOCK_SIZE
);
176 blk
->sector
= cur_sector
;
179 blk
->iov
.iov_base
= blk
->buf
;
180 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
181 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
183 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
184 nr_sectors
, blk_mig_read_cb
, blk
);
188 block_mig_state
.submitted
++;
190 if (bdrv_read(bs
, cur_sector
, blk
->buf
, nr_sectors
) < 0) {
199 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
200 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
202 return (bmds
->cur_sector
>= total_sectors
);
205 monitor_printf(mon
, "Error reading sector %" PRId64
"\n", cur_sector
);
206 qemu_file_set_error(f
);
212 static void set_dirty_tracking(int enable
)
214 BlkMigDevState
*bmds
;
216 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
217 bdrv_set_dirty_tracking(bmds
->bs
, enable
);
221 static void init_blk_migration(Monitor
*mon
, QEMUFile
*f
)
223 BlkMigDevState
*bmds
;
224 BlockDriverState
*bs
;
227 block_mig_state
.submitted
= 0;
228 block_mig_state
.read_done
= 0;
229 block_mig_state
.transferred
= 0;
230 block_mig_state
.total_sector_sum
= 0;
231 block_mig_state
.prev_progress
= -1;
233 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
234 if (bs
->type
== BDRV_TYPE_HD
) {
235 sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
240 bmds
= qemu_mallocz(sizeof(BlkMigDevState
));
242 bmds
->bulk_completed
= 0;
243 bmds
->total_sectors
= sectors
;
244 bmds
->completed_sectors
= 0;
245 bmds
->shared_base
= block_mig_state
.shared_base
;
247 block_mig_state
.total_sector_sum
+= sectors
;
249 if (bmds
->shared_base
) {
250 monitor_printf(mon
, "Start migration for %s with shared base "
254 monitor_printf(mon
, "Start full migration for %s\n",
258 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
263 static int blk_mig_save_bulked_block(Monitor
*mon
, QEMUFile
*f
, int is_async
)
265 int64_t completed_sector_sum
= 0;
266 BlkMigDevState
*bmds
;
270 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
271 if (bmds
->bulk_completed
== 0) {
272 if (mig_save_device_bulk(mon
, f
, bmds
, is_async
) == 1) {
273 /* completed bulk section for this device */
274 bmds
->bulk_completed
= 1;
276 completed_sector_sum
+= bmds
->completed_sectors
;
280 completed_sector_sum
+= bmds
->completed_sectors
;
284 progress
= completed_sector_sum
* 100 / block_mig_state
.total_sector_sum
;
285 if (progress
!= block_mig_state
.prev_progress
) {
286 block_mig_state
.prev_progress
= progress
;
287 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
288 | BLK_MIG_FLAG_PROGRESS
);
289 monitor_printf(mon
, "Completed %d %%\r", progress
);
296 #define MAX_NUM_BLOCKS 4
298 static void blk_mig_save_dirty_blocks(Monitor
*mon
, QEMUFile
*f
)
300 BlkMigDevState
*bmds
;
304 blk
.buf
= qemu_malloc(BLOCK_SIZE
);
306 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
307 for (sector
= 0; sector
< bmds
->cur_sector
;) {
308 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
309 if (bdrv_read(bmds
->bs
, sector
, blk
.buf
,
310 BDRV_SECTORS_PER_DIRTY_CHUNK
) < 0) {
311 monitor_printf(mon
, "Error reading sector %" PRId64
"\n",
313 qemu_file_set_error(f
);
321 bdrv_reset_dirty(bmds
->bs
, sector
,
322 BDRV_SECTORS_PER_DIRTY_CHUNK
);
324 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
331 static void flush_blks(QEMUFile
* f
)
335 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
336 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
337 block_mig_state
.transferred
);
339 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
340 if (qemu_file_rate_limit(f
)) {
344 qemu_file_set_error(f
);
349 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
353 block_mig_state
.read_done
--;
354 block_mig_state
.transferred
++;
355 assert(block_mig_state
.read_done
>= 0);
358 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
359 block_mig_state
.submitted
, block_mig_state
.read_done
,
360 block_mig_state
.transferred
);
363 static int is_stage2_completed(void)
365 BlkMigDevState
*bmds
;
367 if (block_mig_state
.submitted
> 0) {
371 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
372 if (bmds
->bulk_completed
== 0) {
380 static void blk_mig_cleanup(Monitor
*mon
)
382 BlkMigDevState
*bmds
;
385 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
386 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
390 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
391 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
396 set_dirty_tracking(0);
398 monitor_printf(mon
, "\n");
401 static int block_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
403 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
404 stage
, block_mig_state
.submitted
, block_mig_state
.transferred
);
407 blk_mig_cleanup(mon
);
411 if (block_mig_state
.blk_enable
!= 1) {
412 /* no need to migrate storage */
413 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
418 init_blk_migration(mon
, f
);
420 /* start track dirty blocks */
421 set_dirty_tracking(1);
426 if (qemu_file_has_error(f
)) {
427 blk_mig_cleanup(mon
);
431 /* control the rate of transfer */
432 while ((block_mig_state
.submitted
+
433 block_mig_state
.read_done
) * BLOCK_SIZE
<
434 qemu_file_get_rate_limit(f
)) {
435 if (blk_mig_save_bulked_block(mon
, f
, 1) == 0) {
436 /* no more bulk blocks for now */
443 if (qemu_file_has_error(f
)) {
444 blk_mig_cleanup(mon
);
449 while (blk_mig_save_bulked_block(mon
, f
, 0) != 0) {
453 blk_mig_save_dirty_blocks(mon
, f
);
454 blk_mig_cleanup(mon
);
456 /* report completion */
457 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
459 if (qemu_file_has_error(f
)) {
463 monitor_printf(mon
, "Block migration completed\n");
466 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
468 return ((stage
== 2) && is_stage2_completed());
471 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
473 static int banner_printed
;
475 char device_name
[256];
477 BlockDriverState
*bs
;
481 addr
= qemu_get_be64(f
);
483 flags
= addr
& ~BDRV_SECTOR_MASK
;
484 addr
>>= BDRV_SECTOR_BITS
;
486 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
487 /* get device name */
488 len
= qemu_get_byte(f
);
489 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
490 device_name
[len
] = '\0';
492 bs
= bdrv_find(device_name
);
494 fprintf(stderr
, "Error unknown block device %s\n",
499 buf
= qemu_malloc(BLOCK_SIZE
);
501 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
502 bdrv_write(bs
, addr
, buf
, BDRV_SECTORS_PER_DIRTY_CHUNK
);
505 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
506 if (!banner_printed
) {
507 printf("Receiving block device images\n");
510 printf("Completed %d %%%c", (int)addr
,
511 (addr
== 100) ? '\n' : '\r');
513 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
514 fprintf(stderr
, "Unknown flags\n");
517 if (qemu_file_has_error(f
)) {
520 } while (!(flags
& BLK_MIG_FLAG_EOS
));
525 static void block_set_params(int blk_enable
, int shared_base
, void *opaque
)
527 block_mig_state
.blk_enable
= blk_enable
;
528 block_mig_state
.shared_base
= shared_base
;
530 /* shared base means that blk_enable = 1 */
531 block_mig_state
.blk_enable
|= shared_base
;
534 void blk_mig_init(void)
536 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
537 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
539 register_savevm_live("block", 0, 1, block_set_params
, block_save_live
,
540 NULL
, block_load
, &block_mig_state
);