blob: 3cdb4f725719a538ae339c6372e71e38bf27f6e1 [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();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000748 if (s->migration_thread_running) {
749 qemu_thread_join(&s->thread);
750 s->migration_thread_running = false;
751 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100752 qemu_mutex_lock_iothread();
753
Liang Li8706d2d2015-03-23 16:32:17 +0800754 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100755 qemu_fclose(s->file);
756 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000757 }
758
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000759 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
760 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100761
Liang Li94f5a432015-11-02 15:37:00 +0800762 if (s->state == MIGRATION_STATUS_CANCELLING) {
763 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
764 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100765 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100766
767 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000768}
769
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200770void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000771{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100772 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100773 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200774 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100775 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100776}
777
Juan Quintela0edda1c2010-05-11 16:28:39 +0200778static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000779{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000780 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000781 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100782 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000783
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000784 if (s->rp_state.from_dst_file) {
785 /* shutdown the rp socket, so causing the rp thread to shutdown */
786 qemu_file_shutdown(s->rp_state.from_dst_file);
787 }
788
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000789 do {
790 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000791 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000792 break;
793 }
zhanghailiang31194732015-03-13 16:08:38 +0800794 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
795 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000796
797 /*
798 * If we're unlucky the migration code might be stuck somewhere in a
799 * send/write while the network has failed and is waiting to timeout;
800 * if we've got shutdown(2) available then we can force it to quit.
801 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
802 * called in a bh, so there is no race against this cancel.
803 */
zhanghailiang31194732015-03-13 16:08:38 +0800804 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000805 qemu_file_shutdown(f);
806 }
aliguori065e2812008-11-11 16:46:33 +0000807}
808
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100809void add_migration_state_change_notifier(Notifier *notify)
810{
811 notifier_list_add(&migration_state_notifiers, notify);
812}
813
814void remove_migration_state_change_notifier(Notifier *notify)
815{
Paolo Bonzini31552522012-01-13 17:34:01 +0100816 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100817}
818
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200819bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200820{
zhanghailiang31194732015-03-13 16:08:38 +0800821 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200822}
823
Juan Quintela70736932011-02-23 00:43:59 +0100824bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100825{
zhanghailiang31194732015-03-13 16:08:38 +0800826 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100827}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200828
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200829bool migration_has_failed(MigrationState *s)
830{
zhanghailiang31194732015-03-13 16:08:38 +0800831 return (s->state == MIGRATION_STATUS_CANCELLED ||
832 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200833}
834
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000835bool migration_in_postcopy(MigrationState *s)
836{
837 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
838}
839
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +0000840MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200841{
Juan Quintela17549e82011-10-05 13:50:43 +0200842 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100843 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300844 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300845 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800846 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
847 int compress_thread_count =
848 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
849 int decompress_thread_count =
850 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400851 int x_cpu_throttle_initial =
852 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
853 int x_cpu_throttle_increment =
854 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300855
856 memcpy(enabled_capabilities, s->enabled_capabilities,
857 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200858
Juan Quintela17549e82011-10-05 13:50:43 +0200859 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300860 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300861 memcpy(s->enabled_capabilities, enabled_capabilities,
862 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300863 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100864
Liang Li43c60a82015-03-23 16:32:27 +0800865 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
866 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
867 compress_thread_count;
868 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
869 decompress_thread_count;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400870 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
871 x_cpu_throttle_initial;
872 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
873 x_cpu_throttle_increment;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200874 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200875 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200876
Alex Blighbc72ad62013-08-21 16:03:08 +0100877 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200878 return s;
879}
Juan Quintelacab30142011-02-22 23:54:21 +0100880
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600881static GSList *migration_blockers;
882
883void migrate_add_blocker(Error *reason)
884{
885 migration_blockers = g_slist_prepend(migration_blockers, reason);
886}
887
888void migrate_del_blocker(Error *reason)
889{
890 migration_blockers = g_slist_remove(migration_blockers, reason);
891}
892
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000893void qmp_migrate_incoming(const char *uri, Error **errp)
894{
895 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000896 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000897
898 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000899 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000900 return;
901 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000902 if (!once) {
903 error_setg(errp, "The incoming migration has already been started");
904 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000905
906 qemu_start_incoming_migration(uri, &local_err);
907
908 if (local_err) {
909 error_propagate(errp, local_err);
910 return;
911 }
912
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000913 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000914}
915
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200916void qmp_migrate(const char *uri, bool has_blk, bool blk,
917 bool has_inc, bool inc, bool has_detach, bool detach,
918 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100919{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200920 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200921 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300922 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100923 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100924
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900925 params.blk = has_blk && blk;
926 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300927
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000928 if (migration_is_setup_or_active(s->state) ||
zhanghailiang31194732015-03-13 16:08:38 +0800929 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100930 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200931 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100932 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100933 if (runstate_check(RUN_STATE_INMIGRATE)) {
934 error_setg(errp, "Guest is waiting for an incoming migration");
935 return;
936 }
937
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200938 if (qemu_savevm_state_blocked(errp)) {
939 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100940 }
941
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600942 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200943 *errp = error_copy(migration_blockers->data);
944 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600945 }
946
Juan Quintela656a2332015-07-01 09:32:29 +0200947 /* We are starting a new migration, so we want to start in a clean
948 state. This change is only needed if previous migration
949 failed/was cancelled. We don't use migrate_set_state() because
950 we are setting the initial state, not changing it. */
951 s->state = MIGRATION_STATUS_NONE;
952
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300953 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100954
955 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200956 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400957#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800958 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400959 rdma_start_outgoing_migration(s, p, &local_err);
960#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100961#if !defined(WIN32)
962 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200963 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100964 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200965 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100966 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200967 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100968#endif
969 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100970 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
971 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +0200972 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200973 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100974 }
975
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200976 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200977 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200978 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200979 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100980 }
Juan Quintelacab30142011-02-22 23:54:21 +0100981}
982
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200983void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100984{
Juan Quintela17549e82011-10-05 13:50:43 +0200985 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100986}
987
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300988void qmp_migrate_set_cache_size(int64_t value, Error **errp)
989{
990 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200991 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300992
993 /* Check for truncation */
994 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100995 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
996 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300997 return;
998 }
999
Orit Wassermana5615b12014-01-30 20:08:36 +02001000 /* Cache should not be larger than guest ram size */
1001 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001002 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1003 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001004 return;
1005 }
1006
Orit Wassermanc91e6812014-01-30 20:08:34 +02001007 new_size = xbzrle_cache_resize(value);
1008 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001009 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1010 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001011 return;
1012 }
1013
1014 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001015}
1016
1017int64_t qmp_query_migrate_cache_size(Error **errp)
1018{
1019 return migrate_xbzrle_cache_size();
1020}
1021
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001022void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001023{
Juan Quintelacab30142011-02-22 23:54:21 +01001024 MigrationState *s;
1025
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001026 if (value < 0) {
1027 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +01001028 }
Paolo Bonzini442773c2013-02-22 17:36:44 +01001029 if (value > SIZE_MAX) {
1030 value = SIZE_MAX;
1031 }
Juan Quintelacab30142011-02-22 23:54:21 +01001032
Juan Quintela17549e82011-10-05 13:50:43 +02001033 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001034 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +01001035 if (s->file) {
1036 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
1037 }
Juan Quintelacab30142011-02-22 23:54:21 +01001038}
1039
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001040void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001041{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001042 value *= 1e9;
1043 value = MAX(0, MIN(UINT64_MAX, value));
1044 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +00001045}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001046
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001047bool migrate_postcopy_ram(void)
1048{
1049 MigrationState *s;
1050
1051 s = migrate_get_current();
1052
1053 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM];
1054}
1055
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001056bool migrate_auto_converge(void)
1057{
1058 MigrationState *s;
1059
1060 s = migrate_get_current();
1061
1062 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1063}
1064
Peter Lieven323004a2013-07-18 09:48:50 +02001065bool migrate_zero_blocks(void)
1066{
1067 MigrationState *s;
1068
1069 s = migrate_get_current();
1070
1071 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1072}
1073
Liang Li8706d2d2015-03-23 16:32:17 +08001074bool migrate_use_compression(void)
1075{
Liang Lidde4e692015-03-23 16:32:26 +08001076 MigrationState *s;
1077
1078 s = migrate_get_current();
1079
1080 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001081}
1082
1083int migrate_compress_level(void)
1084{
1085 MigrationState *s;
1086
1087 s = migrate_get_current();
1088
Liang Li43c60a82015-03-23 16:32:27 +08001089 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +08001090}
1091
1092int migrate_compress_threads(void)
1093{
1094 MigrationState *s;
1095
1096 s = migrate_get_current();
1097
Liang Li43c60a82015-03-23 16:32:27 +08001098 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +08001099}
1100
Liang Li3fcb38c2015-03-23 16:32:18 +08001101int migrate_decompress_threads(void)
1102{
1103 MigrationState *s;
1104
1105 s = migrate_get_current();
1106
Liang Li43c60a82015-03-23 16:32:27 +08001107 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +08001108}
1109
Juan Quintelab05dc722015-07-07 14:44:05 +02001110bool migrate_use_events(void)
1111{
1112 MigrationState *s;
1113
1114 s = migrate_get_current();
1115
1116 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1117}
1118
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001119int migrate_use_xbzrle(void)
1120{
1121 MigrationState *s;
1122
1123 s = migrate_get_current();
1124
1125 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1126}
1127
1128int64_t migrate_xbzrle_cache_size(void)
1129{
1130 MigrationState *s;
1131
1132 s = migrate_get_current();
1133
1134 return s->xbzrle_cache_size;
1135}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001136
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001137/* migration thread support */
1138/*
1139 * Something bad happened to the RP stream, mark an error
1140 * The caller shall print or trace something to indicate why
1141 */
1142static void mark_source_rp_bad(MigrationState *s)
1143{
1144 s->rp_state.error = true;
1145}
1146
1147static struct rp_cmd_args {
1148 ssize_t len; /* -1 = variable */
1149 const char *name;
1150} rp_cmd_args[] = {
1151 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1152 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1153 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1154 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1155};
1156
1157/*
1158 * Handles messages sent on the return path towards the source VM
1159 *
1160 */
1161static void *source_return_path_thread(void *opaque)
1162{
1163 MigrationState *ms = opaque;
1164 QEMUFile *rp = ms->rp_state.from_dst_file;
1165 uint16_t header_len, header_type;
1166 const int max_len = 512;
1167 uint8_t buf[max_len];
1168 uint32_t tmp32, sibling_error;
1169 int res;
1170
1171 trace_source_return_path_thread_entry();
1172 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1173 migration_is_setup_or_active(ms->state)) {
1174 trace_source_return_path_thread_loop_top();
1175 header_type = qemu_get_be16(rp);
1176 header_len = qemu_get_be16(rp);
1177
1178 if (header_type >= MIG_RP_MSG_MAX ||
1179 header_type == MIG_RP_MSG_INVALID) {
1180 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1181 header_type, header_len);
1182 mark_source_rp_bad(ms);
1183 goto out;
1184 }
1185
1186 if ((rp_cmd_args[header_type].len != -1 &&
1187 header_len != rp_cmd_args[header_type].len) ||
1188 header_len > max_len) {
1189 error_report("RP: Received '%s' message (0x%04x) with"
1190 "incorrect length %d expecting %zu",
1191 rp_cmd_args[header_type].name, header_type, header_len,
1192 (size_t)rp_cmd_args[header_type].len);
1193 mark_source_rp_bad(ms);
1194 goto out;
1195 }
1196
1197 /* We know we've got a valid header by this point */
1198 res = qemu_get_buffer(rp, buf, header_len);
1199 if (res != header_len) {
1200 error_report("RP: Failed reading data for message 0x%04x"
1201 " read %d expected %d",
1202 header_type, res, header_len);
1203 mark_source_rp_bad(ms);
1204 goto out;
1205 }
1206
1207 /* OK, we have the message and the data */
1208 switch (header_type) {
1209 case MIG_RP_MSG_SHUT:
1210 sibling_error = be32_to_cpup((uint32_t *)buf);
1211 trace_source_return_path_thread_shut(sibling_error);
1212 if (sibling_error) {
1213 error_report("RP: Sibling indicated error %d", sibling_error);
1214 mark_source_rp_bad(ms);
1215 }
1216 /*
1217 * We'll let the main thread deal with closing the RP
1218 * we could do a shutdown(2) on it, but we're the only user
1219 * anyway, so there's nothing gained.
1220 */
1221 goto out;
1222
1223 case MIG_RP_MSG_PONG:
1224 tmp32 = be32_to_cpup((uint32_t *)buf);
1225 trace_source_return_path_thread_pong(tmp32);
1226 break;
1227
1228 default:
1229 break;
1230 }
1231 }
1232 if (rp && qemu_file_get_error(rp)) {
1233 trace_source_return_path_thread_bad_end();
1234 mark_source_rp_bad(ms);
1235 }
1236
1237 trace_source_return_path_thread_end();
1238out:
1239 ms->rp_state.from_dst_file = NULL;
1240 qemu_fclose(rp);
1241 return NULL;
1242}
1243
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001244static int open_return_path_on_source(MigrationState *ms)
1245{
1246
1247 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file);
1248 if (!ms->rp_state.from_dst_file) {
1249 return -1;
1250 }
1251
1252 trace_open_return_path_on_source();
1253 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1254 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1255
1256 trace_open_return_path_on_source_continue();
1257
1258 return 0;
1259}
1260
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001261/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1262static int await_return_path_close_on_source(MigrationState *ms)
1263{
1264 /*
1265 * If this is a normal exit then the destination will send a SHUT and the
1266 * rp_thread will exit, however if there's an error we need to cause
1267 * it to exit.
1268 */
1269 if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) {
1270 /*
1271 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1272 * waiting for the destination.
1273 */
1274 qemu_file_shutdown(ms->rp_state.from_dst_file);
1275 mark_source_rp_bad(ms);
1276 }
1277 trace_await_return_path_close_on_source_joining();
1278 qemu_thread_join(&ms->rp_state.rp_thread);
1279 trace_await_return_path_close_on_source_close();
1280 return ms->rp_state.error;
1281}
1282
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001283/*
1284 * Switch from normal iteration to postcopy
1285 * Returns non-0 on error
1286 */
1287static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1288{
1289 int ret;
1290 const QEMUSizedBuffer *qsb;
1291 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1292 migrate_set_state(ms, MIGRATION_STATUS_ACTIVE,
1293 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1294
1295 trace_postcopy_start();
1296 qemu_mutex_lock_iothread();
1297 trace_postcopy_start_set_run();
1298
1299 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1300 *old_vm_running = runstate_is_running();
1301 global_state_store();
1302 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1303
1304 if (ret < 0) {
1305 goto fail;
1306 }
1307
1308 /*
1309 * in Finish migrate and with the io-lock held everything should
1310 * be quiet, but we've potentially still got dirty pages and we
1311 * need to tell the destination to throw any pages it's already received
1312 * that are dirty
1313 */
1314 if (ram_postcopy_send_discard_bitmap(ms)) {
1315 error_report("postcopy send discard bitmap failed");
1316 goto fail;
1317 }
1318
1319 /*
1320 * send rest of state - note things that are doing postcopy
1321 * will notice we're in POSTCOPY_ACTIVE and not actually
1322 * wrap their state up here
1323 */
1324 qemu_file_set_rate_limit(ms->file, INT64_MAX);
1325 /* Ping just for debugging, helps line traces up */
1326 qemu_savevm_send_ping(ms->file, 2);
1327
1328 /*
1329 * While loading the device state we may trigger page transfer
1330 * requests and the fd must be free to process those, and thus
1331 * the destination must read the whole device state off the fd before
1332 * it starts processing it. Unfortunately the ad-hoc migration format
1333 * doesn't allow the destination to know the size to read without fully
1334 * parsing it through each devices load-state code (especially the open
1335 * coded devices that use get/put).
1336 * So we wrap the device state up in a package with a length at the start;
1337 * to do this we use a qemu_buf to hold the whole of the device state.
1338 */
1339 QEMUFile *fb = qemu_bufopen("w", NULL);
1340 if (!fb) {
1341 error_report("Failed to create buffered file");
1342 goto fail;
1343 }
1344
1345 qemu_savevm_state_complete_precopy(fb);
1346 qemu_savevm_send_ping(fb, 3);
1347
1348 qemu_savevm_send_postcopy_run(fb);
1349
1350 /* <><> end of stuff going into the package */
1351 qsb = qemu_buf_get(fb);
1352
1353 /* Now send that blob */
1354 if (qemu_savevm_send_packaged(ms->file, qsb)) {
1355 goto fail_closefb;
1356 }
1357 qemu_fclose(fb);
1358 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1359
1360 qemu_mutex_unlock_iothread();
1361
1362 /*
1363 * Although this ping is just for debug, it could potentially be
1364 * used for getting a better measurement of downtime at the source.
1365 */
1366 qemu_savevm_send_ping(ms->file, 4);
1367
1368 ret = qemu_file_get_error(ms->file);
1369 if (ret) {
1370 error_report("postcopy_start: Migration stream errored");
1371 migrate_set_state(ms, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1372 MIGRATION_STATUS_FAILED);
1373 }
1374
1375 return ret;
1376
1377fail_closefb:
1378 qemu_fclose(fb);
1379fail:
1380 migrate_set_state(ms, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1381 MIGRATION_STATUS_FAILED);
1382 qemu_mutex_unlock_iothread();
1383 return -1;
1384}
1385
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001386/**
1387 * migration_completion: Used by migration_thread when there's not much left.
1388 * The caller 'breaks' the loop when this returns.
1389 *
1390 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001391 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001392 * @*old_vm_running: Pointer to old_vm_running flag
1393 * @*start_time: Pointer to time to update
1394 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001395static void migration_completion(MigrationState *s, int current_active_state,
1396 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001397 int64_t *start_time)
1398{
1399 int ret;
1400
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001401 if (s->state == MIGRATION_STATUS_ACTIVE) {
1402 qemu_mutex_lock_iothread();
1403 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1404 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1405 *old_vm_running = runstate_is_running();
1406 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001407
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001408 if (!ret) {
1409 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1410 if (ret >= 0) {
1411 qemu_file_set_rate_limit(s->file, INT64_MAX);
1412 qemu_savevm_state_complete_precopy(s->file);
1413 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001414 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001415 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001416
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001417 if (ret < 0) {
1418 goto fail;
1419 }
1420 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1421 trace_migration_completion_postcopy_end();
1422
1423 qemu_savevm_state_complete_postcopy(s->file);
1424 trace_migration_completion_postcopy_end_after_complete();
1425 }
1426
1427 /*
1428 * If rp was opened we must clean up the thread before
1429 * cleaning everything else up (since if there are no failures
1430 * it will wait for the destination to send it's status in
1431 * a SHUT command).
1432 * Postcopy opens rp if enabled (even if it's not avtivated)
1433 */
1434 if (migrate_postcopy_ram()) {
1435 int rp_error;
1436 trace_migration_completion_postcopy_end_before_rp();
1437 rp_error = await_return_path_close_on_source(s);
1438 trace_migration_completion_postcopy_end_after_rp(rp_error);
1439 if (rp_error) {
1440 goto fail;
1441 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001442 }
1443
1444 if (qemu_file_get_error(s->file)) {
1445 trace_migration_completion_file_err();
1446 goto fail;
1447 }
1448
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001449 migrate_set_state(s, current_active_state, MIGRATION_STATUS_COMPLETED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001450 return;
1451
1452fail:
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001453 migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001454}
1455
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001456/*
1457 * Master migration thread on the source VM.
1458 * It drives the migration and pumps the data down the outgoing channel.
1459 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001460static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001461{
Juan Quintela9848a402012-12-19 09:55:50 +01001462 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001463 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001464 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1465 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001466 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001467 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001468 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001469 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001470 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001471 bool entered_postcopy = false;
1472 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1473 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
Juan Quintela76f59332012-10-03 20:16:24 +02001474
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001475 rcu_register_thread();
1476
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001477 qemu_savevm_state_header(s->file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001478
1479 if (migrate_postcopy_ram()) {
1480 /* Now tell the dest that it should open its end so it can reply */
1481 qemu_savevm_send_open_return_path(s->file);
1482
1483 /* And do a ping that will make stuff easier to debug */
1484 qemu_savevm_send_ping(s->file, 1);
1485
1486 /*
1487 * Tell the destination that we *might* want to do postcopy later;
1488 * if the other end can't do postcopy it should fail now, nice and
1489 * early.
1490 */
1491 qemu_savevm_send_postcopy_advise(s->file);
1492 }
1493
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001494 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001495
Alex Blighbc72ad62013-08-21 16:03:08 +01001496 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001497 current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang31194732015-03-13 16:08:38 +08001498 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001499
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001500 trace_migration_thread_setup_complete();
1501
1502 while (s->state == MIGRATION_STATUS_ACTIVE ||
1503 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001504 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001505 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001506
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001507 if (!qemu_file_rate_limit(s->file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001508 uint64_t pend_post, pend_nonpost;
1509
1510 qemu_savevm_state_pending(s->file, max_size, &pend_nonpost,
1511 &pend_post);
1512 pending_size = pend_nonpost + pend_post;
1513 trace_migrate_pending(pending_size, max_size,
1514 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001515 if (pending_size && pending_size >= max_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001516 /* Still a significant amount to transfer */
1517
1518 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1519 if (migrate_postcopy_ram() &&
1520 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1521 pend_nonpost <= max_size &&
1522 atomic_read(&s->start_postcopy)) {
1523
1524 if (!postcopy_start(s, &old_vm_running)) {
1525 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1526 entered_postcopy = true;
1527 }
1528
1529 continue;
1530 }
1531 /* Just another iteration step */
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001532 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +02001533 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001534 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001535 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001536 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001537 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001538 }
1539 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001540
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001541 if (qemu_file_get_error(s->file)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001542 migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED);
1543 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001544 break;
1545 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001546 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001547 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001548 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001549 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001550 double bandwidth = transferred_bytes / time_spent;
1551 max_size = bandwidth * migrate_max_downtime() / 1000000;
1552
Michael R. Hines7e114f82013-06-25 21:35:30 -04001553 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1554 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1555
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001556 trace_migrate_transferred(transferred_bytes, time_spent,
1557 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001558 /* if we haven't sent anything, we don't want to recalculate
1559 10000 is a small enough number for our purposes */
1560 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1561 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1562 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001563
Paolo Bonzini1964a392013-02-22 17:36:45 +01001564 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001565 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001566 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001567 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001568 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001569 /* usleep expects microseconds */
1570 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1571 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001572 }
1573
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001574 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001575 /* If we enabled cpu throttling for auto-converge, turn it off. */
1576 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001577 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001578
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001579 qemu_mutex_lock_iothread();
Liang Liea7415f2015-11-02 15:37:01 +08001580 qemu_savevm_state_cleanup();
zhanghailiang31194732015-03-13 16:08:38 +08001581 if (s->state == MIGRATION_STATUS_COMPLETED) {
Peter Lievend6ed7312014-05-12 10:46:00 +02001582 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001583 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001584 if (!entered_postcopy) {
1585 s->downtime = end_time - start_time;
1586 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001587 if (s->total_time) {
1588 s->mbps = (((double) transferred_bytes * 8.0) /
1589 ((double) s->total_time)) / 1000;
1590 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001591 runstate_set(RUN_STATE_POSTMIGRATE);
1592 } else {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001593 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001594 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001595 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001596 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001597 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001598 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001599
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001600 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001601 return NULL;
1602}
1603
Juan Quintela9848a402012-12-19 09:55:50 +01001604void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001605{
Juan Quintelacc283e32013-02-01 11:12:26 +01001606 /* This is a best 1st approximation. ns to ms */
1607 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001608 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001609
Paolo Bonzini442773c2013-02-22 17:36:44 +01001610 qemu_file_set_rate_limit(s->file,
1611 s->bandwidth_limit / XFER_LIMIT_RATIO);
1612
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001613 /* Notify before starting migration thread */
1614 notifier_list_notify(&migration_state_notifiers, s);
1615
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001616 /*
1617 * Open the return path; currently for postcopy but other things might
1618 * also want it.
1619 */
1620 if (migrate_postcopy_ram()) {
1621 if (open_return_path_on_source(s)) {
1622 error_report("Unable to open return-path for postcopy");
1623 migrate_set_state(s, MIGRATION_STATUS_SETUP,
1624 MIGRATION_STATUS_FAILED);
1625 migrate_fd_cleanup(s);
1626 return;
1627 }
1628 }
1629
Liang Li8706d2d2015-03-23 16:32:17 +08001630 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001631 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001632 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001633 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001634}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001635
1636PostcopyState postcopy_state_get(void)
1637{
1638 return atomic_mb_read(&incoming_postcopy_state);
1639}
1640
1641/* Set the state and return the old state */
1642PostcopyState postcopy_state_set(PostcopyState new_state)
1643{
1644 return atomic_xchg(&incoming_postcopy_state, new_state);
1645}
1646