]> git.proxmox.com Git - qemu.git/blame - migration.c
Add migrate-set-capabilities
[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 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
5bb7910a
AL
14 */
15
16#include "qemu-common.h"
17#include "migration.h"
376253ec 18#include "monitor.h"
065e2813
AL
19#include "buffered_file.h"
20#include "sysemu.h"
21#include "block.h"
22#include "qemu_socket.h"
25f23643 23#include "block-migration.h"
791e7c82 24#include "qmp-commands.h"
065e2813
AL
25
26//#define DEBUG_MIGRATION
27
28#ifdef DEBUG_MIGRATION
d0f2c4c6 29#define DPRINTF(fmt, ...) \
065e2813
AL
30 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
31#else
d0f2c4c6 32#define DPRINTF(fmt, ...) \
065e2813
AL
33 do { } while (0)
34#endif
5bb7910a 35
7dc688ed
JQ
36enum {
37 MIG_STATE_ERROR,
38 MIG_STATE_SETUP,
39 MIG_STATE_CANCELLED,
40 MIG_STATE_ACTIVE,
41 MIG_STATE_COMPLETED,
42};
5bb7910a 43
d0ae46c1 44#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
5bb7910a 45
99a0db9b
GH
46static NotifierList migration_state_notifiers =
47 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
48
17549e84
JQ
49/* When we add fault tolerance, we could have several
50 migrations at once. For now we don't need to add
51 dynamic creation of migration */
52
53static MigrationState *migrate_get_current(void)
54{
55 static MigrationState current_migration = {
56 .state = MIG_STATE_SETUP,
d0ae46c1 57 .bandwidth_limit = MAX_THROTTLE,
17549e84
JQ
58 };
59
60 return &current_migration;
61}
62
d5c5dacc 63int qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 64{
34c9dd8e 65 const char *p;
8ca5e801 66 int ret;
34c9dd8e
AL
67
68 if (strstart(uri, "tcp:", &p))
d5c5dacc 69 ret = tcp_start_incoming_migration(p, errp);
065e2813
AL
70#if !defined(WIN32)
71 else if (strstart(uri, "exec:", &p))
8ca5e801 72 ret = exec_start_incoming_migration(p);
4951f65b 73 else if (strstart(uri, "unix:", &p))
8ca5e801 74 ret = unix_start_incoming_migration(p);
5ac1fad3 75 else if (strstart(uri, "fd:", &p))
8ca5e801 76 ret = fd_start_incoming_migration(p);
065e2813 77#endif
8ca5e801 78 else {
34c9dd8e 79 fprintf(stderr, "unknown migration protocol: %s\n", uri);
8ca5e801
JQ
80 ret = -EPROTONOSUPPORT;
81 }
82 return ret;
5bb7910a
AL
83}
84
511c0231
JQ
85void process_incoming_migration(QEMUFile *f)
86{
87 if (qemu_loadvm_state(f) < 0) {
88 fprintf(stderr, "load of migration failed\n");
89 exit(0);
90 }
91 qemu_announce_self();
92 DPRINTF("successfully loaded vm state\n");
93
901862cb 94 bdrv_clear_incoming_migration_all();
0f15423c
AL
95 /* Make sure all file formats flush their mutable metadata */
96 bdrv_invalidate_cache_all();
97
f5bbfba1 98 if (autostart) {
511c0231 99 vm_start();
f5bbfba1 100 } else {
0461d5a6 101 runstate_set(RUN_STATE_PRELAUNCH);
f5bbfba1 102 }
511c0231
JQ
103}
104
a0a3fd60
GC
105/* amount of nanoseconds we are willing to wait for migration to be down.
106 * the choice of nanoseconds is because it is the maximum resolution that
107 * get_clock() can achieve. It is an internal measure. All user-visible
108 * units must be in seconds */
109static uint64_t max_downtime = 30000000;
110
111uint64_t migrate_max_downtime(void)
112{
113 return max_downtime;
114}
115
bbf6da32
OW
116MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
117{
118 MigrationCapabilityStatusList *head = NULL;
119 MigrationCapabilityStatusList *caps;
120 MigrationState *s = migrate_get_current();
121 int i;
122
123 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
124 if (head == NULL) {
125 head = g_malloc0(sizeof(*caps));
126 caps = head;
127 } else {
128 caps->next = g_malloc0(sizeof(*caps));
129 caps = caps->next;
130 }
131 caps->value =
132 g_malloc(sizeof(*caps->value));
133 caps->value->capability = i;
134 caps->value->state = s->enabled_capabilities[i];
135 }
136
137 return head;
138}
139
791e7c82 140MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 141{
791e7c82 142 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
143 MigrationState *s = migrate_get_current();
144
145 switch (s->state) {
146 case MIG_STATE_SETUP:
147 /* no migration has happened ever */
148 break;
149 case MIG_STATE_ACTIVE:
791e7c82
LC
150 info->has_status = true;
151 info->status = g_strdup("active");
17549e84 152
791e7c82
LC
153 info->has_ram = true;
154 info->ram = g_malloc0(sizeof(*info->ram));
155 info->ram->transferred = ram_bytes_transferred();
156 info->ram->remaining = ram_bytes_remaining();
157 info->ram->total = ram_bytes_total();
d5f8a570
JQ
158 info->ram->total_time = qemu_get_clock_ms(rt_clock)
159 - s->total_time;
17549e84
JQ
160
161 if (blk_mig_active()) {
791e7c82
LC
162 info->has_disk = true;
163 info->disk = g_malloc0(sizeof(*info->disk));
164 info->disk->transferred = blk_mig_bytes_transferred();
165 info->disk->remaining = blk_mig_bytes_remaining();
166 info->disk->total = blk_mig_bytes_total();
ff8d81d8 167 }
17549e84
JQ
168 break;
169 case MIG_STATE_COMPLETED:
791e7c82
LC
170 info->has_status = true;
171 info->status = g_strdup("completed");
d5f8a570
JQ
172
173 info->has_ram = true;
174 info->ram = g_malloc0(sizeof(*info->ram));
175 info->ram->transferred = ram_bytes_transferred();
176 info->ram->remaining = 0;
177 info->ram->total = ram_bytes_total();
178 info->ram->total_time = s->total_time;
17549e84
JQ
179 break;
180 case MIG_STATE_ERROR:
791e7c82
LC
181 info->has_status = true;
182 info->status = g_strdup("failed");
17549e84
JQ
183 break;
184 case MIG_STATE_CANCELLED:
791e7c82
LC
185 info->has_status = true;
186 info->status = g_strdup("cancelled");
17549e84 187 break;
5bb7910a 188 }
791e7c82
LC
189
190 return info;
5bb7910a
AL
191}
192
00458433
OW
193void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
194 Error **errp)
195{
196 MigrationState *s = migrate_get_current();
197 MigrationCapabilityStatusList *cap;
198
199 if (s->state == MIG_STATE_ACTIVE) {
200 error_set(errp, QERR_MIGRATION_ACTIVE);
201 return;
202 }
203
204 for (cap = params; cap; cap = cap->next) {
205 s->enabled_capabilities[cap->value->capability] = cap->value->state;
206 }
207}
208
065e2813
AL
209/* shared migration helpers */
210
8b6b99b3 211static int migrate_fd_cleanup(MigrationState *s)
065e2813 212{
41ef56e6
AL
213 int ret = 0;
214
065e2813
AL
215 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
216
217 if (s->file) {
d0f2c4c6 218 DPRINTF("closing file\n");
a6d34a94 219 ret = qemu_fclose(s->file);
5d39c799 220 s->file = NULL;
065e2813
AL
221 }
222
84ec6552 223 if (s->fd != -1) {
065e2813 224 close(s->fd);
84ec6552 225 s->fd = -1;
f327aa0c 226 }
065e2813 227
41ef56e6 228 return ret;
065e2813
AL
229}
230
8b6b99b3 231void migrate_fd_error(MigrationState *s)
065e2813 232{
8b6b99b3
JQ
233 DPRINTF("setting error state\n");
234 s->state = MIG_STATE_ERROR;
e0eb7390 235 notifier_list_notify(&migration_state_notifiers, s);
8b6b99b3
JQ
236 migrate_fd_cleanup(s);
237}
238
458cf28e
JQ
239static void migrate_fd_completed(MigrationState *s)
240{
241 DPRINTF("setting completed state\n");
242 if (migrate_fd_cleanup(s) < 0) {
243 s->state = MIG_STATE_ERROR;
244 } else {
245 s->state = MIG_STATE_COMPLETED;
246 runstate_set(RUN_STATE_POSTMIGRATE);
247 }
e0eb7390 248 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
249}
250
8b6b99b3 251static void migrate_fd_put_notify(void *opaque)
065e2813 252{
22f00a44 253 MigrationState *s = opaque;
065e2813
AL
254
255 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
256 qemu_file_put_notify(s->file);
1fdc11c3 257 if (s->file && qemu_file_get_error(s->file)) {
2350e13c
YT
258 migrate_fd_error(s);
259 }
065e2813
AL
260}
261
8b6b99b3
JQ
262static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
263 size_t size)
065e2813 264{
22f00a44 265 MigrationState *s = opaque;
065e2813
AL
266 ssize_t ret;
267
fdbecb5d
JQ
268 if (s->state != MIG_STATE_ACTIVE) {
269 return -EIO;
270 }
271
065e2813
AL
272 do {
273 ret = s->write(s, data, size);
95b134ea 274 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
275
276 if (ret == -1)
277 ret = -(s->get_error(s));
278
e447b1a6 279 if (ret == -EAGAIN) {
065e2813 280 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
e447b1a6 281 }
065e2813
AL
282
283 return ret;
284}
285
8b6b99b3 286static void migrate_fd_put_ready(void *opaque)
065e2813 287{
22f00a44 288 MigrationState *s = opaque;
065e2813
AL
289 int ret;
290
065e2813 291 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 292 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
293 return;
294 }
295
d0f2c4c6 296 DPRINTF("iterate\n");
539de124 297 ret = qemu_savevm_state_iterate(s->file);
39346385
JQ
298 if (ret < 0) {
299 migrate_fd_error(s);
300 } else if (ret == 1) {
1354869c 301 int old_vm_running = runstate_is_running();
eeb34af9 302
d0f2c4c6 303 DPRINTF("done iterating\n");
7b5d3aa2 304 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
8a9236f1 305 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
065e2813 306
539de124 307 if (qemu_savevm_state_complete(s->file) < 0) {
67afff79 308 migrate_fd_error(s);
b161d123 309 } else {
458cf28e 310 migrate_fd_completed(s);
b161d123 311 }
d5f8a570 312 s->total_time = qemu_get_clock_ms(rt_clock) - s->total_time;
48a2f4d6 313 if (s->state != MIG_STATE_COMPLETED) {
41ef56e6
AL
314 if (old_vm_running) {
315 vm_start();
316 }
f5bbfba1 317 }
065e2813
AL
318 }
319}
320
0edda1c4 321static void migrate_fd_cancel(MigrationState *s)
065e2813 322{
065e2813
AL
323 if (s->state != MIG_STATE_ACTIVE)
324 return;
325
d0f2c4c6 326 DPRINTF("cancelling migration\n");
065e2813
AL
327
328 s->state = MIG_STATE_CANCELLED;
e0eb7390 329 notifier_list_notify(&migration_state_notifiers, s);
539de124 330 qemu_savevm_state_cancel(s->file);
065e2813
AL
331
332 migrate_fd_cleanup(s);
333}
334
8b6b99b3 335static void migrate_fd_wait_for_unfreeze(void *opaque)
065e2813 336{
22f00a44 337 MigrationState *s = opaque;
065e2813
AL
338 int ret;
339
d0f2c4c6 340 DPRINTF("wait for unfreeze\n");
065e2813
AL
341 if (s->state != MIG_STATE_ACTIVE)
342 return;
343
344 do {
345 fd_set wfds;
346
347 FD_ZERO(&wfds);
348 FD_SET(s->fd, &wfds);
349
350 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
351 } while (ret == -1 && (s->get_error(s)) == EINTR);
af509450
JQ
352
353 if (ret == -1) {
dcd1d224 354 qemu_file_set_error(s->file, -s->get_error(s));
af509450 355 }
065e2813
AL
356}
357
8b6b99b3 358static int migrate_fd_close(void *opaque)
065e2813 359{
22f00a44 360 MigrationState *s = opaque;
e19252d3
UL
361
362 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
363 return s->close(s);
364}
99a0db9b
GH
365
366void add_migration_state_change_notifier(Notifier *notify)
367{
368 notifier_list_add(&migration_state_notifiers, notify);
369}
370
371void remove_migration_state_change_notifier(Notifier *notify)
372{
31552529 373 notifier_remove(notify);
99a0db9b
GH
374}
375
afe2df69
GH
376bool migration_is_active(MigrationState *s)
377{
378 return s->state == MIG_STATE_ACTIVE;
379}
380
7073693b 381bool migration_has_finished(MigrationState *s)
99a0db9b 382{
7073693b 383 return s->state == MIG_STATE_COMPLETED;
99a0db9b 384}
0edda1c4 385
afe2df69
GH
386bool migration_has_failed(MigrationState *s)
387{
388 return (s->state == MIG_STATE_CANCELLED ||
389 s->state == MIG_STATE_ERROR);
390}
391
8b6b99b3 392void migrate_fd_connect(MigrationState *s)
99a0db9b 393{
8b6b99b3
JQ
394 int ret;
395
d5934dde 396 s->state = MIG_STATE_ACTIVE;
8b6b99b3
JQ
397 s->file = qemu_fopen_ops_buffered(s,
398 s->bandwidth_limit,
399 migrate_fd_put_buffer,
400 migrate_fd_put_ready,
401 migrate_fd_wait_for_unfreeze,
402 migrate_fd_close);
403
404 DPRINTF("beginning savevm\n");
6607ae23 405 ret = qemu_savevm_state_begin(s->file, &s->params);
8b6b99b3
JQ
406 if (ret < 0) {
407 DPRINTF("failed, %d\n", ret);
408 migrate_fd_error(s);
409 return;
410 }
411 migrate_fd_put_ready(s);
412}
413
6607ae23 414static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 415{
17549e84 416 MigrationState *s = migrate_get_current();
d0ae46c1 417 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32
OW
418 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
419
420 memcpy(enabled_capabilities, s->enabled_capabilities,
421 sizeof(enabled_capabilities));
0edda1c4 422
17549e84 423 memset(s, 0, sizeof(*s));
d0ae46c1 424 s->bandwidth_limit = bandwidth_limit;
6607ae23 425 s->params = *params;
bbf6da32
OW
426 memcpy(s->enabled_capabilities, enabled_capabilities,
427 sizeof(enabled_capabilities));
1299c631 428
0edda1c4 429 s->bandwidth_limit = bandwidth_limit;
d5934dde 430 s->state = MIG_STATE_SETUP;
d5f8a570 431 s->total_time = qemu_get_clock_ms(rt_clock);
0edda1c4 432
0edda1c4
JQ
433 return s;
434}
cab30143 435
fa2756b7
AL
436static GSList *migration_blockers;
437
438void migrate_add_blocker(Error *reason)
439{
440 migration_blockers = g_slist_prepend(migration_blockers, reason);
441}
442
443void migrate_del_blocker(Error *reason)
444{
445 migration_blockers = g_slist_remove(migration_blockers, reason);
446}
447
e1c37d0e
LC
448void qmp_migrate(const char *uri, bool has_blk, bool blk,
449 bool has_inc, bool inc, bool has_detach, bool detach,
450 Error **errp)
cab30143 451{
17549e84 452 MigrationState *s = migrate_get_current();
6607ae23 453 MigrationParams params;
cab30143 454 const char *p;
cab30143
JQ
455 int ret;
456
6607ae23
IY
457 params.blk = blk;
458 params.shared = inc;
459
17549e84 460 if (s->state == MIG_STATE_ACTIVE) {
e1c37d0e
LC
461 error_set(errp, QERR_MIGRATION_ACTIVE);
462 return;
cab30143
JQ
463 }
464
e1c37d0e
LC
465 if (qemu_savevm_state_blocked(errp)) {
466 return;
cab30143
JQ
467 }
468
fa2756b7 469 if (migration_blockers) {
e1c37d0e
LC
470 *errp = error_copy(migration_blockers->data);
471 return;
fa2756b7
AL
472 }
473
6607ae23 474 s = migrate_init(&params);
cab30143
JQ
475
476 if (strstart(uri, "tcp:", &p)) {
d5c5dacc 477 ret = tcp_start_outgoing_migration(s, p, errp);
cab30143
JQ
478#if !defined(WIN32)
479 } else if (strstart(uri, "exec:", &p)) {
480 ret = exec_start_outgoing_migration(s, p);
481 } else if (strstart(uri, "unix:", &p)) {
482 ret = unix_start_outgoing_migration(s, p);
483 } else if (strstart(uri, "fd:", &p)) {
484 ret = fd_start_outgoing_migration(s, p);
485#endif
99a0db9b 486 } else {
e1c37d0e
LC
487 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
488 return;
cab30143
JQ
489 }
490
491 if (ret < 0) {
d5c5dacc
AK
492 if (!error_is_set(errp)) {
493 DPRINTF("migration failed: %s\n", strerror(-ret));
494 /* FIXME: we should return meaningful errors */
495 error_set(errp, QERR_UNDEFINED_ERROR);
496 }
e1c37d0e 497 return;
1299c631
JQ
498 }
499
e0eb7390 500 notifier_list_notify(&migration_state_notifiers, s);
cab30143
JQ
501}
502
6cdedb07 503void qmp_migrate_cancel(Error **errp)
cab30143 504{
17549e84 505 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
506}
507
3dc85383 508void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 509{
cab30143
JQ
510 MigrationState *s;
511
3dc85383
LC
512 if (value < 0) {
513 value = 0;
99a0db9b 514 }
cab30143 515
17549e84 516 s = migrate_get_current();
3dc85383 517 s->bandwidth_limit = value;
d0ae46c1 518 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
cab30143
JQ
519}
520
4f0a993b 521void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 522{
4f0a993b
LC
523 value *= 1e9;
524 value = MAX(0, MIN(UINT64_MAX, value));
525 max_downtime = (uint64_t)value;
99a0db9b 526}