]>
Commit | Line | Data |
---|---|---|
c163b5ca LS |
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 LS |
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 LS |
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 LS |
35 | do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0) |
36 | #else | |
a55eb92c | 37 | #define dprintf(fmt, ...) \ |
c163b5ca LS |
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 LS |
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 LS |
60 | } BlkMigBlock; |
61 | ||
62 | typedef struct BlkMigState { | |
c163b5ca LS |
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 LS |
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 LS |
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 LS |
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 LS |
116 | cur_sector += nr_sectors; |
117 | } | |
118 | } | |
a55eb92c JK |
119 | |
120 | if (cur_sector >= total_sectors) { | |
57cce12d | 121 | bmds->cur_sector = total_sectors; |
c163b5ca LS |
122 | return 1; |
123 | } | |
a55eb92c | 124 | |
d11ecd3d | 125 | if (cur_sector >= block_mig_state.print_completion) { |
c163b5ca LS |
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 LS |
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 LS |
178 | } |
179 | ||
c163b5ca LS |
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 LS |
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 LS |
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 LS |
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 LS |
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 LS |
228 | } |
229 | return 1; | |
230 | } | |
231 | } | |
a55eb92c | 232 | |
c163b5ca | 233 | /* we reached here means bulk is completed */ |
c163b5ca | 234 | return 0; |
c163b5ca LS |
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 LS |
265 | } |
266 | } | |
575a58d7 | 267 | |
13f0b67f | 268 | qemu_free(blk.buf); |
c163b5ca LS |
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 LS |
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 LS |
301 | } |
302 | ||
303 | static int is_stage2_completed(void) | |
304 | { | |
305 | BlkMigDevState *bmds; | |
a55eb92c | 306 | |
d11ecd3d | 307 | if (block_mig_state.submitted > 0) { |
c163b5ca LS |
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 LS |
313 | return 0; |
314 | } | |
315 | } | |
a55eb92c | 316 | |
c163b5ca LS |
317 | return 1; |
318 | } | |
319 | ||
4ec7fcc7 JK |
320 | static void blk_mig_cleanup(void) |
321 | { | |
322 | BlkMigDevState *bmds, *next_bmds; | |
323 | BlkMigBlock *blk, *next_blk; | |
324 | ||
325 | QTAILQ_FOREACH_SAFE(bmds, &block_mig_state.dev_list, entry, next_bmds) { | |
326 | QTAILQ_REMOVE(&block_mig_state.dev_list, bmds, entry); | |
327 | qemu_free(bmds); | |
328 | } | |
329 | ||
330 | QTAILQ_FOREACH_SAFE(blk, &block_mig_state.blk_list, entry, next_blk) { | |
331 | QTAILQ_REMOVE(&block_mig_state.blk_list, blk, entry); | |
332 | qemu_free(blk->buf); | |
333 | qemu_free(blk); | |
334 | } | |
335 | ||
336 | set_dirty_tracking(0); | |
337 | ||
338 | printf("\n"); | |
339 | } | |
340 | ||
c163b5ca LS |
341 | static int block_save_live(QEMUFile *f, int stage, void *opaque) |
342 | { | |
d11ecd3d JK |
343 | dprintf("Enter save live stage %d submitted %d transferred %d\n", |
344 | stage, block_mig_state.submitted, block_mig_state.transferred); | |
a55eb92c | 345 | |
4ec7fcc7 JK |
346 | if (stage < 0) { |
347 | blk_mig_cleanup(); | |
348 | return 0; | |
349 | } | |
350 | ||
d11ecd3d | 351 | if (block_mig_state.blk_enable != 1) { |
c163b5ca | 352 | /* no need to migrate storage */ |
a55eb92c | 353 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
c163b5ca LS |
354 | return 1; |
355 | } | |
a55eb92c JK |
356 | |
357 | if (stage == 1) { | |
c163b5ca | 358 | init_blk_migration(f); |
a55eb92c | 359 | |
c163b5ca LS |
360 | /* start track dirty blocks */ |
361 | set_dirty_tracking(1); | |
c163b5ca LS |
362 | } |
363 | ||
364 | flush_blks(f); | |
a55eb92c | 365 | |
4b640365 | 366 | if (qemu_file_has_error(f)) { |
4ec7fcc7 | 367 | blk_mig_cleanup(); |
4b640365 JK |
368 | return 0; |
369 | } | |
370 | ||
c163b5ca | 371 | /* control the rate of transfer */ |
d11ecd3d JK |
372 | while ((block_mig_state.submitted + |
373 | block_mig_state.read_done) * BLOCK_SIZE < | |
a55eb92c JK |
374 | qemu_file_get_rate_limit(f)) { |
375 | if (blk_mig_save_bulked_block(f, 1) == 0) { | |
376 | /* no more bulk blocks for now */ | |
c163b5ca | 377 | break; |
a55eb92c | 378 | } |
c163b5ca | 379 | } |
a55eb92c | 380 | |
c163b5ca | 381 | flush_blks(f); |
a55eb92c | 382 | |
4b640365 | 383 | if (qemu_file_has_error(f)) { |
4ec7fcc7 | 384 | blk_mig_cleanup(); |
4b640365 JK |
385 | return 0; |
386 | } | |
387 | ||
a55eb92c JK |
388 | if (stage == 3) { |
389 | while (blk_mig_save_bulked_block(f, 0) != 0) { | |
390 | /* empty */ | |
391 | } | |
392 | ||
c163b5ca | 393 | blk_mig_save_dirty_blocks(f); |
4ec7fcc7 | 394 | blk_mig_cleanup(); |
a55eb92c | 395 | |
4b640365 JK |
396 | if (qemu_file_has_error(f)) { |
397 | return 0; | |
398 | } | |
399 | ||
4ec7fcc7 | 400 | printf("Block migration completed\n"); |
c163b5ca | 401 | } |
a55eb92c JK |
402 | |
403 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); | |
404 | ||
c163b5ca LS |
405 | return ((stage == 2) && is_stage2_completed()); |
406 | } | |
407 | ||
408 | static int block_load(QEMUFile *f, void *opaque, int version_id) | |
409 | { | |
410 | int len, flags; | |
411 | char device_name[256]; | |
412 | int64_t addr; | |
413 | BlockDriverState *bs; | |
414 | uint8_t *buf; | |
a55eb92c | 415 | |
c163b5ca | 416 | do { |
c163b5ca | 417 | addr = qemu_get_be64(f); |
a55eb92c | 418 | |
6ea44308 JK |
419 | flags = addr & ~BDRV_SECTOR_MASK; |
420 | addr >>= BDRV_SECTOR_BITS; | |
a55eb92c JK |
421 | |
422 | if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) { | |
c163b5ca LS |
423 | /* get device name */ |
424 | len = qemu_get_byte(f); | |
c163b5ca LS |
425 | qemu_get_buffer(f, (uint8_t *)device_name, len); |
426 | device_name[len] = '\0'; | |
a55eb92c | 427 | |
c163b5ca | 428 | bs = bdrv_find(device_name); |
4b640365 JK |
429 | if (!bs) { |
430 | fprintf(stderr, "Error unknown block device %s\n", | |
431 | device_name); | |
432 | return -EINVAL; | |
433 | } | |
a55eb92c | 434 | |
575a58d7 JK |
435 | buf = qemu_malloc(BLOCK_SIZE); |
436 | ||
a55eb92c | 437 | qemu_get_buffer(f, buf, BLOCK_SIZE); |
4b640365 | 438 | bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK); |
575a58d7 JK |
439 | |
440 | qemu_free(buf); | |
a55eb92c | 441 | } else if (!(flags & BLK_MIG_FLAG_EOS)) { |
4b640365 JK |
442 | fprintf(stderr, "Unknown flags\n"); |
443 | return -EINVAL; | |
444 | } | |
445 | if (qemu_file_has_error(f)) { | |
446 | return -EIO; | |
c163b5ca | 447 | } |
a55eb92c JK |
448 | } while (!(flags & BLK_MIG_FLAG_EOS)); |
449 | ||
c163b5ca LS |
450 | return 0; |
451 | } | |
452 | ||
453 | static void block_set_params(int blk_enable, int shared_base, void *opaque) | |
454 | { | |
d11ecd3d JK |
455 | block_mig_state.blk_enable = blk_enable; |
456 | block_mig_state.shared_base = shared_base; | |
a55eb92c | 457 | |
c163b5ca | 458 | /* shared base means that blk_enable = 1 */ |
d11ecd3d | 459 | block_mig_state.blk_enable |= shared_base; |
c163b5ca LS |
460 | } |
461 | ||
c163b5ca | 462 | void blk_mig_init(void) |
a55eb92c | 463 | { |
5e5328be JK |
464 | QSIMPLEQ_INIT(&block_mig_state.bmds_list); |
465 | QSIMPLEQ_INIT(&block_mig_state.blk_list); | |
466 | ||
a55eb92c | 467 | register_savevm_live("block", 0, 1, block_set_params, block_save_live, |
d11ecd3d | 468 | NULL, block_load, &block_mig_state); |
c163b5ca | 469 | } |