block-migration.c 11.1 KB
Newer Older
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * QEMU live block migration
 *
 * Copyright IBM, Corp. 2009
 *
 * Authors:
 *  Liran Schour   <lirans@il.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

#include "qemu-common.h"
#include "block_int.h"
#include "hw/hw.h"
17
#include "qemu-queue.h"
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
18
19
20
#include "block-migration.h"
#include <assert.h>

21
#define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
22
23
24
25
26
27
28
29
30
31
32
33

#define BLK_MIG_FLAG_DEVICE_BLOCK       0x01
#define BLK_MIG_FLAG_EOS                0x02

#define MAX_IS_ALLOCATED_SEARCH 65536
#define MAX_BLOCKS_READ 10000
#define BLOCKS_READ_CHANGE 100
#define INITIAL_BLOCKS_READ 100

//#define DEBUG_BLK_MIGRATION

#ifdef DEBUG_BLK_MIGRATION
34
#define dprintf(fmt, ...) \
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
35
36
    do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
#else
37
#define dprintf(fmt, ...) \
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
38
39
40
    do { } while (0)
#endif

41
42
43
44
45
46
47
typedef struct BlkMigDevState {
    BlockDriverState *bs;
    int bulk_completed;
    int shared_base;
    int64_t cur_sector;
    int64_t total_sectors;
    int64_t dirty;
48
    QSIMPLEQ_ENTRY(BlkMigDevState) entry;
49
50
} BlkMigDevState;

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
51
52
53
54
55
56
57
58
typedef struct BlkMigBlock {
    uint8_t *buf;
    BlkMigDevState *bmds;
    int64_t sector;
    struct iovec iov;
    QEMUIOVector qiov;
    BlockDriverAIOCB *aiocb;
    int ret;
59
    QSIMPLEQ_ENTRY(BlkMigBlock) entry;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
60
61
62
63
64
} BlkMigBlock;

typedef struct BlkMigState {
    int blk_enable;
    int shared_base;
65
66
    QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
    QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
67
68
69
70
71
72
    int submitted;
    int read_done;
    int transferred;
    int64_t print_completion;
} BlkMigState;

73
static BlkMigState block_mig_state;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
74

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
static void blk_send(QEMUFile *f, BlkMigBlock * blk)
{
    int len;

    /* sector number and flags */
    qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
                     | BLK_MIG_FLAG_DEVICE_BLOCK);

    /* device name */
    len = strlen(blk->bmds->bs->device_name);
    qemu_put_byte(f, len);
    qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);

    qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
}

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
91
92
93
static void blk_mig_read_cb(void *opaque, int ret)
{
    BlkMigBlock *blk = opaque;
94

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
95
    blk->ret = ret;
96

97
    QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
98

99
100
101
    block_mig_state.submitted--;
    block_mig_state.read_done++;
    assert(block_mig_state.submitted >= 0);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
102
103
}

104
static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds, int is_async)
105
{
106
107
108
    int64_t total_sectors = bmds->total_sectors;
    int64_t cur_sector = bmds->cur_sector;
    BlockDriverState *bs = bmds->bs;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
109
    BlkMigBlock *blk;
110
    int nr_sectors;
111

112
    if (bmds->shared_base) {
113
        while (cur_sector < total_sectors &&
114
115
               !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
                                  &nr_sectors)) {
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
116
117
118
            cur_sector += nr_sectors;
        }
    }
119
120

    if (cur_sector >= total_sectors) {
121
        bmds->cur_sector = total_sectors;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
122
123
        return 1;
    }
124

125
    if (cur_sector >= block_mig_state.print_completion) {
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
126
127
        printf("Completed %" PRId64 " %%\r", cur_sector * 100 / total_sectors);
        fflush(stdout);
128
        block_mig_state.print_completion +=
129
            (BDRV_SECTORS_PER_DIRTY_CHUNK * 10000);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
130
    }
131

132
133
    cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);

134
135
    /* we are going to transfer a full block even if it is not allocated */
    nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
136

137
    if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
138
        nr_sectors = total_sectors - cur_sector;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
139
    }
140

141
142
143
144
    blk = qemu_malloc(sizeof(BlkMigBlock));
    blk->buf = qemu_malloc(BLOCK_SIZE);
    blk->bmds = bmds;
    blk->sector = cur_sector;
145

