]> git.proxmox.com Git - qemu.git/blob - block-migration.c
15b76de9bcc4024aae83913b6f0dee9069463ecd
[qemu.git] / block-migration.c
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"
17 #include "qemu-queue.h"
18 #include "block-migration.h"
19 #include <assert.h>
20
21 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
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
34 #define dprintf(fmt, ...) \
35 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
36 #else
37 #define dprintf(fmt, ...) \
38 do { } while (0)
39 #endif
40
41 typedef struct BlkMigDevState {
42 BlockDriverState *bs;
43 int bulk_completed;
44 int shared_base;
45 int64_t cur_sector;
46 int64_t completed_sectors;
47 int64_t total_sectors;
48 int64_t dirty;
49 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
50 } BlkMigDevState;
51
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;
60 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
61 } BlkMigBlock;
62
63 typedef struct BlkMigState {
64 int blk_enable;
65 int shared_base;
66 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
67 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
68 int submitted;
69 int read_done;
70 int transferred;
71 int64_t total_sector_sum;
72 int64_t print_completion;
73 } BlkMigState;
74
75 static BlkMigState block_mig_state;
76
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
93 static void blk_mig_read_cb(void *opaque, int ret)
94 {
95 BlkMigBlock *blk = opaque;
96
97 blk->ret = ret;
98
99 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
100
101 block_mig_state.submitted--;
102 block_mig_state.read_done++;
103 assert(block_mig_state.submitted >= 0);
104 }
105
106 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds, int is_async)
107 {
108 int64_t total_sectors = bmds->total_sectors;
109 int64_t cur_sector = bmds->cur_sector;
110 BlockDriverState *bs = bmds->bs;
111 BlkMigBlock *blk;
112 int nr_sectors;
113
114 if (bmds->shared_base) {
115 while (cur_sector < total_sectors &&
116 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
117 &nr_sectors)) {
118 cur_sector += nr_sectors;
119 }
120 }
121
122 if (cur_sector >= total_sectors) {
123 bmds->cur_sector = bmds->completed_sectors = total_sectors;
124 return 1;
125 }
126
127 bmds->completed_sectors = cur_sector;
128
129 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
130
131 /* we are going to transfer a full block even if it is not allocated */
132 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
133
134 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
135 nr_sectors = total_sectors - cur_sector;
136 }
137
138 blk = qemu_malloc(sizeof(BlkMigBlock));
139 blk->buf = qemu_malloc(BLOCK_SIZE);
140 blk->bmds = bmds;
141 blk->sector = cur_sector;
142
143 if (is_async) {
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);
147
148 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
149 nr_sectors, blk_mig_read_cb, blk);
150 if (!blk->aiocb) {
151 goto error;
152 }
153 block_mig_state.submitted++;
154 } else {
155 if (bdrv_read(bs, cur_sector, blk->buf, nr_sectors) < 0) {
156 goto error;
157 }
158 blk_send(f, blk);
159
160 qemu_free(blk->buf);
161 qemu_free(blk);
162 }
163
164 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
165 bmds->cur_sector = cur_sector + nr_sectors;
166
167 return (bmds->cur_sector >= total_sectors);
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;
175 }
176
177 static void set_dirty_tracking(int enable)
178 {
179 BlkMigDevState *bmds;
180
181 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
182 bdrv_set_dirty_tracking(bmds->bs, enable);
183 }
184 }
185
186 static void init_blk_migration(QEMUFile *f)
187 {
188 BlkMigDevState *bmds;
189 BlockDriverState *bs;
190
191 block_mig_state.submitted = 0;
192 block_mig_state.read_done = 0;
193 block_mig_state.transferred = 0;
194 block_mig_state.total_sector_sum = 0;
195 block_mig_state.print_completion = 0;
196
197 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
198 if (bs->type == BDRV_TYPE_HD) {
199 bmds = qemu_mallocz(sizeof(BlkMigDevState));
200 bmds->bs = bs;
201 bmds->bulk_completed = 0;
202 bmds->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
203 bmds->completed_sectors = 0;
204 bmds->shared_base = block_mig_state.shared_base;
205
206 block_mig_state.total_sector_sum += bmds->total_sectors;
207
208 if (bmds->shared_base) {
209 printf("Start migration for %s with shared base image\n",
210 bs->device_name);
211 } else {
212 printf("Start full migration for %s\n", bs->device_name);
213 }
214
215 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
216 }
217 }
218 }
219
220 static int blk_mig_save_bulked_block(QEMUFile *f, int is_async)
221 {
222 int64_t completed_sector_sum = 0;
223 BlkMigDevState *bmds;
224 int ret = 0;
225
226 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
227 if (bmds->bulk_completed == 0) {
228 if (mig_save_device_bulk(f, bmds, is_async) == 1) {
229 /* completed bulk section for this device */
230 bmds->bulk_completed = 1;
231 }
232 completed_sector_sum += bmds->completed_sectors;
233 ret = 1;
234 break;
235 } else {
236 completed_sector_sum += bmds->completed_sectors;
237 }
238 }
239
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;
249 }
250
251 #define MAX_NUM_BLOCKS 4
252
253 static void blk_mig_save_dirty_blocks(QEMUFile *f)
254 {
255 BlkMigDevState *bmds;
256 BlkMigBlock blk;
257 int64_t sector;
258
259 blk.buf = qemu_malloc(BLOCK_SIZE);
260
261 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
262 for (sector = 0; sector < bmds->cur_sector;) {
263 if (bdrv_get_dirty(bmds->bs, sector)) {
264 if (bdrv_read(bmds->bs, sector, blk.buf,
265 BDRV_SECTORS_PER_DIRTY_CHUNK) < 0) {
266 printf("Error reading sector %" PRId64 "\n", sector);
267 qemu_file_set_error(f);
268 qemu_free(blk.buf);
269 return;
270 }
271 blk.bmds = bmds;
272 blk.sector = sector;
273 blk_send(f, &blk);
274
275 bdrv_reset_dirty(bmds->bs, sector,
276 BDRV_SECTORS_PER_DIRTY_CHUNK);
277 }
278 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
279 }
280 }
281
282 qemu_free(blk.buf);
283 }
284
285 static void flush_blks(QEMUFile* f)
286 {
287 BlkMigBlock *blk;
288
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);
292
293 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
294 if (qemu_file_rate_limit(f)) {
295 break;
296 }
297 if (blk->ret < 0) {
298 qemu_file_set_error(f);
299 break;
300 }
301 blk_send(f, blk);
302
303 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
304 qemu_free(blk->buf);
305 qemu_free(blk);
306
307 block_mig_state.read_done--;
308 block_mig_state.transferred++;
309 assert(block_mig_state.read_done >= 0);
310 }
311
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);
315 }
316
317 static int is_stage2_completed(void)
318 {
319 BlkMigDevState *bmds;
320
321 if (block_mig_state.submitted > 0) {
322 return 0;
323 }
324
325 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
326 if (bmds->bulk_completed == 0) {
327 return 0;
328 }
329 }
330
331 return 1;
332 }
333
334 static void blk_mig_cleanup(void)
335 {
336 BlkMigDevState *bmds;
337 BlkMigBlock *blk;
338
339 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
340 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
341 qemu_free(bmds);
342 }
343
344 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
345 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
346 qemu_free(blk->buf);
347 qemu_free(blk);
348 }
349
350 set_dirty_tracking(0);
351
352 printf("\n");
353 }
354
355 static int block_save_live(QEMUFile *f, int stage, void *opaque)
356 {
357 dprintf("Enter save live stage %d submitted %d transferred %d\n",
358 stage, block_mig_state.submitted, block_mig_state.transferred);
359
360 if (stage < 0) {
361 blk_mig_cleanup();
362 return 0;
363 }
364
365 if (block_mig_state.blk_enable != 1) {
366 /* no need to migrate storage */
367 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
368 return 1;
369 }
370
371 if (stage == 1) {
372 init_blk_migration(f);
373
374 /* start track dirty blocks */
375 set_dirty_tracking(1);
376 }
377
378 flush_blks(f);
379
380 if (qemu_file_has_error(f)) {
381 blk_mig_cleanup();
382 return 0;
383 }
384
385 /* control the rate of transfer */
386 while ((block_mig_state.submitted +
387 block_mig_state.read_done) * BLOCK_SIZE <
388 qemu_file_get_rate_limit(f)) {
389 if (blk_mig_save_bulked_block(f, 1) == 0) {
390 /* no more bulk blocks for now */
391 break;
392 }
393 }
394
395 flush_blks(f);
396
397 if (qemu_file_has_error(f)) {
398 blk_mig_cleanup();
399 return 0;
400 }
401
402 if (stage == 3) {
403 while (blk_mig_save_bulked_block(f, 0) != 0) {
404 /* empty */
405 }
406
407 blk_mig_save_dirty_blocks(f);
408 blk_mig_cleanup();
409
410 if (qemu_file_has_error(f)) {
411 return 0;
412 }
413
414 printf("Block migration completed\n");
415 }
416
417 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
418
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;
429
430 do {
431 addr = qemu_get_be64(f);
432
433 flags = addr & ~BDRV_SECTOR_MASK;
434 addr >>= BDRV_SECTOR_BITS;
435
436 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
437 /* get device name */
438 len = qemu_get_byte(f);
439 qemu_get_buffer(f, (uint8_t *)device_name, len);
440 device_name[len] = '\0';
441
442 bs = bdrv_find(device_name);
443 if (!bs) {
444 fprintf(stderr, "Error unknown block device %s\n",
445 device_name);
446 return -EINVAL;
447 }
448
449 buf = qemu_malloc(BLOCK_SIZE);
450
451 qemu_get_buffer(f, buf, BLOCK_SIZE);
452 bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK);
453
454 qemu_free(buf);
455 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
456 fprintf(stderr, "Unknown flags\n");
457 return -EINVAL;
458 }
459 if (qemu_file_has_error(f)) {
460 return -EIO;
461 }
462 } while (!(flags & BLK_MIG_FLAG_EOS));
463
464 return 0;
465 }
466
467 static void block_set_params(int blk_enable, int shared_base, void *opaque)
468 {
469 block_mig_state.blk_enable = blk_enable;
470 block_mig_state.shared_base = shared_base;
471
472 /* shared base means that blk_enable = 1 */
473 block_mig_state.blk_enable |= shared_base;
474 }
475
476 void blk_mig_init(void)
477 {
478 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
479 QSIMPLEQ_INIT(&block_mig_state.blk_list);
480
481 register_savevm_live("block", 0, 1, block_set_params, block_save_live,
482 NULL, block_load, &block_mig_state);
483 }