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