blob: ba82ff6bd1f382f8dd0a3307ee6ad6cd656f70c1 [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"
Juan Quintela598cd2b2015-05-20 12:16:15 +020030#include "qapi-event.h"
aliguori065e2812008-11-11 16:46:33 +000031
Juan Quintelad0ae46c2011-02-23 00:33:19 +010032#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000033
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010034/* Amount of time to allocate to each "chunk" of bandwidth-throttled
35 * data. */
36#define BUFFER_DELAY 100
37#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
38
Liang Li8706d2d2015-03-23 16:32:17 +080039/* Default compression thread count */
40#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080041/* Default decompression thread count, usually decompression is at
42 * least 4 times as fast as compression.*/
43#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080044/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
45#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
46
Orit Wasserman17ad9b32012-08-06 21:42:53 +030047/* Migration XBZRLE default cache size */
48#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
49
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010050static NotifierList migration_state_notifiers =
51 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
52
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000053static bool deferred_incoming;
54
Juan Quintela17549e82011-10-05 13:50:43 +020055/* When we add fault tolerance, we could have several
56 migrations at once. For now we don't need to add
57 dynamic creation of migration */
58
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010059/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020060MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020061{
62 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080063 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010064 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030065 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040066 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080067 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
68 DEFAULT_MIGRATE_COMPRESS_LEVEL,
69 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
70 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
71 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
72 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Juan Quintela17549e82011-10-05 13:50:43 +020073 };
74
75 return &current_migration;
76}
77
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010078/* For incoming */
79static MigrationIncomingState *mis_current;
80
81MigrationIncomingState *migration_incoming_get_current(void)
82{
83 return mis_current;
84}
85
86MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
87{
88 mis_current = g_malloc0(sizeof(MigrationIncomingState));
89 mis_current->file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010090 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010091
92 return mis_current;
93}
94
95void migration_incoming_state_destroy(void)
96{
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010097 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010098 g_free(mis_current);
99 mis_current = NULL;
100}
101
Juan Quinteladf4b1022014-10-08 10:58:10 +0200102
103typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200104 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200105 uint32_t size;
106 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200107 RunState state;
108 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200109} GlobalState;
110
111static GlobalState global_state;
112
113static int global_state_store(void)
114{
115 if (!runstate_store((char *)global_state.runstate,
116 sizeof(global_state.runstate))) {
117 error_report("runstate name too big: %s", global_state.runstate);
118 trace_migrate_state_too_big();
119 return -EINVAL;
120 }
121 return 0;
122}
123
Juan Quintela172c4352015-07-08 13:56:26 +0200124static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200125{
Juan Quintela172c4352015-07-08 13:56:26 +0200126 return global_state.received;
127}
128
129static RunState global_state_get_runstate(void)
130{
131 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200132}
133
Juan Quintela13d16812014-10-08 13:58:24 +0200134void global_state_set_optional(void)
135{
136 global_state.optional = true;
137}
138
139static bool global_state_needed(void *opaque)
140{
141 GlobalState *s = opaque;
142 char *runstate = (char *)s->runstate;
143
144 /* If it is not optional, it is mandatory */
145
146 if (s->optional == false) {
147 return true;
148 }
149
150 /* If state is running or paused, it is not needed */
151
152 if (strcmp(runstate, "running") == 0 ||
153 strcmp(runstate, "paused") == 0) {
154 return false;
155 }
156
157 /* for any other state it is needed */
158 return true;
159}
160
Juan Quinteladf4b1022014-10-08 10:58:10 +0200161static int global_state_post_load(void *opaque, int version_id)
162{
163 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200164 Error *local_err = NULL;
165 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200166 char *runstate = (char *)s->runstate;
167
Juan Quintela172c4352015-07-08 13:56:26 +0200168 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200169 trace_migrate_global_state_post_load(runstate);
170
Juan Quintela172c4352015-07-08 13:56:26 +0200171 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200172 -1, &local_err);
173
Juan Quintela172c4352015-07-08 13:56:26 +0200174 if (r == -1) {
175 if (local_err) {
176 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200177 }
Juan Quintela172c4352015-07-08 13:56:26 +0200178 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200179 }
Juan Quintela172c4352015-07-08 13:56:26 +0200180 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200181
Juan Quintela172c4352015-07-08 13:56:26 +0200182 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200183}
184
185static void global_state_pre_save(void *opaque)
186{
187 GlobalState *s = opaque;
188
189 trace_migrate_global_state_pre_save((char *)s->runstate);
190 s->size = strlen((char *)s->runstate) + 1;
191}
192
193static const VMStateDescription vmstate_globalstate = {
194 .name = "globalstate",
195 .version_id = 1,
196 .minimum_version_id = 1,
197 .post_load = global_state_post_load,
198 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200199 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200200 .fields = (VMStateField[]) {
201 VMSTATE_UINT32(size, GlobalState),
202 VMSTATE_BUFFER(runstate, GlobalState),
203 VMSTATE_END_OF_LIST()
204 },
205};
206
207void register_global_state(void)
208{
209 /* We would use it independently that we receive it */
210 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200211 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200212 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
213}
214
Juan Quintelab05dc722015-07-07 14:44:05 +0200215static void migrate_generate_event(int new_state)
216{
217 if (migrate_use_events()) {
218 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200219 }
220}
221
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000222/*
223 * Called on -incoming with a defer: uri.
224 * The migration can be started later after any parameters have been
225 * changed.
226 */
227static void deferred_incoming_migration(Error **errp)
228{
229 if (deferred_incoming) {
230 error_setg(errp, "Incoming migration already deferred");
231 }
232 deferred_incoming = true;
233}
234
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200235void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000236{
aliguori34c9dd82008-10-13 03:14:31 +0000237 const char *p;
238
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200239 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000240 if (!strcmp(uri, "defer")) {
241 deferred_incoming_migration(errp);
242 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200243 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400244#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000245 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400246 rdma_start_incoming_migration(p, errp);
247#endif
aliguori065e2812008-11-11 16:46:33 +0000248#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000249 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200250 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000251 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200252 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000253 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200254 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000255#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000256 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100257 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200258 }
aliguori5bb79102008-10-13 03:12:02 +0000259}
260
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200261static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200262{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200263 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100264 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200265 int ret;
266
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100267 migration_incoming_state_new(f);
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200268 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200269 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100270
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200271 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200272 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100273 migration_incoming_state_destroy();
274
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200275 if (ret < 0) {
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200276 migrate_generate_event(MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200277 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800278 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600279 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200280 }
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200281 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
Juan Quintela511c0232010-06-09 14:10:55 +0200282 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200283
Anthony Liguori0f154232011-11-14 15:09:45 -0600284 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100285 bdrv_invalidate_cache_all(&local_err);
286 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100287 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800288 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100289 exit(EXIT_FAILURE);
290 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600291
Juan Quintela172c4352015-07-08 13:56:26 +0200292 /* If global state section was not received or we are in running
293 state, we need to obey autostart. Any other state is set with
294 runstate_set. */
Juan Quinteladf4b1022014-10-08 10:58:10 +0200295
Juan Quintela172c4352015-07-08 13:56:26 +0200296 if (!global_state_received() ||
297 global_state_get_runstate() == RUN_STATE_RUNNING) {
Juan Quinteladf4b1022014-10-08 10:58:10 +0200298 if (autostart) {
299 vm_start();
300 } else {
301 runstate_set(RUN_STATE_PAUSED);
302 }
Juan Quintela172c4352015-07-08 13:56:26 +0200303 } else {
304 runstate_set(global_state_get_runstate());
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300305 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800306 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200307}
308
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200309void process_incoming_migration(QEMUFile *f)
310{
311 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
312 int fd = qemu_get_fd(f);
313
314 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800315 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100316 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200317 qemu_coroutine_enter(co, f);
318}
319
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400320/* amount of nanoseconds we are willing to wait for migration to be down.
321 * the choice of nanoseconds is because it is the maximum resolution that
322 * get_clock() can achieve. It is an internal measure. All user-visible
323 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100324static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400325
326uint64_t migrate_max_downtime(void)
327{
328 return max_downtime;
329}
330
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300331MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
332{
333 MigrationCapabilityStatusList *head = NULL;
334 MigrationCapabilityStatusList *caps;
335 MigrationState *s = migrate_get_current();
336 int i;
337
Michael Tokarev387eede2013-10-05 13:18:28 +0400338 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300339 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
340 if (head == NULL) {
341 head = g_malloc0(sizeof(*caps));
342 caps = head;
343 } else {
344 caps->next = g_malloc0(sizeof(*caps));
345 caps = caps->next;
346 }
347 caps->value =
348 g_malloc(sizeof(*caps->value));
349 caps->value->capability = i;
350 caps->value->state = s->enabled_capabilities[i];
351 }
352
353 return head;
354}
355
Liang Li85de8322015-03-23 16:32:28 +0800356MigrationParameters *qmp_query_migrate_parameters(Error **errp)
357{
358 MigrationParameters *params;
359 MigrationState *s = migrate_get_current();
360
361 params = g_malloc0(sizeof(*params));
362 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
363 params->compress_threads =
364 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
365 params->decompress_threads =
366 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
367
368 return params;
369}
370
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300371static void get_xbzrle_cache_stats(MigrationInfo *info)
372{
373 if (migrate_use_xbzrle()) {
374 info->has_xbzrle_cache = true;
375 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
376 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
377 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
378 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
379 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800380 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300381 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
382 }
383}
384
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300385MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000386{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300387 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200388 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000389
Juan Quintela17549e82011-10-05 13:50:43 +0200390 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800391 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200392 /* no migration has happened ever */
393 break;
zhanghailiang31194732015-03-13 16:08:38 +0800394 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400395 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400396 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400397 break;
zhanghailiang31194732015-03-13 16:08:38 +0800398 case MIGRATION_STATUS_ACTIVE:
399 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300400 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200401 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100402 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200403 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200404 info->has_expected_downtime = true;
405 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400406 info->has_setup_time = true;
407 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200408
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300409 info->has_ram = true;
410 info->ram = g_malloc0(sizeof(*info->ram));
411 info->ram->transferred = ram_bytes_transferred();
412 info->ram->remaining = ram_bytes_remaining();
413 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300414 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100415 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300416 info->ram->normal = norm_mig_pages_transferred();
417 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200418 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400419 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800420 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200421
Juan Quintela17549e82011-10-05 13:50:43 +0200422 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300423 info->has_disk = true;
424 info->disk = g_malloc0(sizeof(*info->disk));
425 info->disk->transferred = blk_mig_bytes_transferred();
426 info->disk->remaining = blk_mig_bytes_remaining();
427 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000428 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300429
430 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200431 break;
zhanghailiang31194732015-03-13 16:08:38 +0800432 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300433 get_xbzrle_cache_stats(info);
434
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300435 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900436 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200437 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200438 info->has_downtime = true;
439 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400440 info->has_setup_time = true;
441 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200442
443 info->has_ram = true;
444 info->ram = g_malloc0(sizeof(*info->ram));
445 info->ram->transferred = ram_bytes_transferred();
446 info->ram->remaining = 0;
447 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300448 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100449 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300450 info->ram->normal = norm_mig_pages_transferred();
451 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400452 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800453 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200454 break;
zhanghailiang31194732015-03-13 16:08:38 +0800455 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300456 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200457 break;
zhanghailiang31194732015-03-13 16:08:38 +0800458 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300459 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200460 break;
aliguori5bb79102008-10-13 03:12:02 +0000461 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800462 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300463
464 return info;
aliguori5bb79102008-10-13 03:12:02 +0000465}
466
Orit Wasserman00458432012-08-06 21:42:48 +0300467void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
468 Error **errp)
469{
470 MigrationState *s = migrate_get_current();
471 MigrationCapabilityStatusList *cap;
472
zhanghailiang31194732015-03-13 16:08:38 +0800473 if (s->state == MIGRATION_STATUS_ACTIVE ||
474 s->state == MIGRATION_STATUS_SETUP) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100475 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300476 return;
477 }
478
479 for (cap = params; cap; cap = cap->next) {
480 s->enabled_capabilities[cap->value->capability] = cap->value->state;
481 }
482}
483
Liang Li85de8322015-03-23 16:32:28 +0800484void qmp_migrate_set_parameters(bool has_compress_level,
485 int64_t compress_level,
486 bool has_compress_threads,
487 int64_t compress_threads,
488 bool has_decompress_threads,
489 int64_t decompress_threads, Error **errp)
490{
491 MigrationState *s = migrate_get_current();
492
493 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100494 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
495 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800496 return;
497 }
498 if (has_compress_threads &&
499 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100500 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
501 "compress_threads",
502 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800503 return;
504 }
505 if (has_decompress_threads &&
506 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100507 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
508 "decompress_threads",
509 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800510 return;
511 }
512
513 if (has_compress_level) {
514 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
515 }
516 if (has_compress_threads) {
517 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
518 }
519 if (has_decompress_threads) {
520 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
521 decompress_threads;
522 }
523}
524
aliguori065e2812008-11-11 16:46:33 +0000525/* shared migration helpers */
526
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000527static void migrate_set_state(MigrationState *s, int old_state, int new_state)
528{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200529 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200530 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200531 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000532 }
533}
534
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100535static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000536{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100537 MigrationState *s = opaque;
538
539 qemu_bh_delete(s->cleanup_bh);
540 s->cleanup_bh = NULL;
541
aliguori065e2812008-11-11 16:46:33 +0000542 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100543 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100544 qemu_mutex_unlock_iothread();
545 qemu_thread_join(&s->thread);
546 qemu_mutex_lock_iothread();
547
Liang Li8706d2d2015-03-23 16:32:17 +0800548 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100549 qemu_fclose(s->file);
550 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000551 }
552
zhanghailiang31194732015-03-13 16:08:38 +0800553 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100554
zhanghailiang31194732015-03-13 16:08:38 +0800555 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100556 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800557 if (s->state == MIGRATION_STATUS_CANCELLING) {
558 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
559 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000560 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100561 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100562
563 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000564}
565
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200566void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000567{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100568 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100569 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200570 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100571 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100572}
573
Juan Quintela0edda1c2010-05-11 16:28:39 +0200574static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000575{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000576 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000577 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100578 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000579
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000580 do {
581 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800582 if (old_state != MIGRATION_STATUS_SETUP &&
583 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000584 break;
585 }
zhanghailiang31194732015-03-13 16:08:38 +0800586 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
587 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000588
589 /*
590 * If we're unlucky the migration code might be stuck somewhere in a
591 * send/write while the network has failed and is waiting to timeout;
592 * if we've got shutdown(2) available then we can force it to quit.
593 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
594 * called in a bh, so there is no race against this cancel.
595 */
zhanghailiang31194732015-03-13 16:08:38 +0800596 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000597 qemu_file_shutdown(f);
598 }
aliguori065e2812008-11-11 16:46:33 +0000599}
600
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100601void add_migration_state_change_notifier(Notifier *notify)
602{
603 notifier_list_add(&migration_state_notifiers, notify);
604}
605
606void remove_migration_state_change_notifier(Notifier *notify)
607{
Paolo Bonzini31552522012-01-13 17:34:01 +0100608 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100609}
610
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200611bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200612{
zhanghailiang31194732015-03-13 16:08:38 +0800613 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200614}
615
Juan Quintela70736932011-02-23 00:43:59 +0100616bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100617{
zhanghailiang31194732015-03-13 16:08:38 +0800618 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100619}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200620
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200621bool migration_has_failed(MigrationState *s)
622{
zhanghailiang31194732015-03-13 16:08:38 +0800623 return (s->state == MIGRATION_STATUS_CANCELLED ||
624 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200625}
626
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300627static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200628{
Juan Quintela17549e82011-10-05 13:50:43 +0200629 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100630 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300631 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300632 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800633 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
634 int compress_thread_count =
635 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
636 int decompress_thread_count =
637 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300638
639 memcpy(enabled_capabilities, s->enabled_capabilities,
640 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200641
Juan Quintela17549e82011-10-05 13:50:43 +0200642 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300643 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300644 memcpy(s->enabled_capabilities, enabled_capabilities,
645 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300646 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100647
Liang Li43c60a82015-03-23 16:32:27 +0800648 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
649 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
650 compress_thread_count;
651 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
652 decompress_thread_count;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200653 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200654 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200655
Alex Blighbc72ad62013-08-21 16:03:08 +0100656 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200657 return s;
658}
Juan Quintelacab30142011-02-22 23:54:21 +0100659
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600660static GSList *migration_blockers;
661
662void migrate_add_blocker(Error *reason)
663{
664 migration_blockers = g_slist_prepend(migration_blockers, reason);
665}
666
667void migrate_del_blocker(Error *reason)
668{
669 migration_blockers = g_slist_remove(migration_blockers, reason);
670}
671
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000672void qmp_migrate_incoming(const char *uri, Error **errp)
673{
674 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000675 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000676
677 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000678 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000679 return;
680 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000681 if (!once) {
682 error_setg(errp, "The incoming migration has already been started");
683 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000684
685 qemu_start_incoming_migration(uri, &local_err);
686
687 if (local_err) {
688 error_propagate(errp, local_err);
689 return;
690 }
691
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000692 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000693}
694
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200695void qmp_migrate(const char *uri, bool has_blk, bool blk,
696 bool has_inc, bool inc, bool has_detach, bool detach,
697 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100698{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200699 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200700 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300701 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100702 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100703
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900704 params.blk = has_blk && blk;
705 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300706
zhanghailiang31194732015-03-13 16:08:38 +0800707 if (s->state == MIGRATION_STATUS_ACTIVE ||
708 s->state == MIGRATION_STATUS_SETUP ||
709 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100710 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200711 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100712 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100713 if (runstate_check(RUN_STATE_INMIGRATE)) {
714 error_setg(errp, "Guest is waiting for an incoming migration");
715 return;
716 }
717
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200718 if (qemu_savevm_state_blocked(errp)) {
719 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100720 }
721
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600722 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200723 *errp = error_copy(migration_blockers->data);
724 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600725 }
726
Juan Quintela656a2332015-07-01 09:32:29 +0200727 /* We are starting a new migration, so we want to start in a clean
728 state. This change is only needed if previous migration
729 failed/was cancelled. We don't use migrate_set_state() because
730 we are setting the initial state, not changing it. */
731 s->state = MIGRATION_STATUS_NONE;
732
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300733 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100734
735 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200736 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400737#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800738 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400739 rdma_start_outgoing_migration(s, p, &local_err);
740#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100741#if !defined(WIN32)
742 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200743 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100744 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200745 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100746 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200747 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100748#endif
749 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100750 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
751 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +0200752 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200753 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100754 }
755
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200756 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200757 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200758 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200759 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100760 }
Juan Quintelacab30142011-02-22 23:54:21 +0100761}
762
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200763void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100764{
Juan Quintela17549e82011-10-05 13:50:43 +0200765 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100766}
767
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300768void qmp_migrate_set_cache_size(int64_t value, Error **errp)
769{
770 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200771 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300772
773 /* Check for truncation */
774 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100775 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
776 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300777 return;
778 }
779
Orit Wassermana5615b12014-01-30 20:08:36 +0200780 /* Cache should not be larger than guest ram size */
781 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100782 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
783 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +0200784 return;
785 }
786
Orit Wassermanc91e6812014-01-30 20:08:34 +0200787 new_size = xbzrle_cache_resize(value);
788 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100789 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
790 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +0200791 return;
792 }
793
794 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300795}
796
797int64_t qmp_query_migrate_cache_size(Error **errp)
798{
799 return migrate_xbzrle_cache_size();
800}
801
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200802void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100803{
Juan Quintelacab30142011-02-22 23:54:21 +0100804 MigrationState *s;
805
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200806 if (value < 0) {
807 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100808 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100809 if (value > SIZE_MAX) {
810 value = SIZE_MAX;
811 }
Juan Quintelacab30142011-02-22 23:54:21 +0100812
Juan Quintela17549e82011-10-05 13:50:43 +0200813 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200814 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100815 if (s->file) {
816 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
817 }
Juan Quintelacab30142011-02-22 23:54:21 +0100818}
819
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200820void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100821{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200822 value *= 1e9;
823 value = MAX(0, MIN(UINT64_MAX, value));
824 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000825}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300826
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600827bool migrate_auto_converge(void)
828{
829 MigrationState *s;
830
831 s = migrate_get_current();
832
833 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
834}
835
Peter Lieven323004a2013-07-18 09:48:50 +0200836bool migrate_zero_blocks(void)
837{
838 MigrationState *s;
839
840 s = migrate_get_current();
841
842 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
843}
844
Liang Li8706d2d2015-03-23 16:32:17 +0800845bool migrate_use_compression(void)
846{
Liang Lidde4e692015-03-23 16:32:26 +0800847 MigrationState *s;
848
849 s = migrate_get_current();
850
851 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800852}
853
854int migrate_compress_level(void)
855{
856 MigrationState *s;
857
858 s = migrate_get_current();
859
Liang Li43c60a82015-03-23 16:32:27 +0800860 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800861}
862
863int migrate_compress_threads(void)
864{
865 MigrationState *s;
866
867 s = migrate_get_current();
868
Liang Li43c60a82015-03-23 16:32:27 +0800869 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800870}
871
Liang Li3fcb38c2015-03-23 16:32:18 +0800872int migrate_decompress_threads(void)
873{
874 MigrationState *s;
875
876 s = migrate_get_current();
877
Liang Li43c60a82015-03-23 16:32:27 +0800878 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800879}
880
Juan Quintelab05dc722015-07-07 14:44:05 +0200881bool migrate_use_events(void)
882{
883 MigrationState *s;
884
885 s = migrate_get_current();
886
887 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
888}
889
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300890int migrate_use_xbzrle(void)
891{
892 MigrationState *s;
893
894 s = migrate_get_current();
895
896 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
897}
898
899int64_t migrate_xbzrle_cache_size(void)
900{
901 MigrationState *s;
902
903 s = migrate_get_current();
904
905 return s->xbzrle_cache_size;
906}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200907
908/* migration thread support */
909
Juan Quintela5f496a12013-02-22 17:36:30 +0100910static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200911{
Juan Quintela9848a402012-12-19 09:55:50 +0100912 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100913 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
914 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100915 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200916 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100917 int64_t start_time = initial_time;
918 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +0200919
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +0100920 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100921 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200922
Alex Blighbc72ad62013-08-21 16:03:08 +0100923 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +0800924 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400925
zhanghailiang31194732015-03-13 16:08:38 +0800926 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +0100927 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +0200928 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200929
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100930 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +0200931 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100932 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +0200933 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100934 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +0200935 } else {
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200936 int ret;
937
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100938 qemu_mutex_lock_iothread();
Alex Blighbc72ad62013-08-21 16:03:08 +0100939 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintelac369f402012-10-03 20:33:34 +0200940 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100941 old_vm_running = runstate_is_running();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200942
Juan Quinteladf4b1022014-10-08 10:58:10 +0200943 ret = global_state_store();
944 if (!ret) {
945 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
946 if (ret >= 0) {
947 qemu_file_set_rate_limit(s->file, INT64_MAX);
948 qemu_savevm_state_complete(s->file);
949 }
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200950 }
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100951 qemu_mutex_unlock_iothread();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200952
953 if (ret < 0) {
zhanghailiang31194732015-03-13 16:08:38 +0800954 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
955 MIGRATION_STATUS_FAILED);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200956 break;
957 }
958
Paolo Bonzini059f8962013-02-22 17:36:32 +0100959 if (!qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800960 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
961 MIGRATION_STATUS_COMPLETED);
Paolo Bonzini059f8962013-02-22 17:36:32 +0100962 break;
963 }
Juan Quintelac369f402012-10-03 20:33:34 +0200964 }
965 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100966
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100967 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800968 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
969 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100970 break;
971 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100972 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200973 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100974 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -0500975 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200976 double bandwidth = transferred_bytes / time_spent;
977 max_size = bandwidth * migrate_max_downtime() / 1000000;
978
Michael R. Hines7e114f82013-06-25 21:35:30 -0400979 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
980 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
981
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100982 trace_migrate_transferred(transferred_bytes, time_spent,
983 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100984 /* if we haven't sent anything, we don't want to recalculate
985 10000 is a small enough number for our purposes */
986 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
987 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
988 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200989
Paolo Bonzini1964a392013-02-22 17:36:45 +0100990 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200991 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100992 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200993 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100994 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200995 /* usleep expects microseconds */
996 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
997 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100998 }
999
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001000 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +08001001 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001002 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +02001003 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001004 s->total_time = end_time - s->total_time;
1005 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +02001006 if (s->total_time) {
1007 s->mbps = (((double) transferred_bytes * 8.0) /
1008 ((double) s->total_time)) / 1000;
1009 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001010 runstate_set(RUN_STATE_POSTMIGRATE);
1011 } else {
1012 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001013 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001014 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001015 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001016 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001017 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001018
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001019 return NULL;
1020}
1021
Juan Quintela9848a402012-12-19 09:55:50 +01001022void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001023{
Juan Quintelacc283e32013-02-01 11:12:26 +01001024 /* This is a best 1st approximation. ns to ms */
1025 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001026 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001027
Paolo Bonzini442773c2013-02-22 17:36:44 +01001028 qemu_file_set_rate_limit(s->file,
1029 s->bandwidth_limit / XFER_LIMIT_RATIO);
1030
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001031 /* Notify before starting migration thread */
1032 notifier_list_notify(&migration_state_notifiers, s);
1033
Liang Li8706d2d2015-03-23 16:32:17 +08001034 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001035 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001036 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001037}