blob: 8a1af3b123280920db28d9d83ee6ee12252f5620 [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
Jason J. Herne1626fee2015-09-08 13:12:34 -040047/* Define default autoconverge cpu throttle migration parameters */
48#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20
49#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080050
Orit Wasserman17ad9b32012-08-06 21:42:53 +030051/* Migration XBZRLE default cache size */
52#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
53
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010054static NotifierList migration_state_notifiers =
55 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
56
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000057static bool deferred_incoming;
58
Juan Quintela17549e82011-10-05 13:50:43 +020059/* When we add fault tolerance, we could have several
60 migrations at once. For now we don't need to add
61 dynamic creation of migration */
62
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010063/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020064MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020065{
66 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080067 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010068 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030069 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040070 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080071 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
72 DEFAULT_MIGRATE_COMPRESS_LEVEL,
73 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
74 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
75 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
76 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Jason J. Herne1626fee2015-09-08 13:12:34 -040077 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
78 DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL,
79 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
80 DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT,
Juan Quintela17549e82011-10-05 13:50:43 +020081 };
82
83 return &current_migration;
84}
85
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010086/* For incoming */
87static MigrationIncomingState *mis_current;
88
89MigrationIncomingState *migration_incoming_get_current(void)
90{
91 return mis_current;
92}
93
94MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
95{
Markus Armbruster97f3ad32015-09-14 13:51:31 +020096 mis_current = g_new0(MigrationIncomingState, 1);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010097 mis_current->file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010098 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010099
100 return mis_current;
101}
102
103void migration_incoming_state_destroy(void)
104{
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100105 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100106 g_free(mis_current);
107 mis_current = NULL;
108}
109
Juan Quinteladf4b1022014-10-08 10:58:10 +0200110
111typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200112 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200113 uint32_t size;
114 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200115 RunState state;
116 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200117} GlobalState;
118
119static GlobalState global_state;
120
Juan Quintela560d0272015-07-15 09:53:46 +0200121int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200122{
123 if (!runstate_store((char *)global_state.runstate,
124 sizeof(global_state.runstate))) {
125 error_report("runstate name too big: %s", global_state.runstate);
126 trace_migrate_state_too_big();
127 return -EINVAL;
128 }
129 return 0;
130}
131
Anthony PERARDc69adea2015-08-03 15:29:19 +0100132void global_state_store_running(void)
133{
134 const char *state = RunState_lookup[RUN_STATE_RUNNING];
135 strncpy((char *)global_state.runstate,
136 state, sizeof(global_state.runstate));
137}
138
Juan Quintela172c4352015-07-08 13:56:26 +0200139static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200140{
Juan Quintela172c4352015-07-08 13:56:26 +0200141 return global_state.received;
142}
143
144static RunState global_state_get_runstate(void)
145{
146 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200147}
148
Juan Quintela13d16812014-10-08 13:58:24 +0200149void global_state_set_optional(void)
150{
151 global_state.optional = true;
152}
153
154static bool global_state_needed(void *opaque)
155{
156 GlobalState *s = opaque;
157 char *runstate = (char *)s->runstate;
158
159 /* If it is not optional, it is mandatory */
160
161 if (s->optional == false) {
162 return true;
163 }
164
165 /* If state is running or paused, it is not needed */
166
167 if (strcmp(runstate, "running") == 0 ||
168 strcmp(runstate, "paused") == 0) {
169 return false;
170 }
171
172 /* for any other state it is needed */
173 return true;
174}
175
Juan Quinteladf4b1022014-10-08 10:58:10 +0200176static int global_state_post_load(void *opaque, int version_id)
177{
178 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200179 Error *local_err = NULL;
180 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200181 char *runstate = (char *)s->runstate;
182
Juan Quintela172c4352015-07-08 13:56:26 +0200183 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200184 trace_migrate_global_state_post_load(runstate);
185
Juan Quintela172c4352015-07-08 13:56:26 +0200186 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200187 -1, &local_err);
188
Juan Quintela172c4352015-07-08 13:56:26 +0200189 if (r == -1) {
190 if (local_err) {
191 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200192 }
Juan Quintela172c4352015-07-08 13:56:26 +0200193 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200194 }
Juan Quintela172c4352015-07-08 13:56:26 +0200195 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200196
Juan Quintela172c4352015-07-08 13:56:26 +0200197 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200198}
199
200static void global_state_pre_save(void *opaque)
201{
202 GlobalState *s = opaque;
203
204 trace_migrate_global_state_pre_save((char *)s->runstate);
205 s->size = strlen((char *)s->runstate) + 1;
206}
207
208static const VMStateDescription vmstate_globalstate = {
209 .name = "globalstate",
210 .version_id = 1,
211 .minimum_version_id = 1,
212 .post_load = global_state_post_load,
213 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200214 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200215 .fields = (VMStateField[]) {
216 VMSTATE_UINT32(size, GlobalState),
217 VMSTATE_BUFFER(runstate, GlobalState),
218 VMSTATE_END_OF_LIST()
219 },
220};
221
222void register_global_state(void)
223{
224 /* We would use it independently that we receive it */
225 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200226 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200227 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
228}
229
Juan Quintelab05dc722015-07-07 14:44:05 +0200230static void migrate_generate_event(int new_state)
231{
232 if (migrate_use_events()) {
233 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200234 }
235}
236
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000237/*
238 * Called on -incoming with a defer: uri.
239 * The migration can be started later after any parameters have been
240 * changed.
241 */
242static void deferred_incoming_migration(Error **errp)
243{
244 if (deferred_incoming) {
245 error_setg(errp, "Incoming migration already deferred");
246 }
247 deferred_incoming = true;
248}
249
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200250void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000251{
aliguori34c9dd82008-10-13 03:14:31 +0000252 const char *p;
253
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200254 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000255 if (!strcmp(uri, "defer")) {
256 deferred_incoming_migration(errp);
257 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200258 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400259#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000260 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400261 rdma_start_incoming_migration(p, errp);
262#endif
aliguori065e2812008-11-11 16:46:33 +0000263#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000264 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200265 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000266 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200267 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000268 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200269 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000270#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000271 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100272 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200273 }
aliguori5bb79102008-10-13 03:12:02 +0000274}
275
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200276static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200277{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200278 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100279 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200280 int ret;
281
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100282 migration_incoming_state_new(f);
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200283 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200284 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100285
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200286 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200287 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100288 migration_incoming_state_destroy();
289
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200290 if (ret < 0) {
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200291 migrate_generate_event(MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200292 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800293 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600294 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200295 }
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200296 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
Juan Quintela511c0232010-06-09 14:10:55 +0200297 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200298
Anthony Liguori0f154232011-11-14 15:09:45 -0600299 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100300 bdrv_invalidate_cache_all(&local_err);
301 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100302 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800303 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100304 exit(EXIT_FAILURE);
305 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600306
Juan Quintela172c4352015-07-08 13:56:26 +0200307 /* If global state section was not received or we are in running
308 state, we need to obey autostart. Any other state is set with
309 runstate_set. */
Juan Quinteladf4b1022014-10-08 10:58:10 +0200310
Juan Quintela172c4352015-07-08 13:56:26 +0200311 if (!global_state_received() ||
312 global_state_get_runstate() == RUN_STATE_RUNNING) {
Juan Quinteladf4b1022014-10-08 10:58:10 +0200313 if (autostart) {
314 vm_start();
315 } else {
316 runstate_set(RUN_STATE_PAUSED);
317 }
Juan Quintela172c4352015-07-08 13:56:26 +0200318 } else {
319 runstate_set(global_state_get_runstate());
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300320 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800321 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200322}
323
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200324void process_incoming_migration(QEMUFile *f)
325{
326 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
327 int fd = qemu_get_fd(f);
328
329 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800330 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100331 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200332 qemu_coroutine_enter(co, f);
333}
334
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400335/* amount of nanoseconds we are willing to wait for migration to be down.
336 * the choice of nanoseconds is because it is the maximum resolution that
337 * get_clock() can achieve. It is an internal measure. All user-visible
338 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100339static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400340
341uint64_t migrate_max_downtime(void)
342{
343 return max_downtime;
344}
345
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300346MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
347{
348 MigrationCapabilityStatusList *head = NULL;
349 MigrationCapabilityStatusList *caps;
350 MigrationState *s = migrate_get_current();
351 int i;
352
Michael Tokarev387eede2013-10-05 13:18:28 +0400353 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300354 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
355 if (head == NULL) {
356 head = g_malloc0(sizeof(*caps));
357 caps = head;
358 } else {
359 caps->next = g_malloc0(sizeof(*caps));
360 caps = caps->next;
361 }
362 caps->value =
363 g_malloc(sizeof(*caps->value));
364 caps->value->capability = i;
365 caps->value->state = s->enabled_capabilities[i];
366 }
367
368 return head;
369}
370
Liang Li85de8322015-03-23 16:32:28 +0800371MigrationParameters *qmp_query_migrate_parameters(Error **errp)
372{
373 MigrationParameters *params;
374 MigrationState *s = migrate_get_current();
375
376 params = g_malloc0(sizeof(*params));
377 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
378 params->compress_threads =
379 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
380 params->decompress_threads =
381 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400382 params->x_cpu_throttle_initial =
383 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
384 params->x_cpu_throttle_increment =
385 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Liang Li85de8322015-03-23 16:32:28 +0800386
387 return params;
388}
389
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300390static void get_xbzrle_cache_stats(MigrationInfo *info)
391{
392 if (migrate_use_xbzrle()) {
393 info->has_xbzrle_cache = true;
394 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
395 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
396 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
397 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
398 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800399 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300400 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
401 }
402}
403
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300404MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000405{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300406 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200407 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000408
Juan Quintela17549e82011-10-05 13:50:43 +0200409 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800410 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200411 /* no migration has happened ever */
412 break;
zhanghailiang31194732015-03-13 16:08:38 +0800413 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400414 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400415 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400416 break;
zhanghailiang31194732015-03-13 16:08:38 +0800417 case MIGRATION_STATUS_ACTIVE:
418 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300419 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200420 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100421 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200422 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200423 info->has_expected_downtime = true;
424 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400425 info->has_setup_time = true;
426 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200427
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300428 info->has_ram = true;
429 info->ram = g_malloc0(sizeof(*info->ram));
430 info->ram->transferred = ram_bytes_transferred();
431 info->ram->remaining = ram_bytes_remaining();
432 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300433 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100434 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300435 info->ram->normal = norm_mig_pages_transferred();
436 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200437 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400438 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800439 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200440
Juan Quintela17549e82011-10-05 13:50:43 +0200441 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300442 info->has_disk = true;
443 info->disk = g_malloc0(sizeof(*info->disk));
444 info->disk->transferred = blk_mig_bytes_transferred();
445 info->disk->remaining = blk_mig_bytes_remaining();
446 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000447 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300448
449 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200450 break;
zhanghailiang31194732015-03-13 16:08:38 +0800451 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300452 get_xbzrle_cache_stats(info);
453
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300454 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900455 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200456 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200457 info->has_downtime = true;
458 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400459 info->has_setup_time = true;
460 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200461
462 info->has_ram = true;
463 info->ram = g_malloc0(sizeof(*info->ram));
464 info->ram->transferred = ram_bytes_transferred();
465 info->ram->remaining = 0;
466 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300467 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100468 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300469 info->ram->normal = norm_mig_pages_transferred();
470 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400471 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800472 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200473 break;
zhanghailiang31194732015-03-13 16:08:38 +0800474 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300475 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200476 break;
zhanghailiang31194732015-03-13 16:08:38 +0800477 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300478 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200479 break;
aliguori5bb79102008-10-13 03:12:02 +0000480 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800481 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300482
483 return info;
aliguori5bb79102008-10-13 03:12:02 +0000484}
485
Orit Wasserman00458432012-08-06 21:42:48 +0300486void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
487 Error **errp)
488{
489 MigrationState *s = migrate_get_current();
490 MigrationCapabilityStatusList *cap;
491
zhanghailiang31194732015-03-13 16:08:38 +0800492 if (s->state == MIGRATION_STATUS_ACTIVE ||
493 s->state == MIGRATION_STATUS_SETUP) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100494 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300495 return;
496 }
497
498 for (cap = params; cap; cap = cap->next) {
499 s->enabled_capabilities[cap->value->capability] = cap->value->state;
500 }
501}
502
Liang Li85de8322015-03-23 16:32:28 +0800503void qmp_migrate_set_parameters(bool has_compress_level,
504 int64_t compress_level,
505 bool has_compress_threads,
506 int64_t compress_threads,
507 bool has_decompress_threads,
Jason J. Herne1626fee2015-09-08 13:12:34 -0400508 int64_t decompress_threads,
509 bool has_x_cpu_throttle_initial,
510 int64_t x_cpu_throttle_initial,
511 bool has_x_cpu_throttle_increment,
512 int64_t x_cpu_throttle_increment, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800513{
514 MigrationState *s = migrate_get_current();
515
516 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100517 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
518 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800519 return;
520 }
521 if (has_compress_threads &&
522 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100523 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
524 "compress_threads",
525 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800526 return;
527 }
528 if (has_decompress_threads &&
529 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100530 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
531 "decompress_threads",
532 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800533 return;
534 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400535 if (has_x_cpu_throttle_initial &&
536 (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
537 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
538 "x_cpu_throttle_initial",
539 "an integer in the range of 1 to 99");
540 }
541 if (has_x_cpu_throttle_increment &&
542 (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
543 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
544 "x_cpu_throttle_increment",
545 "an integer in the range of 1 to 99");
546 }
Liang Li85de8322015-03-23 16:32:28 +0800547
548 if (has_compress_level) {
549 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
550 }
551 if (has_compress_threads) {
552 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
553 }
554 if (has_decompress_threads) {
555 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
556 decompress_threads;
557 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400558 if (has_x_cpu_throttle_initial) {
559 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
560 x_cpu_throttle_initial;
561 }
562
563 if (has_x_cpu_throttle_increment) {
564 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
565 x_cpu_throttle_increment;
566 }
Liang Li85de8322015-03-23 16:32:28 +0800567}
568
aliguori065e2812008-11-11 16:46:33 +0000569/* shared migration helpers */
570
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000571static void migrate_set_state(MigrationState *s, int old_state, int new_state)
572{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200573 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200574 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200575 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000576 }
577}
578
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100579static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000580{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100581 MigrationState *s = opaque;
582
583 qemu_bh_delete(s->cleanup_bh);
584 s->cleanup_bh = NULL;
585
aliguori065e2812008-11-11 16:46:33 +0000586 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100587 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100588 qemu_mutex_unlock_iothread();
589 qemu_thread_join(&s->thread);
590 qemu_mutex_lock_iothread();
591
Liang Li8706d2d2015-03-23 16:32:17 +0800592 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100593 qemu_fclose(s->file);
594 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000595 }
596
zhanghailiang31194732015-03-13 16:08:38 +0800597 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100598
zhanghailiang31194732015-03-13 16:08:38 +0800599 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100600 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800601 if (s->state == MIGRATION_STATUS_CANCELLING) {
602 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
603 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000604 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100605 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100606
607 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000608}
609
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200610void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000611{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100612 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100613 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200614 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100615 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100616}
617
Juan Quintela0edda1c2010-05-11 16:28:39 +0200618static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000619{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000620 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000621 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100622 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000623
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000624 do {
625 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800626 if (old_state != MIGRATION_STATUS_SETUP &&
627 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000628 break;
629 }
zhanghailiang31194732015-03-13 16:08:38 +0800630 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
631 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000632
633 /*
634 * If we're unlucky the migration code might be stuck somewhere in a
635 * send/write while the network has failed and is waiting to timeout;
636 * if we've got shutdown(2) available then we can force it to quit.
637 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
638 * called in a bh, so there is no race against this cancel.
639 */
zhanghailiang31194732015-03-13 16:08:38 +0800640 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000641 qemu_file_shutdown(f);
642 }
aliguori065e2812008-11-11 16:46:33 +0000643}
644
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100645void add_migration_state_change_notifier(Notifier *notify)
646{
647 notifier_list_add(&migration_state_notifiers, notify);
648}
649
650void remove_migration_state_change_notifier(Notifier *notify)
651{
Paolo Bonzini31552522012-01-13 17:34:01 +0100652 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100653}
654
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200655bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200656{
zhanghailiang31194732015-03-13 16:08:38 +0800657 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200658}
659
Juan Quintela70736932011-02-23 00:43:59 +0100660bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100661{
zhanghailiang31194732015-03-13 16:08:38 +0800662 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100663}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200664
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200665bool migration_has_failed(MigrationState *s)
666{
zhanghailiang31194732015-03-13 16:08:38 +0800667 return (s->state == MIGRATION_STATUS_CANCELLED ||
668 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200669}
670
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300671static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200672{
Juan Quintela17549e82011-10-05 13:50:43 +0200673 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100674 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300675 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300676 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800677 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
678 int compress_thread_count =
679 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
680 int decompress_thread_count =
681 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400682 int x_cpu_throttle_initial =
683 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
684 int x_cpu_throttle_increment =
685 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300686
687 memcpy(enabled_capabilities, s->enabled_capabilities,
688 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200689
Juan Quintela17549e82011-10-05 13:50:43 +0200690 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300691 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300692 memcpy(s->enabled_capabilities, enabled_capabilities,
693 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300694 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100695
Liang Li43c60a82015-03-23 16:32:27 +0800696 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
697 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
698 compress_thread_count;
699 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
700 decompress_thread_count;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400701 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
702 x_cpu_throttle_initial;
703 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
704 x_cpu_throttle_increment;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200705 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200706 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200707
Alex Blighbc72ad62013-08-21 16:03:08 +0100708 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200709 return s;
710}
Juan Quintelacab30142011-02-22 23:54:21 +0100711
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600712static GSList *migration_blockers;
713
714void migrate_add_blocker(Error *reason)
715{
716 migration_blockers = g_slist_prepend(migration_blockers, reason);
717}
718
719void migrate_del_blocker(Error *reason)
720{
721 migration_blockers = g_slist_remove(migration_blockers, reason);
722}
723
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000724void qmp_migrate_incoming(const char *uri, Error **errp)
725{
726 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000727 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000728
729 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000730 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000731 return;
732 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000733 if (!once) {
734 error_setg(errp, "The incoming migration has already been started");
735 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000736
737 qemu_start_incoming_migration(uri, &local_err);
738
739 if (local_err) {
740 error_propagate(errp, local_err);
741 return;
742 }
743
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000744 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000745}
746
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200747void qmp_migrate(const char *uri, bool has_blk, bool blk,
748 bool has_inc, bool inc, bool has_detach, bool detach,
749 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100750{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200751 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200752 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300753 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100754 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100755
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900756 params.blk = has_blk && blk;
757 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300758
zhanghailiang31194732015-03-13 16:08:38 +0800759 if (s->state == MIGRATION_STATUS_ACTIVE ||
760 s->state == MIGRATION_STATUS_SETUP ||
761 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100762 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200763 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100764 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100765 if (runstate_check(RUN_STATE_INMIGRATE)) {
766 error_setg(errp, "Guest is waiting for an incoming migration");
767 return;
768 }
769
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200770 if (qemu_savevm_state_blocked(errp)) {
771 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100772 }
773
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600774 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200775 *errp = error_copy(migration_blockers->data);
776 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600777 }
778
Juan Quintela656a2332015-07-01 09:32:29 +0200779 /* We are starting a new migration, so we want to start in a clean
780 state. This change is only needed if previous migration
781 failed/was cancelled. We don't use migrate_set_state() because
782 we are setting the initial state, not changing it. */
783 s->state = MIGRATION_STATUS_NONE;
784
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300785 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100786
787 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200788 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400789#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800790 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400791 rdma_start_outgoing_migration(s, p, &local_err);
792#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100793#if !defined(WIN32)
794 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200795 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100796 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200797 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100798 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200799 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100800#endif
801 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100802 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
803 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +0200804 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200805 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100806 }
807
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200808 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200809 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200810 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200811 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100812 }
Juan Quintelacab30142011-02-22 23:54:21 +0100813}
814
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200815void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100816{
Juan Quintela17549e82011-10-05 13:50:43 +0200817 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100818}
819
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300820void qmp_migrate_set_cache_size(int64_t value, Error **errp)
821{
822 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200823 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300824
825 /* Check for truncation */
826 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100827 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
828 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300829 return;
830 }
831
Orit Wassermana5615b12014-01-30 20:08:36 +0200832 /* Cache should not be larger than guest ram size */
833 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100834 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
835 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +0200836 return;
837 }
838
Orit Wassermanc91e6812014-01-30 20:08:34 +0200839 new_size = xbzrle_cache_resize(value);
840 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100841 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
842 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +0200843 return;
844 }
845
846 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300847}
848
849int64_t qmp_query_migrate_cache_size(Error **errp)
850{
851 return migrate_xbzrle_cache_size();
852}
853
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200854void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100855{
Juan Quintelacab30142011-02-22 23:54:21 +0100856 MigrationState *s;
857
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200858 if (value < 0) {
859 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100860 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100861 if (value > SIZE_MAX) {
862 value = SIZE_MAX;
863 }
Juan Quintelacab30142011-02-22 23:54:21 +0100864
Juan Quintela17549e82011-10-05 13:50:43 +0200865 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200866 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100867 if (s->file) {
868 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
869 }
Juan Quintelacab30142011-02-22 23:54:21 +0100870}
871
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200872void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100873{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200874 value *= 1e9;
875 value = MAX(0, MIN(UINT64_MAX, value));
876 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000877}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300878
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600879bool migrate_auto_converge(void)
880{
881 MigrationState *s;
882
883 s = migrate_get_current();
884
885 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
886}
887
Peter Lieven323004a2013-07-18 09:48:50 +0200888bool migrate_zero_blocks(void)
889{
890 MigrationState *s;
891
892 s = migrate_get_current();
893
894 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
895}
896
Liang Li8706d2d2015-03-23 16:32:17 +0800897bool migrate_use_compression(void)
898{
Liang Lidde4e692015-03-23 16:32:26 +0800899 MigrationState *s;
900
901 s = migrate_get_current();
902
903 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800904}
905
906int migrate_compress_level(void)
907{
908 MigrationState *s;
909
910 s = migrate_get_current();
911
Liang Li43c60a82015-03-23 16:32:27 +0800912 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800913}
914
915int migrate_compress_threads(void)
916{
917 MigrationState *s;
918
919 s = migrate_get_current();
920
Liang Li43c60a82015-03-23 16:32:27 +0800921 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800922}
923
Liang Li3fcb38c2015-03-23 16:32:18 +0800924int migrate_decompress_threads(void)
925{
926 MigrationState *s;
927
928 s = migrate_get_current();
929
Liang Li43c60a82015-03-23 16:32:27 +0800930 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800931}
932
Juan Quintelab05dc722015-07-07 14:44:05 +0200933bool migrate_use_events(void)
934{
935 MigrationState *s;
936
937 s = migrate_get_current();
938
939 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
940}
941
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300942int migrate_use_xbzrle(void)
943{
944 MigrationState *s;
945
946 s = migrate_get_current();
947
948 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
949}
950
951int64_t migrate_xbzrle_cache_size(void)
952{
953 MigrationState *s;
954
955 s = migrate_get_current();
956
957 return s->xbzrle_cache_size;
958}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200959
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +0100960/**
961 * migration_completion: Used by migration_thread when there's not much left.
962 * The caller 'breaks' the loop when this returns.
963 *
964 * @s: Current migration state
965 * @*old_vm_running: Pointer to old_vm_running flag
966 * @*start_time: Pointer to time to update
967 */
968static void migration_completion(MigrationState *s, bool *old_vm_running,
969 int64_t *start_time)
970{
971 int ret;
972
973 qemu_mutex_lock_iothread();
974 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
975 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
976 *old_vm_running = runstate_is_running();
977
978 ret = global_state_store();
979 if (!ret) {
980 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
981 if (ret >= 0) {
982 qemu_file_set_rate_limit(s->file, INT64_MAX);
983 qemu_savevm_state_complete(s->file);
984 }
985 }
986 qemu_mutex_unlock_iothread();
987
988 if (ret < 0) {
989 goto fail;
990 }
991
992 if (qemu_file_get_error(s->file)) {
993 trace_migration_completion_file_err();
994 goto fail;
995 }
996
997 migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_COMPLETED);
998 return;
999
1000fail:
1001 migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_FAILED);
1002}
1003
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001004/* migration thread support */
1005
Juan Quintela5f496a12013-02-22 17:36:30 +01001006static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001007{
Juan Quintela9848a402012-12-19 09:55:50 +01001008 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01001009 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1010 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001011 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001012 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001013 int64_t start_time = initial_time;
1014 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +02001015
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001016 rcu_register_thread();
1017
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001018 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001019 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001020
Alex Blighbc72ad62013-08-21 16:03:08 +01001021 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +08001022 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001023
zhanghailiang31194732015-03-13 16:08:38 +08001024 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001025 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001026 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001027
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001028 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +02001029 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001030 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001031 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001032 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +02001033 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001034 trace_migration_thread_low_pending(pending_size);
1035 migration_completion(s, &old_vm_running, &start_time);
1036 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001037 }
1038 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001039
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001040 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +08001041 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
1042 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001043 break;
1044 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001045 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001046 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001047 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001048 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001049 double bandwidth = transferred_bytes / time_spent;
1050 max_size = bandwidth * migrate_max_downtime() / 1000000;
1051
Michael R. Hines7e114f82013-06-25 21:35:30 -04001052 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1053 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1054
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001055 trace_migrate_transferred(transferred_bytes, time_spent,
1056 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001057 /* if we haven't sent anything, we don't want to recalculate
1058 10000 is a small enough number for our purposes */
1059 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1060 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1061 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001062
Paolo Bonzini1964a392013-02-22 17:36:45 +01001063 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001064 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001065 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001066 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001067 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001068 /* usleep expects microseconds */
1069 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1070 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001071 }
1072
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001073 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +08001074 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001075 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +02001076 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001077 s->total_time = end_time - s->total_time;
1078 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +02001079 if (s->total_time) {
1080 s->mbps = (((double) transferred_bytes * 8.0) /
1081 ((double) s->total_time)) / 1000;
1082 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001083 runstate_set(RUN_STATE_POSTMIGRATE);
1084 } else {
1085 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001086 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001087 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001088 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001089 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001090 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001091
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001092 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001093 return NULL;
1094}
1095
Juan Quintela9848a402012-12-19 09:55:50 +01001096void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001097{
Juan Quintelacc283e32013-02-01 11:12:26 +01001098 /* This is a best 1st approximation. ns to ms */
1099 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001100 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001101
Paolo Bonzini442773c2013-02-22 17:36:44 +01001102 qemu_file_set_rate_limit(s->file,
1103 s->bandwidth_limit / XFER_LIMIT_RATIO);
1104
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001105 /* Notify before starting migration thread */
1106 notifier_list_notify(&migration_state_notifiers, s);
1107
Liang Li8706d2d2015-03-23 16:32:17 +08001108 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001109 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001110 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001111}