blob: afc863a84d24e5b3aff13daf8cf0ff0d4e1597a1 [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"
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000027#include "migration/postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020028#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030029#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010030#include "trace.h"
Juan Quinteladf4b1022014-10-08 10:58:10 +020031#include "qapi/util.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020032#include "qapi-event.h"
Jason J. Herne070afca2015-09-08 13:12:35 -040033#include "qom/cpu.h"
aliguori065e2812008-11-11 16:46:33 +000034
Jason J. Hernedc325622015-09-08 13:12:37 -040035#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000036
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010037/* Amount of time to allocate to each "chunk" of bandwidth-throttled
38 * data. */
39#define BUFFER_DELAY 100
40#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
41
Liang Li8706d2d2015-03-23 16:32:17 +080042/* Default compression thread count */
43#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080044/* Default decompression thread count, usually decompression is at
45 * least 4 times as fast as compression.*/
46#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080047/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
48#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040049/* Define default autoconverge cpu throttle migration parameters */
50#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20
51#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080052
Orit Wasserman17ad9b32012-08-06 21:42:53 +030053/* Migration XBZRLE default cache size */
54#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
55
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010056static NotifierList migration_state_notifiers =
57 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
58
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000059static bool deferred_incoming;
60
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000061/*
62 * Current state of incoming postcopy; note this is not part of
63 * MigrationIncomingState since it's state is used during cleanup
64 * at the end as MIS is being freed.
65 */
66static PostcopyState incoming_postcopy_state;
67
Juan Quintela17549e82011-10-05 13:50:43 +020068/* When we add fault tolerance, we could have several
69 migrations at once. For now we don't need to add
70 dynamic creation of migration */
71
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010072/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020073MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020074{
75 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080076 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010077 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030078 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040079 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080080 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
81 DEFAULT_MIGRATE_COMPRESS_LEVEL,
82 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
83 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
84 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
85 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Jason J. Herne1626fee2015-09-08 13:12:34 -040086 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
87 DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL,
88 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
89 DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT,
Juan Quintela17549e82011-10-05 13:50:43 +020090 };
91
92 return &current_migration;
93}
94
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010095/* For incoming */
96static MigrationIncomingState *mis_current;
97
98MigrationIncomingState *migration_incoming_get_current(void)
99{
100 return mis_current;
101}
102
103MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
104{
Markus Armbruster97f3ad32015-09-14 13:51:31 +0200105 mis_current = g_new0(MigrationIncomingState, 1);
Dr. David Alan Gilbert42e2aa52015-11-05 18:10:34 +0000106 mis_current->from_src_file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100107 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000108 qemu_mutex_init(&mis_current->rp_mutex);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000109 qemu_event_init(&mis_current->main_thread_load_event, false);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100110
111 return mis_current;
112}
113
114void migration_incoming_state_destroy(void)
115{
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000116 qemu_event_destroy(&mis_current->main_thread_load_event);
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100117 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100118 g_free(mis_current);
119 mis_current = NULL;
120}
121
Juan Quinteladf4b1022014-10-08 10:58:10 +0200122
123typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200124 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200125 uint32_t size;
126 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200127 RunState state;
128 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200129} GlobalState;
130
131static GlobalState global_state;
132
Juan Quintela560d0272015-07-15 09:53:46 +0200133int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200134{
135 if (!runstate_store((char *)global_state.runstate,
136 sizeof(global_state.runstate))) {
137 error_report("runstate name too big: %s", global_state.runstate);
138 trace_migrate_state_too_big();
139 return -EINVAL;
140 }
141 return 0;
142}
143
Anthony PERARDc69adea2015-08-03 15:29:19 +0100144void global_state_store_running(void)
145{
146 const char *state = RunState_lookup[RUN_STATE_RUNNING];
147 strncpy((char *)global_state.runstate,
148 state, sizeof(global_state.runstate));
149}
150
Juan Quintela172c4352015-07-08 13:56:26 +0200151static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200152{
Juan Quintela172c4352015-07-08 13:56:26 +0200153 return global_state.received;
154}
155
156static RunState global_state_get_runstate(void)
157{
158 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200159}
160
Juan Quintela13d16812014-10-08 13:58:24 +0200161void global_state_set_optional(void)
162{
163 global_state.optional = true;
164}
165
166static bool global_state_needed(void *opaque)
167{
168 GlobalState *s = opaque;
169 char *runstate = (char *)s->runstate;
170
171 /* If it is not optional, it is mandatory */
172
173 if (s->optional == false) {
174 return true;
175 }
176
177 /* If state is running or paused, it is not needed */
178
179 if (strcmp(runstate, "running") == 0 ||
180 strcmp(runstate, "paused") == 0) {
181 return false;
182 }
183
184 /* for any other state it is needed */
185 return true;
186}
187
Juan Quinteladf4b1022014-10-08 10:58:10 +0200188static int global_state_post_load(void *opaque, int version_id)
189{
190 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200191 Error *local_err = NULL;
192 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200193 char *runstate = (char *)s->runstate;
194
Juan Quintela172c4352015-07-08 13:56:26 +0200195 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200196 trace_migrate_global_state_post_load(runstate);
197
Juan Quintela172c4352015-07-08 13:56:26 +0200198 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200199 -1, &local_err);
200
Juan Quintela172c4352015-07-08 13:56:26 +0200201 if (r == -1) {
202 if (local_err) {
203 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200204 }
Juan Quintela172c4352015-07-08 13:56:26 +0200205 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200206 }
Juan Quintela172c4352015-07-08 13:56:26 +0200207 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200208
Juan Quintela172c4352015-07-08 13:56:26 +0200209 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200210}
211
212static void global_state_pre_save(void *opaque)
213{
214 GlobalState *s = opaque;
215
216 trace_migrate_global_state_pre_save((char *)s->runstate);
217 s->size = strlen((char *)s->runstate) + 1;
218}
219
220static const VMStateDescription vmstate_globalstate = {
221 .name = "globalstate",
222 .version_id = 1,
223 .minimum_version_id = 1,
224 .post_load = global_state_post_load,
225 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200226 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200227 .fields = (VMStateField[]) {
228 VMSTATE_UINT32(size, GlobalState),
229 VMSTATE_BUFFER(runstate, GlobalState),
230 VMSTATE_END_OF_LIST()
231 },
232};
233
234void register_global_state(void)
235{
236 /* We would use it independently that we receive it */
237 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200238 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200239 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
240}
241
Juan Quintelab05dc722015-07-07 14:44:05 +0200242static void migrate_generate_event(int new_state)
243{
244 if (migrate_use_events()) {
245 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200246 }
247}
248
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000249/*
250 * Called on -incoming with a defer: uri.
251 * The migration can be started later after any parameters have been
252 * changed.
253 */
254static void deferred_incoming_migration(Error **errp)
255{
256 if (deferred_incoming) {
257 error_setg(errp, "Incoming migration already deferred");
258 }
259 deferred_incoming = true;
260}
261
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200262void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000263{
aliguori34c9dd82008-10-13 03:14:31 +0000264 const char *p;
265
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200266 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000267 if (!strcmp(uri, "defer")) {
268 deferred_incoming_migration(errp);
269 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200270 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400271#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000272 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400273 rdma_start_incoming_migration(p, errp);
274#endif
aliguori065e2812008-11-11 16:46:33 +0000275#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000276 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200277 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000278 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200279 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000280 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200281 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000282#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000283 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100284 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200285 }
aliguori5bb79102008-10-13 03:12:02 +0000286}
287
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200288static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200289{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200290 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100291 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200292 int ret;
293
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100294 migration_incoming_state_new(f);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000295 postcopy_state_set(POSTCOPY_INCOMING_NONE);
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200296 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200297 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100298
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200299 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200300 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100301 migration_incoming_state_destroy();
302
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200303 if (ret < 0) {
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200304 migrate_generate_event(MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200305 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800306 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600307 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200308 }
Juan Quintela511c0232010-06-09 14:10:55 +0200309
Anthony Liguori0f154232011-11-14 15:09:45 -0600310 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100311 bdrv_invalidate_cache_all(&local_err);
312 if (local_err) {
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100313 migrate_generate_event(MIGRATION_STATUS_FAILED);
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100314 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800315 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100316 exit(EXIT_FAILURE);
317 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600318
Amit Shah92e37622015-10-14 17:37:19 +0530319 /*
320 * This must happen after all error conditions are dealt with and
321 * we're sure the VM is going to be running on this host.
322 */
323 qemu_announce_self();
324
Juan Quintela172c4352015-07-08 13:56:26 +0200325 /* If global state section was not received or we are in running
326 state, we need to obey autostart. Any other state is set with
327 runstate_set. */
Juan Quinteladf4b1022014-10-08 10:58:10 +0200328
Juan Quintela172c4352015-07-08 13:56:26 +0200329 if (!global_state_received() ||
330 global_state_get_runstate() == RUN_STATE_RUNNING) {
Juan Quinteladf4b1022014-10-08 10:58:10 +0200331 if (autostart) {
332 vm_start();
333 } else {
334 runstate_set(RUN_STATE_PAUSED);
335 }
Juan Quintela172c4352015-07-08 13:56:26 +0200336 } else {
337 runstate_set(global_state_get_runstate());
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300338 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800339 migrate_decompress_threads_join();
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100340 /*
341 * This must happen after any state changes since as soon as an external
342 * observer sees this event they might start to prod at the VM assuming
343 * it's ready to use.
344 */
345 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
Juan Quintela511c0232010-06-09 14:10:55 +0200346}
347
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200348void process_incoming_migration(QEMUFile *f)
349{
350 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
351 int fd = qemu_get_fd(f);
352
353 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800354 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100355 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200356 qemu_coroutine_enter(co, f);
357}
358
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000359/*
360 * Send a message on the return channel back to the source
361 * of the migration.
362 */
363void migrate_send_rp_message(MigrationIncomingState *mis,
364 enum mig_rp_message_type message_type,
365 uint16_t len, void *data)
366{
367 trace_migrate_send_rp_message((int)message_type, len);
368 qemu_mutex_lock(&mis->rp_mutex);
369 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
370 qemu_put_be16(mis->to_src_file, len);
371 qemu_put_buffer(mis->to_src_file, data, len);
372 qemu_fflush(mis->to_src_file);
373 qemu_mutex_unlock(&mis->rp_mutex);
374}
375
376/*
377 * Send a 'SHUT' message on the return channel with the given value
378 * to indicate that we've finished with the RP. Non-0 value indicates
379 * error.
380 */
381void migrate_send_rp_shut(MigrationIncomingState *mis,
382 uint32_t value)
383{
384 uint32_t buf;
385
386 buf = cpu_to_be32(value);
387 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
388}
389
390/*
391 * Send a 'PONG' message on the return channel with the given value
392 * (normally in response to a 'PING')
393 */
394void migrate_send_rp_pong(MigrationIncomingState *mis,
395 uint32_t value)
396{
397 uint32_t buf;
398
399 buf = cpu_to_be32(value);
400 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
401}
402
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400403/* amount of nanoseconds we are willing to wait for migration to be down.
404 * the choice of nanoseconds is because it is the maximum resolution that
405 * get_clock() can achieve. It is an internal measure. All user-visible
406 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100407static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400408
409uint64_t migrate_max_downtime(void)
410{
411 return max_downtime;
412}
413
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300414MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
415{
416 MigrationCapabilityStatusList *head = NULL;
417 MigrationCapabilityStatusList *caps;
418 MigrationState *s = migrate_get_current();
419 int i;
420
Michael Tokarev387eede2013-10-05 13:18:28 +0400421 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300422 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
423 if (head == NULL) {
424 head = g_malloc0(sizeof(*caps));
425 caps = head;
426 } else {
427 caps->next = g_malloc0(sizeof(*caps));
428 caps = caps->next;
429 }
430 caps->value =
431 g_malloc(sizeof(*caps->value));
432 caps->value->capability = i;
433 caps->value->state = s->enabled_capabilities[i];
434 }
435
436 return head;
437}
438
Liang Li85de8322015-03-23 16:32:28 +0800439MigrationParameters *qmp_query_migrate_parameters(Error **errp)
440{
441 MigrationParameters *params;
442 MigrationState *s = migrate_get_current();
443
444 params = g_malloc0(sizeof(*params));
445 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
446 params->compress_threads =
447 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
448 params->decompress_threads =
449 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400450 params->x_cpu_throttle_initial =
451 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
452 params->x_cpu_throttle_increment =
453 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Liang Li85de8322015-03-23 16:32:28 +0800454
455 return params;
456}
457
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000458/*
459 * Return true if we're already in the middle of a migration
460 * (i.e. any of the active or setup states)
461 */
462static bool migration_is_setup_or_active(int state)
463{
464 switch (state) {
465 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000466 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000467 case MIGRATION_STATUS_SETUP:
468 return true;
469
470 default:
471 return false;
472
473 }
474}
475
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300476static void get_xbzrle_cache_stats(MigrationInfo *info)
477{
478 if (migrate_use_xbzrle()) {
479 info->has_xbzrle_cache = true;
480 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
481 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
482 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
483 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
484 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800485 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300486 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
487 }
488}
489
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300490MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000491{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300492 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200493 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000494
Juan Quintela17549e82011-10-05 13:50:43 +0200495 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800496 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200497 /* no migration has happened ever */
498 break;
zhanghailiang31194732015-03-13 16:08:38 +0800499 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400500 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400501 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400502 break;
zhanghailiang31194732015-03-13 16:08:38 +0800503 case MIGRATION_STATUS_ACTIVE:
504 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300505 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200506 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100507 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200508 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200509 info->has_expected_downtime = true;
510 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400511 info->has_setup_time = true;
512 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200513
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300514 info->has_ram = true;
515 info->ram = g_malloc0(sizeof(*info->ram));
516 info->ram->transferred = ram_bytes_transferred();
517 info->ram->remaining = ram_bytes_remaining();
518 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300519 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100520 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300521 info->ram->normal = norm_mig_pages_transferred();
522 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200523 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400524 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800525 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200526
Juan Quintela17549e82011-10-05 13:50:43 +0200527 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300528 info->has_disk = true;
529 info->disk = g_malloc0(sizeof(*info->disk));
530 info->disk->transferred = blk_mig_bytes_transferred();
531 info->disk->remaining = blk_mig_bytes_remaining();
532 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000533 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300534
Jason J. Herne47828932015-09-08 13:12:36 -0400535 if (cpu_throttle_active()) {
536 info->has_x_cpu_throttle_percentage = true;
537 info->x_cpu_throttle_percentage = cpu_throttle_get_percentage();
538 }
539
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300540 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200541 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000542 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
543 /* Mostly the same as active; TODO add some postcopy stats */
544 info->has_status = true;
545 info->has_total_time = true;
546 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
547 - s->total_time;
548 info->has_expected_downtime = true;
549 info->expected_downtime = s->expected_downtime;
550 info->has_setup_time = true;
551 info->setup_time = s->setup_time;
552
553 info->has_ram = true;
554 info->ram = g_malloc0(sizeof(*info->ram));
555 info->ram->transferred = ram_bytes_transferred();
556 info->ram->remaining = ram_bytes_remaining();
557 info->ram->total = ram_bytes_total();
558 info->ram->duplicate = dup_mig_pages_transferred();
559 info->ram->skipped = skipped_mig_pages_transferred();
560 info->ram->normal = norm_mig_pages_transferred();
561 info->ram->normal_bytes = norm_mig_bytes_transferred();
562 info->ram->dirty_pages_rate = s->dirty_pages_rate;
563 info->ram->mbps = s->mbps;
564
565 if (blk_mig_active()) {
566 info->has_disk = true;
567 info->disk = g_malloc0(sizeof(*info->disk));
568 info->disk->transferred = blk_mig_bytes_transferred();
569 info->disk->remaining = blk_mig_bytes_remaining();
570 info->disk->total = blk_mig_bytes_total();
571 }
572
573 get_xbzrle_cache_stats(info);
574 break;
zhanghailiang31194732015-03-13 16:08:38 +0800575 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300576 get_xbzrle_cache_stats(info);
577
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300578 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900579 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200580 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200581 info->has_downtime = true;
582 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400583 info->has_setup_time = true;
584 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200585
586 info->has_ram = true;
587 info->ram = g_malloc0(sizeof(*info->ram));
588 info->ram->transferred = ram_bytes_transferred();
589 info->ram->remaining = 0;
590 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300591 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100592 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300593 info->ram->normal = norm_mig_pages_transferred();
594 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400595 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800596 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200597 break;
zhanghailiang31194732015-03-13 16:08:38 +0800598 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300599 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200600 break;
zhanghailiang31194732015-03-13 16:08:38 +0800601 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300602 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200603 break;
aliguori5bb79102008-10-13 03:12:02 +0000604 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800605 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300606
607 return info;
aliguori5bb79102008-10-13 03:12:02 +0000608}
609
Orit Wasserman00458432012-08-06 21:42:48 +0300610void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
611 Error **errp)
612{
613 MigrationState *s = migrate_get_current();
614 MigrationCapabilityStatusList *cap;
615
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000616 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100617 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300618 return;
619 }
620
621 for (cap = params; cap; cap = cap->next) {
622 s->enabled_capabilities[cap->value->capability] = cap->value->state;
623 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000624
625 if (migrate_postcopy_ram()) {
626 if (migrate_use_compression()) {
627 /* The decompression threads asynchronously write into RAM
628 * rather than use the atomic copies needed to avoid
629 * userfaulting. It should be possible to fix the decompression
630 * threads for compatibility in future.
631 */
632 error_report("Postcopy is not currently compatible with "
633 "compression");
634 s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM] =
635 false;
636 }
637 }
Orit Wasserman00458432012-08-06 21:42:48 +0300638}
639
Liang Li85de8322015-03-23 16:32:28 +0800640void qmp_migrate_set_parameters(bool has_compress_level,
641 int64_t compress_level,
642 bool has_compress_threads,
643 int64_t compress_threads,
644 bool has_decompress_threads,
Jason J. Herne1626fee2015-09-08 13:12:34 -0400645 int64_t decompress_threads,
646 bool has_x_cpu_throttle_initial,
647 int64_t x_cpu_throttle_initial,
648 bool has_x_cpu_throttle_increment,
649 int64_t x_cpu_throttle_increment, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800650{
651 MigrationState *s = migrate_get_current();
652
653 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100654 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
655 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800656 return;
657 }
658 if (has_compress_threads &&
659 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100660 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
661 "compress_threads",
662 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800663 return;
664 }
665 if (has_decompress_threads &&
666 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100667 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
668 "decompress_threads",
669 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800670 return;
671 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400672 if (has_x_cpu_throttle_initial &&
673 (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
674 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
675 "x_cpu_throttle_initial",
676 "an integer in the range of 1 to 99");
677 }
678 if (has_x_cpu_throttle_increment &&
679 (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
680 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
681 "x_cpu_throttle_increment",
682 "an integer in the range of 1 to 99");
683 }
Liang Li85de8322015-03-23 16:32:28 +0800684
685 if (has_compress_level) {
686 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
687 }
688 if (has_compress_threads) {
689 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
690 }
691 if (has_decompress_threads) {
692 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
693 decompress_threads;
694 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400695 if (has_x_cpu_throttle_initial) {
696 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
697 x_cpu_throttle_initial;
698 }
699
700 if (has_x_cpu_throttle_increment) {
701 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
702 x_cpu_throttle_increment;
703 }
Liang Li85de8322015-03-23 16:32:28 +0800704}
705
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000706void qmp_migrate_start_postcopy(Error **errp)
707{
708 MigrationState *s = migrate_get_current();
709
710 if (!migrate_postcopy_ram()) {
711 error_setg(errp, "Enable postcopy with migration_set_capability before"
712 " the start of migration");
713 return;
714 }
715
716 if (s->state == MIGRATION_STATUS_NONE) {
717 error_setg(errp, "Postcopy must be started after migration has been"
718 " started");
719 return;
720 }
721 /*
722 * we don't error if migration has finished since that would be racy
723 * with issuing this command.
724 */
725 atomic_set(&s->start_postcopy, true);
726}
727
aliguori065e2812008-11-11 16:46:33 +0000728/* shared migration helpers */
729
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000730static void migrate_set_state(MigrationState *s, int old_state, int new_state)
731{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200732 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200733 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200734 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000735 }
736}
737
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100738static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000739{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100740 MigrationState *s = opaque;
741
742 qemu_bh_delete(s->cleanup_bh);
743 s->cleanup_bh = NULL;
744
aliguori065e2812008-11-11 16:46:33 +0000745 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100746 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100747 qemu_mutex_unlock_iothread();
748 qemu_thread_join(&s->thread);
749 qemu_mutex_lock_iothread();
750
Liang Li8706d2d2015-03-23 16:32:17 +0800751 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100752 qemu_fclose(s->file);
753 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000754 }
755
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000756 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
757 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100758
Liang Li94f5a432015-11-02 15:37:00 +0800759 if (s->state == MIGRATION_STATUS_CANCELLING) {
760 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
761 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100762 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100763
764 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000765}
766
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200767void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000768{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100769 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100770 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200771 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100772 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100773}
774
Juan Quintela0edda1c2010-05-11 16:28:39 +0200775static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000776{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000777 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000778 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100779 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000780
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000781 if (s->rp_state.from_dst_file) {
782 /* shutdown the rp socket, so causing the rp thread to shutdown */
783 qemu_file_shutdown(s->rp_state.from_dst_file);
784 }
785
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000786 do {
787 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000788 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000789 break;
790 }
zhanghailiang31194732015-03-13 16:08:38 +0800791 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
792 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000793
794 /*
795 * If we're unlucky the migration code might be stuck somewhere in a
796 * send/write while the network has failed and is waiting to timeout;
797 * if we've got shutdown(2) available then we can force it to quit.
798 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
799 * called in a bh, so there is no race against this cancel.
800 */
zhanghailiang31194732015-03-13 16:08:38 +0800801 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000802 qemu_file_shutdown(f);
803 }
aliguori065e2812008-11-11 16:46:33 +0000804}
805
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100806void add_migration_state_change_notifier(Notifier *notify)
807{
808 notifier_list_add(&migration_state_notifiers, notify);
809}
810
811void remove_migration_state_change_notifier(Notifier *notify)
812{
Paolo Bonzini31552522012-01-13 17:34:01 +0100813 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100814}
815
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200816bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200817{
zhanghailiang31194732015-03-13 16:08:38 +0800818 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200819}
820
Juan Quintela70736932011-02-23 00:43:59 +0100821bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100822{
zhanghailiang31194732015-03-13 16:08:38 +0800823 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100824}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200825
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200826bool migration_has_failed(MigrationState *s)
827{
zhanghailiang31194732015-03-13 16:08:38 +0800828 return (s->state == MIGRATION_STATUS_CANCELLED ||
829 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200830}
831
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000832bool migration_in_postcopy(MigrationState *s)
833{
834 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
835}
836
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +0000837MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200838{
Juan Quintela17549e82011-10-05 13:50:43 +0200839 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100840 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300841 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300842 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800843 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
844 int compress_thread_count =
845 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
846 int decompress_thread_count =
847 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400848 int x_cpu_throttle_initial =
849 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
850 int x_cpu_throttle_increment =
851 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300852
853 memcpy(enabled_capabilities, s->enabled_capabilities,
854 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200855
Juan Quintela17549e82011-10-05 13:50:43 +0200856 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300857 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300858 memcpy(s->enabled_capabilities, enabled_capabilities,
859 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300860 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100861
Liang Li43c60a82015-03-23 16:32:27 +0800862 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
863 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
864 compress_thread_count;
865 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
866 decompress_thread_count;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400867 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
868 x_cpu_throttle_initial;
869 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
870 x_cpu_throttle_increment;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200871 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200872 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200873
Alex Blighbc72ad62013-08-21 16:03:08 +0100874 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200875 return s;
876}
Juan Quintelacab30142011-02-22 23:54:21 +0100877
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600878static GSList *migration_blockers;
879
880void migrate_add_blocker(Error *reason)
881{
882 migration_blockers = g_slist_prepend(migration_blockers, reason);
883}
884
885void migrate_del_blocker(Error *reason)
886{
887 migration_blockers = g_slist_remove(migration_blockers, reason);
888}
889
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000890void qmp_migrate_incoming(const char *uri, Error **errp)
891{
892 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000893 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000894
895 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000896 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000897 return;
898 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000899 if (!once) {
900 error_setg(errp, "The incoming migration has already been started");
901 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000902
903 qemu_start_incoming_migration(uri, &local_err);
904
905 if (local_err) {
906 error_propagate(errp, local_err);
907 return;
908 }
909
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000910 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000911}
912
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200913void qmp_migrate(const char *uri, bool has_blk, bool blk,
914 bool has_inc, bool inc, bool has_detach, bool detach,
915 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100916{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200917 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200918 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300919 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100920 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100921
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900922 params.blk = has_blk && blk;
923 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300924
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000925 if (migration_is_setup_or_active(s->state) ||
zhanghailiang31194732015-03-13 16:08:38 +0800926 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100927 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200928 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100929 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100930 if (runstate_check(RUN_STATE_INMIGRATE)) {
931 error_setg(errp, "Guest is waiting for an incoming migration");
932 return;
933 }
934
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200935 if (qemu_savevm_state_blocked(errp)) {
936 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100937 }
938
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600939 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200940 *errp = error_copy(migration_blockers->data);
941 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600942 }
943
Juan Quintela656a2332015-07-01 09:32:29 +0200944 /* We are starting a new migration, so we want to start in a clean
945 state. This change is only needed if previous migration
946 failed/was cancelled. We don't use migrate_set_state() because
947 we are setting the initial state, not changing it. */
948 s->state = MIGRATION_STATUS_NONE;
949
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300950 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100951
952 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200953 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400954#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800955 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400956 rdma_start_outgoing_migration(s, p, &local_err);
957#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100958#if !defined(WIN32)
959 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200960 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100961 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200962 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100963 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200964 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100965#endif
966 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100967 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
968 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +0200969 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200970 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100971 }
972
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200973 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200974 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200975 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200976 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100977 }
Juan Quintelacab30142011-02-22 23:54:21 +0100978}
979
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200980void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100981{
Juan Quintela17549e82011-10-05 13:50:43 +0200982 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100983}
984
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300985void qmp_migrate_set_cache_size(int64_t value, Error **errp)
986{
987 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200988 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300989
990 /* Check for truncation */
991 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100992 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
993 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300994 return;
995 }
996
Orit Wassermana5615b12014-01-30 20:08:36 +0200997 /* Cache should not be larger than guest ram size */
998 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100999 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1000 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001001 return;
1002 }
1003
Orit Wassermanc91e6812014-01-30 20:08:34 +02001004 new_size = xbzrle_cache_resize(value);
1005 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001006 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1007 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001008 return;
1009 }
1010
1011 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001012}
1013
1014int64_t qmp_query_migrate_cache_size(Error **errp)
1015{
1016 return migrate_xbzrle_cache_size();
1017}
1018
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001019void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001020{
Juan Quintelacab30142011-02-22 23:54:21 +01001021 MigrationState *s;
1022
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001023 if (value < 0) {
1024 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +01001025 }
Paolo Bonzini442773c2013-02-22 17:36:44 +01001026 if (value > SIZE_MAX) {
1027 value = SIZE_MAX;
1028 }
Juan Quintelacab30142011-02-22 23:54:21 +01001029
Juan Quintela17549e82011-10-05 13:50:43 +02001030 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001031 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +01001032 if (s->file) {
1033 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
1034 }
Juan Quintelacab30142011-02-22 23:54:21 +01001035}
1036
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001037void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001038{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001039 value *= 1e9;
1040 value = MAX(0, MIN(UINT64_MAX, value));
1041 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +00001042}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001043
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001044bool migrate_postcopy_ram(void)
1045{
1046 MigrationState *s;
1047
1048 s = migrate_get_current();
1049
1050 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM];
1051}
1052
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001053bool migrate_auto_converge(void)
1054{
1055 MigrationState *s;
1056
1057 s = migrate_get_current();
1058
1059 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1060}
1061
Peter Lieven323004a2013-07-18 09:48:50 +02001062bool migrate_zero_blocks(void)
1063{
1064 MigrationState *s;
1065
1066 s = migrate_get_current();
1067
1068 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1069}
1070
Liang Li8706d2d2015-03-23 16:32:17 +08001071bool migrate_use_compression(void)
1072{
Liang Lidde4e692015-03-23 16:32:26 +08001073 MigrationState *s;
1074
1075 s = migrate_get_current();
1076
1077 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001078}
1079
1080int migrate_compress_level(void)
1081{
1082 MigrationState *s;
1083
1084 s = migrate_get_current();
1085
Liang Li43c60a82015-03-23 16:32:27 +08001086 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +08001087}
1088
1089int migrate_compress_threads(void)
1090{
1091 MigrationState *s;
1092
1093 s = migrate_get_current();
1094
Liang Li43c60a82015-03-23 16:32:27 +08001095 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +08001096}
1097
Liang Li3fcb38c2015-03-23 16:32:18 +08001098int migrate_decompress_threads(void)
1099{
1100 MigrationState *s;
1101
1102 s = migrate_get_current();
1103
Liang Li43c60a82015-03-23 16:32:27 +08001104 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +08001105}
1106
Juan Quintelab05dc722015-07-07 14:44:05 +02001107bool migrate_use_events(void)
1108{
1109 MigrationState *s;
1110
1111 s = migrate_get_current();
1112
1113 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1114}
1115
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001116int migrate_use_xbzrle(void)
1117{
1118 MigrationState *s;
1119
1120 s = migrate_get_current();
1121
1122 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1123}
1124
1125int64_t migrate_xbzrle_cache_size(void)
1126{
1127 MigrationState *s;
1128
1129 s = migrate_get_current();
1130
1131 return s->xbzrle_cache_size;
1132}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001133
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001134/* migration thread support */
1135/*
1136 * Something bad happened to the RP stream, mark an error
1137 * The caller shall print or trace something to indicate why
1138 */
1139static void mark_source_rp_bad(MigrationState *s)
1140{
1141 s->rp_state.error = true;
1142}
1143
1144static struct rp_cmd_args {
1145 ssize_t len; /* -1 = variable */
1146 const char *name;
1147} rp_cmd_args[] = {
1148 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1149 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1150 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1151 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1152};
1153
1154/*
1155 * Handles messages sent on the return path towards the source VM
1156 *
1157 */
1158static void *source_return_path_thread(void *opaque)
1159{
1160 MigrationState *ms = opaque;
1161 QEMUFile *rp = ms->rp_state.from_dst_file;
1162 uint16_t header_len, header_type;
1163 const int max_len = 512;
1164 uint8_t buf[max_len];
1165 uint32_t tmp32, sibling_error;
1166 int res;
1167
1168 trace_source_return_path_thread_entry();
1169 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1170 migration_is_setup_or_active(ms->state)) {
1171 trace_source_return_path_thread_loop_top();
1172 header_type = qemu_get_be16(rp);
1173 header_len = qemu_get_be16(rp);
1174
1175 if (header_type >= MIG_RP_MSG_MAX ||
1176 header_type == MIG_RP_MSG_INVALID) {
1177 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1178 header_type, header_len);
1179 mark_source_rp_bad(ms);
1180 goto out;
1181 }
1182
1183 if ((rp_cmd_args[header_type].len != -1 &&
1184 header_len != rp_cmd_args[header_type].len) ||
1185 header_len > max_len) {
1186 error_report("RP: Received '%s' message (0x%04x) with"
1187 "incorrect length %d expecting %zu",
1188 rp_cmd_args[header_type].name, header_type, header_len,
1189 (size_t)rp_cmd_args[header_type].len);
1190 mark_source_rp_bad(ms);
1191 goto out;
1192 }
1193
1194 /* We know we've got a valid header by this point */
1195 res = qemu_get_buffer(rp, buf, header_len);
1196 if (res != header_len) {
1197 error_report("RP: Failed reading data for message 0x%04x"
1198 " read %d expected %d",
1199 header_type, res, header_len);
1200 mark_source_rp_bad(ms);
1201 goto out;
1202 }
1203
1204 /* OK, we have the message and the data */
1205 switch (header_type) {
1206 case MIG_RP_MSG_SHUT:
1207 sibling_error = be32_to_cpup((uint32_t *)buf);
1208 trace_source_return_path_thread_shut(sibling_error);
1209 if (sibling_error) {
1210 error_report("RP: Sibling indicated error %d", sibling_error);
1211 mark_source_rp_bad(ms);
1212 }
1213 /*
1214 * We'll let the main thread deal with closing the RP
1215 * we could do a shutdown(2) on it, but we're the only user
1216 * anyway, so there's nothing gained.
1217 */
1218 goto out;
1219
1220 case MIG_RP_MSG_PONG:
1221 tmp32 = be32_to_cpup((uint32_t *)buf);
1222 trace_source_return_path_thread_pong(tmp32);
1223 break;
1224
1225 default:
1226 break;
1227 }
1228 }
1229 if (rp && qemu_file_get_error(rp)) {
1230 trace_source_return_path_thread_bad_end();
1231 mark_source_rp_bad(ms);
1232 }
1233
1234 trace_source_return_path_thread_end();
1235out:
1236 ms->rp_state.from_dst_file = NULL;
1237 qemu_fclose(rp);
1238 return NULL;
1239}
1240
1241__attribute__ (( unused )) /* Until later in patch series */
1242static int open_return_path_on_source(MigrationState *ms)
1243{
1244
1245 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file);
1246 if (!ms->rp_state.from_dst_file) {
1247 return -1;
1248 }
1249
1250 trace_open_return_path_on_source();
1251 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1252 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1253
1254 trace_open_return_path_on_source_continue();
1255
1256 return 0;
1257}
1258
1259__attribute__ (( unused )) /* Until later in patch series */
1260/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1261static int await_return_path_close_on_source(MigrationState *ms)
1262{
1263 /*
1264 * If this is a normal exit then the destination will send a SHUT and the
1265 * rp_thread will exit, however if there's an error we need to cause
1266 * it to exit.
1267 */
1268 if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) {
1269 /*
1270 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1271 * waiting for the destination.
1272 */
1273 qemu_file_shutdown(ms->rp_state.from_dst_file);
1274 mark_source_rp_bad(ms);
1275 }
1276 trace_await_return_path_close_on_source_joining();
1277 qemu_thread_join(&ms->rp_state.rp_thread);
1278 trace_await_return_path_close_on_source_close();
1279 return ms->rp_state.error;
1280}
1281
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001282/**
1283 * migration_completion: Used by migration_thread when there's not much left.
1284 * The caller 'breaks' the loop when this returns.
1285 *
1286 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001287 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001288 * @*old_vm_running: Pointer to old_vm_running flag
1289 * @*start_time: Pointer to time to update
1290 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001291static void migration_completion(MigrationState *s, int current_active_state,
1292 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001293 int64_t *start_time)
1294{
1295 int ret;
1296
1297 qemu_mutex_lock_iothread();
1298 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1299 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1300 *old_vm_running = runstate_is_running();
1301
1302 ret = global_state_store();
1303 if (!ret) {
1304 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1305 if (ret >= 0) {
1306 qemu_file_set_rate_limit(s->file, INT64_MAX);
Dr. David Alan Gilberta3e06c32015-11-05 18:10:41 +00001307 qemu_savevm_state_complete_precopy(s->file);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001308 }
1309 }
1310 qemu_mutex_unlock_iothread();
1311
1312 if (ret < 0) {
1313 goto fail;
1314 }
1315
1316 if (qemu_file_get_error(s->file)) {
1317 trace_migration_completion_file_err();
1318 goto fail;
1319 }
1320
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001321 migrate_set_state(s, current_active_state, MIGRATION_STATUS_COMPLETED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001322 return;
1323
1324fail:
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001325 migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001326}
1327
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001328/*
1329 * Master migration thread on the source VM.
1330 * It drives the migration and pumps the data down the outgoing channel.
1331 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001332static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001333{
Juan Quintela9848a402012-12-19 09:55:50 +01001334 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01001335 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1336 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001337 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001338 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001339 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001340 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001341 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +02001342
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001343 rcu_register_thread();
1344
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001345 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001346 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001347
Alex Blighbc72ad62013-08-21 16:03:08 +01001348 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +08001349 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001350
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001351 trace_migration_thread_setup_complete();
1352
1353 while (s->state == MIGRATION_STATUS_ACTIVE ||
1354 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001355 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001356 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001357
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001358 if (!qemu_file_rate_limit(s->file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001359 uint64_t pend_post, pend_nonpost;
1360
1361 qemu_savevm_state_pending(s->file, max_size, &pend_nonpost,
1362 &pend_post);
1363 pending_size = pend_nonpost + pend_post;
1364 trace_migrate_pending(pending_size, max_size,
1365 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001366 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001367 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +02001368 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001369 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001370 migration_completion(s, MIGRATION_STATUS_ACTIVE,
1371 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001372 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001373 }
1374 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001375
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001376 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +08001377 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
1378 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001379 break;
1380 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001381 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001382 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001383 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001384 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001385 double bandwidth = transferred_bytes / time_spent;
1386 max_size = bandwidth * migrate_max_downtime() / 1000000;
1387
Michael R. Hines7e114f82013-06-25 21:35:30 -04001388 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1389 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1390
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001391 trace_migrate_transferred(transferred_bytes, time_spent,
1392 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001393 /* if we haven't sent anything, we don't want to recalculate
1394 10000 is a small enough number for our purposes */
1395 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1396 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1397 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001398
Paolo Bonzini1964a392013-02-22 17:36:45 +01001399 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001400 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001401 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001402 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001403 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001404 /* usleep expects microseconds */
1405 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1406 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001407 }
1408
Jason J. Herne070afca2015-09-08 13:12:35 -04001409 /* If we enabled cpu throttling for auto-converge, turn it off. */
1410 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001411 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001412
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001413 qemu_mutex_lock_iothread();
Liang Liea7415f2015-11-02 15:37:01 +08001414 qemu_savevm_state_cleanup();
zhanghailiang31194732015-03-13 16:08:38 +08001415 if (s->state == MIGRATION_STATUS_COMPLETED) {
Peter Lievend6ed7312014-05-12 10:46:00 +02001416 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001417 s->total_time = end_time - s->total_time;
1418 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +02001419 if (s->total_time) {
1420 s->mbps = (((double) transferred_bytes * 8.0) /
1421 ((double) s->total_time)) / 1000;
1422 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001423 runstate_set(RUN_STATE_POSTMIGRATE);
1424 } else {
1425 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001426 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001427 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001428 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001429 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001430 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001431
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001432 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001433 return NULL;
1434}
1435
Juan Quintela9848a402012-12-19 09:55:50 +01001436void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001437{
Juan Quintelacc283e32013-02-01 11:12:26 +01001438 /* This is a best 1st approximation. ns to ms */
1439 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001440 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001441
Paolo Bonzini442773c2013-02-22 17:36:44 +01001442 qemu_file_set_rate_limit(s->file,
1443 s->bandwidth_limit / XFER_LIMIT_RATIO);
1444
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001445 /* Notify before starting migration thread */
1446 notifier_list_notify(&migration_state_notifiers, s);
1447
Liang Li8706d2d2015-03-23 16:32:17 +08001448 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001449 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001450 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001451}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001452
1453PostcopyState postcopy_state_get(void)
1454{
1455 return atomic_mb_read(&incoming_postcopy_state);
1456}
1457
1458/* Set the state and return the old state */
1459PostcopyState postcopy_state_set(PostcopyState new_state)
1460{
1461 return atomic_xchg(&incoming_postcopy_state, new_state);
1462}
1463