]> git.proxmox.com Git - qemu.git/blame - migration.c
migration: clean up server sockets and handlers before invoking process_incoming_migr...
[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
17ad9b35
OW
46/* Migration XBZRLE default cache size */
47#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
48
99a0db9b
GH
49static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
51
17549e84
JQ
52/* When we add fault tolerance, we could have several
53 migrations at once. For now we don't need to add
54 dynamic creation of migration */
55
859bc756 56MigrationState *migrate_get_current(void)
17549e84
JQ
57{
58 static MigrationState current_migration = {
59 .state = MIG_STATE_SETUP,
d0ae46c1 60 .bandwidth_limit = MAX_THROTTLE,
17ad9b35 61 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
17549e84
JQ
62 };
63
64 return &current_migration;
65}
66
43eaae28 67void qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 68{
34c9dd8e
AL
69 const char *p;
70
71 if (strstart(uri, "tcp:", &p))
43eaae28 72 tcp_start_incoming_migration(p, errp);
065e2813
AL
73#if !defined(WIN32)
74 else if (strstart(uri, "exec:", &p))
43eaae28 75 exec_start_incoming_migration(p, errp);
4951f65b 76 else if (strstart(uri, "unix:", &p))
43eaae28 77 unix_start_incoming_migration(p, errp);
5ac1fad3 78 else if (strstart(uri, "fd:", &p))
43eaae28 79 fd_start_incoming_migration(p, errp);
065e2813 80#endif
8ca5e801 81 else {
43eaae28 82 error_setg(errp, "unknown migration protocol: %s\n", uri);
8ca5e801 83 }
5bb7910a
AL
84}
85
511c0231
JQ
86void process_incoming_migration(QEMUFile *f)
87{
88 if (qemu_loadvm_state(f) < 0) {
89 fprintf(stderr, "load of migration failed\n");
90 exit(0);
91 }
92 qemu_announce_self();
93 DPRINTF("successfully loaded vm state\n");
94
901862cb 95 bdrv_clear_incoming_migration_all();
0f15423c
AL
96 /* Make sure all file formats flush their mutable metadata */
97 bdrv_invalidate_cache_all();
98
f5bbfba1 99 if (autostart) {
511c0231 100 vm_start();
f5bbfba1 101 } else {
29ed72f1 102 runstate_set(RUN_STATE_PAUSED);
f5bbfba1 103 }
511c0231
JQ
104}
105
a0a3fd60
GC
106/* amount of nanoseconds we are willing to wait for migration to be down.
107 * the choice of nanoseconds is because it is the maximum resolution that
108 * get_clock() can achieve. It is an internal measure. All user-visible
109 * units must be in seconds */
110static uint64_t max_downtime = 30000000;
111
112uint64_t migrate_max_downtime(void)
113{
114 return max_downtime;
115}
116
bbf6da32
OW
117MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
118{
119 MigrationCapabilityStatusList *head = NULL;
120 MigrationCapabilityStatusList *caps;
121 MigrationState *s = migrate_get_current();
122 int i;
123
124 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
125 if (head == NULL) {
126 head = g_malloc0(sizeof(*caps));
127 caps = head;
128 } else {
129 caps->next = g_malloc0(sizeof(*caps));
130 caps = caps->next;
131 }
132 caps->value =
133 g_malloc(sizeof(*caps->value));
134 caps->value->capability = i;
135 caps->value->state = s->enabled_capabilities[i];
136 }
137
138 return head;
139}
140
f36d55af
OW
141static void get_xbzrle_cache_stats(MigrationInfo *info)
142{
143 if (migrate_use_xbzrle()) {
144 info->has_xbzrle_cache = true;
145 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
146 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
147 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
148 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
149 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
150 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
151 }
152}
153
791e7c82 154MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 155{
791e7c82 156 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
157 MigrationState *s = migrate_get_current();
158
159 switch (s->state) {
160 case MIG_STATE_SETUP:
161 /* no migration has happened ever */
162 break;
163 case MIG_STATE_ACTIVE:
791e7c82
LC
164 info->has_status = true;
165 info->status = g_strdup("active");
7aa939af
JQ
166 info->has_total_time = true;
167 info->total_time = qemu_get_clock_ms(rt_clock)
168 - s->total_time;
2c52ddf1
JQ
169 info->has_expected_downtime = true;
170 info->expected_downtime = s->expected_downtime;
17549e84 171
791e7c82
LC
172 info->has_ram = true;
173 info->ram = g_malloc0(sizeof(*info->ram));
174 info->ram->transferred = ram_bytes_transferred();
175 info->ram->remaining = ram_bytes_remaining();
176 info->ram->total = ram_bytes_total();
004d4c10
OW
177 info->ram->duplicate = dup_mig_pages_transferred();
178 info->ram->normal = norm_mig_pages_transferred();
179 info->ram->normal_bytes = norm_mig_bytes_transferred();
8d017193
JQ
180 info->ram->dirty_pages_rate = s->dirty_pages_rate;
181
17549e84
JQ
182
183 if (blk_mig_active()) {
791e7c82
LC
184 info->has_disk = true;
185 info->disk = g_malloc0(sizeof(*info->disk));
186 info->disk->transferred = blk_mig_bytes_transferred();
187 info->disk->remaining = blk_mig_bytes_remaining();
188 info->disk->total = blk_mig_bytes_total();
ff8d81d8 189 }
f36d55af
OW
190
191 get_xbzrle_cache_stats(info);
17549e84
JQ
192 break;
193 case MIG_STATE_COMPLETED:
f36d55af
OW
194 get_xbzrle_cache_stats(info);
195
791e7c82
LC
196 info->has_status = true;
197 info->status = g_strdup("completed");
7aa939af 198 info->total_time = s->total_time;
9c5a9fcf
JQ
199 info->has_downtime = true;
200 info->downtime = s->downtime;
d5f8a570
JQ
201
202 info->has_ram = true;
203 info->ram = g_malloc0(sizeof(*info->ram));
204 info->ram->transferred = ram_bytes_transferred();
205 info->ram->remaining = 0;
206 info->ram->total = ram_bytes_total();
004d4c10
OW
207 info->ram->duplicate = dup_mig_pages_transferred();
208 info->ram->normal = norm_mig_pages_transferred();
209 info->ram->normal_bytes = norm_mig_bytes_transferred();
17549e84
JQ
210 break;
211 case MIG_STATE_ERROR:
791e7c82
LC
212 info->has_status = true;
213 info->status = g_strdup("failed");
17549e84
JQ
214 break;
215 case MIG_STATE_CANCELLED:
791e7c82
LC
216 info->has_status = true;
217 info->status = g_strdup("cancelled");
17549e84 218 break;
5bb7910a 219 }
791e7c82
LC
220
221 return info;
5bb7910a
AL
222}
223
00458433
OW
224void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
225 Error **errp)
226{
227 MigrationState *s = migrate_get_current();
228 MigrationCapabilityStatusList *cap;
229
230 if (s->state == MIG_STATE_ACTIVE) {
231 error_set(errp, QERR_MIGRATION_ACTIVE);
232 return;
233 }
234
235 for (cap = params; cap; cap = cap->next) {
236 s->enabled_capabilities[cap->value->capability] = cap->value->state;
237 }
238}
239
065e2813
AL
240/* shared migration helpers */
241
8b6b99b3 242static int migrate_fd_cleanup(MigrationState *s)
065e2813 243{
41ef56e6
AL
244 int ret = 0;
245
3202beca
OW
246 if (s->fd != -1) {
247 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
248 }
065e2813
AL
249
250 if (s->file) {
d0f2c4c6 251 DPRINTF("closing file\n");
a6d34a94 252 ret = qemu_fclose(s->file);
5d39c799 253 s->file = NULL;
065e2813
AL
254 }
255
84ec6552 256 if (s->fd != -1) {
065e2813 257 close(s->fd);
84ec6552 258 s->fd = -1;
f327aa0c 259 }
065e2813 260
41ef56e6 261 return ret;
065e2813
AL
262}
263
8b6b99b3 264void migrate_fd_error(MigrationState *s)
065e2813 265{
8b6b99b3
JQ
266 DPRINTF("setting error state\n");
267 s->state = MIG_STATE_ERROR;
e0eb7390 268 notifier_list_notify(&migration_state_notifiers, s);
8b6b99b3
JQ
269 migrate_fd_cleanup(s);
270}
271
458cf28e
JQ
272static void migrate_fd_completed(MigrationState *s)
273{
274 DPRINTF("setting completed state\n");
275 if (migrate_fd_cleanup(s) < 0) {
276 s->state = MIG_STATE_ERROR;
277 } else {
278 s->state = MIG_STATE_COMPLETED;
279 runstate_set(RUN_STATE_POSTMIGRATE);
280 }
e0eb7390 281 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
282}
283
8b6b99b3 284static void migrate_fd_put_notify(void *opaque)
065e2813 285{
22f00a44 286 MigrationState *s = opaque;
a2b41351 287 int ret;
065e2813
AL
288
289 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
a2b41351
JQ
290 ret = qemu_file_put_notify(s->file);
291 if (ret) {
2350e13c
YT
292 migrate_fd_error(s);
293 }
065e2813
AL
294}
295
c87b015b
JQ
296ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
297 size_t size)
065e2813 298{
065e2813
AL
299 ssize_t ret;
300
fdbecb5d
JQ
301 if (s->state != MIG_STATE_ACTIVE) {
302 return -EIO;
303 }
304
065e2813
AL
305 do {
306 ret = s->write(s, data, size);
95b134ea 307 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
308
309 if (ret == -1)
310 ret = -(s->get_error(s));
311
e447b1a6 312 if (ret == -EAGAIN) {
065e2813 313 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
e447b1a6 314 }
065e2813
AL
315
316 return ret;
317}
318
2c9adcb8 319void migrate_fd_put_ready(MigrationState *s)
065e2813
AL
320{
321 int ret;
322
065e2813 323 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 324 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
325 return;
326 }
327
d0f2c4c6 328 DPRINTF("iterate\n");
539de124 329 ret = qemu_savevm_state_iterate(s->file);
39346385
JQ
330 if (ret < 0) {
331 migrate_fd_error(s);
332 } else if (ret == 1) {
1354869c 333 int old_vm_running = runstate_is_running();
9c5a9fcf 334 int64_t start_time, end_time;
eeb34af9 335
d0f2c4c6 336 DPRINTF("done iterating\n");
9c5a9fcf 337 start_time = qemu_get_clock_ms(rt_clock);
7b5d3aa2 338 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
8a9236f1 339 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
065e2813 340
539de124 341 if (qemu_savevm_state_complete(s->file) < 0) {
67afff79 342 migrate_fd_error(s);
b161d123 343 } else {
458cf28e 344 migrate_fd_completed(s);
b161d123 345 }
97d4d961
JQ
346 end_time = qemu_get_clock_ms(rt_clock);
347 s->total_time = end_time - s->total_time;
9c5a9fcf 348 s->downtime = end_time - start_time;
48a2f4d6 349 if (s->state != MIG_STATE_COMPLETED) {
41ef56e6
AL
350 if (old_vm_running) {
351 vm_start();
352 }
f5bbfba1 353 }
065e2813
AL
354 }
355}
356
0edda1c4 357static void migrate_fd_cancel(MigrationState *s)
065e2813 358{
065e2813
AL
359 if (s->state != MIG_STATE_ACTIVE)
360 return;
361
d0f2c4c6 362 DPRINTF("cancelling migration\n");
065e2813
AL
363
364 s->state = MIG_STATE_CANCELLED;
e0eb7390 365 notifier_list_notify(&migration_state_notifiers, s);
539de124 366 qemu_savevm_state_cancel(s->file);
065e2813
AL
367
368 migrate_fd_cleanup(s);
369}
370
9499743f 371int migrate_fd_wait_for_unfreeze(MigrationState *s)
065e2813 372{
065e2813
AL
373 int ret;
374
d0f2c4c6 375 DPRINTF("wait for unfreeze\n");
065e2813 376 if (s->state != MIG_STATE_ACTIVE)
9499743f 377 return -EINVAL;
065e2813
AL
378
379 do {
380 fd_set wfds;
381
382 FD_ZERO(&wfds);
383 FD_SET(s->fd, &wfds);
384
385 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
386 } while (ret == -1 && (s->get_error(s)) == EINTR);
af509450
JQ
387
388 if (ret == -1) {
9499743f 389 return -s->get_error(s);
af509450 390 }
9499743f 391 return 0;
065e2813
AL
392}
393
11c76741 394int migrate_fd_close(MigrationState *s)
065e2813 395{
e19252d3 396 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
397 return s->close(s);
398}
99a0db9b
GH
399
400void add_migration_state_change_notifier(Notifier *notify)
401{
402 notifier_list_add(&migration_state_notifiers, notify);
403}
404
405void remove_migration_state_change_notifier(Notifier *notify)
406{
31552529 407 notifier_remove(notify);
99a0db9b
GH
408}
409
afe2df69
GH
410bool migration_is_active(MigrationState *s)
411{
412 return s->state == MIG_STATE_ACTIVE;
413}
414
7073693b 415bool migration_has_finished(MigrationState *s)
99a0db9b 416{
7073693b 417 return s->state == MIG_STATE_COMPLETED;
99a0db9b 418}
0edda1c4 419
afe2df69
GH
420bool migration_has_failed(MigrationState *s)
421{
422 return (s->state == MIG_STATE_CANCELLED ||
423 s->state == MIG_STATE_ERROR);
424}
425
8b6b99b3 426void migrate_fd_connect(MigrationState *s)
99a0db9b 427{
8b6b99b3
JQ
428 int ret;
429
d5934dde 430 s->state = MIG_STATE_ACTIVE;
796b4b0f 431 s->file = qemu_fopen_ops_buffered(s);
8b6b99b3
JQ
432
433 DPRINTF("beginning savevm\n");
6607ae23 434 ret = qemu_savevm_state_begin(s->file, &s->params);
8b6b99b3
JQ
435 if (ret < 0) {
436 DPRINTF("failed, %d\n", ret);
437 migrate_fd_error(s);
438 return;
439 }
440 migrate_fd_put_ready(s);
441}
442
6607ae23 443static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 444{
17549e84 445 MigrationState *s = migrate_get_current();
d0ae46c1 446 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 447 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 448 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
bbf6da32
OW
449
450 memcpy(enabled_capabilities, s->enabled_capabilities,
451 sizeof(enabled_capabilities));
0edda1c4 452
17549e84 453 memset(s, 0, sizeof(*s));
d0ae46c1 454 s->bandwidth_limit = bandwidth_limit;
6607ae23 455 s->params = *params;
bbf6da32
OW
456 memcpy(s->enabled_capabilities, enabled_capabilities,
457 sizeof(enabled_capabilities));
17ad9b35 458 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 459
0edda1c4 460 s->bandwidth_limit = bandwidth_limit;
d5934dde 461 s->state = MIG_STATE_SETUP;
d5f8a570 462 s->total_time = qemu_get_clock_ms(rt_clock);
0edda1c4 463
0edda1c4
JQ
464 return s;
465}
cab30143 466
fa2756b7
AL
467static GSList *migration_blockers;
468
469void migrate_add_blocker(Error *reason)
470{
471 migration_blockers = g_slist_prepend(migration_blockers, reason);
472}
473
474void migrate_del_blocker(Error *reason)
475{
476 migration_blockers = g_slist_remove(migration_blockers, reason);
477}
478
e1c37d0e
LC
479void qmp_migrate(const char *uri, bool has_blk, bool blk,
480 bool has_inc, bool inc, bool has_detach, bool detach,
481 Error **errp)
cab30143 482{
be7059cd 483 Error *local_err = NULL;
17549e84 484 MigrationState *s = migrate_get_current();
6607ae23 485 MigrationParams params;
cab30143 486 const char *p;
cab30143 487
6607ae23
IY
488 params.blk = blk;
489 params.shared = inc;
490
17549e84 491 if (s->state == MIG_STATE_ACTIVE) {
e1c37d0e
LC
492 error_set(errp, QERR_MIGRATION_ACTIVE);
493 return;
cab30143
JQ
494 }
495
e1c37d0e
LC
496 if (qemu_savevm_state_blocked(errp)) {
497 return;
cab30143
JQ
498 }
499
fa2756b7 500 if (migration_blockers) {
e1c37d0e
LC
501 *errp = error_copy(migration_blockers->data);
502 return;
fa2756b7
AL
503 }
504
6607ae23 505 s = migrate_init(&params);
cab30143
JQ
506
507 if (strstart(uri, "tcp:", &p)) {
f37afb5a 508 tcp_start_outgoing_migration(s, p, &local_err);
cab30143
JQ
509#if !defined(WIN32)
510 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 511 exec_start_outgoing_migration(s, p, &local_err);
cab30143 512 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 513 unix_start_outgoing_migration(s, p, &local_err);
cab30143 514 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 515 fd_start_outgoing_migration(s, p, &local_err);
cab30143 516#endif
99a0db9b 517 } else {
e1c37d0e
LC
518 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
519 return;
cab30143
JQ
520 }
521
f37afb5a 522 if (local_err) {
342ab8d1 523 migrate_fd_error(s);
f37afb5a 524 error_propagate(errp, local_err);
e1c37d0e 525 return;
1299c631
JQ
526 }
527
e0eb7390 528 notifier_list_notify(&migration_state_notifiers, s);
cab30143
JQ
529}
530
6cdedb07 531void qmp_migrate_cancel(Error **errp)
cab30143 532{
17549e84 533 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
534}
535
9e1ba4cc
OW
536void qmp_migrate_set_cache_size(int64_t value, Error **errp)
537{
538 MigrationState *s = migrate_get_current();
539
540 /* Check for truncation */
541 if (value != (size_t)value) {
542 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
543 "exceeding address space");
544 return;
545 }
546
547 s->xbzrle_cache_size = xbzrle_cache_resize(value);
548}
549
550int64_t qmp_query_migrate_cache_size(Error **errp)
551{
552 return migrate_xbzrle_cache_size();
553}
554
3dc85383 555void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 556{
cab30143
JQ
557 MigrationState *s;
558
3dc85383
LC
559 if (value < 0) {
560 value = 0;
99a0db9b 561 }
cab30143 562
17549e84 563 s = migrate_get_current();
3dc85383 564 s->bandwidth_limit = value;
d0ae46c1 565 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
cab30143
JQ
566}
567
4f0a993b 568void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 569{
4f0a993b
LC
570 value *= 1e9;
571 value = MAX(0, MIN(UINT64_MAX, value));
572 max_downtime = (uint64_t)value;
99a0db9b 573}
17ad9b35
OW
574
575int migrate_use_xbzrle(void)
576{
577 MigrationState *s;
578
579 s = migrate_get_current();
580
581 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
582}
583
584int64_t migrate_xbzrle_cache_size(void)
585{
586 MigrationState *s;
587
588 s = migrate_get_current();
589
590 return s->xbzrle_cache_size;
591}