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