146
    if (is_async) {
147
148
149
        blk->iov.iov_base = blk->buf;
        blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
        qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
150

151
152
153
154
155
156
157
158
159
160
        blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
                                    nr_sectors, blk_mig_read_cb, blk);
        if (!blk->aiocb) {
            printf("Error reading sector %" PRId64 "\n", cur_sector);
            qemu_free(blk->buf);
            qemu_free(blk);
            return 0;
        }
        block_mig_state.submitted++;
    } else {
161
        if (bdrv_read(bs, cur_sector, blk->buf, nr_sectors) < 0) {
162
            printf("Error reading sector %" PRId64 "\n", cur_sector);
163
            return 0;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
164
        }
165
        blk_send(f, blk);
166

167
168
        qemu_free(blk->buf);
        qemu_free(blk);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
169
170
    }

171
172
    bdrv_reset_dirty(bs, cur_sector, nr_sectors);
    bmds->cur_sector = cur_sector + nr_sectors;
173

174
    return (bmds->cur_sector >= total_sectors);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
175
176
177
178
179
}

static void set_dirty_tracking(int enable)
{
    BlkMigDevState *bmds;
180
181

    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
182
        bdrv_set_dirty_tracking(bmds->bs, enable);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
183
184
185
186
187
    }
}

static void init_blk_migration(QEMUFile *f)
{
188
    BlkMigDevState *bmds;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
189
    BlockDriverState *bs;
190

191
192
193
194
195
    block_mig_state.submitted = 0;
    block_mig_state.read_done = 0;
    block_mig_state.transferred = 0;
    block_mig_state.print_completion = 0;

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
196
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
197
        if (bs->type == BDRV_TYPE_HD) {
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
198
199
200
            bmds = qemu_mallocz(sizeof(BlkMigDevState));
            bmds->bs = bs;
            bmds->bulk_completed = 0;
201
            bmds->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
202
            bmds->shared_base = block_mig_state.shared_base;
203
204
205

            if (bmds->shared_base) {
                printf("Start migration for %s with shared base image\n",
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
206
207
208
209
                       bs->device_name);
            } else {
                printf("Start full migration for %s\n", bs->device_name);
            }
210

211
            QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
212
        }
213
    }
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
214
215
216
217
218
219
}

static int blk_mig_save_bulked_block(QEMUFile *f, int is_async)
{
    BlkMigDevState *bmds;

220
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
221
        if (bmds->bulk_completed == 0) {
222
223
224
            if (mig_save_device_bulk(f, bmds, is_async) == 1) {
                /* completed bulk section for this device */
                bmds->bulk_completed = 1;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
225
226
227
228
            }
            return 1;
        }
    }
229

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
230
231
232
233
234
235
236
237
238
    /* we reached here means bulk is completed */
    return 0;
}

#define MAX_NUM_BLOCKS 4

static void blk_mig_save_dirty_blocks(QEMUFile *f)
{
    BlkMigDevState *bmds;
239
    BlkMigBlock blk;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
240
    int64_t sector;
241

242
    blk.buf = qemu_malloc(BLOCK_SIZE);
243

244
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
245
246
        for (sector = 0; sector < bmds->cur_sector;) {
            if (bdrv_get_dirty(bmds->bs, sector)) {
247
                if (bdrv_read(bmds->bs, sector, blk.buf,
248
                              BDRV_SECTORS_PER_DIRTY_CHUNK) < 0) {
249
                    printf("Error reading sector %" PRId64 "\n", sector);
250
                    /* FIXME: add error handling */
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
251
                }
252
253
254
                blk.bmds = bmds;
                blk.sector = sector;
                blk_send(f, &blk);
255
256

                bdrv_reset_dirty(bmds->bs, sector,
257
                                 BDRV_SECTORS_PER_DIRTY_CHUNK);
258
            }
259
            sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
260
261
        }
    }
262

263
    qemu_free(blk.buf);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
264
265
266
267
}

static void flush_blks(QEMUFile* f)
{
268
    BlkMigBlock *blk;
269

270
271
272
    dprintf("%s Enter submitted %d read_done %d transferred %d\n",
            __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
            block_mig_state.transferred);
273

274
275
276
277
    while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
        if (qemu_file_rate_limit(f)) {
            break;
        }
278
        blk_send(f, blk);
279

280
        QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
281
282
        qemu_free(blk->buf);
        qemu_free(blk);
283

284
285
286
        block_mig_state.read_done--;
        block_mig_state.transferred++;
        assert(block_mig_state.read_done >= 0);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
287
288
    }

