]> git.proxmox.com Git - qemu.git/blame - block-migration.c
block migration: Increase dirty chunk size to 1M
[qemu.git] / block-migration.c
CommitLineData
c163b5ca 1/*
2 * QEMU live block migration
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Liran Schour <lirans@il.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#include "qemu-common.h"
15#include "block_int.h"
16#include "hw/hw.h"
5e5328be 17#include "qemu-queue.h"
7184049e 18#include "monitor.h"
c163b5ca 19#include "block-migration.h"
20#include <assert.h>
c163b5ca 21
6ea44308 22#define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
c163b5ca 23
24#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
25#define BLK_MIG_FLAG_EOS 0x02
01e61e2d 26#define BLK_MIG_FLAG_PROGRESS 0x04
c163b5ca 27
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
32
33//#define DEBUG_BLK_MIGRATION
34
35#ifdef DEBUG_BLK_MIGRATION
a55eb92c 36#define dprintf(fmt, ...) \
c163b5ca 37 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
38#else
a55eb92c 39#define dprintf(fmt, ...) \
c163b5ca 40 do { } while (0)
41#endif
42
a55eb92c
JK
43typedef struct BlkMigDevState {
44 BlockDriverState *bs;
45 int bulk_completed;
46 int shared_base;
a55eb92c 47 int64_t cur_sector;
82801d8f 48 int64_t completed_sectors;
a55eb92c
JK
49 int64_t total_sectors;
50 int64_t dirty;
5e5328be 51 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
a55eb92c
JK
52} BlkMigDevState;
53
c163b5ca 54typedef struct BlkMigBlock {
55 uint8_t *buf;
56 BlkMigDevState *bmds;
57 int64_t sector;
58 struct iovec iov;
59 QEMUIOVector qiov;
60 BlockDriverAIOCB *aiocb;
61 int ret;
5e5328be 62 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
c163b5ca 63} BlkMigBlock;
64
65typedef struct BlkMigState {
c163b5ca 66 int blk_enable;
67 int shared_base;
5e5328be
JK
68 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
69 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
c163b5ca 70 int submitted;
71 int read_done;
72 int transferred;
82801d8f 73 int64_t total_sector_sum;
01e61e2d 74 int prev_progress;
c163b5ca 75} BlkMigState;
76
d11ecd3d 77static BlkMigState block_mig_state;
c163b5ca 78
13f0b67f
JK
79static void blk_send(QEMUFile *f, BlkMigBlock * blk)
80{
81 int len;
82
83 /* sector number and flags */
84 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
85 | BLK_MIG_FLAG_DEVICE_BLOCK);
86
87 /* device name */
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);
91
92 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
93}
94
25f23643
JK
95int blk_mig_active(void)
96{
97 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
98}
99
100uint64_t blk_mig_bytes_transferred(void)
101{
102 BlkMigDevState *bmds;
103 uint64_t sum = 0;
104
105 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
106 sum += bmds->completed_sectors;
107 }
108 return sum << BDRV_SECTOR_BITS;
109}
110
111uint64_t blk_mig_bytes_remaining(void)
112{
113 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
114}
115
116uint64_t blk_mig_bytes_total(void)
117{
118 BlkMigDevState *bmds;
119 uint64_t sum = 0;
120
121 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
122 sum += bmds->total_sectors;
123 }
124 return sum << BDRV_SECTOR_BITS;
125}
126
c163b5ca 127static void blk_mig_read_cb(void *opaque, int ret)
128{
129 BlkMigBlock *blk = opaque;
a55eb92c 130
c163b5ca 131 blk->ret = ret;
a55eb92c 132
5e5328be 133 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
a55eb92c 134
d11ecd3d
JK
135 block_mig_state.submitted--;
136 block_mig_state.read_done++;
137 assert(block_mig_state.submitted >= 0);
c163b5ca 138}
139
7184049e
JK
140static int mig_save_device_bulk(Monitor *mon, QEMUFile *f,
141 BlkMigDevState *bmds, int is_async)
a55eb92c 142{
57cce12d
JK
143 int64_t total_sectors = bmds->total_sectors;
144 int64_t cur_sector = bmds->cur_sector;
145 BlockDriverState *bs = bmds->bs;
c163b5ca 146 BlkMigBlock *blk;
13f0b67f 147 int nr_sectors;
a55eb92c 148
57cce12d 149 if (bmds->shared_base) {
b1d10856 150 while (cur_sector < total_sectors &&
57cce12d
JK
151 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
152 &nr_sectors)) {
c163b5ca 153 cur_sector += nr_sectors;
154 }
155 }
a55eb92c
JK
156
157 if (cur_sector >= total_sectors) {
82801d8f 158 bmds->cur_sector = bmds->completed_sectors = total_sectors;
c163b5ca 159 return 1;
160 }
a55eb92c 161
82801d8f 162 bmds->completed_sectors = cur_sector;
a55eb92c 163
57cce12d
JK
164 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
165
6ea44308
JK
166 /* we are going to transfer a full block even if it is not allocated */
167 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
c163b5ca 168
6ea44308 169 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
57cce12d 170 nr_sectors = total_sectors - cur_sector;
c163b5ca 171 }
a55eb92c 172
13f0b67f
JK
173 blk = qemu_malloc(sizeof(BlkMigBlock));
174 blk->buf = qemu_malloc(BLOCK_SIZE);
175 blk->bmds = bmds;
176 blk->sector = cur_sector;
a55eb92c 177
13f0b67f 178 if (is_async) {
57cce12d
JK
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);
a55eb92c 182
57cce12d
JK
183 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
184 nr_sectors, blk_mig_read_cb, blk);
57cce12d 185 if (!blk->aiocb) {
4b640365 186 goto error;
57cce12d 187 }
57cce12d 188 block_mig_state.submitted++;
57cce12d 189 } else {
13f0b67f 190 if (bdrv_read(bs, cur_sector, blk->buf, nr_sectors) < 0) {
4b640365 191 goto error;
c163b5ca 192 }
13f0b67f 193 blk_send(f, blk);
a55eb92c 194
13f0b67f
JK
195 qemu_free(blk->buf);
196 qemu_free(blk);
c163b5ca 197 }
198
13f0b67f
JK
199 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
200 bmds->cur_sector = cur_sector + nr_sectors;
a55eb92c 201
13f0b67f 202 return (bmds->cur_sector >= total_sectors);
4b640365
JK
203
204error:
7184049e 205 monitor_printf(mon, "Error reading sector %" PRId64 "\n", cur_sector);
4b640365
JK
206 qemu_file_set_error(f);
207 qemu_free(blk->buf);
208 qemu_free(blk);
209 return 0;
c163b5ca 210}
211
c163b5ca 212static void set_dirty_tracking(int enable)
213{
214 BlkMigDevState *bmds;
5e5328be
JK
215
216 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 217 bdrv_set_dirty_tracking(bmds->bs, enable);
c163b5ca 218 }
c163b5ca 219}
220
7184049e 221static void init_blk_migration(Monitor *mon, QEMUFile *f)
c163b5ca 222{
5e5328be 223 BlkMigDevState *bmds;
c163b5ca 224 BlockDriverState *bs;
a55eb92c 225
69d63a97
JK
226 block_mig_state.submitted = 0;
227 block_mig_state.read_done = 0;
228 block_mig_state.transferred = 0;
82801d8f 229 block_mig_state.total_sector_sum = 0;
01e61e2d 230 block_mig_state.prev_progress = -1;
69d63a97 231
c163b5ca 232 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
a55eb92c 233 if (bs->type == BDRV_TYPE_HD) {
c163b5ca 234 bmds = qemu_mallocz(sizeof(BlkMigDevState));
235 bmds->bs = bs;
236 bmds->bulk_completed = 0;
6ea44308 237 bmds->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
82801d8f 238 bmds->completed_sectors = 0;
d11ecd3d 239 bmds->shared_base = block_mig_state.shared_base;
a55eb92c 240
82801d8f
JK
241 block_mig_state.total_sector_sum += bmds->total_sectors;
242
a55eb92c 243 if (bmds->shared_base) {
7184049e
JK
244 monitor_printf(mon, "Start migration for %s with shared base "
245 "image\n",
246 bs->device_name);
c163b5ca 247 } else {
7184049e
JK
248 monitor_printf(mon, "Start full migration for %s\n",
249 bs->device_name);
c163b5ca 250 }
a55eb92c 251
5e5328be 252 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
c163b5ca 253 }
a55eb92c 254 }
c163b5ca 255}
256
7184049e 257static int blk_mig_save_bulked_block(Monitor *mon, QEMUFile *f, int is_async)
c163b5ca 258{
82801d8f 259 int64_t completed_sector_sum = 0;
c163b5ca 260 BlkMigDevState *bmds;
01e61e2d 261 int progress;
82801d8f 262 int ret = 0;
c163b5ca 263
5e5328be 264 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 265 if (bmds->bulk_completed == 0) {
7184049e 266 if (mig_save_device_bulk(mon, f, bmds, is_async) == 1) {
57cce12d
JK
267 /* completed bulk section for this device */
268 bmds->bulk_completed = 1;
c163b5ca 269 }
82801d8f
JK
270 completed_sector_sum += bmds->completed_sectors;
271 ret = 1;
272 break;
273 } else {
274 completed_sector_sum += bmds->completed_sectors;
c163b5ca 275 }
276 }
a55eb92c 277
01e61e2d
JK
278 progress = completed_sector_sum * 100 / block_mig_state.total_sector_sum;
279 if (progress != block_mig_state.prev_progress) {
280 block_mig_state.prev_progress = progress;
281 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
282 | BLK_MIG_FLAG_PROGRESS);
283 monitor_printf(mon, "Completed %d %%\r", progress);
7184049e 284 monitor_flush(mon);
82801d8f
JK
285 }
286
287 return ret;
c163b5ca 288}
289
290#define MAX_NUM_BLOCKS 4
291
7184049e 292static void blk_mig_save_dirty_blocks(Monitor *mon, QEMUFile *f)
c163b5ca 293{
294 BlkMigDevState *bmds;
13f0b67f 295 BlkMigBlock blk;
c163b5ca 296 int64_t sector;
a55eb92c 297
13f0b67f 298 blk.buf = qemu_malloc(BLOCK_SIZE);
575a58d7 299
5e5328be 300 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c
JK
301 for (sector = 0; sector < bmds->cur_sector;) {
302 if (bdrv_get_dirty(bmds->bs, sector)) {
13f0b67f 303 if (bdrv_read(bmds->bs, sector, blk.buf,
6ea44308 304 BDRV_SECTORS_PER_DIRTY_CHUNK) < 0) {
7184049e
JK
305 monitor_printf(mon, "Error reading sector %" PRId64 "\n",
306 sector);
4b640365
JK
307 qemu_file_set_error(f);
308 qemu_free(blk.buf);
309 return;
c163b5ca 310 }
13f0b67f
JK
311 blk.bmds = bmds;
312 blk.sector = sector;
313 blk_send(f, &blk);
a55eb92c
JK
314
315 bdrv_reset_dirty(bmds->bs, sector,
6ea44308 316 BDRV_SECTORS_PER_DIRTY_CHUNK);
a55eb92c 317 }
6ea44308 318 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
c163b5ca 319 }
320 }
575a58d7 321
13f0b67f 322 qemu_free(blk.buf);
c163b5ca 323}
324
325static void flush_blks(QEMUFile* f)
326{
5e5328be 327 BlkMigBlock *blk;
a55eb92c 328
d11ecd3d
JK
329 dprintf("%s Enter submitted %d read_done %d transferred %d\n",
330 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
331 block_mig_state.transferred);
a55eb92c 332
5e5328be
JK
333 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
334 if (qemu_file_rate_limit(f)) {
335 break;
336 }
4b640365
JK
337 if (blk->ret < 0) {
338 qemu_file_set_error(f);
339 break;
340 }
13f0b67f 341 blk_send(f, blk);
a55eb92c 342
5e5328be 343 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
c163b5ca 344 qemu_free(blk->buf);
345 qemu_free(blk);
a55eb92c 346
d11ecd3d
JK
347 block_mig_state.read_done--;
348 block_mig_state.transferred++;
349 assert(block_mig_state.read_done >= 0);
c163b5ca 350 }
c163b5ca 351
d11ecd3d
JK
352 dprintf("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
353 block_mig_state.submitted, block_mig_state.read_done,
354 block_mig_state.transferred);
c163b5ca 355}
356
357static int is_stage2_completed(void)
358{
359 BlkMigDevState *bmds;
a55eb92c 360
d11ecd3d 361 if (block_mig_state.submitted > 0) {
c163b5ca 362 return 0;
363 }
a55eb92c 364
5e5328be 365 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 366 if (bmds->bulk_completed == 0) {
c163b5ca 367 return 0;
368 }
369 }
a55eb92c 370
c163b5ca 371 return 1;
372}
373
7184049e 374static void blk_mig_cleanup(Monitor *mon)
4ec7fcc7 375{
82801d8f
JK
376 BlkMigDevState *bmds;
377 BlkMigBlock *blk;
4ec7fcc7 378
82801d8f
JK
379 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
380 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
4ec7fcc7
JK
381 qemu_free(bmds);
382 }
383
82801d8f
JK
384 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
385 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
4ec7fcc7
JK
386 qemu_free(blk->buf);
387 qemu_free(blk);
388 }
389
390 set_dirty_tracking(0);
391
7184049e 392 monitor_printf(mon, "\n");
4ec7fcc7
JK
393}
394
f327aa0c 395static int block_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
c163b5ca 396{
d11ecd3d
JK
397 dprintf("Enter save live stage %d submitted %d transferred %d\n",
398 stage, block_mig_state.submitted, block_mig_state.transferred);
a55eb92c 399
4ec7fcc7 400 if (stage < 0) {
7184049e 401 blk_mig_cleanup(mon);
4ec7fcc7
JK
402 return 0;
403 }
404
d11ecd3d 405 if (block_mig_state.blk_enable != 1) {
c163b5ca 406 /* no need to migrate storage */
a55eb92c 407 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
c163b5ca 408 return 1;
409 }
a55eb92c
JK
410
411 if (stage == 1) {
7184049e 412 init_blk_migration(mon, f);
a55eb92c 413
c163b5ca 414 /* start track dirty blocks */
415 set_dirty_tracking(1);
c163b5ca 416 }
417
418 flush_blks(f);
a55eb92c 419
4b640365 420 if (qemu_file_has_error(f)) {
7184049e 421 blk_mig_cleanup(mon);
4b640365
JK
422 return 0;
423 }
424
c163b5ca 425 /* control the rate of transfer */
d11ecd3d
JK
426 while ((block_mig_state.submitted +
427 block_mig_state.read_done) * BLOCK_SIZE <
a55eb92c 428 qemu_file_get_rate_limit(f)) {
7184049e 429 if (blk_mig_save_bulked_block(mon, f, 1) == 0) {
a55eb92c 430 /* no more bulk blocks for now */
c163b5ca 431 break;
a55eb92c 432 }
c163b5ca 433 }
a55eb92c 434
c163b5ca 435 flush_blks(f);
a55eb92c 436
4b640365 437 if (qemu_file_has_error(f)) {
7184049e 438 blk_mig_cleanup(mon);
4b640365
JK
439 return 0;
440 }
441
a55eb92c 442 if (stage == 3) {
7184049e 443 while (blk_mig_save_bulked_block(mon, f, 0) != 0) {
a55eb92c
JK
444 /* empty */
445 }
446
7184049e
JK
447 blk_mig_save_dirty_blocks(mon, f);
448 blk_mig_cleanup(mon);
a55eb92c 449
01e61e2d
JK
450 /* report completion */
451 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
452
4b640365
JK
453 if (qemu_file_has_error(f)) {
454 return 0;
455 }
456
7184049e 457 monitor_printf(mon, "Block migration completed\n");
c163b5ca 458 }
a55eb92c
JK
459
460 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
461
c163b5ca 462 return ((stage == 2) && is_stage2_completed());
463}
464
465static int block_load(QEMUFile *f, void *opaque, int version_id)
466{
01e61e2d 467 static int banner_printed;
c163b5ca 468 int len, flags;
469 char device_name[256];
470 int64_t addr;
471 BlockDriverState *bs;
472 uint8_t *buf;
a55eb92c 473
c163b5ca 474 do {
c163b5ca 475 addr = qemu_get_be64(f);
a55eb92c 476
6ea44308
JK
477 flags = addr & ~BDRV_SECTOR_MASK;
478 addr >>= BDRV_SECTOR_BITS;
a55eb92c
JK
479
480 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
c163b5ca 481 /* get device name */
482 len = qemu_get_byte(f);
c163b5ca 483 qemu_get_buffer(f, (uint8_t *)device_name, len);
484 device_name[len] = '\0';
a55eb92c 485
c163b5ca 486 bs = bdrv_find(device_name);
4b640365
JK
487 if (!bs) {
488 fprintf(stderr, "Error unknown block device %s\n",
489 device_name);
490 return -EINVAL;
491 }
a55eb92c 492
575a58d7
JK
493 buf = qemu_malloc(BLOCK_SIZE);
494
a55eb92c 495 qemu_get_buffer(f, buf, BLOCK_SIZE);
4b640365 496 bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK);
575a58d7
JK
497
498 qemu_free(buf);
01e61e2d
JK
499 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
500 if (!banner_printed) {
501 printf("Receiving block device images\n");
502 banner_printed = 1;
503 }
504 printf("Completed %d %%%c", (int)addr,
505 (addr == 100) ? '\n' : '\r');
506 fflush(stdout);
a55eb92c 507 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
4b640365
JK
508 fprintf(stderr, "Unknown flags\n");
509 return -EINVAL;
510 }
511 if (qemu_file_has_error(f)) {
512 return -EIO;
c163b5ca 513 }
a55eb92c
JK
514 } while (!(flags & BLK_MIG_FLAG_EOS));
515
c163b5ca 516 return 0;
517}
518
519static void block_set_params(int blk_enable, int shared_base, void *opaque)
520{
d11ecd3d
JK
521 block_mig_state.blk_enable = blk_enable;
522 block_mig_state.shared_base = shared_base;
a55eb92c 523
c163b5ca 524 /* shared base means that blk_enable = 1 */
d11ecd3d 525 block_mig_state.blk_enable |= shared_base;
c163b5ca 526}
527
c163b5ca 528void blk_mig_init(void)
a55eb92c 529{
5e5328be
JK
530 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
531 QSIMPLEQ_INIT(&block_mig_state.blk_list);
532
a55eb92c 533 register_savevm_live("block", 0, 1, block_set_params, block_save_live,
d11ecd3d 534 NULL, block_load, &block_mig_state);
c163b5ca 535}