]> git.proxmox.com Git - qemu.git/blob - migration.c
vfio-pci: Implement PCI hot reset
[qemu.git] / migration.c
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 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
14 */
15
16 #include "qemu-common.h"
17 #include "qemu/main-loop.h"
18 #include "migration/migration.h"
19 #include "monitor/monitor.h"
20 #include "migration/qemu-file.h"
21 #include "sysemu/sysemu.h"
22 #include "block/block.h"
23 #include "qemu/sockets.h"
24 #include "migration/block.h"
25 #include "qemu/thread.h"
26 #include "qmp-commands.h"
27 #include "trace.h"
28
29 //#define DEBUG_MIGRATION
30
31 #ifdef DEBUG_MIGRATION
32 #define DPRINTF(fmt, ...) \
33 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
34 #else
35 #define DPRINTF(fmt, ...) \
36 do { } while (0)
37 #endif
38
39 enum {
40 MIG_STATE_ERROR = -1,
41 MIG_STATE_NONE,
42 MIG_STATE_SETUP,
43 MIG_STATE_CANCELLED,
44 MIG_STATE_ACTIVE,
45 MIG_STATE_COMPLETED,
46 };
47
48 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
49
50 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
51 * data. */
52 #define BUFFER_DELAY 100
53 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
54
55 /* Migration XBZRLE default cache size */
56 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
57
58 static NotifierList migration_state_notifiers =
59 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
60
61 /* When we add fault tolerance, we could have several
62 migrations at once. For now we don't need to add
63 dynamic creation of migration */
64
65 MigrationState *migrate_get_current(void)
66 {
67 static MigrationState current_migration = {
68 .state = MIG_STATE_NONE,
69 .bandwidth_limit = MAX_THROTTLE,
70 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
71 .mbps = -1,
72 };
73
74 return &current_migration;
75 }
76
77 void qemu_start_incoming_migration(const char *uri, Error **errp)
78 {
79 const char *p;
80
81 if (strstart(uri, "tcp:", &p))
82 tcp_start_incoming_migration(p, errp);
83 #ifdef CONFIG_RDMA
84 else if (strstart(uri, "x-rdma:", &p))
85 rdma_start_incoming_migration(p, errp);
86 #endif
87 #if !defined(WIN32)
88 else if (strstart(uri, "exec:", &p))
89 exec_start_incoming_migration(p, errp);
90 else if (strstart(uri, "unix:", &p))
91 unix_start_incoming_migration(p, errp);
92 else if (strstart(uri, "fd:", &p))
93 fd_start_incoming_migration(p, errp);
94 #endif
95 else {
96 error_setg(errp, "unknown migration protocol: %s", uri);
97 }
98 }
99
100 static void process_incoming_migration_co(void *opaque)
101 {
102 QEMUFile *f = opaque;
103 int ret;
104
105 ret = qemu_loadvm_state(f);
106 qemu_fclose(f);
107 if (ret < 0) {
108 fprintf(stderr, "load of migration failed\n");
109 exit(EXIT_FAILURE);
110 }
111 qemu_announce_self();
112 DPRINTF("successfully loaded vm state\n");
113
114 bdrv_clear_incoming_migration_all();
115 /* Make sure all file formats flush their mutable metadata */
116 bdrv_invalidate_cache_all();
117
118 if (autostart) {
119 vm_start();
120 } else {
121 runstate_set(RUN_STATE_PAUSED);
122 }
123 }
124
125 void process_incoming_migration(QEMUFile *f)
126 {
127 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
128 int fd = qemu_get_fd(f);
129
130 assert(fd != -1);
131 qemu_set_nonblock(fd);
132 qemu_coroutine_enter(co, f);
133 }
134
135 /* amount of nanoseconds we are willing to wait for migration to be down.
136 * the choice of nanoseconds is because it is the maximum resolution that
137 * get_clock() can achieve. It is an internal measure. All user-visible
138 * units must be in seconds */
139 static uint64_t max_downtime = 30000000;
140
141 uint64_t migrate_max_downtime(void)
142 {
143 return max_downtime;
144 }
145
146 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
147 {
148 MigrationCapabilityStatusList *head = NULL;
149 MigrationCapabilityStatusList *caps;
150 MigrationState *s = migrate_get_current();
151 int i;
152
153 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
154 if (head == NULL) {
155 head = g_malloc0(sizeof(*caps));
156 caps = head;
157 } else {
158 caps->next = g_malloc0(sizeof(*caps));
159 caps = caps->next;
160 }
161 caps->value =
162 g_malloc(sizeof(*caps->value));
163 caps->value->capability = i;
164 caps->value->state = s->enabled_capabilities[i];
165 }
166
167 return head;
168 }
169
170 static void get_xbzrle_cache_stats(MigrationInfo *info)
171 {
172 if (migrate_use_xbzrle()) {
173 info->has_xbzrle_cache = true;
174 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
175 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
176 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
177 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
178 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
179 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
180 }
181 }
182
183 MigrationInfo *qmp_query_migrate(Error **errp)
184 {
185 MigrationInfo *info = g_malloc0(sizeof(*info));
186 MigrationState *s = migrate_get_current();
187
188 switch (s->state) {
189 case MIG_STATE_NONE:
190 /* no migration has happened ever */
191 break;
192 case MIG_STATE_SETUP:
193 info->has_status = true;
194 info->status = g_strdup("setup");
195 info->has_total_time = false;
196 break;
197 case MIG_STATE_ACTIVE:
198 info->has_status = true;
199 info->status = g_strdup("active");
200 info->has_total_time = true;
201 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
202 - s->total_time;
203 info->has_expected_downtime = true;
204 info->expected_downtime = s->expected_downtime;
205 info->has_setup_time = true;
206 info->setup_time = s->setup_time;
207
208 info->has_ram = true;
209 info->ram = g_malloc0(sizeof(*info->ram));
210 info->ram->transferred = ram_bytes_transferred();
211 info->ram->remaining = ram_bytes_remaining();
212 info->ram->total = ram_bytes_total();
213 info->ram->duplicate = dup_mig_pages_transferred();
214 info->ram->skipped = skipped_mig_pages_transferred();
215 info->ram->normal = norm_mig_pages_transferred();
216 info->ram->normal_bytes = norm_mig_bytes_transferred();
217 info->ram->dirty_pages_rate = s->dirty_pages_rate;
218 info->ram->mbps = s->mbps;
219
220 if (blk_mig_active()) {
221 info->has_disk = true;
222 info->disk = g_malloc0(sizeof(*info->disk));
223 info->disk->transferred = blk_mig_bytes_transferred();
224 info->disk->remaining = blk_mig_bytes_remaining();
225 info->disk->total = blk_mig_bytes_total();
226 }
227
228 get_xbzrle_cache_stats(info);
229 break;
230 case MIG_STATE_COMPLETED:
231 get_xbzrle_cache_stats(info);
232
233 info->has_status = true;
234 info->status = g_strdup("completed");
235 info->has_total_time = true;
236 info->total_time = s->total_time;
237 info->has_downtime = true;
238 info->downtime = s->downtime;
239 info->has_setup_time = true;
240 info->setup_time = s->setup_time;
241
242 info->has_ram = true;
243 info->ram = g_malloc0(sizeof(*info->ram));
244 info->ram->transferred = ram_bytes_transferred();
245 info->ram->remaining = 0;
246 info->ram->total = ram_bytes_total();
247 info->ram->duplicate = dup_mig_pages_transferred();
248 info->ram->skipped = skipped_mig_pages_transferred();
249 info->ram->normal = norm_mig_pages_transferred();
250 info->ram->normal_bytes = norm_mig_bytes_transferred();
251 info->ram->mbps = s->mbps;
252 break;
253 case MIG_STATE_ERROR:
254 info->has_status = true;
255 info->status = g_strdup("failed");
256 break;
257 case MIG_STATE_CANCELLED:
258 info->has_status = true;
259 info->status = g_strdup("cancelled");
260 break;
261 }
262
263 return info;
264 }
265
266 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
267 Error **errp)
268 {
269 MigrationState *s = migrate_get_current();
270 MigrationCapabilityStatusList *cap;
271
272 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
273 error_set(errp, QERR_MIGRATION_ACTIVE);
274 return;
275 }
276
277 for (cap = params; cap; cap = cap->next) {
278 s->enabled_capabilities[cap->value->capability] = cap->value->state;
279 }
280 }
281
282 /* shared migration helpers */
283
284 static void migrate_fd_cleanup(void *opaque)
285 {
286 MigrationState *s = opaque;
287
288 qemu_bh_delete(s->cleanup_bh);
289 s->cleanup_bh = NULL;
290
291 if (s->file) {
292 DPRINTF("closing file\n");
293 qemu_mutex_unlock_iothread();
294 qemu_thread_join(&s->thread);
295 qemu_mutex_lock_iothread();
296
297 qemu_fclose(s->file);
298 s->file = NULL;
299 }
300
301 assert(s->state != MIG_STATE_ACTIVE);
302
303 if (s->state != MIG_STATE_COMPLETED) {
304 qemu_savevm_state_cancel();
305 }
306
307 notifier_list_notify(&migration_state_notifiers, s);
308 }
309
310 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
311 {
312 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
313 trace_migrate_set_state(new_state);
314 }
315 }
316
317 void migrate_fd_error(MigrationState *s)
318 {
319 DPRINTF("setting error state\n");
320 assert(s->file == NULL);
321 s->state = MIG_STATE_ERROR;
322 trace_migrate_set_state(MIG_STATE_ERROR);
323 notifier_list_notify(&migration_state_notifiers, s);
324 }
325
326 static void migrate_fd_cancel(MigrationState *s)
327 {
328 DPRINTF("cancelling migration\n");
329
330 migrate_set_state(s, s->state, MIG_STATE_CANCELLED);
331 }
332
333 void add_migration_state_change_notifier(Notifier *notify)
334 {
335 notifier_list_add(&migration_state_notifiers, notify);
336 }
337
338 void remove_migration_state_change_notifier(Notifier *notify)
339 {
340 notifier_remove(notify);
341 }
342
343 bool migration_in_setup(MigrationState *s)
344 {
345 return s->state == MIG_STATE_SETUP;
346 }
347
348 bool migration_has_finished(MigrationState *s)
349 {
350 return s->state == MIG_STATE_COMPLETED;
351 }
352
353 bool migration_has_failed(MigrationState *s)
354 {
355 return (s->state == MIG_STATE_CANCELLED ||
356 s->state == MIG_STATE_ERROR);
357 }
358
359 static MigrationState *migrate_init(const MigrationParams *params)
360 {
361 MigrationState *s = migrate_get_current();
362 int64_t bandwidth_limit = s->bandwidth_limit;
363 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
364 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
365
366 memcpy(enabled_capabilities, s->enabled_capabilities,
367 sizeof(enabled_capabilities));
368
369 memset(s, 0, sizeof(*s));
370 s->params = *params;
371 memcpy(s->enabled_capabilities, enabled_capabilities,
372 sizeof(enabled_capabilities));
373 s->xbzrle_cache_size = xbzrle_cache_size;
374
375 s->bandwidth_limit = bandwidth_limit;
376 s->state = MIG_STATE_SETUP;
377 trace_migrate_set_state(MIG_STATE_SETUP);
378
379 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
380 return s;
381 }
382
383 static GSList *migration_blockers;
384
385 void migrate_add_blocker(Error *reason)
386 {
387 migration_blockers = g_slist_prepend(migration_blockers, reason);
388 }
389
390 void migrate_del_blocker(Error *reason)
391 {
392 migration_blockers = g_slist_remove(migration_blockers, reason);
393 }
394
395 void qmp_migrate(const char *uri, bool has_blk, bool blk,
396 bool has_inc, bool inc, bool has_detach, bool detach,
397 Error **errp)
398 {
399 Error *local_err = NULL;
400 MigrationState *s = migrate_get_current();
401 MigrationParams params;
402 const char *p;
403
404 params.blk = has_blk && blk;
405 params.shared = has_inc && inc;
406
407 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
408 error_set(errp, QERR_MIGRATION_ACTIVE);
409 return;
410 }
411
412 if (qemu_savevm_state_blocked(errp)) {
413 return;
414 }
415
416 if (migration_blockers) {
417 *errp = error_copy(migration_blockers->data);
418 return;
419 }
420
421 s = migrate_init(&params);
422
423 if (strstart(uri, "tcp:", &p)) {
424 tcp_start_outgoing_migration(s, p, &local_err);
425 #ifdef CONFIG_RDMA
426 } else if (strstart(uri, "x-rdma:", &p)) {
427 rdma_start_outgoing_migration(s, p, &local_err);
428 #endif
429 #if !defined(WIN32)
430 } else if (strstart(uri, "exec:", &p)) {
431 exec_start_outgoing_migration(s, p, &local_err);
432 } else if (strstart(uri, "unix:", &p)) {
433 unix_start_outgoing_migration(s, p, &local_err);
434 } else if (strstart(uri, "fd:", &p)) {
435 fd_start_outgoing_migration(s, p, &local_err);
436 #endif
437 } else {
438 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
439 return;
440 }
441
442 if (local_err) {
443 migrate_fd_error(s);
444 error_propagate(errp, local_err);
445 return;
446 }
447 }
448
449 void qmp_migrate_cancel(Error **errp)
450 {
451 migrate_fd_cancel(migrate_get_current());
452 }
453
454 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
455 {
456 MigrationState *s = migrate_get_current();
457
458 /* Check for truncation */
459 if (value != (size_t)value) {
460 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
461 "exceeding address space");
462 return;
463 }
464
465 s->xbzrle_cache_size = xbzrle_cache_resize(value);
466 }
467
468 int64_t qmp_query_migrate_cache_size(Error **errp)
469 {
470 return migrate_xbzrle_cache_size();
471 }
472
473 void qmp_migrate_set_speed(int64_t value, Error **errp)
474 {
475 MigrationState *s;
476
477 if (value < 0) {
478 value = 0;
479 }
480 if (value > SIZE_MAX) {
481 value = SIZE_MAX;
482 }
483
484 s = migrate_get_current();
485 s->bandwidth_limit = value;
486 if (s->file) {
487 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
488 }
489 }
490
491 void qmp_migrate_set_downtime(double value, Error **errp)
492 {
493 value *= 1e9;
494 value = MAX(0, MIN(UINT64_MAX, value));
495 max_downtime = (uint64_t)value;
496 }
497
498 bool migrate_rdma_pin_all(void)
499 {
500 MigrationState *s;
501
502 s = migrate_get_current();
503
504 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL];
505 }
506
507 bool migrate_auto_converge(void)
508 {
509 MigrationState *s;
510
511 s = migrate_get_current();
512
513 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
514 }
515
516 bool migrate_zero_blocks(void)
517 {
518 MigrationState *s;
519
520 s = migrate_get_current();
521
522 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
523 }
524
525 int migrate_use_xbzrle(void)
526 {
527 MigrationState *s;
528
529 s = migrate_get_current();
530
531 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
532 }
533
534 int64_t migrate_xbzrle_cache_size(void)
535 {
536 MigrationState *s;
537
538 s = migrate_get_current();
539
540 return s->xbzrle_cache_size;
541 }
542
543 /* migration thread support */
544
545 static void *migration_thread(void *opaque)
546 {
547 MigrationState *s = opaque;
548 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
549 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
550 int64_t initial_bytes = 0;
551 int64_t max_size = 0;
552 int64_t start_time = initial_time;
553 bool old_vm_running = false;
554
555 DPRINTF("beginning savevm\n");
556 qemu_savevm_state_begin(s->file, &s->params);
557
558 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
559 migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
560
561 DPRINTF("setup complete\n");
562
563 while (s->state == MIG_STATE_ACTIVE) {
564 int64_t current_time;
565 uint64_t pending_size;
566
567 if (!qemu_file_rate_limit(s->file)) {
568 DPRINTF("iterate\n");
569 pending_size = qemu_savevm_state_pending(s->file, max_size);
570 DPRINTF("pending size %" PRIu64 " max %" PRIu64 "\n",
571 pending_size, max_size);
572 if (pending_size && pending_size >= max_size) {
573 qemu_savevm_state_iterate(s->file);
574 } else {
575 int ret;
576
577 DPRINTF("done iterating\n");
578 qemu_mutex_lock_iothread();
579 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
580 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
581 old_vm_running = runstate_is_running();
582
583 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
584 if (ret >= 0) {
585 qemu_file_set_rate_limit(s->file, INT_MAX);
586 qemu_savevm_state_complete(s->file);
587 }
588 qemu_mutex_unlock_iothread();
589
590 if (ret < 0) {
591 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
592 break;
593 }
594
595 if (!qemu_file_get_error(s->file)) {
596 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
597 break;
598 }
599 }
600 }
601
602 if (qemu_file_get_error(s->file)) {
603 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
604 break;
605 }
606 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
607 if (current_time >= initial_time + BUFFER_DELAY) {
608 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
609 uint64_t time_spent = current_time - initial_time;
610 double bandwidth = transferred_bytes / time_spent;
611 max_size = bandwidth * migrate_max_downtime() / 1000000;
612
613 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
614 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
615
616 DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
617 " bandwidth %g max_size %" PRId64 "\n",
618 transferred_bytes, time_spent, bandwidth, max_size);
619 /* if we haven't sent anything, we don't want to recalculate
620 10000 is a small enough number for our purposes */
621 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
622 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
623 }
624
625 qemu_file_reset_rate_limit(s->file);
626 initial_time = current_time;
627 initial_bytes = qemu_ftell(s->file);
628 }
629 if (qemu_file_rate_limit(s->file)) {
630 /* usleep expects microseconds */
631 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
632 }
633 }
634
635 qemu_mutex_lock_iothread();
636 if (s->state == MIG_STATE_COMPLETED) {
637 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
638 s->total_time = end_time - s->total_time;
639 s->downtime = end_time - start_time;
640 runstate_set(RUN_STATE_POSTMIGRATE);
641 } else {
642 if (old_vm_running) {
643 vm_start();
644 }
645 }
646 qemu_bh_schedule(s->cleanup_bh);
647 qemu_mutex_unlock_iothread();
648
649 return NULL;
650 }
651
652 void migrate_fd_connect(MigrationState *s)
653 {
654 s->state = MIG_STATE_SETUP;
655 trace_migrate_set_state(MIG_STATE_SETUP);
656
657 /* This is a best 1st approximation. ns to ms */
658 s->expected_downtime = max_downtime/1000000;
659 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
660
661 qemu_file_set_rate_limit(s->file,
662 s->bandwidth_limit / XFER_LIMIT_RATIO);
663
664 /* Notify before starting migration thread */
665 notifier_list_notify(&migration_state_notifiers, s);
666
667 qemu_thread_create(&s->thread, migration_thread, s,
668 QEMU_THREAD_JOINABLE);
669 }