]> git.proxmox.com Git - qemu.git/blame - migration.c
s390x: implement SIGP restart and shutdown
[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 158 if (monitor_suspend(mon) == 0) {
d0f2c4c6 159 DPRINTF("suspending monitor\n");
f327aa0c
JK
160 } else {
161 monitor_printf(mon, "terminal does not allow synchronous "
cde76ee1 162 "migration, continuing detached\n");
f327aa0c 163 }
731b0364
AL
164}
165
8b6b99b3 166static int migrate_fd_cleanup(MigrationState *s)
065e2813 167{
41ef56e6
AL
168 int ret = 0;
169
065e2813
AL
170 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
171
172 if (s->file) {
d0f2c4c6 173 DPRINTF("closing file\n");
41ef56e6
AL
174 if (qemu_fclose(s->file) != 0) {
175 ret = -1;
176 }
5d39c799 177 s->file = NULL;
84ec6552
JK
178 } else {
179 if (s->mon) {
180 monitor_resume(s->mon);
181 }
065e2813
AL
182 }
183
84ec6552 184 if (s->fd != -1) {
065e2813 185 close(s->fd);
84ec6552 186 s->fd = -1;
f327aa0c 187 }
065e2813 188
41ef56e6 189 return ret;
065e2813
AL
190}
191
8b6b99b3 192void migrate_fd_error(MigrationState *s)
065e2813 193{
8b6b99b3
JQ
194 DPRINTF("setting error state\n");
195 s->state = MIG_STATE_ERROR;
e0eb7390 196 notifier_list_notify(&migration_state_notifiers, s);
8b6b99b3
JQ
197 migrate_fd_cleanup(s);
198}
199
458cf28e
JQ
200static void migrate_fd_completed(MigrationState *s)
201{
202 DPRINTF("setting completed state\n");
203 if (migrate_fd_cleanup(s) < 0) {
204 s->state = MIG_STATE_ERROR;
205 } else {
206 s->state = MIG_STATE_COMPLETED;
207 runstate_set(RUN_STATE_POSTMIGRATE);
208 }
e0eb7390 209 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
210}
211
8b6b99b3 212static void migrate_fd_put_notify(void *opaque)
065e2813 213{
22f00a44 214 MigrationState *s = opaque;
065e2813
AL
215
216 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
217 qemu_file_put_notify(s->file);
1fdc11c3 218 if (s->file && qemu_file_get_error(s->file)) {
2350e13c
YT
219 migrate_fd_error(s);
220 }
065e2813
AL
221}
222
8b6b99b3
JQ
223static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
224 size_t size)
065e2813 225{
22f00a44 226 MigrationState *s = opaque;
065e2813
AL
227 ssize_t ret;
228
fdbecb5d
JQ
229 if (s->state != MIG_STATE_ACTIVE) {
230 return -EIO;
231 }
232
065e2813
AL
233 do {
234 ret = s->write(s, data, size);
95b134ea 235 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
236
237 if (ret == -1)
238 ret = -(s->get_error(s));
239
e447b1a6 240 if (ret == -EAGAIN) {
065e2813 241 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
e447b1a6 242 }
065e2813
AL
243
244 return ret;
245}
246
8b6b99b3 247static void migrate_fd_put_ready(void *opaque)
065e2813 248{
22f00a44 249 MigrationState *s = opaque;
065e2813
AL
250 int ret;
251
065e2813 252 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 253 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
254 return;
255 }
256
d0f2c4c6 257 DPRINTF("iterate\n");
39346385
JQ
258 ret = qemu_savevm_state_iterate(s->mon, s->file);
259 if (ret < 0) {
260 migrate_fd_error(s);
261 } else if (ret == 1) {
1354869c 262 int old_vm_running = runstate_is_running();
eeb34af9 263
d0f2c4c6 264 DPRINTF("done iterating\n");
8a9236f1 265 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
065e2813 266
67afff79
JQ
267 if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
268 migrate_fd_error(s);
b161d123 269 } else {
458cf28e 270 migrate_fd_completed(s);
b161d123 271 }
48a2f4d6 272 if (s->state != MIG_STATE_COMPLETED) {
41ef56e6
AL
273 if (old_vm_running) {
274 vm_start();
275 }
f5bbfba1 276 }
065e2813
AL
277 }
278}
279
0edda1c4 280static void migrate_fd_cancel(MigrationState *s)
065e2813 281{
065e2813
AL
282 if (s->state != MIG_STATE_ACTIVE)
283 return;
284
d0f2c4c6 285 DPRINTF("cancelling migration\n");
065e2813
AL
286
287 s->state = MIG_STATE_CANCELLED;
e0eb7390 288 notifier_list_notify(&migration_state_notifiers, s);
f327aa0c 289 qemu_savevm_state_cancel(s->mon, s->file);
065e2813
AL
290
291 migrate_fd_cleanup(s);
292}
293
8b6b99b3 294static void migrate_fd_wait_for_unfreeze(void *opaque)
065e2813 295{
22f00a44 296 MigrationState *s = opaque;
065e2813
AL
297 int ret;
298
d0f2c4c6 299 DPRINTF("wait for unfreeze\n");
065e2813
AL
300 if (s->state != MIG_STATE_ACTIVE)
301 return;
302
303 do {
304 fd_set wfds;
305
306 FD_ZERO(&wfds);
307 FD_SET(s->fd, &wfds);
308
309 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
310 } while (ret == -1 && (s->get_error(s)) == EINTR);
af509450
JQ
311
312 if (ret == -1) {
dcd1d224 313 qemu_file_set_error(s->file, -s->get_error(s));
af509450 314 }
065e2813
AL
315}
316
8b6b99b3 317static int migrate_fd_close(void *opaque)
065e2813 318{
22f00a44 319 MigrationState *s = opaque;
e19252d3 320
84ec6552
JK
321 if (s->mon) {
322 monitor_resume(s->mon);
323 }
e19252d3 324 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
325 return s->close(s);
326}
99a0db9b
GH
327
328void add_migration_state_change_notifier(Notifier *notify)
329{
330 notifier_list_add(&migration_state_notifiers, notify);
331}
332
333void remove_migration_state_change_notifier(Notifier *notify)
334{
335 notifier_list_remove(&migration_state_notifiers, notify);
336}
337
afe2df69
GH
338bool migration_is_active(MigrationState *s)
339{
340 return s->state == MIG_STATE_ACTIVE;
341}
342
7073693b 343bool migration_has_finished(MigrationState *s)
99a0db9b 344{
7073693b 345 return s->state == MIG_STATE_COMPLETED;
99a0db9b 346}
0edda1c4 347
afe2df69
GH
348bool migration_has_failed(MigrationState *s)
349{
350 return (s->state == MIG_STATE_CANCELLED ||
351 s->state == MIG_STATE_ERROR);
352}
353
8b6b99b3 354void migrate_fd_connect(MigrationState *s)
99a0db9b 355{
8b6b99b3
JQ
356 int ret;
357
d5934dde 358 s->state = MIG_STATE_ACTIVE;
8b6b99b3
JQ
359 s->file = qemu_fopen_ops_buffered(s,
360 s->bandwidth_limit,
361 migrate_fd_put_buffer,
362 migrate_fd_put_ready,
363 migrate_fd_wait_for_unfreeze,
364 migrate_fd_close);
365
366 DPRINTF("beginning savevm\n");
367 ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
368 if (ret < 0) {
369 DPRINTF("failed, %d\n", ret);
370 migrate_fd_error(s);
371 return;
372 }
373 migrate_fd_put_ready(s);
374}
375
d0ae46c1 376static MigrationState *migrate_init(Monitor *mon, int detach, int blk, int inc)
0edda1c4 377{
17549e84 378 MigrationState *s = migrate_get_current();
d0ae46c1 379 int64_t bandwidth_limit = s->bandwidth_limit;
0edda1c4 380
17549e84 381 memset(s, 0, sizeof(*s));
d0ae46c1 382 s->bandwidth_limit = bandwidth_limit;
0edda1c4
JQ
383 s->blk = blk;
384 s->shared = inc;
1299c631
JQ
385
386 /* s->mon is used for two things:
387 - pass fd in fd migration
388 - suspend/resume monitor for not detached migration
389 */
390 s->mon = mon;
0edda1c4 391 s->bandwidth_limit = bandwidth_limit;
d5934dde 392 s->state = MIG_STATE_SETUP;
0edda1c4
JQ
393
394 if (!detach) {
395 migrate_fd_monitor_suspend(s, mon);
396 }
397
398 return s;
399}
cab30143
JQ
400
401int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
402{
17549e84 403 MigrationState *s = migrate_get_current();
cab30143
JQ
404 const char *p;
405 int detach = qdict_get_try_bool(qdict, "detach", 0);
406 int blk = qdict_get_try_bool(qdict, "blk", 0);
407 int inc = qdict_get_try_bool(qdict, "inc", 0);
408 const char *uri = qdict_get_str(qdict, "uri");
409 int ret;
410
17549e84 411 if (s->state == MIG_STATE_ACTIVE) {
cab30143
JQ
412 monitor_printf(mon, "migration already in progress\n");
413 return -1;
414 }
415
416 if (qemu_savevm_state_blocked(mon)) {
417 return -1;
418 }
419
d0ae46c1 420 s = migrate_init(mon, detach, blk, inc);
cab30143
JQ
421
422 if (strstart(uri, "tcp:", &p)) {
423 ret = tcp_start_outgoing_migration(s, p);
424#if !defined(WIN32)
425 } else if (strstart(uri, "exec:", &p)) {
426 ret = exec_start_outgoing_migration(s, p);
427 } else if (strstart(uri, "unix:", &p)) {
428 ret = unix_start_outgoing_migration(s, p);
429 } else if (strstart(uri, "fd:", &p)) {
430 ret = fd_start_outgoing_migration(s, p);
431#endif
99a0db9b 432 } else {
cab30143
JQ
433 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
434 ret = -EINVAL;
cab30143
JQ
435 }
436
437 if (ret < 0) {
17549e84
JQ
438 monitor_printf(mon, "migration failed: %s\n", strerror(-ret));
439 return ret;
cab30143
JQ
440 }
441
1299c631
JQ
442 if (detach) {
443 s->mon = NULL;
444 }
445
e0eb7390 446 notifier_list_notify(&migration_state_notifiers, s);
cab30143 447 return 0;
cab30143
JQ
448}
449
450int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
451{
17549e84 452 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
453 return 0;
454}
455
456int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
457{
458 int64_t d;
459 MigrationState *s;
460
461 d = qdict_get_int(qdict, "value");
462 if (d < 0) {
463 d = 0;
99a0db9b 464 }
cab30143 465
17549e84 466 s = migrate_get_current();
d0ae46c1
JQ
467 s->bandwidth_limit = d;
468 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
cab30143
JQ
469
470 return 0;
471}
472
473int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
474 QObject **ret_data)
475{
476 double d;
477
478 d = qdict_get_double(qdict, "value") * 1e9;
479 d = MAX(0, MIN(UINT64_MAX, d));
480 max_downtime = (uint64_t)d;
481
482 return 0;
99a0db9b 483}