blob: 221b22c5d8454f5c4ebed16cdce14f1f5e028465 [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
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100118MigrationIncomingState *migration_incoming_get_current(void)
119{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100120 static bool once;
121 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100122
Juan Quintelab4b076d2017-01-23 22:32:06 +0100123 if (!once) {
124 mis_current.state = MIGRATION_STATUS_NONE;
125 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100126 qemu_mutex_init(&mis_current.rp_mutex);
127 qemu_event_init(&mis_current.main_thread_load_event, false);
128 once = true;
129 }
130 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100131}
132
133void migration_incoming_state_destroy(void)
134{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100135 struct MigrationIncomingState *mis = migration_incoming_get_current();
136
Peter Xu34826552017-05-19 14:43:29 +0800137 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800138 /* Tell source that we are done */
139 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800140 qemu_fclose(mis->to_src_file);
141 mis->to_src_file = NULL;
142 }
143
Peter Xu660819b2017-05-19 14:43:30 +0800144 if (mis->from_src_file) {
145 qemu_fclose(mis->from_src_file);
146 mis->from_src_file = NULL;
147 }
148
Juan Quintelab4b076d2017-01-23 22:32:06 +0100149 qemu_event_destroy(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100150}
151
Juan Quintelab05dc722015-07-07 14:44:05 +0200152static void migrate_generate_event(int new_state)
153{
154 if (migrate_use_events()) {
155 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200156 }
157}
158
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000159/*
160 * Called on -incoming with a defer: uri.
161 * The migration can be started later after any parameters have been
162 * changed.
163 */
164static void deferred_incoming_migration(Error **errp)
165{
166 if (deferred_incoming) {
167 error_setg(errp, "Incoming migration already deferred");
168 }
169 deferred_incoming = true;
170}
171
Juan Quintelada6f1792017-04-24 17:37:14 +0200172/*
173 * Send a message on the return channel back to the source
174 * of the migration.
175 */
176static void migrate_send_rp_message(MigrationIncomingState *mis,
177 enum mig_rp_message_type message_type,
178 uint16_t len, void *data)
179{
180 trace_migrate_send_rp_message((int)message_type, len);
181 qemu_mutex_lock(&mis->rp_mutex);
182 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
183 qemu_put_be16(mis->to_src_file, len);
184 qemu_put_buffer(mis->to_src_file, data, len);
185 qemu_fflush(mis->to_src_file);
186 qemu_mutex_unlock(&mis->rp_mutex);
187}
188
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000189/* Request a range of pages from the source VM at the given
190 * start address.
191 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
192 * as the last request (a name must have been given previously)
193 * Start: Address offset within the RB
194 * Len: Length in bytes required - must be a multiple of pagesize
195 */
196void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
197 ram_addr_t start, size_t len)
198{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100199 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000200 size_t msglen = 12; /* start + len */
201
202 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
203 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
204
205 if (rbname) {
206 int rbname_len = strlen(rbname);
207 assert(rbname_len < 256);
208
209 bufc[msglen++] = rbname_len;
210 memcpy(bufc + msglen, rbname, rbname_len);
211 msglen += rbname_len;
212 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
213 } else {
214 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
215 }
216}
217
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200218void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000219{
aliguori34c9dd82008-10-13 03:14:31 +0000220 const char *p;
221
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200222 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000223 if (!strcmp(uri, "defer")) {
224 deferred_incoming_migration(errp);
225 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200226 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400227#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000228 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400229 rdma_start_incoming_migration(p, errp);
230#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000231 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200232 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000233 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200234 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000235 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200236 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000237 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100238 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200239 }
aliguori5bb79102008-10-13 03:12:02 +0000240}
241
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300242static void process_incoming_migration_bh(void *opaque)
243{
244 Error *local_err = NULL;
245 MigrationIncomingState *mis = opaque;
246
Kevin Wolface21a52017-05-04 18:52:36 +0200247 /* Make sure all file formats flush their mutable metadata.
248 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300249 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200250 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200251 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200252 local_err = NULL;
253 autostart = false;
254 }
255
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300256 /*
257 * This must happen after all error conditions are dealt with and
258 * we're sure the VM is going to be running on this host.
259 */
260 qemu_announce_self();
261
262 /* If global state section was not received or we are in running
263 state, we need to obey autostart. Any other state is set with
264 runstate_set. */
265
266 if (!global_state_received() ||
267 global_state_get_runstate() == RUN_STATE_RUNNING) {
268 if (autostart) {
269 vm_start();
270 } else {
271 runstate_set(RUN_STATE_PAUSED);
272 }
273 } else {
274 runstate_set(global_state_get_runstate());
275 }
276 migrate_decompress_threads_join();
277 /*
278 * This must happen after any state changes since as soon as an external
279 * observer sees this event they might start to prod at the VM assuming
280 * it's ready to use.
281 */
282 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
283 MIGRATION_STATUS_COMPLETED);
284 qemu_bh_delete(mis->bh);
285 migration_incoming_state_destroy();
286}
287
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200288static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200289{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200290 QEMUFile *f = opaque;
Juan Quintelab4b076d2017-01-23 22:32:06 +0100291 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000292 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200293 int ret;
294
Juan Quintelab4b076d2017-01-23 22:32:06 +0100295 mis->from_src_file = f;
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000296 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000297 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000298 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
299 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200300 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100301
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000302 ps = postcopy_state_get();
303 trace_process_incoming_migration_co_end(ret, ps);
304 if (ps != POSTCOPY_INCOMING_NONE) {
305 if (ps == POSTCOPY_INCOMING_ADVISE) {
306 /*
307 * Where a migration had postcopy enabled (and thus went to advise)
308 * but managed to complete within the precopy period, we can use
309 * the normal exit.
310 */
311 postcopy_ram_incoming_cleanup(mis);
312 } else if (ret >= 0) {
313 /*
314 * Postcopy was started, cleanup should happen at the end of the
315 * postcopy thread.
316 */
317 trace_process_incoming_migration_co_postcopy_end_main();
318 return;
319 }
320 /* Else if something went wrong then just fall out of the normal exit */
321 }
322
zhanghailiang25d0c162016-10-27 14:42:55 +0800323 /* we get COLO info, and know if we are in COLO mode */
324 if (!ret && migration_incoming_enable_colo()) {
325 mis->migration_incoming_co = qemu_coroutine_self();
326 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
327 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
328 mis->have_colo_incoming_thread = true;
329 qemu_coroutine_yield();
330
331 /* Wait checkpoint incoming thread exit before free resource */
332 qemu_thread_join(&mis->colo_incoming_thread);
333 }
334
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200335 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000336 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
337 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200338 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800339 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600340 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200341 }
Juan Quintela511c0232010-06-09 14:10:55 +0200342
Laurent Viviere8199e42017-04-12 15:53:11 +0200343 free_xbzrle_decoded_buf();
344
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300345 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
346 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200347}
348
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100349void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200350{
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200351 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200352
Liang Li3fcb38c2015-03-23 16:32:18 +0800353 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100354 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200355 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200356}
357
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000358/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000359 * Send a 'SHUT' message on the return channel with the given value
360 * to indicate that we've finished with the RP. Non-0 value indicates
361 * error.
362 */
363void migrate_send_rp_shut(MigrationIncomingState *mis,
364 uint32_t value)
365{
366 uint32_t buf;
367
368 buf = cpu_to_be32(value);
369 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
370}
371
372/*
373 * Send a 'PONG' message on the return channel with the given value
374 * (normally in response to a 'PING')
375 */
376void migrate_send_rp_pong(MigrationIncomingState *mis,
377 uint32_t value)
378{
379 uint32_t buf;
380
381 buf = cpu_to_be32(value);
382 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
383}
384
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300385MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
386{
387 MigrationCapabilityStatusList *head = NULL;
388 MigrationCapabilityStatusList *caps;
389 MigrationState *s = migrate_get_current();
390 int i;
391
Michael Tokarev387eede2013-10-05 13:18:28 +0400392 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700393 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100394#ifndef CONFIG_LIVE_BLOCK_MIGRATION
395 if (i == MIGRATION_CAPABILITY_BLOCK) {
396 continue;
397 }
398#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800399 if (i == MIGRATION_CAPABILITY_X_COLO && !colo_supported()) {
400 continue;
401 }
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300402 if (head == NULL) {
403 head = g_malloc0(sizeof(*caps));
404 caps = head;
405 } else {
406 caps->next = g_malloc0(sizeof(*caps));
407 caps = caps->next;
408 }
409 caps->value =
410 g_malloc(sizeof(*caps->value));
411 caps->value->capability = i;
412 caps->value->state = s->enabled_capabilities[i];
413 }
414
415 return head;
416}
417
Liang Li85de8322015-03-23 16:32:28 +0800418MigrationParameters *qmp_query_migrate_parameters(Error **errp)
419{
420 MigrationParameters *params;
421 MigrationState *s = migrate_get_current();
422
423 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500424 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100425 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500426 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100427 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500428 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100429 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500430 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100431 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500432 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100433 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Eric Blakede63ab62016-09-08 22:14:15 -0500434 params->has_tls_creds = !!s->parameters.tls_creds;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100435 params->tls_creds = g_strdup(s->parameters.tls_creds);
Eric Blakede63ab62016-09-08 22:14:15 -0500436 params->has_tls_hostname = !!s->parameters.tls_hostname;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100437 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530438 params->has_max_bandwidth = true;
439 params->max_bandwidth = s->parameters.max_bandwidth;
440 params->has_downtime_limit = true;
441 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800442 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800443 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200444 params->has_block_incremental = true;
445 params->block_incremental = s->parameters.block_incremental;
Liang Li85de8322015-03-23 16:32:28 +0800446
447 return params;
448}
449
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000450/*
451 * Return true if we're already in the middle of a migration
452 * (i.e. any of the active or setup states)
453 */
454static bool migration_is_setup_or_active(int state)
455{
456 switch (state) {
457 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000458 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000459 case MIGRATION_STATUS_SETUP:
460 return true;
461
462 default:
463 return false;
464
465 }
466}
467
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100468static void populate_ram_info(MigrationInfo *info, MigrationState *s)
469{
470 info->has_ram = true;
471 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200472 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100473 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200474 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100475 /* legacy value. It is not used anymore */
476 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200477 info->ram->normal = ram_counters.normal;
478 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100479 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100480 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200481 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
482 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800483 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100484
Juan Quintela114f5ae2017-05-04 10:09:21 +0200485 if (migrate_use_xbzrle()) {
486 info->has_xbzrle_cache = true;
487 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
488 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200489 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
490 info->xbzrle_cache->pages = xbzrle_counters.pages;
491 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
492 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
493 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200494 }
495
Juan Quintela338182c2017-05-03 13:16:38 +0200496 if (cpu_throttle_active()) {
497 info->has_cpu_throttle_percentage = true;
498 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
499 }
500
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100501 if (s->state != MIGRATION_STATUS_COMPLETED) {
502 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200503 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100504 }
505}
506
Juan Quintela930ac042017-05-04 10:21:46 +0200507static void populate_disk_info(MigrationInfo *info)
508{
509 if (blk_mig_active()) {
510 info->has_disk = true;
511 info->disk = g_malloc0(sizeof(*info->disk));
512 info->disk->transferred = blk_mig_bytes_transferred();
513 info->disk->remaining = blk_mig_bytes_remaining();
514 info->disk->total = blk_mig_bytes_total();
515 }
516}
517
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300518MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000519{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300520 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200521 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000522
Juan Quintela17549e82011-10-05 13:50:43 +0200523 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800524 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200525 /* no migration has happened ever */
526 break;
zhanghailiang31194732015-03-13 16:08:38 +0800527 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400528 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400529 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400530 break;
zhanghailiang31194732015-03-13 16:08:38 +0800531 case MIGRATION_STATUS_ACTIVE:
532 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000533 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200534 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000535 info->has_status = true;
536 info->has_total_time = true;
537 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
538 - s->total_time;
539 info->has_expected_downtime = true;
540 info->expected_downtime = s->expected_downtime;
541 info->has_setup_time = true;
542 info->setup_time = s->setup_time;
543
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100544 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200545 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000546 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800547 case MIGRATION_STATUS_COLO:
548 info->has_status = true;
549 /* TODO: display COLO specific information (checkpoint info etc.) */
550 break;
zhanghailiang31194732015-03-13 16:08:38 +0800551 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300552 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900553 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200554 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200555 info->has_downtime = true;
556 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400557 info->has_setup_time = true;
558 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200559
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100560 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200561 break;
zhanghailiang31194732015-03-13 16:08:38 +0800562 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300563 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100564 if (s->error) {
565 info->has_error_desc = true;
566 info->error_desc = g_strdup(error_get_pretty(s->error));
567 }
Juan Quintela17549e82011-10-05 13:50:43 +0200568 break;
zhanghailiang31194732015-03-13 16:08:38 +0800569 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300570 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200571 break;
aliguori5bb79102008-10-13 03:12:02 +0000572 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800573 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300574
575 return info;
aliguori5bb79102008-10-13 03:12:02 +0000576}
577
Orit Wasserman00458432012-08-06 21:42:48 +0300578void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
579 Error **errp)
580{
581 MigrationState *s = migrate_get_current();
582 MigrationCapabilityStatusList *cap;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100583 bool old_postcopy_cap = migrate_postcopy_ram();
Orit Wasserman00458432012-08-06 21:42:48 +0300584
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000585 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100586 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300587 return;
588 }
589
590 for (cap = params; cap; cap = cap->next) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100591#ifndef CONFIG_LIVE_BLOCK_MIGRATION
592 if (cap->value->capability == MIGRATION_CAPABILITY_BLOCK
593 && cap->value->state) {
594 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
595 "block migration");
596 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
597 continue;
598 }
599#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800600 if (cap->value->capability == MIGRATION_CAPABILITY_X_COLO) {
601 if (!colo_supported()) {
602 error_setg(errp, "COLO is not currently supported, please"
603 " configure with --enable-colo option in order to"
604 " support COLO feature");
605 continue;
606 }
607 }
Orit Wasserman00458432012-08-06 21:42:48 +0300608 s->enabled_capabilities[cap->value->capability] = cap->value->state;
609 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000610
611 if (migrate_postcopy_ram()) {
612 if (migrate_use_compression()) {
613 /* The decompression threads asynchronously write into RAM
614 * rather than use the atomic copies needed to avoid
615 * userfaulting. It should be possible to fix the decompression
616 * threads for compatibility in future.
617 */
618 error_report("Postcopy is not currently compatible with "
619 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000620 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000621 false;
622 }
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100623 /* This check is reasonably expensive, so only when it's being
624 * set the first time, also it's only the destination that needs
625 * special support.
626 */
627 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
628 !postcopy_ram_supported_by_host()) {
629 /* postcopy_ram_supported_by_host will have emitted a more
630 * detailed message
631 */
632 error_report("Postcopy is not supported");
633 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
634 false;
635 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000636 }
Orit Wasserman00458432012-08-06 21:42:48 +0300637}
638
Eric Blake7f375e02016-09-08 22:14:16 -0500639void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800640{
641 MigrationState *s = migrate_get_current();
642
Eric Blake7f375e02016-09-08 22:14:16 -0500643 if (params->has_compress_level &&
644 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100645 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
646 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800647 return;
648 }
Eric Blake7f375e02016-09-08 22:14:16 -0500649 if (params->has_compress_threads &&
650 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100651 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
652 "compress_threads",
653 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800654 return;
655 }
Eric Blake7f375e02016-09-08 22:14:16 -0500656 if (params->has_decompress_threads &&
657 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100658 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
659 "decompress_threads",
660 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800661 return;
662 }
Eric Blake7f375e02016-09-08 22:14:16 -0500663 if (params->has_cpu_throttle_initial &&
664 (params->cpu_throttle_initial < 1 ||
665 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400666 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400667 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400668 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530669 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400670 }
Eric Blake7f375e02016-09-08 22:14:16 -0500671 if (params->has_cpu_throttle_increment &&
672 (params->cpu_throttle_increment < 1 ||
673 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400674 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400675 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400676 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530677 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400678 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530679 if (params->has_max_bandwidth &&
680 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
681 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
682 " range of 0 to %zu bytes/second", SIZE_MAX);
683 return;
684 }
685 if (params->has_downtime_limit &&
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300686 (params->downtime_limit < 0 ||
687 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
688 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
689 "the range of 0 to %d milliseconds",
690 MAX_MIGRATE_DOWNTIME);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530691 return;
692 }
zhanghailiang68b53592016-10-27 14:43:01 +0800693 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
694 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
695 "x_checkpoint_delay",
696 "is invalid, it should be positive");
697 }
Liang Li85de8322015-03-23 16:32:28 +0800698
Eric Blake7f375e02016-09-08 22:14:16 -0500699 if (params->has_compress_level) {
700 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800701 }
Eric Blake7f375e02016-09-08 22:14:16 -0500702 if (params->has_compress_threads) {
703 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800704 }
Eric Blake7f375e02016-09-08 22:14:16 -0500705 if (params->has_decompress_threads) {
706 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800707 }
Eric Blake7f375e02016-09-08 22:14:16 -0500708 if (params->has_cpu_throttle_initial) {
709 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400710 }
Eric Blake7f375e02016-09-08 22:14:16 -0500711 if (params->has_cpu_throttle_increment) {
712 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400713 }
Eric Blake7f375e02016-09-08 22:14:16 -0500714 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100715 g_free(s->parameters.tls_creds);
Eric Blake7f375e02016-09-08 22:14:16 -0500716 s->parameters.tls_creds = g_strdup(params->tls_creds);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100717 }
Eric Blake7f375e02016-09-08 22:14:16 -0500718 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100719 g_free(s->parameters.tls_hostname);
Eric Blake7f375e02016-09-08 22:14:16 -0500720 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100721 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530722 if (params->has_max_bandwidth) {
723 s->parameters.max_bandwidth = params->max_bandwidth;
724 if (s->to_dst_file) {
725 qemu_file_set_rate_limit(s->to_dst_file,
726 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
727 }
728 }
729 if (params->has_downtime_limit) {
730 s->parameters.downtime_limit = params->downtime_limit;
731 }
zhanghailiang68b53592016-10-27 14:43:01 +0800732
733 if (params->has_x_checkpoint_delay) {
734 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800735 if (migration_in_colo_state()) {
736 colo_checkpoint_notify(s);
737 }
zhanghailiang68b53592016-10-27 14:43:01 +0800738 }
Juan Quintela2833c592017-04-05 18:32:37 +0200739 if (params->has_block_incremental) {
740 s->parameters.block_incremental = params->block_incremental;
741 }
Liang Li85de8322015-03-23 16:32:28 +0800742}
743
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100744
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000745void qmp_migrate_start_postcopy(Error **errp)
746{
747 MigrationState *s = migrate_get_current();
748
749 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000750 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000751 " the start of migration");
752 return;
753 }
754
755 if (s->state == MIGRATION_STATUS_NONE) {
756 error_setg(errp, "Postcopy must be started after migration has been"
757 " started");
758 return;
759 }
760 /*
761 * we don't error if migration has finished since that would be racy
762 * with issuing this command.
763 */
764 atomic_set(&s->start_postcopy, true);
765}
766
aliguori065e2812008-11-11 16:46:33 +0000767/* shared migration helpers */
768
zhanghailiang48781e52015-12-16 11:47:33 +0000769void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000770{
zhanghailiang48781e52015-12-16 11:47:33 +0000771 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200772 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200773 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000774 }
775}
776
Juan Quintela2833c592017-04-05 18:32:37 +0200777void migrate_set_block_enabled(bool value, Error **errp)
778{
779 MigrationCapabilityStatusList *cap;
780
781 cap = g_new0(MigrationCapabilityStatusList, 1);
782 cap->value = g_new0(MigrationCapabilityStatus, 1);
783 cap->value->capability = MIGRATION_CAPABILITY_BLOCK;
784 cap->value->state = value;
785 qmp_migrate_set_capabilities(cap, errp);
786 qapi_free_MigrationCapabilityStatusList(cap);
787}
788
789static void migrate_set_block_incremental(MigrationState *s, bool value)
790{
791 s->parameters.block_incremental = value;
792}
793
794static void block_cleanup_parameters(MigrationState *s)
795{
796 if (s->must_remove_block_options) {
797 /* setting to false can never fail */
798 migrate_set_block_enabled(false, &error_abort);
799 migrate_set_block_incremental(s, false);
800 s->must_remove_block_options = false;
801 }
802}
803
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100804static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000805{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100806 MigrationState *s = opaque;
807
808 qemu_bh_delete(s->cleanup_bh);
809 s->cleanup_bh = NULL;
810
zhanghailiang89a02a92016-01-15 11:37:42 +0800811 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100812 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100813 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000814 if (s->migration_thread_running) {
815 qemu_thread_join(&s->thread);
816 s->migration_thread_running = false;
817 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100818 qemu_mutex_lock_iothread();
819
Liang Li8706d2d2015-03-23 16:32:17 +0800820 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800821 qemu_fclose(s->to_dst_file);
822 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000823 }
824
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000825 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
826 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100827
Liang Li94f5a432015-11-02 15:37:00 +0800828 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000829 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800830 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100831 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100832
833 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200834 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000835}
836
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100837void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000838{
Peter Maydell25174052016-10-21 18:41:45 +0100839 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +0800840 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000841 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
842 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100843 if (!s->error) {
844 s->error = error_copy(error);
845 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100846 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200847 block_cleanup_parameters(s);
Juan Quintela458cf282011-02-22 23:32:54 +0100848}
849
Juan Quintela0edda1c2010-05-11 16:28:39 +0200850static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000851{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000852 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800853 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100854 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000855
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000856 if (s->rp_state.from_dst_file) {
857 /* shutdown the rp socket, so causing the rp thread to shutdown */
858 qemu_file_shutdown(s->rp_state.from_dst_file);
859 }
860
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000861 do {
862 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000863 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000864 break;
865 }
zhanghailiang48781e52015-12-16 11:47:33 +0000866 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +0800867 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000868
869 /*
870 * If we're unlucky the migration code might be stuck somewhere in a
871 * send/write while the network has failed and is waiting to timeout;
872 * if we've got shutdown(2) available then we can force it to quit.
873 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
874 * called in a bh, so there is no race against this cancel.
875 */
zhanghailiang31194732015-03-13 16:08:38 +0800876 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000877 qemu_file_shutdown(f);
878 }
zhanghailiang1d2acc32017-01-24 15:59:52 +0800879 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
880 Error *local_err = NULL;
881
882 bdrv_invalidate_cache_all(&local_err);
883 if (local_err) {
884 error_report_err(local_err);
885 } else {
886 s->block_inactive = false;
887 }
888 }
Juan Quintela2833c592017-04-05 18:32:37 +0200889 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000890}
891
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100892void add_migration_state_change_notifier(Notifier *notify)
893{
894 notifier_list_add(&migration_state_notifiers, notify);
895}
896
897void remove_migration_state_change_notifier(Notifier *notify)
898{
Paolo Bonzini31552522012-01-13 17:34:01 +0100899 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100900}
901
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200902bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200903{
zhanghailiang31194732015-03-13 16:08:38 +0800904 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200905}
906
Juan Quintela70736932011-02-23 00:43:59 +0100907bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100908{
zhanghailiang31194732015-03-13 16:08:38 +0800909 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100910}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200911
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200912bool migration_has_failed(MigrationState *s)
913{
zhanghailiang31194732015-03-13 16:08:38 +0800914 return (s->state == MIGRATION_STATUS_CANCELLED ||
915 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200916}
917
Juan Quintela57273092017-03-20 22:25:28 +0100918bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000919{
Juan Quintela57273092017-03-20 22:25:28 +0100920 MigrationState *s = migrate_get_current();
921
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000922 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
923}
924
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000925bool migration_in_postcopy_after_devices(MigrationState *s)
926{
Juan Quintela57273092017-03-20 22:25:28 +0100927 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000928}
929
Juan Quintelafab35002017-03-22 17:36:57 +0100930bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530931{
Juan Quintelafab35002017-03-22 17:36:57 +0100932 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530933
934 switch (s->state) {
935 case MIGRATION_STATUS_NONE:
936 case MIGRATION_STATUS_CANCELLED:
937 case MIGRATION_STATUS_COMPLETED:
938 case MIGRATION_STATUS_FAILED:
939 return true;
940 case MIGRATION_STATUS_SETUP:
941 case MIGRATION_STATUS_CANCELLING:
942 case MIGRATION_STATUS_ACTIVE:
943 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
944 case MIGRATION_STATUS_COLO:
945 return false;
946 case MIGRATION_STATUS__MAX:
947 g_assert_not_reached();
948 }
949
950 return false;
951}
952
Juan Quintelaa0762d92017-04-05 21:00:09 +0200953MigrationState *migrate_init(void)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200954{
Juan Quintela17549e82011-10-05 13:50:43 +0200955 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300956
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000957 /*
958 * Reinitialise all migration state, except
959 * parameters/capabilities that the user set, and
960 * locks.
961 */
962 s->bytes_xfer = 0;
963 s->xfer_limit = 0;
964 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +0800965 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000966 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000967 s->rp_state.from_dst_file = NULL;
968 s->rp_state.error = false;
969 s->mbps = 0.0;
970 s->downtime = 0;
971 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000972 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000973 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000974 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000975 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100976 error_free(s->error);
977 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +0100978
zhanghailiang48781e52015-12-16 11:47:33 +0000979 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200980
Alex Blighbc72ad62013-08-21 16:03:08 +0100981 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200982 return s;
983}
Juan Quintelacab30142011-02-22 23:54:21 +0100984
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600985static GSList *migration_blockers;
986
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530987int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600988{
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +0530989 if (only_migratable) {
990 error_propagate(errp, error_copy(reason));
991 error_prepend(errp, "disallowing migration blocker "
992 "(--only_migratable) for: ");
993 return -EACCES;
994 }
995
Juan Quintelafab35002017-03-22 17:36:57 +0100996 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530997 migration_blockers = g_slist_prepend(migration_blockers, reason);
998 return 0;
999 }
1000
1001 error_propagate(errp, error_copy(reason));
1002 error_prepend(errp, "disallowing migration blocker (migration in "
1003 "progress) for: ");
1004 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001005}
1006
1007void migrate_del_blocker(Error *reason)
1008{
1009 migration_blockers = g_slist_remove(migration_blockers, reason);
1010}
1011
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001012void qmp_migrate_incoming(const char *uri, Error **errp)
1013{
1014 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001015 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001016
1017 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001018 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001019 return;
1020 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001021 if (!once) {
1022 error_setg(errp, "The incoming migration has already been started");
1023 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001024
1025 qemu_start_incoming_migration(uri, &local_err);
1026
1027 if (local_err) {
1028 error_propagate(errp, local_err);
1029 return;
1030 }
1031
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001032 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001033}
1034
Greg Kurz24f39022016-05-04 21:44:19 +02001035bool migration_is_blocked(Error **errp)
1036{
1037 if (qemu_savevm_state_blocked(errp)) {
1038 return true;
1039 }
1040
1041 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001042 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001043 return true;
1044 }
1045
1046 return false;
1047}
1048
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001049void qmp_migrate(const char *uri, bool has_blk, bool blk,
1050 bool has_inc, bool inc, bool has_detach, bool detach,
1051 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001052{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001053 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001054 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001055 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001056
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001057 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001058 s->state == MIGRATION_STATUS_CANCELLING ||
1059 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001060 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001061 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001062 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001063 if (runstate_check(RUN_STATE_INMIGRATE)) {
1064 error_setg(errp, "Guest is waiting for an incoming migration");
1065 return;
1066 }
1067
Greg Kurz24f39022016-05-04 21:44:19 +02001068 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001069 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001070 }
1071
Juan Quintela2833c592017-04-05 18:32:37 +02001072 if ((has_blk && blk) || (has_inc && inc)) {
1073 if (migrate_use_block() || migrate_use_block_incremental()) {
1074 error_setg(errp, "Command options are incompatible with "
1075 "current migration capabilities");
1076 return;
1077 }
1078 migrate_set_block_enabled(true, &local_err);
1079 if (local_err) {
1080 error_propagate(errp, local_err);
1081 return;
1082 }
1083 s->must_remove_block_options = true;
1084 }
1085
1086 if (has_inc && inc) {
1087 migrate_set_block_incremental(s, true);
1088 }
1089
Juan Quintelaa0762d92017-04-05 21:00:09 +02001090 s = migrate_init();
Juan Quintelacab30142011-02-22 23:54:21 +01001091
1092 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001093 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001094#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001095 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001096 rdma_start_outgoing_migration(s, p, &local_err);
1097#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001098 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001099 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001100 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001101 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001102 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001103 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001104 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001105 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1106 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001107 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1108 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001109 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001110 }
1111
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001112 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001113 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001114 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001115 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001116 }
Juan Quintelacab30142011-02-22 23:54:21 +01001117}
1118
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001119void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001120{
Juan Quintela17549e82011-10-05 13:50:43 +02001121 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001122}
1123
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001124void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1125{
1126 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001127 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001128
1129 /* Check for truncation */
1130 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001131 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1132 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001133 return;
1134 }
1135
Orit Wassermana5615b12014-01-30 20:08:36 +02001136 /* Cache should not be larger than guest ram size */
1137 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001138 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1139 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001140 return;
1141 }
1142
Orit Wassermanc91e6812014-01-30 20:08:34 +02001143 new_size = xbzrle_cache_resize(value);
1144 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001145 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1146 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001147 return;
1148 }
1149
1150 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001151}
1152
1153int64_t qmp_query_migrate_cache_size(Error **errp)
1154{
1155 return migrate_xbzrle_cache_size();
1156}
1157
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001158void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001159{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301160 MigrationParameters p = {
1161 .has_max_bandwidth = true,
1162 .max_bandwidth = value,
1163 };
Juan Quintelacab30142011-02-22 23:54:21 +01001164
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301165 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001166}
1167
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001168void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001169{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001170 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1171 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1172 "the range of 0 to %d seconds",
1173 MAX_MIGRATE_DOWNTIME_SECONDS);
1174 return;
1175 }
1176
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301177 value *= 1000; /* Convert to milliseconds */
1178 value = MAX(0, MIN(INT64_MAX, value));
1179
1180 MigrationParameters p = {
1181 .has_downtime_limit = true,
1182 .downtime_limit = value,
1183 };
1184
1185 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001186}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001187
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001188bool migrate_release_ram(void)
1189{
1190 MigrationState *s;
1191
1192 s = migrate_get_current();
1193
1194 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1195}
1196
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001197bool migrate_postcopy_ram(void)
1198{
1199 MigrationState *s;
1200
1201 s = migrate_get_current();
1202
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001203 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001204}
1205
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001206bool migrate_auto_converge(void)
1207{
1208 MigrationState *s;
1209
1210 s = migrate_get_current();
1211
1212 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1213}
1214
Peter Lieven323004a2013-07-18 09:48:50 +02001215bool migrate_zero_blocks(void)
1216{
1217 MigrationState *s;
1218
1219 s = migrate_get_current();
1220
1221 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1222}
1223
Liang Li8706d2d2015-03-23 16:32:17 +08001224bool migrate_use_compression(void)
1225{
Liang Lidde4e692015-03-23 16:32:26 +08001226 MigrationState *s;
1227
1228 s = migrate_get_current();
1229
1230 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001231}
1232
1233int migrate_compress_level(void)
1234{
1235 MigrationState *s;
1236
1237 s = migrate_get_current();
1238
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001239 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001240}
1241
1242int migrate_compress_threads(void)
1243{
1244 MigrationState *s;
1245
1246 s = migrate_get_current();
1247
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001248 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001249}
1250
Liang Li3fcb38c2015-03-23 16:32:18 +08001251int migrate_decompress_threads(void)
1252{
1253 MigrationState *s;
1254
1255 s = migrate_get_current();
1256
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001257 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001258}
1259
Juan Quintelab05dc722015-07-07 14:44:05 +02001260bool migrate_use_events(void)
1261{
1262 MigrationState *s;
1263
1264 s = migrate_get_current();
1265
1266 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1267}
1268
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001269int migrate_use_xbzrle(void)
1270{
1271 MigrationState *s;
1272
1273 s = migrate_get_current();
1274
1275 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1276}
1277
1278int64_t migrate_xbzrle_cache_size(void)
1279{
1280 MigrationState *s;
1281
1282 s = migrate_get_current();
1283
1284 return s->xbzrle_cache_size;
1285}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001286
Juan Quintela2833c592017-04-05 18:32:37 +02001287bool migrate_use_block(void)
1288{
1289 MigrationState *s;
1290
1291 s = migrate_get_current();
1292
1293 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1294}
1295
1296bool migrate_use_block_incremental(void)
1297{
1298 MigrationState *s;
1299
1300 s = migrate_get_current();
1301
1302 return s->parameters.block_incremental;
1303}
1304
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001305/* migration thread support */
1306/*
1307 * Something bad happened to the RP stream, mark an error
1308 * The caller shall print or trace something to indicate why
1309 */
1310static void mark_source_rp_bad(MigrationState *s)
1311{
1312 s->rp_state.error = true;
1313}
1314
1315static struct rp_cmd_args {
1316 ssize_t len; /* -1 = variable */
1317 const char *name;
1318} rp_cmd_args[] = {
1319 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1320 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1321 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001322 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1323 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001324 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1325};
1326
1327/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001328 * Process a request for pages received on the return path,
1329 * We're allowed to send more than requested (e.g. to round to our page size)
1330 * and we don't need to send pages that have already been sent.
1331 */
1332static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1333 ram_addr_t start, size_t len)
1334{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001335 long our_host_ps = getpagesize();
1336
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001337 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001338
1339 /*
1340 * Since we currently insist on matching page sizes, just sanity check
1341 * we're being asked for whole host pages.
1342 */
1343 if (start & (our_host_ps-1) ||
1344 (len & (our_host_ps-1))) {
1345 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1346 " len: %zd", __func__, start, len);
1347 mark_source_rp_bad(ms);
1348 return;
1349 }
1350
Juan Quintela96506892017-03-14 18:41:03 +01001351 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001352 mark_source_rp_bad(ms);
1353 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001354}
1355
1356/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001357 * Handles messages sent on the return path towards the source VM
1358 *
1359 */
1360static void *source_return_path_thread(void *opaque)
1361{
1362 MigrationState *ms = opaque;
1363 QEMUFile *rp = ms->rp_state.from_dst_file;
1364 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001365 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001366 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001367 ram_addr_t start = 0; /* =0 to silence warning */
1368 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001369 int res;
1370
1371 trace_source_return_path_thread_entry();
1372 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1373 migration_is_setup_or_active(ms->state)) {
1374 trace_source_return_path_thread_loop_top();
1375 header_type = qemu_get_be16(rp);
1376 header_len = qemu_get_be16(rp);
1377
1378 if (header_type >= MIG_RP_MSG_MAX ||
1379 header_type == MIG_RP_MSG_INVALID) {
1380 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1381 header_type, header_len);
1382 mark_source_rp_bad(ms);
1383 goto out;
1384 }
1385
1386 if ((rp_cmd_args[header_type].len != -1 &&
1387 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001388 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001389 error_report("RP: Received '%s' message (0x%04x) with"
1390 "incorrect length %d expecting %zu",
1391 rp_cmd_args[header_type].name, header_type, header_len,
1392 (size_t)rp_cmd_args[header_type].len);
1393 mark_source_rp_bad(ms);
1394 goto out;
1395 }
1396
1397 /* We know we've got a valid header by this point */
1398 res = qemu_get_buffer(rp, buf, header_len);
1399 if (res != header_len) {
1400 error_report("RP: Failed reading data for message 0x%04x"
1401 " read %d expected %d",
1402 header_type, res, header_len);
1403 mark_source_rp_bad(ms);
1404 goto out;
1405 }
1406
1407 /* OK, we have the message and the data */
1408 switch (header_type) {
1409 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001410 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001411 trace_source_return_path_thread_shut(sibling_error);
1412 if (sibling_error) {
1413 error_report("RP: Sibling indicated error %d", sibling_error);
1414 mark_source_rp_bad(ms);
1415 }
1416 /*
1417 * We'll let the main thread deal with closing the RP
1418 * we could do a shutdown(2) on it, but we're the only user
1419 * anyway, so there's nothing gained.
1420 */
1421 goto out;
1422
1423 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001424 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001425 trace_source_return_path_thread_pong(tmp32);
1426 break;
1427
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001428 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001429 start = ldq_be_p(buf);
1430 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001431 migrate_handle_rp_req_pages(ms, NULL, start, len);
1432 break;
1433
1434 case MIG_RP_MSG_REQ_PAGES_ID:
1435 expected_len = 12 + 1; /* header + termination */
1436
1437 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001438 start = ldq_be_p(buf);
1439 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001440 /* Now we expect an idstr */
1441 tmp32 = buf[12]; /* Length of the following idstr */
1442 buf[13 + tmp32] = '\0';
1443 expected_len += tmp32;
1444 }
1445 if (header_len != expected_len) {
1446 error_report("RP: Req_Page_id with length %d expecting %zd",
1447 header_len, expected_len);
1448 mark_source_rp_bad(ms);
1449 goto out;
1450 }
1451 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1452 break;
1453
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001454 default:
1455 break;
1456 }
1457 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001458 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001459 trace_source_return_path_thread_bad_end();
1460 mark_source_rp_bad(ms);
1461 }
1462
1463 trace_source_return_path_thread_end();
1464out:
1465 ms->rp_state.from_dst_file = NULL;
1466 qemu_fclose(rp);
1467 return NULL;
1468}
1469
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001470static int open_return_path_on_source(MigrationState *ms)
1471{
1472
zhanghailiang89a02a92016-01-15 11:37:42 +08001473 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001474 if (!ms->rp_state.from_dst_file) {
1475 return -1;
1476 }
1477
1478 trace_open_return_path_on_source();
1479 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1480 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1481
1482 trace_open_return_path_on_source_continue();
1483
1484 return 0;
1485}
1486
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001487/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1488static int await_return_path_close_on_source(MigrationState *ms)
1489{
1490 /*
1491 * If this is a normal exit then the destination will send a SHUT and the
1492 * rp_thread will exit, however if there's an error we need to cause
1493 * it to exit.
1494 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001495 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001496 /*
1497 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1498 * waiting for the destination.
1499 */
1500 qemu_file_shutdown(ms->rp_state.from_dst_file);
1501 mark_source_rp_bad(ms);
1502 }
1503 trace_await_return_path_close_on_source_joining();
1504 qemu_thread_join(&ms->rp_state.rp_thread);
1505 trace_await_return_path_close_on_source_close();
1506 return ms->rp_state.error;
1507}
1508
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001509/*
1510 * Switch from normal iteration to postcopy
1511 * Returns non-0 on error
1512 */
1513static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1514{
1515 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001516 QIOChannelBuffer *bioc;
1517 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001518 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001519 bool restart_block = false;
zhanghailiang48781e52015-12-16 11:47:33 +00001520 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001521 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1522
1523 trace_postcopy_start();
1524 qemu_mutex_lock_iothread();
1525 trace_postcopy_start_set_run();
1526
1527 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1528 *old_vm_running = runstate_is_running();
1529 global_state_store();
1530 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001531 if (ret < 0) {
1532 goto fail;
1533 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001534
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001535 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001536 if (ret < 0) {
1537 goto fail;
1538 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001539 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001540
1541 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001542 * Cause any non-postcopiable, but iterative devices to
1543 * send out their final data.
1544 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08001545 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001546
1547 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001548 * in Finish migrate and with the io-lock held everything should
1549 * be quiet, but we've potentially still got dirty pages and we
1550 * need to tell the destination to throw any pages it's already received
1551 * that are dirty
1552 */
1553 if (ram_postcopy_send_discard_bitmap(ms)) {
1554 error_report("postcopy send discard bitmap failed");
1555 goto fail;
1556 }
1557
1558 /*
1559 * send rest of state - note things that are doing postcopy
1560 * will notice we're in POSTCOPY_ACTIVE and not actually
1561 * wrap their state up here
1562 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001563 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001564 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001565 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001566
1567 /*
1568 * While loading the device state we may trigger page transfer
1569 * requests and the fd must be free to process those, and thus
1570 * the destination must read the whole device state off the fd before
1571 * it starts processing it. Unfortunately the ad-hoc migration format
1572 * doesn't allow the destination to know the size to read without fully
1573 * parsing it through each devices load-state code (especially the open
1574 * coded devices that use get/put).
1575 * So we wrap the device state up in a package with a length at the start;
1576 * to do this we use a qemu_buf to hold the whole of the device state.
1577 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001578 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001579 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001580 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1581 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001582
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001583 /*
1584 * Make sure the receiver can get incoming pages before we send the rest
1585 * of the state
1586 */
1587 qemu_savevm_send_postcopy_listen(fb);
1588
Fam Zhenga1fbe752017-06-17 00:06:58 +08001589 qemu_savevm_state_complete_precopy(fb, false, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001590 qemu_savevm_send_ping(fb, 3);
1591
1592 qemu_savevm_send_postcopy_run(fb);
1593
1594 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001595
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001596 /* Last point of recovery; as soon as we send the package the destination
1597 * can open devices and potentially start running.
1598 * Lets just check again we've not got any errors.
1599 */
1600 ret = qemu_file_get_error(ms->to_dst_file);
1601 if (ret) {
1602 error_report("postcopy_start: Migration stream errored (pre package)");
1603 goto fail_closefb;
1604 }
1605
1606 restart_block = false;
1607
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001608 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001609 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001610 goto fail_closefb;
1611 }
1612 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001613
1614 /* Send a notify to give a chance for anything that needs to happen
1615 * at the transition to postcopy and after the device state; in particular
1616 * spice needs to trigger a transition now
1617 */
1618 ms->postcopy_after_devices = true;
1619 notifier_list_notify(&migration_state_notifiers, ms);
1620
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001621 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1622
1623 qemu_mutex_unlock_iothread();
1624
1625 /*
1626 * Although this ping is just for debug, it could potentially be
1627 * used for getting a better measurement of downtime at the source.
1628 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001629 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001630
Pavel Butsykinced1c612017-02-03 18:23:21 +03001631 if (migrate_release_ram()) {
1632 ram_postcopy_migrated_memory_release(ms);
1633 }
1634
zhanghailiang89a02a92016-01-15 11:37:42 +08001635 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001636 if (ret) {
1637 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001638 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001639 MIGRATION_STATUS_FAILED);
1640 }
1641
1642 return ret;
1643
1644fail_closefb:
1645 qemu_fclose(fb);
1646fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001647 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001648 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001649 if (restart_block) {
1650 /* A failure happened early enough that we know the destination hasn't
1651 * accessed block devices, so we're safe to recover.
1652 */
1653 Error *local_err = NULL;
1654
1655 bdrv_invalidate_cache_all(&local_err);
1656 if (local_err) {
1657 error_report_err(local_err);
1658 }
1659 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001660 qemu_mutex_unlock_iothread();
1661 return -1;
1662}
1663
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001664/**
1665 * migration_completion: Used by migration_thread when there's not much left.
1666 * The caller 'breaks' the loop when this returns.
1667 *
1668 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001669 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001670 * @*old_vm_running: Pointer to old_vm_running flag
1671 * @*start_time: Pointer to time to update
1672 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001673static void migration_completion(MigrationState *s, int current_active_state,
1674 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001675 int64_t *start_time)
1676{
1677 int ret;
1678
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001679 if (s->state == MIGRATION_STATUS_ACTIVE) {
1680 qemu_mutex_lock_iothread();
1681 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1682 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1683 *old_vm_running = runstate_is_running();
1684 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001685
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001686 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08001687 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001688 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001689 if (ret >= 0) {
1690 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08001691 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
1692 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001693 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08001694 if (inactivate && ret >= 0) {
1695 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001696 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001697 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001698 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001699
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001700 if (ret < 0) {
1701 goto fail;
1702 }
1703 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1704 trace_migration_completion_postcopy_end();
1705
zhanghailiang89a02a92016-01-15 11:37:42 +08001706 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001707 trace_migration_completion_postcopy_end_after_complete();
1708 }
1709
1710 /*
1711 * If rp was opened we must clean up the thread before
1712 * cleaning everything else up (since if there are no failures
1713 * it will wait for the destination to send it's status in
1714 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001715 */
Peter Xu0425dc92017-05-31 18:35:34 +08001716 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001717 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08001718 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001719 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08001720 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001721 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001722 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001723 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001724 }
1725
zhanghailiang89a02a92016-01-15 11:37:42 +08001726 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001727 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001728 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001729 }
1730
zhanghailiang0b827d52016-10-27 14:42:54 +08001731 if (!migrate_colo_enabled()) {
1732 migrate_set_state(&s->state, current_active_state,
1733 MIGRATION_STATUS_COMPLETED);
1734 }
1735
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001736 return;
1737
Greg Kurzfe904ea2016-05-18 15:44:36 +02001738fail_invalidate:
1739 /* If not doing postcopy, vm_start() will be called: let's regain
1740 * control on images.
1741 */
1742 if (s->state == MIGRATION_STATUS_ACTIVE) {
1743 Error *local_err = NULL;
1744
zhanghailiang1d2acc32017-01-24 15:59:52 +08001745 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001746 bdrv_invalidate_cache_all(&local_err);
1747 if (local_err) {
1748 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001749 } else {
1750 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02001751 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001752 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001753 }
1754
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001755fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001756 migrate_set_state(&s->state, current_active_state,
1757 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001758}
1759
zhanghailiang35a6ed42016-10-27 14:42:52 +08001760bool migrate_colo_enabled(void)
1761{
1762 MigrationState *s = migrate_get_current();
1763 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
1764}
1765
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001766/*
1767 * Master migration thread on the source VM.
1768 * It drives the migration and pumps the data down the outgoing channel.
1769 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001770static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001771{
Juan Quintela9848a402012-12-19 09:55:50 +01001772 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001773 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001774 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1775 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001776 int64_t initial_bytes = 0;
Peter Xufaec0662017-04-01 16:18:43 +08001777 /*
1778 * The final stage happens when the remaining data is smaller than
1779 * this threshold; it's calculated from the requested downtime and
1780 * measured bandwidth
1781 */
1782 int64_t threshold_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001783 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001784 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001785 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001786 bool entered_postcopy = false;
1787 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1788 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang0b827d52016-10-27 14:42:54 +08001789 bool enable_colo = migrate_colo_enabled();
Juan Quintela76f59332012-10-03 20:16:24 +02001790
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001791 rcu_register_thread();
1792
zhanghailiang89a02a92016-01-15 11:37:42 +08001793 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001794
Peter Xu62a02652017-06-14 15:55:58 +08001795 /*
1796 * If we opened the return path, we need to make sure dst has it
1797 * opened as well.
1798 */
1799 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001800 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001801 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001802
1803 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001804 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08001805 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001806
Peter Xu0425dc92017-05-31 18:35:34 +08001807 if (migrate_postcopy_ram()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001808 /*
1809 * Tell the destination that we *might* want to do postcopy later;
1810 * if the other end can't do postcopy it should fail now, nice and
1811 * early.
1812 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001813 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001814 }
1815
Juan Quintelaa0762d92017-04-05 21:00:09 +02001816 qemu_savevm_state_begin(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001817
Alex Blighbc72ad62013-08-21 16:03:08 +01001818 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00001819 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1820 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001821
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001822 trace_migration_thread_setup_complete();
1823
1824 while (s->state == MIGRATION_STATUS_ACTIVE ||
1825 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001826 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001827 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001828
zhanghailiang89a02a92016-01-15 11:37:42 +08001829 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001830 uint64_t pend_post, pend_nonpost;
1831
Peter Xufaec0662017-04-01 16:18:43 +08001832 qemu_savevm_state_pending(s->to_dst_file, threshold_size,
1833 &pend_nonpost, &pend_post);
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001834 pending_size = pend_nonpost + pend_post;
Peter Xufaec0662017-04-01 16:18:43 +08001835 trace_migrate_pending(pending_size, threshold_size,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001836 pend_post, pend_nonpost);
Peter Xufaec0662017-04-01 16:18:43 +08001837 if (pending_size && pending_size >= threshold_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001838 /* Still a significant amount to transfer */
1839
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001840 if (migrate_postcopy_ram() &&
1841 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
Peter Xufaec0662017-04-01 16:18:43 +08001842 pend_nonpost <= threshold_size &&
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001843 atomic_read(&s->start_postcopy)) {
1844
1845 if (!postcopy_start(s, &old_vm_running)) {
1846 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1847 entered_postcopy = true;
1848 }
1849
1850 continue;
1851 }
1852 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001853 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001854 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001855 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001856 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001857 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001858 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001859 }
1860 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001861
zhanghailiang89a02a92016-01-15 11:37:42 +08001862 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001863 migrate_set_state(&s->state, current_active_state,
1864 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001865 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001866 break;
1867 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001868 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001869 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001870 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1871 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001872 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001873 double bandwidth = (double)transferred_bytes / time_spent;
Peter Xufaec0662017-04-01 16:18:43 +08001874 threshold_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001875
Wei Yang5b648de2016-01-24 14:09:57 +00001876 s->mbps = (((double) transferred_bytes * 8.0) /
1877 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04001878
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001879 trace_migrate_transferred(transferred_bytes, time_spent,
Peter Xufaec0662017-04-01 16:18:43 +08001880 bandwidth, threshold_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001881 /* if we haven't sent anything, we don't want to recalculate
1882 10000 is a small enough number for our purposes */
Juan Quintela93604472017-06-06 19:49:03 +02001883 if (ram_counters.dirty_pages_rate && transferred_bytes > 10000) {
1884 s->expected_downtime = ram_counters.dirty_pages_rate *
Juan Quintela20afaed2017-03-21 09:09:14 +01001885 qemu_target_page_size() / bandwidth;
Juan Quintela90f8ae72013-02-01 13:22:37 +01001886 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001887
zhanghailiang89a02a92016-01-15 11:37:42 +08001888 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001889 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08001890 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001891 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001892 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001893 /* usleep expects microseconds */
1894 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1895 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001896 }
1897
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001898 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001899 /* If we enabled cpu throttling for auto-converge, turn it off. */
1900 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001901 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001902
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001903 qemu_mutex_lock_iothread();
zhanghailiang0b827d52016-10-27 14:42:54 +08001904 /*
1905 * The resource has been allocated by migration will be reused in COLO
1906 * process, so don't release them.
1907 */
1908 if (!enable_colo) {
1909 qemu_savevm_state_cleanup();
1910 }
zhanghailiang31194732015-03-13 16:08:38 +08001911 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001912 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001913 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001914 if (!entered_postcopy) {
1915 s->downtime = end_time - start_time;
1916 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001917 if (s->total_time) {
1918 s->mbps = (((double) transferred_bytes * 8.0) /
1919 ((double) s->total_time)) / 1000;
1920 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001921 runstate_set(RUN_STATE_POSTMIGRATE);
1922 } else {
zhanghailiang0b827d52016-10-27 14:42:54 +08001923 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
1924 migrate_start_colo_process(s);
1925 qemu_savevm_state_cleanup();
1926 /*
1927 * Fixme: we will run VM in COLO no matter its old running state.
1928 * After exited COLO, we will keep running.
1929 */
1930 old_vm_running = true;
1931 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001932 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001933 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01001934 } else {
1935 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
1936 runstate_set(RUN_STATE_POSTMIGRATE);
1937 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001938 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001939 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001940 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001941 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001942
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001943 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001944 return NULL;
1945}
1946
Juan Quintela9848a402012-12-19 09:55:50 +01001947void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001948{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301949 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001950 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001951
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01001952 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08001953 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301954 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01001955
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001956 /* Notify before starting migration thread */
1957 notifier_list_notify(&migration_state_notifiers, s);
1958
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001959 /*
1960 * Open the return path; currently for postcopy but other things might
1961 * also want it.
1962 */
1963 if (migrate_postcopy_ram()) {
1964 if (open_return_path_on_source(s)) {
1965 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00001966 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001967 MIGRATION_STATUS_FAILED);
1968 migrate_fd_cleanup(s);
1969 return;
1970 }
1971 }
1972
Liang Li8706d2d2015-03-23 16:32:17 +08001973 migrate_compress_threads_create();
Pankaj Gupta009fad72017-01-23 19:12:56 +05301974 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001975 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001976 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001977}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001978
Peter Xu52722982017-06-27 12:10:14 +08001979static Property migration_properties[] = {
1980 DEFINE_PROP_BOOL("store-global-state", MigrationState,
1981 store_global_state, true),
1982 DEFINE_PROP_END_OF_LIST(),
1983};
1984
Peter Xue5cb7e72017-06-27 12:10:13 +08001985static void migration_class_init(ObjectClass *klass, void *data)
1986{
1987 DeviceClass *dc = DEVICE_CLASS(klass);
1988
1989 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08001990 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08001991}
1992
1993static void migration_instance_init(Object *obj)
1994{
1995 MigrationState *ms = MIGRATION_OBJ(obj);
1996
1997 ms->state = MIGRATION_STATUS_NONE;
1998 ms->xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE;
1999 ms->mbps = -1;
2000 ms->parameters = (MigrationParameters) {
2001 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
2002 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
2003 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
2004 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
2005 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
2006 .max_bandwidth = MAX_THROTTLE,
2007 .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME,
2008 .x_checkpoint_delay = DEFAULT_MIGRATE_X_CHECKPOINT_DELAY,
2009 };
2010 ms->parameters.tls_creds = g_strdup("");
2011 ms->parameters.tls_hostname = g_strdup("");
2012}
2013
2014static const TypeInfo migration_type = {
2015 .name = TYPE_MIGRATION,
2016 .parent = TYPE_DEVICE,
2017 .class_init = migration_class_init,
2018 .class_size = sizeof(MigrationClass),
2019 .instance_size = sizeof(MigrationState),
2020 .instance_init = migration_instance_init,
2021};
2022
2023static void register_migration_types(void)
2024{
2025 type_register_static(&migration_type);
2026}
2027
2028type_init(register_migration_types);