root / migration.c @ b0fbf7d3
History | View | Annotate | Download (12 kB)
1 |
/*
|
---|---|
2 |
* QEMU live migration
|
3 |
*
|
4 |
* Copyright IBM, Corp. 2008
|
5 |
*
|
6 |
* Authors:
|
7 |
* Anthony Liguori <aliguori@us.ibm.com>
|
8 |
*
|
9 |
* This work is licensed under the terms of the GNU GPL, version 2. See
|
10 |
* the COPYING file in the top-level directory.
|
11 |
*
|
12 |
*/
|
13 |
|
14 |
#include "qemu-common.h" |
15 |
#include "migration.h" |
16 |
#include "monitor.h" |
17 |
#include "buffered_file.h" |
18 |
#include "sysemu.h" |
19 |
#include "block.h" |
20 |
#include "qemu_socket.h" |
21 |
#include "block-migration.h" |
22 |
#include "qemu-objects.h" |
23 |
|
24 |
//#define DEBUG_MIGRATION
|
25 |
|
26 |
#ifdef DEBUG_MIGRATION
|
27 |
#define dprintf(fmt, ...) \
|
28 |
do { printf("migration: " fmt, ## __VA_ARGS__); } while (0) |
29 |
#else
|
30 |
#define dprintf(fmt, ...) \
|
31 |
do { } while (0) |
32 |
#endif
|
33 |
|
34 |
/* Migration speed throttling */
|
35 |
static uint32_t max_throttle = (32 << 20); |
36 |
|
37 |
static MigrationState *current_migration;
|
38 |
|
39 |
void qemu_start_incoming_migration(const char *uri) |
40 |
{ |
41 |
const char *p; |
42 |
|
43 |
if (strstart(uri, "tcp:", &p)) |
44 |
tcp_start_incoming_migration(p); |
45 |
#if !defined(WIN32)
|
46 |
else if (strstart(uri, "exec:", &p)) |
47 |
exec_start_incoming_migration(p); |
48 |
else if (strstart(uri, "unix:", &p)) |
49 |
unix_start_incoming_migration(p); |
50 |
else if (strstart(uri, "fd:", &p)) |
51 |
fd_start_incoming_migration(p); |
52 |
#endif
|
53 |
else
|
54 |
fprintf(stderr, "unknown migration protocol: %s\n", uri);
|
55 |
} |
56 |
|
57 |
void do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data) |
58 |
{ |
59 |
MigrationState *s = NULL;
|
60 |
const char *p; |
61 |
int detach = qdict_get_int(qdict, "detach"); |
62 |
const char *uri = qdict_get_str(qdict, "uri"); |
63 |
|
64 |
if (current_migration &&
|
65 |
current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) { |
66 |
monitor_printf(mon, "migration already in progress\n");
|
67 |
return;
|
68 |
} |
69 |
|
70 |
if (strstart(uri, "tcp:", &p)) |
71 |
s = tcp_start_outgoing_migration(mon, p, max_throttle, detach, |
72 |
(int)qdict_get_int(qdict, "blk"), |
73 |
(int)qdict_get_int(qdict, "inc")); |
74 |
#if !defined(WIN32)
|
75 |
else if (strstart(uri, "exec:", &p)) |
76 |
s = exec_start_outgoing_migration(mon, p, max_throttle, detach, |
77 |
(int)qdict_get_int(qdict, "blk"), |
78 |
(int)qdict_get_int(qdict, "inc")); |
79 |
else if (strstart(uri, "unix:", &p)) |
80 |
s = unix_start_outgoing_migration(mon, p, max_throttle, detach, |
81 |
(int)qdict_get_int(qdict, "blk"), |
82 |
(int)qdict_get_int(qdict, "inc")); |
83 |
else if (strstart(uri, "fd:", &p)) |
84 |
s = fd_start_outgoing_migration(mon, p, max_throttle, detach, |
85 |
(int)qdict_get_int(qdict, "blk"), |
86 |
(int)qdict_get_int(qdict, "inc")); |
87 |
#endif
|
88 |
else
|
89 |
monitor_printf(mon, "unknown migration protocol: %s\n", uri);
|
90 |
|
91 |
if (s == NULL) |
92 |
monitor_printf(mon, "migration failed\n");
|
93 |
else {
|
94 |
if (current_migration)
|
95 |
current_migration->release(current_migration); |
96 |
|
97 |
current_migration = s; |
98 |
} |
99 |
} |
100 |
|
101 |
void do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data) |
102 |
{ |
103 |
MigrationState *s = current_migration; |
104 |
|
105 |
if (s)
|
106 |
s->cancel(s); |
107 |
} |
108 |
|
109 |
void do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data) |
110 |
{ |
111 |
double d;
|
112 |
FdMigrationState *s; |
113 |
|
114 |
d = qdict_get_double(qdict, "value");
|
115 |
d = MAX(0, MIN(UINT32_MAX, d));
|
116 |
max_throttle = d; |
117 |
|
118 |
s = migrate_to_fms(current_migration); |
119 |
if (s && s->file) {
|
120 |
qemu_file_set_rate_limit(s->file, max_throttle); |
121 |
} |
122 |
} |
123 |
|
124 |
/* amount of nanoseconds we are willing to wait for migration to be down.
|
125 |
* the choice of nanoseconds is because it is the maximum resolution that
|
126 |
* get_clock() can achieve. It is an internal measure. All user-visible
|
127 |
* units must be in seconds */
|
128 |
static uint64_t max_downtime = 30000000; |
129 |
|
130 |
uint64_t migrate_max_downtime(void)
|
131 |
{ |
132 |
return max_downtime;
|
133 |
} |
134 |
|
135 |
void do_migrate_set_downtime(Monitor *mon, const QDict *qdict) |
136 |
{ |
137 |
double d;
|
138 |
|
139 |
d = qdict_get_double(qdict, "value") * 1e9; |
140 |
d = MAX(0, MIN(UINT64_MAX, d));
|
141 |
max_downtime = (uint64_t)d; |
142 |
} |
143 |
|
144 |
static void migrate_print_status(Monitor *mon, const char *name, |
145 |
const QDict *status_dict)
|
146 |
{ |
147 |
QDict *qdict; |
148 |
|
149 |
qdict = qobject_to_qdict(qdict_get(status_dict, name)); |
150 |
|
151 |
monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name, |
152 |
qdict_get_int(qdict, "transferred") >> 10); |
153 |
monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name, |
154 |
qdict_get_int(qdict, "remaining") >> 10); |
155 |
monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name, |
156 |
qdict_get_int(qdict, "total") >> 10); |
157 |
} |
158 |
|
159 |
void do_info_migrate_print(Monitor *mon, const QObject *data) |
160 |
{ |
161 |
QDict *qdict; |
162 |
|
163 |
qdict = qobject_to_qdict(data); |
164 |
|
165 |
monitor_printf(mon, "Migration status: %s\n",
|
166 |
qdict_get_str(qdict, "status"));
|
167 |
|
168 |
if (qdict_haskey(qdict, "ram")) { |
169 |
migrate_print_status(mon, "ram", qdict);
|
170 |
} |
171 |
|
172 |
if (qdict_haskey(qdict, "disk")) { |
173 |
migrate_print_status(mon, "disk", qdict);
|
174 |
} |
175 |
} |
176 |
|
177 |
static void migrate_put_status(QDict *qdict, const char *name, |
178 |
uint64_t trans, uint64_t rem, uint64_t total) |
179 |
{ |
180 |
QObject *obj; |
181 |
|
182 |
obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", " |
183 |
"'remaining': %" PRId64 ", " |
184 |
"'total': %" PRId64 " }", trans, rem, total); |
185 |
assert(obj != NULL);
|
186 |
|
187 |
qdict_put_obj(qdict, name, obj); |
188 |
} |
189 |
|
190 |
/**
|
191 |
* do_info_migrate(): Migration status
|
192 |
*
|
193 |
* Return a QDict. If migration is active there will be another
|
194 |
* QDict with RAM migration status and if block migration is active
|
195 |
* another one with block migration status.
|
196 |
*
|
197 |
* The main QDict contains the following:
|
198 |
*
|
199 |
* - "status": migration status
|
200 |
* - "ram": only present if "status" is "active", it is a QDict with the
|
201 |
* following RAM information (in bytes):
|
202 |
* - "transferred": amount transferred
|
203 |
* - "remaining": amount remaining
|
204 |
* - "total": total
|
205 |
* - "disk": only present if "status" is "active" and it is a block migration,
|
206 |
* it is a QDict with the following disk information (in bytes):
|
207 |
* - "transferred": amount transferred
|
208 |
* - "remaining": amount remaining
|
209 |
* - "total": total
|
210 |
*
|
211 |
* Examples:
|
212 |
*
|
213 |
* 1. Migration is "completed":
|
214 |
*
|
215 |
* { "status": "completed" }
|
216 |
*
|
217 |
* 2. Migration is "active" and it is not a block migration:
|
218 |
*
|
219 |
* { "status": "active",
|
220 |
* "ram": { "transferred": 123, "remaining": 123, "total": 246 } }
|
221 |
*
|
222 |
* 3. Migration is "active" and it is a block migration:
|
223 |
*
|
224 |
* { "status": "active",
|
225 |
* "ram": { "total": 1057024, "remaining": 1053304, "transferred": 3720 },
|
226 |
* "disk": { "total": 20971520, "remaining": 20880384, "transferred": 91136 }}
|
227 |
*/
|
228 |
void do_info_migrate(Monitor *mon, QObject **ret_data)
|
229 |
{ |
230 |
QDict *qdict; |
231 |
MigrationState *s = current_migration; |
232 |
|
233 |
if (s) {
|
234 |
switch (s->get_status(s)) {
|
235 |
case MIG_STATE_ACTIVE:
|
236 |
qdict = qdict_new(); |
237 |
qdict_put(qdict, "status", qstring_from_str("active")); |
238 |
|
239 |
migrate_put_status(qdict, "ram", ram_bytes_transferred(),
|
240 |
ram_bytes_remaining(), ram_bytes_total()); |
241 |
|
242 |
if (blk_mig_active()) {
|
243 |
migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
|
244 |
blk_mig_bytes_remaining(), |
245 |
blk_mig_bytes_total()); |
246 |
} |
247 |
|
248 |
*ret_data = QOBJECT(qdict); |
249 |
break;
|
250 |
case MIG_STATE_COMPLETED:
|
251 |
*ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
|
252 |
break;
|
253 |
case MIG_STATE_ERROR:
|
254 |
*ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
|
255 |
break;
|
256 |
case MIG_STATE_CANCELLED:
|
257 |
*ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
|
258 |
break;
|
259 |
} |
260 |
assert(*ret_data != NULL);
|
261 |
} |
262 |
} |
263 |
|
264 |
/* shared migration helpers */
|
265 |
|
266 |
void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
|
267 |
{ |
268 |
s->mon = mon; |
269 |
if (monitor_suspend(mon) == 0) { |
270 |
dprintf("suspending monitor\n");
|
271 |
} else {
|
272 |
monitor_printf(mon, "terminal does not allow synchronous "
|
273 |
"migration, continuing detached\n");
|
274 |
} |
275 |
} |
276 |
|
277 |
void migrate_fd_error(FdMigrationState *s)
|
278 |
{ |
279 |
dprintf("setting error state\n");
|
280 |
s->state = MIG_STATE_ERROR; |
281 |
migrate_fd_cleanup(s); |
282 |
} |
283 |
|
284 |
void migrate_fd_cleanup(FdMigrationState *s)
|
285 |
{ |
286 |
qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
287 |
|
288 |
if (s->file) {
|
289 |
dprintf("closing file\n");
|
290 |
qemu_fclose(s->file); |
291 |
s->file = NULL;
|
292 |
} |
293 |
|
294 |
if (s->fd != -1) |
295 |
close(s->fd); |
296 |
|
297 |
/* Don't resume monitor until we've flushed all of the buffers */
|
298 |
if (s->mon) {
|
299 |
monitor_resume(s->mon); |
300 |
} |
301 |
|
302 |
s->fd = -1;
|
303 |
} |
304 |
|
305 |
void migrate_fd_put_notify(void *opaque) |
306 |
{ |
307 |
FdMigrationState *s = opaque; |
308 |
|
309 |
qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
310 |
qemu_file_put_notify(s->file); |
311 |
} |
312 |
|
313 |
ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size) |
314 |
{ |
315 |
FdMigrationState *s = opaque; |
316 |
ssize_t ret; |
317 |
|
318 |
do {
|
319 |
ret = s->write(s, data, size); |
320 |
} while (ret == -1 && ((s->get_error(s)) == EINTR)); |
321 |
|
322 |
if (ret == -1) |
323 |
ret = -(s->get_error(s)); |
324 |
|
325 |
if (ret == -EAGAIN)
|
326 |
qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s); |
327 |
|
328 |
return ret;
|
329 |
} |
330 |
|
331 |
void migrate_fd_connect(FdMigrationState *s)
|
332 |
{ |
333 |
int ret;
|
334 |
|
335 |
s->file = qemu_fopen_ops_buffered(s, |
336 |
s->bandwidth_limit, |
337 |
migrate_fd_put_buffer, |
338 |
migrate_fd_put_ready, |
339 |
migrate_fd_wait_for_unfreeze, |
340 |
migrate_fd_close); |
341 |
|
342 |
dprintf("beginning savevm\n");
|
343 |
ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk, |
344 |
s->mig_state.shared); |
345 |
if (ret < 0) { |
346 |
dprintf("failed, %d\n", ret);
|
347 |
migrate_fd_error(s); |
348 |
return;
|
349 |
} |
350 |
|
351 |
migrate_fd_put_ready(s); |
352 |
} |
353 |
|
354 |
void migrate_fd_put_ready(void *opaque) |
355 |
{ |
356 |
FdMigrationState *s = opaque; |
357 |
|
358 |
if (s->state != MIG_STATE_ACTIVE) {
|
359 |
dprintf("put_ready returning because of non-active state\n");
|
360 |
return;
|
361 |
} |
362 |
|
363 |
dprintf("iterate\n");
|
364 |
if (qemu_savevm_state_iterate(s->mon, s->file) == 1) { |
365 |
int state;
|
366 |
int old_vm_running = vm_running;
|
367 |
|
368 |
dprintf("done iterating\n");
|
369 |
vm_stop(0);
|
370 |
|
371 |
qemu_aio_flush(); |
372 |
bdrv_flush_all(); |
373 |
if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) { |
374 |
if (old_vm_running) {
|
375 |
vm_start(); |
376 |
} |
377 |
state = MIG_STATE_ERROR; |
378 |
} else {
|
379 |
state = MIG_STATE_COMPLETED; |
380 |
} |
381 |
migrate_fd_cleanup(s); |
382 |
s->state = state; |
383 |
} |
384 |
} |
385 |
|
386 |
int migrate_fd_get_status(MigrationState *mig_state)
|
387 |
{ |
388 |
FdMigrationState *s = migrate_to_fms(mig_state); |
389 |
return s->state;
|
390 |
} |
391 |
|
392 |
void migrate_fd_cancel(MigrationState *mig_state)
|
393 |
{ |
394 |
FdMigrationState *s = migrate_to_fms(mig_state); |
395 |
|
396 |
if (s->state != MIG_STATE_ACTIVE)
|
397 |
return;
|
398 |
|
399 |
dprintf("cancelling migration\n");
|
400 |
|
401 |
s->state = MIG_STATE_CANCELLED; |
402 |
qemu_savevm_state_cancel(s->mon, s->file); |
403 |
|
404 |
migrate_fd_cleanup(s); |
405 |
} |
406 |
|
407 |
void migrate_fd_release(MigrationState *mig_state)
|
408 |
{ |
409 |
FdMigrationState *s = migrate_to_fms(mig_state); |
410 |
|
411 |
dprintf("releasing state\n");
|
412 |
|
413 |
if (s->state == MIG_STATE_ACTIVE) {
|
414 |
s->state = MIG_STATE_CANCELLED; |
415 |
migrate_fd_cleanup(s); |
416 |
} |
417 |
free(s); |
418 |
} |
419 |
|
420 |
void migrate_fd_wait_for_unfreeze(void *opaque) |
421 |
{ |
422 |
FdMigrationState *s = opaque; |
423 |
int ret;
|
424 |
|
425 |
dprintf("wait for unfreeze\n");
|
426 |
if (s->state != MIG_STATE_ACTIVE)
|
427 |
return;
|
428 |
|
429 |
do {
|
430 |
fd_set wfds; |
431 |
|
432 |
FD_ZERO(&wfds); |
433 |
FD_SET(s->fd, &wfds); |
434 |
|
435 |
ret = select(s->fd + 1, NULL, &wfds, NULL, NULL); |
436 |
} while (ret == -1 && (s->get_error(s)) == EINTR); |
437 |
} |
438 |
|
439 |
int migrate_fd_close(void *opaque) |
440 |
{ |
441 |
FdMigrationState *s = opaque; |
442 |
|
443 |
qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
444 |
return s->close(s);
|
445 |
} |