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