blob: 414e14db6cf8eaf35f62f0f83f889a55e713bcc1 [file] [log] [blame]
aliguori5bb79102008-10-13 03:12:02 +00001/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori5bb79102008-10-13 03:12:02 +000014 */
15
Peter Maydell1393a482016-01-26 18:16:54 +000016#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020019#include "migration/blocker.h"
Juan Quintelaf4dbe1b2017-04-05 15:54:10 +020020#include "exec.h"
Juan Quintela7fcac4a2017-04-05 15:58:29 +020021#include "fd.h"
Juan Quintela61e8b142017-04-05 17:40:11 +020022#include "socket.h"
Juan Quintelae1a3ece2017-04-17 20:32:36 +020023#include "rdma.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020024#include "ram.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020025#include "migration/global_state.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020026#include "migration/misc.h"
Juan Quintela6666c962017-04-24 20:07:27 +020027#include "migration.h"
Juan Quintela20a519a2017-04-20 14:48:46 +020028#include "savevm.h"
Juan Quintela40014d82017-04-17 19:34:36 +020029#include "qemu-file-channel.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020030#include "qemu-file.h"
Juan Quintela987772d2017-04-17 19:02:59 +020031#include "migration/vmstate.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010032#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010033#include "qapi/qmp/qerror.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000034#include "qapi/util.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020035#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020036#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020037#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020038#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030039#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010040#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020041#include "qapi-event.h"
Juan Quintela51180422017-04-24 20:50:19 +020042#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010043#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080044#include "migration/colo.h"
aliguori065e2812008-11-11 16:46:33 +000045
Jason J. Hernedc325622015-09-08 13:12:37 -040046#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000047
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010048/* Amount of time to allocate to each "chunk" of bandwidth-throttled
49 * data. */
50#define BUFFER_DELAY 100
51#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
52
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053053/* Time in milliseconds we are allowed to stop the source,
54 * for sending the last part */
55#define DEFAULT_MIGRATE_SET_DOWNTIME 300
56
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030057/* Maximum migrate downtime set to 2000 seconds */
58#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
59#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
60
Liang Li8706d2d2015-03-23 16:32:17 +080061/* Default compression thread count */
62#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080063/* Default decompression thread count, usually decompression is at
64 * least 4 times as fast as compression.*/
65#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080066/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
67#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040068/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040069#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
70#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080071
Orit Wasserman17ad9b32012-08-06 21:42:53 +030072/* Migration XBZRLE default cache size */
73#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
74
zhanghailiang68b53592016-10-27 14:43:01 +080075/* The delay time (in ms) between two COLO checkpoints
76 * Note: Please change this default value to 10000 when we support hybrid mode.
77 */
78#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
79
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010080static NotifierList migration_state_notifiers =
81 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
82
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000083static bool deferred_incoming;
84
Juan Quintelada6f1792017-04-24 17:37:14 +020085/* Messages sent on the return path from destination to source */
86enum mig_rp_message_type {
87 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
88 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
89 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
90
91 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
92 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
93
94 MIG_RP_MSG_MAX
95};
96
Juan Quintela17549e82011-10-05 13:50:43 +020097/* When we add fault tolerance, we could have several
98 migrations at once. For now we don't need to add
99 dynamic creation of migration */
100
Peter Xue5cb7e72017-06-27 12:10:13 +0800101static MigrationState *current_migration;
102
103void migration_object_init(void)
104{
105 /* This can only be called once. */
106 assert(!current_migration);
107 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
108}
109
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100110/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200111MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200112{
Peter Xue5cb7e72017-06-27 12:10:13 +0800113 /* This can only be called after the object created. */
114 assert(current_migration);
115 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200116}
117
Peter Xu3df663e2017-06-27 12:10:15 +0800118void migration_only_migratable_set(void)
119{
120 migrate_get_current()->only_migratable = true;
121}
122
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100123MigrationIncomingState *migration_incoming_get_current(void)
124{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100125 static bool once;
126 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100127
Juan Quintelab4b076d2017-01-23 22:32:06 +0100128 if (!once) {
129 mis_current.state = MIGRATION_STATUS_NONE;
130 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100131 qemu_mutex_init(&mis_current.rp_mutex);
132 qemu_event_init(&mis_current.main_thread_load_event, false);
133 once = true;
134 }
135 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100136}
137
138void migration_incoming_state_destroy(void)
139{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100140 struct MigrationIncomingState *mis = migration_incoming_get_current();
141
Peter Xu34826552017-05-19 14:43:29 +0800142 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800143 /* Tell source that we are done */
144 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800145 qemu_fclose(mis->to_src_file);
146 mis->to_src_file = NULL;
147 }
148
Peter Xu660819b2017-05-19 14:43:30 +0800149 if (mis->from_src_file) {
150 qemu_fclose(mis->from_src_file);
151 mis->from_src_file = NULL;
152 }
153
Juan Quintelab4b076d2017-01-23 22:32:06 +0100154 qemu_event_destroy(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100155}
156
Juan Quintelab05dc722015-07-07 14:44:05 +0200157static void migrate_generate_event(int new_state)
158{
159 if (migrate_use_events()) {
160 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200161 }
162}
163
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000164/*
165 * Called on -incoming with a defer: uri.
166 * The migration can be started later after any parameters have been
167 * changed.
168 */
169static void deferred_incoming_migration(Error **errp)
170{
171 if (deferred_incoming) {
172 error_setg(errp, "Incoming migration already deferred");
173 }
174 deferred_incoming = true;
175}
176
Juan Quintelada6f1792017-04-24 17:37:14 +0200177/*
178 * Send a message on the return channel back to the source
179 * of the migration.
180 */
181static void migrate_send_rp_message(MigrationIncomingState *mis,
182 enum mig_rp_message_type message_type,
183 uint16_t len, void *data)
184{
185 trace_migrate_send_rp_message((int)message_type, len);
186 qemu_mutex_lock(&mis->rp_mutex);
187 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
188 qemu_put_be16(mis->to_src_file, len);
189 qemu_put_buffer(mis->to_src_file, data, len);
190 qemu_fflush(mis->to_src_file);
191 qemu_mutex_unlock(&mis->rp_mutex);
192}
193
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000194/* Request a range of pages from the source VM at the given
195 * start address.
196 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
197 * as the last request (a name must have been given previously)
198 * Start: Address offset within the RB
199 * Len: Length in bytes required - must be a multiple of pagesize
200 */
201void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
202 ram_addr_t start, size_t len)
203{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100204 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000205 size_t msglen = 12; /* start + len */
206
207 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
208 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
209
210 if (rbname) {
211 int rbname_len = strlen(rbname);
212 assert(rbname_len < 256);
213
214 bufc[msglen++] = rbname_len;
215 memcpy(bufc + msglen, rbname, rbname_len);
216 msglen += rbname_len;
217 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
218 } else {
219 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
220 }
221}
222
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200223void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000224{
aliguori34c9dd82008-10-13 03:14:31 +0000225 const char *p;
226
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200227 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000228 if (!strcmp(uri, "defer")) {
229 deferred_incoming_migration(errp);
230 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200231 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400232#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000233 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400234 rdma_start_incoming_migration(p, errp);
235#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000236 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200237 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000238 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200239 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000240 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200241 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000242 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100243 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200244 }
aliguori5bb79102008-10-13 03:12:02 +0000245}
246
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300247static void process_incoming_migration_bh(void *opaque)
248{
249 Error *local_err = NULL;
250 MigrationIncomingState *mis = opaque;
251
Kevin Wolface21a52017-05-04 18:52:36 +0200252 /* Make sure all file formats flush their mutable metadata.
253 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300254 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200255 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200256 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200257 local_err = NULL;
258 autostart = false;
259 }
260
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300261 /*
262 * This must happen after all error conditions are dealt with and
263 * we're sure the VM is going to be running on this host.
264 */
265 qemu_announce_self();
266
267 /* If global state section was not received or we are in running
268 state, we need to obey autostart. Any other state is set with
269 runstate_set. */
270
271 if (!global_state_received() ||
272 global_state_get_runstate() == RUN_STATE_RUNNING) {
273 if (autostart) {
274 vm_start();
275 } else {
276 runstate_set(RUN_STATE_PAUSED);
277 }
278 } else {
279 runstate_set(global_state_get_runstate());
280 }
281 migrate_decompress_threads_join();
282 /*
283 * This must happen after any state changes since as soon as an external
284 * observer sees this event they might start to prod at the VM assuming
285 * it's ready to use.
286 */
287 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
288 MIGRATION_STATUS_COMPLETED);
289 qemu_bh_delete(mis->bh);
290 migration_incoming_state_destroy();
291}
292
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200293static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200294{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200295 QEMUFile *f = opaque;
Juan Quintelab4b076d2017-01-23 22:32:06 +0100296 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000297 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200298 int ret;
299
Juan Quintelab4b076d2017-01-23 22:32:06 +0100300 mis->from_src_file = f;
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000301 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000302 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000303 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
304 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200305 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100306
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000307 ps = postcopy_state_get();
308 trace_process_incoming_migration_co_end(ret, ps);
309 if (ps != POSTCOPY_INCOMING_NONE) {
310 if (ps == POSTCOPY_INCOMING_ADVISE) {
311 /*
312 * Where a migration had postcopy enabled (and thus went to advise)
313 * but managed to complete within the precopy period, we can use
314 * the normal exit.
315 */
316 postcopy_ram_incoming_cleanup(mis);
317 } else if (ret >= 0) {
318 /*
319 * Postcopy was started, cleanup should happen at the end of the
320 * postcopy thread.
321 */
322 trace_process_incoming_migration_co_postcopy_end_main();
323 return;
324 }
325 /* Else if something went wrong then just fall out of the normal exit */
326 }
327
zhanghailiang25d0c162016-10-27 14:42:55 +0800328 /* we get COLO info, and know if we are in COLO mode */
329 if (!ret && migration_incoming_enable_colo()) {
330 mis->migration_incoming_co = qemu_coroutine_self();
331 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
332 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
333 mis->have_colo_incoming_thread = true;
334 qemu_coroutine_yield();
335
336 /* Wait checkpoint incoming thread exit before free resource */
337 qemu_thread_join(&mis->colo_incoming_thread);
338 }
339
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200340 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000341 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
342 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200343 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800344 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600345 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200346 }
Juan Quintela511c0232010-06-09 14:10:55 +0200347
Laurent Viviere8199e42017-04-12 15:53:11 +0200348 free_xbzrle_decoded_buf();
349
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300350 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
351 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200352}
353
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100354void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200355{
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200356 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200357
Liang Li3fcb38c2015-03-23 16:32:18 +0800358 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100359 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200360 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200361}
362
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000363/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000364 * Send a 'SHUT' message on the return channel with the given value
365 * to indicate that we've finished with the RP. Non-0 value indicates
366 * error.
367 */
368void migrate_send_rp_shut(MigrationIncomingState *mis,
369 uint32_t value)
370{
371 uint32_t buf;
372
373 buf = cpu_to_be32(value);
374 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
375}
376
377/*
378 * Send a 'PONG' message on the return channel with the given value
379 * (normally in response to a 'PING')
380 */
381void migrate_send_rp_pong(MigrationIncomingState *mis,
382 uint32_t value)
383{
384 uint32_t buf;
385
386 buf = cpu_to_be32(value);
387 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
388}
389
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300390MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
391{
392 MigrationCapabilityStatusList *head = NULL;
393 MigrationCapabilityStatusList *caps;
394 MigrationState *s = migrate_get_current();
395 int i;
396
Michael Tokarev387eede2013-10-05 13:18:28 +0400397 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700398 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100399#ifndef CONFIG_LIVE_BLOCK_MIGRATION
400 if (i == MIGRATION_CAPABILITY_BLOCK) {
401 continue;
402 }
403#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800404 if (i == MIGRATION_CAPABILITY_X_COLO && !colo_supported()) {
405 continue;
406 }
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300407 if (head == NULL) {
408 head = g_malloc0(sizeof(*caps));
409 caps = head;
410 } else {
411 caps->next = g_malloc0(sizeof(*caps));
412 caps = caps->next;
413 }
414 caps->value =
415 g_malloc(sizeof(*caps->value));
416 caps->value->capability = i;
417 caps->value->state = s->enabled_capabilities[i];
418 }
419
420 return head;
421}
422
Liang Li85de8322015-03-23 16:32:28 +0800423MigrationParameters *qmp_query_migrate_parameters(Error **errp)
424{
425 MigrationParameters *params;
426 MigrationState *s = migrate_get_current();
427
428 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500429 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100430 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500431 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100432 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500433 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100434 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500435 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100436 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500437 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100438 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Eric Blakede63ab62016-09-08 22:14:15 -0500439 params->has_tls_creds = !!s->parameters.tls_creds;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100440 params->tls_creds = g_strdup(s->parameters.tls_creds);
Eric Blakede63ab62016-09-08 22:14:15 -0500441 params->has_tls_hostname = !!s->parameters.tls_hostname;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100442 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530443 params->has_max_bandwidth = true;
444 params->max_bandwidth = s->parameters.max_bandwidth;
445 params->has_downtime_limit = true;
446 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800447 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800448 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200449 params->has_block_incremental = true;
450 params->block_incremental = s->parameters.block_incremental;
Liang Li85de8322015-03-23 16:32:28 +0800451
452 return params;
453}
454
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000455/*
456 * Return true if we're already in the middle of a migration
457 * (i.e. any of the active or setup states)
458 */
459static bool migration_is_setup_or_active(int state)
460{
461 switch (state) {
462 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000463 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000464 case MIGRATION_STATUS_SETUP:
465 return true;
466
467 default:
468 return false;
469
470 }
471}
472
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100473static void populate_ram_info(MigrationInfo *info, MigrationState *s)
474{
475 info->has_ram = true;
476 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200477 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100478 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200479 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100480 /* legacy value. It is not used anymore */
481 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200482 info->ram->normal = ram_counters.normal;
483 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100484 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100485 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200486 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
487 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800488 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100489
Juan Quintela114f5ae2017-05-04 10:09:21 +0200490 if (migrate_use_xbzrle()) {
491 info->has_xbzrle_cache = true;
492 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
493 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200494 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
495 info->xbzrle_cache->pages = xbzrle_counters.pages;
496 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
497 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
498 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200499 }
500
Juan Quintela338182c2017-05-03 13:16:38 +0200501 if (cpu_throttle_active()) {
502 info->has_cpu_throttle_percentage = true;
503 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
504 }
505
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100506 if (s->state != MIGRATION_STATUS_COMPLETED) {
507 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200508 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100509 }
510}
511
Juan Quintela930ac042017-05-04 10:21:46 +0200512static void populate_disk_info(MigrationInfo *info)
513{
514 if (blk_mig_active()) {
515 info->has_disk = true;
516 info->disk = g_malloc0(sizeof(*info->disk));
517 info->disk->transferred = blk_mig_bytes_transferred();
518 info->disk->remaining = blk_mig_bytes_remaining();
519 info->disk->total = blk_mig_bytes_total();
520 }
521}
522
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300523MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000524{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300525 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200526 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000527
Juan Quintela17549e82011-10-05 13:50:43 +0200528 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800529 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200530 /* no migration has happened ever */
531 break;
zhanghailiang31194732015-03-13 16:08:38 +0800532 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400533 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400534 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400535 break;
zhanghailiang31194732015-03-13 16:08:38 +0800536 case MIGRATION_STATUS_ACTIVE:
537 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000538 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200539 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000540 info->has_status = true;
541 info->has_total_time = true;
542 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
543 - s->total_time;
544 info->has_expected_downtime = true;
545 info->expected_downtime = s->expected_downtime;
546 info->has_setup_time = true;
547 info->setup_time = s->setup_time;
548
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100549 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200550 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000551 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800552 case MIGRATION_STATUS_COLO:
553 info->has_status = true;
554 /* TODO: display COLO specific information (checkpoint info etc.) */
555 break;
zhanghailiang31194732015-03-13 16:08:38 +0800556 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300557 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900558 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200559 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200560 info->has_downtime = true;
561 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400562 info->has_setup_time = true;
563 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200564
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100565 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200566 break;
zhanghailiang31194732015-03-13 16:08:38 +0800567 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300568 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100569 if (s->error) {
570 info->has_error_desc = true;
571 info->error_desc = g_strdup(error_get_pretty(s->error));
572 }
Juan Quintela17549e82011-10-05 13:50:43 +0200573 break;
zhanghailiang31194732015-03-13 16:08:38 +0800574 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300575 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200576 break;
aliguori5bb79102008-10-13 03:12:02 +0000577 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800578 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300579
580 return info;
aliguori5bb79102008-10-13 03:12:02 +0000581}
582
Orit Wasserman00458432012-08-06 21:42:48 +0300583void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
584 Error **errp)
585{
586 MigrationState *s = migrate_get_current();
587 MigrationCapabilityStatusList *cap;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100588 bool old_postcopy_cap = migrate_postcopy_ram();
Orit Wasserman00458432012-08-06 21:42:48 +0300589
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000590 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100591 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300592 return;
593 }
594
595 for (cap = params; cap; cap = cap->next) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100596#ifndef CONFIG_LIVE_BLOCK_MIGRATION
597 if (cap->value->capability == MIGRATION_CAPABILITY_BLOCK
598 && cap->value->state) {
599 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
600 "block migration");
601 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
602 continue;
603 }
604#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800605 if (cap->value->capability == MIGRATION_CAPABILITY_X_COLO) {
606 if (!colo_supported()) {
607 error_setg(errp, "COLO is not currently supported, please"
608 " configure with --enable-colo option in order to"
609 " support COLO feature");
610 continue;
611 }
612 }
Orit Wasserman00458432012-08-06 21:42:48 +0300613 s->enabled_capabilities[cap->value->capability] = cap->value->state;
614 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000615
616 if (migrate_postcopy_ram()) {
617 if (migrate_use_compression()) {
618 /* The decompression threads asynchronously write into RAM
619 * rather than use the atomic copies needed to avoid
620 * userfaulting. It should be possible to fix the decompression
621 * threads for compatibility in future.
622 */
623 error_report("Postcopy is not currently compatible with "
624 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000625 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000626 false;
627 }
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100628 /* This check is reasonably expensive, so only when it's being
629 * set the first time, also it's only the destination that needs
630 * special support.
631 */
632 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
633 !postcopy_ram_supported_by_host()) {
634 /* postcopy_ram_supported_by_host will have emitted a more
635 * detailed message
636 */
637 error_report("Postcopy is not supported");
638 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
639 false;
640 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000641 }
Orit Wasserman00458432012-08-06 21:42:48 +0300642}
643
Eric Blake7f375e02016-09-08 22:14:16 -0500644void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800645{
646 MigrationState *s = migrate_get_current();
647
Eric Blake7f375e02016-09-08 22:14:16 -0500648 if (params->has_compress_level &&
649 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100650 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
651 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800652 return;
653 }
Eric Blake7f375e02016-09-08 22:14:16 -0500654 if (params->has_compress_threads &&
655 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100656 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
657 "compress_threads",
658 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800659 return;
660 }
Eric Blake7f375e02016-09-08 22:14:16 -0500661 if (params->has_decompress_threads &&
662 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100663 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
664 "decompress_threads",
665 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800666 return;
667 }
Eric Blake7f375e02016-09-08 22:14:16 -0500668 if (params->has_cpu_throttle_initial &&
669 (params->cpu_throttle_initial < 1 ||
670 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400671 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400672 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400673 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530674 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400675 }
Eric Blake7f375e02016-09-08 22:14:16 -0500676 if (params->has_cpu_throttle_increment &&
677 (params->cpu_throttle_increment < 1 ||
678 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400679 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400680 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400681 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530682 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400683 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530684 if (params->has_max_bandwidth &&
685 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
686 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
687 " range of 0 to %zu bytes/second", SIZE_MAX);
688 return;
689 }
690 if (params->has_downtime_limit &&
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300691 (params->downtime_limit < 0 ||
692 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
693 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
694 "the range of 0 to %d milliseconds",
695 MAX_MIGRATE_DOWNTIME);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530696 return;
697 }
zhanghailiang68b53592016-10-27 14:43:01 +0800698 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
699 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
700 "x_checkpoint_delay",
701 "is invalid, it should be positive");
702 }
Liang Li85de8322015-03-23 16:32:28 +0800703
Eric Blake7f375e02016-09-08 22:14:16 -0500704 if (params->has_compress_level) {
705 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800706 }
Eric Blake7f375e02016-09-08 22:14:16 -0500707 if (params->has_compress_threads) {
708 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800709 }
Eric Blake7f375e02016-09-08 22:14:16 -0500710 if (params->has_decompress_threads) {
711 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800712 }
Eric Blake7f375e02016-09-08 22:14:16 -0500713 if (params->has_cpu_throttle_initial) {
714 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400715 }
Eric Blake7f375e02016-09-08 22:14:16 -0500716 if (params->has_cpu_throttle_increment) {
717 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400718 }
Eric Blake7f375e02016-09-08 22:14:16 -0500719 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100720 g_free(s->parameters.tls_creds);
Eric Blake7f375e02016-09-08 22:14:16 -0500721 s->parameters.tls_creds = g_strdup(params->tls_creds);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100722 }
Eric Blake7f375e02016-09-08 22:14:16 -0500723 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100724 g_free(s->parameters.tls_hostname);
Eric Blake7f375e02016-09-08 22:14:16 -0500725 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100726 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530727 if (params->has_max_bandwidth) {
728 s->parameters.max_bandwidth = params->max_bandwidth;
729 if (s->to_dst_file) {
730 qemu_file_set_rate_limit(s->to_dst_file,
731 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
732 }
733 }
734 if (params->has_downtime_limit) {
735 s->parameters.downtime_limit = params->downtime_limit;
736 }
zhanghailiang68b53592016-10-27 14:43:01 +0800737
738 if (params->has_x_checkpoint_delay) {
739 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800740 if (migration_in_colo_state()) {
741 colo_checkpoint_notify(s);
742 }
zhanghailiang68b53592016-10-27 14:43:01 +0800743 }
Juan Quintela2833c592017-04-05 18:32:37 +0200744 if (params->has_block_incremental) {
745 s->parameters.block_incremental = params->block_incremental;
746 }
Liang Li85de8322015-03-23 16:32:28 +0800747}
748
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100749
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000750void qmp_migrate_start_postcopy(Error **errp)
751{
752 MigrationState *s = migrate_get_current();
753
754 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000755 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000756 " the start of migration");
757 return;
758 }
759
760 if (s->state == MIGRATION_STATUS_NONE) {
761 error_setg(errp, "Postcopy must be started after migration has been"
762 " started");
763 return;
764 }
765 /*
766 * we don't error if migration has finished since that would be racy
767 * with issuing this command.
768 */
769 atomic_set(&s->start_postcopy, true);
770}
771
aliguori065e2812008-11-11 16:46:33 +0000772/* shared migration helpers */
773
zhanghailiang48781e52015-12-16 11:47:33 +0000774void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000775{
zhanghailiang48781e52015-12-16 11:47:33 +0000776 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200777 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200778 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000779 }
780}
781
Juan Quintela2833c592017-04-05 18:32:37 +0200782void migrate_set_block_enabled(bool value, Error **errp)
783{
784 MigrationCapabilityStatusList *cap;
785
786 cap = g_new0(MigrationCapabilityStatusList, 1);
787 cap->value = g_new0(MigrationCapabilityStatus, 1);
788 cap->value->capability = MIGRATION_CAPABILITY_BLOCK;
789 cap->value->state = value;
790 qmp_migrate_set_capabilities(cap, errp);
791 qapi_free_MigrationCapabilityStatusList(cap);
792}
793
794static void migrate_set_block_incremental(MigrationState *s, bool value)
795{
796 s->parameters.block_incremental = value;
797}
798
799static void block_cleanup_parameters(MigrationState *s)
800{
801 if (s->must_remove_block_options) {
802 /* setting to false can never fail */
803 migrate_set_block_enabled(false, &error_abort);
804 migrate_set_block_incremental(s, false);
805 s->must_remove_block_options = false;
806 }
807}
808
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100809static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000810{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100811 MigrationState *s = opaque;
812
813 qemu_bh_delete(s->cleanup_bh);
814 s->cleanup_bh = NULL;
815
zhanghailiang89a02a92016-01-15 11:37:42 +0800816 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100817 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100818 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000819 if (s->migration_thread_running) {
820 qemu_thread_join(&s->thread);
821 s->migration_thread_running = false;
822 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100823 qemu_mutex_lock_iothread();
824
Liang Li8706d2d2015-03-23 16:32:17 +0800825 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800826 qemu_fclose(s->to_dst_file);
827 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000828 }
829
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000830 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
831 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100832
Liang Li94f5a432015-11-02 15:37:00 +0800833 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000834 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800835 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100836 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100837
838 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200839 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000840}
841
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100842void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000843{
Peter Maydell25174052016-10-21 18:41:45 +0100844 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +0800845 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000846 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
847 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100848 if (!s->error) {
849 s->error = error_copy(error);
850 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100851 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200852 block_cleanup_parameters(s);
Juan Quintela458cf282011-02-22 23:32:54 +0100853}
854
Juan Quintela0edda1c2010-05-11 16:28:39 +0200855static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000856{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000857 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800858 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100859 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000860
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000861 if (s->rp_state.from_dst_file) {
862 /* shutdown the rp socket, so causing the rp thread to shutdown */
863 qemu_file_shutdown(s->rp_state.from_dst_file);
864 }
865
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000866 do {
867 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000868 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000869 break;
870 }
zhanghailiang48781e52015-12-16 11:47:33 +0000871 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +0800872 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000873
874 /*
875 * If we're unlucky the migration code might be stuck somewhere in a
876 * send/write while the network has failed and is waiting to timeout;
877 * if we've got shutdown(2) available then we can force it to quit.
878 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
879 * called in a bh, so there is no race against this cancel.
880 */
zhanghailiang31194732015-03-13 16:08:38 +0800881 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000882 qemu_file_shutdown(f);
883 }
zhanghailiang1d2acc32017-01-24 15:59:52 +0800884 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
885 Error *local_err = NULL;
886
887 bdrv_invalidate_cache_all(&local_err);
888 if (local_err) {
889 error_report_err(local_err);
890 } else {
891 s->block_inactive = false;
892 }
893 }
Juan Quintela2833c592017-04-05 18:32:37 +0200894 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000895}
896
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100897void add_migration_state_change_notifier(Notifier *notify)
898{
899 notifier_list_add(&migration_state_notifiers, notify);
900}
901
902void remove_migration_state_change_notifier(Notifier *notify)
903{
Paolo Bonzini31552522012-01-13 17:34:01 +0100904 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100905}
906
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200907bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200908{
zhanghailiang31194732015-03-13 16:08:38 +0800909 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200910}
911
Juan Quintela70736932011-02-23 00:43:59 +0100912bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100913{
zhanghailiang31194732015-03-13 16:08:38 +0800914 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100915}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200916
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200917bool migration_has_failed(MigrationState *s)
918{
zhanghailiang31194732015-03-13 16:08:38 +0800919 return (s->state == MIGRATION_STATUS_CANCELLED ||
920 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200921}
922
Juan Quintela57273092017-03-20 22:25:28 +0100923bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000924{
Juan Quintela57273092017-03-20 22:25:28 +0100925 MigrationState *s = migrate_get_current();
926
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000927 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
928}
929
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000930bool migration_in_postcopy_after_devices(MigrationState *s)
931{
Juan Quintela57273092017-03-20 22:25:28 +0100932 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000933}
934
Juan Quintelafab35002017-03-22 17:36:57 +0100935bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530936{
Juan Quintelafab35002017-03-22 17:36:57 +0100937 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530938
939 switch (s->state) {
940 case MIGRATION_STATUS_NONE:
941 case MIGRATION_STATUS_CANCELLED:
942 case MIGRATION_STATUS_COMPLETED:
943 case MIGRATION_STATUS_FAILED:
944 return true;
945 case MIGRATION_STATUS_SETUP:
946 case MIGRATION_STATUS_CANCELLING:
947 case MIGRATION_STATUS_ACTIVE:
948 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
949 case MIGRATION_STATUS_COLO:
950 return false;
951 case MIGRATION_STATUS__MAX:
952 g_assert_not_reached();
953 }
954
955 return false;
956}
957
Juan Quintelaa0762d92017-04-05 21:00:09 +0200958MigrationState *migrate_init(void)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200959{
Juan Quintela17549e82011-10-05 13:50:43 +0200960 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300961
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000962 /*
963 * Reinitialise all migration state, except
964 * parameters/capabilities that the user set, and
965 * locks.
966 */
967 s->bytes_xfer = 0;
968 s->xfer_limit = 0;
969 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +0800970 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000971 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000972 s->rp_state.from_dst_file = NULL;
973 s->rp_state.error = false;
974 s->mbps = 0.0;
975 s->downtime = 0;
976 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000977 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000978 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000979 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000980 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100981 error_free(s->error);
982 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +0100983
zhanghailiang48781e52015-12-16 11:47:33 +0000984 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200985
Alex Blighbc72ad62013-08-21 16:03:08 +0100986 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200987 return s;
988}
Juan Quintelacab30142011-02-22 23:54:21 +0100989
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600990static GSList *migration_blockers;
991
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530992int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600993{
Peter Xu3df663e2017-06-27 12:10:15 +0800994 if (migrate_get_current()->only_migratable) {
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +0530995 error_propagate(errp, error_copy(reason));
996 error_prepend(errp, "disallowing migration blocker "
997 "(--only_migratable) for: ");
998 return -EACCES;
999 }
1000
Juan Quintelafab35002017-03-22 17:36:57 +01001001 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301002 migration_blockers = g_slist_prepend(migration_blockers, reason);
1003 return 0;
1004 }
1005
1006 error_propagate(errp, error_copy(reason));
1007 error_prepend(errp, "disallowing migration blocker (migration in "
1008 "progress) for: ");
1009 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001010}
1011
1012void migrate_del_blocker(Error *reason)
1013{
1014 migration_blockers = g_slist_remove(migration_blockers, reason);
1015}
1016
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001017void qmp_migrate_incoming(const char *uri, Error **errp)
1018{
1019 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001020 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001021
1022 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001023 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001024 return;
1025 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001026 if (!once) {
1027 error_setg(errp, "The incoming migration has already been started");
1028 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001029
1030 qemu_start_incoming_migration(uri, &local_err);
1031
1032 if (local_err) {
1033 error_propagate(errp, local_err);
1034 return;
1035 }
1036
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001037 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001038}
1039
Greg Kurz24f39022016-05-04 21:44:19 +02001040bool migration_is_blocked(Error **errp)
1041{
1042 if (qemu_savevm_state_blocked(errp)) {
1043 return true;
1044 }
1045
1046 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001047 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001048 return true;
1049 }
1050
1051 return false;
1052}
1053
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001054void qmp_migrate(const char *uri, bool has_blk, bool blk,
1055 bool has_inc, bool inc, bool has_detach, bool detach,
1056 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001057{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001058 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001059 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001060 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001061
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001062 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001063 s->state == MIGRATION_STATUS_CANCELLING ||
1064 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001065 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001066 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001067 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001068 if (runstate_check(RUN_STATE_INMIGRATE)) {
1069 error_setg(errp, "Guest is waiting for an incoming migration");
1070 return;
1071 }
1072
Greg Kurz24f39022016-05-04 21:44:19 +02001073 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001074 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001075 }
1076
Juan Quintela2833c592017-04-05 18:32:37 +02001077 if ((has_blk && blk) || (has_inc && inc)) {
1078 if (migrate_use_block() || migrate_use_block_incremental()) {
1079 error_setg(errp, "Command options are incompatible with "
1080 "current migration capabilities");
1081 return;
1082 }
1083 migrate_set_block_enabled(true, &local_err);
1084 if (local_err) {
1085 error_propagate(errp, local_err);
1086 return;
1087 }
1088 s->must_remove_block_options = true;
1089 }
1090
1091 if (has_inc && inc) {
1092 migrate_set_block_incremental(s, true);
1093 }
1094
Juan Quintelaa0762d92017-04-05 21:00:09 +02001095 s = migrate_init();
Juan Quintelacab30142011-02-22 23:54:21 +01001096
1097 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001098 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001099#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001100 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001101 rdma_start_outgoing_migration(s, p, &local_err);
1102#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001103 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001104 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001105 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001106 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001107 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001108 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001109 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001110 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1111 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001112 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1113 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001114 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001115 }
1116
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001117 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001118 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001119 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001120 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001121 }
Juan Quintelacab30142011-02-22 23:54:21 +01001122}
1123
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001124void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001125{
Juan Quintela17549e82011-10-05 13:50:43 +02001126 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001127}
1128
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001129void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1130{
1131 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001132 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001133
1134 /* Check for truncation */
1135 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001136 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1137 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001138 return;
1139 }
1140
Orit Wassermana5615b12014-01-30 20:08:36 +02001141 /* Cache should not be larger than guest ram size */
1142 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001143 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1144 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001145 return;
1146 }
1147
Orit Wassermanc91e6812014-01-30 20:08:34 +02001148 new_size = xbzrle_cache_resize(value);
1149 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001150 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1151 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001152 return;
1153 }
1154
1155 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001156}
1157
1158int64_t qmp_query_migrate_cache_size(Error **errp)
1159{
1160 return migrate_xbzrle_cache_size();
1161}
1162
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001163void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001164{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301165 MigrationParameters p = {
1166 .has_max_bandwidth = true,
1167 .max_bandwidth = value,
1168 };
Juan Quintelacab30142011-02-22 23:54:21 +01001169
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301170 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001171}
1172
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001173void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001174{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001175 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1176 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1177 "the range of 0 to %d seconds",
1178 MAX_MIGRATE_DOWNTIME_SECONDS);
1179 return;
1180 }
1181
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301182 value *= 1000; /* Convert to milliseconds */
1183 value = MAX(0, MIN(INT64_MAX, value));
1184
1185 MigrationParameters p = {
1186 .has_downtime_limit = true,
1187 .downtime_limit = value,
1188 };
1189
1190 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001191}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001192
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001193bool migrate_release_ram(void)
1194{
1195 MigrationState *s;
1196
1197 s = migrate_get_current();
1198
1199 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1200}
1201
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001202bool migrate_postcopy_ram(void)
1203{
1204 MigrationState *s;
1205
1206 s = migrate_get_current();
1207
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001208 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001209}
1210
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001211bool migrate_auto_converge(void)
1212{
1213 MigrationState *s;
1214
1215 s = migrate_get_current();
1216
1217 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1218}
1219
Peter Lieven323004a2013-07-18 09:48:50 +02001220bool migrate_zero_blocks(void)
1221{
1222 MigrationState *s;
1223
1224 s = migrate_get_current();
1225
1226 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1227}
1228
Liang Li8706d2d2015-03-23 16:32:17 +08001229bool migrate_use_compression(void)
1230{
Liang Lidde4e692015-03-23 16:32:26 +08001231 MigrationState *s;
1232
1233 s = migrate_get_current();
1234
1235 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001236}
1237
1238int migrate_compress_level(void)
1239{
1240 MigrationState *s;
1241
1242 s = migrate_get_current();
1243
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001244 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001245}
1246
1247int migrate_compress_threads(void)
1248{
1249 MigrationState *s;
1250
1251 s = migrate_get_current();
1252
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001253 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001254}
1255
Liang Li3fcb38c2015-03-23 16:32:18 +08001256int migrate_decompress_threads(void)
1257{
1258 MigrationState *s;
1259
1260 s = migrate_get_current();
1261
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001262 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001263}
1264
Juan Quintelab05dc722015-07-07 14:44:05 +02001265bool migrate_use_events(void)
1266{
1267 MigrationState *s;
1268
1269 s = migrate_get_current();
1270
1271 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1272}
1273
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001274int migrate_use_xbzrle(void)
1275{
1276 MigrationState *s;
1277
1278 s = migrate_get_current();
1279
1280 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1281}
1282
1283int64_t migrate_xbzrle_cache_size(void)
1284{
1285 MigrationState *s;
1286
1287 s = migrate_get_current();
1288
1289 return s->xbzrle_cache_size;
1290}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001291
Juan Quintela2833c592017-04-05 18:32:37 +02001292bool migrate_use_block(void)
1293{
1294 MigrationState *s;
1295
1296 s = migrate_get_current();
1297
1298 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1299}
1300
1301bool migrate_use_block_incremental(void)
1302{
1303 MigrationState *s;
1304
1305 s = migrate_get_current();
1306
1307 return s->parameters.block_incremental;
1308}
1309
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001310/* migration thread support */
1311/*
1312 * Something bad happened to the RP stream, mark an error
1313 * The caller shall print or trace something to indicate why
1314 */
1315static void mark_source_rp_bad(MigrationState *s)
1316{
1317 s->rp_state.error = true;
1318}
1319
1320static struct rp_cmd_args {
1321 ssize_t len; /* -1 = variable */
1322 const char *name;
1323} rp_cmd_args[] = {
1324 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1325 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1326 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001327 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1328 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001329 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1330};
1331
1332/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001333 * Process a request for pages received on the return path,
1334 * We're allowed to send more than requested (e.g. to round to our page size)
1335 * and we don't need to send pages that have already been sent.
1336 */
1337static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1338 ram_addr_t start, size_t len)
1339{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001340 long our_host_ps = getpagesize();
1341
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001342 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001343
1344 /*
1345 * Since we currently insist on matching page sizes, just sanity check
1346 * we're being asked for whole host pages.
1347 */
1348 if (start & (our_host_ps-1) ||
1349 (len & (our_host_ps-1))) {
1350 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1351 " len: %zd", __func__, start, len);
1352 mark_source_rp_bad(ms);
1353 return;
1354 }
1355
Juan Quintela96506892017-03-14 18:41:03 +01001356 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001357 mark_source_rp_bad(ms);
1358 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001359}
1360
1361/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001362 * Handles messages sent on the return path towards the source VM
1363 *
1364 */
1365static void *source_return_path_thread(void *opaque)
1366{
1367 MigrationState *ms = opaque;
1368 QEMUFile *rp = ms->rp_state.from_dst_file;
1369 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001370 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001371 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001372 ram_addr_t start = 0; /* =0 to silence warning */
1373 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001374 int res;
1375
1376 trace_source_return_path_thread_entry();
1377 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1378 migration_is_setup_or_active(ms->state)) {
1379 trace_source_return_path_thread_loop_top();
1380 header_type = qemu_get_be16(rp);
1381 header_len = qemu_get_be16(rp);
1382
1383 if (header_type >= MIG_RP_MSG_MAX ||
1384 header_type == MIG_RP_MSG_INVALID) {
1385 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1386 header_type, header_len);
1387 mark_source_rp_bad(ms);
1388 goto out;
1389 }
1390
1391 if ((rp_cmd_args[header_type].len != -1 &&
1392 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001393 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001394 error_report("RP: Received '%s' message (0x%04x) with"
1395 "incorrect length %d expecting %zu",
1396 rp_cmd_args[header_type].name, header_type, header_len,
1397 (size_t)rp_cmd_args[header_type].len);
1398 mark_source_rp_bad(ms);
1399 goto out;
1400 }
1401
1402 /* We know we've got a valid header by this point */
1403 res = qemu_get_buffer(rp, buf, header_len);
1404 if (res != header_len) {
1405 error_report("RP: Failed reading data for message 0x%04x"
1406 " read %d expected %d",
1407 header_type, res, header_len);
1408 mark_source_rp_bad(ms);
1409 goto out;
1410 }
1411
1412 /* OK, we have the message and the data */
1413 switch (header_type) {
1414 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001415 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001416 trace_source_return_path_thread_shut(sibling_error);
1417 if (sibling_error) {
1418 error_report("RP: Sibling indicated error %d", sibling_error);
1419 mark_source_rp_bad(ms);
1420 }
1421 /*
1422 * We'll let the main thread deal with closing the RP
1423 * we could do a shutdown(2) on it, but we're the only user
1424 * anyway, so there's nothing gained.
1425 */
1426 goto out;
1427
1428 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001429 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001430 trace_source_return_path_thread_pong(tmp32);
1431 break;
1432
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001433 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001434 start = ldq_be_p(buf);
1435 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001436 migrate_handle_rp_req_pages(ms, NULL, start, len);
1437 break;
1438
1439 case MIG_RP_MSG_REQ_PAGES_ID:
1440 expected_len = 12 + 1; /* header + termination */
1441
1442 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001443 start = ldq_be_p(buf);
1444 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001445 /* Now we expect an idstr */
1446 tmp32 = buf[12]; /* Length of the following idstr */
1447 buf[13 + tmp32] = '\0';
1448 expected_len += tmp32;
1449 }
1450 if (header_len != expected_len) {
1451 error_report("RP: Req_Page_id with length %d expecting %zd",
1452 header_len, expected_len);
1453 mark_source_rp_bad(ms);
1454 goto out;
1455 }
1456 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1457 break;
1458
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001459 default:
1460 break;
1461 }
1462 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001463 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001464 trace_source_return_path_thread_bad_end();
1465 mark_source_rp_bad(ms);
1466 }
1467
1468 trace_source_return_path_thread_end();
1469out:
1470 ms->rp_state.from_dst_file = NULL;
1471 qemu_fclose(rp);
1472 return NULL;
1473}
1474
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001475static int open_return_path_on_source(MigrationState *ms)
1476{
1477
zhanghailiang89a02a92016-01-15 11:37:42 +08001478 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001479 if (!ms->rp_state.from_dst_file) {
1480 return -1;
1481 }
1482
1483 trace_open_return_path_on_source();
1484 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1485 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1486
1487 trace_open_return_path_on_source_continue();
1488
1489 return 0;
1490}
1491
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001492/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1493static int await_return_path_close_on_source(MigrationState *ms)
1494{
1495 /*
1496 * If this is a normal exit then the destination will send a SHUT and the
1497 * rp_thread will exit, however if there's an error we need to cause
1498 * it to exit.
1499 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001500 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001501 /*
1502 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1503 * waiting for the destination.
1504 */
1505 qemu_file_shutdown(ms->rp_state.from_dst_file);
1506 mark_source_rp_bad(ms);
1507 }
1508 trace_await_return_path_close_on_source_joining();
1509 qemu_thread_join(&ms->rp_state.rp_thread);
1510 trace_await_return_path_close_on_source_close();
1511 return ms->rp_state.error;
1512}
1513
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001514/*
1515 * Switch from normal iteration to postcopy
1516 * Returns non-0 on error
1517 */
1518static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1519{
1520 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001521 QIOChannelBuffer *bioc;
1522 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001523 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001524 bool restart_block = false;
zhanghailiang48781e52015-12-16 11:47:33 +00001525 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001526 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1527
1528 trace_postcopy_start();
1529 qemu_mutex_lock_iothread();
1530 trace_postcopy_start_set_run();
1531
1532 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1533 *old_vm_running = runstate_is_running();
1534 global_state_store();
1535 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001536 if (ret < 0) {
1537 goto fail;
1538 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001539
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001540 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001541 if (ret < 0) {
1542 goto fail;
1543 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001544 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001545
1546 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001547 * Cause any non-postcopiable, but iterative devices to
1548 * send out their final data.
1549 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08001550 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001551
1552 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001553 * in Finish migrate and with the io-lock held everything should
1554 * be quiet, but we've potentially still got dirty pages and we
1555 * need to tell the destination to throw any pages it's already received
1556 * that are dirty
1557 */
1558 if (ram_postcopy_send_discard_bitmap(ms)) {
1559 error_report("postcopy send discard bitmap failed");
1560 goto fail;
1561 }
1562
1563 /*
1564 * send rest of state - note things that are doing postcopy
1565 * will notice we're in POSTCOPY_ACTIVE and not actually
1566 * wrap their state up here
1567 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001568 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001569 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001570 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001571
1572 /*
1573 * While loading the device state we may trigger page transfer
1574 * requests and the fd must be free to process those, and thus
1575 * the destination must read the whole device state off the fd before
1576 * it starts processing it. Unfortunately the ad-hoc migration format
1577 * doesn't allow the destination to know the size to read without fully
1578 * parsing it through each devices load-state code (especially the open
1579 * coded devices that use get/put).
1580 * So we wrap the device state up in a package with a length at the start;
1581 * to do this we use a qemu_buf to hold the whole of the device state.
1582 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001583 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001584 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001585 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1586 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001587
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001588 /*
1589 * Make sure the receiver can get incoming pages before we send the rest
1590 * of the state
1591 */
1592 qemu_savevm_send_postcopy_listen(fb);
1593
Fam Zhenga1fbe752017-06-17 00:06:58 +08001594 qemu_savevm_state_complete_precopy(fb, false, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001595 qemu_savevm_send_ping(fb, 3);
1596
1597 qemu_savevm_send_postcopy_run(fb);
1598
1599 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001600
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001601 /* Last point of recovery; as soon as we send the package the destination
1602 * can open devices and potentially start running.
1603 * Lets just check again we've not got any errors.
1604 */
1605 ret = qemu_file_get_error(ms->to_dst_file);
1606 if (ret) {
1607 error_report("postcopy_start: Migration stream errored (pre package)");
1608 goto fail_closefb;
1609 }
1610
1611 restart_block = false;
1612
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001613 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001614 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001615 goto fail_closefb;
1616 }
1617 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001618
1619 /* Send a notify to give a chance for anything that needs to happen
1620 * at the transition to postcopy and after the device state; in particular
1621 * spice needs to trigger a transition now
1622 */
1623 ms->postcopy_after_devices = true;
1624 notifier_list_notify(&migration_state_notifiers, ms);
1625
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001626 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1627
1628 qemu_mutex_unlock_iothread();
1629
1630 /*
1631 * Although this ping is just for debug, it could potentially be
1632 * used for getting a better measurement of downtime at the source.
1633 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001634 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001635
Pavel Butsykinced1c612017-02-03 18:23:21 +03001636 if (migrate_release_ram()) {
1637 ram_postcopy_migrated_memory_release(ms);
1638 }
1639
zhanghailiang89a02a92016-01-15 11:37:42 +08001640 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001641 if (ret) {
1642 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001643 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001644 MIGRATION_STATUS_FAILED);
1645 }
1646
1647 return ret;
1648
1649fail_closefb:
1650 qemu_fclose(fb);
1651fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001652 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001653 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001654 if (restart_block) {
1655 /* A failure happened early enough that we know the destination hasn't
1656 * accessed block devices, so we're safe to recover.
1657 */
1658 Error *local_err = NULL;
1659
1660 bdrv_invalidate_cache_all(&local_err);
1661 if (local_err) {
1662 error_report_err(local_err);
1663 }
1664 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001665 qemu_mutex_unlock_iothread();
1666 return -1;
1667}
1668
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001669/**
1670 * migration_completion: Used by migration_thread when there's not much left.
1671 * The caller 'breaks' the loop when this returns.
1672 *
1673 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001674 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001675 * @*old_vm_running: Pointer to old_vm_running flag
1676 * @*start_time: Pointer to time to update
1677 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001678static void migration_completion(MigrationState *s, int current_active_state,
1679 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001680 int64_t *start_time)
1681{
1682 int ret;
1683
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001684 if (s->state == MIGRATION_STATUS_ACTIVE) {
1685 qemu_mutex_lock_iothread();
1686 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1687 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1688 *old_vm_running = runstate_is_running();
1689 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001690
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001691 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08001692 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001693 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001694 if (ret >= 0) {
1695 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08001696 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
1697 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001698 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08001699 if (inactivate && ret >= 0) {
1700 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001701 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001702 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001703 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001704
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001705 if (ret < 0) {
1706 goto fail;
1707 }
1708 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1709 trace_migration_completion_postcopy_end();
1710
zhanghailiang89a02a92016-01-15 11:37:42 +08001711 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001712 trace_migration_completion_postcopy_end_after_complete();
1713 }
1714
1715 /*
1716 * If rp was opened we must clean up the thread before
1717 * cleaning everything else up (since if there are no failures
1718 * it will wait for the destination to send it's status in
1719 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001720 */
Peter Xu0425dc92017-05-31 18:35:34 +08001721 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001722 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08001723 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001724 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08001725 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001726 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001727 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001728 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001729 }
1730
zhanghailiang89a02a92016-01-15 11:37:42 +08001731 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001732 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001733 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001734 }
1735
zhanghailiang0b827d52016-10-27 14:42:54 +08001736 if (!migrate_colo_enabled()) {
1737 migrate_set_state(&s->state, current_active_state,
1738 MIGRATION_STATUS_COMPLETED);
1739 }
1740
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001741 return;
1742
Greg Kurzfe904ea2016-05-18 15:44:36 +02001743fail_invalidate:
1744 /* If not doing postcopy, vm_start() will be called: let's regain
1745 * control on images.
1746 */
1747 if (s->state == MIGRATION_STATUS_ACTIVE) {
1748 Error *local_err = NULL;
1749
zhanghailiang1d2acc32017-01-24 15:59:52 +08001750 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001751 bdrv_invalidate_cache_all(&local_err);
1752 if (local_err) {
1753 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001754 } else {
1755 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02001756 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001757 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001758 }
1759
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001760fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001761 migrate_set_state(&s->state, current_active_state,
1762 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001763}
1764
zhanghailiang35a6ed42016-10-27 14:42:52 +08001765bool migrate_colo_enabled(void)
1766{
1767 MigrationState *s = migrate_get_current();
1768 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
1769}
1770
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001771/*
1772 * Master migration thread on the source VM.
1773 * It drives the migration and pumps the data down the outgoing channel.
1774 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001775static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001776{
Juan Quintela9848a402012-12-19 09:55:50 +01001777 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001778 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001779 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1780 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001781 int64_t initial_bytes = 0;
Peter Xufaec0662017-04-01 16:18:43 +08001782 /*
1783 * The final stage happens when the remaining data is smaller than
1784 * this threshold; it's calculated from the requested downtime and
1785 * measured bandwidth
1786 */
1787 int64_t threshold_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001788 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001789 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001790 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001791 bool entered_postcopy = false;
1792 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1793 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang0b827d52016-10-27 14:42:54 +08001794 bool enable_colo = migrate_colo_enabled();
Juan Quintela76f59332012-10-03 20:16:24 +02001795
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001796 rcu_register_thread();
1797
zhanghailiang89a02a92016-01-15 11:37:42 +08001798 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001799
Peter Xu62a02652017-06-14 15:55:58 +08001800 /*
1801 * If we opened the return path, we need to make sure dst has it
1802 * opened as well.
1803 */
1804 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001805 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001806 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001807
1808 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001809 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08001810 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001811
Peter Xu0425dc92017-05-31 18:35:34 +08001812 if (migrate_postcopy_ram()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001813 /*
1814 * Tell the destination that we *might* want to do postcopy later;
1815 * if the other end can't do postcopy it should fail now, nice and
1816 * early.
1817 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001818 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001819 }
1820
Juan Quintelaa0762d92017-04-05 21:00:09 +02001821 qemu_savevm_state_begin(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001822
Alex Blighbc72ad62013-08-21 16:03:08 +01001823 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00001824 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1825 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001826
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001827 trace_migration_thread_setup_complete();
1828
1829 while (s->state == MIGRATION_STATUS_ACTIVE ||
1830 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001831 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001832 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001833
zhanghailiang89a02a92016-01-15 11:37:42 +08001834 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001835 uint64_t pend_post, pend_nonpost;
1836
Peter Xufaec0662017-04-01 16:18:43 +08001837 qemu_savevm_state_pending(s->to_dst_file, threshold_size,
1838 &pend_nonpost, &pend_post);
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001839 pending_size = pend_nonpost + pend_post;
Peter Xufaec0662017-04-01 16:18:43 +08001840 trace_migrate_pending(pending_size, threshold_size,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001841 pend_post, pend_nonpost);
Peter Xufaec0662017-04-01 16:18:43 +08001842 if (pending_size && pending_size >= threshold_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001843 /* Still a significant amount to transfer */
1844
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001845 if (migrate_postcopy_ram() &&
1846 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
Peter Xufaec0662017-04-01 16:18:43 +08001847 pend_nonpost <= threshold_size &&
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001848 atomic_read(&s->start_postcopy)) {
1849
1850 if (!postcopy_start(s, &old_vm_running)) {
1851 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1852 entered_postcopy = true;
1853 }
1854
1855 continue;
1856 }
1857 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001858 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001859 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001860 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001861 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001862 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001863 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001864 }
1865 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001866
zhanghailiang89a02a92016-01-15 11:37:42 +08001867 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001868 migrate_set_state(&s->state, current_active_state,
1869 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001870 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001871 break;
1872 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001873 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001874 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001875 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1876 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001877 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001878 double bandwidth = (double)transferred_bytes / time_spent;
Peter Xufaec0662017-04-01 16:18:43 +08001879 threshold_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001880
Wei Yang5b648de2016-01-24 14:09:57 +00001881 s->mbps = (((double) transferred_bytes * 8.0) /
1882 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04001883
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001884 trace_migrate_transferred(transferred_bytes, time_spent,
Peter Xufaec0662017-04-01 16:18:43 +08001885 bandwidth, threshold_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001886 /* if we haven't sent anything, we don't want to recalculate
1887 10000 is a small enough number for our purposes */
Juan Quintela93604472017-06-06 19:49:03 +02001888 if (ram_counters.dirty_pages_rate && transferred_bytes > 10000) {
1889 s->expected_downtime = ram_counters.dirty_pages_rate *
Juan Quintela20afaed2017-03-21 09:09:14 +01001890 qemu_target_page_size() / bandwidth;
Juan Quintela90f8ae72013-02-01 13:22:37 +01001891 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001892
zhanghailiang89a02a92016-01-15 11:37:42 +08001893 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001894 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08001895 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001896 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001897 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001898 /* usleep expects microseconds */
1899 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1900 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001901 }
1902
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001903 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001904 /* If we enabled cpu throttling for auto-converge, turn it off. */
1905 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001906 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001907
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001908 qemu_mutex_lock_iothread();
zhanghailiang0b827d52016-10-27 14:42:54 +08001909 /*
1910 * The resource has been allocated by migration will be reused in COLO
1911 * process, so don't release them.
1912 */
1913 if (!enable_colo) {
1914 qemu_savevm_state_cleanup();
1915 }
zhanghailiang31194732015-03-13 16:08:38 +08001916 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001917 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001918 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001919 if (!entered_postcopy) {
1920 s->downtime = end_time - start_time;
1921 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001922 if (s->total_time) {
1923 s->mbps = (((double) transferred_bytes * 8.0) /
1924 ((double) s->total_time)) / 1000;
1925 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001926 runstate_set(RUN_STATE_POSTMIGRATE);
1927 } else {
zhanghailiang0b827d52016-10-27 14:42:54 +08001928 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
1929 migrate_start_colo_process(s);
1930 qemu_savevm_state_cleanup();
1931 /*
1932 * Fixme: we will run VM in COLO no matter its old running state.
1933 * After exited COLO, we will keep running.
1934 */
1935 old_vm_running = true;
1936 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001937 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001938 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01001939 } else {
1940 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
1941 runstate_set(RUN_STATE_POSTMIGRATE);
1942 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001943 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001944 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001945 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001946 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001947
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001948 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001949 return NULL;
1950}
1951
Juan Quintela9848a402012-12-19 09:55:50 +01001952void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001953{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301954 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001955 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001956
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01001957 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08001958 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301959 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01001960
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001961 /* Notify before starting migration thread */
1962 notifier_list_notify(&migration_state_notifiers, s);
1963
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001964 /*
1965 * Open the return path; currently for postcopy but other things might
1966 * also want it.
1967 */
1968 if (migrate_postcopy_ram()) {
1969 if (open_return_path_on_source(s)) {
1970 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00001971 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001972 MIGRATION_STATUS_FAILED);
1973 migrate_fd_cleanup(s);
1974 return;
1975 }
1976 }
1977
Liang Li8706d2d2015-03-23 16:32:17 +08001978 migrate_compress_threads_create();
Pankaj Gupta009fad72017-01-23 19:12:56 +05301979 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001980 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001981 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001982}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001983
Peter Xu52722982017-06-27 12:10:14 +08001984static Property migration_properties[] = {
1985 DEFINE_PROP_BOOL("store-global-state", MigrationState,
1986 store_global_state, true),
Peter Xu3df663e2017-06-27 12:10:15 +08001987 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
Peter Xu71dd4c12017-06-27 12:10:16 +08001988 DEFINE_PROP_BOOL("send-configuration", MigrationState,
1989 send_configuration, true),
Peter Xu52722982017-06-27 12:10:14 +08001990 DEFINE_PROP_END_OF_LIST(),
1991};
1992
Peter Xue5cb7e72017-06-27 12:10:13 +08001993static void migration_class_init(ObjectClass *klass, void *data)
1994{
1995 DeviceClass *dc = DEVICE_CLASS(klass);
1996
1997 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08001998 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08001999}
2000
2001static void migration_instance_init(Object *obj)
2002{
2003 MigrationState *ms = MIGRATION_OBJ(obj);
2004
2005 ms->state = MIGRATION_STATUS_NONE;
2006 ms->xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE;
2007 ms->mbps = -1;
2008 ms->parameters = (MigrationParameters) {
2009 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
2010 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
2011 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
2012 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
2013 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
2014 .max_bandwidth = MAX_THROTTLE,
2015 .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME,
2016 .x_checkpoint_delay = DEFAULT_MIGRATE_X_CHECKPOINT_DELAY,
2017 };
2018 ms->parameters.tls_creds = g_strdup("");
2019 ms->parameters.tls_hostname = g_strdup("");
2020}
2021
2022static const TypeInfo migration_type = {
2023 .name = TYPE_MIGRATION,
2024 .parent = TYPE_DEVICE,
2025 .class_init = migration_class_init,
2026 .class_size = sizeof(MigrationClass),
2027 .instance_size = sizeof(MigrationState),
2028 .instance_init = migration_instance_init,
2029};
2030
2031static void register_migration_types(void)
2032{
2033 type_register_static(&migration_type);
2034}
2035
2036type_init(register_migration_types);