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 static void blk_mig_read_cb(void *opaque
, int ret
)
96 BlkMigBlock
*blk
= opaque
;
100 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
102 block_mig_state
.submitted
--;
103 block_mig_state
.read_done
++;
104 assert(block_mig_state
.submitted
>= 0);
107 static int mig_save_device_bulk(Monitor
*mon
, QEMUFile
*f
,
108 BlkMigDevState
*bmds
, int is_async
)
110 int64_t total_sectors
= bmds
->total_sectors
;
111 int64_t cur_sector
= bmds
->cur_sector
;
112 BlockDriverState
*bs
= bmds
->bs
;
116 if (bmds
->shared_base
) {
117 while (cur_sector
< total_sectors
&&
118 !bdrv_is_allocated(bs
, cur_sector
, MAX_IS_ALLOCATED_SEARCH
,
120 cur_sector
+= nr_sectors
;
124 if (cur_sector
>= total_sectors
) {
125 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
129 bmds
->completed_sectors
= cur_sector
;
131 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
133 /* we are going to transfer a full block even if it is not allocated */
134 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
136 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
137 nr_sectors
= total_sectors
- cur_sector
;
140 blk
= qemu_malloc(sizeof(BlkMigBlock
));
141 blk
->buf
= qemu_malloc(BLOCK_SIZE
);
143 blk
->sector
= cur_sector
;
146 blk
->iov
.iov_base
= blk
->buf
;
147 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
148 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
150 blk
->aiocb
= bdrv_aio_readv(bs
, cur_sector
, &blk
->qiov
,
151 nr_sectors
, blk_mig_read_cb
, blk
);
155 block_mig_state
.submitted
++;
157 if (bdrv_read(bs
, cur_sector
, blk
->buf
, nr_sectors
) < 0) {
166 bdrv_reset_dirty(bs
, cur_sector
, nr_sectors
);
167 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
169 return (bmds
->cur_sector
>= total_sectors
);
172 monitor_printf(mon
, "Error reading sector %" PRId64
"\n", cur_sector
);
173 qemu_file_set_error(f
);
179 static void set_dirty_tracking(int enable
)
181 BlkMigDevState
*bmds
;
183 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
184 bdrv_set_dirty_tracking(bmds
->bs
, enable
);
188 static void init_blk_migration(Monitor
*mon
, QEMUFile
*f
)
190 BlkMigDevState
*bmds
;
191 BlockDriverState
*bs
;
193 block_mig_state
.submitted
= 0;
194 block_mig_state
.read_done
= 0;
195 block_mig_state
.transferred
= 0;
196 block_mig_state
.total_sector_sum
= 0;
197 block_mig_state
.print_completion
= 0;
199 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
200 if (bs
->type
== BDRV_TYPE_HD
) {
201 bmds
= qemu_mallocz(sizeof(BlkMigDevState
));
203 bmds
->bulk_completed
= 0;
204 bmds
->total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
205 bmds
->completed_sectors
= 0;
206 bmds
->shared_base
= block_mig_state
.shared_base
;
208 block_mig_state
.total_sector_sum
+= bmds
->total_sectors
;
210 if (bmds
->shared_base
) {
211 monitor_printf(mon
, "Start migration for %s with shared base "
215 monitor_printf(mon
, "Start full migration for %s\n",
219 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
224 static int blk_mig_save_bulked_block(Monitor
*mon
, QEMUFile
*f
, int is_async
)
226 int64_t completed_sector_sum
= 0;
227 BlkMigDevState
*bmds
;
230 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
231 if (bmds
->bulk_completed
== 0) {
232 if (mig_save_device_bulk(mon
, f
, bmds
, is_async
) == 1) {
233 /* completed bulk section for this device */
234 bmds
->bulk_completed
= 1;
236 completed_sector_sum
+= bmds
->completed_sectors
;
240 completed_sector_sum
+= bmds
->completed_sectors
;
244 if (completed_sector_sum
>= block_mig_state
.print_completion
) {
245 monitor_printf(mon
, "Completed %" PRId64
" %%\r",
246 completed_sector_sum
* 100 /
247 block_mig_state
.total_sector_sum
);
249 block_mig_state
.print_completion
+=
250 (BDRV_SECTORS_PER_DIRTY_CHUNK
* 10000);
256 #define MAX_NUM_BLOCKS 4
258 static void blk_mig_save_dirty_blocks(Monitor
*mon
, QEMUFile
*f
)
260 BlkMigDevState
*bmds
;
264 blk
.buf
= qemu_malloc(BLOCK_SIZE
);
266 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
267 for (sector
= 0; sector
< bmds
->cur_sector
;) {
268 if (bdrv_get_dirty(bmds
->bs
, sector
)) {
269 if (bdrv_read(bmds
->bs
, sector
, blk
.buf
,
270 BDRV_SECTORS_PER_DIRTY_CHUNK
) < 0) {
271 monitor_printf(mon
, "Error reading sector %" PRId64
"\n",
273 qemu_file_set_error(f
);
281 bdrv_reset_dirty(bmds
->bs
, sector
,
282 BDRV_SECTORS_PER_DIRTY_CHUNK
);
284 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
291 static void flush_blks(QEMUFile
* f
)
295 dprintf("%s Enter submitted %d read_done %d transferred %d\n",
296 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
297 block_mig_state
.transferred
);
299 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
300 if (qemu_file_rate_limit(f
)) {
304 qemu_file_set_error(f
);
309 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
313 block_mig_state
.read_done
--;
314 block_mig_state
.transferred
++;
315 assert(block_mig_state
.read_done
>= 0);
318 dprintf("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
319 block_mig_state
.submitted
, block_mig_state
.read_done
,
320 block_mig_state
.transferred
);
323 static int is_stage2_completed(void)
325 BlkMigDevState
*bmds
;
327 if (block_mig_state
.submitted
> 0) {
331 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
332 if (bmds
->bulk_completed
== 0) {
340 static void blk_mig_cleanup(Monitor
*mon
)
342 BlkMigDevState
*bmds
;
345 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
346 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
350 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
351 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
356 set_dirty_tracking(0);
358 monitor_printf(mon
, "\n");
361 static int block_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
363 dprintf("Enter save live stage %d submitted %d transferred %d\n",
364 stage
, block_mig_state
.submitted
, block_mig_state
.transferred
);
367 blk_mig_cleanup(mon
);
371 if (block_mig_state
.blk_enable
!= 1) {
372 /* no need to migrate storage */
373 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
378 init_blk_migration(mon
, f
);
380 /* start track dirty blocks */
381 set_dirty_tracking(1);
386 if (qemu_file_has_error(f
)) {
387 blk_mig_cleanup(mon
);
391 /* control the rate of transfer */
392 while ((block_mig_state
.submitted
+
393 block_mig_state
.read_done
) * BLOCK_SIZE
<
394 qemu_file_get_rate_limit(f
)) {
395 if (blk_mig_save_bulked_block(mon
, f
, 1) == 0) {
396 /* no more bulk blocks for now */
403 if (qemu_file_has_error(f
)) {
404 blk_mig_cleanup(mon
);
409 while (blk_mig_save_bulked_block(mon
, f
, 0) != 0) {
413 blk_mig_save_dirty_blocks(mon
, f
);
414 blk_mig_cleanup(mon
);
416 if (qemu_file_has_error(f
)) {
420 monitor_printf(mon
, "Block migration completed\n");
423 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
425 return ((stage
== 2) && is_stage2_completed());
428 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
431 char device_name
[256];
433 BlockDriverState
*bs
;
437 addr
= qemu_get_be64(f
);
439 flags
= addr
& ~BDRV_SECTOR_MASK
;
440 addr
>>= BDRV_SECTOR_BITS
;
442 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
443 /* get device name */
444 len
= qemu_get_byte(f
);
445 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
446 device_name
[len
] = '\0';
448 bs
= bdrv_find(device_name
);
450 fprintf(stderr
, "Error unknown block device %s\n",
455 buf
= qemu_malloc(BLOCK_SIZE
);
457 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
458 bdrv_write(bs
, addr
, buf
, BDRV_SECTORS_PER_DIRTY_CHUNK
);
461 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
462 fprintf(stderr
, "Unknown flags\n");
465 if (qemu_file_has_error(f
)) {
468 } while (!(flags
& BLK_MIG_FLAG_EOS
));
473 static void block_set_params(int blk_enable
, int shared_base
, void *opaque
)
475 block_mig_state
.blk_enable
= blk_enable
;
476 block_mig_state
.shared_base
= shared_base
;
478 /* shared base means that blk_enable = 1 */
479 block_mig_state
.blk_enable
|= shared_base
;
482 void blk_mig_init(void)
484 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
485 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
487 register_savevm_live("block", 0, 1, block_set_params
, block_save_live
,
488 NULL
, block_load
, &block_mig_state
);