]> git.proxmox.com Git - qemu.git/blob - block-migration.c
live migration: Allow cleanup after cancellation or error
[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 total_sectors;
47 int64_t dirty;
48 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
49 } BlkMigDevState;
50
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;
59 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
60 } BlkMigBlock;
61
62 typedef struct BlkMigState {
63 int blk_enable;
64 int shared_base;
65 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
66 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
67 int submitted;
68 int read_done;
69 int transferred;
70 int64_t print_completion;
71 } BlkMigState;
72
73 static BlkMigState block_mig_state;
74
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
91 static void blk_mig_read_cb(void *opaque, int ret)
92 {
93 BlkMigBlock *blk = opaque;
94
95 blk->ret = ret;
96
97 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
98
99 block_mig_state.submitted--;
100 block_mig_state.read_done++;
101 assert(block_mig_state.submitted >= 0);
102 }
103
104 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds, int is_async)
105 {
106 int64_t total_sectors = bmds->total_sectors;
107 int64_t cur_sector = bmds->cur_sector;
108 BlockDriverState *bs = bmds->bs;
109 BlkMigBlock *blk;
110 int nr_sectors;
111
112 if (bmds->shared_base) {
113 while (cur_sector < total_sectors &&
114 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
115 &nr_sectors)) {
116 cur_sector += nr_sectors;
117 }
118 }
119
120 if (cur_sector >= total_sectors) {
121 bmds->cur_sector = total_sectors;
122 return 1;
123 }
124
125 if (cur_sector >= block_mig_state.print_completion) {
126 printf("Completed %" PRId64 " %%\r", cur_sector * 100 / total_sectors);
127 fflush(stdout);
128 block_mig_state.print_completion +=
129 (BDRV_SECTORS_PER_DIRTY_CHUNK * 10000);
130 }
131
132 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
133
134 /* we are going to transfer a full block even if it is not allocated */
135 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
136
137 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
138 nr_sectors = total_sectors - cur_sector;
139 }
140
141 blk = qemu_malloc(sizeof(BlkMigBlock));
142 blk->buf = qemu_malloc(BLOCK_SIZE);
143 blk->bmds = bmds;
144 blk->sector = cur_sector;
145
146 if (is_async) {
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);
150
151 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
152 nr_sectors, blk_mig_read_cb, blk);
153 if (!blk->aiocb) {
154 goto error;
155 }
156 block_mig_state.submitted++;
157 } else {
158 if (bdrv_read(bs, cur_sector, blk->buf, nr_sectors) < 0) {
159 goto error;
160 }
161 blk_send(f, blk);
162
163 qemu_free(blk->buf);
164 qemu_free(blk);
165 }
166
167 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
168 bmds->cur_sector = cur_sector + nr_sectors;
169
170 return (bmds->cur_sector >= total_sectors);
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;
178 }
179
180 static void set_dirty_tracking(int enable)
181 {
182 BlkMigDevState *bmds;
183
184 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
185 bdrv_set_dirty_tracking(bmds->bs, enable);
186 }
187 }
188
189 static void init_blk_migration(QEMUFile *f)
190 {
191 BlkMigDevState *bmds;
192 BlockDriverState *bs;
193
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
199 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
200 if (bs->type == BDRV_TYPE_HD) {
201 bmds = qemu_mallocz(sizeof(BlkMigDevState));
202 bmds->bs = bs;
203 bmds->bulk_completed = 0;
204 bmds->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
205 bmds->shared_base = block_mig_state.shared_base;
206
207 if (bmds->shared_base) {
208 printf("Start migration for %s with shared base image\n",
209 bs->device_name);
210 } else {
211 printf("Start full migration for %s\n", bs->device_name);
212 }
213
214 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
215 }
216 }
217 }
218
219 static int blk_mig_save_bulked_block(QEMUFile *f, int is_async)
220 {
221 BlkMigDevState *bmds;
222
223 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
224 if (bmds->bulk_completed == 0) {
225 if (mig_save_device_bulk(f, bmds, is_async) == 1) {
226 /* completed bulk section for this device */
227 bmds->bulk_completed = 1;
228 }
229 return 1;
230 }
231 }
232
233 /* we reached here means bulk is completed */
234 return 0;
235 }
236
237 #define MAX_NUM_BLOCKS 4
238
239 static void blk_mig_save_dirty_blocks(QEMUFile *f)
240 {
241 BlkMigDevState *bmds;
242 BlkMigBlock blk;
243 int64_t sector;
244
245 blk.buf = qemu_malloc(BLOCK_SIZE);
246
247 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
248 for (sector = 0; sector < bmds->cur_sector;) {
249 if (bdrv_get_dirty(bmds->bs, sector)) {
250 if (bdrv_read(bmds->bs, sector, blk.buf,
251 BDRV_SECTORS_PER_DIRTY_CHUNK) < 0) {
252 printf("Error reading sector %" PRId64 "\n", sector);
253 qemu_file_set_error(f);
254 qemu_free(blk.buf);
255 return;
256 }
257 blk.bmds = bmds;
258 blk.sector = sector;
259 blk_send(f, &blk);
260
261 bdrv_reset_dirty(bmds->bs, sector,
262 BDRV_SECTORS_PER_DIRTY_CHUNK);
263 }
264 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
265 }
266 }
267
268 qemu_free(blk.buf);
269 }
270
271 static void flush_blks(QEMUFile* f)
272 {
273 BlkMigBlock *blk;
274
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);
278
279 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
280 if (qemu_file_rate_limit(f)) {
281 break;
282 }
283 if (blk->ret < 0) {
284 qemu_file_set_error(f);
285 break;
286 }
287 blk_send(f, blk);
288
289 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
290 qemu_free(blk->buf);
291 qemu_free(blk);
292
293 block_mig_state.read_done--;
294 block_mig_state.transferred++;
295 assert(block_mig_state.read_done >= 0);
296 }
297
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);
301 }
302
303 static int is_stage2_completed(void)
304 {
305 BlkMigDevState *bmds;
306
307 if (block_mig_state.submitted > 0) {
308 return 0;
309 }
310
311 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
312 if (bmds->bulk_completed == 0) {
313 return 0;
314 }
315 }
316
317 return 1;
318 }
319
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
341 static int block_save_live(QEMUFile *f, int stage, void *opaque)
342 {
343 dprintf("Enter save live stage %d submitted %d transferred %d\n",
344 stage, block_mig_state.submitted, block_mig_state.transferred);
345
346 if (stage < 0) {
347 blk_mig_cleanup();
348 return 0;
349 }
350
351 if (block_mig_state.blk_enable != 1) {
352 /* no need to migrate storage */
353 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
354 return 1;
355 }
356
357 if (stage == 1) {
358 init_blk_migration(f);
359
360 /* start track dirty blocks */
361 set_dirty_tracking(1);
362 }
363
364 flush_blks(f);
365
366 if (qemu_file_has_error(f)) {
367 blk_mig_cleanup();
368 return 0;
369 }
370
371 /* control the rate of transfer */
372 while ((block_mig_state.submitted +
373 block_mig_state.read_done) * BLOCK_SIZE <
374 qemu_file_get_rate_limit(f)) {
375 if (blk_mig_save_bulked_block(f, 1) == 0) {
376 /* no more bulk blocks for now */
377 break;
378 }
379 }
380
381 flush_blks(f);
382
383 if (qemu_file_has_error(f)) {
384 blk_mig_cleanup();
385 return 0;
386 }
387
388 if (stage == 3) {
389 while (blk_mig_save_bulked_block(f, 0) != 0) {
390 /* empty */
391 }
392
393 blk_mig_save_dirty_blocks(f);
394 blk_mig_cleanup();
395
396 if (qemu_file_has_error(f)) {
397 return 0;
398 }
399
400 printf("Block migration completed\n");
401 }
402
403 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
404
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;
415
416 do {
417 addr = qemu_get_be64(f);
418
419 flags = addr & ~BDRV_SECTOR_MASK;
420 addr >>= BDRV_SECTOR_BITS;
421
422 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
423 /* get device name */
424 len = qemu_get_byte(f);
425 qemu_get_buffer(f, (uint8_t *)device_name, len);
426 device_name[len] = '\0';
427
428 bs = bdrv_find(device_name);
429 if (!bs) {
430 fprintf(stderr, "Error unknown block device %s\n",
431 device_name);
432 return -EINVAL;
433 }
434
435 buf = qemu_malloc(BLOCK_SIZE);
436
437 qemu_get_buffer(f, buf, BLOCK_SIZE);
438 bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK);
439
440 qemu_free(buf);
441 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
442 fprintf(stderr, "Unknown flags\n");
443 return -EINVAL;
444 }
445 if (qemu_file_has_error(f)) {
446 return -EIO;
447 }
448 } while (!(flags & BLK_MIG_FLAG_EOS));
449
450 return 0;
451 }
452
453 static void block_set_params(int blk_enable, int shared_base, void *opaque)
454 {
455 block_mig_state.blk_enable = blk_enable;
456 block_mig_state.shared_base = shared_base;
457
458 /* shared base means that blk_enable = 1 */
459 block_mig_state.blk_enable |= shared_base;
460 }
461
462 void blk_mig_init(void)
463 {
464 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
465 QSIMPLEQ_INIT(&block_mig_state.blk_list);
466
467 register_savevm_live("block", 0, 1, block_set_params, block_save_live,
468 NULL, block_load, &block_mig_state);
469 }