blob: f9053b07ddd3e9142d2be1c72639ebe142b5a955 [file] [log] [blame]
aliguori5bb79102008-10-13 03:12:02 +00001/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori5bb79102008-10-13 03:12:02 +000014 */
15
Peter Maydell1393a482016-01-26 18:16:54 +000016#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010019#include "qemu/main-loop.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020020#include "migration/blocker.h"
Juan Quintelaf4dbe1b2017-04-05 15:54:10 +020021#include "exec.h"
Juan Quintela7fcac4a2017-04-05 15:58:29 +020022#include "fd.h"
Juan Quintela61e8b142017-04-05 17:40:11 +020023#include "socket.h"
Juan Quintelae1a3ece2017-04-17 20:32:36 +020024#include "rdma.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020025#include "ram.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020026#include "migration/global_state.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020027#include "migration/misc.h"
Juan Quintela6666c962017-04-24 20:07:27 +020028#include "migration.h"
Juan Quintela20a519a2017-04-20 14:48:46 +020029#include "savevm.h"
Juan Quintela40014d82017-04-17 19:34:36 +020030#include "qemu-file-channel.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020031#include "qemu-file.h"
Juan Quintela987772d2017-04-17 19:02:59 +020032#include "migration/vmstate.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010034#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010035#include "qapi/qmp/qerror.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000036#include "qapi/util.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/sockets.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020038#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020039#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020040#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020041#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030042#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010043#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020044#include "qapi-event.h"
Jason J. Herne070afca2015-09-08 13:12:35 -040045#include "qom/cpu.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000046#include "exec/memory.h"
47#include "exec/address-spaces.h"
Juan Quintela51180422017-04-24 20:50:19 +020048#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010049#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080050#include "migration/colo.h"
aliguori065e2812008-11-11 16:46:33 +000051
Jason J. Hernedc325622015-09-08 13:12:37 -040052#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000053
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010054/* Amount of time to allocate to each "chunk" of bandwidth-throttled
55 * data. */
56#define BUFFER_DELAY 100
57#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
58
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053059/* Time in milliseconds we are allowed to stop the source,
60 * for sending the last part */
61#define DEFAULT_MIGRATE_SET_DOWNTIME 300
62
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030063/* Maximum migrate downtime set to 2000 seconds */
64#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
65#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
66
Liang Li8706d2d2015-03-23 16:32:17 +080067/* Default compression thread count */
68#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080069/* Default decompression thread count, usually decompression is at
70 * least 4 times as fast as compression.*/
71#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080072/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
73#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040074/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040075#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
76#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080077
Orit Wasserman17ad9b32012-08-06 21:42:53 +030078/* Migration XBZRLE default cache size */
79#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
80
zhanghailiang68b53592016-10-27 14:43:01 +080081/* The delay time (in ms) between two COLO checkpoints
82 * Note: Please change this default value to 10000 when we support hybrid mode.
83 */
84#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
85
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010086static NotifierList migration_state_notifiers =
87 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
88
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000089static bool deferred_incoming;
90
Juan Quintelada6f1792017-04-24 17:37:14 +020091/* Messages sent on the return path from destination to source */
92enum mig_rp_message_type {
93 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
94 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
95 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
96
97 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
98 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
99
100 MIG_RP_MSG_MAX
101};
102
Juan Quintela17549e82011-10-05 13:50:43 +0200103/* When we add fault tolerance, we could have several
104 migrations at once. For now we don't need to add
105 dynamic creation of migration */
106
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100107/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200108MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200109{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000110 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +0200111 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +0800112 .state = MIGRATION_STATUS_NONE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300113 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -0400114 .mbps = -1,
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100115 .parameters = {
116 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
117 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
118 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
119 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
120 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530121 .max_bandwidth = MAX_THROTTLE,
122 .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME,
zhanghailiang68b53592016-10-27 14:43:01 +0800123 .x_checkpoint_delay = DEFAULT_MIGRATE_X_CHECKPOINT_DELAY,
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100124 },
Juan Quintela17549e82011-10-05 13:50:43 +0200125 };
126
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000127 if (!once) {
Daniel P. Berrange4af245d2017-03-15 16:16:03 +0000128 current_migration.parameters.tls_creds = g_strdup("");
129 current_migration.parameters.tls_hostname = g_strdup("");
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000130 once = true;
131 }
Juan Quintela17549e82011-10-05 13:50:43 +0200132 return &current_migration;
133}
134
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100135MigrationIncomingState *migration_incoming_get_current(void)
136{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100137 static bool once;
138 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100139
Juan Quintelab4b076d2017-01-23 22:32:06 +0100140 if (!once) {
141 mis_current.state = MIGRATION_STATUS_NONE;
142 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100143 qemu_mutex_init(&mis_current.rp_mutex);
144 qemu_event_init(&mis_current.main_thread_load_event, false);
145 once = true;
146 }
147 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100148}
149
150void migration_incoming_state_destroy(void)
151{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100152 struct MigrationIncomingState *mis = migration_incoming_get_current();
153
Peter Xu34826552017-05-19 14:43:29 +0800154 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800155 /* Tell source that we are done */
156 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800157 qemu_fclose(mis->to_src_file);
158 mis->to_src_file = NULL;
159 }
160
Peter Xu660819b2017-05-19 14:43:30 +0800161 if (mis->from_src_file) {
162 qemu_fclose(mis->from_src_file);
163 mis->from_src_file = NULL;
164 }
165
Juan Quintelab4b076d2017-01-23 22:32:06 +0100166 qemu_event_destroy(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100167}
168
Juan Quintelab05dc722015-07-07 14:44:05 +0200169static void migrate_generate_event(int new_state)
170{
171 if (migrate_use_events()) {
172 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200173 }
174}
175
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000176/*
177 * Called on -incoming with a defer: uri.
178 * The migration can be started later after any parameters have been
179 * changed.
180 */
181static void deferred_incoming_migration(Error **errp)
182{
183 if (deferred_incoming) {
184 error_setg(errp, "Incoming migration already deferred");
185 }
186 deferred_incoming = true;
187}
188
Juan Quintelada6f1792017-04-24 17:37:14 +0200189/*
190 * Send a message on the return channel back to the source
191 * of the migration.
192 */
193static void migrate_send_rp_message(MigrationIncomingState *mis,
194 enum mig_rp_message_type message_type,
195 uint16_t len, void *data)
196{
197 trace_migrate_send_rp_message((int)message_type, len);
198 qemu_mutex_lock(&mis->rp_mutex);
199 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
200 qemu_put_be16(mis->to_src_file, len);
201 qemu_put_buffer(mis->to_src_file, data, len);
202 qemu_fflush(mis->to_src_file);
203 qemu_mutex_unlock(&mis->rp_mutex);
204}
205
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000206/* Request a range of pages from the source VM at the given
207 * start address.
208 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
209 * as the last request (a name must have been given previously)
210 * Start: Address offset within the RB
211 * Len: Length in bytes required - must be a multiple of pagesize
212 */
213void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
214 ram_addr_t start, size_t len)
215{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100216 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000217 size_t msglen = 12; /* start + len */
218
219 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
220 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
221
222 if (rbname) {
223 int rbname_len = strlen(rbname);
224 assert(rbname_len < 256);
225
226 bufc[msglen++] = rbname_len;
227 memcpy(bufc + msglen, rbname, rbname_len);
228 msglen += rbname_len;
229 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
230 } else {
231 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
232 }
233}
234
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200235void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000236{
aliguori34c9dd82008-10-13 03:14:31 +0000237 const char *p;
238
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200239 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000240 if (!strcmp(uri, "defer")) {
241 deferred_incoming_migration(errp);
242 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200243 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400244#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000245 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400246 rdma_start_incoming_migration(p, errp);
247#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000248 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200249 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000250 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200251 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000252 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200253 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000254 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100255 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200256 }
aliguori5bb79102008-10-13 03:12:02 +0000257}
258
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300259static void process_incoming_migration_bh(void *opaque)
260{
261 Error *local_err = NULL;
262 MigrationIncomingState *mis = opaque;
263
Kevin Wolface21a52017-05-04 18:52:36 +0200264 /* Make sure all file formats flush their mutable metadata.
265 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300266 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200267 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200268 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200269 local_err = NULL;
270 autostart = false;
271 }
272
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300273 /*
274 * This must happen after all error conditions are dealt with and
275 * we're sure the VM is going to be running on this host.
276 */
277 qemu_announce_self();
278
279 /* If global state section was not received or we are in running
280 state, we need to obey autostart. Any other state is set with
281 runstate_set. */
282
283 if (!global_state_received() ||
284 global_state_get_runstate() == RUN_STATE_RUNNING) {
285 if (autostart) {
286 vm_start();
287 } else {
288 runstate_set(RUN_STATE_PAUSED);
289 }
290 } else {
291 runstate_set(global_state_get_runstate());
292 }
293 migrate_decompress_threads_join();
294 /*
295 * This must happen after any state changes since as soon as an external
296 * observer sees this event they might start to prod at the VM assuming
297 * it's ready to use.
298 */
299 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
300 MIGRATION_STATUS_COMPLETED);
301 qemu_bh_delete(mis->bh);
302 migration_incoming_state_destroy();
303}
304
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200305static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200306{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200307 QEMUFile *f = opaque;
Juan Quintelab4b076d2017-01-23 22:32:06 +0100308 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000309 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200310 int ret;
311
Juan Quintelab4b076d2017-01-23 22:32:06 +0100312 mis->from_src_file = f;
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000313 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000314 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000315 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
316 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200317 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100318
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000319 ps = postcopy_state_get();
320 trace_process_incoming_migration_co_end(ret, ps);
321 if (ps != POSTCOPY_INCOMING_NONE) {
322 if (ps == POSTCOPY_INCOMING_ADVISE) {
323 /*
324 * Where a migration had postcopy enabled (and thus went to advise)
325 * but managed to complete within the precopy period, we can use
326 * the normal exit.
327 */
328 postcopy_ram_incoming_cleanup(mis);
329 } else if (ret >= 0) {
330 /*
331 * Postcopy was started, cleanup should happen at the end of the
332 * postcopy thread.
333 */
334 trace_process_incoming_migration_co_postcopy_end_main();
335 return;
336 }
337 /* Else if something went wrong then just fall out of the normal exit */
338 }
339
zhanghailiang25d0c162016-10-27 14:42:55 +0800340 /* we get COLO info, and know if we are in COLO mode */
341 if (!ret && migration_incoming_enable_colo()) {
342 mis->migration_incoming_co = qemu_coroutine_self();
343 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
344 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
345 mis->have_colo_incoming_thread = true;
346 qemu_coroutine_yield();
347
348 /* Wait checkpoint incoming thread exit before free resource */
349 qemu_thread_join(&mis->colo_incoming_thread);
350 }
351
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200352 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000353 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
354 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200355 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800356 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600357 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200358 }
Juan Quintela511c0232010-06-09 14:10:55 +0200359
Laurent Viviere8199e42017-04-12 15:53:11 +0200360 free_xbzrle_decoded_buf();
361
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300362 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
363 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200364}
365
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100366void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200367{
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200368 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200369
Liang Li3fcb38c2015-03-23 16:32:18 +0800370 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100371 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200372 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200373}
374
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000375/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000376 * Send a 'SHUT' message on the return channel with the given value
377 * to indicate that we've finished with the RP. Non-0 value indicates
378 * error.
379 */
380void migrate_send_rp_shut(MigrationIncomingState *mis,
381 uint32_t value)
382{
383 uint32_t buf;
384
385 buf = cpu_to_be32(value);
386 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
387}
388
389/*
390 * Send a 'PONG' message on the return channel with the given value
391 * (normally in response to a 'PING')
392 */
393void migrate_send_rp_pong(MigrationIncomingState *mis,
394 uint32_t value)
395{
396 uint32_t buf;
397
398 buf = cpu_to_be32(value);
399 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
400}
401
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300402MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
403{
404 MigrationCapabilityStatusList *head = NULL;
405 MigrationCapabilityStatusList *caps;
406 MigrationState *s = migrate_get_current();
407 int i;
408
Michael Tokarev387eede2013-10-05 13:18:28 +0400409 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700410 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100411#ifndef CONFIG_LIVE_BLOCK_MIGRATION
412 if (i == MIGRATION_CAPABILITY_BLOCK) {
413 continue;
414 }
415#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800416 if (i == MIGRATION_CAPABILITY_X_COLO && !colo_supported()) {
417 continue;
418 }
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300419 if (head == NULL) {
420 head = g_malloc0(sizeof(*caps));
421 caps = head;
422 } else {
423 caps->next = g_malloc0(sizeof(*caps));
424 caps = caps->next;
425 }
426 caps->value =
427 g_malloc(sizeof(*caps->value));
428 caps->value->capability = i;
429 caps->value->state = s->enabled_capabilities[i];
430 }
431
432 return head;
433}
434
Liang Li85de8322015-03-23 16:32:28 +0800435MigrationParameters *qmp_query_migrate_parameters(Error **errp)
436{
437 MigrationParameters *params;
438 MigrationState *s = migrate_get_current();
439
440 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500441 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100442 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500443 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100444 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500445 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100446 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500447 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100448 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500449 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100450 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Eric Blakede63ab62016-09-08 22:14:15 -0500451 params->has_tls_creds = !!s->parameters.tls_creds;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100452 params->tls_creds = g_strdup(s->parameters.tls_creds);
Eric Blakede63ab62016-09-08 22:14:15 -0500453 params->has_tls_hostname = !!s->parameters.tls_hostname;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100454 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530455 params->has_max_bandwidth = true;
456 params->max_bandwidth = s->parameters.max_bandwidth;
457 params->has_downtime_limit = true;
458 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800459 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800460 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200461 params->has_block_incremental = true;
462 params->block_incremental = s->parameters.block_incremental;
Liang Li85de8322015-03-23 16:32:28 +0800463
464 return params;
465}
466
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000467/*
468 * Return true if we're already in the middle of a migration
469 * (i.e. any of the active or setup states)
470 */
471static bool migration_is_setup_or_active(int state)
472{
473 switch (state) {
474 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000475 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000476 case MIGRATION_STATUS_SETUP:
477 return true;
478
479 default:
480 return false;
481
482 }
483}
484
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100485static void populate_ram_info(MigrationInfo *info, MigrationState *s)
486{
487 info->has_ram = true;
488 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200489 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100490 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200491 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100492 /* legacy value. It is not used anymore */
493 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200494 info->ram->normal = ram_counters.normal;
495 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100496 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100497 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200498 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
499 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800500 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100501
Juan Quintela114f5ae2017-05-04 10:09:21 +0200502 if (migrate_use_xbzrle()) {
503 info->has_xbzrle_cache = true;
504 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
505 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200506 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
507 info->xbzrle_cache->pages = xbzrle_counters.pages;
508 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
509 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
510 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200511 }
512
Juan Quintela338182c2017-05-03 13:16:38 +0200513 if (cpu_throttle_active()) {
514 info->has_cpu_throttle_percentage = true;
515 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
516 }
517
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100518 if (s->state != MIGRATION_STATUS_COMPLETED) {
519 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200520 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100521 }
522}
523
Juan Quintela930ac042017-05-04 10:21:46 +0200524static void populate_disk_info(MigrationInfo *info)
525{
526 if (blk_mig_active()) {
527 info->has_disk = true;
528 info->disk = g_malloc0(sizeof(*info->disk));
529 info->disk->transferred = blk_mig_bytes_transferred();
530 info->disk->remaining = blk_mig_bytes_remaining();
531 info->disk->total = blk_mig_bytes_total();
532 }
533}
534
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300535MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000536{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300537 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200538 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000539
Juan Quintela17549e82011-10-05 13:50:43 +0200540 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800541 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200542 /* no migration has happened ever */
543 break;
zhanghailiang31194732015-03-13 16:08:38 +0800544 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400545 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400546 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400547 break;
zhanghailiang31194732015-03-13 16:08:38 +0800548 case MIGRATION_STATUS_ACTIVE:
549 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000550 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200551 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000552 info->has_status = true;
553 info->has_total_time = true;
554 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
555 - s->total_time;
556 info->has_expected_downtime = true;
557 info->expected_downtime = s->expected_downtime;
558 info->has_setup_time = true;
559 info->setup_time = s->setup_time;
560
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100561 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200562 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000563 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800564 case MIGRATION_STATUS_COLO:
565 info->has_status = true;
566 /* TODO: display COLO specific information (checkpoint info etc.) */
567 break;
zhanghailiang31194732015-03-13 16:08:38 +0800568 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300569 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900570 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200571 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200572 info->has_downtime = true;
573 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400574 info->has_setup_time = true;
575 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200576
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100577 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200578 break;
zhanghailiang31194732015-03-13 16:08:38 +0800579 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300580 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100581 if (s->error) {
582 info->has_error_desc = true;
583 info->error_desc = g_strdup(error_get_pretty(s->error));
584 }
Juan Quintela17549e82011-10-05 13:50:43 +0200585 break;
zhanghailiang31194732015-03-13 16:08:38 +0800586 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300587 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200588 break;
aliguori5bb79102008-10-13 03:12:02 +0000589 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800590 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300591
592 return info;
aliguori5bb79102008-10-13 03:12:02 +0000593}
594
Orit Wasserman00458432012-08-06 21:42:48 +0300595void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
596 Error **errp)
597{
598 MigrationState *s = migrate_get_current();
599 MigrationCapabilityStatusList *cap;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100600 bool old_postcopy_cap = migrate_postcopy_ram();
Orit Wasserman00458432012-08-06 21:42:48 +0300601
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000602 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100603 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300604 return;
605 }
606
607 for (cap = params; cap; cap = cap->next) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100608#ifndef CONFIG_LIVE_BLOCK_MIGRATION
609 if (cap->value->capability == MIGRATION_CAPABILITY_BLOCK
610 && cap->value->state) {
611 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
612 "block migration");
613 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
614 continue;
615 }
616#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800617 if (cap->value->capability == MIGRATION_CAPABILITY_X_COLO) {
618 if (!colo_supported()) {
619 error_setg(errp, "COLO is not currently supported, please"
620 " configure with --enable-colo option in order to"
621 " support COLO feature");
622 continue;
623 }
624 }
Orit Wasserman00458432012-08-06 21:42:48 +0300625 s->enabled_capabilities[cap->value->capability] = cap->value->state;
626 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000627
628 if (migrate_postcopy_ram()) {
629 if (migrate_use_compression()) {
630 /* The decompression threads asynchronously write into RAM
631 * rather than use the atomic copies needed to avoid
632 * userfaulting. It should be possible to fix the decompression
633 * threads for compatibility in future.
634 */
635 error_report("Postcopy is not currently compatible with "
636 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000637 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000638 false;
639 }
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100640 /* This check is reasonably expensive, so only when it's being
641 * set the first time, also it's only the destination that needs
642 * special support.
643 */
644 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
645 !postcopy_ram_supported_by_host()) {
646 /* postcopy_ram_supported_by_host will have emitted a more
647 * detailed message
648 */
649 error_report("Postcopy is not supported");
650 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
651 false;
652 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000653 }
Orit Wasserman00458432012-08-06 21:42:48 +0300654}
655
Eric Blake7f375e02016-09-08 22:14:16 -0500656void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800657{
658 MigrationState *s = migrate_get_current();
659
Eric Blake7f375e02016-09-08 22:14:16 -0500660 if (params->has_compress_level &&
661 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100662 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
663 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800664 return;
665 }
Eric Blake7f375e02016-09-08 22:14:16 -0500666 if (params->has_compress_threads &&
667 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100668 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
669 "compress_threads",
670 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800671 return;
672 }
Eric Blake7f375e02016-09-08 22:14:16 -0500673 if (params->has_decompress_threads &&
674 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100675 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
676 "decompress_threads",
677 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800678 return;
679 }
Eric Blake7f375e02016-09-08 22:14:16 -0500680 if (params->has_cpu_throttle_initial &&
681 (params->cpu_throttle_initial < 1 ||
682 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400683 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400684 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400685 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530686 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400687 }
Eric Blake7f375e02016-09-08 22:14:16 -0500688 if (params->has_cpu_throttle_increment &&
689 (params->cpu_throttle_increment < 1 ||
690 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400691 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400692 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400693 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530694 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400695 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530696 if (params->has_max_bandwidth &&
697 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
698 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
699 " range of 0 to %zu bytes/second", SIZE_MAX);
700 return;
701 }
702 if (params->has_downtime_limit &&
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300703 (params->downtime_limit < 0 ||
704 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
705 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
706 "the range of 0 to %d milliseconds",
707 MAX_MIGRATE_DOWNTIME);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530708 return;
709 }
zhanghailiang68b53592016-10-27 14:43:01 +0800710 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
711 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
712 "x_checkpoint_delay",
713 "is invalid, it should be positive");
714 }
Liang Li85de8322015-03-23 16:32:28 +0800715
Eric Blake7f375e02016-09-08 22:14:16 -0500716 if (params->has_compress_level) {
717 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800718 }
Eric Blake7f375e02016-09-08 22:14:16 -0500719 if (params->has_compress_threads) {
720 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800721 }
Eric Blake7f375e02016-09-08 22:14:16 -0500722 if (params->has_decompress_threads) {
723 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800724 }
Eric Blake7f375e02016-09-08 22:14:16 -0500725 if (params->has_cpu_throttle_initial) {
726 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400727 }
Eric Blake7f375e02016-09-08 22:14:16 -0500728 if (params->has_cpu_throttle_increment) {
729 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400730 }
Eric Blake7f375e02016-09-08 22:14:16 -0500731 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100732 g_free(s->parameters.tls_creds);
Eric Blake7f375e02016-09-08 22:14:16 -0500733 s->parameters.tls_creds = g_strdup(params->tls_creds);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100734 }
Eric Blake7f375e02016-09-08 22:14:16 -0500735 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100736 g_free(s->parameters.tls_hostname);
Eric Blake7f375e02016-09-08 22:14:16 -0500737 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100738 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530739 if (params->has_max_bandwidth) {
740 s->parameters.max_bandwidth = params->max_bandwidth;
741 if (s->to_dst_file) {
742 qemu_file_set_rate_limit(s->to_dst_file,
743 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
744 }
745 }
746 if (params->has_downtime_limit) {
747 s->parameters.downtime_limit = params->downtime_limit;
748 }
zhanghailiang68b53592016-10-27 14:43:01 +0800749
750 if (params->has_x_checkpoint_delay) {
751 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800752 if (migration_in_colo_state()) {
753 colo_checkpoint_notify(s);
754 }
zhanghailiang68b53592016-10-27 14:43:01 +0800755 }
Juan Quintela2833c592017-04-05 18:32:37 +0200756 if (params->has_block_incremental) {
757 s->parameters.block_incremental = params->block_incremental;
758 }
Liang Li85de8322015-03-23 16:32:28 +0800759}
760
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100761
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000762void qmp_migrate_start_postcopy(Error **errp)
763{
764 MigrationState *s = migrate_get_current();
765
766 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000767 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000768 " the start of migration");
769 return;
770 }
771
772 if (s->state == MIGRATION_STATUS_NONE) {
773 error_setg(errp, "Postcopy must be started after migration has been"
774 " started");
775 return;
776 }
777 /*
778 * we don't error if migration has finished since that would be racy
779 * with issuing this command.
780 */
781 atomic_set(&s->start_postcopy, true);
782}
783
aliguori065e2812008-11-11 16:46:33 +0000784/* shared migration helpers */
785
zhanghailiang48781e52015-12-16 11:47:33 +0000786void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000787{
zhanghailiang48781e52015-12-16 11:47:33 +0000788 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200789 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200790 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000791 }
792}
793
Juan Quintela2833c592017-04-05 18:32:37 +0200794void migrate_set_block_enabled(bool value, Error **errp)
795{
796 MigrationCapabilityStatusList *cap;
797
798 cap = g_new0(MigrationCapabilityStatusList, 1);
799 cap->value = g_new0(MigrationCapabilityStatus, 1);
800 cap->value->capability = MIGRATION_CAPABILITY_BLOCK;
801 cap->value->state = value;
802 qmp_migrate_set_capabilities(cap, errp);
803 qapi_free_MigrationCapabilityStatusList(cap);
804}
805
806static void migrate_set_block_incremental(MigrationState *s, bool value)
807{
808 s->parameters.block_incremental = value;
809}
810
811static void block_cleanup_parameters(MigrationState *s)
812{
813 if (s->must_remove_block_options) {
814 /* setting to false can never fail */
815 migrate_set_block_enabled(false, &error_abort);
816 migrate_set_block_incremental(s, false);
817 s->must_remove_block_options = false;
818 }
819}
820
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100821static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000822{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100823 MigrationState *s = opaque;
824
825 qemu_bh_delete(s->cleanup_bh);
826 s->cleanup_bh = NULL;
827
zhanghailiang89a02a92016-01-15 11:37:42 +0800828 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100829 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100830 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000831 if (s->migration_thread_running) {
832 qemu_thread_join(&s->thread);
833 s->migration_thread_running = false;
834 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100835 qemu_mutex_lock_iothread();
836
Liang Li8706d2d2015-03-23 16:32:17 +0800837 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800838 qemu_fclose(s->to_dst_file);
839 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000840 }
841
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000842 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
843 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100844
Liang Li94f5a432015-11-02 15:37:00 +0800845 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000846 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800847 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100848 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100849
850 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200851 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000852}
853
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100854void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000855{
Peter Maydell25174052016-10-21 18:41:45 +0100856 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +0800857 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000858 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
859 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100860 if (!s->error) {
861 s->error = error_copy(error);
862 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100863 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200864 block_cleanup_parameters(s);
Juan Quintela458cf282011-02-22 23:32:54 +0100865}
866
Juan Quintela0edda1c2010-05-11 16:28:39 +0200867static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000868{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000869 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800870 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100871 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000872
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000873 if (s->rp_state.from_dst_file) {
874 /* shutdown the rp socket, so causing the rp thread to shutdown */
875 qemu_file_shutdown(s->rp_state.from_dst_file);
876 }
877
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000878 do {
879 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000880 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000881 break;
882 }
zhanghailiang48781e52015-12-16 11:47:33 +0000883 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +0800884 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000885
886 /*
887 * If we're unlucky the migration code might be stuck somewhere in a
888 * send/write while the network has failed and is waiting to timeout;
889 * if we've got shutdown(2) available then we can force it to quit.
890 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
891 * called in a bh, so there is no race against this cancel.
892 */
zhanghailiang31194732015-03-13 16:08:38 +0800893 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000894 qemu_file_shutdown(f);
895 }
zhanghailiang1d2acc32017-01-24 15:59:52 +0800896 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
897 Error *local_err = NULL;
898
899 bdrv_invalidate_cache_all(&local_err);
900 if (local_err) {
901 error_report_err(local_err);
902 } else {
903 s->block_inactive = false;
904 }
905 }
Juan Quintela2833c592017-04-05 18:32:37 +0200906 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000907}
908
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100909void add_migration_state_change_notifier(Notifier *notify)
910{
911 notifier_list_add(&migration_state_notifiers, notify);
912}
913
914void remove_migration_state_change_notifier(Notifier *notify)
915{
Paolo Bonzini31552522012-01-13 17:34:01 +0100916 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100917}
918
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200919bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200920{
zhanghailiang31194732015-03-13 16:08:38 +0800921 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200922}
923
Juan Quintela70736932011-02-23 00:43:59 +0100924bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100925{
zhanghailiang31194732015-03-13 16:08:38 +0800926 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100927}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200928
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200929bool migration_has_failed(MigrationState *s)
930{
zhanghailiang31194732015-03-13 16:08:38 +0800931 return (s->state == MIGRATION_STATUS_CANCELLED ||
932 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200933}
934
Juan Quintela57273092017-03-20 22:25:28 +0100935bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000936{
Juan Quintela57273092017-03-20 22:25:28 +0100937 MigrationState *s = migrate_get_current();
938
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000939 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
940}
941
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000942bool migration_in_postcopy_after_devices(MigrationState *s)
943{
Juan Quintela57273092017-03-20 22:25:28 +0100944 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000945}
946
Juan Quintelafab35002017-03-22 17:36:57 +0100947bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530948{
Juan Quintelafab35002017-03-22 17:36:57 +0100949 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530950
951 switch (s->state) {
952 case MIGRATION_STATUS_NONE:
953 case MIGRATION_STATUS_CANCELLED:
954 case MIGRATION_STATUS_COMPLETED:
955 case MIGRATION_STATUS_FAILED:
956 return true;
957 case MIGRATION_STATUS_SETUP:
958 case MIGRATION_STATUS_CANCELLING:
959 case MIGRATION_STATUS_ACTIVE:
960 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
961 case MIGRATION_STATUS_COLO:
962 return false;
963 case MIGRATION_STATUS__MAX:
964 g_assert_not_reached();
965 }
966
967 return false;
968}
969
Juan Quintelaa0762d92017-04-05 21:00:09 +0200970MigrationState *migrate_init(void)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200971{
Juan Quintela17549e82011-10-05 13:50:43 +0200972 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300973
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000974 /*
975 * Reinitialise all migration state, except
976 * parameters/capabilities that the user set, and
977 * locks.
978 */
979 s->bytes_xfer = 0;
980 s->xfer_limit = 0;
981 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +0800982 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000983 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000984 s->rp_state.from_dst_file = NULL;
985 s->rp_state.error = false;
986 s->mbps = 0.0;
987 s->downtime = 0;
988 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000989 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000990 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000991 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000992 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100993 error_free(s->error);
994 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +0100995
zhanghailiang48781e52015-12-16 11:47:33 +0000996 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200997
Alex Blighbc72ad62013-08-21 16:03:08 +0100998 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200999 return s;
1000}
Juan Quintelacab30142011-02-22 23:54:21 +01001001
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001002static GSList *migration_blockers;
1003
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301004int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001005{
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301006 if (only_migratable) {
1007 error_propagate(errp, error_copy(reason));
1008 error_prepend(errp, "disallowing migration blocker "
1009 "(--only_migratable) for: ");
1010 return -EACCES;
1011 }
1012
Juan Quintelafab35002017-03-22 17:36:57 +01001013 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301014 migration_blockers = g_slist_prepend(migration_blockers, reason);
1015 return 0;
1016 }
1017
1018 error_propagate(errp, error_copy(reason));
1019 error_prepend(errp, "disallowing migration blocker (migration in "
1020 "progress) for: ");
1021 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001022}
1023
1024void migrate_del_blocker(Error *reason)
1025{
1026 migration_blockers = g_slist_remove(migration_blockers, reason);
1027}
1028
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001029void qmp_migrate_incoming(const char *uri, Error **errp)
1030{
1031 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001032 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001033
1034 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001035 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001036 return;
1037 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001038 if (!once) {
1039 error_setg(errp, "The incoming migration has already been started");
1040 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001041
1042 qemu_start_incoming_migration(uri, &local_err);
1043
1044 if (local_err) {
1045 error_propagate(errp, local_err);
1046 return;
1047 }
1048
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001049 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001050}
1051
Greg Kurz24f39022016-05-04 21:44:19 +02001052bool migration_is_blocked(Error **errp)
1053{
1054 if (qemu_savevm_state_blocked(errp)) {
1055 return true;
1056 }
1057
1058 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001059 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001060 return true;
1061 }
1062
1063 return false;
1064}
1065
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001066void qmp_migrate(const char *uri, bool has_blk, bool blk,
1067 bool has_inc, bool inc, bool has_detach, bool detach,
1068 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001069{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001070 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001071 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001072 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001073
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001074 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001075 s->state == MIGRATION_STATUS_CANCELLING ||
1076 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001077 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001078 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001079 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001080 if (runstate_check(RUN_STATE_INMIGRATE)) {
1081 error_setg(errp, "Guest is waiting for an incoming migration");
1082 return;
1083 }
1084
Greg Kurz24f39022016-05-04 21:44:19 +02001085 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001086 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001087 }
1088
Juan Quintela2833c592017-04-05 18:32:37 +02001089 if ((has_blk && blk) || (has_inc && inc)) {
1090 if (migrate_use_block() || migrate_use_block_incremental()) {
1091 error_setg(errp, "Command options are incompatible with "
1092 "current migration capabilities");
1093 return;
1094 }
1095 migrate_set_block_enabled(true, &local_err);
1096 if (local_err) {
1097 error_propagate(errp, local_err);
1098 return;
1099 }
1100 s->must_remove_block_options = true;
1101 }
1102
1103 if (has_inc && inc) {
1104 migrate_set_block_incremental(s, true);
1105 }
1106
Juan Quintelaa0762d92017-04-05 21:00:09 +02001107 s = migrate_init();
Juan Quintelacab30142011-02-22 23:54:21 +01001108
1109 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001110 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001111#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001112 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001113 rdma_start_outgoing_migration(s, p, &local_err);
1114#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001115 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001116 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001117 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001118 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001119 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001120 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001121 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001122 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1123 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001124 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1125 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001126 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001127 }
1128
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001129 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001130 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001131 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001132 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001133 }
Juan Quintelacab30142011-02-22 23:54:21 +01001134}
1135
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001136void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001137{
Juan Quintela17549e82011-10-05 13:50:43 +02001138 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001139}
1140
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001141void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1142{
1143 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001144 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001145
1146 /* Check for truncation */
1147 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001148 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1149 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001150 return;
1151 }
1152
Orit Wassermana5615b12014-01-30 20:08:36 +02001153 /* Cache should not be larger than guest ram size */
1154 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001155 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1156 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001157 return;
1158 }
1159
Orit Wassermanc91e6812014-01-30 20:08:34 +02001160 new_size = xbzrle_cache_resize(value);
1161 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001162 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1163 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001164 return;
1165 }
1166
1167 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001168}
1169
1170int64_t qmp_query_migrate_cache_size(Error **errp)
1171{
1172 return migrate_xbzrle_cache_size();
1173}
1174
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001175void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001176{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301177 MigrationParameters p = {
1178 .has_max_bandwidth = true,
1179 .max_bandwidth = value,
1180 };
Juan Quintelacab30142011-02-22 23:54:21 +01001181
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301182 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001183}
1184
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001185void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001186{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001187 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1188 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1189 "the range of 0 to %d seconds",
1190 MAX_MIGRATE_DOWNTIME_SECONDS);
1191 return;
1192 }
1193
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301194 value *= 1000; /* Convert to milliseconds */
1195 value = MAX(0, MIN(INT64_MAX, value));
1196
1197 MigrationParameters p = {
1198 .has_downtime_limit = true,
1199 .downtime_limit = value,
1200 };
1201
1202 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001203}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001204
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001205bool migrate_release_ram(void)
1206{
1207 MigrationState *s;
1208
1209 s = migrate_get_current();
1210
1211 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1212}
1213
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001214bool migrate_postcopy_ram(void)
1215{
1216 MigrationState *s;
1217
1218 s = migrate_get_current();
1219
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001220 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001221}
1222
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001223bool migrate_auto_converge(void)
1224{
1225 MigrationState *s;
1226
1227 s = migrate_get_current();
1228
1229 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1230}
1231
Peter Lieven323004a2013-07-18 09:48:50 +02001232bool migrate_zero_blocks(void)
1233{
1234 MigrationState *s;
1235
1236 s = migrate_get_current();
1237
1238 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1239}
1240
Liang Li8706d2d2015-03-23 16:32:17 +08001241bool migrate_use_compression(void)
1242{
Liang Lidde4e692015-03-23 16:32:26 +08001243 MigrationState *s;
1244
1245 s = migrate_get_current();
1246
1247 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001248}
1249
1250int migrate_compress_level(void)
1251{
1252 MigrationState *s;
1253
1254 s = migrate_get_current();
1255
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001256 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001257}
1258
1259int migrate_compress_threads(void)
1260{
1261 MigrationState *s;
1262
1263 s = migrate_get_current();
1264
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001265 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001266}
1267
Liang Li3fcb38c2015-03-23 16:32:18 +08001268int migrate_decompress_threads(void)
1269{
1270 MigrationState *s;
1271
1272 s = migrate_get_current();
1273
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001274 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001275}
1276
Juan Quintelab05dc722015-07-07 14:44:05 +02001277bool migrate_use_events(void)
1278{
1279 MigrationState *s;
1280
1281 s = migrate_get_current();
1282
1283 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1284}
1285
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001286int migrate_use_xbzrle(void)
1287{
1288 MigrationState *s;
1289
1290 s = migrate_get_current();
1291
1292 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1293}
1294
1295int64_t migrate_xbzrle_cache_size(void)
1296{
1297 MigrationState *s;
1298
1299 s = migrate_get_current();
1300
1301 return s->xbzrle_cache_size;
1302}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001303
Juan Quintela2833c592017-04-05 18:32:37 +02001304bool migrate_use_block(void)
1305{
1306 MigrationState *s;
1307
1308 s = migrate_get_current();
1309
1310 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1311}
1312
1313bool migrate_use_block_incremental(void)
1314{
1315 MigrationState *s;
1316
1317 s = migrate_get_current();
1318
1319 return s->parameters.block_incremental;
1320}
1321
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001322/* migration thread support */
1323/*
1324 * Something bad happened to the RP stream, mark an error
1325 * The caller shall print or trace something to indicate why
1326 */
1327static void mark_source_rp_bad(MigrationState *s)
1328{
1329 s->rp_state.error = true;
1330}
1331
1332static struct rp_cmd_args {
1333 ssize_t len; /* -1 = variable */
1334 const char *name;
1335} rp_cmd_args[] = {
1336 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1337 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1338 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001339 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1340 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001341 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1342};
1343
1344/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001345 * Process a request for pages received on the return path,
1346 * We're allowed to send more than requested (e.g. to round to our page size)
1347 * and we don't need to send pages that have already been sent.
1348 */
1349static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1350 ram_addr_t start, size_t len)
1351{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001352 long our_host_ps = getpagesize();
1353
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001354 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001355
1356 /*
1357 * Since we currently insist on matching page sizes, just sanity check
1358 * we're being asked for whole host pages.
1359 */
1360 if (start & (our_host_ps-1) ||
1361 (len & (our_host_ps-1))) {
1362 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1363 " len: %zd", __func__, start, len);
1364 mark_source_rp_bad(ms);
1365 return;
1366 }
1367
Juan Quintela96506892017-03-14 18:41:03 +01001368 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001369 mark_source_rp_bad(ms);
1370 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001371}
1372
1373/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001374 * Handles messages sent on the return path towards the source VM
1375 *
1376 */
1377static void *source_return_path_thread(void *opaque)
1378{
1379 MigrationState *ms = opaque;
1380 QEMUFile *rp = ms->rp_state.from_dst_file;
1381 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001382 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001383 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001384 ram_addr_t start = 0; /* =0 to silence warning */
1385 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001386 int res;
1387
1388 trace_source_return_path_thread_entry();
1389 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1390 migration_is_setup_or_active(ms->state)) {
1391 trace_source_return_path_thread_loop_top();
1392 header_type = qemu_get_be16(rp);
1393 header_len = qemu_get_be16(rp);
1394
1395 if (header_type >= MIG_RP_MSG_MAX ||
1396 header_type == MIG_RP_MSG_INVALID) {
1397 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1398 header_type, header_len);
1399 mark_source_rp_bad(ms);
1400 goto out;
1401 }
1402
1403 if ((rp_cmd_args[header_type].len != -1 &&
1404 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001405 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001406 error_report("RP: Received '%s' message (0x%04x) with"
1407 "incorrect length %d expecting %zu",
1408 rp_cmd_args[header_type].name, header_type, header_len,
1409 (size_t)rp_cmd_args[header_type].len);
1410 mark_source_rp_bad(ms);
1411 goto out;
1412 }
1413
1414 /* We know we've got a valid header by this point */
1415 res = qemu_get_buffer(rp, buf, header_len);
1416 if (res != header_len) {
1417 error_report("RP: Failed reading data for message 0x%04x"
1418 " read %d expected %d",
1419 header_type, res, header_len);
1420 mark_source_rp_bad(ms);
1421 goto out;
1422 }
1423
1424 /* OK, we have the message and the data */
1425 switch (header_type) {
1426 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001427 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001428 trace_source_return_path_thread_shut(sibling_error);
1429 if (sibling_error) {
1430 error_report("RP: Sibling indicated error %d", sibling_error);
1431 mark_source_rp_bad(ms);
1432 }
1433 /*
1434 * We'll let the main thread deal with closing the RP
1435 * we could do a shutdown(2) on it, but we're the only user
1436 * anyway, so there's nothing gained.
1437 */
1438 goto out;
1439
1440 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001441 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001442 trace_source_return_path_thread_pong(tmp32);
1443 break;
1444
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001445 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001446 start = ldq_be_p(buf);
1447 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001448 migrate_handle_rp_req_pages(ms, NULL, start, len);
1449 break;
1450
1451 case MIG_RP_MSG_REQ_PAGES_ID:
1452 expected_len = 12 + 1; /* header + termination */
1453
1454 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001455 start = ldq_be_p(buf);
1456 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001457 /* Now we expect an idstr */
1458 tmp32 = buf[12]; /* Length of the following idstr */
1459 buf[13 + tmp32] = '\0';
1460 expected_len += tmp32;
1461 }
1462 if (header_len != expected_len) {
1463 error_report("RP: Req_Page_id with length %d expecting %zd",
1464 header_len, expected_len);
1465 mark_source_rp_bad(ms);
1466 goto out;
1467 }
1468 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1469 break;
1470
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001471 default:
1472 break;
1473 }
1474 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001475 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001476 trace_source_return_path_thread_bad_end();
1477 mark_source_rp_bad(ms);
1478 }
1479
1480 trace_source_return_path_thread_end();
1481out:
1482 ms->rp_state.from_dst_file = NULL;
1483 qemu_fclose(rp);
1484 return NULL;
1485}
1486
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001487static int open_return_path_on_source(MigrationState *ms)
1488{
1489
zhanghailiang89a02a92016-01-15 11:37:42 +08001490 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001491 if (!ms->rp_state.from_dst_file) {
1492 return -1;
1493 }
1494
1495 trace_open_return_path_on_source();
1496 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1497 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1498
1499 trace_open_return_path_on_source_continue();
1500
1501 return 0;
1502}
1503
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001504/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1505static int await_return_path_close_on_source(MigrationState *ms)
1506{
1507 /*
1508 * If this is a normal exit then the destination will send a SHUT and the
1509 * rp_thread will exit, however if there's an error we need to cause
1510 * it to exit.
1511 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001512 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001513 /*
1514 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1515 * waiting for the destination.
1516 */
1517 qemu_file_shutdown(ms->rp_state.from_dst_file);
1518 mark_source_rp_bad(ms);
1519 }
1520 trace_await_return_path_close_on_source_joining();
1521 qemu_thread_join(&ms->rp_state.rp_thread);
1522 trace_await_return_path_close_on_source_close();
1523 return ms->rp_state.error;
1524}
1525
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001526/*
1527 * Switch from normal iteration to postcopy
1528 * Returns non-0 on error
1529 */
1530static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1531{
1532 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001533 QIOChannelBuffer *bioc;
1534 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001535 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001536 bool restart_block = false;
zhanghailiang48781e52015-12-16 11:47:33 +00001537 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001538 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1539
1540 trace_postcopy_start();
1541 qemu_mutex_lock_iothread();
1542 trace_postcopy_start_set_run();
1543
1544 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1545 *old_vm_running = runstate_is_running();
1546 global_state_store();
1547 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001548 if (ret < 0) {
1549 goto fail;
1550 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001551
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001552 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001553 if (ret < 0) {
1554 goto fail;
1555 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001556 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001557
1558 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001559 * Cause any non-postcopiable, but iterative devices to
1560 * send out their final data.
1561 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001562 qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001563
1564 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001565 * in Finish migrate and with the io-lock held everything should
1566 * be quiet, but we've potentially still got dirty pages and we
1567 * need to tell the destination to throw any pages it's already received
1568 * that are dirty
1569 */
1570 if (ram_postcopy_send_discard_bitmap(ms)) {
1571 error_report("postcopy send discard bitmap failed");
1572 goto fail;
1573 }
1574
1575 /*
1576 * send rest of state - note things that are doing postcopy
1577 * will notice we're in POSTCOPY_ACTIVE and not actually
1578 * wrap their state up here
1579 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001580 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001581 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001582 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001583
1584 /*
1585 * While loading the device state we may trigger page transfer
1586 * requests and the fd must be free to process those, and thus
1587 * the destination must read the whole device state off the fd before
1588 * it starts processing it. Unfortunately the ad-hoc migration format
1589 * doesn't allow the destination to know the size to read without fully
1590 * parsing it through each devices load-state code (especially the open
1591 * coded devices that use get/put).
1592 * So we wrap the device state up in a package with a length at the start;
1593 * to do this we use a qemu_buf to hold the whole of the device state.
1594 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001595 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001596 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001597 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1598 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001599
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001600 /*
1601 * Make sure the receiver can get incoming pages before we send the rest
1602 * of the state
1603 */
1604 qemu_savevm_send_postcopy_listen(fb);
1605
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001606 qemu_savevm_state_complete_precopy(fb, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001607 qemu_savevm_send_ping(fb, 3);
1608
1609 qemu_savevm_send_postcopy_run(fb);
1610
1611 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001612
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001613 /* Last point of recovery; as soon as we send the package the destination
1614 * can open devices and potentially start running.
1615 * Lets just check again we've not got any errors.
1616 */
1617 ret = qemu_file_get_error(ms->to_dst_file);
1618 if (ret) {
1619 error_report("postcopy_start: Migration stream errored (pre package)");
1620 goto fail_closefb;
1621 }
1622
1623 restart_block = false;
1624
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001625 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001626 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001627 goto fail_closefb;
1628 }
1629 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001630
1631 /* Send a notify to give a chance for anything that needs to happen
1632 * at the transition to postcopy and after the device state; in particular
1633 * spice needs to trigger a transition now
1634 */
1635 ms->postcopy_after_devices = true;
1636 notifier_list_notify(&migration_state_notifiers, ms);
1637
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001638 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1639
1640 qemu_mutex_unlock_iothread();
1641
1642 /*
1643 * Although this ping is just for debug, it could potentially be
1644 * used for getting a better measurement of downtime at the source.
1645 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001646 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001647
Pavel Butsykinced1c612017-02-03 18:23:21 +03001648 if (migrate_release_ram()) {
1649 ram_postcopy_migrated_memory_release(ms);
1650 }
1651
zhanghailiang89a02a92016-01-15 11:37:42 +08001652 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001653 if (ret) {
1654 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001655 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001656 MIGRATION_STATUS_FAILED);
1657 }
1658
1659 return ret;
1660
1661fail_closefb:
1662 qemu_fclose(fb);
1663fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001664 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001665 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001666 if (restart_block) {
1667 /* A failure happened early enough that we know the destination hasn't
1668 * accessed block devices, so we're safe to recover.
1669 */
1670 Error *local_err = NULL;
1671
1672 bdrv_invalidate_cache_all(&local_err);
1673 if (local_err) {
1674 error_report_err(local_err);
1675 }
1676 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001677 qemu_mutex_unlock_iothread();
1678 return -1;
1679}
1680
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001681/**
1682 * migration_completion: Used by migration_thread when there's not much left.
1683 * The caller 'breaks' the loop when this returns.
1684 *
1685 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001686 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001687 * @*old_vm_running: Pointer to old_vm_running flag
1688 * @*start_time: Pointer to time to update
1689 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001690static void migration_completion(MigrationState *s, int current_active_state,
1691 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001692 int64_t *start_time)
1693{
1694 int ret;
1695
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001696 if (s->state == MIGRATION_STATUS_ACTIVE) {
1697 qemu_mutex_lock_iothread();
1698 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1699 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1700 *old_vm_running = runstate_is_running();
1701 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001702
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001703 if (!ret) {
1704 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001705 if (ret >= 0) {
1706 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1707 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
1708 }
zhanghailiang0b827d52016-10-27 14:42:54 +08001709 /*
1710 * Don't mark the image with BDRV_O_INACTIVE flag if
1711 * we will go into COLO stage later.
1712 */
1713 if (ret >= 0 && !migrate_colo_enabled()) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001714 ret = bdrv_inactivate_all();
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001715 if (ret >= 0) {
1716 s->block_inactive = true;
1717 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001718 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001719 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001720 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001721
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001722 if (ret < 0) {
1723 goto fail;
1724 }
1725 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1726 trace_migration_completion_postcopy_end();
1727
zhanghailiang89a02a92016-01-15 11:37:42 +08001728 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001729 trace_migration_completion_postcopy_end_after_complete();
1730 }
1731
1732 /*
1733 * If rp was opened we must clean up the thread before
1734 * cleaning everything else up (since if there are no failures
1735 * it will wait for the destination to send it's status in
1736 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001737 */
Peter Xu0425dc92017-05-31 18:35:34 +08001738 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001739 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08001740 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001741 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08001742 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001743 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001744 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001745 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001746 }
1747
zhanghailiang89a02a92016-01-15 11:37:42 +08001748 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001749 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001750 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001751 }
1752
zhanghailiang0b827d52016-10-27 14:42:54 +08001753 if (!migrate_colo_enabled()) {
1754 migrate_set_state(&s->state, current_active_state,
1755 MIGRATION_STATUS_COMPLETED);
1756 }
1757
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001758 return;
1759
Greg Kurzfe904ea2016-05-18 15:44:36 +02001760fail_invalidate:
1761 /* If not doing postcopy, vm_start() will be called: let's regain
1762 * control on images.
1763 */
1764 if (s->state == MIGRATION_STATUS_ACTIVE) {
1765 Error *local_err = NULL;
1766
zhanghailiang1d2acc32017-01-24 15:59:52 +08001767 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001768 bdrv_invalidate_cache_all(&local_err);
1769 if (local_err) {
1770 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001771 } else {
1772 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02001773 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001774 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001775 }
1776
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001777fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001778 migrate_set_state(&s->state, current_active_state,
1779 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001780}
1781
zhanghailiang35a6ed42016-10-27 14:42:52 +08001782bool migrate_colo_enabled(void)
1783{
1784 MigrationState *s = migrate_get_current();
1785 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
1786}
1787
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001788/*
1789 * Master migration thread on the source VM.
1790 * It drives the migration and pumps the data down the outgoing channel.
1791 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001792static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001793{
Juan Quintela9848a402012-12-19 09:55:50 +01001794 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001795 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001796 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1797 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001798 int64_t initial_bytes = 0;
Peter Xufaec0662017-04-01 16:18:43 +08001799 /*
1800 * The final stage happens when the remaining data is smaller than
1801 * this threshold; it's calculated from the requested downtime and
1802 * measured bandwidth
1803 */
1804 int64_t threshold_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001805 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001806 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001807 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001808 bool entered_postcopy = false;
1809 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1810 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang0b827d52016-10-27 14:42:54 +08001811 bool enable_colo = migrate_colo_enabled();
Juan Quintela76f59332012-10-03 20:16:24 +02001812
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001813 rcu_register_thread();
1814
zhanghailiang89a02a92016-01-15 11:37:42 +08001815 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001816
Peter Xu62a02652017-06-14 15:55:58 +08001817 /*
1818 * If we opened the return path, we need to make sure dst has it
1819 * opened as well.
1820 */
1821 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001822 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001823 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001824
1825 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001826 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08001827 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001828
Peter Xu0425dc92017-05-31 18:35:34 +08001829 if (migrate_postcopy_ram()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001830 /*
1831 * Tell the destination that we *might* want to do postcopy later;
1832 * if the other end can't do postcopy it should fail now, nice and
1833 * early.
1834 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001835 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001836 }
1837
Juan Quintelaa0762d92017-04-05 21:00:09 +02001838 qemu_savevm_state_begin(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001839
Alex Blighbc72ad62013-08-21 16:03:08 +01001840 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00001841 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1842 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001843
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001844 trace_migration_thread_setup_complete();
1845
1846 while (s->state == MIGRATION_STATUS_ACTIVE ||
1847 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001848 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001849 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001850
zhanghailiang89a02a92016-01-15 11:37:42 +08001851 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001852 uint64_t pend_post, pend_nonpost;
1853
Peter Xufaec0662017-04-01 16:18:43 +08001854 qemu_savevm_state_pending(s->to_dst_file, threshold_size,
1855 &pend_nonpost, &pend_post);
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001856 pending_size = pend_nonpost + pend_post;
Peter Xufaec0662017-04-01 16:18:43 +08001857 trace_migrate_pending(pending_size, threshold_size,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001858 pend_post, pend_nonpost);
Peter Xufaec0662017-04-01 16:18:43 +08001859 if (pending_size && pending_size >= threshold_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001860 /* Still a significant amount to transfer */
1861
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001862 if (migrate_postcopy_ram() &&
1863 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
Peter Xufaec0662017-04-01 16:18:43 +08001864 pend_nonpost <= threshold_size &&
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001865 atomic_read(&s->start_postcopy)) {
1866
1867 if (!postcopy_start(s, &old_vm_running)) {
1868 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1869 entered_postcopy = true;
1870 }
1871
1872 continue;
1873 }
1874 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001875 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001876 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001877 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001878 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001879 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001880 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001881 }
1882 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001883
zhanghailiang89a02a92016-01-15 11:37:42 +08001884 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001885 migrate_set_state(&s->state, current_active_state,
1886 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001887 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001888 break;
1889 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001890 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001891 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001892 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1893 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001894 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001895 double bandwidth = (double)transferred_bytes / time_spent;
Peter Xufaec0662017-04-01 16:18:43 +08001896 threshold_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001897
Wei Yang5b648de2016-01-24 14:09:57 +00001898 s->mbps = (((double) transferred_bytes * 8.0) /
1899 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04001900
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001901 trace_migrate_transferred(transferred_bytes, time_spent,
Peter Xufaec0662017-04-01 16:18:43 +08001902 bandwidth, threshold_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001903 /* if we haven't sent anything, we don't want to recalculate
1904 10000 is a small enough number for our purposes */
Juan Quintela93604472017-06-06 19:49:03 +02001905 if (ram_counters.dirty_pages_rate && transferred_bytes > 10000) {
1906 s->expected_downtime = ram_counters.dirty_pages_rate *
Juan Quintela20afaed2017-03-21 09:09:14 +01001907 qemu_target_page_size() / bandwidth;
Juan Quintela90f8ae72013-02-01 13:22:37 +01001908 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001909
zhanghailiang89a02a92016-01-15 11:37:42 +08001910 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001911 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08001912 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001913 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001914 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001915 /* usleep expects microseconds */
1916 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1917 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001918 }
1919
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001920 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001921 /* If we enabled cpu throttling for auto-converge, turn it off. */
1922 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001923 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001924
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001925 qemu_mutex_lock_iothread();
zhanghailiang0b827d52016-10-27 14:42:54 +08001926 /*
1927 * The resource has been allocated by migration will be reused in COLO
1928 * process, so don't release them.
1929 */
1930 if (!enable_colo) {
1931 qemu_savevm_state_cleanup();
1932 }
zhanghailiang31194732015-03-13 16:08:38 +08001933 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001934 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001935 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001936 if (!entered_postcopy) {
1937 s->downtime = end_time - start_time;
1938 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001939 if (s->total_time) {
1940 s->mbps = (((double) transferred_bytes * 8.0) /
1941 ((double) s->total_time)) / 1000;
1942 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001943 runstate_set(RUN_STATE_POSTMIGRATE);
1944 } else {
zhanghailiang0b827d52016-10-27 14:42:54 +08001945 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
1946 migrate_start_colo_process(s);
1947 qemu_savevm_state_cleanup();
1948 /*
1949 * Fixme: we will run VM in COLO no matter its old running state.
1950 * After exited COLO, we will keep running.
1951 */
1952 old_vm_running = true;
1953 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001954 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001955 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01001956 } else {
1957 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
1958 runstate_set(RUN_STATE_POSTMIGRATE);
1959 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001960 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001961 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001962 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001963 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001964
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001965 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001966 return NULL;
1967}
1968
Juan Quintela9848a402012-12-19 09:55:50 +01001969void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001970{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301971 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001972 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001973
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01001974 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08001975 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301976 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01001977
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001978 /* Notify before starting migration thread */
1979 notifier_list_notify(&migration_state_notifiers, s);
1980
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001981 /*
1982 * Open the return path; currently for postcopy but other things might
1983 * also want it.
1984 */
1985 if (migrate_postcopy_ram()) {
1986 if (open_return_path_on_source(s)) {
1987 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00001988 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001989 MIGRATION_STATUS_FAILED);
1990 migrate_fd_cleanup(s);
1991 return;
1992 }
1993 }
1994
Liang Li8706d2d2015-03-23 16:32:17 +08001995 migrate_compress_threads_create();
Pankaj Gupta009fad72017-01-23 19:12:56 +05301996 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001997 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001998 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001999}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002000