blob: 46bb410ef398662f98f8daf710003f93b09c850c [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 Bonziniab28bd22015-07-09 08:55:38 +020025#include "qemu/rcu.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010026#include "migration/block.h"
Juan Quintela766bd172012-07-23 05:45:29 +020027#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030028#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010029#include "trace.h"
Juan Quinteladf4b1022014-10-08 10:58:10 +020030#include "qapi/util.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020031#include "qapi-event.h"
aliguori065e2812008-11-11 16:46:33 +000032
Juan Quintelad0ae46c2011-02-23 00:33:19 +010033#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000034
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010035/* Amount of time to allocate to each "chunk" of bandwidth-throttled
36 * data. */
37#define BUFFER_DELAY 100
38#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
39
Liang Li8706d2d2015-03-23 16:32:17 +080040/* Default compression thread count */
41#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080042/* Default decompression thread count, usually decompression is at
43 * least 4 times as fast as compression.*/
44#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080045/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
46#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
47
Orit Wasserman17ad9b32012-08-06 21:42:53 +030048/* Migration XBZRLE default cache size */
49#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
50
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010051static NotifierList migration_state_notifiers =
52 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
53
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000054static bool deferred_incoming;
55
Juan Quintela17549e82011-10-05 13:50:43 +020056/* When we add fault tolerance, we could have several
57 migrations at once. For now we don't need to add
58 dynamic creation of migration */
59
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010060/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020061MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020062{
63 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080064 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010065 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030066 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040067 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080068 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
69 DEFAULT_MIGRATE_COMPRESS_LEVEL,
70 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
71 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
72 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
73 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Juan Quintela17549e82011-10-05 13:50:43 +020074 };
75
76 return &current_migration;
77}
78
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010079/* For incoming */
80static MigrationIncomingState *mis_current;
81
82MigrationIncomingState *migration_incoming_get_current(void)
83{
84 return mis_current;
85}
86
87MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
88{
89 mis_current = g_malloc0(sizeof(MigrationIncomingState));
90 mis_current->file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010091 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010092
93 return mis_current;
94}
95
96void migration_incoming_state_destroy(void)
97{
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010098 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010099 g_free(mis_current);
100 mis_current = NULL;
101}
102
Juan Quinteladf4b1022014-10-08 10:58:10 +0200103
104typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200105 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200106 uint32_t size;
107 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200108 RunState state;
109 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200110} GlobalState;
111
112static GlobalState global_state;
113
Juan Quintela560d0272015-07-15 09:53:46 +0200114int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200115{
116 if (!runstate_store((char *)global_state.runstate,
117 sizeof(global_state.runstate))) {
118 error_report("runstate name too big: %s", global_state.runstate);
119 trace_migrate_state_too_big();
120 return -EINVAL;
121 }
122 return 0;
123}
124
Anthony PERARDc69adea2015-08-03 15:29:19 +0100125void global_state_store_running(void)
126{
127 const char *state = RunState_lookup[RUN_STATE_RUNNING];
128 strncpy((char *)global_state.runstate,
129 state, sizeof(global_state.runstate));
130}
131
Juan Quintela172c4352015-07-08 13:56:26 +0200132static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200133{
Juan Quintela172c4352015-07-08 13:56:26 +0200134 return global_state.received;
135}
136
137static RunState global_state_get_runstate(void)
138{
139 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200140}
141
Juan Quintela13d16812014-10-08 13:58:24 +0200142void global_state_set_optional(void)
143{
144 global_state.optional = true;
145}
146
147static bool global_state_needed(void *opaque)
148{
149 GlobalState *s = opaque;
150 char *runstate = (char *)s->runstate;
151
152 /* If it is not optional, it is mandatory */
153
154 if (s->optional == false) {
155 return true;
156 }
157
158 /* If state is running or paused, it is not needed */
159
160 if (strcmp(runstate, "running") == 0 ||
161 strcmp(runstate, "paused") == 0) {
162 return false;
163 }
164
165 /* for any other state it is needed */
166 return true;
167}
168
Juan Quinteladf4b1022014-10-08 10:58:10 +0200169static int global_state_post_load(void *opaque, int version_id)
170{
171 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200172 Error *local_err = NULL;
173 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200174 char *runstate = (char *)s->runstate;
175
Juan Quintela172c4352015-07-08 13:56:26 +0200176 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200177 trace_migrate_global_state_post_load(runstate);
178
Juan Quintela172c4352015-07-08 13:56:26 +0200179 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200180 -1, &local_err);
181
Juan Quintela172c4352015-07-08 13:56:26 +0200182 if (r == -1) {
183 if (local_err) {
184 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200185 }
Juan Quintela172c4352015-07-08 13:56:26 +0200186 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200187 }
Juan Quintela172c4352015-07-08 13:56:26 +0200188 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200189
Juan Quintela172c4352015-07-08 13:56:26 +0200190 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200191}
192
193static void global_state_pre_save(void *opaque)
194{
195 GlobalState *s = opaque;
196
197 trace_migrate_global_state_pre_save((char *)s->runstate);
198 s->size = strlen((char *)s->runstate) + 1;
199}
200
201static const VMStateDescription vmstate_globalstate = {
202 .name = "globalstate",
203 .version_id = 1,
204 .minimum_version_id = 1,
205 .post_load = global_state_post_load,
206 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200207 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200208 .fields = (VMStateField[]) {
209 VMSTATE_UINT32(size, GlobalState),
210 VMSTATE_BUFFER(runstate, GlobalState),
211 VMSTATE_END_OF_LIST()
212 },
213};
214
215void register_global_state(void)
216{
217 /* We would use it independently that we receive it */
218 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200219 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200220 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
221}
222
Juan Quintelab05dc722015-07-07 14:44:05 +0200223static void migrate_generate_event(int new_state)
224{
225 if (migrate_use_events()) {
226 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200227 }
228}
229
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000230/*
231 * Called on -incoming with a defer: uri.
232 * The migration can be started later after any parameters have been
233 * changed.
234 */
235static void deferred_incoming_migration(Error **errp)
236{
237 if (deferred_incoming) {
238 error_setg(errp, "Incoming migration already deferred");
239 }
240 deferred_incoming = true;
241}
242
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200243void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000244{
aliguori34c9dd82008-10-13 03:14:31 +0000245 const char *p;
246
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200247 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000248 if (!strcmp(uri, "defer")) {
249 deferred_incoming_migration(errp);
250 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200251 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400252#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000253 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400254 rdma_start_incoming_migration(p, errp);
255#endif
aliguori065e2812008-11-11 16:46:33 +0000256#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000257 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200258 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000259 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200260 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000261 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200262 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000263#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000264 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100265 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200266 }
aliguori5bb79102008-10-13 03:12:02 +0000267}
268
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200269static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200270{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200271 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100272 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200273 int ret;
274
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100275 migration_incoming_state_new(f);
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200276 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200277 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100278
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200279 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200280 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100281 migration_incoming_state_destroy();
282
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200283 if (ret < 0) {
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200284 migrate_generate_event(MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200285 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800286 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600287 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200288 }
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200289 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
Juan Quintela511c0232010-06-09 14:10:55 +0200290 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200291
Anthony Liguori0f154232011-11-14 15:09:45 -0600292 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100293 bdrv_invalidate_cache_all(&local_err);
294 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100295 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800296 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100297 exit(EXIT_FAILURE);
298 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600299
Juan Quintela172c4352015-07-08 13:56:26 +0200300 /* If global state section was not received or we are in running
301 state, we need to obey autostart. Any other state is set with
302 runstate_set. */
Juan Quinteladf4b1022014-10-08 10:58:10 +0200303
Juan Quintela172c4352015-07-08 13:56:26 +0200304 if (!global_state_received() ||
305 global_state_get_runstate() == RUN_STATE_RUNNING) {
Juan Quinteladf4b1022014-10-08 10:58:10 +0200306 if (autostart) {
307 vm_start();
308 } else {
309 runstate_set(RUN_STATE_PAUSED);
310 }
Juan Quintela172c4352015-07-08 13:56:26 +0200311 } else {
312 runstate_set(global_state_get_runstate());
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300313 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800314 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200315}
316
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200317void process_incoming_migration(QEMUFile *f)
318{
319 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
320 int fd = qemu_get_fd(f);
321
322 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800323 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100324 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200325 qemu_coroutine_enter(co, f);
326}
327
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400328/* amount of nanoseconds we are willing to wait for migration to be down.
329 * the choice of nanoseconds is because it is the maximum resolution that
330 * get_clock() can achieve. It is an internal measure. All user-visible
331 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100332static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400333
334uint64_t migrate_max_downtime(void)
335{
336 return max_downtime;
337}
338
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300339MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
340{
341 MigrationCapabilityStatusList *head = NULL;
342 MigrationCapabilityStatusList *caps;
343 MigrationState *s = migrate_get_current();
344 int i;
345
Michael Tokarev387eede2013-10-05 13:18:28 +0400346 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300347 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
348 if (head == NULL) {
349 head = g_malloc0(sizeof(*caps));
350 caps = head;
351 } else {
352 caps->next = g_malloc0(sizeof(*caps));
353 caps = caps->next;
354 }
355 caps->value =
356 g_malloc(sizeof(*caps->value));
357 caps->value->capability = i;
358 caps->value->state = s->enabled_capabilities[i];
359 }
360
361 return head;
362}
363
Liang Li85de8322015-03-23 16:32:28 +0800364MigrationParameters *qmp_query_migrate_parameters(Error **errp)
365{
366 MigrationParameters *params;
367 MigrationState *s = migrate_get_current();
368
369 params = g_malloc0(sizeof(*params));
370 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
371 params->compress_threads =
372 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
373 params->decompress_threads =
374 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
375
376 return params;
377}
378
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300379static void get_xbzrle_cache_stats(MigrationInfo *info)
380{
381 if (migrate_use_xbzrle()) {
382 info->has_xbzrle_cache = true;
383 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
384 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
385 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
386 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
387 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800388 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300389 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
390 }
391}
392
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300393MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000394{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300395 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200396 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000397
Juan Quintela17549e82011-10-05 13:50:43 +0200398 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800399 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200400 /* no migration has happened ever */
401 break;
zhanghailiang31194732015-03-13 16:08:38 +0800402 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400403 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400404 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400405 break;
zhanghailiang31194732015-03-13 16:08:38 +0800406 case MIGRATION_STATUS_ACTIVE:
407 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300408 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200409 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100410 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200411 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200412 info->has_expected_downtime = true;
413 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400414 info->has_setup_time = true;
415 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200416
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300417 info->has_ram = true;
418 info->ram = g_malloc0(sizeof(*info->ram));
419 info->ram->transferred = ram_bytes_transferred();
420 info->ram->remaining = ram_bytes_remaining();
421 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300422 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100423 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300424 info->ram->normal = norm_mig_pages_transferred();
425 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200426 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400427 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800428 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200429
Juan Quintela17549e82011-10-05 13:50:43 +0200430 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300431 info->has_disk = true;
432 info->disk = g_malloc0(sizeof(*info->disk));
433 info->disk->transferred = blk_mig_bytes_transferred();
434 info->disk->remaining = blk_mig_bytes_remaining();
435 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000436 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300437
438 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200439 break;
zhanghailiang31194732015-03-13 16:08:38 +0800440 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300441 get_xbzrle_cache_stats(info);
442
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300443 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900444 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200445 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200446 info->has_downtime = true;
447 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400448 info->has_setup_time = true;
449 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200450
451 info->has_ram = true;
452 info->ram = g_malloc0(sizeof(*info->ram));
453 info->ram->transferred = ram_bytes_transferred();
454 info->ram->remaining = 0;
455 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300456 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100457 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300458 info->ram->normal = norm_mig_pages_transferred();
459 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400460 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800461 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200462 break;
zhanghailiang31194732015-03-13 16:08:38 +0800463 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300464 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200465 break;
zhanghailiang31194732015-03-13 16:08:38 +0800466 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300467 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200468 break;
aliguori5bb79102008-10-13 03:12:02 +0000469 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800470 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300471
472 return info;
aliguori5bb79102008-10-13 03:12:02 +0000473}
474
Orit Wasserman00458432012-08-06 21:42:48 +0300475void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
476 Error **errp)
477{
478 MigrationState *s = migrate_get_current();
479 MigrationCapabilityStatusList *cap;
480
zhanghailiang31194732015-03-13 16:08:38 +0800481 if (s->state == MIGRATION_STATUS_ACTIVE ||
482 s->state == MIGRATION_STATUS_SETUP) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100483 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300484 return;
485 }
486
487 for (cap = params; cap; cap = cap->next) {
488 s->enabled_capabilities[cap->value->capability] = cap->value->state;
489 }
490}
491
Liang Li85de8322015-03-23 16:32:28 +0800492void qmp_migrate_set_parameters(bool has_compress_level,
493 int64_t compress_level,
494 bool has_compress_threads,
495 int64_t compress_threads,
496 bool has_decompress_threads,
497 int64_t decompress_threads, Error **errp)
498{
499 MigrationState *s = migrate_get_current();
500
501 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100502 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
503 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800504 return;
505 }
506 if (has_compress_threads &&
507 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100508 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
509 "compress_threads",
510 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800511 return;
512 }
513 if (has_decompress_threads &&
514 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100515 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
516 "decompress_threads",
517 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800518 return;
519 }
520
521 if (has_compress_level) {
522 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
523 }
524 if (has_compress_threads) {
525 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
526 }
527 if (has_decompress_threads) {
528 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
529 decompress_threads;
530 }
531}
532
aliguori065e2812008-11-11 16:46:33 +0000533/* shared migration helpers */
534
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000535static void migrate_set_state(MigrationState *s, int old_state, int new_state)
536{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200537 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200538 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200539 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000540 }
541}
542
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100543static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000544{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100545 MigrationState *s = opaque;
546
547 qemu_bh_delete(s->cleanup_bh);
548 s->cleanup_bh = NULL;
549
aliguori065e2812008-11-11 16:46:33 +0000550 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100551 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100552 qemu_mutex_unlock_iothread();
553 qemu_thread_join(&s->thread);
554 qemu_mutex_lock_iothread();
555
Liang Li8706d2d2015-03-23 16:32:17 +0800556 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100557 qemu_fclose(s->file);
558 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000559 }
560
zhanghailiang31194732015-03-13 16:08:38 +0800561 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100562
zhanghailiang31194732015-03-13 16:08:38 +0800563 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100564 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800565 if (s->state == MIGRATION_STATUS_CANCELLING) {
566 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
567 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000568 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100569 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100570
571 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000572}
573
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200574void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000575{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100576 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100577 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200578 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100579 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100580}
581
Juan Quintela0edda1c2010-05-11 16:28:39 +0200582static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000583{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000584 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000585 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100586 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000587
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000588 do {
589 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800590 if (old_state != MIGRATION_STATUS_SETUP &&
591 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000592 break;
593 }
zhanghailiang31194732015-03-13 16:08:38 +0800594 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
595 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000596
597 /*
598 * If we're unlucky the migration code might be stuck somewhere in a
599 * send/write while the network has failed and is waiting to timeout;
600 * if we've got shutdown(2) available then we can force it to quit.
601 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
602 * called in a bh, so there is no race against this cancel.
603 */
zhanghailiang31194732015-03-13 16:08:38 +0800604 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000605 qemu_file_shutdown(f);
606 }
aliguori065e2812008-11-11 16:46:33 +0000607}
608
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100609void add_migration_state_change_notifier(Notifier *notify)
610{
611 notifier_list_add(&migration_state_notifiers, notify);
612}
613
614void remove_migration_state_change_notifier(Notifier *notify)
615{
Paolo Bonzini31552522012-01-13 17:34:01 +0100616 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100617}
618
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200619bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200620{
zhanghailiang31194732015-03-13 16:08:38 +0800621 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200622}
623
Juan Quintela70736932011-02-23 00:43:59 +0100624bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100625{
zhanghailiang31194732015-03-13 16:08:38 +0800626 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100627}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200628
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200629bool migration_has_failed(MigrationState *s)
630{
zhanghailiang31194732015-03-13 16:08:38 +0800631 return (s->state == MIGRATION_STATUS_CANCELLED ||
632 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200633}
634
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300635static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200636{
Juan Quintela17549e82011-10-05 13:50:43 +0200637 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100638 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300639 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300640 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800641 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
642 int compress_thread_count =
643 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
644 int decompress_thread_count =
645 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300646
647 memcpy(enabled_capabilities, s->enabled_capabilities,
648 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200649
Juan Quintela17549e82011-10-05 13:50:43 +0200650 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300651 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300652 memcpy(s->enabled_capabilities, enabled_capabilities,
653 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300654 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100655
Liang Li43c60a82015-03-23 16:32:27 +0800656 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
657 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
658 compress_thread_count;
659 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
660 decompress_thread_count;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200661 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200662 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200663
Alex Blighbc72ad62013-08-21 16:03:08 +0100664 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200665 return s;
666}
Juan Quintelacab30142011-02-22 23:54:21 +0100667
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600668static GSList *migration_blockers;
669
670void migrate_add_blocker(Error *reason)
671{
672 migration_blockers = g_slist_prepend(migration_blockers, reason);
673}
674
675void migrate_del_blocker(Error *reason)
676{
677 migration_blockers = g_slist_remove(migration_blockers, reason);
678}
679
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000680void qmp_migrate_incoming(const char *uri, Error **errp)
681{
682 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000683 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000684
685 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000686 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000687 return;
688 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000689 if (!once) {
690 error_setg(errp, "The incoming migration has already been started");
691 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000692
693 qemu_start_incoming_migration(uri, &local_err);
694
695 if (local_err) {
696 error_propagate(errp, local_err);
697 return;
698 }
699
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000700 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000701}
702
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200703void qmp_migrate(const char *uri, bool has_blk, bool blk,
704 bool has_inc, bool inc, bool has_detach, bool detach,
705 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100706{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200707 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200708 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300709 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100710 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100711
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900712 params.blk = has_blk && blk;
713 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300714
zhanghailiang31194732015-03-13 16:08:38 +0800715 if (s->state == MIGRATION_STATUS_ACTIVE ||
716 s->state == MIGRATION_STATUS_SETUP ||
717 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100718 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200719 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100720 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100721 if (runstate_check(RUN_STATE_INMIGRATE)) {
722 error_setg(errp, "Guest is waiting for an incoming migration");
723 return;
724 }
725
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200726 if (qemu_savevm_state_blocked(errp)) {
727 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100728 }
729
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600730 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200731 *errp = error_copy(migration_blockers->data);
732 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600733 }
734
Juan Quintela656a2332015-07-01 09:32:29 +0200735 /* We are starting a new migration, so we want to start in a clean
736 state. This change is only needed if previous migration
737 failed/was cancelled. We don't use migrate_set_state() because
738 we are setting the initial state, not changing it. */
739 s->state = MIGRATION_STATUS_NONE;
740
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300741 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100742
743 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200744 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400745#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800746 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400747 rdma_start_outgoing_migration(s, p, &local_err);
748#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100749#if !defined(WIN32)
750 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200751 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100752 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200753 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100754 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200755 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100756#endif
757 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100758 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
759 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +0200760 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200761 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100762 }
763
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200764 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200765 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200766 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200767 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100768 }
Juan Quintelacab30142011-02-22 23:54:21 +0100769}
770
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200771void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100772{
Juan Quintela17549e82011-10-05 13:50:43 +0200773 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100774}
775
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300776void qmp_migrate_set_cache_size(int64_t value, Error **errp)
777{
778 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200779 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300780
781 /* Check for truncation */
782 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100783 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
784 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300785 return;
786 }
787
Orit Wassermana5615b12014-01-30 20:08:36 +0200788 /* Cache should not be larger than guest ram size */
789 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100790 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
791 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +0200792 return;
793 }
794
Orit Wassermanc91e6812014-01-30 20:08:34 +0200795 new_size = xbzrle_cache_resize(value);
796 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100797 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
798 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +0200799 return;
800 }
801
802 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300803}
804
805int64_t qmp_query_migrate_cache_size(Error **errp)
806{
807 return migrate_xbzrle_cache_size();
808}
809
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200810void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100811{
Juan Quintelacab30142011-02-22 23:54:21 +0100812 MigrationState *s;
813
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200814 if (value < 0) {
815 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100816 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100817 if (value > SIZE_MAX) {
818 value = SIZE_MAX;
819 }
Juan Quintelacab30142011-02-22 23:54:21 +0100820
Juan Quintela17549e82011-10-05 13:50:43 +0200821 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200822 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100823 if (s->file) {
824 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
825 }
Juan Quintelacab30142011-02-22 23:54:21 +0100826}
827
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200828void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100829{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200830 value *= 1e9;
831 value = MAX(0, MIN(UINT64_MAX, value));
832 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000833}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300834
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600835bool migrate_auto_converge(void)
836{
837 MigrationState *s;
838
839 s = migrate_get_current();
840
841 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
842}
843
Peter Lieven323004a2013-07-18 09:48:50 +0200844bool migrate_zero_blocks(void)
845{
846 MigrationState *s;
847
848 s = migrate_get_current();
849
850 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
851}
852
Liang Li8706d2d2015-03-23 16:32:17 +0800853bool migrate_use_compression(void)
854{
Liang Lidde4e692015-03-23 16:32:26 +0800855 MigrationState *s;
856
857 s = migrate_get_current();
858
859 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800860}
861
862int migrate_compress_level(void)
863{
864 MigrationState *s;
865
866 s = migrate_get_current();
867
Liang Li43c60a82015-03-23 16:32:27 +0800868 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800869}
870
871int migrate_compress_threads(void)
872{
873 MigrationState *s;
874
875 s = migrate_get_current();
876
Liang Li43c60a82015-03-23 16:32:27 +0800877 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800878}
879
Liang Li3fcb38c2015-03-23 16:32:18 +0800880int migrate_decompress_threads(void)
881{
882 MigrationState *s;
883
884 s = migrate_get_current();
885
Liang Li43c60a82015-03-23 16:32:27 +0800886 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800887}
888
Juan Quintelab05dc722015-07-07 14:44:05 +0200889bool migrate_use_events(void)
890{
891 MigrationState *s;
892
893 s = migrate_get_current();
894
895 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
896}
897
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300898int migrate_use_xbzrle(void)
899{
900 MigrationState *s;
901
902 s = migrate_get_current();
903
904 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
905}
906
907int64_t migrate_xbzrle_cache_size(void)
908{
909 MigrationState *s;
910
911 s = migrate_get_current();
912
913 return s->xbzrle_cache_size;
914}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200915
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +0100916/**
917 * migration_completion: Used by migration_thread when there's not much left.
918 * The caller 'breaks' the loop when this returns.
919 *
920 * @s: Current migration state
921 * @*old_vm_running: Pointer to old_vm_running flag
922 * @*start_time: Pointer to time to update
923 */
924static void migration_completion(MigrationState *s, bool *old_vm_running,
925 int64_t *start_time)
926{
927 int ret;
928
929 qemu_mutex_lock_iothread();
930 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
931 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
932 *old_vm_running = runstate_is_running();
933
934 ret = global_state_store();
935 if (!ret) {
936 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
937 if (ret >= 0) {
938 qemu_file_set_rate_limit(s->file, INT64_MAX);
939 qemu_savevm_state_complete(s->file);
940 }
941 }
942 qemu_mutex_unlock_iothread();
943
944 if (ret < 0) {
945 goto fail;
946 }
947
948 if (qemu_file_get_error(s->file)) {
949 trace_migration_completion_file_err();
950 goto fail;
951 }
952
953 migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_COMPLETED);
954 return;
955
956fail:
957 migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_FAILED);
958}
959
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200960/* migration thread support */
961
Juan Quintela5f496a12013-02-22 17:36:30 +0100962static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200963{
Juan Quintela9848a402012-12-19 09:55:50 +0100964 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100965 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
966 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100967 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200968 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100969 int64_t start_time = initial_time;
970 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +0200971
Paolo Bonziniab28bd22015-07-09 08:55:38 +0200972 rcu_register_thread();
973
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +0100974 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100975 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200976
Alex Blighbc72ad62013-08-21 16:03:08 +0100977 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +0800978 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400979
zhanghailiang31194732015-03-13 16:08:38 +0800980 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +0100981 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +0200982 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200983
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100984 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +0200985 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100986 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +0200987 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100988 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +0200989 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +0100990 trace_migration_thread_low_pending(pending_size);
991 migration_completion(s, &old_vm_running, &start_time);
992 break;
Juan Quintelac369f402012-10-03 20:33:34 +0200993 }
994 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100995
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100996 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800997 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
998 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100999 break;
1000 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001001 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001002 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001003 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001004 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001005 double bandwidth = transferred_bytes / time_spent;
1006 max_size = bandwidth * migrate_max_downtime() / 1000000;
1007
Michael R. Hines7e114f82013-06-25 21:35:30 -04001008 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1009 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1010
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001011 trace_migrate_transferred(transferred_bytes, time_spent,
1012 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001013 /* if we haven't sent anything, we don't want to recalculate
1014 10000 is a small enough number for our purposes */
1015 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1016 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1017 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001018
Paolo Bonzini1964a392013-02-22 17:36:45 +01001019 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001020 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001021 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001022 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001023 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001024 /* usleep expects microseconds */
1025 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1026 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001027 }
1028
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001029 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +08001030 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001031 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +02001032 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001033 s->total_time = end_time - s->total_time;
1034 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +02001035 if (s->total_time) {
1036 s->mbps = (((double) transferred_bytes * 8.0) /
1037 ((double) s->total_time)) / 1000;
1038 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001039 runstate_set(RUN_STATE_POSTMIGRATE);
1040 } else {
1041 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001042 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001043 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001044 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001045 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001046 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001047
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001048 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001049 return NULL;
1050}
1051
Juan Quintela9848a402012-12-19 09:55:50 +01001052void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001053{
Juan Quintelacc283e32013-02-01 11:12:26 +01001054 /* This is a best 1st approximation. ns to ms */
1055 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001056 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001057
Paolo Bonzini442773c2013-02-22 17:36:44 +01001058 qemu_file_set_rate_limit(s->file,
1059 s->bandwidth_limit / XFER_LIMIT_RATIO);
1060
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001061 /* Notify before starting migration thread */
1062 notifier_list_notify(&migration_state_notifiers, s);
1063
Liang Li8706d2d2015-03-23 16:32:17 +08001064 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001065 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001066 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001067}