]> git.proxmox.com Git - mirror_qemu.git/blob - migration/migration.c
qerror: Move #include out of qerror.h
[mirror_qemu.git] / migration / 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/error-report.h"
18 #include "qemu/main-loop.h"
19 #include "migration/migration.h"
20 #include "monitor/monitor.h"
21 #include "migration/qemu-file.h"
22 #include "sysemu/sysemu.h"
23 #include "block/block.h"
24 #include "qemu/sockets.h"
25 #include "migration/block.h"
26 #include "qemu/thread.h"
27 #include "qmp-commands.h"
28 #include "trace.h"
29
30 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
31
32 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
33 * data. */
34 #define BUFFER_DELAY 100
35 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
36
37 /* Default compression thread count */
38 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
39 /* Default decompression thread count, usually decompression is at
40 * least 4 times as fast as compression.*/
41 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
42 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
43 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
44
45 /* Migration XBZRLE default cache size */
46 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
47
48 static NotifierList migration_state_notifiers =
49 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
50
51 static bool deferred_incoming;
52
53 /* When we add fault tolerance, we could have several
54 migrations at once. For now we don't need to add
55 dynamic creation of migration */
56
57 /* For outgoing */
58 MigrationState *migrate_get_current(void)
59 {
60 static MigrationState current_migration = {
61 .state = MIGRATION_STATUS_NONE,
62 .bandwidth_limit = MAX_THROTTLE,
63 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
64 .mbps = -1,
65 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
66 DEFAULT_MIGRATE_COMPRESS_LEVEL,
67 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
68 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
69 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
70 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
71 };
72
73 return &current_migration;
74 }
75
76 /* For incoming */
77 static MigrationIncomingState *mis_current;
78
79 MigrationIncomingState *migration_incoming_get_current(void)
80 {
81 return mis_current;
82 }
83
84 MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
85 {
86 mis_current = g_malloc0(sizeof(MigrationIncomingState));
87 mis_current->file = f;
88 QLIST_INIT(&mis_current->loadvm_handlers);
89
90 return mis_current;
91 }
92
93 void migration_incoming_state_destroy(void)
94 {
95 loadvm_free_handlers(mis_current);
96 g_free(mis_current);
97 mis_current = NULL;
98 }
99
100 /*
101 * Called on -incoming with a defer: uri.
102 * The migration can be started later after any parameters have been
103 * changed.
104 */
105 static void deferred_incoming_migration(Error **errp)
106 {
107 if (deferred_incoming) {
108 error_setg(errp, "Incoming migration already deferred");
109 }
110 deferred_incoming = true;
111 }
112
113 void qemu_start_incoming_migration(const char *uri, Error **errp)
114 {
115 const char *p;
116
117 if (!strcmp(uri, "defer")) {
118 deferred_incoming_migration(errp);
119 } else if (strstart(uri, "tcp:", &p)) {
120 tcp_start_incoming_migration(p, errp);
121 #ifdef CONFIG_RDMA
122 } else if (strstart(uri, "rdma:", &p)) {
123 rdma_start_incoming_migration(p, errp);
124 #endif
125 #if !defined(WIN32)
126 } else if (strstart(uri, "exec:", &p)) {
127 exec_start_incoming_migration(p, errp);
128 } else if (strstart(uri, "unix:", &p)) {
129 unix_start_incoming_migration(p, errp);
130 } else if (strstart(uri, "fd:", &p)) {
131 fd_start_incoming_migration(p, errp);
132 #endif
133 } else {
134 error_setg(errp, "unknown migration protocol: %s", uri);
135 }
136 }
137
138 static void process_incoming_migration_co(void *opaque)
139 {
140 QEMUFile *f = opaque;
141 Error *local_err = NULL;
142 int ret;
143
144 migration_incoming_state_new(f);
145
146 ret = qemu_loadvm_state(f);
147
148 qemu_fclose(f);
149 free_xbzrle_decoded_buf();
150 migration_incoming_state_destroy();
151
152 if (ret < 0) {
153 error_report("load of migration failed: %s", strerror(-ret));
154 migrate_decompress_threads_join();
155 exit(EXIT_FAILURE);
156 }
157 qemu_announce_self();
158
159 /* Make sure all file formats flush their mutable metadata */
160 bdrv_invalidate_cache_all(&local_err);
161 if (local_err) {
162 error_report_err(local_err);
163 migrate_decompress_threads_join();
164 exit(EXIT_FAILURE);
165 }
166
167 if (autostart) {
168 vm_start();
169 } else {
170 runstate_set(RUN_STATE_PAUSED);
171 }
172 migrate_decompress_threads_join();
173 }
174
175 void process_incoming_migration(QEMUFile *f)
176 {
177 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
178 int fd = qemu_get_fd(f);
179
180 assert(fd != -1);
181 migrate_decompress_threads_create();
182 qemu_set_nonblock(fd);
183 qemu_coroutine_enter(co, f);
184 }
185
186 /* amount of nanoseconds we are willing to wait for migration to be down.
187 * the choice of nanoseconds is because it is the maximum resolution that
188 * get_clock() can achieve. It is an internal measure. All user-visible
189 * units must be in seconds */
190 static uint64_t max_downtime = 300000000;
191
192 uint64_t migrate_max_downtime(void)
193 {
194 return max_downtime;
195 }
196
197 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
198 {
199 MigrationCapabilityStatusList *head = NULL;
200 MigrationCapabilityStatusList *caps;
201 MigrationState *s = migrate_get_current();
202 int i;
203
204 caps = NULL; /* silence compiler warning */
205 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
206 if (head == NULL) {
207 head = g_malloc0(sizeof(*caps));
208 caps = head;
209 } else {
210 caps->next = g_malloc0(sizeof(*caps));
211 caps = caps->next;
212 }
213 caps->value =
214 g_malloc(sizeof(*caps->value));
215 caps->value->capability = i;
216 caps->value->state = s->enabled_capabilities[i];
217 }
218
219 return head;
220 }
221
222 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
223 {
224 MigrationParameters *params;
225 MigrationState *s = migrate_get_current();
226
227 params = g_malloc0(sizeof(*params));
228 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
229 params->compress_threads =
230 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
231 params->decompress_threads =
232 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
233
234 return params;
235 }
236
237 static void get_xbzrle_cache_stats(MigrationInfo *info)
238 {
239 if (migrate_use_xbzrle()) {
240 info->has_xbzrle_cache = true;
241 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
242 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
243 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
244 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
245 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
246 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
247 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
248 }
249 }
250
251 MigrationInfo *qmp_query_migrate(Error **errp)
252 {
253 MigrationInfo *info = g_malloc0(sizeof(*info));
254 MigrationState *s = migrate_get_current();
255
256 switch (s->state) {
257 case MIGRATION_STATUS_NONE:
258 /* no migration has happened ever */
259 break;
260 case MIGRATION_STATUS_SETUP:
261 info->has_status = true;
262 info->has_total_time = false;
263 break;
264 case MIGRATION_STATUS_ACTIVE:
265 case MIGRATION_STATUS_CANCELLING:
266 info->has_status = true;
267 info->has_total_time = true;
268 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
269 - s->total_time;
270 info->has_expected_downtime = true;
271 info->expected_downtime = s->expected_downtime;
272 info->has_setup_time = true;
273 info->setup_time = s->setup_time;
274
275 info->has_ram = true;
276 info->ram = g_malloc0(sizeof(*info->ram));
277 info->ram->transferred = ram_bytes_transferred();
278 info->ram->remaining = ram_bytes_remaining();
279 info->ram->total = ram_bytes_total();
280 info->ram->duplicate = dup_mig_pages_transferred();
281 info->ram->skipped = skipped_mig_pages_transferred();
282 info->ram->normal = norm_mig_pages_transferred();
283 info->ram->normal_bytes = norm_mig_bytes_transferred();
284 info->ram->dirty_pages_rate = s->dirty_pages_rate;
285 info->ram->mbps = s->mbps;
286 info->ram->dirty_sync_count = s->dirty_sync_count;
287
288 if (blk_mig_active()) {
289 info->has_disk = true;
290 info->disk = g_malloc0(sizeof(*info->disk));
291 info->disk->transferred = blk_mig_bytes_transferred();
292 info->disk->remaining = blk_mig_bytes_remaining();
293 info->disk->total = blk_mig_bytes_total();
294 }
295
296 get_xbzrle_cache_stats(info);
297 break;
298 case MIGRATION_STATUS_COMPLETED:
299 get_xbzrle_cache_stats(info);
300
301 info->has_status = true;
302 info->has_total_time = true;
303 info->total_time = s->total_time;
304 info->has_downtime = true;
305 info->downtime = s->downtime;
306 info->has_setup_time = true;
307 info->setup_time = s->setup_time;
308
309 info->has_ram = true;
310 info->ram = g_malloc0(sizeof(*info->ram));
311 info->ram->transferred = ram_bytes_transferred();
312 info->ram->remaining = 0;
313 info->ram->total = ram_bytes_total();
314 info->ram->duplicate = dup_mig_pages_transferred();
315 info->ram->skipped = skipped_mig_pages_transferred();
316 info->ram->normal = norm_mig_pages_transferred();
317 info->ram->normal_bytes = norm_mig_bytes_transferred();
318 info->ram->mbps = s->mbps;
319 info->ram->dirty_sync_count = s->dirty_sync_count;
320 break;
321 case MIGRATION_STATUS_FAILED:
322 info->has_status = true;
323 break;
324 case MIGRATION_STATUS_CANCELLED:
325 info->has_status = true;
326 break;
327 }
328 info->status = s->state;
329
330 return info;
331 }
332
333 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
334 Error **errp)
335 {
336 MigrationState *s = migrate_get_current();
337 MigrationCapabilityStatusList *cap;
338
339 if (s->state == MIGRATION_STATUS_ACTIVE ||
340 s->state == MIGRATION_STATUS_SETUP) {
341 error_setg(errp, QERR_MIGRATION_ACTIVE);
342 return;
343 }
344
345 for (cap = params; cap; cap = cap->next) {
346 s->enabled_capabilities[cap->value->capability] = cap->value->state;
347 }
348 }
349
350 void qmp_migrate_set_parameters(bool has_compress_level,
351 int64_t compress_level,
352 bool has_compress_threads,
353 int64_t compress_threads,
354 bool has_decompress_threads,
355 int64_t decompress_threads, Error **errp)
356 {
357 MigrationState *s = migrate_get_current();
358
359 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
360 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
361 "is invalid, it should be in the range of 0 to 9");
362 return;
363 }
364 if (has_compress_threads &&
365 (compress_threads < 1 || compress_threads > 255)) {
366 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
367 "compress_threads",
368 "is invalid, it should be in the range of 1 to 255");
369 return;
370 }
371 if (has_decompress_threads &&
372 (decompress_threads < 1 || decompress_threads > 255)) {
373 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
374 "decompress_threads",
375 "is invalid, it should be in the range of 1 to 255");
376 return;
377 }
378
379 if (has_compress_level) {
380 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
381 }
382 if (has_compress_threads) {
383 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
384 }
385 if (has_decompress_threads) {
386 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
387 decompress_threads;
388 }
389 }
390
391 /* shared migration helpers */
392
393 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
394 {
395 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
396 trace_migrate_set_state(new_state);
397 }
398 }
399
400 static void migrate_fd_cleanup(void *opaque)
401 {
402 MigrationState *s = opaque;
403
404 qemu_bh_delete(s->cleanup_bh);
405 s->cleanup_bh = NULL;
406
407 if (s->file) {
408 trace_migrate_fd_cleanup();
409 qemu_mutex_unlock_iothread();
410 qemu_thread_join(&s->thread);
411 qemu_mutex_lock_iothread();
412
413 migrate_compress_threads_join();
414 qemu_fclose(s->file);
415 s->file = NULL;
416 }
417
418 assert(s->state != MIGRATION_STATUS_ACTIVE);
419
420 if (s->state != MIGRATION_STATUS_COMPLETED) {
421 qemu_savevm_state_cancel();
422 if (s->state == MIGRATION_STATUS_CANCELLING) {
423 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
424 MIGRATION_STATUS_CANCELLED);
425 }
426 }
427
428 notifier_list_notify(&migration_state_notifiers, s);
429 }
430
431 void migrate_fd_error(MigrationState *s)
432 {
433 trace_migrate_fd_error();
434 assert(s->file == NULL);
435 s->state = MIGRATION_STATUS_FAILED;
436 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
437 notifier_list_notify(&migration_state_notifiers, s);
438 }
439
440 static void migrate_fd_cancel(MigrationState *s)
441 {
442 int old_state ;
443 QEMUFile *f = migrate_get_current()->file;
444 trace_migrate_fd_cancel();
445
446 do {
447 old_state = s->state;
448 if (old_state != MIGRATION_STATUS_SETUP &&
449 old_state != MIGRATION_STATUS_ACTIVE) {
450 break;
451 }
452 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
453 } while (s->state != MIGRATION_STATUS_CANCELLING);
454
455 /*
456 * If we're unlucky the migration code might be stuck somewhere in a
457 * send/write while the network has failed and is waiting to timeout;
458 * if we've got shutdown(2) available then we can force it to quit.
459 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
460 * called in a bh, so there is no race against this cancel.
461 */
462 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
463 qemu_file_shutdown(f);
464 }
465 }
466
467 void add_migration_state_change_notifier(Notifier *notify)
468 {
469 notifier_list_add(&migration_state_notifiers, notify);
470 }
471
472 void remove_migration_state_change_notifier(Notifier *notify)
473 {
474 notifier_remove(notify);
475 }
476
477 bool migration_in_setup(MigrationState *s)
478 {
479 return s->state == MIGRATION_STATUS_SETUP;
480 }
481
482 bool migration_has_finished(MigrationState *s)
483 {
484 return s->state == MIGRATION_STATUS_COMPLETED;
485 }
486
487 bool migration_has_failed(MigrationState *s)
488 {
489 return (s->state == MIGRATION_STATUS_CANCELLED ||
490 s->state == MIGRATION_STATUS_FAILED);
491 }
492
493 static MigrationState *migrate_init(const MigrationParams *params)
494 {
495 MigrationState *s = migrate_get_current();
496 int64_t bandwidth_limit = s->bandwidth_limit;
497 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
498 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
499 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
500 int compress_thread_count =
501 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
502 int decompress_thread_count =
503 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
504
505 memcpy(enabled_capabilities, s->enabled_capabilities,
506 sizeof(enabled_capabilities));
507
508 memset(s, 0, sizeof(*s));
509 s->params = *params;
510 memcpy(s->enabled_capabilities, enabled_capabilities,
511 sizeof(enabled_capabilities));
512 s->xbzrle_cache_size = xbzrle_cache_size;
513
514 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
515 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
516 compress_thread_count;
517 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
518 decompress_thread_count;
519 s->bandwidth_limit = bandwidth_limit;
520 s->state = MIGRATION_STATUS_SETUP;
521 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
522
523 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
524 return s;
525 }
526
527 static GSList *migration_blockers;
528
529 void migrate_add_blocker(Error *reason)
530 {
531 migration_blockers = g_slist_prepend(migration_blockers, reason);
532 }
533
534 void migrate_del_blocker(Error *reason)
535 {
536 migration_blockers = g_slist_remove(migration_blockers, reason);
537 }
538
539 void qmp_migrate_incoming(const char *uri, Error **errp)
540 {
541 Error *local_err = NULL;
542 static bool once = true;
543
544 if (!deferred_incoming) {
545 error_setg(errp, "For use with '-incoming defer'");
546 return;
547 }
548 if (!once) {
549 error_setg(errp, "The incoming migration has already been started");
550 }
551
552 qemu_start_incoming_migration(uri, &local_err);
553
554 if (local_err) {
555 error_propagate(errp, local_err);
556 return;
557 }
558
559 once = false;
560 }
561
562 void qmp_migrate(const char *uri, bool has_blk, bool blk,
563 bool has_inc, bool inc, bool has_detach, bool detach,
564 Error **errp)
565 {
566 Error *local_err = NULL;
567 MigrationState *s = migrate_get_current();
568 MigrationParams params;
569 const char *p;
570
571 params.blk = has_blk && blk;
572 params.shared = has_inc && inc;
573
574 if (s->state == MIGRATION_STATUS_ACTIVE ||
575 s->state == MIGRATION_STATUS_SETUP ||
576 s->state == MIGRATION_STATUS_CANCELLING) {
577 error_setg(errp, QERR_MIGRATION_ACTIVE);
578 return;
579 }
580
581 if (runstate_check(RUN_STATE_INMIGRATE)) {
582 error_setg(errp, "Guest is waiting for an incoming migration");
583 return;
584 }
585
586 if (qemu_savevm_state_blocked(errp)) {
587 return;
588 }
589
590 if (migration_blockers) {
591 *errp = error_copy(migration_blockers->data);
592 return;
593 }
594
595 s = migrate_init(&params);
596
597 if (strstart(uri, "tcp:", &p)) {
598 tcp_start_outgoing_migration(s, p, &local_err);
599 #ifdef CONFIG_RDMA
600 } else if (strstart(uri, "rdma:", &p)) {
601 rdma_start_outgoing_migration(s, p, &local_err);
602 #endif
603 #if !defined(WIN32)
604 } else if (strstart(uri, "exec:", &p)) {
605 exec_start_outgoing_migration(s, p, &local_err);
606 } else if (strstart(uri, "unix:", &p)) {
607 unix_start_outgoing_migration(s, p, &local_err);
608 } else if (strstart(uri, "fd:", &p)) {
609 fd_start_outgoing_migration(s, p, &local_err);
610 #endif
611 } else {
612 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
613 "a valid migration protocol");
614 s->state = MIGRATION_STATUS_FAILED;
615 return;
616 }
617
618 if (local_err) {
619 migrate_fd_error(s);
620 error_propagate(errp, local_err);
621 return;
622 }
623 }
624
625 void qmp_migrate_cancel(Error **errp)
626 {
627 migrate_fd_cancel(migrate_get_current());
628 }
629
630 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
631 {
632 MigrationState *s = migrate_get_current();
633 int64_t new_size;
634
635 /* Check for truncation */
636 if (value != (size_t)value) {
637 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
638 "exceeding address space");
639 return;
640 }
641
642 /* Cache should not be larger than guest ram size */
643 if (value > ram_bytes_total()) {
644 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
645 "exceeds guest ram size ");
646 return;
647 }
648
649 new_size = xbzrle_cache_resize(value);
650 if (new_size < 0) {
651 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
652 "is smaller than page size");
653 return;
654 }
655
656 s->xbzrle_cache_size = new_size;
657 }
658
659 int64_t qmp_query_migrate_cache_size(Error **errp)
660 {
661 return migrate_xbzrle_cache_size();
662 }
663
664 void qmp_migrate_set_speed(int64_t value, Error **errp)
665 {
666 MigrationState *s;
667
668 if (value < 0) {
669 value = 0;
670 }
671 if (value > SIZE_MAX) {
672 value = SIZE_MAX;
673 }
674
675 s = migrate_get_current();
676 s->bandwidth_limit = value;
677 if (s->file) {
678 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
679 }
680 }
681
682 void qmp_migrate_set_downtime(double value, Error **errp)
683 {
684 value *= 1e9;
685 value = MAX(0, MIN(UINT64_MAX, value));
686 max_downtime = (uint64_t)value;
687 }
688
689 bool migrate_auto_converge(void)
690 {
691 MigrationState *s;
692
693 s = migrate_get_current();
694
695 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
696 }
697
698 bool migrate_zero_blocks(void)
699 {
700 MigrationState *s;
701
702 s = migrate_get_current();
703
704 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
705 }
706
707 bool migrate_use_compression(void)
708 {
709 MigrationState *s;
710
711 s = migrate_get_current();
712
713 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
714 }
715
716 int migrate_compress_level(void)
717 {
718 MigrationState *s;
719
720 s = migrate_get_current();
721
722 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
723 }
724
725 int migrate_compress_threads(void)
726 {
727 MigrationState *s;
728
729 s = migrate_get_current();
730
731 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
732 }
733
734 int migrate_decompress_threads(void)
735 {
736 MigrationState *s;
737
738 s = migrate_get_current();
739
740 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
741 }
742
743 int migrate_use_xbzrle(void)
744 {
745 MigrationState *s;
746
747 s = migrate_get_current();
748
749 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
750 }
751
752 int64_t migrate_xbzrle_cache_size(void)
753 {
754 MigrationState *s;
755
756 s = migrate_get_current();
757
758 return s->xbzrle_cache_size;
759 }
760
761 /* migration thread support */
762
763 static void *migration_thread(void *opaque)
764 {
765 MigrationState *s = opaque;
766 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
767 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
768 int64_t initial_bytes = 0;
769 int64_t max_size = 0;
770 int64_t start_time = initial_time;
771 bool old_vm_running = false;
772
773 qemu_savevm_state_header(s->file);
774 qemu_savevm_state_begin(s->file, &s->params);
775
776 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
777 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
778
779 while (s->state == MIGRATION_STATUS_ACTIVE) {
780 int64_t current_time;
781 uint64_t pending_size;
782
783 if (!qemu_file_rate_limit(s->file)) {
784 pending_size = qemu_savevm_state_pending(s->file, max_size);
785 trace_migrate_pending(pending_size, max_size);
786 if (pending_size && pending_size >= max_size) {
787 qemu_savevm_state_iterate(s->file);
788 } else {
789 int ret;
790
791 qemu_mutex_lock_iothread();
792 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
793 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
794 old_vm_running = runstate_is_running();
795
796 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
797 if (ret >= 0) {
798 qemu_file_set_rate_limit(s->file, INT64_MAX);
799 qemu_savevm_state_complete(s->file);
800 }
801 qemu_mutex_unlock_iothread();
802
803 if (ret < 0) {
804 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
805 MIGRATION_STATUS_FAILED);
806 break;
807 }
808
809 if (!qemu_file_get_error(s->file)) {
810 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
811 MIGRATION_STATUS_COMPLETED);
812 break;
813 }
814 }
815 }
816
817 if (qemu_file_get_error(s->file)) {
818 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
819 MIGRATION_STATUS_FAILED);
820 break;
821 }
822 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
823 if (current_time >= initial_time + BUFFER_DELAY) {
824 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
825 uint64_t time_spent = current_time - initial_time;
826 double bandwidth = transferred_bytes / time_spent;
827 max_size = bandwidth * migrate_max_downtime() / 1000000;
828
829 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
830 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
831
832 trace_migrate_transferred(transferred_bytes, time_spent,
833 bandwidth, max_size);
834 /* if we haven't sent anything, we don't want to recalculate
835 10000 is a small enough number for our purposes */
836 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
837 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
838 }
839
840 qemu_file_reset_rate_limit(s->file);
841 initial_time = current_time;
842 initial_bytes = qemu_ftell(s->file);
843 }
844 if (qemu_file_rate_limit(s->file)) {
845 /* usleep expects microseconds */
846 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
847 }
848 }
849
850 qemu_mutex_lock_iothread();
851 if (s->state == MIGRATION_STATUS_COMPLETED) {
852 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
853 uint64_t transferred_bytes = qemu_ftell(s->file);
854 s->total_time = end_time - s->total_time;
855 s->downtime = end_time - start_time;
856 if (s->total_time) {
857 s->mbps = (((double) transferred_bytes * 8.0) /
858 ((double) s->total_time)) / 1000;
859 }
860 runstate_set(RUN_STATE_POSTMIGRATE);
861 } else {
862 if (old_vm_running) {
863 vm_start();
864 }
865 }
866 qemu_bh_schedule(s->cleanup_bh);
867 qemu_mutex_unlock_iothread();
868
869 return NULL;
870 }
871
872 void migrate_fd_connect(MigrationState *s)
873 {
874 /* This is a best 1st approximation. ns to ms */
875 s->expected_downtime = max_downtime/1000000;
876 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
877
878 qemu_file_set_rate_limit(s->file,
879 s->bandwidth_limit / XFER_LIMIT_RATIO);
880
881 /* Notify before starting migration thread */
882 notifier_list_notify(&migration_state_notifiers, s);
883
884 migrate_compress_threads_create();
885 qemu_thread_create(&s->thread, "migration", migration_thread, s,
886 QEMU_THREAD_JOINABLE);
887 }