blob: b1b08250ce30d22e98c855657e9f3097237fdc8a [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"
Peter Xu4ffdb332017-06-27 12:10:18 +080045#include "hw/boards.h"
Peter Xu9d18af92017-06-27 12:10:19 +080046#include "monitor/monitor.h"
aliguori065e2812008-11-11 16:46:33 +000047
Jason J. Hernedc325622015-09-08 13:12:37 -040048#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000049
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010050/* Amount of time to allocate to each "chunk" of bandwidth-throttled
51 * data. */
52#define BUFFER_DELAY 100
53#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
54
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053055/* Time in milliseconds we are allowed to stop the source,
56 * for sending the last part */
57#define DEFAULT_MIGRATE_SET_DOWNTIME 300
58
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030059/* Maximum migrate downtime set to 2000 seconds */
60#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
61#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
62
Liang Li8706d2d2015-03-23 16:32:17 +080063/* Default compression thread count */
64#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080065/* Default decompression thread count, usually decompression is at
66 * least 4 times as fast as compression.*/
67#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080068/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
69#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040070/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040071#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
72#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080073
Orit Wasserman17ad9b32012-08-06 21:42:53 +030074/* Migration XBZRLE default cache size */
75#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
76
zhanghailiang68b53592016-10-27 14:43:01 +080077/* The delay time (in ms) between two COLO checkpoints
78 * Note: Please change this default value to 10000 when we support hybrid mode.
79 */
80#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
81
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010082static NotifierList migration_state_notifiers =
83 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
84
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000085static bool deferred_incoming;
86
Juan Quintelada6f1792017-04-24 17:37:14 +020087/* Messages sent on the return path from destination to source */
88enum mig_rp_message_type {
89 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
90 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
91 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
92
93 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
94 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
95
96 MIG_RP_MSG_MAX
97};
98
Juan Quintela17549e82011-10-05 13:50:43 +020099/* When we add fault tolerance, we could have several
100 migrations at once. For now we don't need to add
101 dynamic creation of migration */
102
Peter Xue5cb7e72017-06-27 12:10:13 +0800103static MigrationState *current_migration;
104
105void migration_object_init(void)
106{
Peter Xu4ffdb332017-06-27 12:10:18 +0800107 MachineState *ms = MACHINE(qdev_get_machine());
108
Peter Xue5cb7e72017-06-27 12:10:13 +0800109 /* This can only be called once. */
110 assert(!current_migration);
111 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800112
113 /*
114 * We cannot really do this in migration_instance_init() since at
115 * that time global properties are not yet applied, then this
116 * value will be definitely replaced by something else.
117 */
118 if (ms->enforce_config_section) {
119 current_migration->send_configuration = true;
120 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800121}
122
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100123/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200124MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200125{
Peter Xue5cb7e72017-06-27 12:10:13 +0800126 /* This can only be called after the object created. */
127 assert(current_migration);
128 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200129}
130
Peter Xu3df663e2017-06-27 12:10:15 +0800131void migration_only_migratable_set(void)
132{
133 migrate_get_current()->only_migratable = true;
134}
135
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100136MigrationIncomingState *migration_incoming_get_current(void)
137{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100138 static bool once;
139 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100140
Juan Quintelab4b076d2017-01-23 22:32:06 +0100141 if (!once) {
142 mis_current.state = MIGRATION_STATUS_NONE;
143 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100144 qemu_mutex_init(&mis_current.rp_mutex);
145 qemu_event_init(&mis_current.main_thread_load_event, false);
146 once = true;
147 }
148 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100149}
150
151void migration_incoming_state_destroy(void)
152{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100153 struct MigrationIncomingState *mis = migration_incoming_get_current();
154
Peter Xu34826552017-05-19 14:43:29 +0800155 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800156 /* Tell source that we are done */
157 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800158 qemu_fclose(mis->to_src_file);
159 mis->to_src_file = NULL;
160 }
161
Peter Xu660819b2017-05-19 14:43:30 +0800162 if (mis->from_src_file) {
163 qemu_fclose(mis->from_src_file);
164 mis->from_src_file = NULL;
165 }
166
Juan Quintelab4b076d2017-01-23 22:32:06 +0100167 qemu_event_destroy(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100168}
169
Juan Quintelab05dc722015-07-07 14:44:05 +0200170static void migrate_generate_event(int new_state)
171{
172 if (migrate_use_events()) {
173 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200174 }
175}
176
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000177/*
178 * Called on -incoming with a defer: uri.
179 * The migration can be started later after any parameters have been
180 * changed.
181 */
182static void deferred_incoming_migration(Error **errp)
183{
184 if (deferred_incoming) {
185 error_setg(errp, "Incoming migration already deferred");
186 }
187 deferred_incoming = true;
188}
189
Juan Quintelada6f1792017-04-24 17:37:14 +0200190/*
191 * Send a message on the return channel back to the source
192 * of the migration.
193 */
194static void migrate_send_rp_message(MigrationIncomingState *mis,
195 enum mig_rp_message_type message_type,
196 uint16_t len, void *data)
197{
198 trace_migrate_send_rp_message((int)message_type, len);
199 qemu_mutex_lock(&mis->rp_mutex);
200 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
201 qemu_put_be16(mis->to_src_file, len);
202 qemu_put_buffer(mis->to_src_file, data, len);
203 qemu_fflush(mis->to_src_file);
204 qemu_mutex_unlock(&mis->rp_mutex);
205}
206
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000207/* Request a range of pages from the source VM at the given
208 * start address.
209 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
210 * as the last request (a name must have been given previously)
211 * Start: Address offset within the RB
212 * Len: Length in bytes required - must be a multiple of pagesize
213 */
214void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
215 ram_addr_t start, size_t len)
216{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100217 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000218 size_t msglen = 12; /* start + len */
219
220 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
221 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
222
223 if (rbname) {
224 int rbname_len = strlen(rbname);
225 assert(rbname_len < 256);
226
227 bufc[msglen++] = rbname_len;
228 memcpy(bufc + msglen, rbname, rbname_len);
229 msglen += rbname_len;
230 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
231 } else {
232 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
233 }
234}
235
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200236void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000237{
aliguori34c9dd82008-10-13 03:14:31 +0000238 const char *p;
239
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200240 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000241 if (!strcmp(uri, "defer")) {
242 deferred_incoming_migration(errp);
243 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200244 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400245#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000246 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400247 rdma_start_incoming_migration(p, errp);
248#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000249 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200250 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000251 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200252 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000253 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200254 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000255 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100256 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200257 }
aliguori5bb79102008-10-13 03:12:02 +0000258}
259
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300260static void process_incoming_migration_bh(void *opaque)
261{
262 Error *local_err = NULL;
263 MigrationIncomingState *mis = opaque;
264
Kevin Wolface21a52017-05-04 18:52:36 +0200265 /* Make sure all file formats flush their mutable metadata.
266 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300267 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200268 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200269 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200270 local_err = NULL;
271 autostart = false;
272 }
273
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300274 /*
275 * This must happen after all error conditions are dealt with and
276 * we're sure the VM is going to be running on this host.
277 */
278 qemu_announce_self();
279
280 /* If global state section was not received or we are in running
281 state, we need to obey autostart. Any other state is set with
282 runstate_set. */
283
284 if (!global_state_received() ||
285 global_state_get_runstate() == RUN_STATE_RUNNING) {
286 if (autostart) {
287 vm_start();
288 } else {
289 runstate_set(RUN_STATE_PAUSED);
290 }
291 } else {
292 runstate_set(global_state_get_runstate());
293 }
294 migrate_decompress_threads_join();
295 /*
296 * This must happen after any state changes since as soon as an external
297 * observer sees this event they might start to prod at the VM assuming
298 * it's ready to use.
299 */
300 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
301 MIGRATION_STATUS_COMPLETED);
302 qemu_bh_delete(mis->bh);
303 migration_incoming_state_destroy();
304}
305
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200306static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200307{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200308 QEMUFile *f = opaque;
Juan Quintelab4b076d2017-01-23 22:32:06 +0100309 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000310 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200311 int ret;
312
Juan Quintelab4b076d2017-01-23 22:32:06 +0100313 mis->from_src_file = f;
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000314 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000315 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000316 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
317 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200318 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100319
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000320 ps = postcopy_state_get();
321 trace_process_incoming_migration_co_end(ret, ps);
322 if (ps != POSTCOPY_INCOMING_NONE) {
323 if (ps == POSTCOPY_INCOMING_ADVISE) {
324 /*
325 * Where a migration had postcopy enabled (and thus went to advise)
326 * but managed to complete within the precopy period, we can use
327 * the normal exit.
328 */
329 postcopy_ram_incoming_cleanup(mis);
330 } else if (ret >= 0) {
331 /*
332 * Postcopy was started, cleanup should happen at the end of the
333 * postcopy thread.
334 */
335 trace_process_incoming_migration_co_postcopy_end_main();
336 return;
337 }
338 /* Else if something went wrong then just fall out of the normal exit */
339 }
340
zhanghailiang25d0c162016-10-27 14:42:55 +0800341 /* we get COLO info, and know if we are in COLO mode */
342 if (!ret && migration_incoming_enable_colo()) {
343 mis->migration_incoming_co = qemu_coroutine_self();
344 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
345 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
346 mis->have_colo_incoming_thread = true;
347 qemu_coroutine_yield();
348
349 /* Wait checkpoint incoming thread exit before free resource */
350 qemu_thread_join(&mis->colo_incoming_thread);
351 }
352
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200353 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000354 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
355 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200356 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800357 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600358 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200359 }
Juan Quintela511c0232010-06-09 14:10:55 +0200360
Laurent Viviere8199e42017-04-12 15:53:11 +0200361 free_xbzrle_decoded_buf();
362
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300363 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
364 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200365}
366
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100367void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200368{
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200369 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200370
Liang Li3fcb38c2015-03-23 16:32:18 +0800371 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100372 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200373 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200374}
375
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000376/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000377 * Send a 'SHUT' message on the return channel with the given value
378 * to indicate that we've finished with the RP. Non-0 value indicates
379 * error.
380 */
381void migrate_send_rp_shut(MigrationIncomingState *mis,
382 uint32_t value)
383{
384 uint32_t buf;
385
386 buf = cpu_to_be32(value);
387 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
388}
389
390/*
391 * Send a 'PONG' message on the return channel with the given value
392 * (normally in response to a 'PING')
393 */
394void migrate_send_rp_pong(MigrationIncomingState *mis,
395 uint32_t value)
396{
397 uint32_t buf;
398
399 buf = cpu_to_be32(value);
400 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
401}
402
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300403MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
404{
405 MigrationCapabilityStatusList *head = NULL;
406 MigrationCapabilityStatusList *caps;
407 MigrationState *s = migrate_get_current();
408 int i;
409
Michael Tokarev387eede2013-10-05 13:18:28 +0400410 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700411 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100412#ifndef CONFIG_LIVE_BLOCK_MIGRATION
413 if (i == MIGRATION_CAPABILITY_BLOCK) {
414 continue;
415 }
416#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800417 if (i == MIGRATION_CAPABILITY_X_COLO && !colo_supported()) {
418 continue;
419 }
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300420 if (head == NULL) {
421 head = g_malloc0(sizeof(*caps));
422 caps = head;
423 } else {
424 caps->next = g_malloc0(sizeof(*caps));
425 caps = caps->next;
426 }
427 caps->value =
428 g_malloc(sizeof(*caps->value));
429 caps->value->capability = i;
430 caps->value->state = s->enabled_capabilities[i];
431 }
432
433 return head;
434}
435
Liang Li85de8322015-03-23 16:32:28 +0800436MigrationParameters *qmp_query_migrate_parameters(Error **errp)
437{
438 MigrationParameters *params;
439 MigrationState *s = migrate_get_current();
440
441 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500442 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100443 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500444 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100445 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500446 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100447 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500448 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100449 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500450 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100451 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Eric Blakede63ab62016-09-08 22:14:15 -0500452 params->has_tls_creds = !!s->parameters.tls_creds;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100453 params->tls_creds = g_strdup(s->parameters.tls_creds);
Eric Blakede63ab62016-09-08 22:14:15 -0500454 params->has_tls_hostname = !!s->parameters.tls_hostname;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100455 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530456 params->has_max_bandwidth = true;
457 params->max_bandwidth = s->parameters.max_bandwidth;
458 params->has_downtime_limit = true;
459 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800460 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800461 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200462 params->has_block_incremental = true;
463 params->block_incremental = s->parameters.block_incremental;
Liang Li85de8322015-03-23 16:32:28 +0800464
465 return params;
466}
467
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000468/*
469 * Return true if we're already in the middle of a migration
470 * (i.e. any of the active or setup states)
471 */
472static bool migration_is_setup_or_active(int state)
473{
474 switch (state) {
475 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000476 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000477 case MIGRATION_STATUS_SETUP:
478 return true;
479
480 default:
481 return false;
482
483 }
484}
485
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100486static void populate_ram_info(MigrationInfo *info, MigrationState *s)
487{
488 info->has_ram = true;
489 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200490 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100491 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200492 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100493 /* legacy value. It is not used anymore */
494 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200495 info->ram->normal = ram_counters.normal;
496 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100497 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100498 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200499 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
500 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800501 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100502
Juan Quintela114f5ae2017-05-04 10:09:21 +0200503 if (migrate_use_xbzrle()) {
504 info->has_xbzrle_cache = true;
505 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
506 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200507 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
508 info->xbzrle_cache->pages = xbzrle_counters.pages;
509 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
510 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
511 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200512 }
513
Juan Quintela338182c2017-05-03 13:16:38 +0200514 if (cpu_throttle_active()) {
515 info->has_cpu_throttle_percentage = true;
516 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
517 }
518
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100519 if (s->state != MIGRATION_STATUS_COMPLETED) {
520 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200521 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100522 }
523}
524
Juan Quintela930ac042017-05-04 10:21:46 +0200525static void populate_disk_info(MigrationInfo *info)
526{
527 if (blk_mig_active()) {
528 info->has_disk = true;
529 info->disk = g_malloc0(sizeof(*info->disk));
530 info->disk->transferred = blk_mig_bytes_transferred();
531 info->disk->remaining = blk_mig_bytes_remaining();
532 info->disk->total = blk_mig_bytes_total();
533 }
534}
535
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300536MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000537{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300538 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200539 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000540
Juan Quintela17549e82011-10-05 13:50:43 +0200541 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800542 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200543 /* no migration has happened ever */
544 break;
zhanghailiang31194732015-03-13 16:08:38 +0800545 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400546 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400547 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400548 break;
zhanghailiang31194732015-03-13 16:08:38 +0800549 case MIGRATION_STATUS_ACTIVE:
550 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000551 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200552 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000553 info->has_status = true;
554 info->has_total_time = true;
555 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
556 - s->total_time;
557 info->has_expected_downtime = true;
558 info->expected_downtime = s->expected_downtime;
559 info->has_setup_time = true;
560 info->setup_time = s->setup_time;
561
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100562 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200563 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000564 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800565 case MIGRATION_STATUS_COLO:
566 info->has_status = true;
567 /* TODO: display COLO specific information (checkpoint info etc.) */
568 break;
zhanghailiang31194732015-03-13 16:08:38 +0800569 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300570 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900571 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200572 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200573 info->has_downtime = true;
574 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400575 info->has_setup_time = true;
576 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200577
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100578 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200579 break;
zhanghailiang31194732015-03-13 16:08:38 +0800580 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300581 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100582 if (s->error) {
583 info->has_error_desc = true;
584 info->error_desc = g_strdup(error_get_pretty(s->error));
585 }
Juan Quintela17549e82011-10-05 13:50:43 +0200586 break;
zhanghailiang31194732015-03-13 16:08:38 +0800587 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300588 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200589 break;
aliguori5bb79102008-10-13 03:12:02 +0000590 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800591 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300592
593 return info;
aliguori5bb79102008-10-13 03:12:02 +0000594}
595
Orit Wasserman00458432012-08-06 21:42:48 +0300596void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
597 Error **errp)
598{
599 MigrationState *s = migrate_get_current();
600 MigrationCapabilityStatusList *cap;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100601 bool old_postcopy_cap = migrate_postcopy_ram();
Orit Wasserman00458432012-08-06 21:42:48 +0300602
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000603 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100604 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300605 return;
606 }
607
608 for (cap = params; cap; cap = cap->next) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100609#ifndef CONFIG_LIVE_BLOCK_MIGRATION
610 if (cap->value->capability == MIGRATION_CAPABILITY_BLOCK
611 && cap->value->state) {
612 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
613 "block migration");
614 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
615 continue;
616 }
617#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800618 if (cap->value->capability == MIGRATION_CAPABILITY_X_COLO) {
619 if (!colo_supported()) {
620 error_setg(errp, "COLO is not currently supported, please"
621 " configure with --enable-colo option in order to"
622 " support COLO feature");
623 continue;
624 }
625 }
Orit Wasserman00458432012-08-06 21:42:48 +0300626 s->enabled_capabilities[cap->value->capability] = cap->value->state;
627 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000628
629 if (migrate_postcopy_ram()) {
630 if (migrate_use_compression()) {
631 /* The decompression threads asynchronously write into RAM
632 * rather than use the atomic copies needed to avoid
633 * userfaulting. It should be possible to fix the decompression
634 * threads for compatibility in future.
635 */
636 error_report("Postcopy is not currently compatible with "
637 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000638 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000639 false;
640 }
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100641 /* This check is reasonably expensive, so only when it's being
642 * set the first time, also it's only the destination that needs
643 * special support.
644 */
645 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
646 !postcopy_ram_supported_by_host()) {
647 /* postcopy_ram_supported_by_host will have emitted a more
648 * detailed message
649 */
650 error_report("Postcopy is not supported");
651 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
652 false;
653 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000654 }
Orit Wasserman00458432012-08-06 21:42:48 +0300655}
656
Eric Blake7f375e02016-09-08 22:14:16 -0500657void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800658{
659 MigrationState *s = migrate_get_current();
660
Eric Blake7f375e02016-09-08 22:14:16 -0500661 if (params->has_compress_level &&
662 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100663 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
664 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800665 return;
666 }
Eric Blake7f375e02016-09-08 22:14:16 -0500667 if (params->has_compress_threads &&
668 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100669 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
670 "compress_threads",
671 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800672 return;
673 }
Eric Blake7f375e02016-09-08 22:14:16 -0500674 if (params->has_decompress_threads &&
675 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100676 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
677 "decompress_threads",
678 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800679 return;
680 }
Eric Blake7f375e02016-09-08 22:14:16 -0500681 if (params->has_cpu_throttle_initial &&
682 (params->cpu_throttle_initial < 1 ||
683 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400684 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400685 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400686 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530687 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400688 }
Eric Blake7f375e02016-09-08 22:14:16 -0500689 if (params->has_cpu_throttle_increment &&
690 (params->cpu_throttle_increment < 1 ||
691 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400692 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400693 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400694 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530695 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400696 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530697 if (params->has_max_bandwidth &&
698 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
699 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
700 " range of 0 to %zu bytes/second", SIZE_MAX);
701 return;
702 }
703 if (params->has_downtime_limit &&
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300704 (params->downtime_limit < 0 ||
705 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
706 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
707 "the range of 0 to %d milliseconds",
708 MAX_MIGRATE_DOWNTIME);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530709 return;
710 }
zhanghailiang68b53592016-10-27 14:43:01 +0800711 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
712 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
713 "x_checkpoint_delay",
714 "is invalid, it should be positive");
715 }
Liang Li85de8322015-03-23 16:32:28 +0800716
Eric Blake7f375e02016-09-08 22:14:16 -0500717 if (params->has_compress_level) {
718 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800719 }
Eric Blake7f375e02016-09-08 22:14:16 -0500720 if (params->has_compress_threads) {
721 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800722 }
Eric Blake7f375e02016-09-08 22:14:16 -0500723 if (params->has_decompress_threads) {
724 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800725 }
Eric Blake7f375e02016-09-08 22:14:16 -0500726 if (params->has_cpu_throttle_initial) {
727 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400728 }
Eric Blake7f375e02016-09-08 22:14:16 -0500729 if (params->has_cpu_throttle_increment) {
730 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400731 }
Eric Blake7f375e02016-09-08 22:14:16 -0500732 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100733 g_free(s->parameters.tls_creds);
Eric Blake7f375e02016-09-08 22:14:16 -0500734 s->parameters.tls_creds = g_strdup(params->tls_creds);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100735 }
Eric Blake7f375e02016-09-08 22:14:16 -0500736 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100737 g_free(s->parameters.tls_hostname);
Eric Blake7f375e02016-09-08 22:14:16 -0500738 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100739 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530740 if (params->has_max_bandwidth) {
741 s->parameters.max_bandwidth = params->max_bandwidth;
742 if (s->to_dst_file) {
743 qemu_file_set_rate_limit(s->to_dst_file,
744 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
745 }
746 }
747 if (params->has_downtime_limit) {
748 s->parameters.downtime_limit = params->downtime_limit;
749 }
zhanghailiang68b53592016-10-27 14:43:01 +0800750
751 if (params->has_x_checkpoint_delay) {
752 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800753 if (migration_in_colo_state()) {
754 colo_checkpoint_notify(s);
755 }
zhanghailiang68b53592016-10-27 14:43:01 +0800756 }
Juan Quintela2833c592017-04-05 18:32:37 +0200757 if (params->has_block_incremental) {
758 s->parameters.block_incremental = params->block_incremental;
759 }
Liang Li85de8322015-03-23 16:32:28 +0800760}
761
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100762
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000763void qmp_migrate_start_postcopy(Error **errp)
764{
765 MigrationState *s = migrate_get_current();
766
767 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000768 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000769 " the start of migration");
770 return;
771 }
772
773 if (s->state == MIGRATION_STATUS_NONE) {
774 error_setg(errp, "Postcopy must be started after migration has been"
775 " started");
776 return;
777 }
778 /*
779 * we don't error if migration has finished since that would be racy
780 * with issuing this command.
781 */
782 atomic_set(&s->start_postcopy, true);
783}
784
aliguori065e2812008-11-11 16:46:33 +0000785/* shared migration helpers */
786
zhanghailiang48781e52015-12-16 11:47:33 +0000787void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000788{
zhanghailiang48781e52015-12-16 11:47:33 +0000789 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200790 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200791 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000792 }
793}
794
Juan Quintela2833c592017-04-05 18:32:37 +0200795void migrate_set_block_enabled(bool value, Error **errp)
796{
797 MigrationCapabilityStatusList *cap;
798
799 cap = g_new0(MigrationCapabilityStatusList, 1);
800 cap->value = g_new0(MigrationCapabilityStatus, 1);
801 cap->value->capability = MIGRATION_CAPABILITY_BLOCK;
802 cap->value->state = value;
803 qmp_migrate_set_capabilities(cap, errp);
804 qapi_free_MigrationCapabilityStatusList(cap);
805}
806
807static void migrate_set_block_incremental(MigrationState *s, bool value)
808{
809 s->parameters.block_incremental = value;
810}
811
812static void block_cleanup_parameters(MigrationState *s)
813{
814 if (s->must_remove_block_options) {
815 /* setting to false can never fail */
816 migrate_set_block_enabled(false, &error_abort);
817 migrate_set_block_incremental(s, false);
818 s->must_remove_block_options = false;
819 }
820}
821
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100822static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000823{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100824 MigrationState *s = opaque;
825
826 qemu_bh_delete(s->cleanup_bh);
827 s->cleanup_bh = NULL;
828
zhanghailiang89a02a92016-01-15 11:37:42 +0800829 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100830 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100831 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000832 if (s->migration_thread_running) {
833 qemu_thread_join(&s->thread);
834 s->migration_thread_running = false;
835 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100836 qemu_mutex_lock_iothread();
837
Liang Li8706d2d2015-03-23 16:32:17 +0800838 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800839 qemu_fclose(s->to_dst_file);
840 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000841 }
842
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000843 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
844 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100845
Liang Li94f5a432015-11-02 15:37:00 +0800846 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000847 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800848 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100849 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100850
851 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200852 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000853}
854
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100855void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000856{
Peter Maydell25174052016-10-21 18:41:45 +0100857 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +0800858 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000859 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
860 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100861 if (!s->error) {
862 s->error = error_copy(error);
863 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100864 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200865 block_cleanup_parameters(s);
Juan Quintela458cf282011-02-22 23:32:54 +0100866}
867
Juan Quintela0edda1c2010-05-11 16:28:39 +0200868static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000869{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000870 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800871 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100872 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000873
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000874 if (s->rp_state.from_dst_file) {
875 /* shutdown the rp socket, so causing the rp thread to shutdown */
876 qemu_file_shutdown(s->rp_state.from_dst_file);
877 }
878
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000879 do {
880 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000881 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000882 break;
883 }
zhanghailiang48781e52015-12-16 11:47:33 +0000884 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +0800885 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000886
887 /*
888 * If we're unlucky the migration code might be stuck somewhere in a
889 * send/write while the network has failed and is waiting to timeout;
890 * if we've got shutdown(2) available then we can force it to quit.
891 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
892 * called in a bh, so there is no race against this cancel.
893 */
zhanghailiang31194732015-03-13 16:08:38 +0800894 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000895 qemu_file_shutdown(f);
896 }
zhanghailiang1d2acc32017-01-24 15:59:52 +0800897 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
898 Error *local_err = NULL;
899
900 bdrv_invalidate_cache_all(&local_err);
901 if (local_err) {
902 error_report_err(local_err);
903 } else {
904 s->block_inactive = false;
905 }
906 }
Juan Quintela2833c592017-04-05 18:32:37 +0200907 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000908}
909
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100910void add_migration_state_change_notifier(Notifier *notify)
911{
912 notifier_list_add(&migration_state_notifiers, notify);
913}
914
915void remove_migration_state_change_notifier(Notifier *notify)
916{
Paolo Bonzini31552522012-01-13 17:34:01 +0100917 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100918}
919
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200920bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200921{
zhanghailiang31194732015-03-13 16:08:38 +0800922 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200923}
924
Juan Quintela70736932011-02-23 00:43:59 +0100925bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100926{
zhanghailiang31194732015-03-13 16:08:38 +0800927 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100928}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200929
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200930bool migration_has_failed(MigrationState *s)
931{
zhanghailiang31194732015-03-13 16:08:38 +0800932 return (s->state == MIGRATION_STATUS_CANCELLED ||
933 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200934}
935
Juan Quintela57273092017-03-20 22:25:28 +0100936bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000937{
Juan Quintela57273092017-03-20 22:25:28 +0100938 MigrationState *s = migrate_get_current();
939
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000940 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
941}
942
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000943bool migration_in_postcopy_after_devices(MigrationState *s)
944{
Juan Quintela57273092017-03-20 22:25:28 +0100945 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000946}
947
Juan Quintelafab35002017-03-22 17:36:57 +0100948bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530949{
Juan Quintelafab35002017-03-22 17:36:57 +0100950 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530951
952 switch (s->state) {
953 case MIGRATION_STATUS_NONE:
954 case MIGRATION_STATUS_CANCELLED:
955 case MIGRATION_STATUS_COMPLETED:
956 case MIGRATION_STATUS_FAILED:
957 return true;
958 case MIGRATION_STATUS_SETUP:
959 case MIGRATION_STATUS_CANCELLING:
960 case MIGRATION_STATUS_ACTIVE:
961 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
962 case MIGRATION_STATUS_COLO:
963 return false;
964 case MIGRATION_STATUS__MAX:
965 g_assert_not_reached();
966 }
967
968 return false;
969}
970
Juan Quintelaa0762d92017-04-05 21:00:09 +0200971MigrationState *migrate_init(void)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200972{
Juan Quintela17549e82011-10-05 13:50:43 +0200973 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300974
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000975 /*
976 * Reinitialise all migration state, except
977 * parameters/capabilities that the user set, and
978 * locks.
979 */
980 s->bytes_xfer = 0;
981 s->xfer_limit = 0;
982 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +0800983 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000984 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000985 s->rp_state.from_dst_file = NULL;
986 s->rp_state.error = false;
987 s->mbps = 0.0;
988 s->downtime = 0;
989 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000990 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000991 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000992 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000993 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100994 error_free(s->error);
995 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +0100996
zhanghailiang48781e52015-12-16 11:47:33 +0000997 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200998
Alex Blighbc72ad62013-08-21 16:03:08 +0100999 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001000 return s;
1001}
Juan Quintelacab30142011-02-22 23:54:21 +01001002
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001003static GSList *migration_blockers;
1004
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301005int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001006{
Peter Xu3df663e2017-06-27 12:10:15 +08001007 if (migrate_get_current()->only_migratable) {
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301008 error_propagate(errp, error_copy(reason));
1009 error_prepend(errp, "disallowing migration blocker "
1010 "(--only_migratable) for: ");
1011 return -EACCES;
1012 }
1013
Juan Quintelafab35002017-03-22 17:36:57 +01001014 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301015 migration_blockers = g_slist_prepend(migration_blockers, reason);
1016 return 0;
1017 }
1018
1019 error_propagate(errp, error_copy(reason));
1020 error_prepend(errp, "disallowing migration blocker (migration in "
1021 "progress) for: ");
1022 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001023}
1024
1025void migrate_del_blocker(Error *reason)
1026{
1027 migration_blockers = g_slist_remove(migration_blockers, reason);
1028}
1029
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001030void qmp_migrate_incoming(const char *uri, Error **errp)
1031{
1032 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001033 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001034
1035 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001036 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001037 return;
1038 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001039 if (!once) {
1040 error_setg(errp, "The incoming migration has already been started");
1041 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001042
1043 qemu_start_incoming_migration(uri, &local_err);
1044
1045 if (local_err) {
1046 error_propagate(errp, local_err);
1047 return;
1048 }
1049
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001050 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001051}
1052
Greg Kurz24f39022016-05-04 21:44:19 +02001053bool migration_is_blocked(Error **errp)
1054{
1055 if (qemu_savevm_state_blocked(errp)) {
1056 return true;
1057 }
1058
1059 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001060 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001061 return true;
1062 }
1063
1064 return false;
1065}
1066
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001067void qmp_migrate(const char *uri, bool has_blk, bool blk,
1068 bool has_inc, bool inc, bool has_detach, bool detach,
1069 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001070{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001071 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001072 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001073 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001074
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001075 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001076 s->state == MIGRATION_STATUS_CANCELLING ||
1077 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001078 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001079 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001080 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001081 if (runstate_check(RUN_STATE_INMIGRATE)) {
1082 error_setg(errp, "Guest is waiting for an incoming migration");
1083 return;
1084 }
1085
Greg Kurz24f39022016-05-04 21:44:19 +02001086 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001087 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001088 }
1089
Juan Quintela2833c592017-04-05 18:32:37 +02001090 if ((has_blk && blk) || (has_inc && inc)) {
1091 if (migrate_use_block() || migrate_use_block_incremental()) {
1092 error_setg(errp, "Command options are incompatible with "
1093 "current migration capabilities");
1094 return;
1095 }
1096 migrate_set_block_enabled(true, &local_err);
1097 if (local_err) {
1098 error_propagate(errp, local_err);
1099 return;
1100 }
1101 s->must_remove_block_options = true;
1102 }
1103
1104 if (has_inc && inc) {
1105 migrate_set_block_incremental(s, true);
1106 }
1107
Juan Quintelaa0762d92017-04-05 21:00:09 +02001108 s = migrate_init();
Juan Quintelacab30142011-02-22 23:54:21 +01001109
1110 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001111 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001112#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001113 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001114 rdma_start_outgoing_migration(s, p, &local_err);
1115#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001116 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001117 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001118 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001119 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001120 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001121 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001122 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001123 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1124 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001125 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1126 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001127 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001128 }
1129
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001130 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001131 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001132 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001133 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001134 }
Juan Quintelacab30142011-02-22 23:54:21 +01001135}
1136
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001137void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001138{
Juan Quintela17549e82011-10-05 13:50:43 +02001139 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001140}
1141
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001142void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1143{
1144 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001145 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001146
1147 /* Check for truncation */
1148 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001149 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1150 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001151 return;
1152 }
1153
Orit Wassermana5615b12014-01-30 20:08:36 +02001154 /* Cache should not be larger than guest ram size */
1155 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001156 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1157 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001158 return;
1159 }
1160
Orit Wassermanc91e6812014-01-30 20:08:34 +02001161 new_size = xbzrle_cache_resize(value);
1162 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001163 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1164 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001165 return;
1166 }
1167
1168 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001169}
1170
1171int64_t qmp_query_migrate_cache_size(Error **errp)
1172{
1173 return migrate_xbzrle_cache_size();
1174}
1175
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001176void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001177{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301178 MigrationParameters p = {
1179 .has_max_bandwidth = true,
1180 .max_bandwidth = value,
1181 };
Juan Quintelacab30142011-02-22 23:54:21 +01001182
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301183 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001184}
1185
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001186void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001187{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001188 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1189 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1190 "the range of 0 to %d seconds",
1191 MAX_MIGRATE_DOWNTIME_SECONDS);
1192 return;
1193 }
1194
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301195 value *= 1000; /* Convert to milliseconds */
1196 value = MAX(0, MIN(INT64_MAX, value));
1197
1198 MigrationParameters p = {
1199 .has_downtime_limit = true,
1200 .downtime_limit = value,
1201 };
1202
1203 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001204}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001205
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001206bool migrate_release_ram(void)
1207{
1208 MigrationState *s;
1209
1210 s = migrate_get_current();
1211
1212 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1213}
1214
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001215bool migrate_postcopy_ram(void)
1216{
1217 MigrationState *s;
1218
1219 s = migrate_get_current();
1220
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001221 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001222}
1223
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001224bool migrate_auto_converge(void)
1225{
1226 MigrationState *s;
1227
1228 s = migrate_get_current();
1229
1230 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1231}
1232
Peter Lieven323004a2013-07-18 09:48:50 +02001233bool migrate_zero_blocks(void)
1234{
1235 MigrationState *s;
1236
1237 s = migrate_get_current();
1238
1239 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1240}
1241
Liang Li8706d2d2015-03-23 16:32:17 +08001242bool migrate_use_compression(void)
1243{
Liang Lidde4e692015-03-23 16:32:26 +08001244 MigrationState *s;
1245
1246 s = migrate_get_current();
1247
1248 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001249}
1250
1251int migrate_compress_level(void)
1252{
1253 MigrationState *s;
1254
1255 s = migrate_get_current();
1256
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001257 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001258}
1259
1260int migrate_compress_threads(void)
1261{
1262 MigrationState *s;
1263
1264 s = migrate_get_current();
1265
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001266 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001267}
1268
Liang Li3fcb38c2015-03-23 16:32:18 +08001269int migrate_decompress_threads(void)
1270{
1271 MigrationState *s;
1272
1273 s = migrate_get_current();
1274
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001275 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001276}
1277
Juan Quintelab05dc722015-07-07 14:44:05 +02001278bool migrate_use_events(void)
1279{
1280 MigrationState *s;
1281
1282 s = migrate_get_current();
1283
1284 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1285}
1286
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001287int migrate_use_xbzrle(void)
1288{
1289 MigrationState *s;
1290
1291 s = migrate_get_current();
1292
1293 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1294}
1295
1296int64_t migrate_xbzrle_cache_size(void)
1297{
1298 MigrationState *s;
1299
1300 s = migrate_get_current();
1301
1302 return s->xbzrle_cache_size;
1303}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001304
Juan Quintela2833c592017-04-05 18:32:37 +02001305bool migrate_use_block(void)
1306{
1307 MigrationState *s;
1308
1309 s = migrate_get_current();
1310
1311 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1312}
1313
1314bool migrate_use_block_incremental(void)
1315{
1316 MigrationState *s;
1317
1318 s = migrate_get_current();
1319
1320 return s->parameters.block_incremental;
1321}
1322
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001323/* migration thread support */
1324/*
1325 * Something bad happened to the RP stream, mark an error
1326 * The caller shall print or trace something to indicate why
1327 */
1328static void mark_source_rp_bad(MigrationState *s)
1329{
1330 s->rp_state.error = true;
1331}
1332
1333static struct rp_cmd_args {
1334 ssize_t len; /* -1 = variable */
1335 const char *name;
1336} rp_cmd_args[] = {
1337 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1338 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1339 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001340 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1341 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001342 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1343};
1344
1345/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001346 * Process a request for pages received on the return path,
1347 * We're allowed to send more than requested (e.g. to round to our page size)
1348 * and we don't need to send pages that have already been sent.
1349 */
1350static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1351 ram_addr_t start, size_t len)
1352{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001353 long our_host_ps = getpagesize();
1354
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001355 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001356
1357 /*
1358 * Since we currently insist on matching page sizes, just sanity check
1359 * we're being asked for whole host pages.
1360 */
1361 if (start & (our_host_ps-1) ||
1362 (len & (our_host_ps-1))) {
1363 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1364 " len: %zd", __func__, start, len);
1365 mark_source_rp_bad(ms);
1366 return;
1367 }
1368
Juan Quintela96506892017-03-14 18:41:03 +01001369 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001370 mark_source_rp_bad(ms);
1371 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001372}
1373
1374/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001375 * Handles messages sent on the return path towards the source VM
1376 *
1377 */
1378static void *source_return_path_thread(void *opaque)
1379{
1380 MigrationState *ms = opaque;
1381 QEMUFile *rp = ms->rp_state.from_dst_file;
1382 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001383 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001384 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001385 ram_addr_t start = 0; /* =0 to silence warning */
1386 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001387 int res;
1388
1389 trace_source_return_path_thread_entry();
1390 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1391 migration_is_setup_or_active(ms->state)) {
1392 trace_source_return_path_thread_loop_top();
1393 header_type = qemu_get_be16(rp);
1394 header_len = qemu_get_be16(rp);
1395
1396 if (header_type >= MIG_RP_MSG_MAX ||
1397 header_type == MIG_RP_MSG_INVALID) {
1398 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1399 header_type, header_len);
1400 mark_source_rp_bad(ms);
1401 goto out;
1402 }
1403
1404 if ((rp_cmd_args[header_type].len != -1 &&
1405 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001406 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001407 error_report("RP: Received '%s' message (0x%04x) with"
1408 "incorrect length %d expecting %zu",
1409 rp_cmd_args[header_type].name, header_type, header_len,
1410 (size_t)rp_cmd_args[header_type].len);
1411 mark_source_rp_bad(ms);
1412 goto out;
1413 }
1414
1415 /* We know we've got a valid header by this point */
1416 res = qemu_get_buffer(rp, buf, header_len);
1417 if (res != header_len) {
1418 error_report("RP: Failed reading data for message 0x%04x"
1419 " read %d expected %d",
1420 header_type, res, header_len);
1421 mark_source_rp_bad(ms);
1422 goto out;
1423 }
1424
1425 /* OK, we have the message and the data */
1426 switch (header_type) {
1427 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001428 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001429 trace_source_return_path_thread_shut(sibling_error);
1430 if (sibling_error) {
1431 error_report("RP: Sibling indicated error %d", sibling_error);
1432 mark_source_rp_bad(ms);
1433 }
1434 /*
1435 * We'll let the main thread deal with closing the RP
1436 * we could do a shutdown(2) on it, but we're the only user
1437 * anyway, so there's nothing gained.
1438 */
1439 goto out;
1440
1441 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001442 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001443 trace_source_return_path_thread_pong(tmp32);
1444 break;
1445
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001446 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001447 start = ldq_be_p(buf);
1448 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001449 migrate_handle_rp_req_pages(ms, NULL, start, len);
1450 break;
1451
1452 case MIG_RP_MSG_REQ_PAGES_ID:
1453 expected_len = 12 + 1; /* header + termination */
1454
1455 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001456 start = ldq_be_p(buf);
1457 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001458 /* Now we expect an idstr */
1459 tmp32 = buf[12]; /* Length of the following idstr */
1460 buf[13 + tmp32] = '\0';
1461 expected_len += tmp32;
1462 }
1463 if (header_len != expected_len) {
1464 error_report("RP: Req_Page_id with length %d expecting %zd",
1465 header_len, expected_len);
1466 mark_source_rp_bad(ms);
1467 goto out;
1468 }
1469 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1470 break;
1471
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001472 default:
1473 break;
1474 }
1475 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001476 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001477 trace_source_return_path_thread_bad_end();
1478 mark_source_rp_bad(ms);
1479 }
1480
1481 trace_source_return_path_thread_end();
1482out:
1483 ms->rp_state.from_dst_file = NULL;
1484 qemu_fclose(rp);
1485 return NULL;
1486}
1487
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001488static int open_return_path_on_source(MigrationState *ms)
1489{
1490
zhanghailiang89a02a92016-01-15 11:37:42 +08001491 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001492 if (!ms->rp_state.from_dst_file) {
1493 return -1;
1494 }
1495
1496 trace_open_return_path_on_source();
1497 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1498 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1499
1500 trace_open_return_path_on_source_continue();
1501
1502 return 0;
1503}
1504
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001505/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1506static int await_return_path_close_on_source(MigrationState *ms)
1507{
1508 /*
1509 * If this is a normal exit then the destination will send a SHUT and the
1510 * rp_thread will exit, however if there's an error we need to cause
1511 * it to exit.
1512 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001513 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001514 /*
1515 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1516 * waiting for the destination.
1517 */
1518 qemu_file_shutdown(ms->rp_state.from_dst_file);
1519 mark_source_rp_bad(ms);
1520 }
1521 trace_await_return_path_close_on_source_joining();
1522 qemu_thread_join(&ms->rp_state.rp_thread);
1523 trace_await_return_path_close_on_source_close();
1524 return ms->rp_state.error;
1525}
1526
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001527/*
1528 * Switch from normal iteration to postcopy
1529 * Returns non-0 on error
1530 */
1531static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1532{
1533 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001534 QIOChannelBuffer *bioc;
1535 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001536 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001537 bool restart_block = false;
zhanghailiang48781e52015-12-16 11:47:33 +00001538 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001539 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1540
1541 trace_postcopy_start();
1542 qemu_mutex_lock_iothread();
1543 trace_postcopy_start_set_run();
1544
1545 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1546 *old_vm_running = runstate_is_running();
1547 global_state_store();
1548 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001549 if (ret < 0) {
1550 goto fail;
1551 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001552
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001553 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001554 if (ret < 0) {
1555 goto fail;
1556 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001557 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001558
1559 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001560 * Cause any non-postcopiable, but iterative devices to
1561 * send out their final data.
1562 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08001563 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001564
1565 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001566 * in Finish migrate and with the io-lock held everything should
1567 * be quiet, but we've potentially still got dirty pages and we
1568 * need to tell the destination to throw any pages it's already received
1569 * that are dirty
1570 */
1571 if (ram_postcopy_send_discard_bitmap(ms)) {
1572 error_report("postcopy send discard bitmap failed");
1573 goto fail;
1574 }
1575
1576 /*
1577 * send rest of state - note things that are doing postcopy
1578 * will notice we're in POSTCOPY_ACTIVE and not actually
1579 * wrap their state up here
1580 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001581 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001582 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001583 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001584
1585 /*
1586 * While loading the device state we may trigger page transfer
1587 * requests and the fd must be free to process those, and thus
1588 * the destination must read the whole device state off the fd before
1589 * it starts processing it. Unfortunately the ad-hoc migration format
1590 * doesn't allow the destination to know the size to read without fully
1591 * parsing it through each devices load-state code (especially the open
1592 * coded devices that use get/put).
1593 * So we wrap the device state up in a package with a length at the start;
1594 * to do this we use a qemu_buf to hold the whole of the device state.
1595 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001596 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001597 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001598 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1599 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001600
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001601 /*
1602 * Make sure the receiver can get incoming pages before we send the rest
1603 * of the state
1604 */
1605 qemu_savevm_send_postcopy_listen(fb);
1606
Fam Zhenga1fbe752017-06-17 00:06:58 +08001607 qemu_savevm_state_complete_precopy(fb, false, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001608 qemu_savevm_send_ping(fb, 3);
1609
1610 qemu_savevm_send_postcopy_run(fb);
1611
1612 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001613
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001614 /* Last point of recovery; as soon as we send the package the destination
1615 * can open devices and potentially start running.
1616 * Lets just check again we've not got any errors.
1617 */
1618 ret = qemu_file_get_error(ms->to_dst_file);
1619 if (ret) {
1620 error_report("postcopy_start: Migration stream errored (pre package)");
1621 goto fail_closefb;
1622 }
1623
1624 restart_block = false;
1625
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001626 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001627 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001628 goto fail_closefb;
1629 }
1630 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001631
1632 /* Send a notify to give a chance for anything that needs to happen
1633 * at the transition to postcopy and after the device state; in particular
1634 * spice needs to trigger a transition now
1635 */
1636 ms->postcopy_after_devices = true;
1637 notifier_list_notify(&migration_state_notifiers, ms);
1638
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001639 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1640
1641 qemu_mutex_unlock_iothread();
1642
1643 /*
1644 * Although this ping is just for debug, it could potentially be
1645 * used for getting a better measurement of downtime at the source.
1646 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001647 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001648
Pavel Butsykinced1c612017-02-03 18:23:21 +03001649 if (migrate_release_ram()) {
1650 ram_postcopy_migrated_memory_release(ms);
1651 }
1652
zhanghailiang89a02a92016-01-15 11:37:42 +08001653 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001654 if (ret) {
1655 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001656 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001657 MIGRATION_STATUS_FAILED);
1658 }
1659
1660 return ret;
1661
1662fail_closefb:
1663 qemu_fclose(fb);
1664fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001665 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001666 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001667 if (restart_block) {
1668 /* A failure happened early enough that we know the destination hasn't
1669 * accessed block devices, so we're safe to recover.
1670 */
1671 Error *local_err = NULL;
1672
1673 bdrv_invalidate_cache_all(&local_err);
1674 if (local_err) {
1675 error_report_err(local_err);
1676 }
1677 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001678 qemu_mutex_unlock_iothread();
1679 return -1;
1680}
1681
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001682/**
1683 * migration_completion: Used by migration_thread when there's not much left.
1684 * The caller 'breaks' the loop when this returns.
1685 *
1686 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001687 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001688 * @*old_vm_running: Pointer to old_vm_running flag
1689 * @*start_time: Pointer to time to update
1690 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001691static void migration_completion(MigrationState *s, int current_active_state,
1692 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001693 int64_t *start_time)
1694{
1695 int ret;
1696
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001697 if (s->state == MIGRATION_STATUS_ACTIVE) {
1698 qemu_mutex_lock_iothread();
1699 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1700 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1701 *old_vm_running = runstate_is_running();
1702 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001703
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001704 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08001705 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001706 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001707 if (ret >= 0) {
1708 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08001709 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
1710 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001711 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08001712 if (inactivate && ret >= 0) {
1713 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001714 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001715 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001716 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001717
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001718 if (ret < 0) {
1719 goto fail;
1720 }
1721 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1722 trace_migration_completion_postcopy_end();
1723
zhanghailiang89a02a92016-01-15 11:37:42 +08001724 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001725 trace_migration_completion_postcopy_end_after_complete();
1726 }
1727
1728 /*
1729 * If rp was opened we must clean up the thread before
1730 * cleaning everything else up (since if there are no failures
1731 * it will wait for the destination to send it's status in
1732 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001733 */
Peter Xu0425dc92017-05-31 18:35:34 +08001734 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001735 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08001736 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001737 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08001738 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001739 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001740 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001741 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001742 }
1743
zhanghailiang89a02a92016-01-15 11:37:42 +08001744 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001745 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001746 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001747 }
1748
zhanghailiang0b827d52016-10-27 14:42:54 +08001749 if (!migrate_colo_enabled()) {
1750 migrate_set_state(&s->state, current_active_state,
1751 MIGRATION_STATUS_COMPLETED);
1752 }
1753
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001754 return;
1755
Greg Kurzfe904ea2016-05-18 15:44:36 +02001756fail_invalidate:
1757 /* If not doing postcopy, vm_start() will be called: let's regain
1758 * control on images.
1759 */
1760 if (s->state == MIGRATION_STATUS_ACTIVE) {
1761 Error *local_err = NULL;
1762
zhanghailiang1d2acc32017-01-24 15:59:52 +08001763 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001764 bdrv_invalidate_cache_all(&local_err);
1765 if (local_err) {
1766 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001767 } else {
1768 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02001769 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001770 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001771 }
1772
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001773fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001774 migrate_set_state(&s->state, current_active_state,
1775 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001776}
1777
zhanghailiang35a6ed42016-10-27 14:42:52 +08001778bool migrate_colo_enabled(void)
1779{
1780 MigrationState *s = migrate_get_current();
1781 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
1782}
1783
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001784/*
1785 * Master migration thread on the source VM.
1786 * It drives the migration and pumps the data down the outgoing channel.
1787 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001788static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001789{
Juan Quintela9848a402012-12-19 09:55:50 +01001790 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001791 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001792 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1793 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001794 int64_t initial_bytes = 0;
Peter Xufaec0662017-04-01 16:18:43 +08001795 /*
1796 * The final stage happens when the remaining data is smaller than
1797 * this threshold; it's calculated from the requested downtime and
1798 * measured bandwidth
1799 */
1800 int64_t threshold_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001801 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001802 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001803 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001804 bool entered_postcopy = false;
1805 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1806 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang0b827d52016-10-27 14:42:54 +08001807 bool enable_colo = migrate_colo_enabled();
Juan Quintela76f59332012-10-03 20:16:24 +02001808
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001809 rcu_register_thread();
1810
zhanghailiang89a02a92016-01-15 11:37:42 +08001811 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001812
Peter Xu62a02652017-06-14 15:55:58 +08001813 /*
1814 * If we opened the return path, we need to make sure dst has it
1815 * opened as well.
1816 */
1817 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001818 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001819 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001820
1821 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001822 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08001823 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001824
Peter Xu0425dc92017-05-31 18:35:34 +08001825 if (migrate_postcopy_ram()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001826 /*
1827 * Tell the destination that we *might* want to do postcopy later;
1828 * if the other end can't do postcopy it should fail now, nice and
1829 * early.
1830 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001831 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001832 }
1833
Juan Quintelaa0762d92017-04-05 21:00:09 +02001834 qemu_savevm_state_begin(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001835
Alex Blighbc72ad62013-08-21 16:03:08 +01001836 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00001837 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1838 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001839
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001840 trace_migration_thread_setup_complete();
1841
1842 while (s->state == MIGRATION_STATUS_ACTIVE ||
1843 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001844 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001845 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001846
zhanghailiang89a02a92016-01-15 11:37:42 +08001847 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001848 uint64_t pend_post, pend_nonpost;
1849
Peter Xufaec0662017-04-01 16:18:43 +08001850 qemu_savevm_state_pending(s->to_dst_file, threshold_size,
1851 &pend_nonpost, &pend_post);
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001852 pending_size = pend_nonpost + pend_post;
Peter Xufaec0662017-04-01 16:18:43 +08001853 trace_migrate_pending(pending_size, threshold_size,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001854 pend_post, pend_nonpost);
Peter Xufaec0662017-04-01 16:18:43 +08001855 if (pending_size && pending_size >= threshold_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001856 /* Still a significant amount to transfer */
1857
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001858 if (migrate_postcopy_ram() &&
1859 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
Peter Xufaec0662017-04-01 16:18:43 +08001860 pend_nonpost <= threshold_size &&
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001861 atomic_read(&s->start_postcopy)) {
1862
1863 if (!postcopy_start(s, &old_vm_running)) {
1864 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1865 entered_postcopy = true;
1866 }
1867
1868 continue;
1869 }
1870 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001871 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001872 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001873 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001874 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001875 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001876 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001877 }
1878 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001879
zhanghailiang89a02a92016-01-15 11:37:42 +08001880 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001881 migrate_set_state(&s->state, current_active_state,
1882 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001883 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001884 break;
1885 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001886 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001887 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001888 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1889 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001890 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001891 double bandwidth = (double)transferred_bytes / time_spent;
Peter Xufaec0662017-04-01 16:18:43 +08001892 threshold_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001893
Wei Yang5b648de2016-01-24 14:09:57 +00001894 s->mbps = (((double) transferred_bytes * 8.0) /
1895 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04001896
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001897 trace_migrate_transferred(transferred_bytes, time_spent,
Peter Xufaec0662017-04-01 16:18:43 +08001898 bandwidth, threshold_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001899 /* if we haven't sent anything, we don't want to recalculate
1900 10000 is a small enough number for our purposes */
Juan Quintela93604472017-06-06 19:49:03 +02001901 if (ram_counters.dirty_pages_rate && transferred_bytes > 10000) {
1902 s->expected_downtime = ram_counters.dirty_pages_rate *
Juan Quintela20afaed2017-03-21 09:09:14 +01001903 qemu_target_page_size() / bandwidth;
Juan Quintela90f8ae72013-02-01 13:22:37 +01001904 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001905
zhanghailiang89a02a92016-01-15 11:37:42 +08001906 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001907 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08001908 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001909 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001910 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001911 /* usleep expects microseconds */
1912 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1913 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001914 }
1915
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001916 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001917 /* If we enabled cpu throttling for auto-converge, turn it off. */
1918 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001919 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001920
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001921 qemu_mutex_lock_iothread();
zhanghailiang0b827d52016-10-27 14:42:54 +08001922 /*
1923 * The resource has been allocated by migration will be reused in COLO
1924 * process, so don't release them.
1925 */
1926 if (!enable_colo) {
1927 qemu_savevm_state_cleanup();
1928 }
zhanghailiang31194732015-03-13 16:08:38 +08001929 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001930 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001931 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001932 if (!entered_postcopy) {
1933 s->downtime = end_time - start_time;
1934 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001935 if (s->total_time) {
1936 s->mbps = (((double) transferred_bytes * 8.0) /
1937 ((double) s->total_time)) / 1000;
1938 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001939 runstate_set(RUN_STATE_POSTMIGRATE);
1940 } else {
zhanghailiang0b827d52016-10-27 14:42:54 +08001941 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
1942 migrate_start_colo_process(s);
1943 qemu_savevm_state_cleanup();
1944 /*
1945 * Fixme: we will run VM in COLO no matter its old running state.
1946 * After exited COLO, we will keep running.
1947 */
1948 old_vm_running = true;
1949 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001950 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001951 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01001952 } else {
1953 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
1954 runstate_set(RUN_STATE_POSTMIGRATE);
1955 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001956 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001957 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001958 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001959 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001960
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001961 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001962 return NULL;
1963}
1964
Juan Quintela9848a402012-12-19 09:55:50 +01001965void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001966{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301967 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001968 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001969
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01001970 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08001971 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301972 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01001973
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001974 /* Notify before starting migration thread */
1975 notifier_list_notify(&migration_state_notifiers, s);
1976
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001977 /*
1978 * Open the return path; currently for postcopy but other things might
1979 * also want it.
1980 */
1981 if (migrate_postcopy_ram()) {
1982 if (open_return_path_on_source(s)) {
1983 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00001984 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001985 MIGRATION_STATUS_FAILED);
1986 migrate_fd_cleanup(s);
1987 return;
1988 }
1989 }
1990
Liang Li8706d2d2015-03-23 16:32:17 +08001991 migrate_compress_threads_create();
Pankaj Gupta009fad72017-01-23 19:12:56 +05301992 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001993 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001994 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001995}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001996
Peter Xu9d18af92017-06-27 12:10:19 +08001997void migration_global_dump(Monitor *mon)
1998{
1999 MigrationState *ms = migrate_get_current();
2000
2001 monitor_printf(mon, "globals: store-global-state=%d, only_migratable=%d, "
2002 "send-configuration=%d, send-section-footer=%d\n",
2003 ms->store_global_state, ms->only_migratable,
2004 ms->send_configuration, ms->send_section_footer);
2005}
2006
Peter Xu52722982017-06-27 12:10:14 +08002007static Property migration_properties[] = {
2008 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2009 store_global_state, true),
Peter Xu3df663e2017-06-27 12:10:15 +08002010 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
Peter Xu71dd4c12017-06-27 12:10:16 +08002011 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2012 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08002013 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2014 send_section_footer, true),
Peter Xu52722982017-06-27 12:10:14 +08002015 DEFINE_PROP_END_OF_LIST(),
2016};
2017
Peter Xue5cb7e72017-06-27 12:10:13 +08002018static void migration_class_init(ObjectClass *klass, void *data)
2019{
2020 DeviceClass *dc = DEVICE_CLASS(klass);
2021
2022 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08002023 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08002024}
2025
2026static void migration_instance_init(Object *obj)
2027{
2028 MigrationState *ms = MIGRATION_OBJ(obj);
2029
2030 ms->state = MIGRATION_STATUS_NONE;
2031 ms->xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE;
2032 ms->mbps = -1;
2033 ms->parameters = (MigrationParameters) {
2034 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
2035 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
2036 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
2037 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
2038 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
2039 .max_bandwidth = MAX_THROTTLE,
2040 .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME,
2041 .x_checkpoint_delay = DEFAULT_MIGRATE_X_CHECKPOINT_DELAY,
2042 };
2043 ms->parameters.tls_creds = g_strdup("");
2044 ms->parameters.tls_hostname = g_strdup("");
2045}
2046
2047static const TypeInfo migration_type = {
2048 .name = TYPE_MIGRATION,
2049 .parent = TYPE_DEVICE,
2050 .class_init = migration_class_init,
2051 .class_size = sizeof(MigrationClass),
2052 .instance_size = sizeof(MigrationState),
2053 .instance_init = migration_instance_init,
2054};
2055
2056static void register_migration_types(void)
2057{
2058 type_register_static(&migration_type);
2059}
2060
2061type_init(register_migration_types);