root / block-migration.c @ 130c57c0
History | View | Annotate | Download (20.2 kB)
1 | c163b5ca | lirans@il.ibm.com | /*
|
---|---|---|---|
2 | c163b5ca | lirans@il.ibm.com | * QEMU live block migration
|
3 | c163b5ca | lirans@il.ibm.com | *
|
4 | c163b5ca | lirans@il.ibm.com | * Copyright IBM, Corp. 2009
|
5 | c163b5ca | lirans@il.ibm.com | *
|
6 | c163b5ca | lirans@il.ibm.com | * Authors:
|
7 | c163b5ca | lirans@il.ibm.com | * Liran Schour <lirans@il.ibm.com>
|
8 | c163b5ca | lirans@il.ibm.com | *
|
9 | c163b5ca | lirans@il.ibm.com | * This work is licensed under the terms of the GNU GPL, version 2. See
|
10 | c163b5ca | lirans@il.ibm.com | * the COPYING file in the top-level directory.
|
11 | c163b5ca | lirans@il.ibm.com | *
|
12 | 6b620ca3 | Paolo Bonzini | * Contributions after 2012-01-13 are licensed under the terms of the
|
13 | 6b620ca3 | Paolo Bonzini | * GNU GPL, version 2 or (at your option) any later version.
|
14 | c163b5ca | lirans@il.ibm.com | */
|
15 | c163b5ca | lirans@il.ibm.com | |
16 | c163b5ca | lirans@il.ibm.com | #include "qemu-common.h" |
17 | c163b5ca | lirans@il.ibm.com | #include "block_int.h" |
18 | c163b5ca | lirans@il.ibm.com | #include "hw/hw.h" |
19 | 5e5328be | Jan Kiszka | #include "qemu-queue.h" |
20 | 889ae39c | Liran Schour | #include "qemu-timer.h" |
21 | c163b5ca | lirans@il.ibm.com | #include "block-migration.h" |
22 | 889ae39c | Liran Schour | #include "migration.h" |
23 | f48905d4 | Marcelo Tosatti | #include "blockdev.h" |
24 | c163b5ca | lirans@il.ibm.com | #include <assert.h> |
25 | c163b5ca | lirans@il.ibm.com | |
26 | 6ea44308 | Jan Kiszka | #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
|
27 | c163b5ca | lirans@il.ibm.com | |
28 | c163b5ca | lirans@il.ibm.com | #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01 |
29 | c163b5ca | lirans@il.ibm.com | #define BLK_MIG_FLAG_EOS 0x02 |
30 | 01e61e2d | Jan Kiszka | #define BLK_MIG_FLAG_PROGRESS 0x04 |
31 | c163b5ca | lirans@il.ibm.com | |
32 | c163b5ca | lirans@il.ibm.com | #define MAX_IS_ALLOCATED_SEARCH 65536 |
33 | c163b5ca | lirans@il.ibm.com | |
34 | c163b5ca | lirans@il.ibm.com | //#define DEBUG_BLK_MIGRATION
|
35 | c163b5ca | lirans@il.ibm.com | |
36 | c163b5ca | lirans@il.ibm.com | #ifdef DEBUG_BLK_MIGRATION
|
37 | d0f2c4c6 | malc | #define DPRINTF(fmt, ...) \
|
38 | c163b5ca | lirans@il.ibm.com | do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0) |
39 | c163b5ca | lirans@il.ibm.com | #else
|
40 | d0f2c4c6 | malc | #define DPRINTF(fmt, ...) \
|
41 | c163b5ca | lirans@il.ibm.com | do { } while (0) |
42 | c163b5ca | lirans@il.ibm.com | #endif
|
43 | c163b5ca | lirans@il.ibm.com | |
44 | a55eb92c | Jan Kiszka | typedef struct BlkMigDevState { |
45 | a55eb92c | Jan Kiszka | BlockDriverState *bs; |
46 | a55eb92c | Jan Kiszka | int bulk_completed;
|
47 | a55eb92c | Jan Kiszka | int shared_base;
|
48 | a55eb92c | Jan Kiszka | int64_t cur_sector; |
49 | d76cac7d | Liran Schour | int64_t cur_dirty; |
50 | 82801d8f | Jan Kiszka | int64_t completed_sectors; |
51 | a55eb92c | Jan Kiszka | int64_t total_sectors; |
52 | a55eb92c | Jan Kiszka | int64_t dirty; |
53 | 5e5328be | Jan Kiszka | QSIMPLEQ_ENTRY(BlkMigDevState) entry; |
54 | 33656af7 | Marcelo Tosatti | unsigned long *aio_bitmap; |
55 | a55eb92c | Jan Kiszka | } BlkMigDevState; |
56 | a55eb92c | Jan Kiszka | |
57 | c163b5ca | lirans@il.ibm.com | typedef struct BlkMigBlock { |
58 | c163b5ca | lirans@il.ibm.com | uint8_t *buf; |
59 | c163b5ca | lirans@il.ibm.com | BlkMigDevState *bmds; |
60 | c163b5ca | lirans@il.ibm.com | int64_t sector; |
61 | 33656af7 | Marcelo Tosatti | int nr_sectors;
|
62 | c163b5ca | lirans@il.ibm.com | struct iovec iov;
|
63 | c163b5ca | lirans@il.ibm.com | QEMUIOVector qiov; |
64 | c163b5ca | lirans@il.ibm.com | BlockDriverAIOCB *aiocb; |
65 | c163b5ca | lirans@il.ibm.com | int ret;
|
66 | 5e5328be | Jan Kiszka | QSIMPLEQ_ENTRY(BlkMigBlock) entry; |
67 | c163b5ca | lirans@il.ibm.com | } BlkMigBlock; |
68 | c163b5ca | lirans@il.ibm.com | |
69 | c163b5ca | lirans@il.ibm.com | typedef struct BlkMigState { |
70 | c163b5ca | lirans@il.ibm.com | int blk_enable;
|
71 | c163b5ca | lirans@il.ibm.com | int shared_base;
|
72 | 5e5328be | Jan Kiszka | QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list; |
73 | 5e5328be | Jan Kiszka | QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list; |
74 | c163b5ca | lirans@il.ibm.com | int submitted;
|
75 | c163b5ca | lirans@il.ibm.com | int read_done;
|
76 | c163b5ca | lirans@il.ibm.com | int transferred;
|
77 | 82801d8f | Jan Kiszka | int64_t total_sector_sum; |
78 | 01e61e2d | Jan Kiszka | int prev_progress;
|
79 | e970ec0b | Liran Schour | int bulk_completed;
|
80 | 889ae39c | Liran Schour | long double total_time; |
81 | ff5c52a3 | Avishay Traeger | long double prev_time_offset; |
82 | 889ae39c | Liran Schour | int reads;
|
83 | c163b5ca | lirans@il.ibm.com | } BlkMigState; |
84 | c163b5ca | lirans@il.ibm.com | |
85 | d11ecd3d | Jan Kiszka | static BlkMigState block_mig_state;
|
86 | c163b5ca | lirans@il.ibm.com | |
87 | 13f0b67f | Jan Kiszka | static void blk_send(QEMUFile *f, BlkMigBlock * blk) |
88 | 13f0b67f | Jan Kiszka | { |
89 | 13f0b67f | Jan Kiszka | int len;
|
90 | 13f0b67f | Jan Kiszka | |
91 | 13f0b67f | Jan Kiszka | /* sector number and flags */
|
92 | 13f0b67f | Jan Kiszka | qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS) |
93 | 13f0b67f | Jan Kiszka | | BLK_MIG_FLAG_DEVICE_BLOCK); |
94 | 13f0b67f | Jan Kiszka | |
95 | 13f0b67f | Jan Kiszka | /* device name */
|
96 | 13f0b67f | Jan Kiszka | len = strlen(blk->bmds->bs->device_name); |
97 | 13f0b67f | Jan Kiszka | qemu_put_byte(f, len); |
98 | 13f0b67f | Jan Kiszka | qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len); |
99 | 13f0b67f | Jan Kiszka | |
100 | 13f0b67f | Jan Kiszka | qemu_put_buffer(f, blk->buf, BLOCK_SIZE); |
101 | 13f0b67f | Jan Kiszka | } |
102 | 13f0b67f | Jan Kiszka | |
103 | 25f23643 | Jan Kiszka | int blk_mig_active(void) |
104 | 25f23643 | Jan Kiszka | { |
105 | 25f23643 | Jan Kiszka | return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
|
106 | 25f23643 | Jan Kiszka | } |
107 | 25f23643 | Jan Kiszka | |
108 | 25f23643 | Jan Kiszka | uint64_t blk_mig_bytes_transferred(void)
|
109 | 25f23643 | Jan Kiszka | { |
110 | 25f23643 | Jan Kiszka | BlkMigDevState *bmds; |
111 | 25f23643 | Jan Kiszka | uint64_t sum = 0;
|
112 | 25f23643 | Jan Kiszka | |
113 | 25f23643 | Jan Kiszka | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
114 | 25f23643 | Jan Kiszka | sum += bmds->completed_sectors; |
115 | 25f23643 | Jan Kiszka | } |
116 | 25f23643 | Jan Kiszka | return sum << BDRV_SECTOR_BITS;
|
117 | 25f23643 | Jan Kiszka | } |
118 | 25f23643 | Jan Kiszka | |
119 | 25f23643 | Jan Kiszka | uint64_t blk_mig_bytes_remaining(void)
|
120 | 25f23643 | Jan Kiszka | { |
121 | 25f23643 | Jan Kiszka | return blk_mig_bytes_total() - blk_mig_bytes_transferred();
|
122 | 25f23643 | Jan Kiszka | } |
123 | 25f23643 | Jan Kiszka | |
124 | 25f23643 | Jan Kiszka | uint64_t blk_mig_bytes_total(void)
|
125 | 25f23643 | Jan Kiszka | { |
126 | 25f23643 | Jan Kiszka | BlkMigDevState *bmds; |
127 | 25f23643 | Jan Kiszka | uint64_t sum = 0;
|
128 | 25f23643 | Jan Kiszka | |
129 | 25f23643 | Jan Kiszka | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
130 | 25f23643 | Jan Kiszka | sum += bmds->total_sectors; |
131 | 25f23643 | Jan Kiszka | } |
132 | 25f23643 | Jan Kiszka | return sum << BDRV_SECTOR_BITS;
|
133 | 25f23643 | Jan Kiszka | } |
134 | 25f23643 | Jan Kiszka | |
135 | 889ae39c | Liran Schour | static inline long double compute_read_bwidth(void) |
136 | 889ae39c | Liran Schour | { |
137 | 889ae39c | Liran Schour | assert(block_mig_state.total_time != 0);
|
138 | 155eb9aa | Avishay Traeger | return (block_mig_state.reads / block_mig_state.total_time) * BLOCK_SIZE;
|
139 | 889ae39c | Liran Schour | } |
140 | 889ae39c | Liran Schour | |
141 | 33656af7 | Marcelo Tosatti | static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector) |
142 | 33656af7 | Marcelo Tosatti | { |
143 | 33656af7 | Marcelo Tosatti | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
144 | 33656af7 | Marcelo Tosatti | |
145 | 62155e2b | Marcelo Tosatti | if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
|
146 | 33656af7 | Marcelo Tosatti | return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
147 | 33656af7 | Marcelo Tosatti | (1UL << (chunk % (sizeof(unsigned long) * 8)))); |
148 | 33656af7 | Marcelo Tosatti | } else {
|
149 | 33656af7 | Marcelo Tosatti | return 0; |
150 | 33656af7 | Marcelo Tosatti | } |
151 | 33656af7 | Marcelo Tosatti | } |
152 | 33656af7 | Marcelo Tosatti | |
153 | 33656af7 | Marcelo Tosatti | static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num, |
154 | 33656af7 | Marcelo Tosatti | int nb_sectors, int set) |
155 | 33656af7 | Marcelo Tosatti | { |
156 | 33656af7 | Marcelo Tosatti | int64_t start, end; |
157 | 33656af7 | Marcelo Tosatti | unsigned long val, idx, bit; |
158 | 33656af7 | Marcelo Tosatti | |
159 | 33656af7 | Marcelo Tosatti | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
160 | 33656af7 | Marcelo Tosatti | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
|
161 | 33656af7 | Marcelo Tosatti | |
162 | 33656af7 | Marcelo Tosatti | for (; start <= end; start++) {
|
163 | 33656af7 | Marcelo Tosatti | idx = start / (sizeof(unsigned long) * 8); |
164 | 33656af7 | Marcelo Tosatti | bit = start % (sizeof(unsigned long) * 8); |
165 | 33656af7 | Marcelo Tosatti | val = bmds->aio_bitmap[idx]; |
166 | 33656af7 | Marcelo Tosatti | if (set) {
|
167 | 62155e2b | Marcelo Tosatti | val |= 1UL << bit;
|
168 | 33656af7 | Marcelo Tosatti | } else {
|
169 | 62155e2b | Marcelo Tosatti | val &= ~(1UL << bit);
|
170 | 33656af7 | Marcelo Tosatti | } |
171 | 33656af7 | Marcelo Tosatti | bmds->aio_bitmap[idx] = val; |
172 | 33656af7 | Marcelo Tosatti | } |
173 | 33656af7 | Marcelo Tosatti | } |
174 | 33656af7 | Marcelo Tosatti | |
175 | 33656af7 | Marcelo Tosatti | static void alloc_aio_bitmap(BlkMigDevState *bmds) |
176 | 33656af7 | Marcelo Tosatti | { |
177 | 33656af7 | Marcelo Tosatti | BlockDriverState *bs = bmds->bs; |
178 | 33656af7 | Marcelo Tosatti | int64_t bitmap_size; |
179 | 33656af7 | Marcelo Tosatti | |
180 | 33656af7 | Marcelo Tosatti | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + |
181 | 33656af7 | Marcelo Tosatti | BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; |
182 | 33656af7 | Marcelo Tosatti | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
|
183 | 33656af7 | Marcelo Tosatti | |
184 | 7267c094 | Anthony Liguori | bmds->aio_bitmap = g_malloc0(bitmap_size); |
185 | 33656af7 | Marcelo Tosatti | } |
186 | 33656af7 | Marcelo Tosatti | |
187 | c163b5ca | lirans@il.ibm.com | static void blk_mig_read_cb(void *opaque, int ret) |
188 | c163b5ca | lirans@il.ibm.com | { |
189 | ff5c52a3 | Avishay Traeger | long double curr_time = qemu_get_clock_ns(rt_clock); |
190 | c163b5ca | lirans@il.ibm.com | BlkMigBlock *blk = opaque; |
191 | a55eb92c | Jan Kiszka | |
192 | c163b5ca | lirans@il.ibm.com | blk->ret = ret; |
193 | a55eb92c | Jan Kiszka | |
194 | ff5c52a3 | Avishay Traeger | block_mig_state.reads++; |
195 | ff5c52a3 | Avishay Traeger | block_mig_state.total_time += (curr_time - block_mig_state.prev_time_offset); |
196 | ff5c52a3 | Avishay Traeger | block_mig_state.prev_time_offset = curr_time; |
197 | 889ae39c | Liran Schour | |
198 | 5e5328be | Jan Kiszka | QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry); |
199 | 33656af7 | Marcelo Tosatti | bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
|
200 | a55eb92c | Jan Kiszka | |
201 | d11ecd3d | Jan Kiszka | block_mig_state.submitted--; |
202 | d11ecd3d | Jan Kiszka | block_mig_state.read_done++; |
203 | d11ecd3d | Jan Kiszka | assert(block_mig_state.submitted >= 0);
|
204 | c163b5ca | lirans@il.ibm.com | } |
205 | c163b5ca | lirans@il.ibm.com | |
206 | 539de124 | Luiz Capitulino | static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) |
207 | a55eb92c | Jan Kiszka | { |
208 | 57cce12d | Jan Kiszka | int64_t total_sectors = bmds->total_sectors; |
209 | 57cce12d | Jan Kiszka | int64_t cur_sector = bmds->cur_sector; |
210 | 57cce12d | Jan Kiszka | BlockDriverState *bs = bmds->bs; |
211 | c163b5ca | lirans@il.ibm.com | BlkMigBlock *blk; |
212 | 13f0b67f | Jan Kiszka | int nr_sectors;
|
213 | a55eb92c | Jan Kiszka | |
214 | 57cce12d | Jan Kiszka | if (bmds->shared_base) {
|
215 | b1d10856 | Jan Kiszka | while (cur_sector < total_sectors &&
|
216 | 57cce12d | Jan Kiszka | !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH, |
217 | 57cce12d | Jan Kiszka | &nr_sectors)) { |
218 | c163b5ca | lirans@il.ibm.com | cur_sector += nr_sectors; |
219 | c163b5ca | lirans@il.ibm.com | } |
220 | c163b5ca | lirans@il.ibm.com | } |
221 | a55eb92c | Jan Kiszka | |
222 | a55eb92c | Jan Kiszka | if (cur_sector >= total_sectors) {
|
223 | 82801d8f | Jan Kiszka | bmds->cur_sector = bmds->completed_sectors = total_sectors; |
224 | c163b5ca | lirans@il.ibm.com | return 1; |
225 | c163b5ca | lirans@il.ibm.com | } |
226 | a55eb92c | Jan Kiszka | |
227 | 82801d8f | Jan Kiszka | bmds->completed_sectors = cur_sector; |
228 | a55eb92c | Jan Kiszka | |
229 | 57cce12d | Jan Kiszka | cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
|
230 | 57cce12d | Jan Kiszka | |
231 | 6ea44308 | Jan Kiszka | /* we are going to transfer a full block even if it is not allocated */
|
232 | 6ea44308 | Jan Kiszka | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; |
233 | c163b5ca | lirans@il.ibm.com | |
234 | 6ea44308 | Jan Kiszka | if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
|
235 | 57cce12d | Jan Kiszka | nr_sectors = total_sectors - cur_sector; |
236 | c163b5ca | lirans@il.ibm.com | } |
237 | a55eb92c | Jan Kiszka | |
238 | 7267c094 | Anthony Liguori | blk = g_malloc(sizeof(BlkMigBlock));
|
239 | 7267c094 | Anthony Liguori | blk->buf = g_malloc(BLOCK_SIZE); |
240 | 13f0b67f | Jan Kiszka | blk->bmds = bmds; |
241 | 13f0b67f | Jan Kiszka | blk->sector = cur_sector; |
242 | 33656af7 | Marcelo Tosatti | blk->nr_sectors = nr_sectors; |
243 | a55eb92c | Jan Kiszka | |
244 | e970ec0b | Liran Schour | blk->iov.iov_base = blk->buf; |
245 | e970ec0b | Liran Schour | blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE; |
246 | e970ec0b | Liran Schour | qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
|
247 | a55eb92c | Jan Kiszka | |
248 | ff5c52a3 | Avishay Traeger | if (block_mig_state.submitted == 0) { |
249 | ff5c52a3 | Avishay Traeger | block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock); |
250 | ff5c52a3 | Avishay Traeger | } |
251 | 889ae39c | Liran Schour | |
252 | e970ec0b | Liran Schour | blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov, |
253 | e970ec0b | Liran Schour | nr_sectors, blk_mig_read_cb, blk); |
254 | e970ec0b | Liran Schour | block_mig_state.submitted++; |
255 | d76cac7d | Liran Schour | |
256 | 13f0b67f | Jan Kiszka | bdrv_reset_dirty(bs, cur_sector, nr_sectors); |
257 | 13f0b67f | Jan Kiszka | bmds->cur_sector = cur_sector + nr_sectors; |
258 | a55eb92c | Jan Kiszka | |
259 | 13f0b67f | Jan Kiszka | return (bmds->cur_sector >= total_sectors);
|
260 | c163b5ca | lirans@il.ibm.com | } |
261 | c163b5ca | lirans@il.ibm.com | |
262 | c163b5ca | lirans@il.ibm.com | static void set_dirty_tracking(int enable) |
263 | c163b5ca | lirans@il.ibm.com | { |
264 | c163b5ca | lirans@il.ibm.com | BlkMigDevState *bmds; |
265 | 5e5328be | Jan Kiszka | |
266 | 5e5328be | Jan Kiszka | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
267 | a55eb92c | Jan Kiszka | bdrv_set_dirty_tracking(bmds->bs, enable); |
268 | c163b5ca | lirans@il.ibm.com | } |
269 | c163b5ca | lirans@il.ibm.com | } |
270 | c163b5ca | lirans@il.ibm.com | |
271 | b66460e4 | Stefan Hajnoczi | static void init_blk_migration_it(void *opaque, BlockDriverState *bs) |
272 | c163b5ca | lirans@il.ibm.com | { |
273 | 5e5328be | Jan Kiszka | BlkMigDevState *bmds; |
274 | 792773b2 | Jan Kiszka | int64_t sectors; |
275 | a55eb92c | Jan Kiszka | |
276 | d246673d | Markus Armbruster | if (!bdrv_is_read_only(bs)) {
|
277 | b66460e4 | Stefan Hajnoczi | sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
278 | 31f54f24 | Shahar Havivi | if (sectors <= 0) { |
279 | b66460e4 | Stefan Hajnoczi | return;
|
280 | b66460e4 | Stefan Hajnoczi | } |
281 | b66460e4 | Stefan Hajnoczi | |
282 | 7267c094 | Anthony Liguori | bmds = g_malloc0(sizeof(BlkMigDevState));
|
283 | b66460e4 | Stefan Hajnoczi | bmds->bs = bs; |
284 | b66460e4 | Stefan Hajnoczi | bmds->bulk_completed = 0;
|
285 | b66460e4 | Stefan Hajnoczi | bmds->total_sectors = sectors; |
286 | b66460e4 | Stefan Hajnoczi | bmds->completed_sectors = 0;
|
287 | b66460e4 | Stefan Hajnoczi | bmds->shared_base = block_mig_state.shared_base; |
288 | 33656af7 | Marcelo Tosatti | alloc_aio_bitmap(bmds); |
289 | f48905d4 | Marcelo Tosatti | drive_get_ref(drive_get_by_blockdev(bs)); |
290 | 8591675f | Marcelo Tosatti | bdrv_set_in_use(bs, 1);
|
291 | b66460e4 | Stefan Hajnoczi | |
292 | b66460e4 | Stefan Hajnoczi | block_mig_state.total_sector_sum += sectors; |
293 | b66460e4 | Stefan Hajnoczi | |
294 | b66460e4 | Stefan Hajnoczi | if (bmds->shared_base) {
|
295 | 539de124 | Luiz Capitulino | DPRINTF("Start migration for %s with shared base image\n",
|
296 | 539de124 | Luiz Capitulino | bs->device_name); |
297 | b66460e4 | Stefan Hajnoczi | } else {
|
298 | 539de124 | Luiz Capitulino | DPRINTF("Start full migration for %s\n", bs->device_name);
|
299 | b66460e4 | Stefan Hajnoczi | } |
300 | b66460e4 | Stefan Hajnoczi | |
301 | b66460e4 | Stefan Hajnoczi | QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry); |
302 | b66460e4 | Stefan Hajnoczi | } |
303 | b66460e4 | Stefan Hajnoczi | } |
304 | b66460e4 | Stefan Hajnoczi | |
305 | 539de124 | Luiz Capitulino | static void init_blk_migration(QEMUFile *f) |
306 | b66460e4 | Stefan Hajnoczi | { |
307 | 69d63a97 | Jan Kiszka | block_mig_state.submitted = 0;
|
308 | 69d63a97 | Jan Kiszka | block_mig_state.read_done = 0;
|
309 | 69d63a97 | Jan Kiszka | block_mig_state.transferred = 0;
|
310 | 82801d8f | Jan Kiszka | block_mig_state.total_sector_sum = 0;
|
311 | 01e61e2d | Jan Kiszka | block_mig_state.prev_progress = -1;
|
312 | e970ec0b | Liran Schour | block_mig_state.bulk_completed = 0;
|
313 | 889ae39c | Liran Schour | block_mig_state.total_time = 0;
|
314 | 889ae39c | Liran Schour | block_mig_state.reads = 0;
|
315 | 69d63a97 | Jan Kiszka | |
316 | 539de124 | Luiz Capitulino | bdrv_iterate(init_blk_migration_it, NULL);
|
317 | c163b5ca | lirans@il.ibm.com | } |
318 | c163b5ca | lirans@il.ibm.com | |
319 | 539de124 | Luiz Capitulino | static int blk_mig_save_bulked_block(QEMUFile *f) |
320 | c163b5ca | lirans@il.ibm.com | { |
321 | 82801d8f | Jan Kiszka | int64_t completed_sector_sum = 0;
|
322 | c163b5ca | lirans@il.ibm.com | BlkMigDevState *bmds; |
323 | 01e61e2d | Jan Kiszka | int progress;
|
324 | 82801d8f | Jan Kiszka | int ret = 0; |
325 | c163b5ca | lirans@il.ibm.com | |
326 | 5e5328be | Jan Kiszka | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
327 | a55eb92c | Jan Kiszka | if (bmds->bulk_completed == 0) { |
328 | 539de124 | Luiz Capitulino | if (mig_save_device_bulk(f, bmds) == 1) { |
329 | 57cce12d | Jan Kiszka | /* completed bulk section for this device */
|
330 | 57cce12d | Jan Kiszka | bmds->bulk_completed = 1;
|
331 | c163b5ca | lirans@il.ibm.com | } |
332 | 82801d8f | Jan Kiszka | completed_sector_sum += bmds->completed_sectors; |
333 | 82801d8f | Jan Kiszka | ret = 1;
|
334 | 82801d8f | Jan Kiszka | break;
|
335 | 82801d8f | Jan Kiszka | } else {
|
336 | 82801d8f | Jan Kiszka | completed_sector_sum += bmds->completed_sectors; |
337 | c163b5ca | lirans@il.ibm.com | } |
338 | c163b5ca | lirans@il.ibm.com | } |
339 | a55eb92c | Jan Kiszka | |
340 | 8b6b2afc | Pierre Riteau | if (block_mig_state.total_sector_sum != 0) { |
341 | 8b6b2afc | Pierre Riteau | progress = completed_sector_sum * 100 /
|
342 | 8b6b2afc | Pierre Riteau | block_mig_state.total_sector_sum; |
343 | 8b6b2afc | Pierre Riteau | } else {
|
344 | 8b6b2afc | Pierre Riteau | progress = 100;
|
345 | 8b6b2afc | Pierre Riteau | } |
346 | 01e61e2d | Jan Kiszka | if (progress != block_mig_state.prev_progress) {
|
347 | 01e61e2d | Jan Kiszka | block_mig_state.prev_progress = progress; |
348 | 01e61e2d | Jan Kiszka | qemu_put_be64(f, (progress << BDRV_SECTOR_BITS) |
349 | 01e61e2d | Jan Kiszka | | BLK_MIG_FLAG_PROGRESS); |
350 | 539de124 | Luiz Capitulino | DPRINTF("Completed %d %%\r", progress);
|
351 | 82801d8f | Jan Kiszka | } |
352 | 82801d8f | Jan Kiszka | |
353 | 82801d8f | Jan Kiszka | return ret;
|
354 | c163b5ca | lirans@il.ibm.com | } |
355 | c163b5ca | lirans@il.ibm.com | |
356 | d76cac7d | Liran Schour | static void blk_mig_reset_dirty_cursor(void) |
357 | c163b5ca | lirans@il.ibm.com | { |
358 | c163b5ca | lirans@il.ibm.com | BlkMigDevState *bmds; |
359 | d76cac7d | Liran Schour | |
360 | d76cac7d | Liran Schour | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
361 | d76cac7d | Liran Schour | bmds->cur_dirty = 0;
|
362 | d76cac7d | Liran Schour | } |
363 | d76cac7d | Liran Schour | } |
364 | d76cac7d | Liran Schour | |
365 | 539de124 | Luiz Capitulino | static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds, |
366 | 539de124 | Luiz Capitulino | int is_async)
|
367 | d76cac7d | Liran Schour | { |
368 | d76cac7d | Liran Schour | BlkMigBlock *blk; |
369 | d76cac7d | Liran Schour | int64_t total_sectors = bmds->total_sectors; |
370 | c163b5ca | lirans@il.ibm.com | int64_t sector; |
371 | d76cac7d | Liran Schour | int nr_sectors;
|
372 | dcd1d224 | Juan Quintela | int ret = -EIO;
|
373 | a55eb92c | Jan Kiszka | |
374 | d76cac7d | Liran Schour | for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
|
375 | 62155e2b | Marcelo Tosatti | if (bmds_aio_inflight(bmds, sector)) {
|
376 | 922453bc | Stefan Hajnoczi | bdrv_drain_all(); |
377 | 62155e2b | Marcelo Tosatti | } |
378 | d76cac7d | Liran Schour | if (bdrv_get_dirty(bmds->bs, sector)) {
|
379 | 575a58d7 | Jan Kiszka | |
380 | d76cac7d | Liran Schour | if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
|
381 | d76cac7d | Liran Schour | nr_sectors = total_sectors - sector; |
382 | d76cac7d | Liran Schour | } else {
|
383 | d76cac7d | Liran Schour | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; |
384 | d76cac7d | Liran Schour | } |
385 | 7267c094 | Anthony Liguori | blk = g_malloc(sizeof(BlkMigBlock));
|
386 | 7267c094 | Anthony Liguori | blk->buf = g_malloc(BLOCK_SIZE); |
387 | d76cac7d | Liran Schour | blk->bmds = bmds; |
388 | d76cac7d | Liran Schour | blk->sector = sector; |
389 | 33656af7 | Marcelo Tosatti | blk->nr_sectors = nr_sectors; |
390 | d76cac7d | Liran Schour | |
391 | 889ae39c | Liran Schour | if (is_async) {
|
392 | d76cac7d | Liran Schour | blk->iov.iov_base = blk->buf; |
393 | d76cac7d | Liran Schour | blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE; |
394 | d76cac7d | Liran Schour | qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
|
395 | d76cac7d | Liran Schour | |
396 | ff5c52a3 | Avishay Traeger | if (block_mig_state.submitted == 0) { |
397 | ff5c52a3 | Avishay Traeger | block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock); |
398 | ff5c52a3 | Avishay Traeger | } |
399 | 889ae39c | Liran Schour | |
400 | d76cac7d | Liran Schour | blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov, |
401 | d76cac7d | Liran Schour | nr_sectors, blk_mig_read_cb, blk); |
402 | d76cac7d | Liran Schour | block_mig_state.submitted++; |
403 | 33656af7 | Marcelo Tosatti | bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
|
404 | d76cac7d | Liran Schour | } else {
|
405 | dcd1d224 | Juan Quintela | ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors); |
406 | dcd1d224 | Juan Quintela | if (ret < 0) { |
407 | d76cac7d | Liran Schour | goto error;
|
408 | c163b5ca | lirans@il.ibm.com | } |
409 | d76cac7d | Liran Schour | blk_send(f, blk); |
410 | a55eb92c | Jan Kiszka | |
411 | 7267c094 | Anthony Liguori | g_free(blk->buf); |
412 | 7267c094 | Anthony Liguori | g_free(blk); |
413 | a55eb92c | Jan Kiszka | } |
414 | d76cac7d | Liran Schour | |
415 | d76cac7d | Liran Schour | bdrv_reset_dirty(bmds->bs, sector, nr_sectors); |
416 | d76cac7d | Liran Schour | break;
|
417 | c163b5ca | lirans@il.ibm.com | } |
418 | d76cac7d | Liran Schour | sector += BDRV_SECTORS_PER_DIRTY_CHUNK; |
419 | d76cac7d | Liran Schour | bmds->cur_dirty = sector; |
420 | c163b5ca | lirans@il.ibm.com | } |
421 | 575a58d7 | Jan Kiszka | |
422 | d76cac7d | Liran Schour | return (bmds->cur_dirty >= bmds->total_sectors);
|
423 | d76cac7d | Liran Schour | |
424 | 889ae39c | Liran Schour | error:
|
425 | 539de124 | Luiz Capitulino | DPRINTF("Error reading sector %" PRId64 "\n", sector); |
426 | 7267c094 | Anthony Liguori | g_free(blk->buf); |
427 | 7267c094 | Anthony Liguori | g_free(blk); |
428 | 43be3a25 | Juan Quintela | return ret;
|
429 | d76cac7d | Liran Schour | } |
430 | d76cac7d | Liran Schour | |
431 | ceb2bd09 | Juan Quintela | /* return value:
|
432 | ceb2bd09 | Juan Quintela | * 0: too much data for max_downtime
|
433 | ceb2bd09 | Juan Quintela | * 1: few enough data for max_downtime
|
434 | ceb2bd09 | Juan Quintela | */
|
435 | 539de124 | Luiz Capitulino | static int blk_mig_save_dirty_block(QEMUFile *f, int is_async) |
436 | d76cac7d | Liran Schour | { |
437 | d76cac7d | Liran Schour | BlkMigDevState *bmds; |
438 | ceb2bd09 | Juan Quintela | int ret = 1; |
439 | d76cac7d | Liran Schour | |
440 | d76cac7d | Liran Schour | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
441 | ceb2bd09 | Juan Quintela | ret = mig_save_device_dirty(f, bmds, is_async); |
442 | 43be3a25 | Juan Quintela | if (ret <= 0) { |
443 | d76cac7d | Liran Schour | break;
|
444 | d76cac7d | Liran Schour | } |
445 | d76cac7d | Liran Schour | } |
446 | d76cac7d | Liran Schour | |
447 | d76cac7d | Liran Schour | return ret;
|
448 | c163b5ca | lirans@il.ibm.com | } |
449 | c163b5ca | lirans@il.ibm.com | |
450 | 59feec42 | Juan Quintela | static int flush_blks(QEMUFile *f) |
451 | c163b5ca | lirans@il.ibm.com | { |
452 | 5e5328be | Jan Kiszka | BlkMigBlock *blk; |
453 | 59feec42 | Juan Quintela | int ret = 0; |
454 | a55eb92c | Jan Kiszka | |
455 | d0f2c4c6 | malc | DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
|
456 | d11ecd3d | Jan Kiszka | __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done, |
457 | d11ecd3d | Jan Kiszka | block_mig_state.transferred); |
458 | a55eb92c | Jan Kiszka | |
459 | 5e5328be | Jan Kiszka | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
460 | 5e5328be | Jan Kiszka | if (qemu_file_rate_limit(f)) {
|
461 | 5e5328be | Jan Kiszka | break;
|
462 | 5e5328be | Jan Kiszka | } |
463 | 4b640365 | Jan Kiszka | if (blk->ret < 0) { |
464 | 59feec42 | Juan Quintela | ret = blk->ret; |
465 | 4b640365 | Jan Kiszka | break;
|
466 | 4b640365 | Jan Kiszka | } |
467 | 13f0b67f | Jan Kiszka | blk_send(f, blk); |
468 | a55eb92c | Jan Kiszka | |
469 | 5e5328be | Jan Kiszka | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); |
470 | 7267c094 | Anthony Liguori | g_free(blk->buf); |
471 | 7267c094 | Anthony Liguori | g_free(blk); |
472 | a55eb92c | Jan Kiszka | |
473 | d11ecd3d | Jan Kiszka | block_mig_state.read_done--; |
474 | d11ecd3d | Jan Kiszka | block_mig_state.transferred++; |
475 | d11ecd3d | Jan Kiszka | assert(block_mig_state.read_done >= 0);
|
476 | c163b5ca | lirans@il.ibm.com | } |
477 | c163b5ca | lirans@il.ibm.com | |
478 | d0f2c4c6 | malc | DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
|
479 | d11ecd3d | Jan Kiszka | block_mig_state.submitted, block_mig_state.read_done, |
480 | d11ecd3d | Jan Kiszka | block_mig_state.transferred); |
481 | 59feec42 | Juan Quintela | return ret;
|
482 | c163b5ca | lirans@il.ibm.com | } |
483 | c163b5ca | lirans@il.ibm.com | |
484 | 889ae39c | Liran Schour | static int64_t get_remaining_dirty(void) |
485 | 889ae39c | Liran Schour | { |
486 | 889ae39c | Liran Schour | BlkMigDevState *bmds; |
487 | 889ae39c | Liran Schour | int64_t dirty = 0;
|
488 | 889ae39c | Liran Schour | |
489 | 889ae39c | Liran Schour | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
490 | 889ae39c | Liran Schour | dirty += bdrv_get_dirty_count(bmds->bs); |
491 | 889ae39c | Liran Schour | } |
492 | 889ae39c | Liran Schour | |
493 | 889ae39c | Liran Schour | return dirty * BLOCK_SIZE;
|
494 | 889ae39c | Liran Schour | } |
495 | 889ae39c | Liran Schour | |
496 | c163b5ca | lirans@il.ibm.com | static int is_stage2_completed(void) |
497 | c163b5ca | lirans@il.ibm.com | { |
498 | 889ae39c | Liran Schour | int64_t remaining_dirty; |
499 | 889ae39c | Liran Schour | long double bwidth; |
500 | 889ae39c | Liran Schour | |
501 | 889ae39c | Liran Schour | if (block_mig_state.bulk_completed == 1) { |
502 | 889ae39c | Liran Schour | |
503 | 889ae39c | Liran Schour | remaining_dirty = get_remaining_dirty(); |
504 | bd0858bb | Yoshiaki Tamura | if (remaining_dirty == 0) { |
505 | bd0858bb | Yoshiaki Tamura | return 1; |
506 | bd0858bb | Yoshiaki Tamura | } |
507 | 889ae39c | Liran Schour | |
508 | bd0858bb | Yoshiaki Tamura | bwidth = compute_read_bwidth(); |
509 | 889ae39c | Liran Schour | |
510 | bd0858bb | Yoshiaki Tamura | if ((remaining_dirty / bwidth) <=
|
511 | 889ae39c | Liran Schour | migrate_max_downtime()) { |
512 | 4238e264 | Stefan Weil | /* finish stage2 because we think that we can finish remaining work
|
513 | 889ae39c | Liran Schour | below max_downtime */
|
514 | 889ae39c | Liran Schour | |
515 | 889ae39c | Liran Schour | return 1; |
516 | 889ae39c | Liran Schour | } |
517 | 889ae39c | Liran Schour | } |
518 | 889ae39c | Liran Schour | |
519 | 889ae39c | Liran Schour | return 0; |
520 | c163b5ca | lirans@il.ibm.com | } |
521 | c163b5ca | lirans@il.ibm.com | |
522 | 539de124 | Luiz Capitulino | static void blk_mig_cleanup(void) |
523 | 4ec7fcc7 | Jan Kiszka | { |
524 | 82801d8f | Jan Kiszka | BlkMigDevState *bmds; |
525 | 82801d8f | Jan Kiszka | BlkMigBlock *blk; |
526 | 4ec7fcc7 | Jan Kiszka | |
527 | 946d58be | Kevin Wolf | bdrv_drain_all(); |
528 | 946d58be | Kevin Wolf | |
529 | 8f794c55 | Marcelo Tosatti | set_dirty_tracking(0);
|
530 | 8f794c55 | Marcelo Tosatti | |
531 | 82801d8f | Jan Kiszka | while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) { |
532 | 82801d8f | Jan Kiszka | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry); |
533 | 8591675f | Marcelo Tosatti | bdrv_set_in_use(bmds->bs, 0);
|
534 | f48905d4 | Marcelo Tosatti | drive_put_ref(drive_get_by_blockdev(bmds->bs)); |
535 | 7267c094 | Anthony Liguori | g_free(bmds->aio_bitmap); |
536 | 7267c094 | Anthony Liguori | g_free(bmds); |
537 | 4ec7fcc7 | Jan Kiszka | } |
538 | 4ec7fcc7 | Jan Kiszka | |
539 | 82801d8f | Jan Kiszka | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
540 | 82801d8f | Jan Kiszka | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); |
541 | 7267c094 | Anthony Liguori | g_free(blk->buf); |
542 | 7267c094 | Anthony Liguori | g_free(blk); |
543 | 4ec7fcc7 | Jan Kiszka | } |
544 | 4ec7fcc7 | Jan Kiszka | } |
545 | 4ec7fcc7 | Jan Kiszka | |
546 | 9b5bfab0 | Juan Quintela | static void block_migration_cancel(void *opaque) |
547 | 9b5bfab0 | Juan Quintela | { |
548 | 9b5bfab0 | Juan Quintela | blk_mig_cleanup(); |
549 | 9b5bfab0 | Juan Quintela | } |
550 | 9b5bfab0 | Juan Quintela | |
551 | d1315aac | Juan Quintela | static int block_save_setup(QEMUFile *f, void *opaque) |
552 | c163b5ca | lirans@il.ibm.com | { |
553 | 2975725f | Juan Quintela | int ret;
|
554 | 2975725f | Juan Quintela | |
555 | d1315aac | Juan Quintela | DPRINTF("Enter save live setup submitted %d transferred %d\n",
|
556 | d1315aac | Juan Quintela | block_mig_state.submitted, block_mig_state.transferred); |
557 | a55eb92c | Jan Kiszka | |
558 | d1315aac | Juan Quintela | init_blk_migration(f); |
559 | d1315aac | Juan Quintela | |
560 | d1315aac | Juan Quintela | /* start track dirty blocks */
|
561 | d1315aac | Juan Quintela | set_dirty_tracking(1);
|
562 | d1315aac | Juan Quintela | |
563 | 59feec42 | Juan Quintela | ret = flush_blks(f); |
564 | d1315aac | Juan Quintela | if (ret) {
|
565 | d1315aac | Juan Quintela | blk_mig_cleanup(); |
566 | d1315aac | Juan Quintela | return ret;
|
567 | c163b5ca | lirans@il.ibm.com | } |
568 | c163b5ca | lirans@il.ibm.com | |
569 | d1315aac | Juan Quintela | blk_mig_reset_dirty_cursor(); |
570 | d1315aac | Juan Quintela | |
571 | d1315aac | Juan Quintela | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
572 | d1315aac | Juan Quintela | |
573 | d1315aac | Juan Quintela | return 0; |
574 | d1315aac | Juan Quintela | } |
575 | d1315aac | Juan Quintela | |
576 | 16310a3c | Juan Quintela | static int block_save_iterate(QEMUFile *f, void *opaque) |
577 | d1315aac | Juan Quintela | { |
578 | d1315aac | Juan Quintela | int ret;
|
579 | d1315aac | Juan Quintela | |
580 | 16310a3c | Juan Quintela | DPRINTF("Enter save live iterate submitted %d transferred %d\n",
|
581 | 16310a3c | Juan Quintela | block_mig_state.submitted, block_mig_state.transferred); |
582 | d1315aac | Juan Quintela | |
583 | 59feec42 | Juan Quintela | ret = flush_blks(f); |
584 | 2975725f | Juan Quintela | if (ret) {
|
585 | 539de124 | Luiz Capitulino | blk_mig_cleanup(); |
586 | 2975725f | Juan Quintela | return ret;
|
587 | 4b640365 | Jan Kiszka | } |
588 | 4b640365 | Jan Kiszka | |
589 | d76cac7d | Liran Schour | blk_mig_reset_dirty_cursor(); |
590 | d76cac7d | Liran Schour | |
591 | 16310a3c | Juan Quintela | /* control the rate of transfer */
|
592 | 16310a3c | Juan Quintela | while ((block_mig_state.submitted +
|
593 | 16310a3c | Juan Quintela | block_mig_state.read_done) * BLOCK_SIZE < |
594 | 16310a3c | Juan Quintela | qemu_file_get_rate_limit(f)) { |
595 | 16310a3c | Juan Quintela | if (block_mig_state.bulk_completed == 0) { |
596 | 16310a3c | Juan Quintela | /* first finish the bulk phase */
|
597 | 16310a3c | Juan Quintela | if (blk_mig_save_bulked_block(f) == 0) { |
598 | 16310a3c | Juan Quintela | /* finished saving bulk on all devices */
|
599 | 16310a3c | Juan Quintela | block_mig_state.bulk_completed = 1;
|
600 | 16310a3c | Juan Quintela | } |
601 | 16310a3c | Juan Quintela | } else {
|
602 | 43be3a25 | Juan Quintela | ret = blk_mig_save_dirty_block(f, 1);
|
603 | 43be3a25 | Juan Quintela | if (ret != 0) { |
604 | 16310a3c | Juan Quintela | /* no more dirty blocks */
|
605 | 16310a3c | Juan Quintela | break;
|
606 | d76cac7d | Liran Schour | } |
607 | a55eb92c | Jan Kiszka | } |
608 | 16310a3c | Juan Quintela | } |
609 | 43be3a25 | Juan Quintela | if (ret) {
|
610 | 43be3a25 | Juan Quintela | blk_mig_cleanup(); |
611 | 43be3a25 | Juan Quintela | return ret;
|
612 | 43be3a25 | Juan Quintela | } |
613 | a55eb92c | Jan Kiszka | |
614 | 59feec42 | Juan Quintela | ret = flush_blks(f); |
615 | 16310a3c | Juan Quintela | if (ret) {
|
616 | 16310a3c | Juan Quintela | blk_mig_cleanup(); |
617 | 16310a3c | Juan Quintela | return ret;
|
618 | 4b640365 | Jan Kiszka | } |
619 | 4b640365 | Jan Kiszka | |
620 | 16310a3c | Juan Quintela | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
621 | 16310a3c | Juan Quintela | |
622 | 16310a3c | Juan Quintela | return is_stage2_completed();
|
623 | 16310a3c | Juan Quintela | } |
624 | 16310a3c | Juan Quintela | |
625 | 16310a3c | Juan Quintela | static int block_save_complete(QEMUFile *f, void *opaque) |
626 | 16310a3c | Juan Quintela | { |
627 | 16310a3c | Juan Quintela | int ret;
|
628 | 16310a3c | Juan Quintela | |
629 | 16310a3c | Juan Quintela | DPRINTF("Enter save live complete submitted %d transferred %d\n",
|
630 | 16310a3c | Juan Quintela | block_mig_state.submitted, block_mig_state.transferred); |
631 | 16310a3c | Juan Quintela | |
632 | 59feec42 | Juan Quintela | ret = flush_blks(f); |
633 | 16310a3c | Juan Quintela | if (ret) {
|
634 | 539de124 | Luiz Capitulino | blk_mig_cleanup(); |
635 | 16310a3c | Juan Quintela | return ret;
|
636 | 16310a3c | Juan Quintela | } |
637 | a55eb92c | Jan Kiszka | |
638 | 16310a3c | Juan Quintela | blk_mig_reset_dirty_cursor(); |
639 | 01e61e2d | Jan Kiszka | |
640 | 16310a3c | Juan Quintela | /* we know for sure that save bulk is completed and
|
641 | 16310a3c | Juan Quintela | all async read completed */
|
642 | 16310a3c | Juan Quintela | assert(block_mig_state.submitted == 0);
|
643 | 16310a3c | Juan Quintela | |
644 | 43be3a25 | Juan Quintela | do {
|
645 | 43be3a25 | Juan Quintela | ret = blk_mig_save_dirty_block(f, 0);
|
646 | 43be3a25 | Juan Quintela | } while (ret == 0); |
647 | 4b640365 | Jan Kiszka | |
648 | 43be3a25 | Juan Quintela | blk_mig_cleanup(); |
649 | 16310a3c | Juan Quintela | if (ret) {
|
650 | 16310a3c | Juan Quintela | return ret;
|
651 | c163b5ca | lirans@il.ibm.com | } |
652 | 43be3a25 | Juan Quintela | /* report completion */
|
653 | 43be3a25 | Juan Quintela | qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
|
654 | a55eb92c | Jan Kiszka | |
655 | 16310a3c | Juan Quintela | DPRINTF("Block migration completed\n");
|
656 | 16310a3c | Juan Quintela | |
657 | a55eb92c | Jan Kiszka | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
658 | a55eb92c | Jan Kiszka | |
659 | 16310a3c | Juan Quintela | return 0; |
660 | c163b5ca | lirans@il.ibm.com | } |
661 | c163b5ca | lirans@il.ibm.com | |
662 | c163b5ca | lirans@il.ibm.com | static int block_load(QEMUFile *f, void *opaque, int version_id) |
663 | c163b5ca | lirans@il.ibm.com | { |
664 | 01e61e2d | Jan Kiszka | static int banner_printed; |
665 | c163b5ca | lirans@il.ibm.com | int len, flags;
|
666 | c163b5ca | lirans@il.ibm.com | char device_name[256]; |
667 | c163b5ca | lirans@il.ibm.com | int64_t addr; |
668 | 77358b59 | Pierre Riteau | BlockDriverState *bs, *bs_prev = NULL;
|
669 | c163b5ca | lirans@il.ibm.com | uint8_t *buf; |
670 | 77358b59 | Pierre Riteau | int64_t total_sectors = 0;
|
671 | 77358b59 | Pierre Riteau | int nr_sectors;
|
672 | 42802d47 | Juan Quintela | int ret;
|
673 | a55eb92c | Jan Kiszka | |
674 | c163b5ca | lirans@il.ibm.com | do {
|
675 | c163b5ca | lirans@il.ibm.com | addr = qemu_get_be64(f); |
676 | a55eb92c | Jan Kiszka | |
677 | 6ea44308 | Jan Kiszka | flags = addr & ~BDRV_SECTOR_MASK; |
678 | 6ea44308 | Jan Kiszka | addr >>= BDRV_SECTOR_BITS; |
679 | a55eb92c | Jan Kiszka | |
680 | a55eb92c | Jan Kiszka | if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
|
681 | c163b5ca | lirans@il.ibm.com | /* get device name */
|
682 | c163b5ca | lirans@il.ibm.com | len = qemu_get_byte(f); |
683 | c163b5ca | lirans@il.ibm.com | qemu_get_buffer(f, (uint8_t *)device_name, len); |
684 | c163b5ca | lirans@il.ibm.com | device_name[len] = '\0';
|
685 | a55eb92c | Jan Kiszka | |
686 | c163b5ca | lirans@il.ibm.com | bs = bdrv_find(device_name); |
687 | 4b640365 | Jan Kiszka | if (!bs) {
|
688 | 4b640365 | Jan Kiszka | fprintf(stderr, "Error unknown block device %s\n",
|
689 | 4b640365 | Jan Kiszka | device_name); |
690 | 4b640365 | Jan Kiszka | return -EINVAL;
|
691 | 4b640365 | Jan Kiszka | } |
692 | a55eb92c | Jan Kiszka | |
693 | 77358b59 | Pierre Riteau | if (bs != bs_prev) {
|
694 | 77358b59 | Pierre Riteau | bs_prev = bs; |
695 | 77358b59 | Pierre Riteau | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
696 | 77358b59 | Pierre Riteau | if (total_sectors <= 0) { |
697 | 6daf194d | Markus Armbruster | error_report("Error getting length of block device %s",
|
698 | 77358b59 | Pierre Riteau | device_name); |
699 | 77358b59 | Pierre Riteau | return -EINVAL;
|
700 | 77358b59 | Pierre Riteau | } |
701 | 77358b59 | Pierre Riteau | } |
702 | 77358b59 | Pierre Riteau | |
703 | 77358b59 | Pierre Riteau | if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
|
704 | 77358b59 | Pierre Riteau | nr_sectors = total_sectors - addr; |
705 | 77358b59 | Pierre Riteau | } else {
|
706 | 77358b59 | Pierre Riteau | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; |
707 | 77358b59 | Pierre Riteau | } |
708 | 77358b59 | Pierre Riteau | |
709 | 7267c094 | Anthony Liguori | buf = g_malloc(BLOCK_SIZE); |
710 | 575a58d7 | Jan Kiszka | |
711 | a55eb92c | Jan Kiszka | qemu_get_buffer(f, buf, BLOCK_SIZE); |
712 | 77358b59 | Pierre Riteau | ret = bdrv_write(bs, addr, buf, nr_sectors); |
713 | 575a58d7 | Jan Kiszka | |
714 | 7267c094 | Anthony Liguori | g_free(buf); |
715 | b02bea3a | Yoshiaki Tamura | if (ret < 0) { |
716 | b02bea3a | Yoshiaki Tamura | return ret;
|
717 | b02bea3a | Yoshiaki Tamura | } |
718 | 01e61e2d | Jan Kiszka | } else if (flags & BLK_MIG_FLAG_PROGRESS) { |
719 | 01e61e2d | Jan Kiszka | if (!banner_printed) {
|
720 | 01e61e2d | Jan Kiszka | printf("Receiving block device images\n");
|
721 | 01e61e2d | Jan Kiszka | banner_printed = 1;
|
722 | 01e61e2d | Jan Kiszka | } |
723 | 01e61e2d | Jan Kiszka | printf("Completed %d %%%c", (int)addr, |
724 | 01e61e2d | Jan Kiszka | (addr == 100) ? '\n' : '\r'); |
725 | 01e61e2d | Jan Kiszka | fflush(stdout); |
726 | a55eb92c | Jan Kiszka | } else if (!(flags & BLK_MIG_FLAG_EOS)) { |
727 | 4b640365 | Jan Kiszka | fprintf(stderr, "Unknown flags\n");
|
728 | 4b640365 | Jan Kiszka | return -EINVAL;
|
729 | 4b640365 | Jan Kiszka | } |
730 | 42802d47 | Juan Quintela | ret = qemu_file_get_error(f); |
731 | 42802d47 | Juan Quintela | if (ret != 0) { |
732 | 42802d47 | Juan Quintela | return ret;
|
733 | c163b5ca | lirans@il.ibm.com | } |
734 | a55eb92c | Jan Kiszka | } while (!(flags & BLK_MIG_FLAG_EOS));
|
735 | a55eb92c | Jan Kiszka | |
736 | c163b5ca | lirans@il.ibm.com | return 0; |
737 | c163b5ca | lirans@il.ibm.com | } |
738 | c163b5ca | lirans@il.ibm.com | |
739 | 6607ae23 | Isaku Yamahata | static void block_set_params(const MigrationParams *params, void *opaque) |
740 | c163b5ca | lirans@il.ibm.com | { |
741 | 6607ae23 | Isaku Yamahata | block_mig_state.blk_enable = params->blk; |
742 | 6607ae23 | Isaku Yamahata | block_mig_state.shared_base = params->shared; |
743 | a55eb92c | Jan Kiszka | |
744 | c163b5ca | lirans@il.ibm.com | /* shared base means that blk_enable = 1 */
|
745 | 6607ae23 | Isaku Yamahata | block_mig_state.blk_enable |= params->shared; |
746 | c163b5ca | lirans@il.ibm.com | } |
747 | c163b5ca | lirans@il.ibm.com | |
748 | 6bd68781 | Juan Quintela | static bool block_is_active(void *opaque) |
749 | 6bd68781 | Juan Quintela | { |
750 | 6bd68781 | Juan Quintela | return block_mig_state.blk_enable == 1; |
751 | 6bd68781 | Juan Quintela | } |
752 | 6bd68781 | Juan Quintela | |
753 | 7908c78d | Juan Quintela | SaveVMHandlers savevm_block_handlers = { |
754 | 7908c78d | Juan Quintela | .set_params = block_set_params, |
755 | d1315aac | Juan Quintela | .save_live_setup = block_save_setup, |
756 | 16310a3c | Juan Quintela | .save_live_iterate = block_save_iterate, |
757 | 16310a3c | Juan Quintela | .save_live_complete = block_save_complete, |
758 | 7908c78d | Juan Quintela | .load_state = block_load, |
759 | 9b5bfab0 | Juan Quintela | .cancel = block_migration_cancel, |
760 | 6bd68781 | Juan Quintela | .is_active = block_is_active, |
761 | 7908c78d | Juan Quintela | }; |
762 | 7908c78d | Juan Quintela | |
763 | c163b5ca | lirans@il.ibm.com | void blk_mig_init(void) |
764 | a55eb92c | Jan Kiszka | { |
765 | 5e5328be | Jan Kiszka | QSIMPLEQ_INIT(&block_mig_state.bmds_list); |
766 | 5e5328be | Jan Kiszka | QSIMPLEQ_INIT(&block_mig_state.blk_list); |
767 | 5e5328be | Jan Kiszka | |
768 | 7908c78d | Juan Quintela | register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers, |
769 | 7908c78d | Juan Quintela | &block_mig_state); |
770 | c163b5ca | lirans@il.ibm.com | } |