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