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