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