blob: 85e68bcfe1980f7eb72d71097a84783093a0452c [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
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000060/*
61 * Current state of incoming postcopy; note this is not part of
62 * MigrationIncomingState since it's state is used during cleanup
63 * at the end as MIS is being freed.
64 */
65static PostcopyState incoming_postcopy_state;
66
Juan Quintela17549e82011-10-05 13:50:43 +020067/* When we add fault tolerance, we could have several
68 migrations at once. For now we don't need to add
69 dynamic creation of migration */
70
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010071/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020072MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020073{
74 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080075 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010076 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030077 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040078 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080079 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
80 DEFAULT_MIGRATE_COMPRESS_LEVEL,
81 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
82 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
83 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
84 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Jason J. Herne1626fee2015-09-08 13:12:34 -040085 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
86 DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL,
87 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
88 DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT,
Juan Quintela17549e82011-10-05 13:50:43 +020089 };
90
91 return &current_migration;
92}
93
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010094/* For incoming */
95static MigrationIncomingState *mis_current;
96
97MigrationIncomingState *migration_incoming_get_current(void)
98{
99 return mis_current;
100}
101
102MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
103{
Markus Armbruster97f3ad32015-09-14 13:51:31 +0200104 mis_current = g_new0(MigrationIncomingState, 1);
Dr. David Alan Gilbert42e2aa52015-11-05 18:10:34 +0000105 mis_current->from_src_file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100106 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000107 qemu_mutex_init(&mis_current->rp_mutex);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000108 qemu_event_init(&mis_current->main_thread_load_event, false);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100109
110 return mis_current;
111}
112
113void migration_incoming_state_destroy(void)
114{
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000115 qemu_event_destroy(&mis_current->main_thread_load_event);
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100116 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100117 g_free(mis_current);
118 mis_current = NULL;
119}
120
Juan Quinteladf4b1022014-10-08 10:58:10 +0200121
122typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200123 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200124 uint32_t size;
125 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200126 RunState state;
127 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200128} GlobalState;
129
130static GlobalState global_state;
131
Juan Quintela560d0272015-07-15 09:53:46 +0200132int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200133{
134 if (!runstate_store((char *)global_state.runstate,
135 sizeof(global_state.runstate))) {
136 error_report("runstate name too big: %s", global_state.runstate);
137 trace_migrate_state_too_big();
138 return -EINVAL;
139 }
140 return 0;
141}
142
Anthony PERARDc69adea2015-08-03 15:29:19 +0100143void global_state_store_running(void)
144{
145 const char *state = RunState_lookup[RUN_STATE_RUNNING];
146 strncpy((char *)global_state.runstate,
147 state, sizeof(global_state.runstate));
148}
149
Juan Quintela172c4352015-07-08 13:56:26 +0200150static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200151{
Juan Quintela172c4352015-07-08 13:56:26 +0200152 return global_state.received;
153}
154
155static RunState global_state_get_runstate(void)
156{
157 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200158}
159
Juan Quintela13d16812014-10-08 13:58:24 +0200160void global_state_set_optional(void)
161{
162 global_state.optional = true;
163}
164
165static bool global_state_needed(void *opaque)
166{
167 GlobalState *s = opaque;
168 char *runstate = (char *)s->runstate;
169
170 /* If it is not optional, it is mandatory */
171
172 if (s->optional == false) {
173 return true;
174 }
175
176 /* If state is running or paused, it is not needed */
177
178 if (strcmp(runstate, "running") == 0 ||
179 strcmp(runstate, "paused") == 0) {
180 return false;
181 }
182
183 /* for any other state it is needed */
184 return true;
185}
186
Juan Quinteladf4b1022014-10-08 10:58:10 +0200187static int global_state_post_load(void *opaque, int version_id)
188{
189 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200190 Error *local_err = NULL;
191 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200192 char *runstate = (char *)s->runstate;
193
Juan Quintela172c4352015-07-08 13:56:26 +0200194 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200195 trace_migrate_global_state_post_load(runstate);
196
Juan Quintela172c4352015-07-08 13:56:26 +0200197 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200198 -1, &local_err);
199
Juan Quintela172c4352015-07-08 13:56:26 +0200200 if (r == -1) {
201 if (local_err) {
202 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200203 }
Juan Quintela172c4352015-07-08 13:56:26 +0200204 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200205 }
Juan Quintela172c4352015-07-08 13:56:26 +0200206 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200207
Juan Quintela172c4352015-07-08 13:56:26 +0200208 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200209}
210
211static void global_state_pre_save(void *opaque)
212{
213 GlobalState *s = opaque;
214
215 trace_migrate_global_state_pre_save((char *)s->runstate);
216 s->size = strlen((char *)s->runstate) + 1;
217}
218
219static const VMStateDescription vmstate_globalstate = {
220 .name = "globalstate",
221 .version_id = 1,
222 .minimum_version_id = 1,
223 .post_load = global_state_post_load,
224 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200225 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200226 .fields = (VMStateField[]) {
227 VMSTATE_UINT32(size, GlobalState),
228 VMSTATE_BUFFER(runstate, GlobalState),
229 VMSTATE_END_OF_LIST()
230 },
231};
232
233void register_global_state(void)
234{
235 /* We would use it independently that we receive it */
236 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200237 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200238 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
239}
240
Juan Quintelab05dc722015-07-07 14:44:05 +0200241static void migrate_generate_event(int new_state)
242{
243 if (migrate_use_events()) {
244 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200245 }
246}
247
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000248/*
249 * Called on -incoming with a defer: uri.
250 * The migration can be started later after any parameters have been
251 * changed.
252 */
253static void deferred_incoming_migration(Error **errp)
254{
255 if (deferred_incoming) {
256 error_setg(errp, "Incoming migration already deferred");
257 }
258 deferred_incoming = true;
259}
260
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200261void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000262{
aliguori34c9dd82008-10-13 03:14:31 +0000263 const char *p;
264
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200265 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000266 if (!strcmp(uri, "defer")) {
267 deferred_incoming_migration(errp);
268 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200269 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400270#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000271 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400272 rdma_start_incoming_migration(p, errp);
273#endif
aliguori065e2812008-11-11 16:46:33 +0000274#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000275 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200276 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000277 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200278 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000279 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200280 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000281#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000282 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100283 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200284 }
aliguori5bb79102008-10-13 03:12:02 +0000285}
286
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200287static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200288{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200289 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100290 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200291 int ret;
292
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100293 migration_incoming_state_new(f);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000294 postcopy_state_set(POSTCOPY_INCOMING_NONE);
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200295 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200296 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100297
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200298 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200299 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100300 migration_incoming_state_destroy();
301
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200302 if (ret < 0) {
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200303 migrate_generate_event(MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200304 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800305 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600306 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200307 }
Juan Quintela511c0232010-06-09 14:10:55 +0200308
Anthony Liguori0f154232011-11-14 15:09:45 -0600309 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100310 bdrv_invalidate_cache_all(&local_err);
311 if (local_err) {
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100312 migrate_generate_event(MIGRATION_STATUS_FAILED);
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100313 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800314 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100315 exit(EXIT_FAILURE);
316 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600317
Amit Shah92e37622015-10-14 17:37:19 +0530318 /*
319 * This must happen after all error conditions are dealt with and
320 * we're sure the VM is going to be running on this host.
321 */
322 qemu_announce_self();
323
Juan Quintela172c4352015-07-08 13:56:26 +0200324 /* If global state section was not received or we are in running
325 state, we need to obey autostart. Any other state is set with
326 runstate_set. */
Juan Quinteladf4b1022014-10-08 10:58:10 +0200327
Juan Quintela172c4352015-07-08 13:56:26 +0200328 if (!global_state_received() ||
329 global_state_get_runstate() == RUN_STATE_RUNNING) {
Juan Quinteladf4b1022014-10-08 10:58:10 +0200330 if (autostart) {
331 vm_start();
332 } else {
333 runstate_set(RUN_STATE_PAUSED);
334 }
Juan Quintela172c4352015-07-08 13:56:26 +0200335 } else {
336 runstate_set(global_state_get_runstate());
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300337 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800338 migrate_decompress_threads_join();
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100339 /*
340 * This must happen after any state changes since as soon as an external
341 * observer sees this event they might start to prod at the VM assuming
342 * it's ready to use.
343 */
344 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
Juan Quintela511c0232010-06-09 14:10:55 +0200345}
346
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200347void process_incoming_migration(QEMUFile *f)
348{
349 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
350 int fd = qemu_get_fd(f);
351
352 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800353 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100354 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200355 qemu_coroutine_enter(co, f);
356}
357
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000358/*
359 * Send a message on the return channel back to the source
360 * of the migration.
361 */
362void migrate_send_rp_message(MigrationIncomingState *mis,
363 enum mig_rp_message_type message_type,
364 uint16_t len, void *data)
365{
366 trace_migrate_send_rp_message((int)message_type, len);
367 qemu_mutex_lock(&mis->rp_mutex);
368 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
369 qemu_put_be16(mis->to_src_file, len);
370 qemu_put_buffer(mis->to_src_file, data, len);
371 qemu_fflush(mis->to_src_file);
372 qemu_mutex_unlock(&mis->rp_mutex);
373}
374
375/*
376 * Send a 'SHUT' message on the return channel with the given value
377 * to indicate that we've finished with the RP. Non-0 value indicates
378 * error.
379 */
380void migrate_send_rp_shut(MigrationIncomingState *mis,
381 uint32_t value)
382{
383 uint32_t buf;
384
385 buf = cpu_to_be32(value);
386 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
387}
388
389/*
390 * Send a 'PONG' message on the return channel with the given value
391 * (normally in response to a 'PING')
392 */
393void migrate_send_rp_pong(MigrationIncomingState *mis,
394 uint32_t value)
395{
396 uint32_t buf;
397
398 buf = cpu_to_be32(value);
399 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
400}
401
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400402/* amount of nanoseconds we are willing to wait for migration to be down.
403 * the choice of nanoseconds is because it is the maximum resolution that
404 * get_clock() can achieve. It is an internal measure. All user-visible
405 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100406static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400407
408uint64_t migrate_max_downtime(void)
409{
410 return max_downtime;
411}
412
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300413MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
414{
415 MigrationCapabilityStatusList *head = NULL;
416 MigrationCapabilityStatusList *caps;
417 MigrationState *s = migrate_get_current();
418 int i;
419
Michael Tokarev387eede2013-10-05 13:18:28 +0400420 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300421 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
422 if (head == NULL) {
423 head = g_malloc0(sizeof(*caps));
424 caps = head;
425 } else {
426 caps->next = g_malloc0(sizeof(*caps));
427 caps = caps->next;
428 }
429 caps->value =
430 g_malloc(sizeof(*caps->value));
431 caps->value->capability = i;
432 caps->value->state = s->enabled_capabilities[i];
433 }
434
435 return head;
436}
437
Liang Li85de8322015-03-23 16:32:28 +0800438MigrationParameters *qmp_query_migrate_parameters(Error **errp)
439{
440 MigrationParameters *params;
441 MigrationState *s = migrate_get_current();
442
443 params = g_malloc0(sizeof(*params));
444 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
445 params->compress_threads =
446 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
447 params->decompress_threads =
448 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400449 params->x_cpu_throttle_initial =
450 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
451 params->x_cpu_throttle_increment =
452 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Liang Li85de8322015-03-23 16:32:28 +0800453
454 return params;
455}
456
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000457/*
458 * Return true if we're already in the middle of a migration
459 * (i.e. any of the active or setup states)
460 */
461static bool migration_is_setup_or_active(int state)
462{
463 switch (state) {
464 case MIGRATION_STATUS_ACTIVE:
465 case MIGRATION_STATUS_SETUP:
466 return true;
467
468 default:
469 return false;
470
471 }
472}
473
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300474static void get_xbzrle_cache_stats(MigrationInfo *info)
475{
476 if (migrate_use_xbzrle()) {
477 info->has_xbzrle_cache = true;
478 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
479 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
480 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
481 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
482 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800483 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300484 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
485 }
486}
487
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300488MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000489{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300490 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200491 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000492
Juan Quintela17549e82011-10-05 13:50:43 +0200493 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800494 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200495 /* no migration has happened ever */
496 break;
zhanghailiang31194732015-03-13 16:08:38 +0800497 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400498 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400499 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400500 break;
zhanghailiang31194732015-03-13 16:08:38 +0800501 case MIGRATION_STATUS_ACTIVE:
502 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300503 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200504 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100505 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200506 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200507 info->has_expected_downtime = true;
508 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400509 info->has_setup_time = true;
510 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200511
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300512 info->has_ram = true;
513 info->ram = g_malloc0(sizeof(*info->ram));
514 info->ram->transferred = ram_bytes_transferred();
515 info->ram->remaining = ram_bytes_remaining();
516 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300517 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100518 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300519 info->ram->normal = norm_mig_pages_transferred();
520 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200521 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400522 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800523 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200524
Juan Quintela17549e82011-10-05 13:50:43 +0200525 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300526 info->has_disk = true;
527 info->disk = g_malloc0(sizeof(*info->disk));
528 info->disk->transferred = blk_mig_bytes_transferred();
529 info->disk->remaining = blk_mig_bytes_remaining();
530 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000531 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300532
Jason J. Herne47828932015-09-08 13:12:36 -0400533 if (cpu_throttle_active()) {
534 info->has_x_cpu_throttle_percentage = true;
535 info->x_cpu_throttle_percentage = cpu_throttle_get_percentage();
536 }
537
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300538 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200539 break;
zhanghailiang31194732015-03-13 16:08:38 +0800540 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300541 get_xbzrle_cache_stats(info);
542
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300543 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900544 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200545 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200546 info->has_downtime = true;
547 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400548 info->has_setup_time = true;
549 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200550
551 info->has_ram = true;
552 info->ram = g_malloc0(sizeof(*info->ram));
553 info->ram->transferred = ram_bytes_transferred();
554 info->ram->remaining = 0;
555 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300556 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100557 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300558 info->ram->normal = norm_mig_pages_transferred();
559 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400560 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800561 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200562 break;
zhanghailiang31194732015-03-13 16:08:38 +0800563 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300564 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200565 break;
zhanghailiang31194732015-03-13 16:08:38 +0800566 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300567 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200568 break;
aliguori5bb79102008-10-13 03:12:02 +0000569 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800570 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300571
572 return info;
aliguori5bb79102008-10-13 03:12:02 +0000573}
574
Orit Wasserman00458432012-08-06 21:42:48 +0300575void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
576 Error **errp)
577{
578 MigrationState *s = migrate_get_current();
579 MigrationCapabilityStatusList *cap;
580
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000581 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100582 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300583 return;
584 }
585
586 for (cap = params; cap; cap = cap->next) {
587 s->enabled_capabilities[cap->value->capability] = cap->value->state;
588 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000589
590 if (migrate_postcopy_ram()) {
591 if (migrate_use_compression()) {
592 /* The decompression threads asynchronously write into RAM
593 * rather than use the atomic copies needed to avoid
594 * userfaulting. It should be possible to fix the decompression
595 * threads for compatibility in future.
596 */
597 error_report("Postcopy is not currently compatible with "
598 "compression");
599 s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM] =
600 false;
601 }
602 }
Orit Wasserman00458432012-08-06 21:42:48 +0300603}
604
Liang Li85de8322015-03-23 16:32:28 +0800605void qmp_migrate_set_parameters(bool has_compress_level,
606 int64_t compress_level,
607 bool has_compress_threads,
608 int64_t compress_threads,
609 bool has_decompress_threads,
Jason J. Herne1626fee2015-09-08 13:12:34 -0400610 int64_t decompress_threads,
611 bool has_x_cpu_throttle_initial,
612 int64_t x_cpu_throttle_initial,
613 bool has_x_cpu_throttle_increment,
614 int64_t x_cpu_throttle_increment, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800615{
616 MigrationState *s = migrate_get_current();
617
618 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100619 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
620 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800621 return;
622 }
623 if (has_compress_threads &&
624 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100625 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
626 "compress_threads",
627 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800628 return;
629 }
630 if (has_decompress_threads &&
631 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100632 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
633 "decompress_threads",
634 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800635 return;
636 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400637 if (has_x_cpu_throttle_initial &&
638 (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
639 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
640 "x_cpu_throttle_initial",
641 "an integer in the range of 1 to 99");
642 }
643 if (has_x_cpu_throttle_increment &&
644 (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
645 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
646 "x_cpu_throttle_increment",
647 "an integer in the range of 1 to 99");
648 }
Liang Li85de8322015-03-23 16:32:28 +0800649
650 if (has_compress_level) {
651 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
652 }
653 if (has_compress_threads) {
654 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
655 }
656 if (has_decompress_threads) {
657 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
658 decompress_threads;
659 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400660 if (has_x_cpu_throttle_initial) {
661 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
662 x_cpu_throttle_initial;
663 }
664
665 if (has_x_cpu_throttle_increment) {
666 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
667 x_cpu_throttle_increment;
668 }
Liang Li85de8322015-03-23 16:32:28 +0800669}
670
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000671void qmp_migrate_start_postcopy(Error **errp)
672{
673 MigrationState *s = migrate_get_current();
674
675 if (!migrate_postcopy_ram()) {
676 error_setg(errp, "Enable postcopy with migration_set_capability before"
677 " the start of migration");
678 return;
679 }
680
681 if (s->state == MIGRATION_STATUS_NONE) {
682 error_setg(errp, "Postcopy must be started after migration has been"
683 " started");
684 return;
685 }
686 /*
687 * we don't error if migration has finished since that would be racy
688 * with issuing this command.
689 */
690 atomic_set(&s->start_postcopy, true);
691}
692
aliguori065e2812008-11-11 16:46:33 +0000693/* shared migration helpers */
694
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000695static void migrate_set_state(MigrationState *s, int old_state, int new_state)
696{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200697 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200698 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200699 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000700 }
701}
702
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100703static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000704{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100705 MigrationState *s = opaque;
706
707 qemu_bh_delete(s->cleanup_bh);
708 s->cleanup_bh = NULL;
709
aliguori065e2812008-11-11 16:46:33 +0000710 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100711 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100712 qemu_mutex_unlock_iothread();
713 qemu_thread_join(&s->thread);
714 qemu_mutex_lock_iothread();
715
Liang Li8706d2d2015-03-23 16:32:17 +0800716 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100717 qemu_fclose(s->file);
718 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000719 }
720
zhanghailiang31194732015-03-13 16:08:38 +0800721 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100722
Liang Li94f5a432015-11-02 15:37:00 +0800723 if (s->state == MIGRATION_STATUS_CANCELLING) {
724 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
725 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100726 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100727
728 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000729}
730
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200731void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000732{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100733 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100734 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200735 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100736 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100737}
738
Juan Quintela0edda1c2010-05-11 16:28:39 +0200739static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000740{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000741 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000742 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100743 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000744
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000745 if (s->rp_state.from_dst_file) {
746 /* shutdown the rp socket, so causing the rp thread to shutdown */
747 qemu_file_shutdown(s->rp_state.from_dst_file);
748 }
749
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000750 do {
751 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000752 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000753 break;
754 }
zhanghailiang31194732015-03-13 16:08:38 +0800755 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
756 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000757
758 /*
759 * If we're unlucky the migration code might be stuck somewhere in a
760 * send/write while the network has failed and is waiting to timeout;
761 * if we've got shutdown(2) available then we can force it to quit.
762 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
763 * called in a bh, so there is no race against this cancel.
764 */
zhanghailiang31194732015-03-13 16:08:38 +0800765 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000766 qemu_file_shutdown(f);
767 }
aliguori065e2812008-11-11 16:46:33 +0000768}
769
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100770void add_migration_state_change_notifier(Notifier *notify)
771{
772 notifier_list_add(&migration_state_notifiers, notify);
773}
774
775void remove_migration_state_change_notifier(Notifier *notify)
776{
Paolo Bonzini31552522012-01-13 17:34:01 +0100777 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100778}
779
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200780bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200781{
zhanghailiang31194732015-03-13 16:08:38 +0800782 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200783}
784
Juan Quintela70736932011-02-23 00:43:59 +0100785bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100786{
zhanghailiang31194732015-03-13 16:08:38 +0800787 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100788}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200789
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200790bool migration_has_failed(MigrationState *s)
791{
zhanghailiang31194732015-03-13 16:08:38 +0800792 return (s->state == MIGRATION_STATUS_CANCELLED ||
793 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200794}
795
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +0000796MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200797{
Juan Quintela17549e82011-10-05 13:50:43 +0200798 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100799 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300800 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300801 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800802 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
803 int compress_thread_count =
804 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
805 int decompress_thread_count =
806 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400807 int x_cpu_throttle_initial =
808 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
809 int x_cpu_throttle_increment =
810 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300811
812 memcpy(enabled_capabilities, s->enabled_capabilities,
813 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200814
Juan Quintela17549e82011-10-05 13:50:43 +0200815 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300816 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300817 memcpy(s->enabled_capabilities, enabled_capabilities,
818 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300819 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100820
Liang Li43c60a82015-03-23 16:32:27 +0800821 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
822 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
823 compress_thread_count;
824 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
825 decompress_thread_count;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400826 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
827 x_cpu_throttle_initial;
828 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
829 x_cpu_throttle_increment;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200830 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200831 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200832
Alex Blighbc72ad62013-08-21 16:03:08 +0100833 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200834 return s;
835}
Juan Quintelacab30142011-02-22 23:54:21 +0100836
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600837static GSList *migration_blockers;
838
839void migrate_add_blocker(Error *reason)
840{
841 migration_blockers = g_slist_prepend(migration_blockers, reason);
842}
843
844void migrate_del_blocker(Error *reason)
845{
846 migration_blockers = g_slist_remove(migration_blockers, reason);
847}
848
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000849void qmp_migrate_incoming(const char *uri, Error **errp)
850{
851 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000852 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000853
854 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000855 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000856 return;
857 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000858 if (!once) {
859 error_setg(errp, "The incoming migration has already been started");
860 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000861
862 qemu_start_incoming_migration(uri, &local_err);
863
864 if (local_err) {
865 error_propagate(errp, local_err);
866 return;
867 }
868
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000869 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000870}
871
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200872void qmp_migrate(const char *uri, bool has_blk, bool blk,
873 bool has_inc, bool inc, bool has_detach, bool detach,
874 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100875{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200876 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200877 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300878 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100879 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100880
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900881 params.blk = has_blk && blk;
882 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300883
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000884 if (migration_is_setup_or_active(s->state) ||
zhanghailiang31194732015-03-13 16:08:38 +0800885 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100886 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200887 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100888 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100889 if (runstate_check(RUN_STATE_INMIGRATE)) {
890 error_setg(errp, "Guest is waiting for an incoming migration");
891 return;
892 }
893
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200894 if (qemu_savevm_state_blocked(errp)) {
895 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100896 }
897
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600898 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200899 *errp = error_copy(migration_blockers->data);
900 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600901 }
902
Juan Quintela656a2332015-07-01 09:32:29 +0200903 /* We are starting a new migration, so we want to start in a clean
904 state. This change is only needed if previous migration
905 failed/was cancelled. We don't use migrate_set_state() because
906 we are setting the initial state, not changing it. */
907 s->state = MIGRATION_STATUS_NONE;
908
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300909 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100910
911 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200912 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400913#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800914 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400915 rdma_start_outgoing_migration(s, p, &local_err);
916#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100917#if !defined(WIN32)
918 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200919 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100920 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200921 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100922 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200923 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100924#endif
925 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100926 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
927 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +0200928 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200929 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100930 }
931
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200932 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200933 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200934 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200935 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100936 }
Juan Quintelacab30142011-02-22 23:54:21 +0100937}
938
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200939void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100940{
Juan Quintela17549e82011-10-05 13:50:43 +0200941 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100942}
943
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300944void qmp_migrate_set_cache_size(int64_t value, Error **errp)
945{
946 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200947 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300948
949 /* Check for truncation */
950 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100951 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
952 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300953 return;
954 }
955
Orit Wassermana5615b12014-01-30 20:08:36 +0200956 /* Cache should not be larger than guest ram size */
957 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100958 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
959 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +0200960 return;
961 }
962
Orit Wassermanc91e6812014-01-30 20:08:34 +0200963 new_size = xbzrle_cache_resize(value);
964 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100965 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
966 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +0200967 return;
968 }
969
970 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300971}
972
973int64_t qmp_query_migrate_cache_size(Error **errp)
974{
975 return migrate_xbzrle_cache_size();
976}
977
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200978void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100979{
Juan Quintelacab30142011-02-22 23:54:21 +0100980 MigrationState *s;
981
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200982 if (value < 0) {
983 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100984 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100985 if (value > SIZE_MAX) {
986 value = SIZE_MAX;
987 }
Juan Quintelacab30142011-02-22 23:54:21 +0100988
Juan Quintela17549e82011-10-05 13:50:43 +0200989 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200990 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100991 if (s->file) {
992 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
993 }
Juan Quintelacab30142011-02-22 23:54:21 +0100994}
995
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200996void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100997{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200998 value *= 1e9;
999 value = MAX(0, MIN(UINT64_MAX, value));
1000 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +00001001}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001002
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001003bool migrate_postcopy_ram(void)
1004{
1005 MigrationState *s;
1006
1007 s = migrate_get_current();
1008
1009 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM];
1010}
1011
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001012bool migrate_auto_converge(void)
1013{
1014 MigrationState *s;
1015
1016 s = migrate_get_current();
1017
1018 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1019}
1020
Peter Lieven323004a2013-07-18 09:48:50 +02001021bool migrate_zero_blocks(void)
1022{
1023 MigrationState *s;
1024
1025 s = migrate_get_current();
1026
1027 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1028}
1029
Liang Li8706d2d2015-03-23 16:32:17 +08001030bool migrate_use_compression(void)
1031{
Liang Lidde4e692015-03-23 16:32:26 +08001032 MigrationState *s;
1033
1034 s = migrate_get_current();
1035
1036 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001037}
1038
1039int migrate_compress_level(void)
1040{
1041 MigrationState *s;
1042
1043 s = migrate_get_current();
1044
Liang Li43c60a82015-03-23 16:32:27 +08001045 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +08001046}
1047
1048int migrate_compress_threads(void)
1049{
1050 MigrationState *s;
1051
1052 s = migrate_get_current();
1053
Liang Li43c60a82015-03-23 16:32:27 +08001054 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +08001055}
1056
Liang Li3fcb38c2015-03-23 16:32:18 +08001057int migrate_decompress_threads(void)
1058{
1059 MigrationState *s;
1060
1061 s = migrate_get_current();
1062
Liang Li43c60a82015-03-23 16:32:27 +08001063 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +08001064}
1065
Juan Quintelab05dc722015-07-07 14:44:05 +02001066bool migrate_use_events(void)
1067{
1068 MigrationState *s;
1069
1070 s = migrate_get_current();
1071
1072 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1073}
1074
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001075int migrate_use_xbzrle(void)
1076{
1077 MigrationState *s;
1078
1079 s = migrate_get_current();
1080
1081 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1082}
1083
1084int64_t migrate_xbzrle_cache_size(void)
1085{
1086 MigrationState *s;
1087
1088 s = migrate_get_current();
1089
1090 return s->xbzrle_cache_size;
1091}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001092
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001093/* migration thread support */
1094/*
1095 * Something bad happened to the RP stream, mark an error
1096 * The caller shall print or trace something to indicate why
1097 */
1098static void mark_source_rp_bad(MigrationState *s)
1099{
1100 s->rp_state.error = true;
1101}
1102
1103static struct rp_cmd_args {
1104 ssize_t len; /* -1 = variable */
1105 const char *name;
1106} rp_cmd_args[] = {
1107 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1108 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1109 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1110 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1111};
1112
1113/*
1114 * Handles messages sent on the return path towards the source VM
1115 *
1116 */
1117static void *source_return_path_thread(void *opaque)
1118{
1119 MigrationState *ms = opaque;
1120 QEMUFile *rp = ms->rp_state.from_dst_file;
1121 uint16_t header_len, header_type;
1122 const int max_len = 512;
1123 uint8_t buf[max_len];
1124 uint32_t tmp32, sibling_error;
1125 int res;
1126
1127 trace_source_return_path_thread_entry();
1128 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1129 migration_is_setup_or_active(ms->state)) {
1130 trace_source_return_path_thread_loop_top();
1131 header_type = qemu_get_be16(rp);
1132 header_len = qemu_get_be16(rp);
1133
1134 if (header_type >= MIG_RP_MSG_MAX ||
1135 header_type == MIG_RP_MSG_INVALID) {
1136 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1137 header_type, header_len);
1138 mark_source_rp_bad(ms);
1139 goto out;
1140 }
1141
1142 if ((rp_cmd_args[header_type].len != -1 &&
1143 header_len != rp_cmd_args[header_type].len) ||
1144 header_len > max_len) {
1145 error_report("RP: Received '%s' message (0x%04x) with"
1146 "incorrect length %d expecting %zu",
1147 rp_cmd_args[header_type].name, header_type, header_len,
1148 (size_t)rp_cmd_args[header_type].len);
1149 mark_source_rp_bad(ms);
1150 goto out;
1151 }
1152
1153 /* We know we've got a valid header by this point */
1154 res = qemu_get_buffer(rp, buf, header_len);
1155 if (res != header_len) {
1156 error_report("RP: Failed reading data for message 0x%04x"
1157 " read %d expected %d",
1158 header_type, res, header_len);
1159 mark_source_rp_bad(ms);
1160 goto out;
1161 }
1162
1163 /* OK, we have the message and the data */
1164 switch (header_type) {
1165 case MIG_RP_MSG_SHUT:
1166 sibling_error = be32_to_cpup((uint32_t *)buf);
1167 trace_source_return_path_thread_shut(sibling_error);
1168 if (sibling_error) {
1169 error_report("RP: Sibling indicated error %d", sibling_error);
1170 mark_source_rp_bad(ms);
1171 }
1172 /*
1173 * We'll let the main thread deal with closing the RP
1174 * we could do a shutdown(2) on it, but we're the only user
1175 * anyway, so there's nothing gained.
1176 */
1177 goto out;
1178
1179 case MIG_RP_MSG_PONG:
1180 tmp32 = be32_to_cpup((uint32_t *)buf);
1181 trace_source_return_path_thread_pong(tmp32);
1182 break;
1183
1184 default:
1185 break;
1186 }
1187 }
1188 if (rp && qemu_file_get_error(rp)) {
1189 trace_source_return_path_thread_bad_end();
1190 mark_source_rp_bad(ms);
1191 }
1192
1193 trace_source_return_path_thread_end();
1194out:
1195 ms->rp_state.from_dst_file = NULL;
1196 qemu_fclose(rp);
1197 return NULL;
1198}
1199
1200__attribute__ (( unused )) /* Until later in patch series */
1201static int open_return_path_on_source(MigrationState *ms)
1202{
1203
1204 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file);
1205 if (!ms->rp_state.from_dst_file) {
1206 return -1;
1207 }
1208
1209 trace_open_return_path_on_source();
1210 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1211 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1212
1213 trace_open_return_path_on_source_continue();
1214
1215 return 0;
1216}
1217
1218__attribute__ (( unused )) /* Until later in patch series */
1219/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1220static int await_return_path_close_on_source(MigrationState *ms)
1221{
1222 /*
1223 * If this is a normal exit then the destination will send a SHUT and the
1224 * rp_thread will exit, however if there's an error we need to cause
1225 * it to exit.
1226 */
1227 if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) {
1228 /*
1229 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1230 * waiting for the destination.
1231 */
1232 qemu_file_shutdown(ms->rp_state.from_dst_file);
1233 mark_source_rp_bad(ms);
1234 }
1235 trace_await_return_path_close_on_source_joining();
1236 qemu_thread_join(&ms->rp_state.rp_thread);
1237 trace_await_return_path_close_on_source_close();
1238 return ms->rp_state.error;
1239}
1240
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001241/**
1242 * migration_completion: Used by migration_thread when there's not much left.
1243 * The caller 'breaks' the loop when this returns.
1244 *
1245 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001246 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001247 * @*old_vm_running: Pointer to old_vm_running flag
1248 * @*start_time: Pointer to time to update
1249 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001250static void migration_completion(MigrationState *s, int current_active_state,
1251 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001252 int64_t *start_time)
1253{
1254 int ret;
1255
1256 qemu_mutex_lock_iothread();
1257 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1258 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1259 *old_vm_running = runstate_is_running();
1260
1261 ret = global_state_store();
1262 if (!ret) {
1263 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1264 if (ret >= 0) {
1265 qemu_file_set_rate_limit(s->file, INT64_MAX);
Dr. David Alan Gilberta3e06c32015-11-05 18:10:41 +00001266 qemu_savevm_state_complete_precopy(s->file);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001267 }
1268 }
1269 qemu_mutex_unlock_iothread();
1270
1271 if (ret < 0) {
1272 goto fail;
1273 }
1274
1275 if (qemu_file_get_error(s->file)) {
1276 trace_migration_completion_file_err();
1277 goto fail;
1278 }
1279
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001280 migrate_set_state(s, current_active_state, MIGRATION_STATUS_COMPLETED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001281 return;
1282
1283fail:
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001284 migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001285}
1286
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001287/*
1288 * Master migration thread on the source VM.
1289 * It drives the migration and pumps the data down the outgoing channel.
1290 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001291static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001292{
Juan Quintela9848a402012-12-19 09:55:50 +01001293 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01001294 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1295 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001296 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001297 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001298 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001299 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001300 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +02001301
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001302 rcu_register_thread();
1303
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001304 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001305 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001306
Alex Blighbc72ad62013-08-21 16:03:08 +01001307 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +08001308 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001309
zhanghailiang31194732015-03-13 16:08:38 +08001310 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001311 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001312 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001313
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001314 if (!qemu_file_rate_limit(s->file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001315 uint64_t pend_post, pend_nonpost;
1316
1317 qemu_savevm_state_pending(s->file, max_size, &pend_nonpost,
1318 &pend_post);
1319 pending_size = pend_nonpost + pend_post;
1320 trace_migrate_pending(pending_size, max_size,
1321 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001322 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001323 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +02001324 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001325 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001326 migration_completion(s, MIGRATION_STATUS_ACTIVE,
1327 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001328 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001329 }
1330 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001331
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001332 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +08001333 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
1334 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001335 break;
1336 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001337 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001338 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001339 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001340 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001341 double bandwidth = transferred_bytes / time_spent;
1342 max_size = bandwidth * migrate_max_downtime() / 1000000;
1343
Michael R. Hines7e114f82013-06-25 21:35:30 -04001344 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1345 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1346
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001347 trace_migrate_transferred(transferred_bytes, time_spent,
1348 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001349 /* if we haven't sent anything, we don't want to recalculate
1350 10000 is a small enough number for our purposes */
1351 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1352 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1353 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001354
Paolo Bonzini1964a392013-02-22 17:36:45 +01001355 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001356 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001357 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001358 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001359 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001360 /* usleep expects microseconds */
1361 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1362 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001363 }
1364
Jason J. Herne070afca2015-09-08 13:12:35 -04001365 /* If we enabled cpu throttling for auto-converge, turn it off. */
1366 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001367 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001368
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001369 qemu_mutex_lock_iothread();
Liang Liea7415f2015-11-02 15:37:01 +08001370 qemu_savevm_state_cleanup();
zhanghailiang31194732015-03-13 16:08:38 +08001371 if (s->state == MIGRATION_STATUS_COMPLETED) {
Peter Lievend6ed7312014-05-12 10:46:00 +02001372 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001373 s->total_time = end_time - s->total_time;
1374 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +02001375 if (s->total_time) {
1376 s->mbps = (((double) transferred_bytes * 8.0) /
1377 ((double) s->total_time)) / 1000;
1378 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001379 runstate_set(RUN_STATE_POSTMIGRATE);
1380 } else {
1381 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001382 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001383 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001384 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001385 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001386 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001387
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001388 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001389 return NULL;
1390}
1391
Juan Quintela9848a402012-12-19 09:55:50 +01001392void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001393{
Juan Quintelacc283e32013-02-01 11:12:26 +01001394 /* This is a best 1st approximation. ns to ms */
1395 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001396 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001397
Paolo Bonzini442773c2013-02-22 17:36:44 +01001398 qemu_file_set_rate_limit(s->file,
1399 s->bandwidth_limit / XFER_LIMIT_RATIO);
1400
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001401 /* Notify before starting migration thread */
1402 notifier_list_notify(&migration_state_notifiers, s);
1403
Liang Li8706d2d2015-03-23 16:32:17 +08001404 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001405 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001406 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001407}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001408
1409PostcopyState postcopy_state_get(void)
1410{
1411 return atomic_mb_read(&incoming_postcopy_state);
1412}
1413
1414/* Set the state and return the old state */
1415PostcopyState postcopy_state_set(PostcopyState new_state)
1416{
1417 return atomic_xchg(&incoming_postcopy_state, new_state);
1418}
1419