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