blob: fd2ffff0d5178d41fdfc3ef95460bb2373de597a [file] [log] [blame]
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001/*
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020014 */
15
16#include "qemu-common.h"
17#include "block_int.h"
18#include "hw/hw.h"
Jan Kiszka5e5328b2009-11-30 18:21:20 +010019#include "qemu-queue.h"
Liran Schour889ae392010-01-26 10:31:49 +020020#include "qemu-timer.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020021#include "block-migration.h"
Liran Schour889ae392010-01-26 10:31:49 +020022#include "migration.h"
Marcelo Tosattif48905d2011-01-26 12:12:33 -020023#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020024#include <assert.h>
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020025
Jan Kiszka6ea44302009-11-30 18:21:19 +010026#define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020027
28#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
29#define BLK_MIG_FLAG_EOS 0x02
Jan Kiszka01e61e22009-12-01 15:20:17 +010030#define BLK_MIG_FLAG_PROGRESS 0x04
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020031
32#define MAX_IS_ALLOCATED_SEARCH 65536
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020033
34//#define DEBUG_BLK_MIGRATION
35
36#ifdef DEBUG_BLK_MIGRATION
malcd0f2c4c2010-02-07 02:03:50 +030037#define DPRINTF(fmt, ...) \
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020038 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
39#else
malcd0f2c4c2010-02-07 02:03:50 +030040#define DPRINTF(fmt, ...) \
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020041 do { } while (0)
42#endif
43
Jan Kiszkaa55eb922009-11-30 18:21:19 +010044typedef struct BlkMigDevState {
45 BlockDriverState *bs;
46 int bulk_completed;
47 int shared_base;
Jan Kiszkaa55eb922009-11-30 18:21:19 +010048 int64_t cur_sector;
Liran Schourd76cac72010-01-26 14:04:11 +020049 int64_t cur_dirty;
Jan Kiszka82801d82009-11-30 18:21:21 +010050 int64_t completed_sectors;
Jan Kiszkaa55eb922009-11-30 18:21:19 +010051 int64_t total_sectors;
52 int64_t dirty;
Jan Kiszka5e5328b2009-11-30 18:21:20 +010053 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
Marcelo Tosatti33656af2010-11-08 17:02:56 -020054 unsigned long *aio_bitmap;
Jan Kiszkaa55eb922009-11-30 18:21:19 +010055} BlkMigDevState;
56
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020057typedef struct BlkMigBlock {
58 uint8_t *buf;
59 BlkMigDevState *bmds;
60 int64_t sector;
Marcelo Tosatti33656af2010-11-08 17:02:56 -020061 int nr_sectors;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020062 struct iovec iov;
63 QEMUIOVector qiov;
64 BlockDriverAIOCB *aiocb;
65 int ret;
Jan Kiszka5e5328b2009-11-30 18:21:20 +010066 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020067} BlkMigBlock;
68
69typedef struct BlkMigState {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020070 int blk_enable;
71 int shared_base;
Jan Kiszka5e5328b2009-11-30 18:21:20 +010072 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
73 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020074 int submitted;
75 int read_done;
76 int transferred;
Jan Kiszka82801d82009-11-30 18:21:21 +010077 int64_t total_sector_sum;
Jan Kiszka01e61e22009-12-01 15:20:17 +010078 int prev_progress;
Liran Schoure970ec02010-01-26 10:31:45 +020079 int bulk_completed;
Liran Schour889ae392010-01-26 10:31:49 +020080 long double total_time;
Avishay Traegerff5c52a2011-04-03 11:31:45 +030081 long double prev_time_offset;
Liran Schour889ae392010-01-26 10:31:49 +020082 int reads;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020083} BlkMigState;
84
Jan Kiszkad11ecd32009-11-30 18:21:20 +010085static BlkMigState block_mig_state;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020086
Jan Kiszka13f0b672009-11-30 18:21:21 +010087static void blk_send(QEMUFile *f, BlkMigBlock * blk)
88{
89 int len;
90
91 /* sector number and flags */
92 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
93 | BLK_MIG_FLAG_DEVICE_BLOCK);
94
95 /* device name */
96 len = strlen(blk->bmds->bs->device_name);
97 qemu_put_byte(f, len);
98 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
99
100 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
101}
102
Jan Kiszka25f23642009-11-30 18:21:21 +0100103int blk_mig_active(void)
104{
105 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
106}
107
108uint64_t blk_mig_bytes_transferred(void)
109{
110 BlkMigDevState *bmds;
111 uint64_t sum = 0;
112
113 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
114 sum += bmds->completed_sectors;
115 }
116 return sum << BDRV_SECTOR_BITS;
117}
118
119uint64_t blk_mig_bytes_remaining(void)
120{
121 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
122}
123
124uint64_t blk_mig_bytes_total(void)
125{
126 BlkMigDevState *bmds;
127 uint64_t sum = 0;
128
129 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
130 sum += bmds->total_sectors;
131 }
132 return sum << BDRV_SECTOR_BITS;
133}
134
Liran Schour889ae392010-01-26 10:31:49 +0200135static inline long double compute_read_bwidth(void)
136{
137 assert(block_mig_state.total_time != 0);
Avishay Traeger155eb9a2011-04-06 10:45:36 +0300138 return (block_mig_state.reads / block_mig_state.total_time) * BLOCK_SIZE;
Liran Schour889ae392010-01-26 10:31:49 +0200139}
140
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200141static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
142{
143 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
144
Marcelo Tosatti62155e22010-11-12 16:07:50 -0200145 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200146 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
147 (1UL << (chunk % (sizeof(unsigned long) * 8))));
148 } else {
149 return 0;
150 }
151}
152
153static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
154 int nb_sectors, int set)
155{
156 int64_t start, end;
157 unsigned long val, idx, bit;
158
159 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
160 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
161
162 for (; start <= end; start++) {
163 idx = start / (sizeof(unsigned long) * 8);
164 bit = start % (sizeof(unsigned long) * 8);
165 val = bmds->aio_bitmap[idx];
166 if (set) {
Marcelo Tosatti62155e22010-11-12 16:07:50 -0200167 val |= 1UL << bit;
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200168 } else {
Marcelo Tosatti62155e22010-11-12 16:07:50 -0200169 val &= ~(1UL << bit);
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200170 }
171 bmds->aio_bitmap[idx] = val;
172 }
173}
174
175static void alloc_aio_bitmap(BlkMigDevState *bmds)
176{
177 BlockDriverState *bs = bmds->bs;
178 int64_t bitmap_size;
179
180 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
181 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
182 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
183
Anthony Liguori7267c092011-08-20 22:09:37 -0500184 bmds->aio_bitmap = g_malloc0(bitmap_size);
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200185}
186
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200187static void blk_mig_read_cb(void *opaque, int ret)
188{
Avishay Traegerff5c52a2011-04-03 11:31:45 +0300189 long double curr_time = qemu_get_clock_ns(rt_clock);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200190 BlkMigBlock *blk = opaque;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100191
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200192 blk->ret = ret;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100193
Avishay Traegerff5c52a2011-04-03 11:31:45 +0300194 block_mig_state.reads++;
195 block_mig_state.total_time += (curr_time - block_mig_state.prev_time_offset);
196 block_mig_state.prev_time_offset = curr_time;
Liran Schour889ae392010-01-26 10:31:49 +0200197
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100198 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200199 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100200
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100201 block_mig_state.submitted--;
202 block_mig_state.read_done++;
203 assert(block_mig_state.submitted >= 0);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200204}
205
Luiz Capitulino539de122011-12-05 14:06:56 -0200206static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100207{
Jan Kiszka57cce122009-11-30 18:21:20 +0100208 int64_t total_sectors = bmds->total_sectors;
209 int64_t cur_sector = bmds->cur_sector;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200210 BlockDriverState *bs = bmds->bs;
Jan Kiszka57cce122009-11-30 18:21:20 +0100211 BlkMigBlock *blk;
Jan Kiszka13f0b672009-11-30 18:21:21 +0100212 int nr_sectors;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100213
214 if (bmds->shared_base) {
Jan Kiszkab1d10852009-11-30 18:21:20 +0100215 while (cur_sector < total_sectors &&
Jan Kiszka57cce122009-11-30 18:21:20 +0100216 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
217 &nr_sectors)) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200218 cur_sector += nr_sectors;
219 }
220 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100221
222 if (cur_sector >= total_sectors) {
Jan Kiszka82801d82009-11-30 18:21:21 +0100223 bmds->cur_sector = bmds->completed_sectors = total_sectors;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200224 return 1;
225 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100226
Jan Kiszka82801d82009-11-30 18:21:21 +0100227 bmds->completed_sectors = cur_sector;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100228
Jan Kiszka6ea44302009-11-30 18:21:19 +0100229 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100230
Jan Kiszka6ea44302009-11-30 18:21:19 +0100231 /* we are going to transfer a full block even if it is not allocated */
232 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100233
Jan Kiszka6ea44302009-11-30 18:21:19 +0100234 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
Jan Kiszka57cce122009-11-30 18:21:20 +0100235 nr_sectors = total_sectors - cur_sector;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200236 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100237
Anthony Liguori7267c092011-08-20 22:09:37 -0500238 blk = g_malloc(sizeof(BlkMigBlock));
239 blk->buf = g_malloc(BLOCK_SIZE);
Jan Kiszka13f0b672009-11-30 18:21:21 +0100240 blk->bmds = bmds;
241 blk->sector = cur_sector;
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200242 blk->nr_sectors = nr_sectors;
Jan Kiszka13f0b672009-11-30 18:21:21 +0100243
Liran Schoure970ec02010-01-26 10:31:45 +0200244 blk->iov.iov_base = blk->buf;
245 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
246 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
Jan Kiszka57cce122009-11-30 18:21:20 +0100247
Avishay Traegerff5c52a2011-04-03 11:31:45 +0300248 if (block_mig_state.submitted == 0) {
249 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
250 }
Liran Schour889ae392010-01-26 10:31:49 +0200251
Liran Schoure970ec02010-01-26 10:31:45 +0200252 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
253 nr_sectors, blk_mig_read_cb, blk);
Liran Schoure970ec02010-01-26 10:31:45 +0200254 block_mig_state.submitted++;
Liran Schourd76cac72010-01-26 14:04:11 +0200255
Jan Kiszka13f0b672009-11-30 18:21:21 +0100256 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
257 bmds->cur_sector = cur_sector + nr_sectors;
258
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200259 return (bmds->cur_sector >= total_sectors);
260}
261
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200262static void set_dirty_tracking(int enable)
263{
264 BlkMigDevState *bmds;
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100265
266 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100267 bdrv_set_dirty_tracking(bmds->bs, enable);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200268 }
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200269}
270
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100271static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200272{
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100273 BlkMigDevState *bmds;
Jan Kiszka792773b2009-11-30 20:34:55 +0100274 int64_t sectors;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100275
Markus Armbrusterd2466732010-06-28 10:45:02 +0200276 if (!bdrv_is_read_only(bs)) {
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100277 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
Shahar Havivi31f54f22010-07-10 18:59:06 +0300278 if (sectors <= 0) {
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100279 return;
280 }
281
Anthony Liguori7267c092011-08-20 22:09:37 -0500282 bmds = g_malloc0(sizeof(BlkMigDevState));
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100283 bmds->bs = bs;
284 bmds->bulk_completed = 0;
285 bmds->total_sectors = sectors;
286 bmds->completed_sectors = 0;
287 bmds->shared_base = block_mig_state.shared_base;
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200288 alloc_aio_bitmap(bmds);
Marcelo Tosattif48905d2011-01-26 12:12:33 -0200289 drive_get_ref(drive_get_by_blockdev(bs));
Marcelo Tosatti85916752011-01-26 12:12:35 -0200290 bdrv_set_in_use(bs, 1);
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100291
292 block_mig_state.total_sector_sum += sectors;
293
294 if (bmds->shared_base) {
Luiz Capitulino539de122011-12-05 14:06:56 -0200295 DPRINTF("Start migration for %s with shared base image\n",
296 bs->device_name);
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100297 } else {
Luiz Capitulino539de122011-12-05 14:06:56 -0200298 DPRINTF("Start full migration for %s\n", bs->device_name);
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100299 }
300
301 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
302 }
303}
304
Luiz Capitulino539de122011-12-05 14:06:56 -0200305static void init_blk_migration(QEMUFile *f)
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100306{
Jan Kiszka69d63a92009-11-30 18:21:20 +0100307 block_mig_state.submitted = 0;
308 block_mig_state.read_done = 0;
309 block_mig_state.transferred = 0;
Jan Kiszka82801d82009-11-30 18:21:21 +0100310 block_mig_state.total_sector_sum = 0;
Jan Kiszka01e61e22009-12-01 15:20:17 +0100311 block_mig_state.prev_progress = -1;
Liran Schoure970ec02010-01-26 10:31:45 +0200312 block_mig_state.bulk_completed = 0;
Liran Schour889ae392010-01-26 10:31:49 +0200313 block_mig_state.total_time = 0;
314 block_mig_state.reads = 0;
Jan Kiszka69d63a92009-11-30 18:21:20 +0100315
Luiz Capitulino539de122011-12-05 14:06:56 -0200316 bdrv_iterate(init_blk_migration_it, NULL);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200317}
318
Luiz Capitulino539de122011-12-05 14:06:56 -0200319static int blk_mig_save_bulked_block(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200320{
Jan Kiszka82801d82009-11-30 18:21:21 +0100321 int64_t completed_sector_sum = 0;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200322 BlkMigDevState *bmds;
Jan Kiszka01e61e22009-12-01 15:20:17 +0100323 int progress;
Jan Kiszka82801d82009-11-30 18:21:21 +0100324 int ret = 0;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200325
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100326 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100327 if (bmds->bulk_completed == 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -0200328 if (mig_save_device_bulk(f, bmds) == 1) {
Jan Kiszka57cce122009-11-30 18:21:20 +0100329 /* completed bulk section for this device */
330 bmds->bulk_completed = 1;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200331 }
Jan Kiszka82801d82009-11-30 18:21:21 +0100332 completed_sector_sum += bmds->completed_sectors;
333 ret = 1;
334 break;
335 } else {
336 completed_sector_sum += bmds->completed_sectors;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200337 }
338 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100339
Pierre Riteau8b6b2af2011-01-12 14:41:00 +0100340 if (block_mig_state.total_sector_sum != 0) {
341 progress = completed_sector_sum * 100 /
342 block_mig_state.total_sector_sum;
343 } else {
344 progress = 100;
345 }
Jan Kiszka01e61e22009-12-01 15:20:17 +0100346 if (progress != block_mig_state.prev_progress) {
347 block_mig_state.prev_progress = progress;
348 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
349 | BLK_MIG_FLAG_PROGRESS);
Luiz Capitulino539de122011-12-05 14:06:56 -0200350 DPRINTF("Completed %d %%\r", progress);
Jan Kiszka82801d82009-11-30 18:21:21 +0100351 }
352
353 return ret;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200354}
355
Liran Schourd76cac72010-01-26 14:04:11 +0200356static void blk_mig_reset_dirty_cursor(void)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200357{
358 BlkMigDevState *bmds;
Jan Kiszka575a58d2009-11-30 18:21:20 +0100359
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100360 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
Liran Schourd76cac72010-01-26 14:04:11 +0200361 bmds->cur_dirty = 0;
362 }
363}
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100364
Luiz Capitulino539de122011-12-05 14:06:56 -0200365static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
366 int is_async)
Liran Schourd76cac72010-01-26 14:04:11 +0200367{
368 BlkMigBlock *blk;
369 int64_t total_sectors = bmds->total_sectors;
370 int64_t sector;
371 int nr_sectors;
Juan Quinteladcd1d222011-09-21 23:01:54 +0200372 int ret = -EIO;
Liran Schourd76cac72010-01-26 14:04:11 +0200373
374 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
Marcelo Tosatti62155e22010-11-12 16:07:50 -0200375 if (bmds_aio_inflight(bmds, sector)) {
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000376 bdrv_drain_all();
Marcelo Tosatti62155e22010-11-12 16:07:50 -0200377 }
Liran Schourd76cac72010-01-26 14:04:11 +0200378 if (bdrv_get_dirty(bmds->bs, sector)) {
379
380 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
381 nr_sectors = total_sectors - sector;
382 } else {
383 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100384 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500385 blk = g_malloc(sizeof(BlkMigBlock));
386 blk->buf = g_malloc(BLOCK_SIZE);
Liran Schourd76cac72010-01-26 14:04:11 +0200387 blk->bmds = bmds;
388 blk->sector = sector;
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200389 blk->nr_sectors = nr_sectors;
Liran Schourd76cac72010-01-26 14:04:11 +0200390
Liran Schour889ae392010-01-26 10:31:49 +0200391 if (is_async) {
Liran Schourd76cac72010-01-26 14:04:11 +0200392 blk->iov.iov_base = blk->buf;
393 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
394 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
395
Avishay Traegerff5c52a2011-04-03 11:31:45 +0300396 if (block_mig_state.submitted == 0) {
397 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
398 }
Liran Schour889ae392010-01-26 10:31:49 +0200399
Liran Schourd76cac72010-01-26 14:04:11 +0200400 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
401 nr_sectors, blk_mig_read_cb, blk);
Liran Schourd76cac72010-01-26 14:04:11 +0200402 block_mig_state.submitted++;
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200403 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
Liran Schourd76cac72010-01-26 14:04:11 +0200404 } else {
Juan Quinteladcd1d222011-09-21 23:01:54 +0200405 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
406 if (ret < 0) {
Liran Schourd76cac72010-01-26 14:04:11 +0200407 goto error;
408 }
409 blk_send(f, blk);
410
Anthony Liguori7267c092011-08-20 22:09:37 -0500411 g_free(blk->buf);
412 g_free(blk);
Liran Schourd76cac72010-01-26 14:04:11 +0200413 }
414
415 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
416 break;
417 }
418 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
419 bmds->cur_dirty = sector;
420 }
421
422 return (bmds->cur_dirty >= bmds->total_sectors);
423
Liran Schour889ae392010-01-26 10:31:49 +0200424error:
Luiz Capitulino539de122011-12-05 14:06:56 -0200425 DPRINTF("Error reading sector %" PRId64 "\n", sector);
Juan Quinteladcd1d222011-09-21 23:01:54 +0200426 qemu_file_set_error(f, ret);
Anthony Liguori7267c092011-08-20 22:09:37 -0500427 g_free(blk->buf);
428 g_free(blk);
Liran Schourd76cac72010-01-26 14:04:11 +0200429 return 0;
430}
431
Luiz Capitulino539de122011-12-05 14:06:56 -0200432static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
Liran Schourd76cac72010-01-26 14:04:11 +0200433{
434 BlkMigDevState *bmds;
435 int ret = 0;
436
437 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
Luiz Capitulino539de122011-12-05 14:06:56 -0200438 if (mig_save_device_dirty(f, bmds, is_async) == 0) {
Liran Schourd76cac72010-01-26 14:04:11 +0200439 ret = 1;
440 break;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200441 }
442 }
Jan Kiszka575a58d2009-11-30 18:21:20 +0100443
Liran Schourd76cac72010-01-26 14:04:11 +0200444 return ret;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200445}
446
447static void flush_blks(QEMUFile* f)
448{
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100449 BlkMigBlock *blk;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100450
malcd0f2c4c2010-02-07 02:03:50 +0300451 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100452 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
453 block_mig_state.transferred);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100454
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100455 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
456 if (qemu_file_rate_limit(f)) {
457 break;
458 }
Jan Kiszka4b640362009-11-30 18:21:21 +0100459 if (blk->ret < 0) {
Juan Quinteladcd1d222011-09-21 23:01:54 +0200460 qemu_file_set_error(f, blk->ret);
Jan Kiszka4b640362009-11-30 18:21:21 +0100461 break;
462 }
Jan Kiszka13f0b672009-11-30 18:21:21 +0100463 blk_send(f, blk);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100464
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100465 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -0500466 g_free(blk->buf);
467 g_free(blk);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100468
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100469 block_mig_state.read_done--;
470 block_mig_state.transferred++;
471 assert(block_mig_state.read_done >= 0);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200472 }
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200473
malcd0f2c4c2010-02-07 02:03:50 +0300474 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100475 block_mig_state.submitted, block_mig_state.read_done,
476 block_mig_state.transferred);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200477}
478
Liran Schour889ae392010-01-26 10:31:49 +0200479static int64_t get_remaining_dirty(void)
480{
481 BlkMigDevState *bmds;
482 int64_t dirty = 0;
483
484 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
485 dirty += bdrv_get_dirty_count(bmds->bs);
486 }
487
488 return dirty * BLOCK_SIZE;
489}
490
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200491static int is_stage2_completed(void)
492{
Liran Schour889ae392010-01-26 10:31:49 +0200493 int64_t remaining_dirty;
494 long double bwidth;
495
496 if (block_mig_state.bulk_completed == 1) {
497
498 remaining_dirty = get_remaining_dirty();
Yoshiaki Tamurabd0858b2010-07-26 13:25:41 +0900499 if (remaining_dirty == 0) {
500 return 1;
501 }
Liran Schour889ae392010-01-26 10:31:49 +0200502
Yoshiaki Tamurabd0858b2010-07-26 13:25:41 +0900503 bwidth = compute_read_bwidth();
Liran Schour889ae392010-01-26 10:31:49 +0200504
Yoshiaki Tamurabd0858b2010-07-26 13:25:41 +0900505 if ((remaining_dirty / bwidth) <=
Liran Schour889ae392010-01-26 10:31:49 +0200506 migrate_max_downtime()) {
Stefan Weil4238e262011-11-13 22:24:27 +0100507 /* finish stage2 because we think that we can finish remaining work
Liran Schour889ae392010-01-26 10:31:49 +0200508 below max_downtime */
509
510 return 1;
511 }
512 }
513
514 return 0;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200515}
516
Luiz Capitulino539de122011-12-05 14:06:56 -0200517static void blk_mig_cleanup(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100518{
Jan Kiszka82801d82009-11-30 18:21:21 +0100519 BlkMigDevState *bmds;
520 BlkMigBlock *blk;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100521
Marcelo Tosatti8f794c52011-01-26 12:12:31 -0200522 set_dirty_tracking(0);
523
Jan Kiszka82801d82009-11-30 18:21:21 +0100524 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
525 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
Marcelo Tosatti85916752011-01-26 12:12:35 -0200526 bdrv_set_in_use(bmds->bs, 0);
Marcelo Tosattif48905d2011-01-26 12:12:33 -0200527 drive_put_ref(drive_get_by_blockdev(bmds->bs));
Anthony Liguori7267c092011-08-20 22:09:37 -0500528 g_free(bmds->aio_bitmap);
529 g_free(bmds);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100530 }
531
Jan Kiszka82801d82009-11-30 18:21:21 +0100532 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
533 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -0500534 g_free(blk->buf);
535 g_free(blk);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100536 }
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100537}
538
Luiz Capitulino539de122011-12-05 14:06:56 -0200539static int block_save_live(QEMUFile *f, int stage, void *opaque)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200540{
Juan Quintela29757252011-10-19 15:22:18 +0200541 int ret;
542
malcd0f2c4c2010-02-07 02:03:50 +0300543 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100544 stage, block_mig_state.submitted, block_mig_state.transferred);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100545
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100546 if (stage < 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -0200547 blk_mig_cleanup();
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100548 return 0;
549 }
550
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100551 if (block_mig_state.blk_enable != 1) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200552 /* no need to migrate storage */
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100553 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200554 return 1;
555 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100556
557 if (stage == 1) {
Luiz Capitulino539de122011-12-05 14:06:56 -0200558 init_blk_migration(f);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100559
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200560 /* start track dirty blocks */
561 set_dirty_tracking(1);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200562 }
563
564 flush_blks(f);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100565
Juan Quintela29757252011-10-19 15:22:18 +0200566 ret = qemu_file_get_error(f);
567 if (ret) {
Luiz Capitulino539de122011-12-05 14:06:56 -0200568 blk_mig_cleanup();
Juan Quintela29757252011-10-19 15:22:18 +0200569 return ret;
Jan Kiszka4b640362009-11-30 18:21:21 +0100570 }
571
Liran Schourd76cac72010-01-26 14:04:11 +0200572 blk_mig_reset_dirty_cursor();
573
Liran Schour889ae392010-01-26 10:31:49 +0200574 if (stage == 2) {
Liran Schourd76cac72010-01-26 14:04:11 +0200575 /* control the rate of transfer */
576 while ((block_mig_state.submitted +
577 block_mig_state.read_done) * BLOCK_SIZE <
578 qemu_file_get_rate_limit(f)) {
579 if (block_mig_state.bulk_completed == 0) {
580 /* first finish the bulk phase */
Luiz Capitulino539de122011-12-05 14:06:56 -0200581 if (blk_mig_save_bulked_block(f) == 0) {
Liran Schour889ae392010-01-26 10:31:49 +0200582 /* finished saving bulk on all devices */
Liran Schourd76cac72010-01-26 14:04:11 +0200583 block_mig_state.bulk_completed = 1;
584 }
585 } else {
Luiz Capitulino539de122011-12-05 14:06:56 -0200586 if (blk_mig_save_dirty_block(f, 1) == 0) {
Liran Schourd76cac72010-01-26 14:04:11 +0200587 /* no more dirty blocks */
588 break;
589 }
590 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100591 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100592
Liran Schourd76cac72010-01-26 14:04:11 +0200593 flush_blks(f);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100594
Juan Quintela29757252011-10-19 15:22:18 +0200595 ret = qemu_file_get_error(f);
596 if (ret) {
Luiz Capitulino539de122011-12-05 14:06:56 -0200597 blk_mig_cleanup();
Juan Quintela29757252011-10-19 15:22:18 +0200598 return ret;
Liran Schourd76cac72010-01-26 14:04:11 +0200599 }
Jan Kiszka4b640362009-11-30 18:21:21 +0100600 }
601
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100602 if (stage == 3) {
Liran Schour889ae392010-01-26 10:31:49 +0200603 /* we know for sure that save bulk is completed and
604 all async read completed */
605 assert(block_mig_state.submitted == 0);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100606
Luiz Capitulino539de122011-12-05 14:06:56 -0200607 while (blk_mig_save_dirty_block(f, 0) != 0);
608 blk_mig_cleanup();
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100609
Jan Kiszka01e61e22009-12-01 15:20:17 +0100610 /* report completion */
611 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
612
Juan Quintela29757252011-10-19 15:22:18 +0200613 ret = qemu_file_get_error(f);
614 if (ret) {
615 return ret;
Jan Kiszka4b640362009-11-30 18:21:21 +0100616 }
617
Luiz Capitulino539de122011-12-05 14:06:56 -0200618 DPRINTF("Block migration completed\n");
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200619 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100620
621 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
622
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200623 return ((stage == 2) && is_stage2_completed());
624}
625
626static int block_load(QEMUFile *f, void *opaque, int version_id)
627{
Jan Kiszka01e61e22009-12-01 15:20:17 +0100628 static int banner_printed;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200629 int len, flags;
630 char device_name[256];
631 int64_t addr;
Pierre Riteau77358b52011-01-21 12:42:30 +0100632 BlockDriverState *bs, *bs_prev = NULL;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200633 uint8_t *buf;
Pierre Riteau77358b52011-01-21 12:42:30 +0100634 int64_t total_sectors = 0;
635 int nr_sectors;
Juan Quintela42802d42011-10-05 01:14:46 +0200636 int ret;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100637
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200638 do {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200639 addr = qemu_get_be64(f);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100640
Jan Kiszka6ea44302009-11-30 18:21:19 +0100641 flags = addr & ~BDRV_SECTOR_MASK;
642 addr >>= BDRV_SECTOR_BITS;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100643
644 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200645 /* get device name */
646 len = qemu_get_byte(f);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200647 qemu_get_buffer(f, (uint8_t *)device_name, len);
648 device_name[len] = '\0';
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100649
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200650 bs = bdrv_find(device_name);
Jan Kiszka4b640362009-11-30 18:21:21 +0100651 if (!bs) {
652 fprintf(stderr, "Error unknown block device %s\n",
653 device_name);
654 return -EINVAL;
655 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100656
Pierre Riteau77358b52011-01-21 12:42:30 +0100657 if (bs != bs_prev) {
658 bs_prev = bs;
659 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
660 if (total_sectors <= 0) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200661 error_report("Error getting length of block device %s",
Pierre Riteau77358b52011-01-21 12:42:30 +0100662 device_name);
663 return -EINVAL;
664 }
665 }
666
667 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
668 nr_sectors = total_sectors - addr;
669 } else {
670 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
671 }
672
Anthony Liguori7267c092011-08-20 22:09:37 -0500673 buf = g_malloc(BLOCK_SIZE);
Jan Kiszka575a58d2009-11-30 18:21:20 +0100674
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100675 qemu_get_buffer(f, buf, BLOCK_SIZE);
Pierre Riteau77358b52011-01-21 12:42:30 +0100676 ret = bdrv_write(bs, addr, buf, nr_sectors);
Jan Kiszka575a58d2009-11-30 18:21:20 +0100677
Anthony Liguori7267c092011-08-20 22:09:37 -0500678 g_free(buf);
Yoshiaki Tamurab02bea32010-07-20 18:19:00 +0900679 if (ret < 0) {
680 return ret;
681 }
Jan Kiszka01e61e22009-12-01 15:20:17 +0100682 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
683 if (!banner_printed) {
684 printf("Receiving block device images\n");
685 banner_printed = 1;
686 }
687 printf("Completed %d %%%c", (int)addr,
688 (addr == 100) ? '\n' : '\r');
689 fflush(stdout);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100690 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
Jan Kiszka4b640362009-11-30 18:21:21 +0100691 fprintf(stderr, "Unknown flags\n");
692 return -EINVAL;
693 }
Juan Quintela42802d42011-10-05 01:14:46 +0200694 ret = qemu_file_get_error(f);
695 if (ret != 0) {
696 return ret;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200697 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100698 } while (!(flags & BLK_MIG_FLAG_EOS));
699
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200700 return 0;
701}
702
703static void block_set_params(int blk_enable, int shared_base, void *opaque)
704{
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100705 block_mig_state.blk_enable = blk_enable;
706 block_mig_state.shared_base = shared_base;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100707
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200708 /* shared base means that blk_enable = 1 */
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100709 block_mig_state.blk_enable |= shared_base;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200710}
711
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200712void blk_mig_init(void)
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100713{
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100714 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
715 QSIMPLEQ_INIT(&block_mig_state.blk_list);
716
Alex Williamson0be71e32010-06-25 11:09:07 -0600717 register_savevm_live(NULL, "block", 0, 1, block_set_params,
718 block_save_live, NULL, block_load, &block_mig_state);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200719}