]> git.proxmox.com Git - mirror_qemu.git/blame - migration.c
pseries: Fix qdev.id handling in the VIO bus code
[mirror_qemu.git] / migration.c
CommitLineData
5bb7910a
AL
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"
376253ec 16#include "monitor.h"
065e2813
AL
17#include "buffered_file.h"
18#include "sysemu.h"
19#include "block.h"
20#include "qemu_socket.h"
25f23643 21#include "block-migration.h"
791e7c82 22#include "qmp-commands.h"
065e2813
AL
23
24//#define DEBUG_MIGRATION
25
26#ifdef DEBUG_MIGRATION
d0f2c4c6 27#define DPRINTF(fmt, ...) \
065e2813
AL
28 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
29#else
d0f2c4c6 30#define DPRINTF(fmt, ...) \
065e2813
AL
31 do { } while (0)
32#endif
5bb7910a 33
7dc688ed
JQ
34enum {
35 MIG_STATE_ERROR,
36 MIG_STATE_SETUP,
37 MIG_STATE_CANCELLED,
38 MIG_STATE_ACTIVE,
39 MIG_STATE_COMPLETED,
40};
5bb7910a 41
d0ae46c1 42#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
5bb7910a 43
99a0db9b
GH
44static NotifierList migration_state_notifiers =
45 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
46
17549e84
JQ
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
51static MigrationState *migrate_get_current(void)
52{
53 static MigrationState current_migration = {
54 .state = MIG_STATE_SETUP,
d0ae46c1 55 .bandwidth_limit = MAX_THROTTLE,
17549e84
JQ
56 };
57
58 return &current_migration;
59}
60
8ca5e801 61int qemu_start_incoming_migration(const char *uri)
5bb7910a 62{
34c9dd8e 63 const char *p;
8ca5e801 64 int ret;
34c9dd8e
AL
65
66 if (strstart(uri, "tcp:", &p))
8ca5e801 67 ret = tcp_start_incoming_migration(p);
065e2813
AL
68#if !defined(WIN32)
69 else if (strstart(uri, "exec:", &p))
8ca5e801 70 ret = exec_start_incoming_migration(p);
4951f65b 71 else if (strstart(uri, "unix:", &p))
8ca5e801 72 ret = unix_start_incoming_migration(p);
5ac1fad3 73 else if (strstart(uri, "fd:", &p))
8ca5e801 74 ret = fd_start_incoming_migration(p);
065e2813 75#endif
8ca5e801 76 else {
34c9dd8e 77 fprintf(stderr, "unknown migration protocol: %s\n", uri);
8ca5e801
JQ
78 ret = -EPROTONOSUPPORT;
79 }
80 return ret;
5bb7910a
AL
81}
82
511c0231
JQ
83void 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
f5bbfba1 92 if (autostart) {
511c0231 93 vm_start();
f5bbfba1 94 } else {
0461d5a6 95 runstate_set(RUN_STATE_PRELAUNCH);
f5bbfba1 96 }
511c0231
JQ
97}
98
a0a3fd60
GC
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 */
103static uint64_t max_downtime = 30000000;
104
105uint64_t migrate_max_downtime(void)
106{
107 return max_downtime;
108}
109
791e7c82 110MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 111{
791e7c82 112 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
113 MigrationState *s = migrate_get_current();
114
115 switch (s->state) {
116 case MIG_STATE_SETUP:
117 /* no migration has happened ever */
118 break;
119 case MIG_STATE_ACTIVE:
791e7c82
LC
120 info->has_status = true;
121 info->status = g_strdup("active");
17549e84 122
791e7c82
LC
123 info->has_ram = true;
124 info->ram = g_malloc0(sizeof(*info->ram));
125 info->ram->transferred = ram_bytes_transferred();
126 info->ram->remaining = ram_bytes_remaining();
127 info->ram->total = ram_bytes_total();
17549e84
JQ
128
129 if (blk_mig_active()) {
791e7c82
LC
130 info->has_disk = true;
131 info->disk = g_malloc0(sizeof(*info->disk));
132 info->disk->transferred = blk_mig_bytes_transferred();
133 info->disk->remaining = blk_mig_bytes_remaining();
134 info->disk->total = blk_mig_bytes_total();
ff8d81d8 135 }
17549e84
JQ
136 break;
137 case MIG_STATE_COMPLETED:
791e7c82
LC
138 info->has_status = true;
139 info->status = g_strdup("completed");
17549e84
JQ
140 break;
141 case MIG_STATE_ERROR:
791e7c82
LC
142 info->has_status = true;
143 info->status = g_strdup("failed");
17549e84
JQ
144 break;
145 case MIG_STATE_CANCELLED:
791e7c82
LC
146 info->has_status = true;
147 info->status = g_strdup("cancelled");
17549e84 148 break;
5bb7910a 149 }
791e7c82
LC
150
151 return info;
5bb7910a
AL
152}
153
065e2813
AL
154/* shared migration helpers */
155
0edda1c4 156static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon)
731b0364 157{
f327aa0c
JK
158 s->mon = mon;
159 if (monitor_suspend(mon) == 0) {
d0f2c4c6 160 DPRINTF("suspending monitor\n");
f327aa0c
JK
161 } else {
162 monitor_printf(mon, "terminal does not allow synchronous "
cde76ee1 163 "migration, continuing detached\n");
f327aa0c 164 }
731b0364
AL
165}
166
8b6b99b3 167static int migrate_fd_cleanup(MigrationState *s)
065e2813 168{
41ef56e6
AL
169 int ret = 0;
170
065e2813
AL
171 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
172
173 if (s->file) {
d0f2c4c6 174 DPRINTF("closing file\n");
41ef56e6
AL
175 if (qemu_fclose(s->file) != 0) {
176 ret = -1;
177 }
5d39c799 178 s->file = NULL;
84ec6552
JK
179 } else {
180 if (s->mon) {
181 monitor_resume(s->mon);
182 }
065e2813
AL
183 }
184
84ec6552 185 if (s->fd != -1) {
065e2813 186 close(s->fd);
84ec6552 187 s->fd = -1;
f327aa0c 188 }
065e2813 189
41ef56e6 190 return ret;
065e2813
AL
191}
192
8b6b99b3 193void migrate_fd_error(MigrationState *s)
065e2813 194{
8b6b99b3
JQ
195 DPRINTF("setting error state\n");
196 s->state = MIG_STATE_ERROR;
e0eb7390 197 notifier_list_notify(&migration_state_notifiers, s);
8b6b99b3
JQ
198 migrate_fd_cleanup(s);
199}
200
458cf28e
JQ
201static void migrate_fd_completed(MigrationState *s)
202{
203 DPRINTF("setting completed state\n");
204 if (migrate_fd_cleanup(s) < 0) {
205 s->state = MIG_STATE_ERROR;
206 } else {
207 s->state = MIG_STATE_COMPLETED;
208 runstate_set(RUN_STATE_POSTMIGRATE);
209 }
e0eb7390 210 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
211}
212
8b6b99b3 213static void migrate_fd_put_notify(void *opaque)
065e2813 214{
22f00a44 215 MigrationState *s = opaque;
065e2813
AL
216
217 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
218 qemu_file_put_notify(s->file);
1fdc11c3 219 if (s->file && qemu_file_get_error(s->file)) {
2350e13c
YT
220 migrate_fd_error(s);
221 }
065e2813
AL
222}
223
8b6b99b3
JQ
224static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
225 size_t size)
065e2813 226{
22f00a44 227 MigrationState *s = opaque;
065e2813
AL
228 ssize_t ret;
229
fdbecb5d
JQ
230 if (s->state != MIG_STATE_ACTIVE) {
231 return -EIO;
232 }
233
065e2813
AL
234 do {
235 ret = s->write(s, data, size);
95b134ea 236 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
237
238 if (ret == -1)
239 ret = -(s->get_error(s));
240
e447b1a6 241 if (ret == -EAGAIN) {
065e2813 242 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
e447b1a6 243 }
065e2813
AL
244
245 return ret;
246}
247
8b6b99b3 248static void migrate_fd_put_ready(void *opaque)
065e2813 249{
22f00a44 250 MigrationState *s = opaque;
065e2813
AL
251 int ret;
252
065e2813 253 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 254 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
255 return;
256 }
257
d0f2c4c6 258 DPRINTF("iterate\n");
39346385
JQ
259 ret = qemu_savevm_state_iterate(s->mon, s->file);
260 if (ret < 0) {
261 migrate_fd_error(s);
262 } else if (ret == 1) {
1354869c 263 int old_vm_running = runstate_is_running();
eeb34af9 264
d0f2c4c6 265 DPRINTF("done iterating\n");
8a9236f1 266 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
065e2813 267
67afff79
JQ
268 if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
269 migrate_fd_error(s);
b161d123 270 } else {
458cf28e 271 migrate_fd_completed(s);
b161d123 272 }
48a2f4d6 273 if (s->state != MIG_STATE_COMPLETED) {
41ef56e6
AL
274 if (old_vm_running) {
275 vm_start();
276 }
f5bbfba1 277 }
065e2813
AL
278 }
279}
280
0edda1c4 281static void migrate_fd_cancel(MigrationState *s)
065e2813 282{
065e2813
AL
283 if (s->state != MIG_STATE_ACTIVE)
284 return;
285
d0f2c4c6 286 DPRINTF("cancelling migration\n");
065e2813
AL
287
288 s->state = MIG_STATE_CANCELLED;
e0eb7390 289 notifier_list_notify(&migration_state_notifiers, s);
f327aa0c 290 qemu_savevm_state_cancel(s->mon, s->file);
065e2813
AL
291
292 migrate_fd_cleanup(s);
293}
294
8b6b99b3 295static void migrate_fd_wait_for_unfreeze(void *opaque)
065e2813 296{
22f00a44 297 MigrationState *s = opaque;
065e2813
AL
298 int ret;
299
d0f2c4c6 300 DPRINTF("wait for unfreeze\n");
065e2813
AL
301 if (s->state != MIG_STATE_ACTIVE)
302 return;
303
304 do {
305 fd_set wfds;
306
307 FD_ZERO(&wfds);
308 FD_SET(s->fd, &wfds);
309
310 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
311 } while (ret == -1 && (s->get_error(s)) == EINTR);
af509450
JQ
312
313 if (ret == -1) {
dcd1d224 314 qemu_file_set_error(s->file, -s->get_error(s));
af509450 315 }
065e2813
AL
316}
317
8b6b99b3 318static int migrate_fd_close(void *opaque)
065e2813 319{
22f00a44 320 MigrationState *s = opaque;
e19252d3 321
84ec6552
JK
322 if (s->mon) {
323 monitor_resume(s->mon);
324 }
e19252d3 325 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
326 return s->close(s);
327}
99a0db9b
GH
328
329void add_migration_state_change_notifier(Notifier *notify)
330{
331 notifier_list_add(&migration_state_notifiers, notify);
332}
333
334void remove_migration_state_change_notifier(Notifier *notify)
335{
336 notifier_list_remove(&migration_state_notifiers, notify);
337}
338
afe2df69
GH
339bool migration_is_active(MigrationState *s)
340{
341 return s->state == MIG_STATE_ACTIVE;
342}
343
7073693b 344bool migration_has_finished(MigrationState *s)
99a0db9b 345{
7073693b 346 return s->state == MIG_STATE_COMPLETED;
99a0db9b 347}
0edda1c4 348
afe2df69
GH
349bool migration_has_failed(MigrationState *s)
350{
351 return (s->state == MIG_STATE_CANCELLED ||
352 s->state == MIG_STATE_ERROR);
353}
354
8b6b99b3 355void migrate_fd_connect(MigrationState *s)
99a0db9b 356{
8b6b99b3
JQ
357 int ret;
358
d5934dde 359 s->state = MIG_STATE_ACTIVE;
8b6b99b3
JQ
360 s->file = qemu_fopen_ops_buffered(s,
361 s->bandwidth_limit,
362 migrate_fd_put_buffer,
363 migrate_fd_put_ready,
364 migrate_fd_wait_for_unfreeze,
365 migrate_fd_close);
366
367 DPRINTF("beginning savevm\n");
368 ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
369 if (ret < 0) {
370 DPRINTF("failed, %d\n", ret);
371 migrate_fd_error(s);
372 return;
373 }
374 migrate_fd_put_ready(s);
375}
376
d0ae46c1 377static MigrationState *migrate_init(Monitor *mon, int detach, int blk, int inc)
0edda1c4 378{
17549e84 379 MigrationState *s = migrate_get_current();
d0ae46c1 380 int64_t bandwidth_limit = s->bandwidth_limit;
0edda1c4 381
17549e84 382 memset(s, 0, sizeof(*s));
d0ae46c1 383 s->bandwidth_limit = bandwidth_limit;
0edda1c4
JQ
384 s->blk = blk;
385 s->shared = inc;
386 s->mon = NULL;
387 s->bandwidth_limit = bandwidth_limit;
d5934dde 388 s->state = MIG_STATE_SETUP;
0edda1c4
JQ
389
390 if (!detach) {
391 migrate_fd_monitor_suspend(s, mon);
392 }
393
394 return s;
395}
cab30143
JQ
396
397int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
398{
17549e84 399 MigrationState *s = migrate_get_current();
cab30143
JQ
400 const char *p;
401 int detach = qdict_get_try_bool(qdict, "detach", 0);
402 int blk = qdict_get_try_bool(qdict, "blk", 0);
403 int inc = qdict_get_try_bool(qdict, "inc", 0);
404 const char *uri = qdict_get_str(qdict, "uri");
405 int ret;
406
17549e84 407 if (s->state == MIG_STATE_ACTIVE) {
cab30143
JQ
408 monitor_printf(mon, "migration already in progress\n");
409 return -1;
410 }
411
412 if (qemu_savevm_state_blocked(mon)) {
413 return -1;
414 }
415
d0ae46c1 416 s = migrate_init(mon, detach, blk, inc);
cab30143
JQ
417
418 if (strstart(uri, "tcp:", &p)) {
419 ret = tcp_start_outgoing_migration(s, p);
420#if !defined(WIN32)
421 } else if (strstart(uri, "exec:", &p)) {
422 ret = exec_start_outgoing_migration(s, p);
423 } else if (strstart(uri, "unix:", &p)) {
424 ret = unix_start_outgoing_migration(s, p);
425 } else if (strstart(uri, "fd:", &p)) {
426 ret = fd_start_outgoing_migration(s, p);
427#endif
99a0db9b 428 } else {
cab30143
JQ
429 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
430 ret = -EINVAL;
cab30143
JQ
431 }
432
433 if (ret < 0) {
17549e84
JQ
434 monitor_printf(mon, "migration failed: %s\n", strerror(-ret));
435 return ret;
cab30143
JQ
436 }
437
e0eb7390 438 notifier_list_notify(&migration_state_notifiers, s);
cab30143 439 return 0;
cab30143
JQ
440}
441
442int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
443{
17549e84 444 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
445 return 0;
446}
447
448int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
449{
450 int64_t d;
451 MigrationState *s;
452
453 d = qdict_get_int(qdict, "value");
454 if (d < 0) {
455 d = 0;
99a0db9b 456 }
cab30143 457
17549e84 458 s = migrate_get_current();
d0ae46c1
JQ
459 s->bandwidth_limit = d;
460 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
cab30143
JQ
461
462 return 0;
463}
464
465int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
466 QObject **ret_data)
467{
468 double d;
469
470 d = qdict_get_double(qdict, "value") * 1e9;
471 d = MAX(0, MIN(UINT64_MAX, d));
472 max_downtime = (uint64_t)d;
473
474 return 0;
99a0db9b 475}