289
290
291
    dprintf("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
            block_mig_state.submitted, block_mig_state.read_done,
            block_mig_state.transferred);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
292
293
294
295
296
}

static int is_stage2_completed(void)
{
    BlkMigDevState *bmds;
297

298
    if (block_mig_state.submitted > 0) {
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
299
300
        return 0;
    }
301

302
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
303
        if (bmds->bulk_completed == 0) {
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
304
305
306
            return 0;
        }
    }
307

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
308
309
310
311
312
    return 1;
}

static int block_save_live(QEMUFile *f, int stage, void *opaque)
{
313
314
    dprintf("Enter save live stage %d submitted %d transferred %d\n",
            stage, block_mig_state.submitted, block_mig_state.transferred);
315

316
    if (block_mig_state.blk_enable != 1) {
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
317
        /* no need to migrate storage */
318
        qemu_put_be64(f, BLK_MIG_FLAG_EOS);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
319
320
        return 1;
    }
321
322

    if (stage == 1) {
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
323
        init_blk_migration(f);
324

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
325
326
327
328
329
        /* start track dirty blocks */
        set_dirty_tracking(1);
    }

    flush_blks(f);
330

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
331
    /* control the rate of transfer */
332
333
    while ((block_mig_state.submitted +
            block_mig_state.read_done) * BLOCK_SIZE <
334
335
336
           qemu_file_get_rate_limit(f)) {
        if (blk_mig_save_bulked_block(f, 1) == 0) {
            /* no more bulk blocks for now */
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
337
            break;
338
        }
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
339
    }
340

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
341
    flush_blks(f);
342
343
344
345
346
347

    if (stage == 3) {
        while (blk_mig_save_bulked_block(f, 0) != 0) {
            /* empty */
        }

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
348
        blk_mig_save_dirty_blocks(f);
349

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
350
        /* stop track dirty blocks */
351
352
353
        set_dirty_tracking(0);

        printf("\nBlock migration completed\n");
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
354
    }
355
356
357

    qemu_put_be64(f, BLK_MIG_FLAG_EOS);

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
358
359
360
361
362
363
364
365
366
367
    return ((stage == 2) && is_stage2_completed());
}

static int block_load(QEMUFile *f, void *opaque, int version_id)
{
    int len, flags;
    char device_name[256];
    int64_t addr;
    BlockDriverState *bs;
    uint8_t *buf;
368

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
369
370
    do {
        addr = qemu_get_be64(f);
371

372
373
        flags = addr & ~BDRV_SECTOR_MASK;
        addr >>= BDRV_SECTOR_BITS;
374
375

        if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
376
377
            /* get device name */
            len = qemu_get_byte(f);
378

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
379
380
            qemu_get_buffer(f, (uint8_t *)device_name, len);
            device_name[len] = '\0';
381

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
382
            bs = bdrv_find(device_name);
383

384
385
            buf = qemu_malloc(BLOCK_SIZE);

386
387
            qemu_get_buffer(f, buf, BLOCK_SIZE);
            if (bs != NULL) {
388
                bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
389
390
            } else {
                printf("Error unknown block device %s\n", device_name);
391
                /* FIXME: add error handling */
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
392
            }
393
394

            qemu_free(buf);
395
        } else if (!(flags & BLK_MIG_FLAG_EOS)) {
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
396
            printf("Unknown flags\n");
397
            /* FIXME: add error handling */
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
398
        }
399
400
    } while (!(flags & BLK_MIG_FLAG_EOS));

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
401
402
403
404
405
    return 0;
}

static void block_set_params(int blk_enable, int shared_base, void *opaque)
{
406
407
    block_mig_state.blk_enable = blk_enable;
    block_mig_state.shared_base = shared_base;
408

lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
409
    /* shared base means that blk_enable = 1 */
410
    block_mig_state.blk_enable |= shared_base;
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
411
412
413
}

void blk_mig_init(void)
414
{
415
416
417
    QSIMPLEQ_INIT(&block_mig_state.bmds_list);
    QSIMPLEQ_INIT(&block_mig_state.blk_list);

418
    register_savevm_live("block", 0, 1, block_set_params, block_save_live,
419
                         NULL, block_load, &block_mig_state);
lirans@il.ibm.com's avatar
lirans@il.ibm.com committed
420
}