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