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