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
27 #define MAX_IS_ALLOCATED_SEARCH 65536
28 #define MAX_BLOCKS_READ 10000
29 #define BLOCKS_READ_CHANGE 100
30 #define INITIAL_BLOCKS_READ 100
32 //#define DEBUG_BLK_MIGRATION
34 #ifdef DEBUG_BLK_MIGRATION
35 #define dprintf(fmt, ...) \
36 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
38 #define dprintf(fmt, ...) \
42 typedef struct BlkMigDevState
{
47 int64_t completed_sectors
;
48 int64_t total_sectors
;
50 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
53 typedef struct BlkMigBlock
{
59 BlockDriverAIOCB
*aiocb
;
61 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
64 typedef struct BlkMigState
{
67 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
68 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
72 int64_t total_sector_sum
;
73 int64_t print_completion
;
76 static BlkMigState block_mig_state
;
78 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
82 /* sector number and flags */
83 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
84 | BLK_MIG_FLAG_DEVICE_BLOCK
);
87 len
= strlen(blk
->bmds
->bs
->device_name
);
88 qemu_put_byte(f
, len
);
89 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
91 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
94 int blk_mig_active(void)
96 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
99 uint64_t blk_mig_bytes_transferred(void)
101 BlkMigDevState
*bmds
;
104 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
105 sum
+= bmds
->completed_sectors
;
107 return sum
<< BDRV_SECTOR_BITS
;
110 uint64_t blk_mig_bytes_remaining(void)
112 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
115 uint64_t blk_mig_bytes_total(void)
117 BlkMigDevState
*bmds
;
120 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
121 sum
+= bmds
->total_sectors
;
123 return sum
<< BDRV_SECTOR_BITS
;
126 static void blk_mig_read_cb(void *opaque
, int ret
)
128 BlkMigBlock
*blk
= opaque
;
132 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
134 block_mig_state
.submitted
--;
135 block_mig_state
.read_done
++;
136 assert(block_mig_state
.submitted
>= 0);
139 static int mig_save_device_bulk(Monitor
*mon
, QEMUFile
*f
,
140 BlkMigDevState
*bmds
, int is_async
)
142 int64_t total_sectors
= bmds
->total_sectors
;
143 int64_t cur_sector
= bmds
->cur_sector
;
144 BlockDriverState
*bs
= bmds
->bs
;
148 if (bmds
->shared_base
) {
149 while (cur_sector
< total_sectors
&&
150 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
152 cur_sector
+= nr_sectors
;
156 if (cur_sector
>= total_sectors
) {
157 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
161 bmds
->completed_sectors
= cur_sector
;
163 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
165 /* we are going to transfer a full block even if it is not allocated */
166 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
168 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
169 nr_sectors
= total_sectors
- cur_sector
;
172 blk
= qemu_malloc(sizeof(BlkMigBlock
));
173 blk
->buf
= qemu_malloc(BLOCK_SIZE
);
175 blk
->sector
= cur_sector
;
178 blk
->iov
.iov_base
= blk
->buf
;
179 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
180 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
182 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
183 nr_sectors
, blk_mig_read_cb
, blk
);
187 block_mig_state
.submitted
++;
189 if (bdrv_read(bs
, cur_sector
, blk
->buf
, nr_sectors
) < 0) {
198 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
199 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
201 return (bmds
->cur_sector
>= total_sectors
);
204 monitor_printf(mon
, "Error reading sector %" PRId64
"\n", cur_sector
);
205 qemu_file_set_error(f
);
211 static void set_dirty_tracking(int enable
)
213 BlkMigDevState
*bmds
;
215 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
216 bdrv_set_dirty_tracking(bmds
->bs
, enable
);
220 static void init_blk_migration(Monitor
*mon
, QEMUFile
*f
)
222 BlkMigDevState
*bmds
;
223 BlockDriverState
*bs
;
225 block_mig_state
.submitted
= 0;
226 block_mig_state
.read_done
= 0;
227 block_mig_state
.transferred
= 0;
228 block_mig_state
.total_sector_sum
= 0;
229 block_mig_state
.print_completion
= 0;
231 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
232 if (bs
->type
== BDRV_TYPE_HD
) {
233 bmds
= qemu_mallocz(sizeof(BlkMigDevState
));
235 bmds
->bulk_completed
= 0;
236 bmds
->total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
237 bmds
->completed_sectors
= 0;
238 bmds
->shared_base
= block_mig_state
.shared_base
;
240 block_mig_state
.total_sector_sum
+= bmds
->total_sectors
;
242 if (bmds
->shared_base
) {
243 monitor_printf(mon
, "Start migration for %s with shared base "
247 monitor_printf(mon
, "Start full migration for %s\n",
251 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
256 static int blk_mig_save_bulked_block(Monitor
*mon
, QEMUFile
*f
, int is_async
)
258 int64_t completed_sector_sum
= 0;
259 BlkMigDevState
*bmds
;
262 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
263 if (bmds
->bulk_completed
== 0) {
264 if (mig_save_device_bulk(mon
, f
, bmds
, is_async
) == 1) {
265 /* completed bulk section for this device */
266 bmds
->bulk_completed
= 1;
268 completed_sector_sum
+= bmds
->completed_sectors
;
272 completed_sector_sum
+= bmds
->completed_sectors
;
276 if (completed_sector_sum
>= block_mig_state
.print_completion
) {
277 monitor_printf(mon
, "Completed %" PRId64
" %%\r",
278 completed_sector_sum
* 100 /
279 block_mig_state
.total_sector_sum
);
281 block_mig_state
.print_completion
+=
282 (BDRV_SECTORS_PER_DIRTY_CHUNK
* 10000);
288 #define MAX_NUM_BLOCKS 4
290 static void blk_mig_save_dirty_blocks(Monitor
*mon
, QEMUFile
*f
)
292 BlkMigDevState
*bmds
;
296 blk
.buf
= qemu_malloc(BLOCK_SIZE
);
298 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
299 for (sector
= 0; sector
< bmds
->cur_sector
;) {
300 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
301 if (bdrv_read(bmds
->bs
, sector
, blk
.buf
,
302 BDRV_SECTORS_PER_DIRTY_CHUNK
) < 0) {
303 monitor_printf(mon
, "Error reading sector %" PRId64
"\n",
305 qemu_file_set_error(f
);
313 bdrv_reset_dirty(bmds
->bs
, sector
,
314 BDRV_SECTORS_PER_DIRTY_CHUNK
);
316 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
323 static void flush_blks(QEMUFile
* f
)
327 dprintf("%s Enter submitted %d read_done %d transferred %d\n",
328 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
329 block_mig_state
.transferred
);
331 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
332 if (qemu_file_rate_limit(f
)) {
336 qemu_file_set_error(f
);
341 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
345 block_mig_state
.read_done
--;
346 block_mig_state
.transferred
++;
347 assert(block_mig_state
.read_done
>= 0);
350 dprintf("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
351 block_mig_state
.submitted
, block_mig_state
.read_done
,
352 block_mig_state
.transferred
);
355 static int is_stage2_completed(void)
357 BlkMigDevState
*bmds
;
359 if (block_mig_state
.submitted
> 0) {
363 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
364 if (bmds
->bulk_completed
== 0) {
372 static void blk_mig_cleanup(Monitor
*mon
)
374 BlkMigDevState
*bmds
;
377 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
378 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
382 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
383 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
388 set_dirty_tracking(0);
390 monitor_printf(mon
, "\n");
393 static int block_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
395 dprintf("Enter save live stage %d submitted %d transferred %d\n",
396 stage
, block_mig_state
.submitted
, block_mig_state
.transferred
);
399 blk_mig_cleanup(mon
);
403 if (block_mig_state
.blk_enable
!= 1) {
404 /* no need to migrate storage */
405 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
410 init_blk_migration(mon
, f
);
412 /* start track dirty blocks */
413 set_dirty_tracking(1);
418 if (qemu_file_has_error(f
)) {
419 blk_mig_cleanup(mon
);
423 /* control the rate of transfer */
424 while ((block_mig_state
.submitted
+
425 block_mig_state
.read_done
) * BLOCK_SIZE
<
426 qemu_file_get_rate_limit(f
)) {
427 if (blk_mig_save_bulked_block(mon
, f
, 1) == 0) {
428 /* no more bulk blocks for now */
435 if (qemu_file_has_error(f
)) {
436 blk_mig_cleanup(mon
);
441 while (blk_mig_save_bulked_block(mon
, f
, 0) != 0) {
445 blk_mig_save_dirty_blocks(mon
, f
);
446 blk_mig_cleanup(mon
);
448 if (qemu_file_has_error(f
)) {
452 monitor_printf(mon
, "Block migration completed\n");
455 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
457 return ((stage
== 2) && is_stage2_completed());
460 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
463 char device_name
[256];
465 BlockDriverState
*bs
;
469 addr
= qemu_get_be64(f
);
471 flags
= addr
& ~BDRV_SECTOR_MASK
;
472 addr
>>= BDRV_SECTOR_BITS
;
474 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
475 /* get device name */
476 len
= qemu_get_byte(f
);
477 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
478 device_name
[len
] = '\0';
480 bs
= bdrv_find(device_name
);
482 fprintf(stderr
, "Error unknown block device %s\n",
487 buf
= qemu_malloc(BLOCK_SIZE
);
489 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
490 bdrv_write(bs
, addr
, buf
, BDRV_SECTORS_PER_DIRTY_CHUNK
);
493 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
494 fprintf(stderr
, "Unknown flags\n");
497 if (qemu_file_has_error(f
)) {
500 } while (!(flags
& BLK_MIG_FLAG_EOS
));
505 static void block_set_params(int blk_enable
, int shared_base
, void *opaque
)
507 block_mig_state
.blk_enable
= blk_enable
;
508 block_mig_state
.shared_base
= shared_base
;
510 /* shared base means that blk_enable = 1 */
511 block_mig_state
.blk_enable
|= shared_base
;
514 void blk_mig_init(void)
516 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
517 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
519 register_savevm_live("block", 0, 1, block_set_params
, block_save_live
,
520 NULL
, block_load
, &block_mig_state
);