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