blob: 5c9285125a7911475a251ec4e783cb43b4302d02 [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"
Juan Quintela795c40b2017-04-06 12:00:28 +020020#include "migration/blocker.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010021#include "migration/migration.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020022#include "migration/qemu-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010023#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010024#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010025#include "qapi/qmp/qerror.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000026#include "qapi/util.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/sockets.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020028#include "qemu/rcu.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010029#include "migration/block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020030#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020031#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030032#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010033#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020034#include "qapi-event.h"
Jason J. Herne070afca2015-09-08 13:12:35 -040035#include "qom/cpu.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000036#include "exec/memory.h"
37#include "exec/address-spaces.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010038#include "io/channel-buffer.h"
Daniel P. Berrangee1226362016-04-27 11:05:16 +010039#include "io/channel-tls.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080040#include "migration/colo.h"
aliguori065e2812008-11-11 16:46:33 +000041
Jason J. Hernedc325622015-09-08 13:12:37 -040042#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000043
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010044/* Amount of time to allocate to each "chunk" of bandwidth-throttled
45 * data. */
46#define BUFFER_DELAY 100
47#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
48
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053049/* Time in milliseconds we are allowed to stop the source,
50 * for sending the last part */
51#define DEFAULT_MIGRATE_SET_DOWNTIME 300
52
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030053/* Maximum migrate downtime set to 2000 seconds */
54#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
55#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
56
Liang Li8706d2d2015-03-23 16:32:17 +080057/* Default compression thread count */
58#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080059/* Default decompression thread count, usually decompression is at
60 * least 4 times as fast as compression.*/
61#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080062/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
63#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040064/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040065#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
66#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080067
Orit Wasserman17ad9b32012-08-06 21:42:53 +030068/* Migration XBZRLE default cache size */
69#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
70
zhanghailiang68b53592016-10-27 14:43:01 +080071/* The delay time (in ms) between two COLO checkpoints
72 * Note: Please change this default value to 10000 when we support hybrid mode.
73 */
74#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
75
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010076static NotifierList migration_state_notifiers =
77 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
78
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000079static bool deferred_incoming;
80
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000081/*
82 * Current state of incoming postcopy; note this is not part of
83 * MigrationIncomingState since it's state is used during cleanup
84 * at the end as MIS is being freed.
85 */
86static PostcopyState incoming_postcopy_state;
87
Juan Quintela17549e82011-10-05 13:50:43 +020088/* When we add fault tolerance, we could have several
89 migrations at once. For now we don't need to add
90 dynamic creation of migration */
91
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010092/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020093MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020094{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000095 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +020096 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080097 .state = MIGRATION_STATUS_NONE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030098 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040099 .mbps = -1,
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100100 .parameters = {
101 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
102 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
103 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
104 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
105 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530106 .max_bandwidth = MAX_THROTTLE,
107 .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME,
zhanghailiang68b53592016-10-27 14:43:01 +0800108 .x_checkpoint_delay = DEFAULT_MIGRATE_X_CHECKPOINT_DELAY,
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100109 },
Juan Quintela17549e82011-10-05 13:50:43 +0200110 };
111
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000112 if (!once) {
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
Kevin Wolface21a52017-05-04 18:52:36 +0200342 /* Make sure all file formats flush their mutable metadata.
343 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300344 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200345 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200346 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200347 local_err = NULL;
348 autostart = false;
349 }
350
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300351 /*
352 * This must happen after all error conditions are dealt with and
353 * we're sure the VM is going to be running on this host.
354 */
355 qemu_announce_self();
356
357 /* If global state section was not received or we are in running
358 state, we need to obey autostart. Any other state is set with
359 runstate_set. */
360
361 if (!global_state_received() ||
362 global_state_get_runstate() == RUN_STATE_RUNNING) {
363 if (autostart) {
364 vm_start();
365 } else {
366 runstate_set(RUN_STATE_PAUSED);
367 }
368 } else {
369 runstate_set(global_state_get_runstate());
370 }
371 migrate_decompress_threads_join();
372 /*
373 * This must happen after any state changes since as soon as an external
374 * observer sees this event they might start to prod at the VM assuming
375 * it's ready to use.
376 */
377 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
378 MIGRATION_STATUS_COMPLETED);
379 qemu_bh_delete(mis->bh);
380 migration_incoming_state_destroy();
381}
382
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200383static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200384{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200385 QEMUFile *f = opaque;
Juan Quintelab4b076d2017-01-23 22:32:06 +0100386 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000387 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200388 int ret;
389
Juan Quintelab4b076d2017-01-23 22:32:06 +0100390 mis->from_src_file = f;
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000391 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000392 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000393 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
394 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200395 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100396
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000397 ps = postcopy_state_get();
398 trace_process_incoming_migration_co_end(ret, ps);
399 if (ps != POSTCOPY_INCOMING_NONE) {
400 if (ps == POSTCOPY_INCOMING_ADVISE) {
401 /*
402 * Where a migration had postcopy enabled (and thus went to advise)
403 * but managed to complete within the precopy period, we can use
404 * the normal exit.
405 */
406 postcopy_ram_incoming_cleanup(mis);
407 } else if (ret >= 0) {
408 /*
409 * Postcopy was started, cleanup should happen at the end of the
410 * postcopy thread.
411 */
412 trace_process_incoming_migration_co_postcopy_end_main();
413 return;
414 }
415 /* Else if something went wrong then just fall out of the normal exit */
416 }
417
zhanghailiang25d0c162016-10-27 14:42:55 +0800418 /* we get COLO info, and know if we are in COLO mode */
419 if (!ret && migration_incoming_enable_colo()) {
420 mis->migration_incoming_co = qemu_coroutine_self();
421 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
422 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
423 mis->have_colo_incoming_thread = true;
424 qemu_coroutine_yield();
425
426 /* Wait checkpoint incoming thread exit before free resource */
427 qemu_thread_join(&mis->colo_incoming_thread);
428 }
429
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200430 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000431 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
432 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200433 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800434 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600435 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200436 }
Juan Quintela511c0232010-06-09 14:10:55 +0200437
Laurent Viviere8199e42017-04-12 15:53:11 +0200438 qemu_fclose(f);
439 free_xbzrle_decoded_buf();
440
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300441 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
442 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200443}
444
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100445void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200446{
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200447 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200448
Liang Li3fcb38c2015-03-23 16:32:18 +0800449 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100450 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200451 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200452}
453
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100454
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100455void migration_channel_process_incoming(MigrationState *s,
456 QIOChannel *ioc)
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100457{
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100458 trace_migration_set_incoming_channel(
459 ioc, object_get_typename(OBJECT(ioc)));
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100460
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100461 if (s->parameters.tls_creds &&
Daniel P. Berrange4af245d2017-03-15 16:16:03 +0000462 *s->parameters.tls_creds &&
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100463 !object_dynamic_cast(OBJECT(ioc),
464 TYPE_QIO_CHANNEL_TLS)) {
465 Error *local_err = NULL;
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100466 migration_tls_channel_process_incoming(s, ioc, &local_err);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100467 if (local_err) {
468 error_report_err(local_err);
469 }
470 } else {
471 QEMUFile *f = qemu_fopen_channel_input(ioc);
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100472 migration_fd_process_incoming(f);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100473 }
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100474}
475
476
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100477void migration_channel_connect(MigrationState *s,
478 QIOChannel *ioc,
479 const char *hostname)
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100480{
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100481 trace_migration_set_outgoing_channel(
482 ioc, object_get_typename(OBJECT(ioc)), hostname);
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100483
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100484 if (s->parameters.tls_creds &&
Daniel P. Berrange4af245d2017-03-15 16:16:03 +0000485 *s->parameters.tls_creds &&
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100486 !object_dynamic_cast(OBJECT(ioc),
487 TYPE_QIO_CHANNEL_TLS)) {
488 Error *local_err = NULL;
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100489 migration_tls_channel_connect(s, ioc, hostname, &local_err);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100490 if (local_err) {
491 migrate_fd_error(s, local_err);
492 error_free(local_err);
493 }
494 } else {
495 QEMUFile *f = qemu_fopen_channel_output(ioc);
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100496
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100497 s->to_dst_file = f;
498
499 migrate_fd_connect(s);
500 }
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100501}
502
503
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000504/*
505 * Send a message on the return channel back to the source
506 * of the migration.
507 */
508void migrate_send_rp_message(MigrationIncomingState *mis,
509 enum mig_rp_message_type message_type,
510 uint16_t len, void *data)
511{
512 trace_migrate_send_rp_message((int)message_type, len);
513 qemu_mutex_lock(&mis->rp_mutex);
514 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
515 qemu_put_be16(mis->to_src_file, len);
516 qemu_put_buffer(mis->to_src_file, data, len);
517 qemu_fflush(mis->to_src_file);
518 qemu_mutex_unlock(&mis->rp_mutex);
519}
520
521/*
522 * Send a 'SHUT' message on the return channel with the given value
523 * to indicate that we've finished with the RP. Non-0 value indicates
524 * error.
525 */
526void migrate_send_rp_shut(MigrationIncomingState *mis,
527 uint32_t value)
528{
529 uint32_t buf;
530
531 buf = cpu_to_be32(value);
532 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
533}
534
535/*
536 * Send a 'PONG' message on the return channel with the given value
537 * (normally in response to a 'PING')
538 */
539void migrate_send_rp_pong(MigrationIncomingState *mis,
540 uint32_t value)
541{
542 uint32_t buf;
543
544 buf = cpu_to_be32(value);
545 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
546}
547
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300548MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
549{
550 MigrationCapabilityStatusList *head = NULL;
551 MigrationCapabilityStatusList *caps;
552 MigrationState *s = migrate_get_current();
553 int i;
554
Michael Tokarev387eede2013-10-05 13:18:28 +0400555 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700556 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
zhanghailiang35a6ed42016-10-27 14:42:52 +0800557 if (i == MIGRATION_CAPABILITY_X_COLO && !colo_supported()) {
558 continue;
559 }
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300560 if (head == NULL) {
561 head = g_malloc0(sizeof(*caps));
562 caps = head;
563 } else {
564 caps->next = g_malloc0(sizeof(*caps));
565 caps = caps->next;
566 }
567 caps->value =
568 g_malloc(sizeof(*caps->value));
569 caps->value->capability = i;
570 caps->value->state = s->enabled_capabilities[i];
571 }
572
573 return head;
574}
575
Liang Li85de8322015-03-23 16:32:28 +0800576MigrationParameters *qmp_query_migrate_parameters(Error **errp)
577{
578 MigrationParameters *params;
579 MigrationState *s = migrate_get_current();
580
581 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500582 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100583 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500584 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100585 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500586 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100587 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500588 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100589 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500590 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100591 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Eric Blakede63ab62016-09-08 22:14:15 -0500592 params->has_tls_creds = !!s->parameters.tls_creds;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100593 params->tls_creds = g_strdup(s->parameters.tls_creds);
Eric Blakede63ab62016-09-08 22:14:15 -0500594 params->has_tls_hostname = !!s->parameters.tls_hostname;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100595 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530596 params->has_max_bandwidth = true;
597 params->max_bandwidth = s->parameters.max_bandwidth;
598 params->has_downtime_limit = true;
599 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800600 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800601 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Liang Li85de8322015-03-23 16:32:28 +0800602
603 return params;
604}
605
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000606/*
607 * Return true if we're already in the middle of a migration
608 * (i.e. any of the active or setup states)
609 */
610static bool migration_is_setup_or_active(int state)
611{
612 switch (state) {
613 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000614 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000615 case MIGRATION_STATUS_SETUP:
616 return true;
617
618 default:
619 return false;
620
621 }
622}
623
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300624static void get_xbzrle_cache_stats(MigrationInfo *info)
625{
626 if (migrate_use_xbzrle()) {
627 info->has_xbzrle_cache = true;
628 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
629 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
630 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
631 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
632 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800633 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300634 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
635 }
636}
637
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100638static void populate_ram_info(MigrationInfo *info, MigrationState *s)
639{
640 info->has_ram = true;
641 info->ram = g_malloc0(sizeof(*info->ram));
642 info->ram->transferred = ram_bytes_transferred();
643 info->ram->total = ram_bytes_total();
644 info->ram->duplicate = dup_mig_pages_transferred();
Juan Quintelabedf53c2017-03-13 20:35:54 +0100645 /* legacy value. It is not used anymore */
646 info->ram->skipped = 0;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100647 info->ram->normal = norm_mig_pages_transferred();
Juan Quintela29cc3d82017-03-13 20:43:34 +0100648 info->ram->normal_bytes = norm_mig_pages_transferred() *
Juan Quintela20afaed2017-03-21 09:09:14 +0100649 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100650 info->ram->mbps = s->mbps;
Juan Quintela42d219d2017-03-14 18:01:38 +0100651 info->ram->dirty_sync_count = ram_dirty_sync_count();
Juan Quintela96506892017-03-14 18:41:03 +0100652 info->ram->postcopy_requests = ram_postcopy_requests();
Chao Fan030ce1f2017-03-21 10:22:43 +0800653 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100654
655 if (s->state != MIGRATION_STATUS_COMPLETED) {
656 info->ram->remaining = ram_bytes_remaining();
Juan Quintela47ad8612017-03-14 18:20:30 +0100657 info->ram->dirty_pages_rate = ram_dirty_pages_rate();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100658 }
659}
660
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300661MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000662{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300663 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200664 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000665
Juan Quintela17549e82011-10-05 13:50:43 +0200666 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800667 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200668 /* no migration has happened ever */
669 break;
zhanghailiang31194732015-03-13 16:08:38 +0800670 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400671 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400672 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400673 break;
zhanghailiang31194732015-03-13 16:08:38 +0800674 case MIGRATION_STATUS_ACTIVE:
675 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300676 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200677 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100678 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200679 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200680 info->has_expected_downtime = true;
681 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400682 info->has_setup_time = true;
683 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200684
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100685 populate_ram_info(info, s);
Juan Quintela8d017192012-08-13 12:31:25 +0200686
Juan Quintela17549e82011-10-05 13:50:43 +0200687 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300688 info->has_disk = true;
689 info->disk = g_malloc0(sizeof(*info->disk));
690 info->disk->transferred = blk_mig_bytes_transferred();
691 info->disk->remaining = blk_mig_bytes_remaining();
692 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000693 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300694
Jason J. Herne47828932015-09-08 13:12:36 -0400695 if (cpu_throttle_active()) {
Jason J. Herned85a31d2016-04-21 14:07:18 -0400696 info->has_cpu_throttle_percentage = true;
697 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
Jason J. Herne47828932015-09-08 13:12:36 -0400698 }
699
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300700 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200701 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000702 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
703 /* Mostly the same as active; TODO add some postcopy stats */
704 info->has_status = true;
705 info->has_total_time = true;
706 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
707 - s->total_time;
708 info->has_expected_downtime = true;
709 info->expected_downtime = s->expected_downtime;
710 info->has_setup_time = true;
711 info->setup_time = s->setup_time;
712
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100713 populate_ram_info(info, s);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000714
715 if (blk_mig_active()) {
716 info->has_disk = true;
717 info->disk = g_malloc0(sizeof(*info->disk));
718 info->disk->transferred = blk_mig_bytes_transferred();
719 info->disk->remaining = blk_mig_bytes_remaining();
720 info->disk->total = blk_mig_bytes_total();
721 }
722
723 get_xbzrle_cache_stats(info);
724 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800725 case MIGRATION_STATUS_COLO:
726 info->has_status = true;
727 /* TODO: display COLO specific information (checkpoint info etc.) */
728 break;
zhanghailiang31194732015-03-13 16:08:38 +0800729 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300730 get_xbzrle_cache_stats(info);
731
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300732 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900733 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200734 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200735 info->has_downtime = true;
736 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400737 info->has_setup_time = true;
738 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200739
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100740 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200741 break;
zhanghailiang31194732015-03-13 16:08:38 +0800742 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300743 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100744 if (s->error) {
745 info->has_error_desc = true;
746 info->error_desc = g_strdup(error_get_pretty(s->error));
747 }
Juan Quintela17549e82011-10-05 13:50:43 +0200748 break;
zhanghailiang31194732015-03-13 16:08:38 +0800749 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300750 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200751 break;
aliguori5bb79102008-10-13 03:12:02 +0000752 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800753 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300754
755 return info;
aliguori5bb79102008-10-13 03:12:02 +0000756}
757
Orit Wasserman00458432012-08-06 21:42:48 +0300758void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
759 Error **errp)
760{
761 MigrationState *s = migrate_get_current();
762 MigrationCapabilityStatusList *cap;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100763 bool old_postcopy_cap = migrate_postcopy_ram();
Orit Wasserman00458432012-08-06 21:42:48 +0300764
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000765 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100766 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300767 return;
768 }
769
770 for (cap = params; cap; cap = cap->next) {
zhanghailiang35a6ed42016-10-27 14:42:52 +0800771 if (cap->value->capability == MIGRATION_CAPABILITY_X_COLO) {
772 if (!colo_supported()) {
773 error_setg(errp, "COLO is not currently supported, please"
774 " configure with --enable-colo option in order to"
775 " support COLO feature");
776 continue;
777 }
778 }
Orit Wasserman00458432012-08-06 21:42:48 +0300779 s->enabled_capabilities[cap->value->capability] = cap->value->state;
780 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000781
782 if (migrate_postcopy_ram()) {
783 if (migrate_use_compression()) {
784 /* The decompression threads asynchronously write into RAM
785 * rather than use the atomic copies needed to avoid
786 * userfaulting. It should be possible to fix the decompression
787 * threads for compatibility in future.
788 */
789 error_report("Postcopy is not currently compatible with "
790 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000791 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000792 false;
793 }
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100794 /* This check is reasonably expensive, so only when it's being
795 * set the first time, also it's only the destination that needs
796 * special support.
797 */
798 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
799 !postcopy_ram_supported_by_host()) {
800 /* postcopy_ram_supported_by_host will have emitted a more
801 * detailed message
802 */
803 error_report("Postcopy is not supported");
804 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
805 false;
806 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000807 }
Orit Wasserman00458432012-08-06 21:42:48 +0300808}
809
Eric Blake7f375e02016-09-08 22:14:16 -0500810void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800811{
812 MigrationState *s = migrate_get_current();
813
Eric Blake7f375e02016-09-08 22:14:16 -0500814 if (params->has_compress_level &&
815 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100816 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
817 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800818 return;
819 }
Eric Blake7f375e02016-09-08 22:14:16 -0500820 if (params->has_compress_threads &&
821 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100822 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
823 "compress_threads",
824 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800825 return;
826 }
Eric Blake7f375e02016-09-08 22:14:16 -0500827 if (params->has_decompress_threads &&
828 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100829 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
830 "decompress_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_cpu_throttle_initial &&
835 (params->cpu_throttle_initial < 1 ||
836 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400837 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400838 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400839 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530840 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400841 }
Eric Blake7f375e02016-09-08 22:14:16 -0500842 if (params->has_cpu_throttle_increment &&
843 (params->cpu_throttle_increment < 1 ||
844 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400845 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400846 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400847 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530848 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400849 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530850 if (params->has_max_bandwidth &&
851 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
852 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
853 " range of 0 to %zu bytes/second", SIZE_MAX);
854 return;
855 }
856 if (params->has_downtime_limit &&
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300857 (params->downtime_limit < 0 ||
858 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
859 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
860 "the range of 0 to %d milliseconds",
861 MAX_MIGRATE_DOWNTIME);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530862 return;
863 }
zhanghailiang68b53592016-10-27 14:43:01 +0800864 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
865 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
866 "x_checkpoint_delay",
867 "is invalid, it should be positive");
868 }
Liang Li85de8322015-03-23 16:32:28 +0800869
Eric Blake7f375e02016-09-08 22:14:16 -0500870 if (params->has_compress_level) {
871 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800872 }
Eric Blake7f375e02016-09-08 22:14:16 -0500873 if (params->has_compress_threads) {
874 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800875 }
Eric Blake7f375e02016-09-08 22:14:16 -0500876 if (params->has_decompress_threads) {
877 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800878 }
Eric Blake7f375e02016-09-08 22:14:16 -0500879 if (params->has_cpu_throttle_initial) {
880 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400881 }
Eric Blake7f375e02016-09-08 22:14:16 -0500882 if (params->has_cpu_throttle_increment) {
883 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400884 }
Eric Blake7f375e02016-09-08 22:14:16 -0500885 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100886 g_free(s->parameters.tls_creds);
Eric Blake7f375e02016-09-08 22:14:16 -0500887 s->parameters.tls_creds = g_strdup(params->tls_creds);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100888 }
Eric Blake7f375e02016-09-08 22:14:16 -0500889 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100890 g_free(s->parameters.tls_hostname);
Eric Blake7f375e02016-09-08 22:14:16 -0500891 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100892 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530893 if (params->has_max_bandwidth) {
894 s->parameters.max_bandwidth = params->max_bandwidth;
895 if (s->to_dst_file) {
896 qemu_file_set_rate_limit(s->to_dst_file,
897 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
898 }
899 }
900 if (params->has_downtime_limit) {
901 s->parameters.downtime_limit = params->downtime_limit;
902 }
zhanghailiang68b53592016-10-27 14:43:01 +0800903
904 if (params->has_x_checkpoint_delay) {
905 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800906 if (migration_in_colo_state()) {
907 colo_checkpoint_notify(s);
908 }
zhanghailiang68b53592016-10-27 14:43:01 +0800909 }
Liang Li85de8322015-03-23 16:32:28 +0800910}
911
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100912
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000913void qmp_migrate_start_postcopy(Error **errp)
914{
915 MigrationState *s = migrate_get_current();
916
917 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000918 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000919 " the start of migration");
920 return;
921 }
922
923 if (s->state == MIGRATION_STATUS_NONE) {
924 error_setg(errp, "Postcopy must be started after migration has been"
925 " started");
926 return;
927 }
928 /*
929 * we don't error if migration has finished since that would be racy
930 * with issuing this command.
931 */
932 atomic_set(&s->start_postcopy, true);
933}
934
aliguori065e2812008-11-11 16:46:33 +0000935/* shared migration helpers */
936
zhanghailiang48781e52015-12-16 11:47:33 +0000937void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000938{
zhanghailiang48781e52015-12-16 11:47:33 +0000939 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200940 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200941 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000942 }
943}
944
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100945static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000946{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100947 MigrationState *s = opaque;
948
949 qemu_bh_delete(s->cleanup_bh);
950 s->cleanup_bh = NULL;
951
Juan Quintelaec481c62017-03-20 22:12:40 +0100952 migration_page_queue_free();
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000953
zhanghailiang89a02a92016-01-15 11:37:42 +0800954 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100955 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100956 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000957 if (s->migration_thread_running) {
958 qemu_thread_join(&s->thread);
959 s->migration_thread_running = false;
960 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100961 qemu_mutex_lock_iothread();
962
Liang Li8706d2d2015-03-23 16:32:17 +0800963 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800964 qemu_fclose(s->to_dst_file);
965 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000966 }
967
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000968 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
969 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100970
Liang Li94f5a432015-11-02 15:37:00 +0800971 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000972 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800973 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100974 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100975
976 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000977}
978
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100979void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000980{
Peter Maydell25174052016-10-21 18:41:45 +0100981 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +0800982 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000983 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
984 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100985 if (!s->error) {
986 s->error = error_copy(error);
987 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100988 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100989}
990
Juan Quintela0edda1c2010-05-11 16:28:39 +0200991static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000992{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000993 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800994 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100995 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000996
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000997 if (s->rp_state.from_dst_file) {
998 /* shutdown the rp socket, so causing the rp thread to shutdown */
999 qemu_file_shutdown(s->rp_state.from_dst_file);
1000 }
1001
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001002 do {
1003 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001004 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001005 break;
1006 }
zhanghailiang48781e52015-12-16 11:47:33 +00001007 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001008 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001009
1010 /*
1011 * If we're unlucky the migration code might be stuck somewhere in a
1012 * send/write while the network has failed and is waiting to timeout;
1013 * if we've got shutdown(2) available then we can force it to quit.
1014 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1015 * called in a bh, so there is no race against this cancel.
1016 */
zhanghailiang31194732015-03-13 16:08:38 +08001017 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001018 qemu_file_shutdown(f);
1019 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001020 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1021 Error *local_err = NULL;
1022
1023 bdrv_invalidate_cache_all(&local_err);
1024 if (local_err) {
1025 error_report_err(local_err);
1026 } else {
1027 s->block_inactive = false;
1028 }
1029 }
aliguori065e2812008-11-11 16:46:33 +00001030}
1031
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001032void add_migration_state_change_notifier(Notifier *notify)
1033{
1034 notifier_list_add(&migration_state_notifiers, notify);
1035}
1036
1037void remove_migration_state_change_notifier(Notifier *notify)
1038{
Paolo Bonzini31552522012-01-13 17:34:01 +01001039 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001040}
1041
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001042bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001043{
zhanghailiang31194732015-03-13 16:08:38 +08001044 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001045}
1046
Juan Quintela70736932011-02-23 00:43:59 +01001047bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001048{
zhanghailiang31194732015-03-13 16:08:38 +08001049 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001050}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001051
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001052bool migration_has_failed(MigrationState *s)
1053{
zhanghailiang31194732015-03-13 16:08:38 +08001054 return (s->state == MIGRATION_STATUS_CANCELLED ||
1055 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001056}
1057
Juan Quintela57273092017-03-20 22:25:28 +01001058bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001059{
Juan Quintela57273092017-03-20 22:25:28 +01001060 MigrationState *s = migrate_get_current();
1061
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001062 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1063}
1064
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001065bool migration_in_postcopy_after_devices(MigrationState *s)
1066{
Juan Quintela57273092017-03-20 22:25:28 +01001067 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001068}
1069
Juan Quintelafab35002017-03-22 17:36:57 +01001070bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301071{
Juan Quintelafab35002017-03-22 17:36:57 +01001072 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301073
1074 switch (s->state) {
1075 case MIGRATION_STATUS_NONE:
1076 case MIGRATION_STATUS_CANCELLED:
1077 case MIGRATION_STATUS_COMPLETED:
1078 case MIGRATION_STATUS_FAILED:
1079 return true;
1080 case MIGRATION_STATUS_SETUP:
1081 case MIGRATION_STATUS_CANCELLING:
1082 case MIGRATION_STATUS_ACTIVE:
1083 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1084 case MIGRATION_STATUS_COLO:
1085 return false;
1086 case MIGRATION_STATUS__MAX:
1087 g_assert_not_reached();
1088 }
1089
1090 return false;
1091}
1092
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +00001093MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001094{
Juan Quintela17549e82011-10-05 13:50:43 +02001095 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +03001096
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001097 /*
1098 * Reinitialise all migration state, except
1099 * parameters/capabilities that the user set, and
1100 * locks.
1101 */
1102 s->bytes_xfer = 0;
1103 s->xfer_limit = 0;
1104 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001105 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001106 s->state = MIGRATION_STATUS_NONE;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001107 s->params = *params;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001108 s->rp_state.from_dst_file = NULL;
1109 s->rp_state.error = false;
1110 s->mbps = 0.0;
1111 s->downtime = 0;
1112 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001113 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001114 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001115 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001116 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001117 error_free(s->error);
1118 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001119
zhanghailiang48781e52015-12-16 11:47:33 +00001120 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001121
Alex Blighbc72ad62013-08-21 16:03:08 +01001122 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001123 return s;
1124}
Juan Quintelacab30142011-02-22 23:54:21 +01001125
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001126static GSList *migration_blockers;
1127
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301128int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001129{
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301130 if (only_migratable) {
1131 error_propagate(errp, error_copy(reason));
1132 error_prepend(errp, "disallowing migration blocker "
1133 "(--only_migratable) for: ");
1134 return -EACCES;
1135 }
1136
Juan Quintelafab35002017-03-22 17:36:57 +01001137 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301138 migration_blockers = g_slist_prepend(migration_blockers, reason);
1139 return 0;
1140 }
1141
1142 error_propagate(errp, error_copy(reason));
1143 error_prepend(errp, "disallowing migration blocker (migration in "
1144 "progress) for: ");
1145 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001146}
1147
1148void migrate_del_blocker(Error *reason)
1149{
1150 migration_blockers = g_slist_remove(migration_blockers, reason);
1151}
1152
Ashijeet Acharya7562f902017-02-13 23:34:48 +05301153int check_migratable(Object *obj, Error **err)
1154{
1155 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1156 if (only_migratable && dc->vmsd) {
1157 if (dc->vmsd->unmigratable) {
1158 error_setg(err, "Device %s is not migratable, but "
1159 "--only-migratable was specified",
1160 object_get_typename(obj));
1161 return -1;
1162 }
1163 }
1164
1165 return 0;
1166}
1167
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001168void qmp_migrate_incoming(const char *uri, Error **errp)
1169{
1170 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001171 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001172
1173 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001174 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001175 return;
1176 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001177 if (!once) {
1178 error_setg(errp, "The incoming migration has already been started");
1179 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001180
1181 qemu_start_incoming_migration(uri, &local_err);
1182
1183 if (local_err) {
1184 error_propagate(errp, local_err);
1185 return;
1186 }
1187
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001188 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001189}
1190
Greg Kurz24f39022016-05-04 21:44:19 +02001191bool migration_is_blocked(Error **errp)
1192{
1193 if (qemu_savevm_state_blocked(errp)) {
1194 return true;
1195 }
1196
1197 if (migration_blockers) {
1198 *errp = error_copy(migration_blockers->data);
1199 return true;
1200 }
1201
1202 return false;
1203}
1204
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001205void qmp_migrate(const char *uri, bool has_blk, bool blk,
1206 bool has_inc, bool inc, bool has_detach, bool detach,
1207 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001208{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001209 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001210 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001211 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +01001212 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001213
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +09001214 params.blk = has_blk && blk;
1215 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001216
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001217 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001218 s->state == MIGRATION_STATUS_CANCELLING ||
1219 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001220 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001221 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001222 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001223 if (runstate_check(RUN_STATE_INMIGRATE)) {
1224 error_setg(errp, "Guest is waiting for an incoming migration");
1225 return;
1226 }
1227
Greg Kurz24f39022016-05-04 21:44:19 +02001228 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001229 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001230 }
1231
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001232 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +01001233
1234 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001235 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001236#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001237 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001238 rdma_start_outgoing_migration(s, p, &local_err);
1239#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001240 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001241 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001242 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001243 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001244 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001245 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001246 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001247 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1248 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001249 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1250 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001251 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001252 }
1253
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001254 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001255 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001256 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001257 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001258 }
Juan Quintelacab30142011-02-22 23:54:21 +01001259}
1260
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001261void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001262{
Juan Quintela17549e82011-10-05 13:50:43 +02001263 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001264}
1265
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001266void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1267{
1268 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001269 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001270
1271 /* Check for truncation */
1272 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001273 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1274 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001275 return;
1276 }
1277
Orit Wassermana5615b12014-01-30 20:08:36 +02001278 /* Cache should not be larger than guest ram size */
1279 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001280 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1281 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001282 return;
1283 }
1284
Orit Wassermanc91e6812014-01-30 20:08:34 +02001285 new_size = xbzrle_cache_resize(value);
1286 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001287 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1288 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001289 return;
1290 }
1291
1292 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001293}
1294
1295int64_t qmp_query_migrate_cache_size(Error **errp)
1296{
1297 return migrate_xbzrle_cache_size();
1298}
1299
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001300void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001301{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301302 MigrationParameters p = {
1303 .has_max_bandwidth = true,
1304 .max_bandwidth = value,
1305 };
Juan Quintelacab30142011-02-22 23:54:21 +01001306
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301307 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001308}
1309
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001310void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001311{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001312 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1313 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1314 "the range of 0 to %d seconds",
1315 MAX_MIGRATE_DOWNTIME_SECONDS);
1316 return;
1317 }
1318
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301319 value *= 1000; /* Convert to milliseconds */
1320 value = MAX(0, MIN(INT64_MAX, value));
1321
1322 MigrationParameters p = {
1323 .has_downtime_limit = true,
1324 .downtime_limit = value,
1325 };
1326
1327 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001328}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001329
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001330bool migrate_release_ram(void)
1331{
1332 MigrationState *s;
1333
1334 s = migrate_get_current();
1335
1336 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1337}
1338
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001339bool migrate_postcopy_ram(void)
1340{
1341 MigrationState *s;
1342
1343 s = migrate_get_current();
1344
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001345 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001346}
1347
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001348bool migrate_auto_converge(void)
1349{
1350 MigrationState *s;
1351
1352 s = migrate_get_current();
1353
1354 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1355}
1356
Peter Lieven323004a2013-07-18 09:48:50 +02001357bool migrate_zero_blocks(void)
1358{
1359 MigrationState *s;
1360
1361 s = migrate_get_current();
1362
1363 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1364}
1365
Liang Li8706d2d2015-03-23 16:32:17 +08001366bool migrate_use_compression(void)
1367{
Liang Lidde4e692015-03-23 16:32:26 +08001368 MigrationState *s;
1369
1370 s = migrate_get_current();
1371
1372 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001373}
1374
1375int migrate_compress_level(void)
1376{
1377 MigrationState *s;
1378
1379 s = migrate_get_current();
1380
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001381 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001382}
1383
1384int migrate_compress_threads(void)
1385{
1386 MigrationState *s;
1387
1388 s = migrate_get_current();
1389
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001390 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001391}
1392
Liang Li3fcb38c2015-03-23 16:32:18 +08001393int migrate_decompress_threads(void)
1394{
1395 MigrationState *s;
1396
1397 s = migrate_get_current();
1398
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001399 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001400}
1401
Juan Quintelab05dc722015-07-07 14:44:05 +02001402bool migrate_use_events(void)
1403{
1404 MigrationState *s;
1405
1406 s = migrate_get_current();
1407
1408 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1409}
1410
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001411int migrate_use_xbzrle(void)
1412{
1413 MigrationState *s;
1414
1415 s = migrate_get_current();
1416
1417 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1418}
1419
1420int64_t migrate_xbzrle_cache_size(void)
1421{
1422 MigrationState *s;
1423
1424 s = migrate_get_current();
1425
1426 return s->xbzrle_cache_size;
1427}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001428
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001429/* migration thread support */
1430/*
1431 * Something bad happened to the RP stream, mark an error
1432 * The caller shall print or trace something to indicate why
1433 */
1434static void mark_source_rp_bad(MigrationState *s)
1435{
1436 s->rp_state.error = true;
1437}
1438
1439static struct rp_cmd_args {
1440 ssize_t len; /* -1 = variable */
1441 const char *name;
1442} rp_cmd_args[] = {
1443 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1444 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1445 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001446 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1447 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001448 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1449};
1450
1451/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001452 * Process a request for pages received on the return path,
1453 * We're allowed to send more than requested (e.g. to round to our page size)
1454 * and we don't need to send pages that have already been sent.
1455 */
1456static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1457 ram_addr_t start, size_t len)
1458{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001459 long our_host_ps = getpagesize();
1460
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001461 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001462
1463 /*
1464 * Since we currently insist on matching page sizes, just sanity check
1465 * we're being asked for whole host pages.
1466 */
1467 if (start & (our_host_ps-1) ||
1468 (len & (our_host_ps-1))) {
1469 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1470 " len: %zd", __func__, start, len);
1471 mark_source_rp_bad(ms);
1472 return;
1473 }
1474
Juan Quintela96506892017-03-14 18:41:03 +01001475 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001476 mark_source_rp_bad(ms);
1477 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001478}
1479
1480/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001481 * Handles messages sent on the return path towards the source VM
1482 *
1483 */
1484static void *source_return_path_thread(void *opaque)
1485{
1486 MigrationState *ms = opaque;
1487 QEMUFile *rp = ms->rp_state.from_dst_file;
1488 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001489 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001490 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001491 ram_addr_t start = 0; /* =0 to silence warning */
1492 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001493 int res;
1494
1495 trace_source_return_path_thread_entry();
1496 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1497 migration_is_setup_or_active(ms->state)) {
1498 trace_source_return_path_thread_loop_top();
1499 header_type = qemu_get_be16(rp);
1500 header_len = qemu_get_be16(rp);
1501
1502 if (header_type >= MIG_RP_MSG_MAX ||
1503 header_type == MIG_RP_MSG_INVALID) {
1504 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1505 header_type, header_len);
1506 mark_source_rp_bad(ms);
1507 goto out;
1508 }
1509
1510 if ((rp_cmd_args[header_type].len != -1 &&
1511 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001512 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001513 error_report("RP: Received '%s' message (0x%04x) with"
1514 "incorrect length %d expecting %zu",
1515 rp_cmd_args[header_type].name, header_type, header_len,
1516 (size_t)rp_cmd_args[header_type].len);
1517 mark_source_rp_bad(ms);
1518 goto out;
1519 }
1520
1521 /* We know we've got a valid header by this point */
1522 res = qemu_get_buffer(rp, buf, header_len);
1523 if (res != header_len) {
1524 error_report("RP: Failed reading data for message 0x%04x"
1525 " read %d expected %d",
1526 header_type, res, header_len);
1527 mark_source_rp_bad(ms);
1528 goto out;
1529 }
1530
1531 /* OK, we have the message and the data */
1532 switch (header_type) {
1533 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001534 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001535 trace_source_return_path_thread_shut(sibling_error);
1536 if (sibling_error) {
1537 error_report("RP: Sibling indicated error %d", sibling_error);
1538 mark_source_rp_bad(ms);
1539 }
1540 /*
1541 * We'll let the main thread deal with closing the RP
1542 * we could do a shutdown(2) on it, but we're the only user
1543 * anyway, so there's nothing gained.
1544 */
1545 goto out;
1546
1547 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001548 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001549 trace_source_return_path_thread_pong(tmp32);
1550 break;
1551
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001552 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001553 start = ldq_be_p(buf);
1554 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001555 migrate_handle_rp_req_pages(ms, NULL, start, len);
1556 break;
1557
1558 case MIG_RP_MSG_REQ_PAGES_ID:
1559 expected_len = 12 + 1; /* header + termination */
1560
1561 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001562 start = ldq_be_p(buf);
1563 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001564 /* Now we expect an idstr */
1565 tmp32 = buf[12]; /* Length of the following idstr */
1566 buf[13 + tmp32] = '\0';
1567 expected_len += tmp32;
1568 }
1569 if (header_len != expected_len) {
1570 error_report("RP: Req_Page_id with length %d expecting %zd",
1571 header_len, expected_len);
1572 mark_source_rp_bad(ms);
1573 goto out;
1574 }
1575 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1576 break;
1577
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001578 default:
1579 break;
1580 }
1581 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001582 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001583 trace_source_return_path_thread_bad_end();
1584 mark_source_rp_bad(ms);
1585 }
1586
1587 trace_source_return_path_thread_end();
1588out:
1589 ms->rp_state.from_dst_file = NULL;
1590 qemu_fclose(rp);
1591 return NULL;
1592}
1593
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001594static int open_return_path_on_source(MigrationState *ms)
1595{
1596
zhanghailiang89a02a92016-01-15 11:37:42 +08001597 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001598 if (!ms->rp_state.from_dst_file) {
1599 return -1;
1600 }
1601
1602 trace_open_return_path_on_source();
1603 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1604 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1605
1606 trace_open_return_path_on_source_continue();
1607
1608 return 0;
1609}
1610
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001611/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1612static int await_return_path_close_on_source(MigrationState *ms)
1613{
1614 /*
1615 * If this is a normal exit then the destination will send a SHUT and the
1616 * rp_thread will exit, however if there's an error we need to cause
1617 * it to exit.
1618 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001619 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001620 /*
1621 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1622 * waiting for the destination.
1623 */
1624 qemu_file_shutdown(ms->rp_state.from_dst_file);
1625 mark_source_rp_bad(ms);
1626 }
1627 trace_await_return_path_close_on_source_joining();
1628 qemu_thread_join(&ms->rp_state.rp_thread);
1629 trace_await_return_path_close_on_source_close();
1630 return ms->rp_state.error;
1631}
1632
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001633/*
1634 * Switch from normal iteration to postcopy
1635 * Returns non-0 on error
1636 */
1637static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1638{
1639 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001640 QIOChannelBuffer *bioc;
1641 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001642 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001643 bool restart_block = false;
zhanghailiang48781e52015-12-16 11:47:33 +00001644 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001645 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1646
1647 trace_postcopy_start();
1648 qemu_mutex_lock_iothread();
1649 trace_postcopy_start_set_run();
1650
1651 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1652 *old_vm_running = runstate_is_running();
1653 global_state_store();
1654 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001655 if (ret < 0) {
1656 goto fail;
1657 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001658
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001659 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001660 if (ret < 0) {
1661 goto fail;
1662 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001663 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001664
1665 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001666 * Cause any non-postcopiable, but iterative devices to
1667 * send out their final data.
1668 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001669 qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001670
1671 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001672 * in Finish migrate and with the io-lock held everything should
1673 * be quiet, but we've potentially still got dirty pages and we
1674 * need to tell the destination to throw any pages it's already received
1675 * that are dirty
1676 */
1677 if (ram_postcopy_send_discard_bitmap(ms)) {
1678 error_report("postcopy send discard bitmap failed");
1679 goto fail;
1680 }
1681
1682 /*
1683 * send rest of state - note things that are doing postcopy
1684 * will notice we're in POSTCOPY_ACTIVE and not actually
1685 * wrap their state up here
1686 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001687 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001688 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001689 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001690
1691 /*
1692 * While loading the device state we may trigger page transfer
1693 * requests and the fd must be free to process those, and thus
1694 * the destination must read the whole device state off the fd before
1695 * it starts processing it. Unfortunately the ad-hoc migration format
1696 * doesn't allow the destination to know the size to read without fully
1697 * parsing it through each devices load-state code (especially the open
1698 * coded devices that use get/put).
1699 * So we wrap the device state up in a package with a length at the start;
1700 * to do this we use a qemu_buf to hold the whole of the device state.
1701 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001702 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001703 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001704 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1705 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001706
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001707 /*
1708 * Make sure the receiver can get incoming pages before we send the rest
1709 * of the state
1710 */
1711 qemu_savevm_send_postcopy_listen(fb);
1712
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001713 qemu_savevm_state_complete_precopy(fb, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001714 qemu_savevm_send_ping(fb, 3);
1715
1716 qemu_savevm_send_postcopy_run(fb);
1717
1718 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001719
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001720 /* Last point of recovery; as soon as we send the package the destination
1721 * can open devices and potentially start running.
1722 * Lets just check again we've not got any errors.
1723 */
1724 ret = qemu_file_get_error(ms->to_dst_file);
1725 if (ret) {
1726 error_report("postcopy_start: Migration stream errored (pre package)");
1727 goto fail_closefb;
1728 }
1729
1730 restart_block = false;
1731
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001732 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001733 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001734 goto fail_closefb;
1735 }
1736 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001737
1738 /* Send a notify to give a chance for anything that needs to happen
1739 * at the transition to postcopy and after the device state; in particular
1740 * spice needs to trigger a transition now
1741 */
1742 ms->postcopy_after_devices = true;
1743 notifier_list_notify(&migration_state_notifiers, ms);
1744
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001745 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1746
1747 qemu_mutex_unlock_iothread();
1748
1749 /*
1750 * Although this ping is just for debug, it could potentially be
1751 * used for getting a better measurement of downtime at the source.
1752 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001753 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001754
Pavel Butsykinced1c612017-02-03 18:23:21 +03001755 if (migrate_release_ram()) {
1756 ram_postcopy_migrated_memory_release(ms);
1757 }
1758
zhanghailiang89a02a92016-01-15 11:37:42 +08001759 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001760 if (ret) {
1761 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001762 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001763 MIGRATION_STATUS_FAILED);
1764 }
1765
1766 return ret;
1767
1768fail_closefb:
1769 qemu_fclose(fb);
1770fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001771 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001772 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001773 if (restart_block) {
1774 /* A failure happened early enough that we know the destination hasn't
1775 * accessed block devices, so we're safe to recover.
1776 */
1777 Error *local_err = NULL;
1778
1779 bdrv_invalidate_cache_all(&local_err);
1780 if (local_err) {
1781 error_report_err(local_err);
1782 }
1783 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001784 qemu_mutex_unlock_iothread();
1785 return -1;
1786}
1787
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001788/**
1789 * migration_completion: Used by migration_thread when there's not much left.
1790 * The caller 'breaks' the loop when this returns.
1791 *
1792 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001793 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001794 * @*old_vm_running: Pointer to old_vm_running flag
1795 * @*start_time: Pointer to time to update
1796 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001797static void migration_completion(MigrationState *s, int current_active_state,
1798 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001799 int64_t *start_time)
1800{
1801 int ret;
1802
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001803 if (s->state == MIGRATION_STATUS_ACTIVE) {
1804 qemu_mutex_lock_iothread();
1805 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1806 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1807 *old_vm_running = runstate_is_running();
1808 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001809
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001810 if (!ret) {
1811 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
zhanghailiang0b827d52016-10-27 14:42:54 +08001812 /*
1813 * Don't mark the image with BDRV_O_INACTIVE flag if
1814 * we will go into COLO stage later.
1815 */
1816 if (ret >= 0 && !migrate_colo_enabled()) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001817 ret = bdrv_inactivate_all();
1818 }
1819 if (ret >= 0) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001820 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1821 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001822 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001823 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001824 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001825 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001826
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001827 if (ret < 0) {
1828 goto fail;
1829 }
1830 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1831 trace_migration_completion_postcopy_end();
1832
zhanghailiang89a02a92016-01-15 11:37:42 +08001833 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001834 trace_migration_completion_postcopy_end_after_complete();
1835 }
1836
1837 /*
1838 * If rp was opened we must clean up the thread before
1839 * cleaning everything else up (since if there are no failures
1840 * it will wait for the destination to send it's status in
1841 * a SHUT command).
1842 * Postcopy opens rp if enabled (even if it's not avtivated)
1843 */
1844 if (migrate_postcopy_ram()) {
1845 int rp_error;
1846 trace_migration_completion_postcopy_end_before_rp();
1847 rp_error = await_return_path_close_on_source(s);
1848 trace_migration_completion_postcopy_end_after_rp(rp_error);
1849 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001850 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001851 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001852 }
1853
zhanghailiang89a02a92016-01-15 11:37:42 +08001854 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001855 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001856 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001857 }
1858
zhanghailiang0b827d52016-10-27 14:42:54 +08001859 if (!migrate_colo_enabled()) {
1860 migrate_set_state(&s->state, current_active_state,
1861 MIGRATION_STATUS_COMPLETED);
1862 }
1863
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001864 return;
1865
Greg Kurzfe904ea2016-05-18 15:44:36 +02001866fail_invalidate:
1867 /* If not doing postcopy, vm_start() will be called: let's regain
1868 * control on images.
1869 */
1870 if (s->state == MIGRATION_STATUS_ACTIVE) {
1871 Error *local_err = NULL;
1872
zhanghailiang1d2acc32017-01-24 15:59:52 +08001873 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001874 bdrv_invalidate_cache_all(&local_err);
1875 if (local_err) {
1876 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001877 } else {
1878 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02001879 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001880 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001881 }
1882
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001883fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001884 migrate_set_state(&s->state, current_active_state,
1885 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001886}
1887
zhanghailiang35a6ed42016-10-27 14:42:52 +08001888bool migrate_colo_enabled(void)
1889{
1890 MigrationState *s = migrate_get_current();
1891 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
1892}
1893
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001894/*
1895 * Master migration thread on the source VM.
1896 * It drives the migration and pumps the data down the outgoing channel.
1897 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001898static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001899{
Juan Quintela9848a402012-12-19 09:55:50 +01001900 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001901 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001902 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1903 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001904 int64_t initial_bytes = 0;
Peter Xufaec0662017-04-01 16:18:43 +08001905 /*
1906 * The final stage happens when the remaining data is smaller than
1907 * this threshold; it's calculated from the requested downtime and
1908 * measured bandwidth
1909 */
1910 int64_t threshold_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001911 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001912 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001913 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001914 bool entered_postcopy = false;
1915 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1916 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang0b827d52016-10-27 14:42:54 +08001917 bool enable_colo = migrate_colo_enabled();
Juan Quintela76f59332012-10-03 20:16:24 +02001918
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001919 rcu_register_thread();
1920
zhanghailiang89a02a92016-01-15 11:37:42 +08001921 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001922
1923 if (migrate_postcopy_ram()) {
1924 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001925 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001926
1927 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001928 qemu_savevm_send_ping(s->to_dst_file, 1);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001929
1930 /*
1931 * Tell the destination that we *might* want to do postcopy later;
1932 * if the other end can't do postcopy it should fail now, nice and
1933 * early.
1934 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001935 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001936 }
1937
zhanghailiang89a02a92016-01-15 11:37:42 +08001938 qemu_savevm_state_begin(s->to_dst_file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001939
Alex Blighbc72ad62013-08-21 16:03:08 +01001940 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00001941 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1942 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001943
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001944 trace_migration_thread_setup_complete();
1945
1946 while (s->state == MIGRATION_STATUS_ACTIVE ||
1947 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001948 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001949 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001950
zhanghailiang89a02a92016-01-15 11:37:42 +08001951 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001952 uint64_t pend_post, pend_nonpost;
1953
Peter Xufaec0662017-04-01 16:18:43 +08001954 qemu_savevm_state_pending(s->to_dst_file, threshold_size,
1955 &pend_nonpost, &pend_post);
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001956 pending_size = pend_nonpost + pend_post;
Peter Xufaec0662017-04-01 16:18:43 +08001957 trace_migrate_pending(pending_size, threshold_size,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001958 pend_post, pend_nonpost);
Peter Xufaec0662017-04-01 16:18:43 +08001959 if (pending_size && pending_size >= threshold_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001960 /* Still a significant amount to transfer */
1961
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001962 if (migrate_postcopy_ram() &&
1963 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
Peter Xufaec0662017-04-01 16:18:43 +08001964 pend_nonpost <= threshold_size &&
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001965 atomic_read(&s->start_postcopy)) {
1966
1967 if (!postcopy_start(s, &old_vm_running)) {
1968 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1969 entered_postcopy = true;
1970 }
1971
1972 continue;
1973 }
1974 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001975 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001976 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001977 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001978 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001979 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001980 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001981 }
1982 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001983
zhanghailiang89a02a92016-01-15 11:37:42 +08001984 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001985 migrate_set_state(&s->state, current_active_state,
1986 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001987 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001988 break;
1989 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001990 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001991 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001992 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1993 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001994 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001995 double bandwidth = (double)transferred_bytes / time_spent;
Peter Xufaec0662017-04-01 16:18:43 +08001996 threshold_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001997
Wei Yang5b648de2016-01-24 14:09:57 +00001998 s->mbps = (((double) transferred_bytes * 8.0) /
1999 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04002000
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11002001 trace_migrate_transferred(transferred_bytes, time_spent,
Peter Xufaec0662017-04-01 16:18:43 +08002002 bandwidth, threshold_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01002003 /* if we haven't sent anything, we don't want to recalculate
2004 10000 is a small enough number for our purposes */
Juan Quintela47ad8612017-03-14 18:20:30 +01002005 if (ram_dirty_pages_rate() && transferred_bytes > 10000) {
2006 s->expected_downtime = ram_dirty_pages_rate() *
Juan Quintela20afaed2017-03-21 09:09:14 +01002007 qemu_target_page_size() / bandwidth;
Juan Quintela90f8ae72013-02-01 13:22:37 +01002008 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002009
zhanghailiang89a02a92016-01-15 11:37:42 +08002010 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002011 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08002012 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002013 }
zhanghailiang89a02a92016-01-15 11:37:42 +08002014 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002015 /* usleep expects microseconds */
2016 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
2017 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002018 }
2019
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002020 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04002021 /* If we enabled cpu throttling for auto-converge, turn it off. */
2022 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08002023 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04002024
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002025 qemu_mutex_lock_iothread();
zhanghailiang0b827d52016-10-27 14:42:54 +08002026 /*
2027 * The resource has been allocated by migration will be reused in COLO
2028 * process, so don't release them.
2029 */
2030 if (!enable_colo) {
2031 qemu_savevm_state_cleanup();
2032 }
zhanghailiang31194732015-03-13 16:08:38 +08002033 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08002034 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002035 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002036 if (!entered_postcopy) {
2037 s->downtime = end_time - start_time;
2038 }
Peter Lievend6ed7312014-05-12 10:46:00 +02002039 if (s->total_time) {
2040 s->mbps = (((double) transferred_bytes * 8.0) /
2041 ((double) s->total_time)) / 1000;
2042 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002043 runstate_set(RUN_STATE_POSTMIGRATE);
2044 } else {
zhanghailiang0b827d52016-10-27 14:42:54 +08002045 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
2046 migrate_start_colo_process(s);
2047 qemu_savevm_state_cleanup();
2048 /*
2049 * Fixme: we will run VM in COLO no matter its old running state.
2050 * After exited COLO, we will keep running.
2051 */
2052 old_vm_running = true;
2053 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002054 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002055 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01002056 } else {
2057 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2058 runstate_set(RUN_STATE_POSTMIGRATE);
2059 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01002060 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002061 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002062 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01002063 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002064
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002065 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002066 return NULL;
2067}
2068
Juan Quintela9848a402012-12-19 09:55:50 +01002069void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002070{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302071 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002072 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002073
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01002074 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08002075 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302076 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002077
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002078 /* Notify before starting migration thread */
2079 notifier_list_notify(&migration_state_notifiers, s);
2080
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002081 /*
2082 * Open the return path; currently for postcopy but other things might
2083 * also want it.
2084 */
2085 if (migrate_postcopy_ram()) {
2086 if (open_return_path_on_source(s)) {
2087 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00002088 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002089 MIGRATION_STATUS_FAILED);
2090 migrate_fd_cleanup(s);
2091 return;
2092 }
2093 }
2094
Liang Li8706d2d2015-03-23 16:32:17 +08002095 migrate_compress_threads_create();
Pankaj Gupta009fad72017-01-23 19:12:56 +05302096 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002097 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002098 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002099}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002100
2101PostcopyState postcopy_state_get(void)
2102{
2103 return atomic_mb_read(&incoming_postcopy_state);
2104}
2105
2106/* Set the state and return the old state */
2107PostcopyState postcopy_state_set(PostcopyState new_state)
2108{
2109 return atomic_xchg(&incoming_postcopy_state, new_state);
2110}
2111