blob: 5e436f7106c6d5dac196882e1857e9592a1ec1dd [file] [log] [blame]
aliguori5bb79102008-10-13 03:12:02 +00001/*
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori5bb79102008-10-13 03:12:02 +000014 */
15
16#include "qemu-common.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010017#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010018#include "qemu/main-loop.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010019#include "migration/migration.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020020#include "migration/qemu-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010021#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010023#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/sockets.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010025#include "migration/block.h"
Juan Quintela766bd172012-07-23 05:45:29 +020026#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030027#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010028#include "trace.h"
Juan Quinteladf4b1022014-10-08 10:58:10 +020029#include "qapi/util.h"
aliguori065e2812008-11-11 16:46:33 +000030
Juan Quintelad0ae46c2011-02-23 00:33:19 +010031#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000032
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010033/* Amount of time to allocate to each "chunk" of bandwidth-throttled
34 * data. */
35#define BUFFER_DELAY 100
36#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
37
Liang Li8706d2d2015-03-23 16:32:17 +080038/* Default compression thread count */
39#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080040/* Default decompression thread count, usually decompression is at
41 * least 4 times as fast as compression.*/
42#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080043/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
44#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
45
Orit Wasserman17ad9b32012-08-06 21:42:53 +030046/* Migration XBZRLE default cache size */
47#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
48
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010049static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
51
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000052static bool deferred_incoming;
53
Juan Quintela17549e82011-10-05 13:50:43 +020054/* When we add fault tolerance, we could have several
55 migrations at once. For now we don't need to add
56 dynamic creation of migration */
57
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010058/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020059MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020060{
61 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080062 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010063 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030064 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040065 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080066 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
67 DEFAULT_MIGRATE_COMPRESS_LEVEL,
68 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
69 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
70 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
71 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Juan Quintela17549e82011-10-05 13:50:43 +020072 };
73
74 return &current_migration;
75}
76
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010077/* For incoming */
78static MigrationIncomingState *mis_current;
79
80MigrationIncomingState *migration_incoming_get_current(void)
81{
82 return mis_current;
83}
84
85MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
86{
87 mis_current = g_malloc0(sizeof(MigrationIncomingState));
88 mis_current->file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010089 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010090
91 return mis_current;
92}
93
94void migration_incoming_state_destroy(void)
95{
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010096 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010097 g_free(mis_current);
98 mis_current = NULL;
99}
100
Juan Quinteladf4b1022014-10-08 10:58:10 +0200101
102typedef struct {
103 uint32_t size;
104 uint8_t runstate[100];
105} GlobalState;
106
107static GlobalState global_state;
108
109static int global_state_store(void)
110{
111 if (!runstate_store((char *)global_state.runstate,
112 sizeof(global_state.runstate))) {
113 error_report("runstate name too big: %s", global_state.runstate);
114 trace_migrate_state_too_big();
115 return -EINVAL;
116 }
117 return 0;
118}
119
120static char *global_state_get_runstate(void)
121{
122 return (char *)global_state.runstate;
123}
124
125static int global_state_post_load(void *opaque, int version_id)
126{
127 GlobalState *s = opaque;
128 int ret = 0;
129 char *runstate = (char *)s->runstate;
130
131 trace_migrate_global_state_post_load(runstate);
132
133 if (strcmp(runstate, "running") != 0) {
134 Error *local_err = NULL;
135 int r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
136 -1, &local_err);
137
138 if (r == -1) {
139 if (local_err) {
140 error_report_err(local_err);
141 }
142 return -EINVAL;
143 }
144 ret = vm_stop_force_state(r);
145 }
146
147 return ret;
148}
149
150static void global_state_pre_save(void *opaque)
151{
152 GlobalState *s = opaque;
153
154 trace_migrate_global_state_pre_save((char *)s->runstate);
155 s->size = strlen((char *)s->runstate) + 1;
156}
157
158static const VMStateDescription vmstate_globalstate = {
159 .name = "globalstate",
160 .version_id = 1,
161 .minimum_version_id = 1,
162 .post_load = global_state_post_load,
163 .pre_save = global_state_pre_save,
164 .fields = (VMStateField[]) {
165 VMSTATE_UINT32(size, GlobalState),
166 VMSTATE_BUFFER(runstate, GlobalState),
167 VMSTATE_END_OF_LIST()
168 },
169};
170
171void register_global_state(void)
172{
173 /* We would use it independently that we receive it */
174 strcpy((char *)&global_state.runstate, "");
175 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
176}
177
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000178/*
179 * Called on -incoming with a defer: uri.
180 * The migration can be started later after any parameters have been
181 * changed.
182 */
183static void deferred_incoming_migration(Error **errp)
184{
185 if (deferred_incoming) {
186 error_setg(errp, "Incoming migration already deferred");
187 }
188 deferred_incoming = true;
189}
190
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200191void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000192{
aliguori34c9dd82008-10-13 03:14:31 +0000193 const char *p;
194
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000195 if (!strcmp(uri, "defer")) {
196 deferred_incoming_migration(errp);
197 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200198 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400199#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000200 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400201 rdma_start_incoming_migration(p, errp);
202#endif
aliguori065e2812008-11-11 16:46:33 +0000203#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000204 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200205 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000206 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200207 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000208 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200209 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000210#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000211 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100212 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200213 }
aliguori5bb79102008-10-13 03:12:02 +0000214}
215
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200216static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200217{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200218 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100219 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200220 int ret;
221
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100222 migration_incoming_state_new(f);
223
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200224 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100225
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200226 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200227 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100228 migration_incoming_state_destroy();
229
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200230 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200231 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800232 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600233 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200234 }
235 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200236
Anthony Liguori0f154232011-11-14 15:09:45 -0600237 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100238 bdrv_invalidate_cache_all(&local_err);
239 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100240 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800241 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100242 exit(EXIT_FAILURE);
243 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600244
Juan Quinteladf4b1022014-10-08 10:58:10 +0200245 /* runstate == "" means that we haven't received it through the
246 * wire, so we obey autostart. runstate == runing means that we
247 * need to run it, we need to make sure that we do it after
248 * everything else has finished. Every other state change is done
249 * at the post_load function */
250
251 if (strcmp(global_state_get_runstate(), "running") == 0) {
Juan Quintela511c0232010-06-09 14:10:55 +0200252 vm_start();
Juan Quinteladf4b1022014-10-08 10:58:10 +0200253 } else if (strcmp(global_state_get_runstate(), "") == 0) {
254 if (autostart) {
255 vm_start();
256 } else {
257 runstate_set(RUN_STATE_PAUSED);
258 }
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300259 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800260 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200261}
262
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200263void process_incoming_migration(QEMUFile *f)
264{
265 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
266 int fd = qemu_get_fd(f);
267
268 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800269 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100270 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200271 qemu_coroutine_enter(co, f);
272}
273
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400274/* amount of nanoseconds we are willing to wait for migration to be down.
275 * the choice of nanoseconds is because it is the maximum resolution that
276 * get_clock() can achieve. It is an internal measure. All user-visible
277 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100278static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400279
280uint64_t migrate_max_downtime(void)
281{
282 return max_downtime;
283}
284
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300285MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
286{
287 MigrationCapabilityStatusList *head = NULL;
288 MigrationCapabilityStatusList *caps;
289 MigrationState *s = migrate_get_current();
290 int i;
291
Michael Tokarev387eede2013-10-05 13:18:28 +0400292 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300293 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
294 if (head == NULL) {
295 head = g_malloc0(sizeof(*caps));
296 caps = head;
297 } else {
298 caps->next = g_malloc0(sizeof(*caps));
299 caps = caps->next;
300 }
301 caps->value =
302 g_malloc(sizeof(*caps->value));
303 caps->value->capability = i;
304 caps->value->state = s->enabled_capabilities[i];
305 }
306
307 return head;
308}
309
Liang Li85de8322015-03-23 16:32:28 +0800310MigrationParameters *qmp_query_migrate_parameters(Error **errp)
311{
312 MigrationParameters *params;
313 MigrationState *s = migrate_get_current();
314
315 params = g_malloc0(sizeof(*params));
316 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
317 params->compress_threads =
318 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
319 params->decompress_threads =
320 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
321
322 return params;
323}
324
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300325static void get_xbzrle_cache_stats(MigrationInfo *info)
326{
327 if (migrate_use_xbzrle()) {
328 info->has_xbzrle_cache = true;
329 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
330 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
331 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
332 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
333 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800334 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300335 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
336 }
337}
338
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300339MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000340{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300341 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200342 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000343
Juan Quintela17549e82011-10-05 13:50:43 +0200344 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800345 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200346 /* no migration has happened ever */
347 break;
zhanghailiang31194732015-03-13 16:08:38 +0800348 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400349 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400350 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400351 break;
zhanghailiang31194732015-03-13 16:08:38 +0800352 case MIGRATION_STATUS_ACTIVE:
353 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300354 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200355 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100356 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200357 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200358 info->has_expected_downtime = true;
359 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400360 info->has_setup_time = true;
361 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200362
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300363 info->has_ram = true;
364 info->ram = g_malloc0(sizeof(*info->ram));
365 info->ram->transferred = ram_bytes_transferred();
366 info->ram->remaining = ram_bytes_remaining();
367 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300368 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100369 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300370 info->ram->normal = norm_mig_pages_transferred();
371 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200372 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400373 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800374 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200375
Juan Quintela17549e82011-10-05 13:50:43 +0200376 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300377 info->has_disk = true;
378 info->disk = g_malloc0(sizeof(*info->disk));
379 info->disk->transferred = blk_mig_bytes_transferred();
380 info->disk->remaining = blk_mig_bytes_remaining();
381 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000382 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300383
384 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200385 break;
zhanghailiang31194732015-03-13 16:08:38 +0800386 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300387 get_xbzrle_cache_stats(info);
388
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300389 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900390 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200391 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200392 info->has_downtime = true;
393 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400394 info->has_setup_time = true;
395 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200396
397 info->has_ram = true;
398 info->ram = g_malloc0(sizeof(*info->ram));
399 info->ram->transferred = ram_bytes_transferred();
400 info->ram->remaining = 0;
401 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300402 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100403 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300404 info->ram->normal = norm_mig_pages_transferred();
405 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400406 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800407 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200408 break;
zhanghailiang31194732015-03-13 16:08:38 +0800409 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300410 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200411 break;
zhanghailiang31194732015-03-13 16:08:38 +0800412 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300413 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200414 break;
aliguori5bb79102008-10-13 03:12:02 +0000415 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800416 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300417
418 return info;
aliguori5bb79102008-10-13 03:12:02 +0000419}
420
Orit Wasserman00458432012-08-06 21:42:48 +0300421void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
422 Error **errp)
423{
424 MigrationState *s = migrate_get_current();
425 MigrationCapabilityStatusList *cap;
426
zhanghailiang31194732015-03-13 16:08:38 +0800427 if (s->state == MIGRATION_STATUS_ACTIVE ||
428 s->state == MIGRATION_STATUS_SETUP) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100429 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300430 return;
431 }
432
433 for (cap = params; cap; cap = cap->next) {
434 s->enabled_capabilities[cap->value->capability] = cap->value->state;
435 }
436}
437
Liang Li85de8322015-03-23 16:32:28 +0800438void qmp_migrate_set_parameters(bool has_compress_level,
439 int64_t compress_level,
440 bool has_compress_threads,
441 int64_t compress_threads,
442 bool has_decompress_threads,
443 int64_t decompress_threads, Error **errp)
444{
445 MigrationState *s = migrate_get_current();
446
447 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100448 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
449 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800450 return;
451 }
452 if (has_compress_threads &&
453 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100454 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
455 "compress_threads",
456 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800457 return;
458 }
459 if (has_decompress_threads &&
460 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100461 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
462 "decompress_threads",
463 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800464 return;
465 }
466
467 if (has_compress_level) {
468 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
469 }
470 if (has_compress_threads) {
471 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
472 }
473 if (has_decompress_threads) {
474 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
475 decompress_threads;
476 }
477}
478
aliguori065e2812008-11-11 16:46:33 +0000479/* shared migration helpers */
480
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000481static void migrate_set_state(MigrationState *s, int old_state, int new_state)
482{
483 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
484 trace_migrate_set_state(new_state);
485 }
486}
487
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100488static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000489{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100490 MigrationState *s = opaque;
491
492 qemu_bh_delete(s->cleanup_bh);
493 s->cleanup_bh = NULL;
494
aliguori065e2812008-11-11 16:46:33 +0000495 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100496 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100497 qemu_mutex_unlock_iothread();
498 qemu_thread_join(&s->thread);
499 qemu_mutex_lock_iothread();
500
Liang Li8706d2d2015-03-23 16:32:17 +0800501 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100502 qemu_fclose(s->file);
503 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000504 }
505
zhanghailiang31194732015-03-13 16:08:38 +0800506 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100507
zhanghailiang31194732015-03-13 16:08:38 +0800508 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100509 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800510 if (s->state == MIGRATION_STATUS_CANCELLING) {
511 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
512 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000513 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100514 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100515
516 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000517}
518
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200519void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000520{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100521 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100522 assert(s->file == NULL);
zhanghailiang31194732015-03-13 16:08:38 +0800523 s->state = MIGRATION_STATUS_FAILED;
524 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100525 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100526}
527
Juan Quintela0edda1c2010-05-11 16:28:39 +0200528static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000529{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000530 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000531 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100532 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000533
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000534 do {
535 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800536 if (old_state != MIGRATION_STATUS_SETUP &&
537 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000538 break;
539 }
zhanghailiang31194732015-03-13 16:08:38 +0800540 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
541 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000542
543 /*
544 * If we're unlucky the migration code might be stuck somewhere in a
545 * send/write while the network has failed and is waiting to timeout;
546 * if we've got shutdown(2) available then we can force it to quit.
547 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
548 * called in a bh, so there is no race against this cancel.
549 */
zhanghailiang31194732015-03-13 16:08:38 +0800550 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000551 qemu_file_shutdown(f);
552 }
aliguori065e2812008-11-11 16:46:33 +0000553}
554
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100555void add_migration_state_change_notifier(Notifier *notify)
556{
557 notifier_list_add(&migration_state_notifiers, notify);
558}
559
560void remove_migration_state_change_notifier(Notifier *notify)
561{
Paolo Bonzini31552522012-01-13 17:34:01 +0100562 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100563}
564
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200565bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200566{
zhanghailiang31194732015-03-13 16:08:38 +0800567 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200568}
569
Juan Quintela70736932011-02-23 00:43:59 +0100570bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100571{
zhanghailiang31194732015-03-13 16:08:38 +0800572 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100573}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200574
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200575bool migration_has_failed(MigrationState *s)
576{
zhanghailiang31194732015-03-13 16:08:38 +0800577 return (s->state == MIGRATION_STATUS_CANCELLED ||
578 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200579}
580
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300581static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200582{
Juan Quintela17549e82011-10-05 13:50:43 +0200583 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100584 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300585 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300586 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800587 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
588 int compress_thread_count =
589 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
590 int decompress_thread_count =
591 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300592
593 memcpy(enabled_capabilities, s->enabled_capabilities,
594 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200595
Juan Quintela17549e82011-10-05 13:50:43 +0200596 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300597 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300598 memcpy(s->enabled_capabilities, enabled_capabilities,
599 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300600 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100601
Liang Li43c60a82015-03-23 16:32:27 +0800602 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
603 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
604 compress_thread_count;
605 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
606 decompress_thread_count;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200607 s->bandwidth_limit = bandwidth_limit;
zhanghailiang31194732015-03-13 16:08:38 +0800608 s->state = MIGRATION_STATUS_SETUP;
609 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200610
Alex Blighbc72ad62013-08-21 16:03:08 +0100611 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200612 return s;
613}
Juan Quintelacab30142011-02-22 23:54:21 +0100614
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600615static GSList *migration_blockers;
616
617void migrate_add_blocker(Error *reason)
618{
619 migration_blockers = g_slist_prepend(migration_blockers, reason);
620}
621
622void migrate_del_blocker(Error *reason)
623{
624 migration_blockers = g_slist_remove(migration_blockers, reason);
625}
626
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000627void qmp_migrate_incoming(const char *uri, Error **errp)
628{
629 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000630 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000631
632 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000633 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000634 return;
635 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000636 if (!once) {
637 error_setg(errp, "The incoming migration has already been started");
638 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000639
640 qemu_start_incoming_migration(uri, &local_err);
641
642 if (local_err) {
643 error_propagate(errp, local_err);
644 return;
645 }
646
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000647 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000648}
649
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200650void qmp_migrate(const char *uri, bool has_blk, bool blk,
651 bool has_inc, bool inc, bool has_detach, bool detach,
652 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100653{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200654 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200655 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300656 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100657 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100658
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900659 params.blk = has_blk && blk;
660 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300661
zhanghailiang31194732015-03-13 16:08:38 +0800662 if (s->state == MIGRATION_STATUS_ACTIVE ||
663 s->state == MIGRATION_STATUS_SETUP ||
664 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100665 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200666 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100667 }
668
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100669 if (runstate_check(RUN_STATE_INMIGRATE)) {
670 error_setg(errp, "Guest is waiting for an incoming migration");
671 return;
672 }
673
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200674 if (qemu_savevm_state_blocked(errp)) {
675 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100676 }
677
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600678 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200679 *errp = error_copy(migration_blockers->data);
680 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600681 }
682
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300683 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100684
685 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200686 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400687#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800688 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400689 rdma_start_outgoing_migration(s, p, &local_err);
690#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100691#if !defined(WIN32)
692 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200693 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100694 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200695 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100696 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200697 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100698#endif
699 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100700 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
701 "a valid migration protocol");
zhanghailiang31194732015-03-13 16:08:38 +0800702 s->state = MIGRATION_STATUS_FAILED;
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200703 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100704 }
705
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200706 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200707 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200708 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200709 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100710 }
Juan Quintelacab30142011-02-22 23:54:21 +0100711}
712
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200713void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100714{
Juan Quintela17549e82011-10-05 13:50:43 +0200715 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100716}
717
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300718void qmp_migrate_set_cache_size(int64_t value, Error **errp)
719{
720 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200721 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300722
723 /* Check for truncation */
724 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100725 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
726 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300727 return;
728 }
729
Orit Wassermana5615b12014-01-30 20:08:36 +0200730 /* Cache should not be larger than guest ram size */
731 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100732 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
733 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +0200734 return;
735 }
736
Orit Wassermanc91e6812014-01-30 20:08:34 +0200737 new_size = xbzrle_cache_resize(value);
738 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100739 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
740 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +0200741 return;
742 }
743
744 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300745}
746
747int64_t qmp_query_migrate_cache_size(Error **errp)
748{
749 return migrate_xbzrle_cache_size();
750}
751
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200752void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100753{
Juan Quintelacab30142011-02-22 23:54:21 +0100754 MigrationState *s;
755
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200756 if (value < 0) {
757 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100758 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100759 if (value > SIZE_MAX) {
760 value = SIZE_MAX;
761 }
Juan Quintelacab30142011-02-22 23:54:21 +0100762
Juan Quintela17549e82011-10-05 13:50:43 +0200763 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200764 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100765 if (s->file) {
766 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
767 }
Juan Quintelacab30142011-02-22 23:54:21 +0100768}
769
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200770void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100771{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200772 value *= 1e9;
773 value = MAX(0, MIN(UINT64_MAX, value));
774 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000775}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300776
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600777bool migrate_auto_converge(void)
778{
779 MigrationState *s;
780
781 s = migrate_get_current();
782
783 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
784}
785
Peter Lieven323004a2013-07-18 09:48:50 +0200786bool migrate_zero_blocks(void)
787{
788 MigrationState *s;
789
790 s = migrate_get_current();
791
792 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
793}
794
Liang Li8706d2d2015-03-23 16:32:17 +0800795bool migrate_use_compression(void)
796{
Liang Lidde4e692015-03-23 16:32:26 +0800797 MigrationState *s;
798
799 s = migrate_get_current();
800
801 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800802}
803
804int migrate_compress_level(void)
805{
806 MigrationState *s;
807
808 s = migrate_get_current();
809
Liang Li43c60a82015-03-23 16:32:27 +0800810 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800811}
812
813int migrate_compress_threads(void)
814{
815 MigrationState *s;
816
817 s = migrate_get_current();
818
Liang Li43c60a82015-03-23 16:32:27 +0800819 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800820}
821
Liang Li3fcb38c2015-03-23 16:32:18 +0800822int migrate_decompress_threads(void)
823{
824 MigrationState *s;
825
826 s = migrate_get_current();
827
Liang Li43c60a82015-03-23 16:32:27 +0800828 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800829}
830
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300831int migrate_use_xbzrle(void)
832{
833 MigrationState *s;
834
835 s = migrate_get_current();
836
837 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
838}
839
840int64_t migrate_xbzrle_cache_size(void)
841{
842 MigrationState *s;
843
844 s = migrate_get_current();
845
846 return s->xbzrle_cache_size;
847}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200848
849/* migration thread support */
850
Juan Quintela5f496a12013-02-22 17:36:30 +0100851static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200852{
Juan Quintela9848a402012-12-19 09:55:50 +0100853 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100854 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
855 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100856 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200857 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100858 int64_t start_time = initial_time;
859 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +0200860
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +0100861 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100862 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200863
Alex Blighbc72ad62013-08-21 16:03:08 +0100864 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +0800865 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400866
zhanghailiang31194732015-03-13 16:08:38 +0800867 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +0100868 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +0200869 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200870
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100871 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +0200872 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100873 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +0200874 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100875 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +0200876 } else {
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200877 int ret;
878
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100879 qemu_mutex_lock_iothread();
Alex Blighbc72ad62013-08-21 16:03:08 +0100880 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintelac369f402012-10-03 20:33:34 +0200881 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100882 old_vm_running = runstate_is_running();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200883
Juan Quinteladf4b1022014-10-08 10:58:10 +0200884 ret = global_state_store();
885 if (!ret) {
886 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
887 if (ret >= 0) {
888 qemu_file_set_rate_limit(s->file, INT64_MAX);
889 qemu_savevm_state_complete(s->file);
890 }
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200891 }
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100892 qemu_mutex_unlock_iothread();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200893
894 if (ret < 0) {
zhanghailiang31194732015-03-13 16:08:38 +0800895 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
896 MIGRATION_STATUS_FAILED);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200897 break;
898 }
899
Paolo Bonzini059f8962013-02-22 17:36:32 +0100900 if (!qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800901 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
902 MIGRATION_STATUS_COMPLETED);
Paolo Bonzini059f8962013-02-22 17:36:32 +0100903 break;
904 }
Juan Quintelac369f402012-10-03 20:33:34 +0200905 }
906 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100907
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100908 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800909 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
910 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100911 break;
912 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100913 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200914 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100915 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -0500916 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200917 double bandwidth = transferred_bytes / time_spent;
918 max_size = bandwidth * migrate_max_downtime() / 1000000;
919
Michael R. Hines7e114f82013-06-25 21:35:30 -0400920 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
921 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
922
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100923 trace_migrate_transferred(transferred_bytes, time_spent,
924 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100925 /* if we haven't sent anything, we don't want to recalculate
926 10000 is a small enough number for our purposes */
927 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
928 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
929 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200930
Paolo Bonzini1964a392013-02-22 17:36:45 +0100931 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200932 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100933 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200934 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100935 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200936 /* usleep expects microseconds */
937 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
938 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100939 }
940
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100941 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +0800942 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100943 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +0200944 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100945 s->total_time = end_time - s->total_time;
946 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +0200947 if (s->total_time) {
948 s->mbps = (((double) transferred_bytes * 8.0) /
949 ((double) s->total_time)) / 1000;
950 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100951 runstate_set(RUN_STATE_POSTMIGRATE);
952 } else {
953 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100954 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100955 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200956 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100957 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100958 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100959
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200960 return NULL;
961}
962
Juan Quintela9848a402012-12-19 09:55:50 +0100963void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200964{
Juan Quintelacc283e32013-02-01 11:12:26 +0100965 /* This is a best 1st approximation. ns to ms */
966 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100967 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200968
Paolo Bonzini442773c2013-02-22 17:36:44 +0100969 qemu_file_set_rate_limit(s->file,
970 s->bandwidth_limit / XFER_LIMIT_RATIO);
971
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +0200972 /* Notify before starting migration thread */
973 notifier_list_notify(&migration_state_notifiers, s);
974
Liang Li8706d2d2015-03-23 16:32:17 +0800975 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +0000976 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100977 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200978}