]>
Commit | Line | Data |
---|---|---|
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 |
41 | typedef 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 | 51 | typedef 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 | ||
62 | typedef 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 | 73 | static BlkMigState block_mig_state; |
c163b5ca | 74 | |
13f0b67f JK |
75 | static 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 | 91 | static 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 | 104 | static 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 | 153 | if (!blk->aiocb) { |
4b640365 | 154 | goto error; |
57cce12d | 155 | } |
57cce12d | 156 | block_mig_state.submitted++; |
57cce12d | 157 | } else { |
13f0b67f | 158 | if (bdrv_read(bs, cur_sector, blk->buf, nr_sectors) < 0) { |
4b640365 | 159 | goto error; |
c163b5ca | 160 | } |
13f0b67f | 161 | blk_send(f, blk); |
a55eb92c | 162 | |
13f0b67f JK |
163 | qemu_free(blk->buf); |
164 | qemu_free(blk); | |
c163b5ca | 165 | } |
166 | ||
13f0b67f JK |
167 | bdrv_reset_dirty(bs, cur_sector, nr_sectors); |
168 | bmds->cur_sector = cur_sector + nr_sectors; | |
a55eb92c | 169 | |
13f0b67f | 170 | return (bmds->cur_sector >= total_sectors); |
4b640365 JK |
171 | |
172 | error: | |
173 | printf("Error reading sector %" PRId64 "\n", cur_sector); | |
174 | qemu_file_set_error(f); | |
175 | qemu_free(blk->buf); | |
176 | qemu_free(blk); | |
177 | return 0; | |
c163b5ca | 178 | } |
179 | ||
c163b5ca | 180 | static void set_dirty_tracking(int enable) |
181 | { | |
182 | BlkMigDevState *bmds; | |
5e5328be JK |
183 | |
184 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
a55eb92c | 185 | bdrv_set_dirty_tracking(bmds->bs, enable); |
c163b5ca | 186 | } |
c163b5ca | 187 | } |
188 | ||
189 | static void init_blk_migration(QEMUFile *f) | |
190 | { | |
5e5328be | 191 | BlkMigDevState *bmds; |
c163b5ca | 192 | BlockDriverState *bs; |
a55eb92c | 193 | |
69d63a97 JK |
194 | block_mig_state.submitted = 0; |
195 | block_mig_state.read_done = 0; | |
196 | block_mig_state.transferred = 0; | |
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; |
d11ecd3d | 205 | bmds->shared_base = block_mig_state.shared_base; |
a55eb92c JK |
206 | |
207 | if (bmds->shared_base) { | |
208 | printf("Start migration for %s with shared base image\n", | |
c163b5ca | 209 | bs->device_name); |
210 | } else { | |
211 | printf("Start full migration for %s\n", bs->device_name); | |
212 | } | |
a55eb92c | 213 | |
5e5328be | 214 | QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry); |
c163b5ca | 215 | } |
a55eb92c | 216 | } |
c163b5ca | 217 | } |
218 | ||
219 | static int blk_mig_save_bulked_block(QEMUFile *f, int is_async) | |
220 | { | |
221 | BlkMigDevState *bmds; | |
222 | ||
5e5328be | 223 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
a55eb92c | 224 | if (bmds->bulk_completed == 0) { |
57cce12d JK |
225 | if (mig_save_device_bulk(f, bmds, is_async) == 1) { |
226 | /* completed bulk section for this device */ | |
227 | bmds->bulk_completed = 1; | |
c163b5ca | 228 | } |
229 | return 1; | |
230 | } | |
231 | } | |
a55eb92c | 232 | |
c163b5ca | 233 | /* we reached here means bulk is completed */ |
c163b5ca | 234 | return 0; |
c163b5ca | 235 | } |
236 | ||
237 | #define MAX_NUM_BLOCKS 4 | |
238 | ||
239 | static void blk_mig_save_dirty_blocks(QEMUFile *f) | |
240 | { | |
241 | BlkMigDevState *bmds; | |
13f0b67f | 242 | BlkMigBlock blk; |
c163b5ca | 243 | int64_t sector; |
a55eb92c | 244 | |
13f0b67f | 245 | blk.buf = qemu_malloc(BLOCK_SIZE); |
575a58d7 | 246 | |
5e5328be | 247 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
a55eb92c JK |
248 | for (sector = 0; sector < bmds->cur_sector;) { |
249 | if (bdrv_get_dirty(bmds->bs, sector)) { | |
13f0b67f | 250 | if (bdrv_read(bmds->bs, sector, blk.buf, |
6ea44308 | 251 | BDRV_SECTORS_PER_DIRTY_CHUNK) < 0) { |
13f0b67f | 252 | printf("Error reading sector %" PRId64 "\n", sector); |
4b640365 JK |
253 | qemu_file_set_error(f); |
254 | qemu_free(blk.buf); | |
255 | return; | |
c163b5ca | 256 | } |
13f0b67f JK |
257 | blk.bmds = bmds; |
258 | blk.sector = sector; | |
259 | blk_send(f, &blk); | |
a55eb92c JK |
260 | |
261 | bdrv_reset_dirty(bmds->bs, sector, | |
6ea44308 | 262 | BDRV_SECTORS_PER_DIRTY_CHUNK); |
a55eb92c | 263 | } |
6ea44308 | 264 | sector += BDRV_SECTORS_PER_DIRTY_CHUNK; |
c163b5ca | 265 | } |
266 | } | |
575a58d7 | 267 | |
13f0b67f | 268 | qemu_free(blk.buf); |
c163b5ca | 269 | } |
270 | ||
271 | static void flush_blks(QEMUFile* f) | |
272 | { | |
5e5328be | 273 | BlkMigBlock *blk; |
a55eb92c | 274 | |
d11ecd3d JK |
275 | dprintf("%s Enter submitted %d read_done %d transferred %d\n", |
276 | __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done, | |
277 | block_mig_state.transferred); | |
a55eb92c | 278 | |
5e5328be JK |
279 | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
280 | if (qemu_file_rate_limit(f)) { | |
281 | break; | |
282 | } | |
4b640365 JK |
283 | if (blk->ret < 0) { |
284 | qemu_file_set_error(f); | |
285 | break; | |
286 | } | |
13f0b67f | 287 | blk_send(f, blk); |
a55eb92c | 288 | |
5e5328be | 289 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); |
c163b5ca | 290 | qemu_free(blk->buf); |
291 | qemu_free(blk); | |
a55eb92c | 292 | |
d11ecd3d JK |
293 | block_mig_state.read_done--; |
294 | block_mig_state.transferred++; | |
295 | assert(block_mig_state.read_done >= 0); | |
c163b5ca | 296 | } |
c163b5ca | 297 | |
d11ecd3d JK |
298 | dprintf("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__, |
299 | block_mig_state.submitted, block_mig_state.read_done, | |
300 | block_mig_state.transferred); | |
c163b5ca | 301 | } |
302 | ||
303 | static int is_stage2_completed(void) | |
304 | { | |
305 | BlkMigDevState *bmds; | |
a55eb92c | 306 | |
d11ecd3d | 307 | if (block_mig_state.submitted > 0) { |
c163b5ca | 308 | return 0; |
309 | } | |
a55eb92c | 310 | |
5e5328be | 311 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
a55eb92c | 312 | if (bmds->bulk_completed == 0) { |
c163b5ca | 313 | return 0; |
314 | } | |
315 | } | |
a55eb92c | 316 | |
c163b5ca | 317 | return 1; |
318 | } | |
319 | ||
320 | static int block_save_live(QEMUFile *f, int stage, void *opaque) | |
321 | { | |
d11ecd3d JK |
322 | dprintf("Enter save live stage %d submitted %d transferred %d\n", |
323 | stage, block_mig_state.submitted, block_mig_state.transferred); | |
a55eb92c | 324 | |
d11ecd3d | 325 | if (block_mig_state.blk_enable != 1) { |
c163b5ca | 326 | /* no need to migrate storage */ |
a55eb92c | 327 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
c163b5ca | 328 | return 1; |
329 | } | |
a55eb92c JK |
330 | |
331 | if (stage == 1) { | |
c163b5ca | 332 | init_blk_migration(f); |
a55eb92c | 333 | |
c163b5ca | 334 | /* start track dirty blocks */ |
335 | set_dirty_tracking(1); | |
c163b5ca | 336 | } |
337 | ||
338 | flush_blks(f); | |
a55eb92c | 339 | |
4b640365 JK |
340 | if (qemu_file_has_error(f)) { |
341 | set_dirty_tracking(0); | |
342 | return 0; | |
343 | } | |
344 | ||
c163b5ca | 345 | /* control the rate of transfer */ |
d11ecd3d JK |
346 | while ((block_mig_state.submitted + |
347 | block_mig_state.read_done) * BLOCK_SIZE < | |
a55eb92c JK |
348 | qemu_file_get_rate_limit(f)) { |
349 | if (blk_mig_save_bulked_block(f, 1) == 0) { | |
350 | /* no more bulk blocks for now */ | |
c163b5ca | 351 | break; |
a55eb92c | 352 | } |
c163b5ca | 353 | } |
a55eb92c | 354 | |
c163b5ca | 355 | flush_blks(f); |
a55eb92c | 356 | |
4b640365 JK |
357 | if (qemu_file_has_error(f)) { |
358 | set_dirty_tracking(0); | |
359 | return 0; | |
360 | } | |
361 | ||
a55eb92c JK |
362 | if (stage == 3) { |
363 | while (blk_mig_save_bulked_block(f, 0) != 0) { | |
364 | /* empty */ | |
365 | } | |
366 | ||
c163b5ca | 367 | blk_mig_save_dirty_blocks(f); |
a55eb92c | 368 | |
c163b5ca | 369 | /* stop track dirty blocks */ |
a55eb92c JK |
370 | set_dirty_tracking(0); |
371 | ||
4b640365 JK |
372 | if (qemu_file_has_error(f)) { |
373 | return 0; | |
374 | } | |
375 | ||
a55eb92c | 376 | printf("\nBlock migration completed\n"); |
c163b5ca | 377 | } |
a55eb92c JK |
378 | |
379 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); | |
380 | ||
c163b5ca | 381 | return ((stage == 2) && is_stage2_completed()); |
382 | } | |
383 | ||
384 | static int block_load(QEMUFile *f, void *opaque, int version_id) | |
385 | { | |
386 | int len, flags; | |
387 | char device_name[256]; | |
388 | int64_t addr; | |
389 | BlockDriverState *bs; | |
390 | uint8_t *buf; | |
a55eb92c | 391 | |
c163b5ca | 392 | do { |
c163b5ca | 393 | addr = qemu_get_be64(f); |
a55eb92c | 394 | |
6ea44308 JK |
395 | flags = addr & ~BDRV_SECTOR_MASK; |
396 | addr >>= BDRV_SECTOR_BITS; | |
a55eb92c JK |
397 | |
398 | if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) { | |
c163b5ca | 399 | /* get device name */ |
400 | len = qemu_get_byte(f); | |
c163b5ca | 401 | qemu_get_buffer(f, (uint8_t *)device_name, len); |
402 | device_name[len] = '\0'; | |
a55eb92c | 403 | |
c163b5ca | 404 | bs = bdrv_find(device_name); |
4b640365 JK |
405 | if (!bs) { |
406 | fprintf(stderr, "Error unknown block device %s\n", | |
407 | device_name); | |
408 | return -EINVAL; | |
409 | } | |
a55eb92c | 410 | |
575a58d7 JK |
411 | buf = qemu_malloc(BLOCK_SIZE); |
412 | ||
a55eb92c | 413 | qemu_get_buffer(f, buf, BLOCK_SIZE); |
4b640365 | 414 | bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK); |
575a58d7 JK |
415 | |
416 | qemu_free(buf); | |
a55eb92c | 417 | } else if (!(flags & BLK_MIG_FLAG_EOS)) { |
4b640365 JK |
418 | fprintf(stderr, "Unknown flags\n"); |
419 | return -EINVAL; | |
420 | } | |
421 | if (qemu_file_has_error(f)) { | |
422 | return -EIO; | |
c163b5ca | 423 | } |
a55eb92c JK |
424 | } while (!(flags & BLK_MIG_FLAG_EOS)); |
425 | ||
c163b5ca | 426 | return 0; |
427 | } | |
428 | ||
429 | static void block_set_params(int blk_enable, int shared_base, void *opaque) | |
430 | { | |
d11ecd3d JK |
431 | block_mig_state.blk_enable = blk_enable; |
432 | block_mig_state.shared_base = shared_base; | |
a55eb92c | 433 | |
c163b5ca | 434 | /* shared base means that blk_enable = 1 */ |
d11ecd3d | 435 | block_mig_state.blk_enable |= shared_base; |
c163b5ca | 436 | } |
437 | ||
c163b5ca | 438 | void blk_mig_init(void) |
a55eb92c | 439 | { |
5e5328be JK |
440 | QSIMPLEQ_INIT(&block_mig_state.bmds_list); |
441 | QSIMPLEQ_INIT(&block_mig_state.blk_list); | |
442 | ||
a55eb92c | 443 | register_savevm_live("block", 0, 1, block_set_params, block_save_live, |
d11ecd3d | 444 | NULL, block_load, &block_mig_state); |
c163b5ca | 445 | } |