]> git.proxmox.com Git - qemu.git/blob - migration.c
e3362b76d8e55efebf124faccee8849195af1c35
[qemu.git] / migration.c
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 int64_t max_throttle = (32 << 20);
36
37 static FdMigrationState *current_migration;
38
39 static NotifierList migration_state_notifiers =
40 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
41
42 int qemu_start_incoming_migration(const char *uri)
43 {
44 const char *p;
45 int ret;
46
47 if (strstart(uri, "tcp:", &p))
48 ret = tcp_start_incoming_migration(p);
49 #if !defined(WIN32)
50 else if (strstart(uri, "exec:", &p))
51 ret = exec_start_incoming_migration(p);
52 else if (strstart(uri, "unix:", &p))
53 ret = unix_start_incoming_migration(p);
54 else if (strstart(uri, "fd:", &p))
55 ret = fd_start_incoming_migration(p);
56 #endif
57 else {
58 fprintf(stderr, "unknown migration protocol: %s\n", uri);
59 ret = -EPROTONOSUPPORT;
60 }
61 return ret;
62 }
63
64 void process_incoming_migration(QEMUFile *f)
65 {
66 if (qemu_loadvm_state(f) < 0) {
67 fprintf(stderr, "load of migration failed\n");
68 exit(0);
69 }
70 qemu_announce_self();
71 DPRINTF("successfully loaded vm state\n");
72
73 if (autostart) {
74 vm_start();
75 } else {
76 runstate_set(RUN_STATE_PRELAUNCH);
77 }
78 }
79
80 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
81 {
82 FdMigrationState *s = NULL;
83 const char *p;
84 int detach = qdict_get_try_bool(qdict, "detach", 0);
85 int blk = qdict_get_try_bool(qdict, "blk", 0);
86 int inc = qdict_get_try_bool(qdict, "inc", 0);
87 const char *uri = qdict_get_str(qdict, "uri");
88
89 if (current_migration &&
90 current_migration->mig_state.get_status(current_migration) ==
91 MIG_STATE_ACTIVE) {
92 monitor_printf(mon, "migration already in progress\n");
93 return -1;
94 }
95
96 if (qemu_savevm_state_blocked(mon)) {
97 return -1;
98 }
99
100 if (strstart(uri, "tcp:", &p)) {
101 s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
102 blk, inc);
103 #if !defined(WIN32)
104 } else if (strstart(uri, "exec:", &p)) {
105 s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
106 blk, inc);
107 } else if (strstart(uri, "unix:", &p)) {
108 s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
109 blk, inc);
110 } else if (strstart(uri, "fd:", &p)) {
111 s = fd_start_outgoing_migration(mon, p, max_throttle, detach,
112 blk, inc);
113 #endif
114 } else {
115 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
116 return -1;
117 }
118
119 if (s == NULL) {
120 monitor_printf(mon, "migration failed\n");
121 return -1;
122 }
123
124 if (current_migration) {
125 current_migration->mig_state.release(current_migration);
126 }
127
128 current_migration = s;
129 notifier_list_notify(&migration_state_notifiers, NULL);
130 return 0;
131 }
132
133 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
134 {
135 FdMigrationState *s = current_migration;
136
137 if (s && s->mig_state.get_status(s) == MIG_STATE_ACTIVE) {
138 s->mig_state.cancel(s);
139 }
140 return 0;
141 }
142
143 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
144 {
145 int64_t d;
146 FdMigrationState *s;
147
148 d = qdict_get_int(qdict, "value");
149 if (d < 0) {
150 d = 0;
151 }
152 max_throttle = d;
153
154 s = current_migration;
155 if (s && s->file) {
156 qemu_file_set_rate_limit(s->file, max_throttle);
157 }
158
159 return 0;
160 }
161
162 /* amount of nanoseconds we are willing to wait for migration to be down.
163 * the choice of nanoseconds is because it is the maximum resolution that
164 * get_clock() can achieve. It is an internal measure. All user-visible
165 * units must be in seconds */
166 static uint64_t max_downtime = 30000000;
167
168 uint64_t migrate_max_downtime(void)
169 {
170 return max_downtime;
171 }
172
173 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
174 QObject **ret_data)
175 {
176 double d;
177
178 d = qdict_get_double(qdict, "value") * 1e9;
179 d = MAX(0, MIN(UINT64_MAX, d));
180 max_downtime = (uint64_t)d;
181
182 return 0;
183 }
184
185 static void migrate_print_status(Monitor *mon, const char *name,
186 const QDict *status_dict)
187 {
188 QDict *qdict;
189
190 qdict = qobject_to_qdict(qdict_get(status_dict, name));
191
192 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
193 qdict_get_int(qdict, "transferred") >> 10);
194 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
195 qdict_get_int(qdict, "remaining") >> 10);
196 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
197 qdict_get_int(qdict, "total") >> 10);
198 }
199
200 void do_info_migrate_print(Monitor *mon, const QObject *data)
201 {
202 QDict *qdict;
203
204 qdict = qobject_to_qdict(data);
205
206 monitor_printf(mon, "Migration status: %s\n",
207 qdict_get_str(qdict, "status"));
208
209 if (qdict_haskey(qdict, "ram")) {
210 migrate_print_status(mon, "ram", qdict);
211 }
212
213 if (qdict_haskey(qdict, "disk")) {
214 migrate_print_status(mon, "disk", qdict);
215 }
216 }
217
218 static void migrate_put_status(QDict *qdict, const char *name,
219 uint64_t trans, uint64_t rem, uint64_t total)
220 {
221 QObject *obj;
222
223 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
224 "'remaining': %" PRId64 ", "
225 "'total': %" PRId64 " }", trans, rem, total);
226 qdict_put_obj(qdict, name, obj);
227 }
228
229 void do_info_migrate(Monitor *mon, QObject **ret_data)
230 {
231 QDict *qdict;
232
233 if (current_migration) {
234 MigrationState *s = &current_migration->mig_state;
235
236 switch (s->get_status(current_migration)) {
237 case MIG_STATE_ACTIVE:
238 qdict = qdict_new();
239 qdict_put(qdict, "status", qstring_from_str("active"));
240
241 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
242 ram_bytes_remaining(), ram_bytes_total());
243
244 if (blk_mig_active()) {
245 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
246 blk_mig_bytes_remaining(),
247 blk_mig_bytes_total());
248 }
249
250 *ret_data = QOBJECT(qdict);
251 break;
252 case MIG_STATE_COMPLETED:
253 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
254 break;
255 case MIG_STATE_ERROR:
256 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
257 break;
258 case MIG_STATE_CANCELLED:
259 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
260 break;
261 }
262 }
263 }
264
265 /* shared migration helpers */
266
267 void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
268 {
269 s->mon = mon;
270 if (monitor_suspend(mon) == 0) {
271 DPRINTF("suspending monitor\n");
272 } else {
273 monitor_printf(mon, "terminal does not allow synchronous "
274 "migration, continuing detached\n");
275 }
276 }
277
278 void migrate_fd_error(FdMigrationState *s)
279 {
280 DPRINTF("setting error state\n");
281 s->state = MIG_STATE_ERROR;
282 notifier_list_notify(&migration_state_notifiers, NULL);
283 migrate_fd_cleanup(s);
284 }
285
286 int migrate_fd_cleanup(FdMigrationState *s)
287 {
288 int ret = 0;
289
290 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
291
292 if (s->file) {
293 DPRINTF("closing file\n");
294 if (qemu_fclose(s->file) != 0) {
295 ret = -1;
296 }
297 s->file = NULL;
298 } else {
299 if (s->mon) {
300 monitor_resume(s->mon);
301 }
302 }
303
304 if (s->fd != -1) {
305 close(s->fd);
306 s->fd = -1;
307 }
308
309 return ret;
310 }
311
312 void migrate_fd_put_notify(void *opaque)
313 {
314 FdMigrationState *s = opaque;
315
316 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
317 qemu_file_put_notify(s->file);
318 if (qemu_file_get_error(s->file)) {
319 migrate_fd_error(s);
320 }
321 }
322
323 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
324 {
325 FdMigrationState *s = opaque;
326 ssize_t ret;
327
328 if (s->state != MIG_STATE_ACTIVE) {
329 return -EIO;
330 }
331
332 do {
333 ret = s->write(s, data, size);
334 } while (ret == -1 && ((s->get_error(s)) == EINTR));
335
336 if (ret == -1)
337 ret = -(s->get_error(s));
338
339 if (ret == -EAGAIN) {
340 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
341 }
342
343 return ret;
344 }
345
346 void migrate_fd_connect(FdMigrationState *s)
347 {
348 int ret;
349
350 s->file = qemu_fopen_ops_buffered(s,
351 s->bandwidth_limit,
352 migrate_fd_put_buffer,
353 migrate_fd_put_ready,
354 migrate_fd_wait_for_unfreeze,
355 migrate_fd_close);
356
357 DPRINTF("beginning savevm\n");
358 ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
359 s->mig_state.shared);
360 if (ret < 0) {
361 DPRINTF("failed, %d\n", ret);
362 migrate_fd_error(s);
363 return;
364 }
365
366 migrate_fd_put_ready(s);
367 }
368
369 void migrate_fd_put_ready(void *opaque)
370 {
371 FdMigrationState *s = opaque;
372 int ret;
373
374 if (s->state != MIG_STATE_ACTIVE) {
375 DPRINTF("put_ready returning because of non-active state\n");
376 return;
377 }
378
379 DPRINTF("iterate\n");
380 ret = qemu_savevm_state_iterate(s->mon, s->file);
381 if (ret < 0) {
382 migrate_fd_error(s);
383 } else if (ret == 1) {
384 int old_vm_running = runstate_is_running();
385
386 DPRINTF("done iterating\n");
387 vm_stop(RUN_STATE_FINISH_MIGRATE);
388
389 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
390 if (old_vm_running) {
391 vm_start();
392 }
393 s->state = MIG_STATE_ERROR;
394 }
395 if (migrate_fd_cleanup(s) < 0) {
396 if (old_vm_running) {
397 vm_start();
398 }
399 s->state = MIG_STATE_ERROR;
400 }
401 if (s->state == MIG_STATE_ACTIVE) {
402 s->state = MIG_STATE_COMPLETED;
403 runstate_set(RUN_STATE_POSTMIGRATE);
404 }
405 notifier_list_notify(&migration_state_notifiers, NULL);
406 }
407 }
408
409 int migrate_fd_get_status(FdMigrationState *s)
410 {
411 return s->state;
412 }
413
414 void migrate_fd_cancel(FdMigrationState *s)
415 {
416 if (s->state != MIG_STATE_ACTIVE)
417 return;
418
419 DPRINTF("cancelling migration\n");
420
421 s->state = MIG_STATE_CANCELLED;
422 notifier_list_notify(&migration_state_notifiers, NULL);
423 qemu_savevm_state_cancel(s->mon, s->file);
424
425 migrate_fd_cleanup(s);
426 }
427
428 void migrate_fd_release(FdMigrationState *s)
429 {
430
431 DPRINTF("releasing state\n");
432
433 if (s->state == MIG_STATE_ACTIVE) {
434 s->state = MIG_STATE_CANCELLED;
435 notifier_list_notify(&migration_state_notifiers, NULL);
436 migrate_fd_cleanup(s);
437 }
438 g_free(s);
439 }
440
441 void migrate_fd_wait_for_unfreeze(void *opaque)
442 {
443 FdMigrationState *s = opaque;
444 int ret;
445
446 DPRINTF("wait for unfreeze\n");
447 if (s->state != MIG_STATE_ACTIVE)
448 return;
449
450 do {
451 fd_set wfds;
452
453 FD_ZERO(&wfds);
454 FD_SET(s->fd, &wfds);
455
456 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
457 } while (ret == -1 && (s->get_error(s)) == EINTR);
458
459 if (ret == -1) {
460 qemu_file_set_error(s->file, -s->get_error(s));
461 }
462 }
463
464 int migrate_fd_close(void *opaque)
465 {
466 FdMigrationState *s = opaque;
467
468 if (s->mon) {
469 monitor_resume(s->mon);
470 }
471 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
472 return s->close(s);
473 }
474
475 void add_migration_state_change_notifier(Notifier *notify)
476 {
477 notifier_list_add(&migration_state_notifiers, notify);
478 }
479
480 void remove_migration_state_change_notifier(Notifier *notify)
481 {
482 notifier_list_remove(&migration_state_notifiers, notify);
483 }
484
485 int get_migration_state(void)
486 {
487 if (current_migration) {
488 return migrate_fd_get_status(current_migration);
489 } else {
490 return MIG_STATE_ERROR;
491 }
492 }