blob: 3d40f2455690c893e9f8516fd83e5d153e26bbad [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"
Jason J. Herne070afca2015-09-08 13:12:35 -040032#include "qom/cpu.h"
aliguori065e2812008-11-11 16:46:33 +000033
Jason J. Hernedc325622015-09-08 13:12:37 -040034#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000035
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010036/* Amount of time to allocate to each "chunk" of bandwidth-throttled
37 * data. */
38#define BUFFER_DELAY 100
39#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
40
Liang Li8706d2d2015-03-23 16:32:17 +080041/* Default compression thread count */
42#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080043/* Default decompression thread count, usually decompression is at
44 * least 4 times as fast as compression.*/
45#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080046/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
47#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040048/* Define default autoconverge cpu throttle migration parameters */
49#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20
50#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080051
Orit Wasserman17ad9b32012-08-06 21:42:53 +030052/* Migration XBZRLE default cache size */
53#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
54
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010055static NotifierList migration_state_notifiers =
56 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
57
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000058static bool deferred_incoming;
59
Juan Quintela17549e82011-10-05 13:50:43 +020060/* When we add fault tolerance, we could have several
61 migrations at once. For now we don't need to add
62 dynamic creation of migration */
63
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010064/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020065MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020066{
67 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080068 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010069 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030070 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040071 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080072 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
73 DEFAULT_MIGRATE_COMPRESS_LEVEL,
74 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
75 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
76 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
77 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Jason J. Herne1626fee2015-09-08 13:12:34 -040078 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
79 DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL,
80 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
81 DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT,
Juan Quintela17549e82011-10-05 13:50:43 +020082 };
83
84 return &current_migration;
85}
86
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010087/* For incoming */
88static MigrationIncomingState *mis_current;
89
90MigrationIncomingState *migration_incoming_get_current(void)
91{
92 return mis_current;
93}
94
95MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
96{
Markus Armbruster97f3ad32015-09-14 13:51:31 +020097 mis_current = g_new0(MigrationIncomingState, 1);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010098 mis_current->file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010099 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100100
101 return mis_current;
102}
103
104void migration_incoming_state_destroy(void)
105{
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100106 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100107 g_free(mis_current);
108 mis_current = NULL;
109}
110
Juan Quinteladf4b1022014-10-08 10:58:10 +0200111
112typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200113 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200114 uint32_t size;
115 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200116 RunState state;
117 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200118} GlobalState;
119
120static GlobalState global_state;
121
Juan Quintela560d0272015-07-15 09:53:46 +0200122int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200123{
124 if (!runstate_store((char *)global_state.runstate,
125 sizeof(global_state.runstate))) {
126 error_report("runstate name too big: %s", global_state.runstate);
127 trace_migrate_state_too_big();
128 return -EINVAL;
129 }
130 return 0;
131}
132
Anthony PERARDc69adea2015-08-03 15:29:19 +0100133void global_state_store_running(void)
134{
135 const char *state = RunState_lookup[RUN_STATE_RUNNING];
136 strncpy((char *)global_state.runstate,
137 state, sizeof(global_state.runstate));
138}
139
Juan Quintela172c4352015-07-08 13:56:26 +0200140static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200141{
Juan Quintela172c4352015-07-08 13:56:26 +0200142 return global_state.received;
143}
144
145static RunState global_state_get_runstate(void)
146{
147 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200148}
149
Juan Quintela13d16812014-10-08 13:58:24 +0200150void global_state_set_optional(void)
151{
152 global_state.optional = true;
153}
154
155static bool global_state_needed(void *opaque)
156{
157 GlobalState *s = opaque;
158 char *runstate = (char *)s->runstate;
159
160 /* If it is not optional, it is mandatory */
161
162 if (s->optional == false) {
163 return true;
164 }
165
166 /* If state is running or paused, it is not needed */
167
168 if (strcmp(runstate, "running") == 0 ||
169 strcmp(runstate, "paused") == 0) {
170 return false;
171 }
172
173 /* for any other state it is needed */
174 return true;
175}
176
Juan Quinteladf4b1022014-10-08 10:58:10 +0200177static int global_state_post_load(void *opaque, int version_id)
178{
179 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200180 Error *local_err = NULL;
181 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200182 char *runstate = (char *)s->runstate;
183
Juan Quintela172c4352015-07-08 13:56:26 +0200184 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200185 trace_migrate_global_state_post_load(runstate);
186
Juan Quintela172c4352015-07-08 13:56:26 +0200187 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200188 -1, &local_err);
189
Juan Quintela172c4352015-07-08 13:56:26 +0200190 if (r == -1) {
191 if (local_err) {
192 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200193 }
Juan Quintela172c4352015-07-08 13:56:26 +0200194 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200195 }
Juan Quintela172c4352015-07-08 13:56:26 +0200196 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200197
Juan Quintela172c4352015-07-08 13:56:26 +0200198 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200199}
200
201static void global_state_pre_save(void *opaque)
202{
203 GlobalState *s = opaque;
204
205 trace_migrate_global_state_pre_save((char *)s->runstate);
206 s->size = strlen((char *)s->runstate) + 1;
207}
208
209static const VMStateDescription vmstate_globalstate = {
210 .name = "globalstate",
211 .version_id = 1,
212 .minimum_version_id = 1,
213 .post_load = global_state_post_load,
214 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200215 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200216 .fields = (VMStateField[]) {
217 VMSTATE_UINT32(size, GlobalState),
218 VMSTATE_BUFFER(runstate, GlobalState),
219 VMSTATE_END_OF_LIST()
220 },
221};
222
223void register_global_state(void)
224{
225 /* We would use it independently that we receive it */
226 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200227 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200228 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
229}
230
Juan Quintelab05dc722015-07-07 14:44:05 +0200231static void migrate_generate_event(int new_state)
232{
233 if (migrate_use_events()) {
234 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200235 }
236}
237
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000238/*
239 * Called on -incoming with a defer: uri.
240 * The migration can be started later after any parameters have been
241 * changed.
242 */
243static void deferred_incoming_migration(Error **errp)
244{
245 if (deferred_incoming) {
246 error_setg(errp, "Incoming migration already deferred");
247 }
248 deferred_incoming = true;
249}
250
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200251void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000252{
aliguori34c9dd82008-10-13 03:14:31 +0000253 const char *p;
254
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200255 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000256 if (!strcmp(uri, "defer")) {
257 deferred_incoming_migration(errp);
258 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200259 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400260#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000261 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400262 rdma_start_incoming_migration(p, errp);
263#endif
aliguori065e2812008-11-11 16:46:33 +0000264#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000265 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200266 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000267 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200268 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000269 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200270 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000271#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000272 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100273 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200274 }
aliguori5bb79102008-10-13 03:12:02 +0000275}
276
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200277static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200278{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200279 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100280 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200281 int ret;
282
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100283 migration_incoming_state_new(f);
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200284 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200285 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100286
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200287 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200288 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100289 migration_incoming_state_destroy();
290
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200291 if (ret < 0) {
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200292 migrate_generate_event(MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200293 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800294 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600295 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200296 }
297 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) {
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100302 migrate_generate_event(MIGRATION_STATUS_FAILED);
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100303 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800304 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100305 exit(EXIT_FAILURE);
306 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600307
Juan Quintela172c4352015-07-08 13:56:26 +0200308 /* If global state section was not received or we are in running
309 state, we need to obey autostart. Any other state is set with
310 runstate_set. */
Juan Quinteladf4b1022014-10-08 10:58:10 +0200311
Juan Quintela172c4352015-07-08 13:56:26 +0200312 if (!global_state_received() ||
313 global_state_get_runstate() == RUN_STATE_RUNNING) {
Juan Quinteladf4b1022014-10-08 10:58:10 +0200314 if (autostart) {
315 vm_start();
316 } else {
317 runstate_set(RUN_STATE_PAUSED);
318 }
Juan Quintela172c4352015-07-08 13:56:26 +0200319 } else {
320 runstate_set(global_state_get_runstate());
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300321 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800322 migrate_decompress_threads_join();
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100323 /*
324 * This must happen after any state changes since as soon as an external
325 * observer sees this event they might start to prod at the VM assuming
326 * it's ready to use.
327 */
328 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
Juan Quintela511c0232010-06-09 14:10:55 +0200329}
330
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200331void process_incoming_migration(QEMUFile *f)
332{
333 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
334 int fd = qemu_get_fd(f);
335
336 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800337 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100338 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200339 qemu_coroutine_enter(co, f);
340}
341
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400342/* amount of nanoseconds we are willing to wait for migration to be down.
343 * the choice of nanoseconds is because it is the maximum resolution that
344 * get_clock() can achieve. It is an internal measure. All user-visible
345 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100346static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400347
348uint64_t migrate_max_downtime(void)
349{
350 return max_downtime;
351}
352
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300353MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
354{
355 MigrationCapabilityStatusList *head = NULL;
356 MigrationCapabilityStatusList *caps;
357 MigrationState *s = migrate_get_current();
358 int i;
359
Michael Tokarev387eede2013-10-05 13:18:28 +0400360 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300361 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
362 if (head == NULL) {
363 head = g_malloc0(sizeof(*caps));
364 caps = head;
365 } else {
366 caps->next = g_malloc0(sizeof(*caps));
367 caps = caps->next;
368 }
369 caps->value =
370 g_malloc(sizeof(*caps->value));
371 caps->value->capability = i;
372 caps->value->state = s->enabled_capabilities[i];
373 }
374
375 return head;
376}
377
Liang Li85de8322015-03-23 16:32:28 +0800378MigrationParameters *qmp_query_migrate_parameters(Error **errp)
379{
380 MigrationParameters *params;
381 MigrationState *s = migrate_get_current();
382
383 params = g_malloc0(sizeof(*params));
384 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
385 params->compress_threads =
386 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
387 params->decompress_threads =
388 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400389 params->x_cpu_throttle_initial =
390 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
391 params->x_cpu_throttle_increment =
392 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Liang Li85de8322015-03-23 16:32:28 +0800393
394 return params;
395}
396
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300397static void get_xbzrle_cache_stats(MigrationInfo *info)
398{
399 if (migrate_use_xbzrle()) {
400 info->has_xbzrle_cache = true;
401 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
402 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
403 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
404 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
405 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800406 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300407 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
408 }
409}
410
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300411MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000412{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300413 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200414 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000415
Juan Quintela17549e82011-10-05 13:50:43 +0200416 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800417 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200418 /* no migration has happened ever */
419 break;
zhanghailiang31194732015-03-13 16:08:38 +0800420 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400421 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400422 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400423 break;
zhanghailiang31194732015-03-13 16:08:38 +0800424 case MIGRATION_STATUS_ACTIVE:
425 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300426 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200427 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100428 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200429 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200430 info->has_expected_downtime = true;
431 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400432 info->has_setup_time = true;
433 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200434
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300435 info->has_ram = true;
436 info->ram = g_malloc0(sizeof(*info->ram));
437 info->ram->transferred = ram_bytes_transferred();
438 info->ram->remaining = ram_bytes_remaining();
439 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300440 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100441 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300442 info->ram->normal = norm_mig_pages_transferred();
443 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200444 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400445 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800446 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200447
Juan Quintela17549e82011-10-05 13:50:43 +0200448 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300449 info->has_disk = true;
450 info->disk = g_malloc0(sizeof(*info->disk));
451 info->disk->transferred = blk_mig_bytes_transferred();
452 info->disk->remaining = blk_mig_bytes_remaining();
453 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000454 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300455
Jason J. Herne47828932015-09-08 13:12:36 -0400456 if (cpu_throttle_active()) {
457 info->has_x_cpu_throttle_percentage = true;
458 info->x_cpu_throttle_percentage = cpu_throttle_get_percentage();
459 }
460
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300461 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200462 break;
zhanghailiang31194732015-03-13 16:08:38 +0800463 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300464 get_xbzrle_cache_stats(info);
465
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300466 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900467 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200468 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200469 info->has_downtime = true;
470 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400471 info->has_setup_time = true;
472 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200473
474 info->has_ram = true;
475 info->ram = g_malloc0(sizeof(*info->ram));
476 info->ram->transferred = ram_bytes_transferred();
477 info->ram->remaining = 0;
478 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300479 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100480 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300481 info->ram->normal = norm_mig_pages_transferred();
482 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400483 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800484 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200485 break;
zhanghailiang31194732015-03-13 16:08:38 +0800486 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300487 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200488 break;
zhanghailiang31194732015-03-13 16:08:38 +0800489 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300490 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200491 break;
aliguori5bb79102008-10-13 03:12:02 +0000492 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800493 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300494
495 return info;
aliguori5bb79102008-10-13 03:12:02 +0000496}
497
Orit Wasserman00458432012-08-06 21:42:48 +0300498void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
499 Error **errp)
500{
501 MigrationState *s = migrate_get_current();
502 MigrationCapabilityStatusList *cap;
503
zhanghailiang31194732015-03-13 16:08:38 +0800504 if (s->state == MIGRATION_STATUS_ACTIVE ||
505 s->state == MIGRATION_STATUS_SETUP) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100506 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300507 return;
508 }
509
510 for (cap = params; cap; cap = cap->next) {
511 s->enabled_capabilities[cap->value->capability] = cap->value->state;
512 }
513}
514
Liang Li85de8322015-03-23 16:32:28 +0800515void qmp_migrate_set_parameters(bool has_compress_level,
516 int64_t compress_level,
517 bool has_compress_threads,
518 int64_t compress_threads,
519 bool has_decompress_threads,
Jason J. Herne1626fee2015-09-08 13:12:34 -0400520 int64_t decompress_threads,
521 bool has_x_cpu_throttle_initial,
522 int64_t x_cpu_throttle_initial,
523 bool has_x_cpu_throttle_increment,
524 int64_t x_cpu_throttle_increment, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800525{
526 MigrationState *s = migrate_get_current();
527
528 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100529 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
530 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800531 return;
532 }
533 if (has_compress_threads &&
534 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100535 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
536 "compress_threads",
537 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800538 return;
539 }
540 if (has_decompress_threads &&
541 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100542 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
543 "decompress_threads",
544 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800545 return;
546 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400547 if (has_x_cpu_throttle_initial &&
548 (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
549 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
550 "x_cpu_throttle_initial",
551 "an integer in the range of 1 to 99");
552 }
553 if (has_x_cpu_throttle_increment &&
554 (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
555 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
556 "x_cpu_throttle_increment",
557 "an integer in the range of 1 to 99");
558 }
Liang Li85de8322015-03-23 16:32:28 +0800559
560 if (has_compress_level) {
561 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
562 }
563 if (has_compress_threads) {
564 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
565 }
566 if (has_decompress_threads) {
567 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
568 decompress_threads;
569 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400570 if (has_x_cpu_throttle_initial) {
571 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
572 x_cpu_throttle_initial;
573 }
574
575 if (has_x_cpu_throttle_increment) {
576 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
577 x_cpu_throttle_increment;
578 }
Liang Li85de8322015-03-23 16:32:28 +0800579}
580
aliguori065e2812008-11-11 16:46:33 +0000581/* shared migration helpers */
582
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000583static void migrate_set_state(MigrationState *s, int old_state, int new_state)
584{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200585 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200586 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200587 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000588 }
589}
590
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100591static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000592{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100593 MigrationState *s = opaque;
594
595 qemu_bh_delete(s->cleanup_bh);
596 s->cleanup_bh = NULL;
597
aliguori065e2812008-11-11 16:46:33 +0000598 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100599 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100600 qemu_mutex_unlock_iothread();
601 qemu_thread_join(&s->thread);
602 qemu_mutex_lock_iothread();
603
Liang Li8706d2d2015-03-23 16:32:17 +0800604 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100605 qemu_fclose(s->file);
606 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000607 }
608
zhanghailiang31194732015-03-13 16:08:38 +0800609 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100610
zhanghailiang31194732015-03-13 16:08:38 +0800611 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100612 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800613 if (s->state == MIGRATION_STATUS_CANCELLING) {
614 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
615 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000616 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100617 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100618
619 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000620}
621
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200622void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000623{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100624 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100625 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200626 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100627 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100628}
629
Juan Quintela0edda1c2010-05-11 16:28:39 +0200630static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000631{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000632 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000633 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100634 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000635
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000636 do {
637 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800638 if (old_state != MIGRATION_STATUS_SETUP &&
639 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000640 break;
641 }
zhanghailiang31194732015-03-13 16:08:38 +0800642 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
643 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000644
645 /*
646 * If we're unlucky the migration code might be stuck somewhere in a
647 * send/write while the network has failed and is waiting to timeout;
648 * if we've got shutdown(2) available then we can force it to quit.
649 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
650 * called in a bh, so there is no race against this cancel.
651 */
zhanghailiang31194732015-03-13 16:08:38 +0800652 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000653 qemu_file_shutdown(f);
654 }
aliguori065e2812008-11-11 16:46:33 +0000655}
656
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100657void add_migration_state_change_notifier(Notifier *notify)
658{
659 notifier_list_add(&migration_state_notifiers, notify);
660}
661
662void remove_migration_state_change_notifier(Notifier *notify)
663{
Paolo Bonzini31552522012-01-13 17:34:01 +0100664 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100665}
666
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200667bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200668{
zhanghailiang31194732015-03-13 16:08:38 +0800669 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200670}
671
Juan Quintela70736932011-02-23 00:43:59 +0100672bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100673{
zhanghailiang31194732015-03-13 16:08:38 +0800674 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100675}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200676
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200677bool migration_has_failed(MigrationState *s)
678{
zhanghailiang31194732015-03-13 16:08:38 +0800679 return (s->state == MIGRATION_STATUS_CANCELLED ||
680 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200681}
682
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300683static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200684{
Juan Quintela17549e82011-10-05 13:50:43 +0200685 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100686 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300687 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300688 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800689 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
690 int compress_thread_count =
691 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
692 int decompress_thread_count =
693 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400694 int x_cpu_throttle_initial =
695 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
696 int x_cpu_throttle_increment =
697 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300698
699 memcpy(enabled_capabilities, s->enabled_capabilities,
700 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200701
Juan Quintela17549e82011-10-05 13:50:43 +0200702 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300703 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300704 memcpy(s->enabled_capabilities, enabled_capabilities,
705 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300706 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100707
Liang Li43c60a82015-03-23 16:32:27 +0800708 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
709 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
710 compress_thread_count;
711 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
712 decompress_thread_count;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400713 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
714 x_cpu_throttle_initial;
715 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
716 x_cpu_throttle_increment;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200717 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200718 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200719
Alex Blighbc72ad62013-08-21 16:03:08 +0100720 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200721 return s;
722}
Juan Quintelacab30142011-02-22 23:54:21 +0100723
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600724static GSList *migration_blockers;
725
726void migrate_add_blocker(Error *reason)
727{
728 migration_blockers = g_slist_prepend(migration_blockers, reason);
729}
730
731void migrate_del_blocker(Error *reason)
732{
733 migration_blockers = g_slist_remove(migration_blockers, reason);
734}
735
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000736void qmp_migrate_incoming(const char *uri, Error **errp)
737{
738 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000739 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000740
741 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000742 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000743 return;
744 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000745 if (!once) {
746 error_setg(errp, "The incoming migration has already been started");
747 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000748
749 qemu_start_incoming_migration(uri, &local_err);
750
751 if (local_err) {
752 error_propagate(errp, local_err);
753 return;
754 }
755
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000756 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000757}
758
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200759void qmp_migrate(const char *uri, bool has_blk, bool blk,
760 bool has_inc, bool inc, bool has_detach, bool detach,
761 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100762{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200763 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200764 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300765 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100766 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100767
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900768 params.blk = has_blk && blk;
769 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300770
zhanghailiang31194732015-03-13 16:08:38 +0800771 if (s->state == MIGRATION_STATUS_ACTIVE ||
772 s->state == MIGRATION_STATUS_SETUP ||
773 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100774 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200775 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100776 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100777 if (runstate_check(RUN_STATE_INMIGRATE)) {
778 error_setg(errp, "Guest is waiting for an incoming migration");
779 return;
780 }
781
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200782 if (qemu_savevm_state_blocked(errp)) {
783 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100784 }
785
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600786 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200787 *errp = error_copy(migration_blockers->data);
788 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600789 }
790
Juan Quintela656a2332015-07-01 09:32:29 +0200791 /* We are starting a new migration, so we want to start in a clean
792 state. This change is only needed if previous migration
793 failed/was cancelled. We don't use migrate_set_state() because
794 we are setting the initial state, not changing it. */
795 s->state = MIGRATION_STATUS_NONE;
796
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300797 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100798
799 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200800 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400801#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800802 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400803 rdma_start_outgoing_migration(s, p, &local_err);
804#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100805#if !defined(WIN32)
806 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200807 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100808 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200809 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100810 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200811 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100812#endif
813 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100814 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
815 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +0200816 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200817 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100818 }
819
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200820 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200821 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200822 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200823 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100824 }
Juan Quintelacab30142011-02-22 23:54:21 +0100825}
826
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200827void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100828{
Juan Quintela17549e82011-10-05 13:50:43 +0200829 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100830}
831
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300832void qmp_migrate_set_cache_size(int64_t value, Error **errp)
833{
834 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200835 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300836
837 /* Check for truncation */
838 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100839 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
840 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300841 return;
842 }
843
Orit Wassermana5615b12014-01-30 20:08:36 +0200844 /* Cache should not be larger than guest ram size */
845 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100846 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
847 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +0200848 return;
849 }
850
Orit Wassermanc91e6812014-01-30 20:08:34 +0200851 new_size = xbzrle_cache_resize(value);
852 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100853 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
854 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +0200855 return;
856 }
857
858 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300859}
860
861int64_t qmp_query_migrate_cache_size(Error **errp)
862{
863 return migrate_xbzrle_cache_size();
864}
865
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200866void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100867{
Juan Quintelacab30142011-02-22 23:54:21 +0100868 MigrationState *s;
869
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200870 if (value < 0) {
871 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100872 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100873 if (value > SIZE_MAX) {
874 value = SIZE_MAX;
875 }
Juan Quintelacab30142011-02-22 23:54:21 +0100876
Juan Quintela17549e82011-10-05 13:50:43 +0200877 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200878 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100879 if (s->file) {
880 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
881 }
Juan Quintelacab30142011-02-22 23:54:21 +0100882}
883
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200884void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100885{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200886 value *= 1e9;
887 value = MAX(0, MIN(UINT64_MAX, value));
888 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000889}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300890
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600891bool migrate_auto_converge(void)
892{
893 MigrationState *s;
894
895 s = migrate_get_current();
896
897 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
898}
899
Peter Lieven323004a2013-07-18 09:48:50 +0200900bool migrate_zero_blocks(void)
901{
902 MigrationState *s;
903
904 s = migrate_get_current();
905
906 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
907}
908
Liang Li8706d2d2015-03-23 16:32:17 +0800909bool migrate_use_compression(void)
910{
Liang Lidde4e692015-03-23 16:32:26 +0800911 MigrationState *s;
912
913 s = migrate_get_current();
914
915 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800916}
917
918int migrate_compress_level(void)
919{
920 MigrationState *s;
921
922 s = migrate_get_current();
923
Liang Li43c60a82015-03-23 16:32:27 +0800924 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800925}
926
927int migrate_compress_threads(void)
928{
929 MigrationState *s;
930
931 s = migrate_get_current();
932
Liang Li43c60a82015-03-23 16:32:27 +0800933 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800934}
935
Liang Li3fcb38c2015-03-23 16:32:18 +0800936int migrate_decompress_threads(void)
937{
938 MigrationState *s;
939
940 s = migrate_get_current();
941
Liang Li43c60a82015-03-23 16:32:27 +0800942 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800943}
944
Juan Quintelab05dc722015-07-07 14:44:05 +0200945bool migrate_use_events(void)
946{
947 MigrationState *s;
948
949 s = migrate_get_current();
950
951 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
952}
953
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300954int migrate_use_xbzrle(void)
955{
956 MigrationState *s;
957
958 s = migrate_get_current();
959
960 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
961}
962
963int64_t migrate_xbzrle_cache_size(void)
964{
965 MigrationState *s;
966
967 s = migrate_get_current();
968
969 return s->xbzrle_cache_size;
970}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200971
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +0100972/**
973 * migration_completion: Used by migration_thread when there's not much left.
974 * The caller 'breaks' the loop when this returns.
975 *
976 * @s: Current migration state
977 * @*old_vm_running: Pointer to old_vm_running flag
978 * @*start_time: Pointer to time to update
979 */
980static void migration_completion(MigrationState *s, bool *old_vm_running,
981 int64_t *start_time)
982{
983 int ret;
984
985 qemu_mutex_lock_iothread();
986 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
987 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
988 *old_vm_running = runstate_is_running();
989
990 ret = global_state_store();
991 if (!ret) {
992 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
993 if (ret >= 0) {
994 qemu_file_set_rate_limit(s->file, INT64_MAX);
995 qemu_savevm_state_complete(s->file);
996 }
997 }
998 qemu_mutex_unlock_iothread();
999
1000 if (ret < 0) {
1001 goto fail;
1002 }
1003
1004 if (qemu_file_get_error(s->file)) {
1005 trace_migration_completion_file_err();
1006 goto fail;
1007 }
1008
1009 migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_COMPLETED);
1010 return;
1011
1012fail:
1013 migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_FAILED);
1014}
1015
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001016/* migration thread support */
1017
Juan Quintela5f496a12013-02-22 17:36:30 +01001018static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001019{
Juan Quintela9848a402012-12-19 09:55:50 +01001020 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01001021 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1022 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001023 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001024 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001025 int64_t start_time = initial_time;
1026 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +02001027
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001028 rcu_register_thread();
1029
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001030 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001031 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001032
Alex Blighbc72ad62013-08-21 16:03:08 +01001033 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +08001034 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001035
zhanghailiang31194732015-03-13 16:08:38 +08001036 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001037 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001038 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001039
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001040 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +02001041 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001042 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001043 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001044 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +02001045 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001046 trace_migration_thread_low_pending(pending_size);
1047 migration_completion(s, &old_vm_running, &start_time);
1048 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001049 }
1050 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001051
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001052 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +08001053 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
1054 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001055 break;
1056 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001057 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001058 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001059 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001060 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001061 double bandwidth = transferred_bytes / time_spent;
1062 max_size = bandwidth * migrate_max_downtime() / 1000000;
1063
Michael R. Hines7e114f82013-06-25 21:35:30 -04001064 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1065 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1066
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001067 trace_migrate_transferred(transferred_bytes, time_spent,
1068 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001069 /* if we haven't sent anything, we don't want to recalculate
1070 10000 is a small enough number for our purposes */
1071 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1072 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1073 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001074
Paolo Bonzini1964a392013-02-22 17:36:45 +01001075 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001076 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001077 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001078 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001079 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001080 /* usleep expects microseconds */
1081 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1082 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001083 }
1084
Jason J. Herne070afca2015-09-08 13:12:35 -04001085 /* If we enabled cpu throttling for auto-converge, turn it off. */
1086 cpu_throttle_stop();
1087
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001088 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +08001089 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001090 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +02001091 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001092 s->total_time = end_time - s->total_time;
1093 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +02001094 if (s->total_time) {
1095 s->mbps = (((double) transferred_bytes * 8.0) /
1096 ((double) s->total_time)) / 1000;
1097 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001098 runstate_set(RUN_STATE_POSTMIGRATE);
1099 } else {
1100 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001101 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001102 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001103 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001104 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001105 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001106
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001107 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001108 return NULL;
1109}
1110
Juan Quintela9848a402012-12-19 09:55:50 +01001111void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001112{
Juan Quintelacc283e32013-02-01 11:12:26 +01001113 /* This is a best 1st approximation. ns to ms */
1114 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001115 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001116
Paolo Bonzini442773c2013-02-22 17:36:44 +01001117 qemu_file_set_rate_limit(s->file,
1118 s->bandwidth_limit / XFER_LIMIT_RATIO);
1119
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001120 /* Notify before starting migration thread */
1121 notifier_list_notify(&migration_state_notifiers, s);
1122
Liang Li8706d2d2015-03-23 16:32:17 +08001123 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001124 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001125 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001126}