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