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 "block-migration.h"
20 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
22 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
23 #define BLK_MIG_FLAG_EOS 0x02
25 #define MAX_IS_ALLOCATED_SEARCH 65536
26 #define MAX_BLOCKS_READ 10000
27 #define BLOCKS_READ_CHANGE 100
28 #define INITIAL_BLOCKS_READ 100
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
{
44 struct BlkMigDevState
*next
;
46 int64_t total_sectors
;
50 typedef struct BlkMigBlock
{
56 BlockDriverAIOCB
*aiocb
;
58 struct BlkMigBlock
*next
;
61 typedef struct BlkMigState
{
64 BlkMigDevState
*bmds_first
;
65 BlkMigBlock
*first_blk
;
66 BlkMigBlock
*last_blk
;
70 int64_t print_completion
;
73 static BlkMigState block_mig_state
;
75 static void blk_mig_read_cb(void *opaque
, int ret
)
77 BlkMigBlock
*blk
= opaque
;
81 /* insert at the end */
82 if (block_mig_state
.last_blk
== NULL
) {
83 block_mig_state
.first_blk
= blk
;
84 block_mig_state
.last_blk
= blk
;
86 block_mig_state
.last_blk
->next
= blk
;
87 block_mig_state
.last_blk
= blk
;
90 block_mig_state
.submitted
--;
91 block_mig_state
.read_done
++;
92 assert(block_mig_state
.submitted
>= 0);
95 static int mig_read_device_bulk(QEMUFile
*f
, BlkMigDevState
*bms
)
98 int64_t total_sectors
, cur_sector
= 0;
99 BlockDriverState
*bs
= bms
->bs
;
102 blk
= qemu_malloc(sizeof(BlkMigBlock
));
103 blk
->buf
= qemu_malloc(BLOCK_SIZE
);
105 cur_sector
= bms
->cur_sector
;
106 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
108 if (bms
->shared_base
) {
109 while (cur_sector
< bms
->total_sectors
&&
110 !bdrv_is_allocated(bms
->bs
, cur_sector
,
111 MAX_IS_ALLOCATED_SEARCH
, &nr_sectors
)) {
112 cur_sector
+= nr_sectors
;
116 if (cur_sector
>= total_sectors
) {
117 bms
->cur_sector
= total_sectors
;
123 if (cur_sector
>= block_mig_state
.print_completion
) {
124 printf("Completed %" PRId64
" %%\r", cur_sector
* 100 / total_sectors
);
126 block_mig_state
.print_completion
+=
127 (BDRV_SECTORS_PER_DIRTY_CHUNK
* 10000);
130 /* we are going to transfer a full block even if it is not allocated */
131 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
133 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
135 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
136 nr_sectors
= (total_sectors
- cur_sector
);
139 bms
->cur_sector
= cur_sector
+ nr_sectors
;
140 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
);
152 printf("Error reading sector %" PRId64
"\n", cur_sector
);
158 bdrv_reset_dirty(bms
->bs
, cur_sector
, nr_sectors
);
159 block_mig_state
.submitted
++;
161 return (bms
->cur_sector
>= total_sectors
);
164 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
167 int64_t total_sectors
= bmds
->total_sectors
, cur_sector
= 0;
168 uint8_t *tmp_buf
= NULL
;
169 BlockDriverState
*bs
= bmds
->bs
;
171 tmp_buf
= qemu_malloc(BLOCK_SIZE
);
173 cur_sector
= bmds
->cur_sector
;
175 if (bmds
->shared_base
) {
176 while (cur_sector
< bmds
->total_sectors
&&
177 !bdrv_is_allocated(bmds
->bs
, cur_sector
,
178 MAX_IS_ALLOCATED_SEARCH
, &nr_sectors
)) {
179 cur_sector
+= nr_sectors
;
183 if (cur_sector
>= total_sectors
) {
184 bmds
->cur_sector
= total_sectors
;
189 if (cur_sector
>= block_mig_state
.print_completion
) {
190 printf("Completed %" PRId64
" %%\r", cur_sector
* 100 / total_sectors
);
192 block_mig_state
.print_completion
+=
193 (BDRV_SECTORS_PER_DIRTY_CHUNK
* 10000);
196 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
198 /* we are going to transfer a full block even if it is not allocated */
199 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
201 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
202 nr_sectors
= (total_sectors
- cur_sector
);
205 if (bdrv_read(bs
, cur_sector
, tmp_buf
, nr_sectors
) < 0) {
206 printf("Error reading sector %" PRId64
"\n", cur_sector
);
209 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
211 /* sector number and flags */
212 qemu_put_be64(f
, (cur_sector
<< BDRV_SECTOR_BITS
)
213 | BLK_MIG_FLAG_DEVICE_BLOCK
);
216 len
= strlen(bs
->device_name
);
217 qemu_put_byte(f
, len
);
218 qemu_put_buffer(f
, (uint8_t *)bs
->device_name
, len
);
220 qemu_put_buffer(f
, tmp_buf
, BLOCK_SIZE
);
222 bmds
->cur_sector
= cur_sector
+ BDRV_SECTORS_PER_DIRTY_CHUNK
;
226 return (bmds
->cur_sector
>= total_sectors
);
229 static void send_blk(QEMUFile
*f
, BlkMigBlock
* blk
)
233 /* sector number and flags */
234 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
235 | BLK_MIG_FLAG_DEVICE_BLOCK
);
238 len
= strlen(blk
->bmds
->bs
->device_name
);
239 qemu_put_byte(f
, len
);
240 qemu_put_buffer(f
, (uint8_t *)blk
->bmds
->bs
->device_name
, len
);
242 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
245 static void set_dirty_tracking(int enable
)
247 BlkMigDevState
*bmds
;
248 for (bmds
= block_mig_state
.bmds_first
; bmds
!= NULL
; bmds
= bmds
->next
) {
249 bdrv_set_dirty_tracking(bmds
->bs
, enable
);
253 static void init_blk_migration(QEMUFile
*f
)
255 BlkMigDevState
**pbmds
, *bmds
;
256 BlockDriverState
*bs
;
258 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
259 if (bs
->type
== BDRV_TYPE_HD
) {
260 bmds
= qemu_mallocz(sizeof(BlkMigDevState
));
262 bmds
->bulk_completed
= 0;
263 bmds
->total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
264 bmds
->shared_base
= block_mig_state
.shared_base
;
266 if (bmds
->shared_base
) {
267 printf("Start migration for %s with shared base image\n",
270 printf("Start full migration for %s\n", bs
->device_name
);
273 /* insert at the end */
274 pbmds
= &block_mig_state
.bmds_first
;
275 while (*pbmds
!= NULL
) {
276 pbmds
= &(*pbmds
)->next
;
283 static int blk_mig_save_bulked_block(QEMUFile
*f
, int is_async
)
285 BlkMigDevState
*bmds
;
287 for (bmds
= block_mig_state
.bmds_first
; bmds
!= NULL
; bmds
= bmds
->next
) {
288 if (bmds
->bulk_completed
== 0) {
290 if (mig_read_device_bulk(f
, bmds
) == 1) {
291 /* completed bulk section for this device */
292 bmds
->bulk_completed
= 1;
295 if (mig_save_device_bulk(f
, bmds
) == 1) {
296 /* completed bulk section for this device */
297 bmds
->bulk_completed
= 1;
304 /* we reached here means bulk is completed */
308 #define MAX_NUM_BLOCKS 4
310 static void blk_mig_save_dirty_blocks(QEMUFile
*f
)
312 BlkMigDevState
*bmds
;
317 buf
= qemu_malloc(BLOCK_SIZE
);
319 for (bmds
= block_mig_state
.bmds_first
; bmds
!= NULL
; bmds
= bmds
->next
) {
320 for (sector
= 0; sector
< bmds
->cur_sector
;) {
321 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
322 if (bdrv_read(bmds
->bs
, sector
, buf
,
323 BDRV_SECTORS_PER_DIRTY_CHUNK
) < 0) {
324 /* FIXME: add error handling */
327 /* sector number and flags */
328 qemu_put_be64(f
, (sector
<< BDRV_SECTOR_BITS
)
329 | BLK_MIG_FLAG_DEVICE_BLOCK
);
332 len
= strlen(bmds
->bs
->device_name
);
333 qemu_put_byte(f
, len
);
334 qemu_put_buffer(f
, (uint8_t *)bmds
->bs
->device_name
, len
);
336 qemu_put_buffer(f
, buf
, BLOCK_SIZE
);
338 bdrv_reset_dirty(bmds
->bs
, sector
,
339 BDRV_SECTORS_PER_DIRTY_CHUNK
);
341 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
348 static void flush_blks(QEMUFile
* f
)
350 BlkMigBlock
*blk
, *next
;
352 dprintf("%s Enter submitted %d read_done %d transferred %d\n",
353 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
354 block_mig_state
.transferred
);
356 for (blk
= block_mig_state
.first_blk
;
357 blk
!= NULL
&& !qemu_file_rate_limit(f
);
365 block_mig_state
.read_done
--;
366 block_mig_state
.transferred
++;
367 assert(block_mig_state
.read_done
>= 0);
369 block_mig_state
.first_blk
= blk
;
371 if (block_mig_state
.first_blk
== NULL
) {
372 block_mig_state
.last_blk
= NULL
;
375 dprintf("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
376 block_mig_state
.submitted
, block_mig_state
.read_done
,
377 block_mig_state
.transferred
);
380 static int is_stage2_completed(void)
382 BlkMigDevState
*bmds
;
384 if (block_mig_state
.submitted
> 0) {
388 for (bmds
= block_mig_state
.bmds_first
; bmds
!= NULL
; bmds
= bmds
->next
) {
389 if (bmds
->bulk_completed
== 0) {
397 static int block_save_live(QEMUFile
*f
, int stage
, void *opaque
)
399 dprintf("Enter save live stage %d submitted %d transferred %d\n",
400 stage
, block_mig_state
.submitted
, block_mig_state
.transferred
);
402 if (block_mig_state
.blk_enable
!= 1) {
403 /* no need to migrate storage */
404 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
409 init_blk_migration(f
);
411 /* start track dirty blocks */
412 set_dirty_tracking(1);
417 /* control the rate of transfer */
418 while ((block_mig_state
.submitted
+
419 block_mig_state
.read_done
) * BLOCK_SIZE
<
420 qemu_file_get_rate_limit(f
)) {
421 if (blk_mig_save_bulked_block(f
, 1) == 0) {
422 /* no more bulk blocks for now */
430 while (blk_mig_save_bulked_block(f
, 0) != 0) {
434 blk_mig_save_dirty_blocks(f
);
436 /* stop track dirty blocks */
437 set_dirty_tracking(0);
439 printf("\nBlock migration completed\n");
442 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
444 return ((stage
== 2) && is_stage2_completed());
447 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
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
);
465 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
466 device_name
[len
] = '\0';
468 bs
= bdrv_find(device_name
);
470 buf
= qemu_malloc(BLOCK_SIZE
);
472 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
474 bdrv_write(bs
, addr
, buf
, BDRV_SECTORS_PER_DIRTY_CHUNK
);
476 printf("Error unknown block device %s\n", device_name
);
477 /* FIXME: add error handling */
481 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
482 printf("Unknown flags\n");
483 /* FIXME: add error handling */
485 } while (!(flags
& BLK_MIG_FLAG_EOS
));
490 static void block_set_params(int blk_enable
, int shared_base
, void *opaque
)
492 block_mig_state
.blk_enable
= blk_enable
;
493 block_mig_state
.shared_base
= shared_base
;
495 /* shared base means that blk_enable = 1 */
496 block_mig_state
.blk_enable
|= shared_base
;
499 void blk_mig_init(void)
501 register_savevm_live("block", 0, 1, block_set_params
, block_save_live
,
502 NULL
, block_load
, &block_mig_state
);