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