]>
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" |
889ae39c | 18 | #include "qemu-timer.h" |
7184049e | 19 | #include "monitor.h" |
c163b5ca | 20 | #include "block-migration.h" |
889ae39c | 21 | #include "migration.h" |
c163b5ca | 22 | #include <assert.h> |
c163b5ca | 23 | |
6ea44308 | 24 | #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS) |
c163b5ca | 25 | |
26 | #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01 | |
27 | #define BLK_MIG_FLAG_EOS 0x02 | |
01e61e2d | 28 | #define BLK_MIG_FLAG_PROGRESS 0x04 |
c163b5ca | 29 | |
30 | #define MAX_IS_ALLOCATED_SEARCH 65536 | |
c163b5ca | 31 | |
32 | //#define DEBUG_BLK_MIGRATION | |
33 | ||
34 | #ifdef DEBUG_BLK_MIGRATION | |
d0f2c4c6 | 35 | #define DPRINTF(fmt, ...) \ |
c163b5ca | 36 | do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0) |
37 | #else | |
d0f2c4c6 | 38 | #define DPRINTF(fmt, ...) \ |
c163b5ca | 39 | do { } while (0) |
40 | #endif | |
41 | ||
a55eb92c JK |
42 | typedef struct BlkMigDevState { |
43 | BlockDriverState *bs; | |
44 | int bulk_completed; | |
45 | int shared_base; | |
a55eb92c | 46 | int64_t cur_sector; |
d76cac7d | 47 | int64_t cur_dirty; |
82801d8f | 48 | int64_t completed_sectors; |
a55eb92c JK |
49 | int64_t total_sectors; |
50 | int64_t dirty; | |
5e5328be | 51 | QSIMPLEQ_ENTRY(BlkMigDevState) entry; |
33656af7 | 52 | unsigned long *aio_bitmap; |
a55eb92c JK |
53 | } BlkMigDevState; |
54 | ||
c163b5ca | 55 | typedef struct BlkMigBlock { |
56 | uint8_t *buf; | |
57 | BlkMigDevState *bmds; | |
58 | int64_t sector; | |
33656af7 | 59 | int nr_sectors; |
c163b5ca | 60 | struct iovec iov; |
61 | QEMUIOVector qiov; | |
62 | BlockDriverAIOCB *aiocb; | |
63 | int ret; | |
889ae39c | 64 | int64_t time; |
5e5328be | 65 | QSIMPLEQ_ENTRY(BlkMigBlock) entry; |
c163b5ca | 66 | } BlkMigBlock; |
67 | ||
68 | typedef struct BlkMigState { | |
c163b5ca | 69 | int blk_enable; |
70 | int shared_base; | |
5e5328be JK |
71 | QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list; |
72 | QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list; | |
c163b5ca | 73 | int submitted; |
74 | int read_done; | |
75 | int transferred; | |
82801d8f | 76 | int64_t total_sector_sum; |
01e61e2d | 77 | int prev_progress; |
e970ec0b | 78 | int bulk_completed; |
889ae39c LS |
79 | long double total_time; |
80 | int reads; | |
c163b5ca | 81 | } BlkMigState; |
82 | ||
d11ecd3d | 83 | static BlkMigState block_mig_state; |
c163b5ca | 84 | |
13f0b67f JK |
85 | static void blk_send(QEMUFile *f, BlkMigBlock * blk) |
86 | { | |
87 | int len; | |
88 | ||
89 | /* sector number and flags */ | |
90 | qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS) | |
91 | | BLK_MIG_FLAG_DEVICE_BLOCK); | |
92 | ||
93 | /* device name */ | |
94 | len = strlen(blk->bmds->bs->device_name); | |
95 | qemu_put_byte(f, len); | |
96 | qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len); | |
97 | ||
98 | qemu_put_buffer(f, blk->buf, BLOCK_SIZE); | |
99 | } | |
100 | ||
25f23643 JK |
101 | int blk_mig_active(void) |
102 | { | |
103 | return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list); | |
104 | } | |
105 | ||
106 | uint64_t blk_mig_bytes_transferred(void) | |
107 | { | |
108 | BlkMigDevState *bmds; | |
109 | uint64_t sum = 0; | |
110 | ||
111 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
112 | sum += bmds->completed_sectors; | |
113 | } | |
114 | return sum << BDRV_SECTOR_BITS; | |
115 | } | |
116 | ||
117 | uint64_t blk_mig_bytes_remaining(void) | |
118 | { | |
119 | return blk_mig_bytes_total() - blk_mig_bytes_transferred(); | |
120 | } | |
121 | ||
122 | uint64_t blk_mig_bytes_total(void) | |
123 | { | |
124 | BlkMigDevState *bmds; | |
125 | uint64_t sum = 0; | |
126 | ||
127 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
128 | sum += bmds->total_sectors; | |
129 | } | |
130 | return sum << BDRV_SECTOR_BITS; | |
131 | } | |
132 | ||
889ae39c LS |
133 | static inline void add_avg_read_time(int64_t time) |
134 | { | |
135 | block_mig_state.reads++; | |
136 | block_mig_state.total_time += time; | |
137 | } | |
138 | ||
139 | static inline long double compute_read_bwidth(void) | |
140 | { | |
141 | assert(block_mig_state.total_time != 0); | |
142 | return (block_mig_state.reads * BLOCK_SIZE)/ block_mig_state.total_time; | |
143 | } | |
144 | ||
33656af7 MT |
145 | static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector) |
146 | { | |
147 | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; | |
148 | ||
149 | if (bmds->aio_bitmap && | |
150 | (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) { | |
151 | return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] & | |
152 | (1UL << (chunk % (sizeof(unsigned long) * 8)))); | |
153 | } else { | |
154 | return 0; | |
155 | } | |
156 | } | |
157 | ||
158 | static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num, | |
159 | int nb_sectors, int set) | |
160 | { | |
161 | int64_t start, end; | |
162 | unsigned long val, idx, bit; | |
163 | ||
164 | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; | |
165 | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; | |
166 | ||
167 | for (; start <= end; start++) { | |
168 | idx = start / (sizeof(unsigned long) * 8); | |
169 | bit = start % (sizeof(unsigned long) * 8); | |
170 | val = bmds->aio_bitmap[idx]; | |
171 | if (set) { | |
172 | if (!(val & (1UL << bit))) { | |
173 | val |= 1UL << bit; | |
174 | } | |
175 | } else { | |
176 | if (val & (1UL << bit)) { | |
177 | val &= ~(1UL << bit); | |
178 | } | |
179 | } | |
180 | bmds->aio_bitmap[idx] = val; | |
181 | } | |
182 | } | |
183 | ||
184 | static void alloc_aio_bitmap(BlkMigDevState *bmds) | |
185 | { | |
186 | BlockDriverState *bs = bmds->bs; | |
187 | int64_t bitmap_size; | |
188 | ||
189 | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + | |
190 | BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; | |
191 | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; | |
192 | ||
193 | bmds->aio_bitmap = qemu_mallocz(bitmap_size); | |
194 | } | |
195 | ||
c163b5ca | 196 | static void blk_mig_read_cb(void *opaque, int ret) |
197 | { | |
198 | BlkMigBlock *blk = opaque; | |
a55eb92c | 199 | |
c163b5ca | 200 | blk->ret = ret; |
a55eb92c | 201 | |
889ae39c LS |
202 | blk->time = qemu_get_clock_ns(rt_clock) - blk->time; |
203 | ||
204 | add_avg_read_time(blk->time); | |
205 | ||
5e5328be | 206 | QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry); |
33656af7 | 207 | bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0); |
a55eb92c | 208 | |
d11ecd3d JK |
209 | block_mig_state.submitted--; |
210 | block_mig_state.read_done++; | |
211 | assert(block_mig_state.submitted >= 0); | |
c163b5ca | 212 | } |
213 | ||
7184049e | 214 | static int mig_save_device_bulk(Monitor *mon, QEMUFile *f, |
e970ec0b | 215 | BlkMigDevState *bmds) |
a55eb92c | 216 | { |
57cce12d JK |
217 | int64_t total_sectors = bmds->total_sectors; |
218 | int64_t cur_sector = bmds->cur_sector; | |
219 | BlockDriverState *bs = bmds->bs; | |
c163b5ca | 220 | BlkMigBlock *blk; |
13f0b67f | 221 | int nr_sectors; |
a55eb92c | 222 | |
57cce12d | 223 | if (bmds->shared_base) { |
b1d10856 | 224 | while (cur_sector < total_sectors && |
57cce12d JK |
225 | !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH, |
226 | &nr_sectors)) { | |
c163b5ca | 227 | cur_sector += nr_sectors; |
228 | } | |
229 | } | |
a55eb92c JK |
230 | |
231 | if (cur_sector >= total_sectors) { | |
82801d8f | 232 | bmds->cur_sector = bmds->completed_sectors = total_sectors; |
c163b5ca | 233 | return 1; |
234 | } | |
a55eb92c | 235 | |
82801d8f | 236 | bmds->completed_sectors = cur_sector; |
a55eb92c | 237 | |
57cce12d JK |
238 | cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1); |
239 | ||
6ea44308 JK |
240 | /* we are going to transfer a full block even if it is not allocated */ |
241 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; | |
c163b5ca | 242 | |
6ea44308 | 243 | if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) { |
57cce12d | 244 | nr_sectors = total_sectors - cur_sector; |
c163b5ca | 245 | } |
a55eb92c | 246 | |
13f0b67f JK |
247 | blk = qemu_malloc(sizeof(BlkMigBlock)); |
248 | blk->buf = qemu_malloc(BLOCK_SIZE); | |
249 | blk->bmds = bmds; | |
250 | blk->sector = cur_sector; | |
33656af7 | 251 | blk->nr_sectors = nr_sectors; |
a55eb92c | 252 | |
e970ec0b LS |
253 | blk->iov.iov_base = blk->buf; |
254 | blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE; | |
255 | qemu_iovec_init_external(&blk->qiov, &blk->iov, 1); | |
a55eb92c | 256 | |
889ae39c LS |
257 | blk->time = qemu_get_clock_ns(rt_clock); |
258 | ||
e970ec0b LS |
259 | blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov, |
260 | nr_sectors, blk_mig_read_cb, blk); | |
261 | if (!blk->aiocb) { | |
262 | goto error; | |
c163b5ca | 263 | } |
e970ec0b | 264 | block_mig_state.submitted++; |
d76cac7d | 265 | |
13f0b67f JK |
266 | bdrv_reset_dirty(bs, cur_sector, nr_sectors); |
267 | bmds->cur_sector = cur_sector + nr_sectors; | |
a55eb92c | 268 | |
13f0b67f | 269 | return (bmds->cur_sector >= total_sectors); |
4b640365 JK |
270 | |
271 | error: | |
7184049e | 272 | monitor_printf(mon, "Error reading sector %" PRId64 "\n", cur_sector); |
4b640365 JK |
273 | qemu_file_set_error(f); |
274 | qemu_free(blk->buf); | |
275 | qemu_free(blk); | |
276 | return 0; | |
c163b5ca | 277 | } |
278 | ||
c163b5ca | 279 | static void set_dirty_tracking(int enable) |
280 | { | |
281 | BlkMigDevState *bmds; | |
5e5328be JK |
282 | |
283 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
a55eb92c | 284 | bdrv_set_dirty_tracking(bmds->bs, enable); |
c163b5ca | 285 | } |
c163b5ca | 286 | } |
287 | ||
b66460e4 | 288 | static void init_blk_migration_it(void *opaque, BlockDriverState *bs) |
c163b5ca | 289 | { |
b66460e4 | 290 | Monitor *mon = opaque; |
5e5328be | 291 | BlkMigDevState *bmds; |
792773b2 | 292 | int64_t sectors; |
a55eb92c | 293 | |
d246673d | 294 | if (!bdrv_is_read_only(bs)) { |
b66460e4 | 295 | sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
31f54f24 | 296 | if (sectors <= 0) { |
b66460e4 SH |
297 | return; |
298 | } | |
299 | ||
300 | bmds = qemu_mallocz(sizeof(BlkMigDevState)); | |
301 | bmds->bs = bs; | |
302 | bmds->bulk_completed = 0; | |
303 | bmds->total_sectors = sectors; | |
304 | bmds->completed_sectors = 0; | |
305 | bmds->shared_base = block_mig_state.shared_base; | |
33656af7 | 306 | alloc_aio_bitmap(bmds); |
b66460e4 SH |
307 | |
308 | block_mig_state.total_sector_sum += sectors; | |
309 | ||
310 | if (bmds->shared_base) { | |
311 | monitor_printf(mon, "Start migration for %s with shared base " | |
312 | "image\n", | |
313 | bs->device_name); | |
314 | } else { | |
315 | monitor_printf(mon, "Start full migration for %s\n", | |
316 | bs->device_name); | |
317 | } | |
318 | ||
319 | QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry); | |
320 | } | |
321 | } | |
322 | ||
323 | static void init_blk_migration(Monitor *mon, QEMUFile *f) | |
324 | { | |
69d63a97 JK |
325 | block_mig_state.submitted = 0; |
326 | block_mig_state.read_done = 0; | |
327 | block_mig_state.transferred = 0; | |
82801d8f | 328 | block_mig_state.total_sector_sum = 0; |
01e61e2d | 329 | block_mig_state.prev_progress = -1; |
e970ec0b | 330 | block_mig_state.bulk_completed = 0; |
889ae39c LS |
331 | block_mig_state.total_time = 0; |
332 | block_mig_state.reads = 0; | |
69d63a97 | 333 | |
b66460e4 | 334 | bdrv_iterate(init_blk_migration_it, mon); |
c163b5ca | 335 | } |
336 | ||
e970ec0b | 337 | static int blk_mig_save_bulked_block(Monitor *mon, QEMUFile *f) |
c163b5ca | 338 | { |
82801d8f | 339 | int64_t completed_sector_sum = 0; |
c163b5ca | 340 | BlkMigDevState *bmds; |
01e61e2d | 341 | int progress; |
82801d8f | 342 | int ret = 0; |
c163b5ca | 343 | |
5e5328be | 344 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
a55eb92c | 345 | if (bmds->bulk_completed == 0) { |
e970ec0b | 346 | if (mig_save_device_bulk(mon, f, bmds) == 1) { |
57cce12d JK |
347 | /* completed bulk section for this device */ |
348 | bmds->bulk_completed = 1; | |
c163b5ca | 349 | } |
82801d8f JK |
350 | completed_sector_sum += bmds->completed_sectors; |
351 | ret = 1; | |
352 | break; | |
353 | } else { | |
354 | completed_sector_sum += bmds->completed_sectors; | |
c163b5ca | 355 | } |
356 | } | |
a55eb92c | 357 | |
01e61e2d JK |
358 | progress = completed_sector_sum * 100 / block_mig_state.total_sector_sum; |
359 | if (progress != block_mig_state.prev_progress) { | |
360 | block_mig_state.prev_progress = progress; | |
361 | qemu_put_be64(f, (progress << BDRV_SECTOR_BITS) | |
362 | | BLK_MIG_FLAG_PROGRESS); | |
363 | monitor_printf(mon, "Completed %d %%\r", progress); | |
7184049e | 364 | monitor_flush(mon); |
82801d8f JK |
365 | } |
366 | ||
367 | return ret; | |
c163b5ca | 368 | } |
369 | ||
d76cac7d | 370 | static void blk_mig_reset_dirty_cursor(void) |
c163b5ca | 371 | { |
372 | BlkMigDevState *bmds; | |
d76cac7d LS |
373 | |
374 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
375 | bmds->cur_dirty = 0; | |
376 | } | |
377 | } | |
378 | ||
379 | static int mig_save_device_dirty(Monitor *mon, QEMUFile *f, | |
380 | BlkMigDevState *bmds, int is_async) | |
381 | { | |
382 | BlkMigBlock *blk; | |
383 | int64_t total_sectors = bmds->total_sectors; | |
c163b5ca | 384 | int64_t sector; |
d76cac7d | 385 | int nr_sectors; |
a55eb92c | 386 | |
d76cac7d | 387 | for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) { |
33656af7 MT |
388 | if (bmds_aio_inflight(bmds, sector)) |
389 | qemu_aio_flush(); | |
d76cac7d | 390 | if (bdrv_get_dirty(bmds->bs, sector)) { |
575a58d7 | 391 | |
d76cac7d LS |
392 | if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) { |
393 | nr_sectors = total_sectors - sector; | |
394 | } else { | |
395 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; | |
396 | } | |
397 | blk = qemu_malloc(sizeof(BlkMigBlock)); | |
398 | blk->buf = qemu_malloc(BLOCK_SIZE); | |
399 | blk->bmds = bmds; | |
400 | blk->sector = sector; | |
33656af7 | 401 | blk->nr_sectors = nr_sectors; |
d76cac7d | 402 | |
889ae39c | 403 | if (is_async) { |
d76cac7d LS |
404 | blk->iov.iov_base = blk->buf; |
405 | blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE; | |
406 | qemu_iovec_init_external(&blk->qiov, &blk->iov, 1); | |
407 | ||
bd0858bb | 408 | blk->time = qemu_get_clock_ns(rt_clock); |
889ae39c | 409 | |
d76cac7d LS |
410 | blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov, |
411 | nr_sectors, blk_mig_read_cb, blk); | |
412 | if (!blk->aiocb) { | |
413 | goto error; | |
414 | } | |
415 | block_mig_state.submitted++; | |
33656af7 | 416 | bmds_set_aio_inflight(bmds, sector, nr_sectors, 1); |
d76cac7d LS |
417 | } else { |
418 | if (bdrv_read(bmds->bs, sector, blk->buf, | |
419 | nr_sectors) < 0) { | |
420 | goto error; | |
c163b5ca | 421 | } |
d76cac7d | 422 | blk_send(f, blk); |
a55eb92c | 423 | |
d76cac7d LS |
424 | qemu_free(blk->buf); |
425 | qemu_free(blk); | |
a55eb92c | 426 | } |
d76cac7d LS |
427 | |
428 | bdrv_reset_dirty(bmds->bs, sector, nr_sectors); | |
429 | break; | |
c163b5ca | 430 | } |
d76cac7d LS |
431 | sector += BDRV_SECTORS_PER_DIRTY_CHUNK; |
432 | bmds->cur_dirty = sector; | |
c163b5ca | 433 | } |
575a58d7 | 434 | |
d76cac7d LS |
435 | return (bmds->cur_dirty >= bmds->total_sectors); |
436 | ||
889ae39c | 437 | error: |
d76cac7d LS |
438 | monitor_printf(mon, "Error reading sector %" PRId64 "\n", sector); |
439 | qemu_file_set_error(f); | |
440 | qemu_free(blk->buf); | |
441 | qemu_free(blk); | |
442 | return 0; | |
443 | } | |
444 | ||
445 | static int blk_mig_save_dirty_block(Monitor *mon, QEMUFile *f, int is_async) | |
446 | { | |
447 | BlkMigDevState *bmds; | |
448 | int ret = 0; | |
449 | ||
450 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
889ae39c | 451 | if (mig_save_device_dirty(mon, f, bmds, is_async) == 0) { |
d76cac7d LS |
452 | ret = 1; |
453 | break; | |
454 | } | |
455 | } | |
456 | ||
457 | return ret; | |
c163b5ca | 458 | } |
459 | ||
460 | static void flush_blks(QEMUFile* f) | |
461 | { | |
5e5328be | 462 | BlkMigBlock *blk; |
a55eb92c | 463 | |
d0f2c4c6 | 464 | DPRINTF("%s Enter submitted %d read_done %d transferred %d\n", |
d11ecd3d JK |
465 | __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done, |
466 | block_mig_state.transferred); | |
a55eb92c | 467 | |
5e5328be JK |
468 | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
469 | if (qemu_file_rate_limit(f)) { | |
470 | break; | |
471 | } | |
4b640365 JK |
472 | if (blk->ret < 0) { |
473 | qemu_file_set_error(f); | |
474 | break; | |
475 | } | |
13f0b67f | 476 | blk_send(f, blk); |
a55eb92c | 477 | |
5e5328be | 478 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); |
c163b5ca | 479 | qemu_free(blk->buf); |
480 | qemu_free(blk); | |
a55eb92c | 481 | |
d11ecd3d JK |
482 | block_mig_state.read_done--; |
483 | block_mig_state.transferred++; | |
484 | assert(block_mig_state.read_done >= 0); | |
c163b5ca | 485 | } |
c163b5ca | 486 | |
d0f2c4c6 | 487 | DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__, |
d11ecd3d JK |
488 | block_mig_state.submitted, block_mig_state.read_done, |
489 | block_mig_state.transferred); | |
c163b5ca | 490 | } |
491 | ||
889ae39c LS |
492 | static int64_t get_remaining_dirty(void) |
493 | { | |
494 | BlkMigDevState *bmds; | |
495 | int64_t dirty = 0; | |
496 | ||
497 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
498 | dirty += bdrv_get_dirty_count(bmds->bs); | |
499 | } | |
500 | ||
501 | return dirty * BLOCK_SIZE; | |
502 | } | |
503 | ||
c163b5ca | 504 | static int is_stage2_completed(void) |
505 | { | |
889ae39c LS |
506 | int64_t remaining_dirty; |
507 | long double bwidth; | |
508 | ||
509 | if (block_mig_state.bulk_completed == 1) { | |
510 | ||
511 | remaining_dirty = get_remaining_dirty(); | |
bd0858bb YT |
512 | if (remaining_dirty == 0) { |
513 | return 1; | |
514 | } | |
889ae39c | 515 | |
bd0858bb | 516 | bwidth = compute_read_bwidth(); |
889ae39c | 517 | |
bd0858bb | 518 | if ((remaining_dirty / bwidth) <= |
889ae39c LS |
519 | migrate_max_downtime()) { |
520 | /* finish stage2 because we think that we can finish remaing work | |
521 | below max_downtime */ | |
522 | ||
523 | return 1; | |
524 | } | |
525 | } | |
526 | ||
527 | return 0; | |
c163b5ca | 528 | } |
529 | ||
7184049e | 530 | static void blk_mig_cleanup(Monitor *mon) |
4ec7fcc7 | 531 | { |
82801d8f JK |
532 | BlkMigDevState *bmds; |
533 | BlkMigBlock *blk; | |
4ec7fcc7 | 534 | |
82801d8f JK |
535 | while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) { |
536 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry); | |
33656af7 | 537 | qemu_free(bmds->aio_bitmap); |
4ec7fcc7 JK |
538 | qemu_free(bmds); |
539 | } | |
540 | ||
82801d8f JK |
541 | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
542 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); | |
4ec7fcc7 JK |
543 | qemu_free(blk->buf); |
544 | qemu_free(blk); | |
545 | } | |
546 | ||
547 | set_dirty_tracking(0); | |
548 | ||
7184049e | 549 | monitor_printf(mon, "\n"); |
4ec7fcc7 JK |
550 | } |
551 | ||
f327aa0c | 552 | static int block_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) |
c163b5ca | 553 | { |
d0f2c4c6 | 554 | DPRINTF("Enter save live stage %d submitted %d transferred %d\n", |
d11ecd3d | 555 | stage, block_mig_state.submitted, block_mig_state.transferred); |
a55eb92c | 556 | |
4ec7fcc7 | 557 | if (stage < 0) { |
7184049e | 558 | blk_mig_cleanup(mon); |
4ec7fcc7 JK |
559 | return 0; |
560 | } | |
561 | ||
d11ecd3d | 562 | if (block_mig_state.blk_enable != 1) { |
c163b5ca | 563 | /* no need to migrate storage */ |
a55eb92c | 564 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
c163b5ca | 565 | return 1; |
566 | } | |
a55eb92c JK |
567 | |
568 | if (stage == 1) { | |
7184049e | 569 | init_blk_migration(mon, f); |
a55eb92c | 570 | |
c163b5ca | 571 | /* start track dirty blocks */ |
572 | set_dirty_tracking(1); | |
c163b5ca | 573 | } |
574 | ||
575 | flush_blks(f); | |
a55eb92c | 576 | |
4b640365 | 577 | if (qemu_file_has_error(f)) { |
7184049e | 578 | blk_mig_cleanup(mon); |
4b640365 JK |
579 | return 0; |
580 | } | |
581 | ||
d76cac7d LS |
582 | blk_mig_reset_dirty_cursor(); |
583 | ||
889ae39c | 584 | if (stage == 2) { |
d76cac7d LS |
585 | /* control the rate of transfer */ |
586 | while ((block_mig_state.submitted + | |
587 | block_mig_state.read_done) * BLOCK_SIZE < | |
588 | qemu_file_get_rate_limit(f)) { | |
589 | if (block_mig_state.bulk_completed == 0) { | |
590 | /* first finish the bulk phase */ | |
591 | if (blk_mig_save_bulked_block(mon, f) == 0) { | |
889ae39c | 592 | /* finished saving bulk on all devices */ |
d76cac7d LS |
593 | block_mig_state.bulk_completed = 1; |
594 | } | |
595 | } else { | |
596 | if (blk_mig_save_dirty_block(mon, f, 1) == 0) { | |
597 | /* no more dirty blocks */ | |
598 | break; | |
599 | } | |
600 | } | |
a55eb92c | 601 | } |
a55eb92c | 602 | |
d76cac7d | 603 | flush_blks(f); |
a55eb92c | 604 | |
d76cac7d LS |
605 | if (qemu_file_has_error(f)) { |
606 | blk_mig_cleanup(mon); | |
607 | return 0; | |
608 | } | |
4b640365 JK |
609 | } |
610 | ||
a55eb92c | 611 | if (stage == 3) { |
889ae39c LS |
612 | /* we know for sure that save bulk is completed and |
613 | all async read completed */ | |
614 | assert(block_mig_state.submitted == 0); | |
a55eb92c | 615 | |
889ae39c | 616 | while (blk_mig_save_dirty_block(mon, f, 0) != 0); |
7184049e | 617 | blk_mig_cleanup(mon); |
a55eb92c | 618 | |
01e61e2d JK |
619 | /* report completion */ |
620 | qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS); | |
621 | ||
4b640365 JK |
622 | if (qemu_file_has_error(f)) { |
623 | return 0; | |
624 | } | |
625 | ||
7184049e | 626 | monitor_printf(mon, "Block migration completed\n"); |
c163b5ca | 627 | } |
a55eb92c JK |
628 | |
629 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); | |
630 | ||
c163b5ca | 631 | return ((stage == 2) && is_stage2_completed()); |
632 | } | |
633 | ||
634 | static int block_load(QEMUFile *f, void *opaque, int version_id) | |
635 | { | |
01e61e2d | 636 | static int banner_printed; |
c163b5ca | 637 | int len, flags; |
638 | char device_name[256]; | |
639 | int64_t addr; | |
640 | BlockDriverState *bs; | |
641 | uint8_t *buf; | |
a55eb92c | 642 | |
c163b5ca | 643 | do { |
c163b5ca | 644 | addr = qemu_get_be64(f); |
a55eb92c | 645 | |
6ea44308 JK |
646 | flags = addr & ~BDRV_SECTOR_MASK; |
647 | addr >>= BDRV_SECTOR_BITS; | |
a55eb92c JK |
648 | |
649 | if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) { | |
b02bea3a | 650 | int ret; |
c163b5ca | 651 | /* get device name */ |
652 | len = qemu_get_byte(f); | |
c163b5ca | 653 | qemu_get_buffer(f, (uint8_t *)device_name, len); |
654 | device_name[len] = '\0'; | |
a55eb92c | 655 | |
c163b5ca | 656 | bs = bdrv_find(device_name); |
4b640365 JK |
657 | if (!bs) { |
658 | fprintf(stderr, "Error unknown block device %s\n", | |
659 | device_name); | |
660 | return -EINVAL; | |
661 | } | |
a55eb92c | 662 | |
575a58d7 JK |
663 | buf = qemu_malloc(BLOCK_SIZE); |
664 | ||
a55eb92c | 665 | qemu_get_buffer(f, buf, BLOCK_SIZE); |
b02bea3a | 666 | ret = bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK); |
575a58d7 JK |
667 | |
668 | qemu_free(buf); | |
b02bea3a YT |
669 | if (ret < 0) { |
670 | return ret; | |
671 | } | |
01e61e2d JK |
672 | } else if (flags & BLK_MIG_FLAG_PROGRESS) { |
673 | if (!banner_printed) { | |
674 | printf("Receiving block device images\n"); | |
675 | banner_printed = 1; | |
676 | } | |
677 | printf("Completed %d %%%c", (int)addr, | |
678 | (addr == 100) ? '\n' : '\r'); | |
679 | fflush(stdout); | |
a55eb92c | 680 | } else if (!(flags & BLK_MIG_FLAG_EOS)) { |
4b640365 JK |
681 | fprintf(stderr, "Unknown flags\n"); |
682 | return -EINVAL; | |
683 | } | |
684 | if (qemu_file_has_error(f)) { | |
685 | return -EIO; | |
c163b5ca | 686 | } |
a55eb92c JK |
687 | } while (!(flags & BLK_MIG_FLAG_EOS)); |
688 | ||
c163b5ca | 689 | return 0; |
690 | } | |
691 | ||
692 | static void block_set_params(int blk_enable, int shared_base, void *opaque) | |
693 | { | |
d11ecd3d JK |
694 | block_mig_state.blk_enable = blk_enable; |
695 | block_mig_state.shared_base = shared_base; | |
a55eb92c | 696 | |
c163b5ca | 697 | /* shared base means that blk_enable = 1 */ |
d11ecd3d | 698 | block_mig_state.blk_enable |= shared_base; |
c163b5ca | 699 | } |
700 | ||
c163b5ca | 701 | void blk_mig_init(void) |
a55eb92c | 702 | { |
5e5328be JK |
703 | QSIMPLEQ_INIT(&block_mig_state.bmds_list); |
704 | QSIMPLEQ_INIT(&block_mig_state.blk_list); | |
705 | ||
0be71e32 AW |
706 | register_savevm_live(NULL, "block", 0, 1, block_set_params, |
707 | block_save_live, NULL, block_load, &block_mig_state); | |
c163b5ca | 708 | } |