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"
18 #include "block-migration.h"
21 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
23 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
24 #define BLK_MIG_FLAG_EOS 0x02
26 #define MAX_IS_ALLOCATED_SEARCH 65536
27 #define MAX_BLOCKS_READ 10000
28 #define BLOCKS_READ_CHANGE 100
29 #define INITIAL_BLOCKS_READ 100
31 //#define DEBUG_BLK_MIGRATION
33 #ifdef DEBUG_BLK_MIGRATION
34 #define dprintf(fmt, ...) \
35 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
37 #define dprintf(fmt, ...) \
41 typedef struct BlkMigDevState
{
46 int64_t completed_sectors
;
47 int64_t total_sectors
;
49 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
52 typedef struct BlkMigBlock
{
58 BlockDriverAIOCB
*aiocb
;
60 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
63 typedef struct BlkMigState
{
66 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
67 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
71 int64_t total_sector_sum
;
72 int64_t print_completion
;
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 static void blk_mig_read_cb(void *opaque
, int ret
)
95 BlkMigBlock
*blk
= opaque
;
99 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
101 block_mig_state
.submitted
--;
102 block_mig_state
.read_done
++;
103 assert(block_mig_state
.submitted
>= 0);
106 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
, int is_async
)
108 int64_t total_sectors
= bmds
->total_sectors
;
109 int64_t cur_sector
= bmds
->cur_sector
;
110 BlockDriverState
*bs
= bmds
->bs
;
114 if (bmds
->shared_base
) {
115 while (cur_sector
< total_sectors
&&
116 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
118 cur_sector
+= nr_sectors
;
122 if (cur_sector
>= total_sectors
) {
123 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
127 bmds
->completed_sectors
= cur_sector
;
129 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
131 /* we are going to transfer a full block even if it is not allocated */
132 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
134 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
135 nr_sectors
= total_sectors
- cur_sector
;
138 blk
= qemu_malloc(sizeof(BlkMigBlock
));
139 blk
->buf
= qemu_malloc(BLOCK_SIZE
);
141 blk
->sector
= cur_sector
;
144 blk
->iov
.iov_base
= blk
->buf
;
145 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
146 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
148 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
149 nr_sectors
, blk_mig_read_cb
, blk
);
153 block_mig_state
.submitted
++;
155 if (bdrv_read(bs
, cur_sector
, blk
->buf
, nr_sectors
) < 0) {
164 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
165 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
167 return (bmds
->cur_sector
>= total_sectors
);
170 printf("Error reading sector %" PRId64
"\n", cur_sector
);
171 qemu_file_set_error(f
);
177 static void set_dirty_tracking(int enable
)
179 BlkMigDevState
*bmds
;
181 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
182 bdrv_set_dirty_tracking(bmds
->bs
, enable
);
186 static void init_blk_migration(QEMUFile
*f
)
188 BlkMigDevState
*bmds
;
189 BlockDriverState
*bs
;
191 block_mig_state
.submitted
= 0;
192 block_mig_state
.read_done
= 0;
193 block_mig_state
.transferred
= 0;
194 block_mig_state
.total_sector_sum
= 0;
195 block_mig_state
.print_completion
= 0;
197 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
198 if (bs
->type
== BDRV_TYPE_HD
) {
199 bmds
= qemu_mallocz(sizeof(BlkMigDevState
));
201 bmds
->bulk_completed
= 0;
202 bmds
->total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
203 bmds
->completed_sectors
= 0;
204 bmds
->shared_base
= block_mig_state
.shared_base
;
206 block_mig_state
.total_sector_sum
+= bmds
->total_sectors
;
208 if (bmds
->shared_base
) {
209 printf("Start migration for %s with shared base image\n",
212 printf("Start full migration for %s\n", bs
->device_name
);
215 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
220 static int blk_mig_save_bulked_block(QEMUFile
*f
, int is_async
)
222 int64_t completed_sector_sum
= 0;
223 BlkMigDevState
*bmds
;
226 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
227 if (bmds
->bulk_completed
== 0) {
228 if (mig_save_device_bulk(f
, bmds
, is_async
) == 1) {
229 /* completed bulk section for this device */
230 bmds
->bulk_completed
= 1;
232 completed_sector_sum
+= bmds
->completed_sectors
;
236 completed_sector_sum
+= bmds
->completed_sectors
;
240 if (completed_sector_sum
>= block_mig_state
.print_completion
) {
241 printf("Completed %" PRId64
" %%\r",
242 completed_sector_sum
* 100 / block_mig_state
.total_sector_sum
);
244 block_mig_state
.print_completion
+=
245 (BDRV_SECTORS_PER_DIRTY_CHUNK
* 10000);
251 #define MAX_NUM_BLOCKS 4
253 static void blk_mig_save_dirty_blocks(QEMUFile
*f
)
255 BlkMigDevState
*bmds
;
259 blk
.buf
= qemu_malloc(BLOCK_SIZE
);
261 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
262 for (sector
= 0; sector
< bmds
->cur_sector
;) {
263 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
264 if (bdrv_read(bmds
->bs
, sector
, blk
.buf
,
265 BDRV_SECTORS_PER_DIRTY_CHUNK
) < 0) {
266 printf("Error reading sector %" PRId64
"\n", sector
);
267 qemu_file_set_error(f
);
275 bdrv_reset_dirty(bmds
->bs
, sector
,
276 BDRV_SECTORS_PER_DIRTY_CHUNK
);
278 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
285 static void flush_blks(QEMUFile
* f
)
289 dprintf("%s Enter submitted %d read_done %d transferred %d\n",
290 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
291 block_mig_state
.transferred
);
293 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
294 if (qemu_file_rate_limit(f
)) {
298 qemu_file_set_error(f
);
303 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
307 block_mig_state
.read_done
--;
308 block_mig_state
.transferred
++;
309 assert(block_mig_state
.read_done
>= 0);
312 dprintf("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
313 block_mig_state
.submitted
, block_mig_state
.read_done
,
314 block_mig_state
.transferred
);
317 static int is_stage2_completed(void)
319 BlkMigDevState
*bmds
;
321 if (block_mig_state
.submitted
> 0) {
325 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
326 if (bmds
->bulk_completed
== 0) {
334 static void blk_mig_cleanup(void)
336 BlkMigDevState
*bmds
;
339 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
340 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
344 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
345 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
350 set_dirty_tracking(0);
355 static int block_save_live(QEMUFile
*f
, int stage
, void *opaque
)
357 dprintf("Enter save live stage %d submitted %d transferred %d\n",
358 stage
, block_mig_state
.submitted
, block_mig_state
.transferred
);
365 if (block_mig_state
.blk_enable
!= 1) {
366 /* no need to migrate storage */
367 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
372 init_blk_migration(f
);
374 /* start track dirty blocks */
375 set_dirty_tracking(1);
380 if (qemu_file_has_error(f
)) {
385 /* control the rate of transfer */
386 while ((block_mig_state
.submitted
+
387 block_mig_state
.read_done
) * BLOCK_SIZE
<
388 qemu_file_get_rate_limit(f
)) {
389 if (blk_mig_save_bulked_block(f
, 1) == 0) {
390 /* no more bulk blocks for now */
397 if (qemu_file_has_error(f
)) {
403 while (blk_mig_save_bulked_block(f
, 0) != 0) {
407 blk_mig_save_dirty_blocks(f
);
410 if (qemu_file_has_error(f
)) {
414 printf("Block migration completed\n");
417 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
419 return ((stage
== 2) && is_stage2_completed());
422 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
425 char device_name
[256];
427 BlockDriverState
*bs
;
431 addr
= qemu_get_be64(f
);
433 flags
= addr
& ~BDRV_SECTOR_MASK
;
434 addr
>>= BDRV_SECTOR_BITS
;
436 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
437 /* get device name */
438 len
= qemu_get_byte(f
);
439 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
440 device_name
[len
] = '\0';
442 bs
= bdrv_find(device_name
);
444 fprintf(stderr
, "Error unknown block device %s\n",
449 buf
= qemu_malloc(BLOCK_SIZE
);
451 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
452 bdrv_write(bs
, addr
, buf
, BDRV_SECTORS_PER_DIRTY_CHUNK
);
455 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
456 fprintf(stderr
, "Unknown flags\n");
459 if (qemu_file_has_error(f
)) {
462 } while (!(flags
& BLK_MIG_FLAG_EOS
));
467 static void block_set_params(int blk_enable
, int shared_base
, void *opaque
)
469 block_mig_state
.blk_enable
= blk_enable
;
470 block_mig_state
.shared_base
= shared_base
;
472 /* shared base means that blk_enable = 1 */
473 block_mig_state
.blk_enable
|= shared_base
;
476 void blk_mig_init(void)
478 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
479 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
481 register_savevm_live("block", 0, 1, block_set_params
, block_save_live
,
482 NULL
, block_load
, &block_mig_state
);