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