blob: 34c1a83ad810316f4bdb6382822942c064827a2b [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
Peter Maydell1393a482016-01-26 18:16:54 +000016#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010019#include "qemu/main-loop.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010020#include "migration/migration.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020021#include "migration/qemu-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010023#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010024#include "qapi/qmp/qerror.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000025#include "qapi/util.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/sockets.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020027#include "qemu/rcu.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010028#include "migration/block.h"
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000029#include "migration/postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020030#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030031#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010032#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020033#include "qapi-event.h"
Jason J. Herne070afca2015-09-08 13:12:35 -040034#include "qom/cpu.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000035#include "exec/memory.h"
36#include "exec/address-spaces.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010037#include "io/channel-buffer.h"
Daniel P. Berrangee1226362016-04-27 11:05:16 +010038#include "io/channel-tls.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080039#include "migration/colo.h"
aliguori065e2812008-11-11 16:46:33 +000040
Jason J. Hernedc325622015-09-08 13:12:37 -040041#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000042
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010043/* Amount of time to allocate to each "chunk" of bandwidth-throttled
44 * data. */
45#define BUFFER_DELAY 100
46#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
47
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053048/* Time in milliseconds we are allowed to stop the source,
49 * for sending the last part */
50#define DEFAULT_MIGRATE_SET_DOWNTIME 300
51
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030052/* Maximum migrate downtime set to 2000 seconds */
53#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
54#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
55
Liang Li8706d2d2015-03-23 16:32:17 +080056/* Default compression thread count */
57#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080058/* Default decompression thread count, usually decompression is at
59 * least 4 times as fast as compression.*/
60#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080061/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
62#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040063/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040064#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
65#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080066
Orit Wasserman17ad9b32012-08-06 21:42:53 +030067/* Migration XBZRLE default cache size */
68#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
69
zhanghailiang68b53592016-10-27 14:43:01 +080070/* The delay time (in ms) between two COLO checkpoints
71 * Note: Please change this default value to 10000 when we support hybrid mode.
72 */
73#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
74
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010075static NotifierList migration_state_notifiers =
76 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
77
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000078static bool deferred_incoming;
79
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000080/*
81 * Current state of incoming postcopy; note this is not part of
82 * MigrationIncomingState since it's state is used during cleanup
83 * at the end as MIS is being freed.
84 */
85static PostcopyState incoming_postcopy_state;
86
Juan Quintela17549e82011-10-05 13:50:43 +020087/* When we add fault tolerance, we could have several
88 migrations at once. For now we don't need to add
89 dynamic creation of migration */
90
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010091/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020092MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020093{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000094 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +020095 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080096 .state = MIGRATION_STATUS_NONE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030097 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040098 .mbps = -1,
Daniel P. Berrange2594f562016-04-27 11:05:14 +010099 .parameters = {
100 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
101 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
102 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
103 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
104 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530105 .max_bandwidth = MAX_THROTTLE,
106 .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME,
zhanghailiang68b53592016-10-27 14:43:01 +0800107 .x_checkpoint_delay = DEFAULT_MIGRATE_X_CHECKPOINT_DELAY,
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100108 },
Juan Quintela17549e82011-10-05 13:50:43 +0200109 };
110
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000111 if (!once) {
112 qemu_mutex_init(&current_migration.src_page_req_mutex);
Daniel P. Berrange4af245d2017-03-15 16:16:03 +0000113 current_migration.parameters.tls_creds = g_strdup("");
114 current_migration.parameters.tls_hostname = g_strdup("");
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000115 once = true;
116 }
Juan Quintela17549e82011-10-05 13:50:43 +0200117 return &current_migration;
118}
119
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100120MigrationIncomingState *migration_incoming_get_current(void)
121{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100122 static bool once;
123 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100124
Juan Quintelab4b076d2017-01-23 22:32:06 +0100125 if (!once) {
126 mis_current.state = MIGRATION_STATUS_NONE;
127 memset(&mis_current, 0, sizeof(MigrationIncomingState));
128 QLIST_INIT(&mis_current.loadvm_handlers);
129 qemu_mutex_init(&mis_current.rp_mutex);
130 qemu_event_init(&mis_current.main_thread_load_event, false);
131 once = true;
132 }
133 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100134}
135
136void migration_incoming_state_destroy(void)
137{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100138 struct MigrationIncomingState *mis = migration_incoming_get_current();
139
140 qemu_event_destroy(&mis->main_thread_load_event);
141 loadvm_free_handlers(mis);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100142}
143
Juan Quinteladf4b1022014-10-08 10:58:10 +0200144
145typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200146 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200147 uint32_t size;
148 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200149 RunState state;
150 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200151} GlobalState;
152
153static GlobalState global_state;
154
Juan Quintela560d0272015-07-15 09:53:46 +0200155int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200156{
157 if (!runstate_store((char *)global_state.runstate,
158 sizeof(global_state.runstate))) {
159 error_report("runstate name too big: %s", global_state.runstate);
160 trace_migrate_state_too_big();
161 return -EINVAL;
162 }
163 return 0;
164}
165
Anthony PERARDc69adea2015-08-03 15:29:19 +0100166void global_state_store_running(void)
167{
168 const char *state = RunState_lookup[RUN_STATE_RUNNING];
169 strncpy((char *)global_state.runstate,
170 state, sizeof(global_state.runstate));
171}
172
Juan Quintela172c4352015-07-08 13:56:26 +0200173static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200174{
Juan Quintela172c4352015-07-08 13:56:26 +0200175 return global_state.received;
176}
177
178static RunState global_state_get_runstate(void)
179{
180 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200181}
182
Juan Quintela13d16812014-10-08 13:58:24 +0200183void global_state_set_optional(void)
184{
185 global_state.optional = true;
186}
187
188static bool global_state_needed(void *opaque)
189{
190 GlobalState *s = opaque;
191 char *runstate = (char *)s->runstate;
192
193 /* If it is not optional, it is mandatory */
194
195 if (s->optional == false) {
196 return true;
197 }
198
199 /* If state is running or paused, it is not needed */
200
201 if (strcmp(runstate, "running") == 0 ||
202 strcmp(runstate, "paused") == 0) {
203 return false;
204 }
205
206 /* for any other state it is needed */
207 return true;
208}
209
Juan Quinteladf4b1022014-10-08 10:58:10 +0200210static int global_state_post_load(void *opaque, int version_id)
211{
212 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200213 Error *local_err = NULL;
214 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200215 char *runstate = (char *)s->runstate;
216
Juan Quintela172c4352015-07-08 13:56:26 +0200217 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200218 trace_migrate_global_state_post_load(runstate);
219
Eric Blake7fb1cf12015-11-18 01:52:57 -0700220 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200221 -1, &local_err);
222
Juan Quintela172c4352015-07-08 13:56:26 +0200223 if (r == -1) {
224 if (local_err) {
225 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200226 }
Juan Quintela172c4352015-07-08 13:56:26 +0200227 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200228 }
Juan Quintela172c4352015-07-08 13:56:26 +0200229 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200230
Juan Quintela172c4352015-07-08 13:56:26 +0200231 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200232}
233
234static void global_state_pre_save(void *opaque)
235{
236 GlobalState *s = opaque;
237
238 trace_migrate_global_state_pre_save((char *)s->runstate);
239 s->size = strlen((char *)s->runstate) + 1;
240}
241
242static const VMStateDescription vmstate_globalstate = {
243 .name = "globalstate",
244 .version_id = 1,
245 .minimum_version_id = 1,
246 .post_load = global_state_post_load,
247 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200248 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200249 .fields = (VMStateField[]) {
250 VMSTATE_UINT32(size, GlobalState),
251 VMSTATE_BUFFER(runstate, GlobalState),
252 VMSTATE_END_OF_LIST()
253 },
254};
255
256void register_global_state(void)
257{
258 /* We would use it independently that we receive it */
259 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200260 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200261 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
262}
263
Juan Quintelab05dc722015-07-07 14:44:05 +0200264static void migrate_generate_event(int new_state)
265{
266 if (migrate_use_events()) {
267 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200268 }
269}
270
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000271/*
272 * Called on -incoming with a defer: uri.
273 * The migration can be started later after any parameters have been
274 * changed.
275 */
276static void deferred_incoming_migration(Error **errp)
277{
278 if (deferred_incoming) {
279 error_setg(errp, "Incoming migration already deferred");
280 }
281 deferred_incoming = true;
282}
283
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000284/* Request a range of pages from the source VM at the given
285 * start address.
286 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
287 * as the last request (a name must have been given previously)
288 * Start: Address offset within the RB
289 * Len: Length in bytes required - must be a multiple of pagesize
290 */
291void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
292 ram_addr_t start, size_t len)
293{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100294 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000295 size_t msglen = 12; /* start + len */
296
297 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
298 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
299
300 if (rbname) {
301 int rbname_len = strlen(rbname);
302 assert(rbname_len < 256);
303
304 bufc[msglen++] = rbname_len;
305 memcpy(bufc + msglen, rbname, rbname_len);
306 msglen += rbname_len;
307 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
308 } else {
309 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
310 }
311}
312
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200313void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000314{
aliguori34c9dd82008-10-13 03:14:31 +0000315 const char *p;
316
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200317 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000318 if (!strcmp(uri, "defer")) {
319 deferred_incoming_migration(errp);
320 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200321 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400322#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000323 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400324 rdma_start_incoming_migration(p, errp);
325#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000326 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200327 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000328 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200329 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000330 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200331 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000332 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100333 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200334 }
aliguori5bb79102008-10-13 03:12:02 +0000335}
336
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300337static void process_incoming_migration_bh(void *opaque)
338{
339 Error *local_err = NULL;
340 MigrationIncomingState *mis = opaque;
341
342 /* Make sure all file formats flush their mutable metadata */
343 bdrv_invalidate_cache_all(&local_err);
344 if (local_err) {
345 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
346 MIGRATION_STATUS_FAILED);
347 error_report_err(local_err);
348 migrate_decompress_threads_join();
349 exit(EXIT_FAILURE);
350 }
351
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200352 /* If we get an error here, just don't restart the VM yet. */
353 blk_resume_after_migration(&local_err);
354 if (local_err) {
355 error_free(local_err);
356 local_err = NULL;
357 autostart = false;
358 }
359
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300360 /*
361 * This must happen after all error conditions are dealt with and
362 * we're sure the VM is going to be running on this host.
363 */
364 qemu_announce_self();
365
366 /* If global state section was not received or we are in running
367 state, we need to obey autostart. Any other state is set with
368 runstate_set. */
369
370 if (!global_state_received() ||
371 global_state_get_runstate() == RUN_STATE_RUNNING) {
372 if (autostart) {
373 vm_start();
374 } else {
375 runstate_set(RUN_STATE_PAUSED);
376 }
377 } else {
378 runstate_set(global_state_get_runstate());
379 }
380 migrate_decompress_threads_join();
381 /*
382 * This must happen after any state changes since as soon as an external
383 * observer sees this event they might start to prod at the VM assuming
384 * it's ready to use.
385 */
386 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
387 MIGRATION_STATUS_COMPLETED);
388 qemu_bh_delete(mis->bh);
389 migration_incoming_state_destroy();
390}
391
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200392static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200393{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200394 QEMUFile *f = opaque;
Juan Quintelab4b076d2017-01-23 22:32:06 +0100395 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000396 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200397 int ret;
398
Juan Quintelab4b076d2017-01-23 22:32:06 +0100399 mis->from_src_file = f;
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000400 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000401 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000402 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
403 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200404 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100405
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000406 ps = postcopy_state_get();
407 trace_process_incoming_migration_co_end(ret, ps);
408 if (ps != POSTCOPY_INCOMING_NONE) {
409 if (ps == POSTCOPY_INCOMING_ADVISE) {
410 /*
411 * Where a migration had postcopy enabled (and thus went to advise)
412 * but managed to complete within the precopy period, we can use
413 * the normal exit.
414 */
415 postcopy_ram_incoming_cleanup(mis);
416 } else if (ret >= 0) {
417 /*
418 * Postcopy was started, cleanup should happen at the end of the
419 * postcopy thread.
420 */
421 trace_process_incoming_migration_co_postcopy_end_main();
422 return;
423 }
424 /* Else if something went wrong then just fall out of the normal exit */
425 }
426
zhanghailiang25d0c162016-10-27 14:42:55 +0800427 /* we get COLO info, and know if we are in COLO mode */
428 if (!ret && migration_incoming_enable_colo()) {
429 mis->migration_incoming_co = qemu_coroutine_self();
430 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
431 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
432 mis->have_colo_incoming_thread = true;
433 qemu_coroutine_yield();
434
435 /* Wait checkpoint incoming thread exit before free resource */
436 qemu_thread_join(&mis->colo_incoming_thread);
437 }
438
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200439 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200440 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100441
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200442 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000443 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
444 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200445 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800446 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600447 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200448 }
Juan Quintela511c0232010-06-09 14:10:55 +0200449
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300450 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
451 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200452}
453
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100454void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200455{
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200456 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200457
Liang Li3fcb38c2015-03-23 16:32:18 +0800458 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100459 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200460 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200461}
462
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100463
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100464void migration_channel_process_incoming(MigrationState *s,
465 QIOChannel *ioc)
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100466{
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100467 trace_migration_set_incoming_channel(
468 ioc, object_get_typename(OBJECT(ioc)));
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100469
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100470 if (s->parameters.tls_creds &&
Daniel P. Berrange4af245d2017-03-15 16:16:03 +0000471 *s->parameters.tls_creds &&
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100472 !object_dynamic_cast(OBJECT(ioc),
473 TYPE_QIO_CHANNEL_TLS)) {
474 Error *local_err = NULL;
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100475 migration_tls_channel_process_incoming(s, ioc, &local_err);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100476 if (local_err) {
477 error_report_err(local_err);
478 }
479 } else {
480 QEMUFile *f = qemu_fopen_channel_input(ioc);
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100481 migration_fd_process_incoming(f);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100482 }
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100483}
484
485
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100486void migration_channel_connect(MigrationState *s,
487 QIOChannel *ioc,
488 const char *hostname)
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100489{
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100490 trace_migration_set_outgoing_channel(
491 ioc, object_get_typename(OBJECT(ioc)), hostname);
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100492
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100493 if (s->parameters.tls_creds &&
Daniel P. Berrange4af245d2017-03-15 16:16:03 +0000494 *s->parameters.tls_creds &&
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100495 !object_dynamic_cast(OBJECT(ioc),
496 TYPE_QIO_CHANNEL_TLS)) {
497 Error *local_err = NULL;
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100498 migration_tls_channel_connect(s, ioc, hostname, &local_err);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100499 if (local_err) {
500 migrate_fd_error(s, local_err);
501 error_free(local_err);
502 }
503 } else {
504 QEMUFile *f = qemu_fopen_channel_output(ioc);
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100505
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100506 s->to_dst_file = f;
507
508 migrate_fd_connect(s);
509 }
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100510}
511
512
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000513/*
514 * Send a message on the return channel back to the source
515 * of the migration.
516 */
517void migrate_send_rp_message(MigrationIncomingState *mis,
518 enum mig_rp_message_type message_type,
519 uint16_t len, void *data)
520{
521 trace_migrate_send_rp_message((int)message_type, len);
522 qemu_mutex_lock(&mis->rp_mutex);
523 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
524 qemu_put_be16(mis->to_src_file, len);
525 qemu_put_buffer(mis->to_src_file, data, len);
526 qemu_fflush(mis->to_src_file);
527 qemu_mutex_unlock(&mis->rp_mutex);
528}
529
530/*
531 * Send a 'SHUT' message on the return channel with the given value
532 * to indicate that we've finished with the RP. Non-0 value indicates
533 * error.
534 */
535void migrate_send_rp_shut(MigrationIncomingState *mis,
536 uint32_t value)
537{
538 uint32_t buf;
539
540 buf = cpu_to_be32(value);
541 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
542}
543
544/*
545 * Send a 'PONG' message on the return channel with the given value
546 * (normally in response to a 'PING')
547 */
548void migrate_send_rp_pong(MigrationIncomingState *mis,
549 uint32_t value)
550{
551 uint32_t buf;
552
553 buf = cpu_to_be32(value);
554 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
555}
556
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300557MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
558{
559 MigrationCapabilityStatusList *head = NULL;
560 MigrationCapabilityStatusList *caps;
561 MigrationState *s = migrate_get_current();
562 int i;
563
Michael Tokarev387eede2013-10-05 13:18:28 +0400564 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700565 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
zhanghailiang35a6ed42016-10-27 14:42:52 +0800566 if (i == MIGRATION_CAPABILITY_X_COLO && !colo_supported()) {
567 continue;
568 }
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300569 if (head == NULL) {
570 head = g_malloc0(sizeof(*caps));
571 caps = head;
572 } else {
573 caps->next = g_malloc0(sizeof(*caps));
574 caps = caps->next;
575 }
576 caps->value =
577 g_malloc(sizeof(*caps->value));
578 caps->value->capability = i;
579 caps->value->state = s->enabled_capabilities[i];
580 }
581
582 return head;
583}
584
Liang Li85de8322015-03-23 16:32:28 +0800585MigrationParameters *qmp_query_migrate_parameters(Error **errp)
586{
587 MigrationParameters *params;
588 MigrationState *s = migrate_get_current();
589
590 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500591 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100592 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500593 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100594 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500595 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100596 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500597 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100598 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500599 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100600 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Eric Blakede63ab62016-09-08 22:14:15 -0500601 params->has_tls_creds = !!s->parameters.tls_creds;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100602 params->tls_creds = g_strdup(s->parameters.tls_creds);
Eric Blakede63ab62016-09-08 22:14:15 -0500603 params->has_tls_hostname = !!s->parameters.tls_hostname;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100604 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530605 params->has_max_bandwidth = true;
606 params->max_bandwidth = s->parameters.max_bandwidth;
607 params->has_downtime_limit = true;
608 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800609 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800610 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Liang Li85de8322015-03-23 16:32:28 +0800611
612 return params;
613}
614
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000615/*
616 * Return true if we're already in the middle of a migration
617 * (i.e. any of the active or setup states)
618 */
619static bool migration_is_setup_or_active(int state)
620{
621 switch (state) {
622 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000623 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000624 case MIGRATION_STATUS_SETUP:
625 return true;
626
627 default:
628 return false;
629
630 }
631}
632
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300633static void get_xbzrle_cache_stats(MigrationInfo *info)
634{
635 if (migrate_use_xbzrle()) {
636 info->has_xbzrle_cache = true;
637 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
638 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
639 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
640 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
641 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800642 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300643 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
644 }
645}
646
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100647static void populate_ram_info(MigrationInfo *info, MigrationState *s)
648{
649 info->has_ram = true;
650 info->ram = g_malloc0(sizeof(*info->ram));
651 info->ram->transferred = ram_bytes_transferred();
652 info->ram->total = ram_bytes_total();
653 info->ram->duplicate = dup_mig_pages_transferred();
Juan Quintelabedf53c2017-03-13 20:35:54 +0100654 /* legacy value. It is not used anymore */
655 info->ram->skipped = 0;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100656 info->ram->normal = norm_mig_pages_transferred();
657 info->ram->normal_bytes = norm_mig_bytes_transferred();
658 info->ram->mbps = s->mbps;
659 info->ram->dirty_sync_count = s->dirty_sync_count;
Dr. David Alan Gilbertd3bf5412016-06-13 12:16:42 +0100660 info->ram->postcopy_requests = s->postcopy_requests;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100661
662 if (s->state != MIGRATION_STATUS_COMPLETED) {
663 info->ram->remaining = ram_bytes_remaining();
664 info->ram->dirty_pages_rate = s->dirty_pages_rate;
665 }
666}
667
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300668MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000669{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300670 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200671 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000672
Juan Quintela17549e82011-10-05 13:50:43 +0200673 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800674 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200675 /* no migration has happened ever */
676 break;
zhanghailiang31194732015-03-13 16:08:38 +0800677 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400678 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400679 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400680 break;
zhanghailiang31194732015-03-13 16:08:38 +0800681 case MIGRATION_STATUS_ACTIVE:
682 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300683 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200684 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100685 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200686 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200687 info->has_expected_downtime = true;
688 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400689 info->has_setup_time = true;
690 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200691
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100692 populate_ram_info(info, s);
Juan Quintela8d017192012-08-13 12:31:25 +0200693
Juan Quintela17549e82011-10-05 13:50:43 +0200694 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300695 info->has_disk = true;
696 info->disk = g_malloc0(sizeof(*info->disk));
697 info->disk->transferred = blk_mig_bytes_transferred();
698 info->disk->remaining = blk_mig_bytes_remaining();
699 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000700 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300701
Jason J. Herne47828932015-09-08 13:12:36 -0400702 if (cpu_throttle_active()) {
Jason J. Herned85a31d2016-04-21 14:07:18 -0400703 info->has_cpu_throttle_percentage = true;
704 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
Jason J. Herne47828932015-09-08 13:12:36 -0400705 }
706
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300707 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200708 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000709 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
710 /* Mostly the same as active; TODO add some postcopy stats */
711 info->has_status = true;
712 info->has_total_time = true;
713 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
714 - s->total_time;
715 info->has_expected_downtime = true;
716 info->expected_downtime = s->expected_downtime;
717 info->has_setup_time = true;
718 info->setup_time = s->setup_time;
719
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100720 populate_ram_info(info, s);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000721
722 if (blk_mig_active()) {
723 info->has_disk = true;
724 info->disk = g_malloc0(sizeof(*info->disk));
725 info->disk->transferred = blk_mig_bytes_transferred();
726 info->disk->remaining = blk_mig_bytes_remaining();
727 info->disk->total = blk_mig_bytes_total();
728 }
729
730 get_xbzrle_cache_stats(info);
731 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800732 case MIGRATION_STATUS_COLO:
733 info->has_status = true;
734 /* TODO: display COLO specific information (checkpoint info etc.) */
735 break;
zhanghailiang31194732015-03-13 16:08:38 +0800736 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300737 get_xbzrle_cache_stats(info);
738
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300739 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900740 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200741 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200742 info->has_downtime = true;
743 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400744 info->has_setup_time = true;
745 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200746
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100747 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200748 break;
zhanghailiang31194732015-03-13 16:08:38 +0800749 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300750 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100751 if (s->error) {
752 info->has_error_desc = true;
753 info->error_desc = g_strdup(error_get_pretty(s->error));
754 }
Juan Quintela17549e82011-10-05 13:50:43 +0200755 break;
zhanghailiang31194732015-03-13 16:08:38 +0800756 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300757 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200758 break;
aliguori5bb79102008-10-13 03:12:02 +0000759 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800760 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300761
762 return info;
aliguori5bb79102008-10-13 03:12:02 +0000763}
764
Orit Wasserman00458432012-08-06 21:42:48 +0300765void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
766 Error **errp)
767{
768 MigrationState *s = migrate_get_current();
769 MigrationCapabilityStatusList *cap;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100770 bool old_postcopy_cap = migrate_postcopy_ram();
Orit Wasserman00458432012-08-06 21:42:48 +0300771
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000772 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100773 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300774 return;
775 }
776
777 for (cap = params; cap; cap = cap->next) {
zhanghailiang35a6ed42016-10-27 14:42:52 +0800778 if (cap->value->capability == MIGRATION_CAPABILITY_X_COLO) {
779 if (!colo_supported()) {
780 error_setg(errp, "COLO is not currently supported, please"
781 " configure with --enable-colo option in order to"
782 " support COLO feature");
783 continue;
784 }
785 }
Orit Wasserman00458432012-08-06 21:42:48 +0300786 s->enabled_capabilities[cap->value->capability] = cap->value->state;
787 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000788
789 if (migrate_postcopy_ram()) {
790 if (migrate_use_compression()) {
791 /* The decompression threads asynchronously write into RAM
792 * rather than use the atomic copies needed to avoid
793 * userfaulting. It should be possible to fix the decompression
794 * threads for compatibility in future.
795 */
796 error_report("Postcopy is not currently compatible with "
797 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000798 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000799 false;
800 }
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100801 /* This check is reasonably expensive, so only when it's being
802 * set the first time, also it's only the destination that needs
803 * special support.
804 */
805 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
806 !postcopy_ram_supported_by_host()) {
807 /* postcopy_ram_supported_by_host will have emitted a more
808 * detailed message
809 */
810 error_report("Postcopy is not supported");
811 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
812 false;
813 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000814 }
Orit Wasserman00458432012-08-06 21:42:48 +0300815}
816
Eric Blake7f375e02016-09-08 22:14:16 -0500817void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800818{
819 MigrationState *s = migrate_get_current();
820
Eric Blake7f375e02016-09-08 22:14:16 -0500821 if (params->has_compress_level &&
822 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100823 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
824 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800825 return;
826 }
Eric Blake7f375e02016-09-08 22:14:16 -0500827 if (params->has_compress_threads &&
828 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100829 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
830 "compress_threads",
831 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800832 return;
833 }
Eric Blake7f375e02016-09-08 22:14:16 -0500834 if (params->has_decompress_threads &&
835 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100836 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
837 "decompress_threads",
838 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800839 return;
840 }
Eric Blake7f375e02016-09-08 22:14:16 -0500841 if (params->has_cpu_throttle_initial &&
842 (params->cpu_throttle_initial < 1 ||
843 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400844 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400845 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400846 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530847 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400848 }
Eric Blake7f375e02016-09-08 22:14:16 -0500849 if (params->has_cpu_throttle_increment &&
850 (params->cpu_throttle_increment < 1 ||
851 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400852 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400853 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400854 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530855 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400856 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530857 if (params->has_max_bandwidth &&
858 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
859 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
860 " range of 0 to %zu bytes/second", SIZE_MAX);
861 return;
862 }
863 if (params->has_downtime_limit &&
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300864 (params->downtime_limit < 0 ||
865 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
866 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
867 "the range of 0 to %d milliseconds",
868 MAX_MIGRATE_DOWNTIME);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530869 return;
870 }
zhanghailiang68b53592016-10-27 14:43:01 +0800871 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
872 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
873 "x_checkpoint_delay",
874 "is invalid, it should be positive");
875 }
Liang Li85de8322015-03-23 16:32:28 +0800876
Eric Blake7f375e02016-09-08 22:14:16 -0500877 if (params->has_compress_level) {
878 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800879 }
Eric Blake7f375e02016-09-08 22:14:16 -0500880 if (params->has_compress_threads) {
881 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800882 }
Eric Blake7f375e02016-09-08 22:14:16 -0500883 if (params->has_decompress_threads) {
884 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800885 }
Eric Blake7f375e02016-09-08 22:14:16 -0500886 if (params->has_cpu_throttle_initial) {
887 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400888 }
Eric Blake7f375e02016-09-08 22:14:16 -0500889 if (params->has_cpu_throttle_increment) {
890 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400891 }
Eric Blake7f375e02016-09-08 22:14:16 -0500892 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100893 g_free(s->parameters.tls_creds);
Eric Blake7f375e02016-09-08 22:14:16 -0500894 s->parameters.tls_creds = g_strdup(params->tls_creds);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100895 }
Eric Blake7f375e02016-09-08 22:14:16 -0500896 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100897 g_free(s->parameters.tls_hostname);
Eric Blake7f375e02016-09-08 22:14:16 -0500898 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100899 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530900 if (params->has_max_bandwidth) {
901 s->parameters.max_bandwidth = params->max_bandwidth;
902 if (s->to_dst_file) {
903 qemu_file_set_rate_limit(s->to_dst_file,
904 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
905 }
906 }
907 if (params->has_downtime_limit) {
908 s->parameters.downtime_limit = params->downtime_limit;
909 }
zhanghailiang68b53592016-10-27 14:43:01 +0800910
911 if (params->has_x_checkpoint_delay) {
912 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800913 if (migration_in_colo_state()) {
914 colo_checkpoint_notify(s);
915 }
zhanghailiang68b53592016-10-27 14:43:01 +0800916 }
Liang Li85de8322015-03-23 16:32:28 +0800917}
918
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100919
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000920void qmp_migrate_start_postcopy(Error **errp)
921{
922 MigrationState *s = migrate_get_current();
923
924 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000925 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000926 " the start of migration");
927 return;
928 }
929
930 if (s->state == MIGRATION_STATUS_NONE) {
931 error_setg(errp, "Postcopy must be started after migration has been"
932 " started");
933 return;
934 }
935 /*
936 * we don't error if migration has finished since that would be racy
937 * with issuing this command.
938 */
939 atomic_set(&s->start_postcopy, true);
940}
941
aliguori065e2812008-11-11 16:46:33 +0000942/* shared migration helpers */
943
zhanghailiang48781e52015-12-16 11:47:33 +0000944void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000945{
zhanghailiang48781e52015-12-16 11:47:33 +0000946 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200947 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200948 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000949 }
950}
951
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100952static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000953{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100954 MigrationState *s = opaque;
955
956 qemu_bh_delete(s->cleanup_bh);
957 s->cleanup_bh = NULL;
958
Juan Quintela5e58f962017-04-03 22:06:54 +0200959 migration_page_queue_free(s);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000960
zhanghailiang89a02a92016-01-15 11:37:42 +0800961 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100962 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100963 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000964 if (s->migration_thread_running) {
965 qemu_thread_join(&s->thread);
966 s->migration_thread_running = false;
967 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100968 qemu_mutex_lock_iothread();
969
Liang Li8706d2d2015-03-23 16:32:17 +0800970 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800971 qemu_fclose(s->to_dst_file);
972 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000973 }
974
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000975 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
976 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100977
Liang Li94f5a432015-11-02 15:37:00 +0800978 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000979 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800980 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100981 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100982
983 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000984}
985
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100986void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000987{
Peter Maydell25174052016-10-21 18:41:45 +0100988 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +0800989 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000990 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
991 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100992 if (!s->error) {
993 s->error = error_copy(error);
994 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100995 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100996}
997
Juan Quintela0edda1c2010-05-11 16:28:39 +0200998static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000999{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001000 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001001 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001002 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001003
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001004 if (s->rp_state.from_dst_file) {
1005 /* shutdown the rp socket, so causing the rp thread to shutdown */
1006 qemu_file_shutdown(s->rp_state.from_dst_file);
1007 }
1008
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001009 do {
1010 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001011 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001012 break;
1013 }
zhanghailiang48781e52015-12-16 11:47:33 +00001014 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001015 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001016
1017 /*
1018 * If we're unlucky the migration code might be stuck somewhere in a
1019 * send/write while the network has failed and is waiting to timeout;
1020 * if we've got shutdown(2) available then we can force it to quit.
1021 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1022 * called in a bh, so there is no race against this cancel.
1023 */
zhanghailiang31194732015-03-13 16:08:38 +08001024 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001025 qemu_file_shutdown(f);
1026 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001027 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1028 Error *local_err = NULL;
1029
1030 bdrv_invalidate_cache_all(&local_err);
1031 if (local_err) {
1032 error_report_err(local_err);
1033 } else {
1034 s->block_inactive = false;
1035 }
1036 }
aliguori065e2812008-11-11 16:46:33 +00001037}
1038
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001039void add_migration_state_change_notifier(Notifier *notify)
1040{
1041 notifier_list_add(&migration_state_notifiers, notify);
1042}
1043
1044void remove_migration_state_change_notifier(Notifier *notify)
1045{
Paolo Bonzini31552522012-01-13 17:34:01 +01001046 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001047}
1048
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001049bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001050{
zhanghailiang31194732015-03-13 16:08:38 +08001051 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001052}
1053
Juan Quintela70736932011-02-23 00:43:59 +01001054bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001055{
zhanghailiang31194732015-03-13 16:08:38 +08001056 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001057}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001058
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001059bool migration_has_failed(MigrationState *s)
1060{
zhanghailiang31194732015-03-13 16:08:38 +08001061 return (s->state == MIGRATION_STATUS_CANCELLED ||
1062 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001063}
1064
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001065bool migration_in_postcopy(MigrationState *s)
1066{
1067 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1068}
1069
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001070bool migration_in_postcopy_after_devices(MigrationState *s)
1071{
1072 return migration_in_postcopy(s) && s->postcopy_after_devices;
1073}
1074
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301075bool migration_is_idle(MigrationState *s)
1076{
1077 if (!s) {
1078 s = migrate_get_current();
1079 }
1080
1081 switch (s->state) {
1082 case MIGRATION_STATUS_NONE:
1083 case MIGRATION_STATUS_CANCELLED:
1084 case MIGRATION_STATUS_COMPLETED:
1085 case MIGRATION_STATUS_FAILED:
1086 return true;
1087 case MIGRATION_STATUS_SETUP:
1088 case MIGRATION_STATUS_CANCELLING:
1089 case MIGRATION_STATUS_ACTIVE:
1090 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1091 case MIGRATION_STATUS_COLO:
1092 return false;
1093 case MIGRATION_STATUS__MAX:
1094 g_assert_not_reached();
1095 }
1096
1097 return false;
1098}
1099
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +00001100MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001101{
Juan Quintela17549e82011-10-05 13:50:43 +02001102 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +03001103
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001104 /*
1105 * Reinitialise all migration state, except
1106 * parameters/capabilities that the user set, and
1107 * locks.
1108 */
1109 s->bytes_xfer = 0;
1110 s->xfer_limit = 0;
1111 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001112 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001113 s->state = MIGRATION_STATUS_NONE;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001114 s->params = *params;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001115 s->rp_state.from_dst_file = NULL;
1116 s->rp_state.error = false;
1117 s->mbps = 0.0;
1118 s->downtime = 0;
1119 s->expected_downtime = 0;
1120 s->dirty_pages_rate = 0;
1121 s->dirty_bytes_rate = 0;
1122 s->setup_time = 0;
1123 s->dirty_sync_count = 0;
1124 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001125 s->postcopy_after_devices = false;
Dr. David Alan Gilbertd3bf5412016-06-13 12:16:42 +01001126 s->postcopy_requests = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001127 s->migration_thread_running = false;
1128 s->last_req_rb = NULL;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001129 error_free(s->error);
1130 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001131
zhanghailiang48781e52015-12-16 11:47:33 +00001132 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001133
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001134 QSIMPLEQ_INIT(&s->src_page_requests);
1135
Alex Blighbc72ad62013-08-21 16:03:08 +01001136 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001137 return s;
1138}
Juan Quintelacab30142011-02-22 23:54:21 +01001139
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001140static GSList *migration_blockers;
1141
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301142int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001143{
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301144 if (only_migratable) {
1145 error_propagate(errp, error_copy(reason));
1146 error_prepend(errp, "disallowing migration blocker "
1147 "(--only_migratable) for: ");
1148 return -EACCES;
1149 }
1150
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301151 if (migration_is_idle(NULL)) {
1152 migration_blockers = g_slist_prepend(migration_blockers, reason);
1153 return 0;
1154 }
1155
1156 error_propagate(errp, error_copy(reason));
1157 error_prepend(errp, "disallowing migration blocker (migration in "
1158 "progress) for: ");
1159 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001160}
1161
1162void migrate_del_blocker(Error *reason)
1163{
1164 migration_blockers = g_slist_remove(migration_blockers, reason);
1165}
1166
Ashijeet Acharya7562f902017-02-13 23:34:48 +05301167int check_migratable(Object *obj, Error **err)
1168{
1169 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1170 if (only_migratable && dc->vmsd) {
1171 if (dc->vmsd->unmigratable) {
1172 error_setg(err, "Device %s is not migratable, but "
1173 "--only-migratable was specified",
1174 object_get_typename(obj));
1175 return -1;
1176 }
1177 }
1178
1179 return 0;
1180}
1181
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001182void qmp_migrate_incoming(const char *uri, Error **errp)
1183{
1184 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001185 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001186
1187 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001188 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001189 return;
1190 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001191 if (!once) {
1192 error_setg(errp, "The incoming migration has already been started");
1193 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001194
1195 qemu_start_incoming_migration(uri, &local_err);
1196
1197 if (local_err) {
1198 error_propagate(errp, local_err);
1199 return;
1200 }
1201
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001202 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001203}
1204
Greg Kurz24f39022016-05-04 21:44:19 +02001205bool migration_is_blocked(Error **errp)
1206{
1207 if (qemu_savevm_state_blocked(errp)) {
1208 return true;
1209 }
1210
1211 if (migration_blockers) {
1212 *errp = error_copy(migration_blockers->data);
1213 return true;
1214 }
1215
1216 return false;
1217}
1218
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001219void qmp_migrate(const char *uri, bool has_blk, bool blk,
1220 bool has_inc, bool inc, bool has_detach, bool detach,
1221 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001222{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001223 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001224 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001225 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +01001226 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001227
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +09001228 params.blk = has_blk && blk;
1229 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001230
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001231 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001232 s->state == MIGRATION_STATUS_CANCELLING ||
1233 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001234 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001235 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001236 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001237 if (runstate_check(RUN_STATE_INMIGRATE)) {
1238 error_setg(errp, "Guest is waiting for an incoming migration");
1239 return;
1240 }
1241
Greg Kurz24f39022016-05-04 21:44:19 +02001242 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001243 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001244 }
1245
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001246 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +01001247
1248 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001249 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001250#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001251 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001252 rdma_start_outgoing_migration(s, p, &local_err);
1253#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001254 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001255 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001256 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001257 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001258 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001259 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001260 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001261 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1262 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001263 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1264 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001265 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001266 }
1267
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001268 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001269 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001270 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001271 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001272 }
Juan Quintelacab30142011-02-22 23:54:21 +01001273}
1274
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001275void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001276{
Juan Quintela17549e82011-10-05 13:50:43 +02001277 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001278}
1279
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001280void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1281{
1282 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001283 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001284
1285 /* Check for truncation */
1286 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001287 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1288 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001289 return;
1290 }
1291
Orit Wassermana5615b12014-01-30 20:08:36 +02001292 /* Cache should not be larger than guest ram size */
1293 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001294 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1295 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001296 return;
1297 }
1298
Orit Wassermanc91e6812014-01-30 20:08:34 +02001299 new_size = xbzrle_cache_resize(value);
1300 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001301 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1302 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001303 return;
1304 }
1305
1306 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001307}
1308
1309int64_t qmp_query_migrate_cache_size(Error **errp)
1310{
1311 return migrate_xbzrle_cache_size();
1312}
1313
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001314void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001315{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301316 MigrationParameters p = {
1317 .has_max_bandwidth = true,
1318 .max_bandwidth = value,
1319 };
Juan Quintelacab30142011-02-22 23:54:21 +01001320
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301321 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001322}
1323
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001324void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001325{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001326 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1327 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1328 "the range of 0 to %d seconds",
1329 MAX_MIGRATE_DOWNTIME_SECONDS);
1330 return;
1331 }
1332
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301333 value *= 1000; /* Convert to milliseconds */
1334 value = MAX(0, MIN(INT64_MAX, value));
1335
1336 MigrationParameters p = {
1337 .has_downtime_limit = true,
1338 .downtime_limit = value,
1339 };
1340
1341 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001342}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001343
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001344bool migrate_release_ram(void)
1345{
1346 MigrationState *s;
1347
1348 s = migrate_get_current();
1349
1350 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1351}
1352
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001353bool migrate_postcopy_ram(void)
1354{
1355 MigrationState *s;
1356
1357 s = migrate_get_current();
1358
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001359 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001360}
1361
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001362bool migrate_auto_converge(void)
1363{
1364 MigrationState *s;
1365
1366 s = migrate_get_current();
1367
1368 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1369}
1370
Peter Lieven323004a2013-07-18 09:48:50 +02001371bool migrate_zero_blocks(void)
1372{
1373 MigrationState *s;
1374
1375 s = migrate_get_current();
1376
1377 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1378}
1379
Liang Li8706d2d2015-03-23 16:32:17 +08001380bool migrate_use_compression(void)
1381{
Liang Lidde4e692015-03-23 16:32:26 +08001382 MigrationState *s;
1383
1384 s = migrate_get_current();
1385
1386 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001387}
1388
1389int migrate_compress_level(void)
1390{
1391 MigrationState *s;
1392
1393 s = migrate_get_current();
1394
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001395 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001396}
1397
1398int migrate_compress_threads(void)
1399{
1400 MigrationState *s;
1401
1402 s = migrate_get_current();
1403
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001404 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001405}
1406
Liang Li3fcb38c2015-03-23 16:32:18 +08001407int migrate_decompress_threads(void)
1408{
1409 MigrationState *s;
1410
1411 s = migrate_get_current();
1412
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001413 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001414}
1415
Juan Quintelab05dc722015-07-07 14:44:05 +02001416bool migrate_use_events(void)
1417{
1418 MigrationState *s;
1419
1420 s = migrate_get_current();
1421
1422 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1423}
1424
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001425int migrate_use_xbzrle(void)
1426{
1427 MigrationState *s;
1428
1429 s = migrate_get_current();
1430
1431 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1432}
1433
1434int64_t migrate_xbzrle_cache_size(void)
1435{
1436 MigrationState *s;
1437
1438 s = migrate_get_current();
1439
1440 return s->xbzrle_cache_size;
1441}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001442
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001443/* migration thread support */
1444/*
1445 * Something bad happened to the RP stream, mark an error
1446 * The caller shall print or trace something to indicate why
1447 */
1448static void mark_source_rp_bad(MigrationState *s)
1449{
1450 s->rp_state.error = true;
1451}
1452
1453static struct rp_cmd_args {
1454 ssize_t len; /* -1 = variable */
1455 const char *name;
1456} rp_cmd_args[] = {
1457 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1458 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1459 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001460 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1461 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001462 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1463};
1464
1465/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001466 * Process a request for pages received on the return path,
1467 * We're allowed to send more than requested (e.g. to round to our page size)
1468 * and we don't need to send pages that have already been sent.
1469 */
1470static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1471 ram_addr_t start, size_t len)
1472{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001473 long our_host_ps = getpagesize();
1474
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001475 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001476
1477 /*
1478 * Since we currently insist on matching page sizes, just sanity check
1479 * we're being asked for whole host pages.
1480 */
1481 if (start & (our_host_ps-1) ||
1482 (len & (our_host_ps-1))) {
1483 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1484 " len: %zd", __func__, start, len);
1485 mark_source_rp_bad(ms);
1486 return;
1487 }
1488
1489 if (ram_save_queue_pages(ms, rbname, start, len)) {
1490 mark_source_rp_bad(ms);
1491 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001492}
1493
1494/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001495 * Handles messages sent on the return path towards the source VM
1496 *
1497 */
1498static void *source_return_path_thread(void *opaque)
1499{
1500 MigrationState *ms = opaque;
1501 QEMUFile *rp = ms->rp_state.from_dst_file;
1502 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001503 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001504 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001505 ram_addr_t start = 0; /* =0 to silence warning */
1506 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001507 int res;
1508
1509 trace_source_return_path_thread_entry();
1510 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1511 migration_is_setup_or_active(ms->state)) {
1512 trace_source_return_path_thread_loop_top();
1513 header_type = qemu_get_be16(rp);
1514 header_len = qemu_get_be16(rp);
1515
1516 if (header_type >= MIG_RP_MSG_MAX ||
1517 header_type == MIG_RP_MSG_INVALID) {
1518 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1519 header_type, header_len);
1520 mark_source_rp_bad(ms);
1521 goto out;
1522 }
1523
1524 if ((rp_cmd_args[header_type].len != -1 &&
1525 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001526 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001527 error_report("RP: Received '%s' message (0x%04x) with"
1528 "incorrect length %d expecting %zu",
1529 rp_cmd_args[header_type].name, header_type, header_len,
1530 (size_t)rp_cmd_args[header_type].len);
1531 mark_source_rp_bad(ms);
1532 goto out;
1533 }
1534
1535 /* We know we've got a valid header by this point */
1536 res = qemu_get_buffer(rp, buf, header_len);
1537 if (res != header_len) {
1538 error_report("RP: Failed reading data for message 0x%04x"
1539 " read %d expected %d",
1540 header_type, res, header_len);
1541 mark_source_rp_bad(ms);
1542 goto out;
1543 }
1544
1545 /* OK, we have the message and the data */
1546 switch (header_type) {
1547 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001548 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001549 trace_source_return_path_thread_shut(sibling_error);
1550 if (sibling_error) {
1551 error_report("RP: Sibling indicated error %d", sibling_error);
1552 mark_source_rp_bad(ms);
1553 }
1554 /*
1555 * We'll let the main thread deal with closing the RP
1556 * we could do a shutdown(2) on it, but we're the only user
1557 * anyway, so there's nothing gained.
1558 */
1559 goto out;
1560
1561 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001562 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001563 trace_source_return_path_thread_pong(tmp32);
1564 break;
1565
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001566 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001567 start = ldq_be_p(buf);
1568 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001569 migrate_handle_rp_req_pages(ms, NULL, start, len);
1570 break;
1571
1572 case MIG_RP_MSG_REQ_PAGES_ID:
1573 expected_len = 12 + 1; /* header + termination */
1574
1575 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001576 start = ldq_be_p(buf);
1577 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001578 /* Now we expect an idstr */
1579 tmp32 = buf[12]; /* Length of the following idstr */
1580 buf[13 + tmp32] = '\0';
1581 expected_len += tmp32;
1582 }
1583 if (header_len != expected_len) {
1584 error_report("RP: Req_Page_id with length %d expecting %zd",
1585 header_len, expected_len);
1586 mark_source_rp_bad(ms);
1587 goto out;
1588 }
1589 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1590 break;
1591
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001592 default:
1593 break;
1594 }
1595 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001596 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001597 trace_source_return_path_thread_bad_end();
1598 mark_source_rp_bad(ms);
1599 }
1600
1601 trace_source_return_path_thread_end();
1602out:
1603 ms->rp_state.from_dst_file = NULL;
1604 qemu_fclose(rp);
1605 return NULL;
1606}
1607
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001608static int open_return_path_on_source(MigrationState *ms)
1609{
1610
zhanghailiang89a02a92016-01-15 11:37:42 +08001611 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001612 if (!ms->rp_state.from_dst_file) {
1613 return -1;
1614 }
1615
1616 trace_open_return_path_on_source();
1617 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1618 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1619
1620 trace_open_return_path_on_source_continue();
1621
1622 return 0;
1623}
1624
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001625/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1626static int await_return_path_close_on_source(MigrationState *ms)
1627{
1628 /*
1629 * If this is a normal exit then the destination will send a SHUT and the
1630 * rp_thread will exit, however if there's an error we need to cause
1631 * it to exit.
1632 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001633 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001634 /*
1635 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1636 * waiting for the destination.
1637 */
1638 qemu_file_shutdown(ms->rp_state.from_dst_file);
1639 mark_source_rp_bad(ms);
1640 }
1641 trace_await_return_path_close_on_source_joining();
1642 qemu_thread_join(&ms->rp_state.rp_thread);
1643 trace_await_return_path_close_on_source_close();
1644 return ms->rp_state.error;
1645}
1646
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001647/*
1648 * Switch from normal iteration to postcopy
1649 * Returns non-0 on error
1650 */
1651static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1652{
1653 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001654 QIOChannelBuffer *bioc;
1655 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001656 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001657 bool restart_block = false;
zhanghailiang48781e52015-12-16 11:47:33 +00001658 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001659 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1660
1661 trace_postcopy_start();
1662 qemu_mutex_lock_iothread();
1663 trace_postcopy_start_set_run();
1664
1665 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1666 *old_vm_running = runstate_is_running();
1667 global_state_store();
1668 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001669 if (ret < 0) {
1670 goto fail;
1671 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001672
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001673 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001674 if (ret < 0) {
1675 goto fail;
1676 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001677 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001678
1679 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001680 * Cause any non-postcopiable, but iterative devices to
1681 * send out their final data.
1682 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001683 qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001684
1685 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001686 * in Finish migrate and with the io-lock held everything should
1687 * be quiet, but we've potentially still got dirty pages and we
1688 * need to tell the destination to throw any pages it's already received
1689 * that are dirty
1690 */
1691 if (ram_postcopy_send_discard_bitmap(ms)) {
1692 error_report("postcopy send discard bitmap failed");
1693 goto fail;
1694 }
1695
1696 /*
1697 * send rest of state - note things that are doing postcopy
1698 * will notice we're in POSTCOPY_ACTIVE and not actually
1699 * wrap their state up here
1700 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001701 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001702 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001703 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001704
1705 /*
1706 * While loading the device state we may trigger page transfer
1707 * requests and the fd must be free to process those, and thus
1708 * the destination must read the whole device state off the fd before
1709 * it starts processing it. Unfortunately the ad-hoc migration format
1710 * doesn't allow the destination to know the size to read without fully
1711 * parsing it through each devices load-state code (especially the open
1712 * coded devices that use get/put).
1713 * So we wrap the device state up in a package with a length at the start;
1714 * to do this we use a qemu_buf to hold the whole of the device state.
1715 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001716 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001717 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001718 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1719 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001720
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001721 /*
1722 * Make sure the receiver can get incoming pages before we send the rest
1723 * of the state
1724 */
1725 qemu_savevm_send_postcopy_listen(fb);
1726
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001727 qemu_savevm_state_complete_precopy(fb, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001728 qemu_savevm_send_ping(fb, 3);
1729
1730 qemu_savevm_send_postcopy_run(fb);
1731
1732 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001733
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001734 /* Last point of recovery; as soon as we send the package the destination
1735 * can open devices and potentially start running.
1736 * Lets just check again we've not got any errors.
1737 */
1738 ret = qemu_file_get_error(ms->to_dst_file);
1739 if (ret) {
1740 error_report("postcopy_start: Migration stream errored (pre package)");
1741 goto fail_closefb;
1742 }
1743
1744 restart_block = false;
1745
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001746 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001747 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001748 goto fail_closefb;
1749 }
1750 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001751
1752 /* Send a notify to give a chance for anything that needs to happen
1753 * at the transition to postcopy and after the device state; in particular
1754 * spice needs to trigger a transition now
1755 */
1756 ms->postcopy_after_devices = true;
1757 notifier_list_notify(&migration_state_notifiers, ms);
1758
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001759 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1760
1761 qemu_mutex_unlock_iothread();
1762
1763 /*
1764 * Although this ping is just for debug, it could potentially be
1765 * used for getting a better measurement of downtime at the source.
1766 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001767 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001768
Pavel Butsykinced1c612017-02-03 18:23:21 +03001769 if (migrate_release_ram()) {
1770 ram_postcopy_migrated_memory_release(ms);
1771 }
1772
zhanghailiang89a02a92016-01-15 11:37:42 +08001773 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001774 if (ret) {
1775 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001776 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001777 MIGRATION_STATUS_FAILED);
1778 }
1779
1780 return ret;
1781
1782fail_closefb:
1783 qemu_fclose(fb);
1784fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001785 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001786 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001787 if (restart_block) {
1788 /* A failure happened early enough that we know the destination hasn't
1789 * accessed block devices, so we're safe to recover.
1790 */
1791 Error *local_err = NULL;
1792
1793 bdrv_invalidate_cache_all(&local_err);
1794 if (local_err) {
1795 error_report_err(local_err);
1796 }
1797 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001798 qemu_mutex_unlock_iothread();
1799 return -1;
1800}
1801
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001802/**
1803 * migration_completion: Used by migration_thread when there's not much left.
1804 * The caller 'breaks' the loop when this returns.
1805 *
1806 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001807 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001808 * @*old_vm_running: Pointer to old_vm_running flag
1809 * @*start_time: Pointer to time to update
1810 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001811static void migration_completion(MigrationState *s, int current_active_state,
1812 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001813 int64_t *start_time)
1814{
1815 int ret;
1816
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001817 if (s->state == MIGRATION_STATUS_ACTIVE) {
1818 qemu_mutex_lock_iothread();
1819 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1820 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1821 *old_vm_running = runstate_is_running();
1822 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001823
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001824 if (!ret) {
1825 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
zhanghailiang0b827d52016-10-27 14:42:54 +08001826 /*
1827 * Don't mark the image with BDRV_O_INACTIVE flag if
1828 * we will go into COLO stage later.
1829 */
1830 if (ret >= 0 && !migrate_colo_enabled()) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001831 ret = bdrv_inactivate_all();
1832 }
1833 if (ret >= 0) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001834 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1835 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001836 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001837 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001838 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001839 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001840
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001841 if (ret < 0) {
1842 goto fail;
1843 }
1844 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1845 trace_migration_completion_postcopy_end();
1846
zhanghailiang89a02a92016-01-15 11:37:42 +08001847 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001848 trace_migration_completion_postcopy_end_after_complete();
1849 }
1850
1851 /*
1852 * If rp was opened we must clean up the thread before
1853 * cleaning everything else up (since if there are no failures
1854 * it will wait for the destination to send it's status in
1855 * a SHUT command).
1856 * Postcopy opens rp if enabled (even if it's not avtivated)
1857 */
1858 if (migrate_postcopy_ram()) {
1859 int rp_error;
1860 trace_migration_completion_postcopy_end_before_rp();
1861 rp_error = await_return_path_close_on_source(s);
1862 trace_migration_completion_postcopy_end_after_rp(rp_error);
1863 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001864 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001865 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001866 }
1867
zhanghailiang89a02a92016-01-15 11:37:42 +08001868 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001869 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001870 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001871 }
1872
zhanghailiang0b827d52016-10-27 14:42:54 +08001873 if (!migrate_colo_enabled()) {
1874 migrate_set_state(&s->state, current_active_state,
1875 MIGRATION_STATUS_COMPLETED);
1876 }
1877
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001878 return;
1879
Greg Kurzfe904ea2016-05-18 15:44:36 +02001880fail_invalidate:
1881 /* If not doing postcopy, vm_start() will be called: let's regain
1882 * control on images.
1883 */
1884 if (s->state == MIGRATION_STATUS_ACTIVE) {
1885 Error *local_err = NULL;
1886
zhanghailiang1d2acc32017-01-24 15:59:52 +08001887 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001888 bdrv_invalidate_cache_all(&local_err);
1889 if (local_err) {
1890 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001891 } else {
1892 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02001893 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001894 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001895 }
1896
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001897fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001898 migrate_set_state(&s->state, current_active_state,
1899 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001900}
1901
zhanghailiang35a6ed42016-10-27 14:42:52 +08001902bool migrate_colo_enabled(void)
1903{
1904 MigrationState *s = migrate_get_current();
1905 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
1906}
1907
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001908/*
1909 * Master migration thread on the source VM.
1910 * It drives the migration and pumps the data down the outgoing channel.
1911 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001912static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001913{
Juan Quintela9848a402012-12-19 09:55:50 +01001914 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001915 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001916 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1917 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001918 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001919 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001920 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001921 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001922 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001923 bool entered_postcopy = false;
1924 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1925 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang0b827d52016-10-27 14:42:54 +08001926 bool enable_colo = migrate_colo_enabled();
Juan Quintela76f59332012-10-03 20:16:24 +02001927
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001928 rcu_register_thread();
1929
zhanghailiang89a02a92016-01-15 11:37:42 +08001930 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001931
1932 if (migrate_postcopy_ram()) {
1933 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001934 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001935
1936 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001937 qemu_savevm_send_ping(s->to_dst_file, 1);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001938
1939 /*
1940 * Tell the destination that we *might* want to do postcopy later;
1941 * if the other end can't do postcopy it should fail now, nice and
1942 * early.
1943 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001944 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001945 }
1946
zhanghailiang89a02a92016-01-15 11:37:42 +08001947 qemu_savevm_state_begin(s->to_dst_file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001948
Alex Blighbc72ad62013-08-21 16:03:08 +01001949 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001950 current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang48781e52015-12-16 11:47:33 +00001951 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1952 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001953
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001954 trace_migration_thread_setup_complete();
1955
1956 while (s->state == MIGRATION_STATUS_ACTIVE ||
1957 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001958 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001959 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001960
zhanghailiang89a02a92016-01-15 11:37:42 +08001961 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001962 uint64_t pend_post, pend_nonpost;
1963
zhanghailiang89a02a92016-01-15 11:37:42 +08001964 qemu_savevm_state_pending(s->to_dst_file, max_size, &pend_nonpost,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001965 &pend_post);
1966 pending_size = pend_nonpost + pend_post;
1967 trace_migrate_pending(pending_size, max_size,
1968 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001969 if (pending_size && pending_size >= max_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001970 /* Still a significant amount to transfer */
1971
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001972 if (migrate_postcopy_ram() &&
1973 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1974 pend_nonpost <= max_size &&
1975 atomic_read(&s->start_postcopy)) {
1976
1977 if (!postcopy_start(s, &old_vm_running)) {
1978 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1979 entered_postcopy = true;
1980 }
1981
1982 continue;
1983 }
1984 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001985 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001986 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001987 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001988 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001989 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001990 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001991 }
1992 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001993
zhanghailiang89a02a92016-01-15 11:37:42 +08001994 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001995 migrate_set_state(&s->state, current_active_state,
1996 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001997 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001998 break;
1999 }
Alex Blighbc72ad62013-08-21 16:03:08 +01002000 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002001 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08002002 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
2003 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05002004 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01002005 double bandwidth = (double)transferred_bytes / time_spent;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302006 max_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002007
Wei Yang5b648de2016-01-24 14:09:57 +00002008 s->mbps = (((double) transferred_bytes * 8.0) /
2009 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04002010
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11002011 trace_migrate_transferred(transferred_bytes, time_spent,
2012 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01002013 /* if we haven't sent anything, we don't want to recalculate
2014 10000 is a small enough number for our purposes */
2015 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
2016 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
2017 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002018
zhanghailiang89a02a92016-01-15 11:37:42 +08002019 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002020 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08002021 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002022 }
zhanghailiang89a02a92016-01-15 11:37:42 +08002023 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002024 /* usleep expects microseconds */
2025 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
2026 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002027 }
2028
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002029 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04002030 /* If we enabled cpu throttling for auto-converge, turn it off. */
2031 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08002032 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04002033
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002034 qemu_mutex_lock_iothread();
zhanghailiang0b827d52016-10-27 14:42:54 +08002035 /*
2036 * The resource has been allocated by migration will be reused in COLO
2037 * process, so don't release them.
2038 */
2039 if (!enable_colo) {
2040 qemu_savevm_state_cleanup();
2041 }
zhanghailiang31194732015-03-13 16:08:38 +08002042 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08002043 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002044 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002045 if (!entered_postcopy) {
2046 s->downtime = end_time - start_time;
2047 }
Peter Lievend6ed7312014-05-12 10:46:00 +02002048 if (s->total_time) {
2049 s->mbps = (((double) transferred_bytes * 8.0) /
2050 ((double) s->total_time)) / 1000;
2051 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002052 runstate_set(RUN_STATE_POSTMIGRATE);
2053 } else {
zhanghailiang0b827d52016-10-27 14:42:54 +08002054 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
2055 migrate_start_colo_process(s);
2056 qemu_savevm_state_cleanup();
2057 /*
2058 * Fixme: we will run VM in COLO no matter its old running state.
2059 * After exited COLO, we will keep running.
2060 */
2061 old_vm_running = true;
2062 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002063 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002064 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01002065 } else {
2066 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2067 runstate_set(RUN_STATE_POSTMIGRATE);
2068 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01002069 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002070 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002071 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01002072 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002073
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002074 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002075 return NULL;
2076}
2077
Juan Quintela9848a402012-12-19 09:55:50 +01002078void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002079{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302080 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002081 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002082
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01002083 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08002084 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302085 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002086
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002087 /* Notify before starting migration thread */
2088 notifier_list_notify(&migration_state_notifiers, s);
2089
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002090 /*
2091 * Open the return path; currently for postcopy but other things might
2092 * also want it.
2093 */
2094 if (migrate_postcopy_ram()) {
2095 if (open_return_path_on_source(s)) {
2096 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00002097 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002098 MIGRATION_STATUS_FAILED);
2099 migrate_fd_cleanup(s);
2100 return;
2101 }
2102 }
2103
Liang Li8706d2d2015-03-23 16:32:17 +08002104 migrate_compress_threads_create();
Pankaj Gupta009fad72017-01-23 19:12:56 +05302105 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002106 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002107 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002108}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002109
2110PostcopyState postcopy_state_get(void)
2111{
2112 return atomic_mb_read(&incoming_postcopy_state);
2113}
2114
2115/* Set the state and return the old state */
2116PostcopyState postcopy_state_set(PostcopyState new_state)
2117{
2118 return atomic_xchg(&incoming_postcopy_state, new_state);
2119}
2120