blob: b9d8798ed164a3734cda12fc3d45495a41ed0386 [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"
Juan Quintela795c40b2017-04-06 12:00:28 +020019#include "migration/blocker.h"
Juan Quintelaf4dbe1b2017-04-05 15:54:10 +020020#include "exec.h"
Juan Quintela7fcac4a2017-04-05 15:58:29 +020021#include "fd.h"
Juan Quintela61e8b142017-04-05 17:40:11 +020022#include "socket.h"
Juan Quintelae1a3ece2017-04-17 20:32:36 +020023#include "rdma.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020024#include "ram.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020025#include "migration/global_state.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020026#include "migration/misc.h"
Juan Quintela6666c962017-04-24 20:07:27 +020027#include "migration.h"
Juan Quintela20a519a2017-04-20 14:48:46 +020028#include "savevm.h"
Juan Quintela40014d82017-04-17 19:34:36 +020029#include "qemu-file-channel.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020030#include "qemu-file.h"
Juan Quintela987772d2017-04-17 19:02:59 +020031#include "migration/vmstate.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010032#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010033#include "qapi/qmp/qerror.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000034#include "qapi/util.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020035#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020036#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020037#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020038#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030039#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010040#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020041#include "qapi-event.h"
Juan Quintela51180422017-04-24 20:50:19 +020042#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010043#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080044#include "migration/colo.h"
aliguori065e2812008-11-11 16:46:33 +000045
Jason J. Hernedc325622015-09-08 13:12:37 -040046#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000047
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010048/* Amount of time to allocate to each "chunk" of bandwidth-throttled
49 * data. */
50#define BUFFER_DELAY 100
51#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
52
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053053/* Time in milliseconds we are allowed to stop the source,
54 * for sending the last part */
55#define DEFAULT_MIGRATE_SET_DOWNTIME 300
56
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030057/* Maximum migrate downtime set to 2000 seconds */
58#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
59#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
60
Liang Li8706d2d2015-03-23 16:32:17 +080061/* Default compression thread count */
62#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080063/* Default decompression thread count, usually decompression is at
64 * least 4 times as fast as compression.*/
65#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080066/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
67#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040068/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040069#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
70#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080071
Orit Wasserman17ad9b32012-08-06 21:42:53 +030072/* Migration XBZRLE default cache size */
73#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
74
zhanghailiang68b53592016-10-27 14:43:01 +080075/* The delay time (in ms) between two COLO checkpoints
76 * Note: Please change this default value to 10000 when we support hybrid mode.
77 */
78#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
79
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010080static NotifierList migration_state_notifiers =
81 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
82
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000083static bool deferred_incoming;
84
Juan Quintelada6f1792017-04-24 17:37:14 +020085/* Messages sent on the return path from destination to source */
86enum mig_rp_message_type {
87 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
88 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
89 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
90
91 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
92 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
93
94 MIG_RP_MSG_MAX
95};
96
Juan Quintela17549e82011-10-05 13:50:43 +020097/* When we add fault tolerance, we could have several
98 migrations at once. For now we don't need to add
99 dynamic creation of migration */
100
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100101/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200102MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200103{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000104 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +0200105 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +0800106 .state = MIGRATION_STATUS_NONE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300107 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -0400108 .mbps = -1,
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100109 .parameters = {
110 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
111 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
112 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
113 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
114 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530115 .max_bandwidth = MAX_THROTTLE,
116 .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME,
zhanghailiang68b53592016-10-27 14:43:01 +0800117 .x_checkpoint_delay = DEFAULT_MIGRATE_X_CHECKPOINT_DELAY,
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100118 },
Juan Quintela17549e82011-10-05 13:50:43 +0200119 };
120
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000121 if (!once) {
Daniel P. Berrange4af245d2017-03-15 16:16:03 +0000122 current_migration.parameters.tls_creds = g_strdup("");
123 current_migration.parameters.tls_hostname = g_strdup("");
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000124 once = true;
125 }
Juan Quintela17549e82011-10-05 13:50:43 +0200126 return &current_migration;
127}
128
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100129MigrationIncomingState *migration_incoming_get_current(void)
130{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100131 static bool once;
132 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100133
Juan Quintelab4b076d2017-01-23 22:32:06 +0100134 if (!once) {
135 mis_current.state = MIGRATION_STATUS_NONE;
136 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100137 qemu_mutex_init(&mis_current.rp_mutex);
138 qemu_event_init(&mis_current.main_thread_load_event, false);
139 once = true;
140 }
141 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100142}
143
144void migration_incoming_state_destroy(void)
145{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100146 struct MigrationIncomingState *mis = migration_incoming_get_current();
147
Peter Xu34826552017-05-19 14:43:29 +0800148 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800149 /* Tell source that we are done */
150 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800151 qemu_fclose(mis->to_src_file);
152 mis->to_src_file = NULL;
153 }
154
Peter Xu660819b2017-05-19 14:43:30 +0800155 if (mis->from_src_file) {
156 qemu_fclose(mis->from_src_file);
157 mis->from_src_file = NULL;
158 }
159
Juan Quintelab4b076d2017-01-23 22:32:06 +0100160 qemu_event_destroy(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100161}
162
Juan Quintelab05dc722015-07-07 14:44:05 +0200163static void migrate_generate_event(int new_state)
164{
165 if (migrate_use_events()) {
166 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200167 }
168}
169
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000170/*
171 * Called on -incoming with a defer: uri.
172 * The migration can be started later after any parameters have been
173 * changed.
174 */
175static void deferred_incoming_migration(Error **errp)
176{
177 if (deferred_incoming) {
178 error_setg(errp, "Incoming migration already deferred");
179 }
180 deferred_incoming = true;
181}
182
Juan Quintelada6f1792017-04-24 17:37:14 +0200183/*
184 * Send a message on the return channel back to the source
185 * of the migration.
186 */
187static void migrate_send_rp_message(MigrationIncomingState *mis,
188 enum mig_rp_message_type message_type,
189 uint16_t len, void *data)
190{
191 trace_migrate_send_rp_message((int)message_type, len);
192 qemu_mutex_lock(&mis->rp_mutex);
193 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
194 qemu_put_be16(mis->to_src_file, len);
195 qemu_put_buffer(mis->to_src_file, data, len);
196 qemu_fflush(mis->to_src_file);
197 qemu_mutex_unlock(&mis->rp_mutex);
198}
199
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000200/* Request a range of pages from the source VM at the given
201 * start address.
202 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
203 * as the last request (a name must have been given previously)
204 * Start: Address offset within the RB
205 * Len: Length in bytes required - must be a multiple of pagesize
206 */
207void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
208 ram_addr_t start, size_t len)
209{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100210 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000211 size_t msglen = 12; /* start + len */
212
213 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
214 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
215
216 if (rbname) {
217 int rbname_len = strlen(rbname);
218 assert(rbname_len < 256);
219
220 bufc[msglen++] = rbname_len;
221 memcpy(bufc + msglen, rbname, rbname_len);
222 msglen += rbname_len;
223 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
224 } else {
225 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
226 }
227}
228
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200229void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000230{
aliguori34c9dd82008-10-13 03:14:31 +0000231 const char *p;
232
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200233 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000234 if (!strcmp(uri, "defer")) {
235 deferred_incoming_migration(errp);
236 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200237 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400238#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000239 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400240 rdma_start_incoming_migration(p, errp);
241#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000242 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200243 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000244 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200245 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000246 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200247 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000248 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100249 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200250 }
aliguori5bb79102008-10-13 03:12:02 +0000251}
252
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300253static void process_incoming_migration_bh(void *opaque)
254{
255 Error *local_err = NULL;
256 MigrationIncomingState *mis = opaque;
257
Kevin Wolface21a52017-05-04 18:52:36 +0200258 /* Make sure all file formats flush their mutable metadata.
259 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300260 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200261 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200262 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200263 local_err = NULL;
264 autostart = false;
265 }
266
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300267 /*
268 * This must happen after all error conditions are dealt with and
269 * we're sure the VM is going to be running on this host.
270 */
271 qemu_announce_self();
272
273 /* If global state section was not received or we are in running
274 state, we need to obey autostart. Any other state is set with
275 runstate_set. */
276
277 if (!global_state_received() ||
278 global_state_get_runstate() == RUN_STATE_RUNNING) {
279 if (autostart) {
280 vm_start();
281 } else {
282 runstate_set(RUN_STATE_PAUSED);
283 }
284 } else {
285 runstate_set(global_state_get_runstate());
286 }
287 migrate_decompress_threads_join();
288 /*
289 * This must happen after any state changes since as soon as an external
290 * observer sees this event they might start to prod at the VM assuming
291 * it's ready to use.
292 */
293 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
294 MIGRATION_STATUS_COMPLETED);
295 qemu_bh_delete(mis->bh);
296 migration_incoming_state_destroy();
297}
298
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200299static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200300{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200301 QEMUFile *f = opaque;
Juan Quintelab4b076d2017-01-23 22:32:06 +0100302 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000303 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200304 int ret;
305
Juan Quintelab4b076d2017-01-23 22:32:06 +0100306 mis->from_src_file = f;
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000307 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000308 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000309 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
310 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200311 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100312
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000313 ps = postcopy_state_get();
314 trace_process_incoming_migration_co_end(ret, ps);
315 if (ps != POSTCOPY_INCOMING_NONE) {
316 if (ps == POSTCOPY_INCOMING_ADVISE) {
317 /*
318 * Where a migration had postcopy enabled (and thus went to advise)
319 * but managed to complete within the precopy period, we can use
320 * the normal exit.
321 */
322 postcopy_ram_incoming_cleanup(mis);
323 } else if (ret >= 0) {
324 /*
325 * Postcopy was started, cleanup should happen at the end of the
326 * postcopy thread.
327 */
328 trace_process_incoming_migration_co_postcopy_end_main();
329 return;
330 }
331 /* Else if something went wrong then just fall out of the normal exit */
332 }
333
zhanghailiang25d0c162016-10-27 14:42:55 +0800334 /* we get COLO info, and know if we are in COLO mode */
335 if (!ret && migration_incoming_enable_colo()) {
336 mis->migration_incoming_co = qemu_coroutine_self();
337 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
338 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
339 mis->have_colo_incoming_thread = true;
340 qemu_coroutine_yield();
341
342 /* Wait checkpoint incoming thread exit before free resource */
343 qemu_thread_join(&mis->colo_incoming_thread);
344 }
345
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200346 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000347 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
348 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200349 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800350 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600351 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200352 }
Juan Quintela511c0232010-06-09 14:10:55 +0200353
Laurent Viviere8199e42017-04-12 15:53:11 +0200354 free_xbzrle_decoded_buf();
355
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300356 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
357 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200358}
359
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100360void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200361{
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200362 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200363
Liang Li3fcb38c2015-03-23 16:32:18 +0800364 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100365 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200366 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200367}
368
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000369/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000370 * Send a 'SHUT' message on the return channel with the given value
371 * to indicate that we've finished with the RP. Non-0 value indicates
372 * error.
373 */
374void migrate_send_rp_shut(MigrationIncomingState *mis,
375 uint32_t value)
376{
377 uint32_t buf;
378
379 buf = cpu_to_be32(value);
380 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
381}
382
383/*
384 * Send a 'PONG' message on the return channel with the given value
385 * (normally in response to a 'PING')
386 */
387void migrate_send_rp_pong(MigrationIncomingState *mis,
388 uint32_t value)
389{
390 uint32_t buf;
391
392 buf = cpu_to_be32(value);
393 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
394}
395
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300396MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
397{
398 MigrationCapabilityStatusList *head = NULL;
399 MigrationCapabilityStatusList *caps;
400 MigrationState *s = migrate_get_current();
401 int i;
402
Michael Tokarev387eede2013-10-05 13:18:28 +0400403 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700404 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100405#ifndef CONFIG_LIVE_BLOCK_MIGRATION
406 if (i == MIGRATION_CAPABILITY_BLOCK) {
407 continue;
408 }
409#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800410 if (i == MIGRATION_CAPABILITY_X_COLO && !colo_supported()) {
411 continue;
412 }
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300413 if (head == NULL) {
414 head = g_malloc0(sizeof(*caps));
415 caps = head;
416 } else {
417 caps->next = g_malloc0(sizeof(*caps));
418 caps = caps->next;
419 }
420 caps->value =
421 g_malloc(sizeof(*caps->value));
422 caps->value->capability = i;
423 caps->value->state = s->enabled_capabilities[i];
424 }
425
426 return head;
427}
428
Liang Li85de8322015-03-23 16:32:28 +0800429MigrationParameters *qmp_query_migrate_parameters(Error **errp)
430{
431 MigrationParameters *params;
432 MigrationState *s = migrate_get_current();
433
434 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500435 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100436 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500437 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100438 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500439 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100440 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500441 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100442 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500443 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100444 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Eric Blakede63ab62016-09-08 22:14:15 -0500445 params->has_tls_creds = !!s->parameters.tls_creds;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100446 params->tls_creds = g_strdup(s->parameters.tls_creds);
Eric Blakede63ab62016-09-08 22:14:15 -0500447 params->has_tls_hostname = !!s->parameters.tls_hostname;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100448 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530449 params->has_max_bandwidth = true;
450 params->max_bandwidth = s->parameters.max_bandwidth;
451 params->has_downtime_limit = true;
452 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800453 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800454 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200455 params->has_block_incremental = true;
456 params->block_incremental = s->parameters.block_incremental;
Liang Li85de8322015-03-23 16:32:28 +0800457
458 return params;
459}
460
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000461/*
462 * Return true if we're already in the middle of a migration
463 * (i.e. any of the active or setup states)
464 */
465static bool migration_is_setup_or_active(int state)
466{
467 switch (state) {
468 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000469 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000470 case MIGRATION_STATUS_SETUP:
471 return true;
472
473 default:
474 return false;
475
476 }
477}
478
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100479static void populate_ram_info(MigrationInfo *info, MigrationState *s)
480{
481 info->has_ram = true;
482 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200483 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100484 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200485 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100486 /* legacy value. It is not used anymore */
487 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200488 info->ram->normal = ram_counters.normal;
489 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100490 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100491 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200492 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
493 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800494 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100495
Juan Quintela114f5ae2017-05-04 10:09:21 +0200496 if (migrate_use_xbzrle()) {
497 info->has_xbzrle_cache = true;
498 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
499 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200500 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
501 info->xbzrle_cache->pages = xbzrle_counters.pages;
502 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
503 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
504 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200505 }
506
Juan Quintela338182c2017-05-03 13:16:38 +0200507 if (cpu_throttle_active()) {
508 info->has_cpu_throttle_percentage = true;
509 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
510 }
511
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100512 if (s->state != MIGRATION_STATUS_COMPLETED) {
513 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200514 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100515 }
516}
517
Juan Quintela930ac042017-05-04 10:21:46 +0200518static void populate_disk_info(MigrationInfo *info)
519{
520 if (blk_mig_active()) {
521 info->has_disk = true;
522 info->disk = g_malloc0(sizeof(*info->disk));
523 info->disk->transferred = blk_mig_bytes_transferred();
524 info->disk->remaining = blk_mig_bytes_remaining();
525 info->disk->total = blk_mig_bytes_total();
526 }
527}
528
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300529MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000530{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300531 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200532 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000533
Juan Quintela17549e82011-10-05 13:50:43 +0200534 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800535 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200536 /* no migration has happened ever */
537 break;
zhanghailiang31194732015-03-13 16:08:38 +0800538 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400539 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400540 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400541 break;
zhanghailiang31194732015-03-13 16:08:38 +0800542 case MIGRATION_STATUS_ACTIVE:
543 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000544 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200545 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000546 info->has_status = true;
547 info->has_total_time = true;
548 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
549 - s->total_time;
550 info->has_expected_downtime = true;
551 info->expected_downtime = s->expected_downtime;
552 info->has_setup_time = true;
553 info->setup_time = s->setup_time;
554
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100555 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200556 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000557 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800558 case MIGRATION_STATUS_COLO:
559 info->has_status = true;
560 /* TODO: display COLO specific information (checkpoint info etc.) */
561 break;
zhanghailiang31194732015-03-13 16:08:38 +0800562 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300563 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900564 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200565 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200566 info->has_downtime = true;
567 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400568 info->has_setup_time = true;
569 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200570
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100571 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200572 break;
zhanghailiang31194732015-03-13 16:08:38 +0800573 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300574 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100575 if (s->error) {
576 info->has_error_desc = true;
577 info->error_desc = g_strdup(error_get_pretty(s->error));
578 }
Juan Quintela17549e82011-10-05 13:50:43 +0200579 break;
zhanghailiang31194732015-03-13 16:08:38 +0800580 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300581 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200582 break;
aliguori5bb79102008-10-13 03:12:02 +0000583 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800584 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300585
586 return info;
aliguori5bb79102008-10-13 03:12:02 +0000587}
588
Orit Wasserman00458432012-08-06 21:42:48 +0300589void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
590 Error **errp)
591{
592 MigrationState *s = migrate_get_current();
593 MigrationCapabilityStatusList *cap;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100594 bool old_postcopy_cap = migrate_postcopy_ram();
Orit Wasserman00458432012-08-06 21:42:48 +0300595
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000596 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100597 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300598 return;
599 }
600
601 for (cap = params; cap; cap = cap->next) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100602#ifndef CONFIG_LIVE_BLOCK_MIGRATION
603 if (cap->value->capability == MIGRATION_CAPABILITY_BLOCK
604 && cap->value->state) {
605 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
606 "block migration");
607 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
608 continue;
609 }
610#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800611 if (cap->value->capability == MIGRATION_CAPABILITY_X_COLO) {
612 if (!colo_supported()) {
613 error_setg(errp, "COLO is not currently supported, please"
614 " configure with --enable-colo option in order to"
615 " support COLO feature");
616 continue;
617 }
618 }
Orit Wasserman00458432012-08-06 21:42:48 +0300619 s->enabled_capabilities[cap->value->capability] = cap->value->state;
620 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000621
622 if (migrate_postcopy_ram()) {
623 if (migrate_use_compression()) {
624 /* The decompression threads asynchronously write into RAM
625 * rather than use the atomic copies needed to avoid
626 * userfaulting. It should be possible to fix the decompression
627 * threads for compatibility in future.
628 */
629 error_report("Postcopy is not currently compatible with "
630 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000631 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000632 false;
633 }
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100634 /* This check is reasonably expensive, so only when it's being
635 * set the first time, also it's only the destination that needs
636 * special support.
637 */
638 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
639 !postcopy_ram_supported_by_host()) {
640 /* postcopy_ram_supported_by_host will have emitted a more
641 * detailed message
642 */
643 error_report("Postcopy is not supported");
644 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
645 false;
646 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000647 }
Orit Wasserman00458432012-08-06 21:42:48 +0300648}
649
Eric Blake7f375e02016-09-08 22:14:16 -0500650void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800651{
652 MigrationState *s = migrate_get_current();
653
Eric Blake7f375e02016-09-08 22:14:16 -0500654 if (params->has_compress_level &&
655 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100656 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
657 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800658 return;
659 }
Eric Blake7f375e02016-09-08 22:14:16 -0500660 if (params->has_compress_threads &&
661 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100662 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
663 "compress_threads",
664 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800665 return;
666 }
Eric Blake7f375e02016-09-08 22:14:16 -0500667 if (params->has_decompress_threads &&
668 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100669 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
670 "decompress_threads",
671 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800672 return;
673 }
Eric Blake7f375e02016-09-08 22:14:16 -0500674 if (params->has_cpu_throttle_initial &&
675 (params->cpu_throttle_initial < 1 ||
676 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400677 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400678 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400679 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530680 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400681 }
Eric Blake7f375e02016-09-08 22:14:16 -0500682 if (params->has_cpu_throttle_increment &&
683 (params->cpu_throttle_increment < 1 ||
684 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400685 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400686 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400687 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530688 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400689 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530690 if (params->has_max_bandwidth &&
691 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
692 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
693 " range of 0 to %zu bytes/second", SIZE_MAX);
694 return;
695 }
696 if (params->has_downtime_limit &&
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300697 (params->downtime_limit < 0 ||
698 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
699 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
700 "the range of 0 to %d milliseconds",
701 MAX_MIGRATE_DOWNTIME);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530702 return;
703 }
zhanghailiang68b53592016-10-27 14:43:01 +0800704 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
705 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
706 "x_checkpoint_delay",
707 "is invalid, it should be positive");
708 }
Liang Li85de8322015-03-23 16:32:28 +0800709
Eric Blake7f375e02016-09-08 22:14:16 -0500710 if (params->has_compress_level) {
711 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800712 }
Eric Blake7f375e02016-09-08 22:14:16 -0500713 if (params->has_compress_threads) {
714 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800715 }
Eric Blake7f375e02016-09-08 22:14:16 -0500716 if (params->has_decompress_threads) {
717 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800718 }
Eric Blake7f375e02016-09-08 22:14:16 -0500719 if (params->has_cpu_throttle_initial) {
720 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400721 }
Eric Blake7f375e02016-09-08 22:14:16 -0500722 if (params->has_cpu_throttle_increment) {
723 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400724 }
Eric Blake7f375e02016-09-08 22:14:16 -0500725 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100726 g_free(s->parameters.tls_creds);
Eric Blake7f375e02016-09-08 22:14:16 -0500727 s->parameters.tls_creds = g_strdup(params->tls_creds);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100728 }
Eric Blake7f375e02016-09-08 22:14:16 -0500729 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100730 g_free(s->parameters.tls_hostname);
Eric Blake7f375e02016-09-08 22:14:16 -0500731 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100732 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530733 if (params->has_max_bandwidth) {
734 s->parameters.max_bandwidth = params->max_bandwidth;
735 if (s->to_dst_file) {
736 qemu_file_set_rate_limit(s->to_dst_file,
737 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
738 }
739 }
740 if (params->has_downtime_limit) {
741 s->parameters.downtime_limit = params->downtime_limit;
742 }
zhanghailiang68b53592016-10-27 14:43:01 +0800743
744 if (params->has_x_checkpoint_delay) {
745 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800746 if (migration_in_colo_state()) {
747 colo_checkpoint_notify(s);
748 }
zhanghailiang68b53592016-10-27 14:43:01 +0800749 }
Juan Quintela2833c592017-04-05 18:32:37 +0200750 if (params->has_block_incremental) {
751 s->parameters.block_incremental = params->block_incremental;
752 }
Liang Li85de8322015-03-23 16:32:28 +0800753}
754
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100755
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000756void qmp_migrate_start_postcopy(Error **errp)
757{
758 MigrationState *s = migrate_get_current();
759
760 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000761 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000762 " the start of migration");
763 return;
764 }
765
766 if (s->state == MIGRATION_STATUS_NONE) {
767 error_setg(errp, "Postcopy must be started after migration has been"
768 " started");
769 return;
770 }
771 /*
772 * we don't error if migration has finished since that would be racy
773 * with issuing this command.
774 */
775 atomic_set(&s->start_postcopy, true);
776}
777
aliguori065e2812008-11-11 16:46:33 +0000778/* shared migration helpers */
779
zhanghailiang48781e52015-12-16 11:47:33 +0000780void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000781{
zhanghailiang48781e52015-12-16 11:47:33 +0000782 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200783 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200784 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000785 }
786}
787
Juan Quintela2833c592017-04-05 18:32:37 +0200788void migrate_set_block_enabled(bool value, Error **errp)
789{
790 MigrationCapabilityStatusList *cap;
791
792 cap = g_new0(MigrationCapabilityStatusList, 1);
793 cap->value = g_new0(MigrationCapabilityStatus, 1);
794 cap->value->capability = MIGRATION_CAPABILITY_BLOCK;
795 cap->value->state = value;
796 qmp_migrate_set_capabilities(cap, errp);
797 qapi_free_MigrationCapabilityStatusList(cap);
798}
799
800static void migrate_set_block_incremental(MigrationState *s, bool value)
801{
802 s->parameters.block_incremental = value;
803}
804
805static void block_cleanup_parameters(MigrationState *s)
806{
807 if (s->must_remove_block_options) {
808 /* setting to false can never fail */
809 migrate_set_block_enabled(false, &error_abort);
810 migrate_set_block_incremental(s, false);
811 s->must_remove_block_options = false;
812 }
813}
814
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100815static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000816{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100817 MigrationState *s = opaque;
818
819 qemu_bh_delete(s->cleanup_bh);
820 s->cleanup_bh = NULL;
821
zhanghailiang89a02a92016-01-15 11:37:42 +0800822 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100823 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100824 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000825 if (s->migration_thread_running) {
826 qemu_thread_join(&s->thread);
827 s->migration_thread_running = false;
828 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100829 qemu_mutex_lock_iothread();
830
Liang Li8706d2d2015-03-23 16:32:17 +0800831 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800832 qemu_fclose(s->to_dst_file);
833 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000834 }
835
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000836 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
837 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100838
Liang Li94f5a432015-11-02 15:37:00 +0800839 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000840 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800841 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100842 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100843
844 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200845 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000846}
847
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100848void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000849{
Peter Maydell25174052016-10-21 18:41:45 +0100850 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +0800851 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000852 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
853 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100854 if (!s->error) {
855 s->error = error_copy(error);
856 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100857 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200858 block_cleanup_parameters(s);
Juan Quintela458cf282011-02-22 23:32:54 +0100859}
860
Juan Quintela0edda1c2010-05-11 16:28:39 +0200861static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000862{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000863 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800864 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100865 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000866
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000867 if (s->rp_state.from_dst_file) {
868 /* shutdown the rp socket, so causing the rp thread to shutdown */
869 qemu_file_shutdown(s->rp_state.from_dst_file);
870 }
871
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000872 do {
873 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000874 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000875 break;
876 }
zhanghailiang48781e52015-12-16 11:47:33 +0000877 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +0800878 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000879
880 /*
881 * If we're unlucky the migration code might be stuck somewhere in a
882 * send/write while the network has failed and is waiting to timeout;
883 * if we've got shutdown(2) available then we can force it to quit.
884 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
885 * called in a bh, so there is no race against this cancel.
886 */
zhanghailiang31194732015-03-13 16:08:38 +0800887 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000888 qemu_file_shutdown(f);
889 }
zhanghailiang1d2acc32017-01-24 15:59:52 +0800890 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
891 Error *local_err = NULL;
892
893 bdrv_invalidate_cache_all(&local_err);
894 if (local_err) {
895 error_report_err(local_err);
896 } else {
897 s->block_inactive = false;
898 }
899 }
Juan Quintela2833c592017-04-05 18:32:37 +0200900 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000901}
902
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100903void add_migration_state_change_notifier(Notifier *notify)
904{
905 notifier_list_add(&migration_state_notifiers, notify);
906}
907
908void remove_migration_state_change_notifier(Notifier *notify)
909{
Paolo Bonzini31552522012-01-13 17:34:01 +0100910 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100911}
912
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200913bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200914{
zhanghailiang31194732015-03-13 16:08:38 +0800915 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200916}
917
Juan Quintela70736932011-02-23 00:43:59 +0100918bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100919{
zhanghailiang31194732015-03-13 16:08:38 +0800920 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100921}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200922
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200923bool migration_has_failed(MigrationState *s)
924{
zhanghailiang31194732015-03-13 16:08:38 +0800925 return (s->state == MIGRATION_STATUS_CANCELLED ||
926 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200927}
928
Juan Quintela57273092017-03-20 22:25:28 +0100929bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000930{
Juan Quintela57273092017-03-20 22:25:28 +0100931 MigrationState *s = migrate_get_current();
932
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000933 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
934}
935
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000936bool migration_in_postcopy_after_devices(MigrationState *s)
937{
Juan Quintela57273092017-03-20 22:25:28 +0100938 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000939}
940
Juan Quintelafab35002017-03-22 17:36:57 +0100941bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530942{
Juan Quintelafab35002017-03-22 17:36:57 +0100943 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530944
945 switch (s->state) {
946 case MIGRATION_STATUS_NONE:
947 case MIGRATION_STATUS_CANCELLED:
948 case MIGRATION_STATUS_COMPLETED:
949 case MIGRATION_STATUS_FAILED:
950 return true;
951 case MIGRATION_STATUS_SETUP:
952 case MIGRATION_STATUS_CANCELLING:
953 case MIGRATION_STATUS_ACTIVE:
954 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
955 case MIGRATION_STATUS_COLO:
956 return false;
957 case MIGRATION_STATUS__MAX:
958 g_assert_not_reached();
959 }
960
961 return false;
962}
963
Juan Quintelaa0762d92017-04-05 21:00:09 +0200964MigrationState *migrate_init(void)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200965{
Juan Quintela17549e82011-10-05 13:50:43 +0200966 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300967
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000968 /*
969 * Reinitialise all migration state, except
970 * parameters/capabilities that the user set, and
971 * locks.
972 */
973 s->bytes_xfer = 0;
974 s->xfer_limit = 0;
975 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +0800976 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000977 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000978 s->rp_state.from_dst_file = NULL;
979 s->rp_state.error = false;
980 s->mbps = 0.0;
981 s->downtime = 0;
982 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000983 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000984 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000985 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000986 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100987 error_free(s->error);
988 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +0100989
zhanghailiang48781e52015-12-16 11:47:33 +0000990 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200991
Alex Blighbc72ad62013-08-21 16:03:08 +0100992 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200993 return s;
994}
Juan Quintelacab30142011-02-22 23:54:21 +0100995
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600996static GSList *migration_blockers;
997
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530998int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600999{
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301000 if (only_migratable) {
1001 error_propagate(errp, error_copy(reason));
1002 error_prepend(errp, "disallowing migration blocker "
1003 "(--only_migratable) for: ");
1004 return -EACCES;
1005 }
1006
Juan Quintelafab35002017-03-22 17:36:57 +01001007 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301008 migration_blockers = g_slist_prepend(migration_blockers, reason);
1009 return 0;
1010 }
1011
1012 error_propagate(errp, error_copy(reason));
1013 error_prepend(errp, "disallowing migration blocker (migration in "
1014 "progress) for: ");
1015 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001016}
1017
1018void migrate_del_blocker(Error *reason)
1019{
1020 migration_blockers = g_slist_remove(migration_blockers, reason);
1021}
1022
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001023void qmp_migrate_incoming(const char *uri, Error **errp)
1024{
1025 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001026 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001027
1028 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001029 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001030 return;
1031 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001032 if (!once) {
1033 error_setg(errp, "The incoming migration has already been started");
1034 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001035
1036 qemu_start_incoming_migration(uri, &local_err);
1037
1038 if (local_err) {
1039 error_propagate(errp, local_err);
1040 return;
1041 }
1042
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001043 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001044}
1045
Greg Kurz24f39022016-05-04 21:44:19 +02001046bool migration_is_blocked(Error **errp)
1047{
1048 if (qemu_savevm_state_blocked(errp)) {
1049 return true;
1050 }
1051
1052 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001053 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001054 return true;
1055 }
1056
1057 return false;
1058}
1059
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001060void qmp_migrate(const char *uri, bool has_blk, bool blk,
1061 bool has_inc, bool inc, bool has_detach, bool detach,
1062 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001063{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001064 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001065 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001066 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001067
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001068 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001069 s->state == MIGRATION_STATUS_CANCELLING ||
1070 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001071 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001072 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001073 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001074 if (runstate_check(RUN_STATE_INMIGRATE)) {
1075 error_setg(errp, "Guest is waiting for an incoming migration");
1076 return;
1077 }
1078
Greg Kurz24f39022016-05-04 21:44:19 +02001079 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001080 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001081 }
1082
Juan Quintela2833c592017-04-05 18:32:37 +02001083 if ((has_blk && blk) || (has_inc && inc)) {
1084 if (migrate_use_block() || migrate_use_block_incremental()) {
1085 error_setg(errp, "Command options are incompatible with "
1086 "current migration capabilities");
1087 return;
1088 }
1089 migrate_set_block_enabled(true, &local_err);
1090 if (local_err) {
1091 error_propagate(errp, local_err);
1092 return;
1093 }
1094 s->must_remove_block_options = true;
1095 }
1096
1097 if (has_inc && inc) {
1098 migrate_set_block_incremental(s, true);
1099 }
1100
Juan Quintelaa0762d92017-04-05 21:00:09 +02001101 s = migrate_init();
Juan Quintelacab30142011-02-22 23:54:21 +01001102
1103 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001104 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001105#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001106 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001107 rdma_start_outgoing_migration(s, p, &local_err);
1108#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001109 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001110 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001111 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001112 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001113 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001114 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001115 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001116 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1117 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001118 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1119 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001120 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001121 }
1122
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001123 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001124 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001125 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001126 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001127 }
Juan Quintelacab30142011-02-22 23:54:21 +01001128}
1129
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001130void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001131{
Juan Quintela17549e82011-10-05 13:50:43 +02001132 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001133}
1134
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001135void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1136{
1137 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001138 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001139
1140 /* Check for truncation */
1141 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001142 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1143 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001144 return;
1145 }
1146
Orit Wassermana5615b12014-01-30 20:08:36 +02001147 /* Cache should not be larger than guest ram size */
1148 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001149 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1150 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001151 return;
1152 }
1153
Orit Wassermanc91e6812014-01-30 20:08:34 +02001154 new_size = xbzrle_cache_resize(value);
1155 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001156 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1157 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001158 return;
1159 }
1160
1161 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001162}
1163
1164int64_t qmp_query_migrate_cache_size(Error **errp)
1165{
1166 return migrate_xbzrle_cache_size();
1167}
1168
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001169void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001170{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301171 MigrationParameters p = {
1172 .has_max_bandwidth = true,
1173 .max_bandwidth = value,
1174 };
Juan Quintelacab30142011-02-22 23:54:21 +01001175
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301176 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001177}
1178
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001179void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001180{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001181 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1182 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1183 "the range of 0 to %d seconds",
1184 MAX_MIGRATE_DOWNTIME_SECONDS);
1185 return;
1186 }
1187
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301188 value *= 1000; /* Convert to milliseconds */
1189 value = MAX(0, MIN(INT64_MAX, value));
1190
1191 MigrationParameters p = {
1192 .has_downtime_limit = true,
1193 .downtime_limit = value,
1194 };
1195
1196 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001197}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001198
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001199bool migrate_release_ram(void)
1200{
1201 MigrationState *s;
1202
1203 s = migrate_get_current();
1204
1205 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1206}
1207
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001208bool migrate_postcopy_ram(void)
1209{
1210 MigrationState *s;
1211
1212 s = migrate_get_current();
1213
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001214 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001215}
1216
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001217bool migrate_auto_converge(void)
1218{
1219 MigrationState *s;
1220
1221 s = migrate_get_current();
1222
1223 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1224}
1225
Peter Lieven323004a2013-07-18 09:48:50 +02001226bool migrate_zero_blocks(void)
1227{
1228 MigrationState *s;
1229
1230 s = migrate_get_current();
1231
1232 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1233}
1234
Liang Li8706d2d2015-03-23 16:32:17 +08001235bool migrate_use_compression(void)
1236{
Liang Lidde4e692015-03-23 16:32:26 +08001237 MigrationState *s;
1238
1239 s = migrate_get_current();
1240
1241 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001242}
1243
1244int migrate_compress_level(void)
1245{
1246 MigrationState *s;
1247
1248 s = migrate_get_current();
1249
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001250 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001251}
1252
1253int migrate_compress_threads(void)
1254{
1255 MigrationState *s;
1256
1257 s = migrate_get_current();
1258
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001259 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001260}
1261
Liang Li3fcb38c2015-03-23 16:32:18 +08001262int migrate_decompress_threads(void)
1263{
1264 MigrationState *s;
1265
1266 s = migrate_get_current();
1267
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001268 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001269}
1270
Juan Quintelab05dc722015-07-07 14:44:05 +02001271bool migrate_use_events(void)
1272{
1273 MigrationState *s;
1274
1275 s = migrate_get_current();
1276
1277 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1278}
1279
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001280int migrate_use_xbzrle(void)
1281{
1282 MigrationState *s;
1283
1284 s = migrate_get_current();
1285
1286 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1287}
1288
1289int64_t migrate_xbzrle_cache_size(void)
1290{
1291 MigrationState *s;
1292
1293 s = migrate_get_current();
1294
1295 return s->xbzrle_cache_size;
1296}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001297
Juan Quintela2833c592017-04-05 18:32:37 +02001298bool migrate_use_block(void)
1299{
1300 MigrationState *s;
1301
1302 s = migrate_get_current();
1303
1304 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1305}
1306
1307bool migrate_use_block_incremental(void)
1308{
1309 MigrationState *s;
1310
1311 s = migrate_get_current();
1312
1313 return s->parameters.block_incremental;
1314}
1315
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001316/* migration thread support */
1317/*
1318 * Something bad happened to the RP stream, mark an error
1319 * The caller shall print or trace something to indicate why
1320 */
1321static void mark_source_rp_bad(MigrationState *s)
1322{
1323 s->rp_state.error = true;
1324}
1325
1326static struct rp_cmd_args {
1327 ssize_t len; /* -1 = variable */
1328 const char *name;
1329} rp_cmd_args[] = {
1330 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1331 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1332 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001333 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1334 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001335 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1336};
1337
1338/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001339 * Process a request for pages received on the return path,
1340 * We're allowed to send more than requested (e.g. to round to our page size)
1341 * and we don't need to send pages that have already been sent.
1342 */
1343static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1344 ram_addr_t start, size_t len)
1345{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001346 long our_host_ps = getpagesize();
1347
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001348 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001349
1350 /*
1351 * Since we currently insist on matching page sizes, just sanity check
1352 * we're being asked for whole host pages.
1353 */
1354 if (start & (our_host_ps-1) ||
1355 (len & (our_host_ps-1))) {
1356 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1357 " len: %zd", __func__, start, len);
1358 mark_source_rp_bad(ms);
1359 return;
1360 }
1361
Juan Quintela96506892017-03-14 18:41:03 +01001362 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001363 mark_source_rp_bad(ms);
1364 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001365}
1366
1367/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001368 * Handles messages sent on the return path towards the source VM
1369 *
1370 */
1371static void *source_return_path_thread(void *opaque)
1372{
1373 MigrationState *ms = opaque;
1374 QEMUFile *rp = ms->rp_state.from_dst_file;
1375 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001376 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001377 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001378 ram_addr_t start = 0; /* =0 to silence warning */
1379 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001380 int res;
1381
1382 trace_source_return_path_thread_entry();
1383 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1384 migration_is_setup_or_active(ms->state)) {
1385 trace_source_return_path_thread_loop_top();
1386 header_type = qemu_get_be16(rp);
1387 header_len = qemu_get_be16(rp);
1388
1389 if (header_type >= MIG_RP_MSG_MAX ||
1390 header_type == MIG_RP_MSG_INVALID) {
1391 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1392 header_type, header_len);
1393 mark_source_rp_bad(ms);
1394 goto out;
1395 }
1396
1397 if ((rp_cmd_args[header_type].len != -1 &&
1398 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001399 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001400 error_report("RP: Received '%s' message (0x%04x) with"
1401 "incorrect length %d expecting %zu",
1402 rp_cmd_args[header_type].name, header_type, header_len,
1403 (size_t)rp_cmd_args[header_type].len);
1404 mark_source_rp_bad(ms);
1405 goto out;
1406 }
1407
1408 /* We know we've got a valid header by this point */
1409 res = qemu_get_buffer(rp, buf, header_len);
1410 if (res != header_len) {
1411 error_report("RP: Failed reading data for message 0x%04x"
1412 " read %d expected %d",
1413 header_type, res, header_len);
1414 mark_source_rp_bad(ms);
1415 goto out;
1416 }
1417
1418 /* OK, we have the message and the data */
1419 switch (header_type) {
1420 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001421 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001422 trace_source_return_path_thread_shut(sibling_error);
1423 if (sibling_error) {
1424 error_report("RP: Sibling indicated error %d", sibling_error);
1425 mark_source_rp_bad(ms);
1426 }
1427 /*
1428 * We'll let the main thread deal with closing the RP
1429 * we could do a shutdown(2) on it, but we're the only user
1430 * anyway, so there's nothing gained.
1431 */
1432 goto out;
1433
1434 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001435 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001436 trace_source_return_path_thread_pong(tmp32);
1437 break;
1438
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001439 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001440 start = ldq_be_p(buf);
1441 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001442 migrate_handle_rp_req_pages(ms, NULL, start, len);
1443 break;
1444
1445 case MIG_RP_MSG_REQ_PAGES_ID:
1446 expected_len = 12 + 1; /* header + termination */
1447
1448 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001449 start = ldq_be_p(buf);
1450 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001451 /* Now we expect an idstr */
1452 tmp32 = buf[12]; /* Length of the following idstr */
1453 buf[13 + tmp32] = '\0';
1454 expected_len += tmp32;
1455 }
1456 if (header_len != expected_len) {
1457 error_report("RP: Req_Page_id with length %d expecting %zd",
1458 header_len, expected_len);
1459 mark_source_rp_bad(ms);
1460 goto out;
1461 }
1462 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1463 break;
1464
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001465 default:
1466 break;
1467 }
1468 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001469 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001470 trace_source_return_path_thread_bad_end();
1471 mark_source_rp_bad(ms);
1472 }
1473
1474 trace_source_return_path_thread_end();
1475out:
1476 ms->rp_state.from_dst_file = NULL;
1477 qemu_fclose(rp);
1478 return NULL;
1479}
1480
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001481static int open_return_path_on_source(MigrationState *ms)
1482{
1483
zhanghailiang89a02a92016-01-15 11:37:42 +08001484 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001485 if (!ms->rp_state.from_dst_file) {
1486 return -1;
1487 }
1488
1489 trace_open_return_path_on_source();
1490 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1491 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1492
1493 trace_open_return_path_on_source_continue();
1494
1495 return 0;
1496}
1497
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001498/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1499static int await_return_path_close_on_source(MigrationState *ms)
1500{
1501 /*
1502 * If this is a normal exit then the destination will send a SHUT and the
1503 * rp_thread will exit, however if there's an error we need to cause
1504 * it to exit.
1505 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001506 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001507 /*
1508 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1509 * waiting for the destination.
1510 */
1511 qemu_file_shutdown(ms->rp_state.from_dst_file);
1512 mark_source_rp_bad(ms);
1513 }
1514 trace_await_return_path_close_on_source_joining();
1515 qemu_thread_join(&ms->rp_state.rp_thread);
1516 trace_await_return_path_close_on_source_close();
1517 return ms->rp_state.error;
1518}
1519
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001520/*
1521 * Switch from normal iteration to postcopy
1522 * Returns non-0 on error
1523 */
1524static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1525{
1526 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001527 QIOChannelBuffer *bioc;
1528 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001529 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001530 bool restart_block = false;
zhanghailiang48781e52015-12-16 11:47:33 +00001531 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001532 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1533
1534 trace_postcopy_start();
1535 qemu_mutex_lock_iothread();
1536 trace_postcopy_start_set_run();
1537
1538 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1539 *old_vm_running = runstate_is_running();
1540 global_state_store();
1541 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001542 if (ret < 0) {
1543 goto fail;
1544 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001545
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001546 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001547 if (ret < 0) {
1548 goto fail;
1549 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001550 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001551
1552 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001553 * Cause any non-postcopiable, but iterative devices to
1554 * send out their final data.
1555 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001556 qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001557
1558 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001559 * in Finish migrate and with the io-lock held everything should
1560 * be quiet, but we've potentially still got dirty pages and we
1561 * need to tell the destination to throw any pages it's already received
1562 * that are dirty
1563 */
1564 if (ram_postcopy_send_discard_bitmap(ms)) {
1565 error_report("postcopy send discard bitmap failed");
1566 goto fail;
1567 }
1568
1569 /*
1570 * send rest of state - note things that are doing postcopy
1571 * will notice we're in POSTCOPY_ACTIVE and not actually
1572 * wrap their state up here
1573 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001574 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001575 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001576 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001577
1578 /*
1579 * While loading the device state we may trigger page transfer
1580 * requests and the fd must be free to process those, and thus
1581 * the destination must read the whole device state off the fd before
1582 * it starts processing it. Unfortunately the ad-hoc migration format
1583 * doesn't allow the destination to know the size to read without fully
1584 * parsing it through each devices load-state code (especially the open
1585 * coded devices that use get/put).
1586 * So we wrap the device state up in a package with a length at the start;
1587 * to do this we use a qemu_buf to hold the whole of the device state.
1588 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001589 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001590 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001591 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1592 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001593
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001594 /*
1595 * Make sure the receiver can get incoming pages before we send the rest
1596 * of the state
1597 */
1598 qemu_savevm_send_postcopy_listen(fb);
1599
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001600 qemu_savevm_state_complete_precopy(fb, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001601 qemu_savevm_send_ping(fb, 3);
1602
1603 qemu_savevm_send_postcopy_run(fb);
1604
1605 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001606
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001607 /* Last point of recovery; as soon as we send the package the destination
1608 * can open devices and potentially start running.
1609 * Lets just check again we've not got any errors.
1610 */
1611 ret = qemu_file_get_error(ms->to_dst_file);
1612 if (ret) {
1613 error_report("postcopy_start: Migration stream errored (pre package)");
1614 goto fail_closefb;
1615 }
1616
1617 restart_block = false;
1618
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001619 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001620 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001621 goto fail_closefb;
1622 }
1623 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001624
1625 /* Send a notify to give a chance for anything that needs to happen
1626 * at the transition to postcopy and after the device state; in particular
1627 * spice needs to trigger a transition now
1628 */
1629 ms->postcopy_after_devices = true;
1630 notifier_list_notify(&migration_state_notifiers, ms);
1631
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001632 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1633
1634 qemu_mutex_unlock_iothread();
1635
1636 /*
1637 * Although this ping is just for debug, it could potentially be
1638 * used for getting a better measurement of downtime at the source.
1639 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001640 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001641
Pavel Butsykinced1c612017-02-03 18:23:21 +03001642 if (migrate_release_ram()) {
1643 ram_postcopy_migrated_memory_release(ms);
1644 }
1645
zhanghailiang89a02a92016-01-15 11:37:42 +08001646 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001647 if (ret) {
1648 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001649 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001650 MIGRATION_STATUS_FAILED);
1651 }
1652
1653 return ret;
1654
1655fail_closefb:
1656 qemu_fclose(fb);
1657fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001658 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001659 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001660 if (restart_block) {
1661 /* A failure happened early enough that we know the destination hasn't
1662 * accessed block devices, so we're safe to recover.
1663 */
1664 Error *local_err = NULL;
1665
1666 bdrv_invalidate_cache_all(&local_err);
1667 if (local_err) {
1668 error_report_err(local_err);
1669 }
1670 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001671 qemu_mutex_unlock_iothread();
1672 return -1;
1673}
1674
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001675/**
1676 * migration_completion: Used by migration_thread when there's not much left.
1677 * The caller 'breaks' the loop when this returns.
1678 *
1679 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001680 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001681 * @*old_vm_running: Pointer to old_vm_running flag
1682 * @*start_time: Pointer to time to update
1683 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001684static void migration_completion(MigrationState *s, int current_active_state,
1685 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001686 int64_t *start_time)
1687{
1688 int ret;
1689
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001690 if (s->state == MIGRATION_STATUS_ACTIVE) {
1691 qemu_mutex_lock_iothread();
1692 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1693 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1694 *old_vm_running = runstate_is_running();
1695 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001696
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001697 if (!ret) {
1698 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001699 if (ret >= 0) {
1700 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1701 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
1702 }
zhanghailiang0b827d52016-10-27 14:42:54 +08001703 /*
1704 * Don't mark the image with BDRV_O_INACTIVE flag if
1705 * we will go into COLO stage later.
1706 */
1707 if (ret >= 0 && !migrate_colo_enabled()) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001708 ret = bdrv_inactivate_all();
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001709 if (ret >= 0) {
1710 s->block_inactive = true;
1711 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001712 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001713 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001714 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001715
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001716 if (ret < 0) {
1717 goto fail;
1718 }
1719 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1720 trace_migration_completion_postcopy_end();
1721
zhanghailiang89a02a92016-01-15 11:37:42 +08001722 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001723 trace_migration_completion_postcopy_end_after_complete();
1724 }
1725
1726 /*
1727 * If rp was opened we must clean up the thread before
1728 * cleaning everything else up (since if there are no failures
1729 * it will wait for the destination to send it's status in
1730 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001731 */
Peter Xu0425dc92017-05-31 18:35:34 +08001732 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001733 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08001734 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001735 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08001736 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001737 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001738 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001739 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001740 }
1741
zhanghailiang89a02a92016-01-15 11:37:42 +08001742 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001743 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001744 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001745 }
1746
zhanghailiang0b827d52016-10-27 14:42:54 +08001747 if (!migrate_colo_enabled()) {
1748 migrate_set_state(&s->state, current_active_state,
1749 MIGRATION_STATUS_COMPLETED);
1750 }
1751
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001752 return;
1753
Greg Kurzfe904ea2016-05-18 15:44:36 +02001754fail_invalidate:
1755 /* If not doing postcopy, vm_start() will be called: let's regain
1756 * control on images.
1757 */
1758 if (s->state == MIGRATION_STATUS_ACTIVE) {
1759 Error *local_err = NULL;
1760
zhanghailiang1d2acc32017-01-24 15:59:52 +08001761 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001762 bdrv_invalidate_cache_all(&local_err);
1763 if (local_err) {
1764 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001765 } else {
1766 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02001767 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001768 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001769 }
1770
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001771fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001772 migrate_set_state(&s->state, current_active_state,
1773 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001774}
1775
zhanghailiang35a6ed42016-10-27 14:42:52 +08001776bool migrate_colo_enabled(void)
1777{
1778 MigrationState *s = migrate_get_current();
1779 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
1780}
1781
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001782/*
1783 * Master migration thread on the source VM.
1784 * It drives the migration and pumps the data down the outgoing channel.
1785 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001786static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001787{
Juan Quintela9848a402012-12-19 09:55:50 +01001788 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001789 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001790 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1791 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001792 int64_t initial_bytes = 0;
Peter Xufaec0662017-04-01 16:18:43 +08001793 /*
1794 * The final stage happens when the remaining data is smaller than
1795 * this threshold; it's calculated from the requested downtime and
1796 * measured bandwidth
1797 */
1798 int64_t threshold_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001799 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001800 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001801 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001802 bool entered_postcopy = false;
1803 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1804 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang0b827d52016-10-27 14:42:54 +08001805 bool enable_colo = migrate_colo_enabled();
Juan Quintela76f59332012-10-03 20:16:24 +02001806
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001807 rcu_register_thread();
1808
zhanghailiang89a02a92016-01-15 11:37:42 +08001809 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001810
Peter Xu62a02652017-06-14 15:55:58 +08001811 /*
1812 * If we opened the return path, we need to make sure dst has it
1813 * opened as well.
1814 */
1815 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001816 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001817 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001818
1819 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001820 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08001821 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001822
Peter Xu0425dc92017-05-31 18:35:34 +08001823 if (migrate_postcopy_ram()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001824 /*
1825 * Tell the destination that we *might* want to do postcopy later;
1826 * if the other end can't do postcopy it should fail now, nice and
1827 * early.
1828 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001829 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001830 }
1831
Juan Quintelaa0762d92017-04-05 21:00:09 +02001832 qemu_savevm_state_begin(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001833
Alex Blighbc72ad62013-08-21 16:03:08 +01001834 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00001835 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1836 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001837
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001838 trace_migration_thread_setup_complete();
1839
1840 while (s->state == MIGRATION_STATUS_ACTIVE ||
1841 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001842 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001843 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001844
zhanghailiang89a02a92016-01-15 11:37:42 +08001845 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001846 uint64_t pend_post, pend_nonpost;
1847
Peter Xufaec0662017-04-01 16:18:43 +08001848 qemu_savevm_state_pending(s->to_dst_file, threshold_size,
1849 &pend_nonpost, &pend_post);
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001850 pending_size = pend_nonpost + pend_post;
Peter Xufaec0662017-04-01 16:18:43 +08001851 trace_migrate_pending(pending_size, threshold_size,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001852 pend_post, pend_nonpost);
Peter Xufaec0662017-04-01 16:18:43 +08001853 if (pending_size && pending_size >= threshold_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001854 /* Still a significant amount to transfer */
1855
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001856 if (migrate_postcopy_ram() &&
1857 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
Peter Xufaec0662017-04-01 16:18:43 +08001858 pend_nonpost <= threshold_size &&
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001859 atomic_read(&s->start_postcopy)) {
1860
1861 if (!postcopy_start(s, &old_vm_running)) {
1862 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1863 entered_postcopy = true;
1864 }
1865
1866 continue;
1867 }
1868 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001869 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001870 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001871 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001872 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001873 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001874 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001875 }
1876 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001877
zhanghailiang89a02a92016-01-15 11:37:42 +08001878 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001879 migrate_set_state(&s->state, current_active_state,
1880 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001881 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001882 break;
1883 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001884 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001885 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001886 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1887 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001888 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001889 double bandwidth = (double)transferred_bytes / time_spent;
Peter Xufaec0662017-04-01 16:18:43 +08001890 threshold_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001891
Wei Yang5b648de2016-01-24 14:09:57 +00001892 s->mbps = (((double) transferred_bytes * 8.0) /
1893 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04001894
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001895 trace_migrate_transferred(transferred_bytes, time_spent,
Peter Xufaec0662017-04-01 16:18:43 +08001896 bandwidth, threshold_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001897 /* if we haven't sent anything, we don't want to recalculate
1898 10000 is a small enough number for our purposes */
Juan Quintela93604472017-06-06 19:49:03 +02001899 if (ram_counters.dirty_pages_rate && transferred_bytes > 10000) {
1900 s->expected_downtime = ram_counters.dirty_pages_rate *
Juan Quintela20afaed2017-03-21 09:09:14 +01001901 qemu_target_page_size() / bandwidth;
Juan Quintela90f8ae72013-02-01 13:22:37 +01001902 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001903
zhanghailiang89a02a92016-01-15 11:37:42 +08001904 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001905 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08001906 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001907 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001908 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001909 /* usleep expects microseconds */
1910 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1911 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001912 }
1913
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001914 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001915 /* If we enabled cpu throttling for auto-converge, turn it off. */
1916 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001917 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001918
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001919 qemu_mutex_lock_iothread();
zhanghailiang0b827d52016-10-27 14:42:54 +08001920 /*
1921 * The resource has been allocated by migration will be reused in COLO
1922 * process, so don't release them.
1923 */
1924 if (!enable_colo) {
1925 qemu_savevm_state_cleanup();
1926 }
zhanghailiang31194732015-03-13 16:08:38 +08001927 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001928 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001929 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001930 if (!entered_postcopy) {
1931 s->downtime = end_time - start_time;
1932 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001933 if (s->total_time) {
1934 s->mbps = (((double) transferred_bytes * 8.0) /
1935 ((double) s->total_time)) / 1000;
1936 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001937 runstate_set(RUN_STATE_POSTMIGRATE);
1938 } else {
zhanghailiang0b827d52016-10-27 14:42:54 +08001939 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
1940 migrate_start_colo_process(s);
1941 qemu_savevm_state_cleanup();
1942 /*
1943 * Fixme: we will run VM in COLO no matter its old running state.
1944 * After exited COLO, we will keep running.
1945 */
1946 old_vm_running = true;
1947 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001948 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001949 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01001950 } else {
1951 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
1952 runstate_set(RUN_STATE_POSTMIGRATE);
1953 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001954 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001955 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001956 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001957 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001958
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001959 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001960 return NULL;
1961}
1962
Juan Quintela9848a402012-12-19 09:55:50 +01001963void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001964{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301965 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001966 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001967
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01001968 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08001969 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301970 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01001971
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001972 /* Notify before starting migration thread */
1973 notifier_list_notify(&migration_state_notifiers, s);
1974
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001975 /*
1976 * Open the return path; currently for postcopy but other things might
1977 * also want it.
1978 */
1979 if (migrate_postcopy_ram()) {
1980 if (open_return_path_on_source(s)) {
1981 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00001982 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001983 MIGRATION_STATUS_FAILED);
1984 migrate_fd_cleanup(s);
1985 return;
1986 }
1987 }
1988
Liang Li8706d2d2015-03-23 16:32:17 +08001989 migrate_compress_threads_create();
Pankaj Gupta009fad72017-01-23 19:12:56 +05301990 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001991 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001992 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001993}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001994