blob: 0fdb2e410ddbb7da9adbef221ccf3899b1bc2841 [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"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020034#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020035#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020036#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020037#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030038#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010039#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020040#include "qapi-event.h"
Juan Quintela51180422017-04-24 20:50:19 +020041#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010042#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080043#include "migration/colo.h"
Peter Xu4ffdb332017-06-27 12:10:18 +080044#include "hw/boards.h"
Peter Xu9d18af92017-06-27 12:10:19 +080045#include "monitor/monitor.h"
aliguori065e2812008-11-11 16:46:33 +000046
Jason J. Hernedc325622015-09-08 13:12:37 -040047#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000048
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010049/* Amount of time to allocate to each "chunk" of bandwidth-throttled
50 * data. */
51#define BUFFER_DELAY 100
52#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
53
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053054/* Time in milliseconds we are allowed to stop the source,
55 * for sending the last part */
56#define DEFAULT_MIGRATE_SET_DOWNTIME 300
57
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030058/* Maximum migrate downtime set to 2000 seconds */
59#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
60#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
61
Liang Li8706d2d2015-03-23 16:32:17 +080062/* Default compression thread count */
63#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080064/* Default decompression thread count, usually decompression is at
65 * least 4 times as fast as compression.*/
66#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080067/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
68#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040069/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040070#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
71#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080072
Orit Wasserman17ad9b32012-08-06 21:42:53 +030073/* Migration XBZRLE default cache size */
Juan Quintela73af8dd2017-10-05 21:30:10 +020074#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
Orit Wasserman17ad9b32012-08-06 21:42:53 +030075
zhanghailiang68b53592016-10-27 14:43:01 +080076/* The delay time (in ms) between two COLO checkpoints
77 * Note: Please change this default value to 10000 when we support hybrid mode.
78 */
79#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
Juan Quintela4075fb12016-01-15 08:56:17 +010080#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
Juan Quintela0fb86602017-04-27 10:48:25 +020081#define DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT 16
zhanghailiang68b53592016-10-27 14:43:01 +080082
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010083static NotifierList migration_state_notifiers =
84 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
85
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000086static bool deferred_incoming;
87
Juan Quintelada6f1792017-04-24 17:37:14 +020088/* Messages sent on the return path from destination to source */
89enum mig_rp_message_type {
90 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
91 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
92 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
93
94 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
95 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
96
97 MIG_RP_MSG_MAX
98};
99
Juan Quintela17549e82011-10-05 13:50:43 +0200100/* When we add fault tolerance, we could have several
101 migrations at once. For now we don't need to add
102 dynamic creation of migration */
103
Peter Xue5cb7e72017-06-27 12:10:13 +0800104static MigrationState *current_migration;
105
Peter Xu8b0b29d2017-07-18 11:39:06 +0800106static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100107static int migration_maybe_pause(MigrationState *s,
108 int *current_active_state,
109 int new_state);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800110
Peter Xue5cb7e72017-06-27 12:10:13 +0800111void migration_object_init(void)
112{
Peter Xu4ffdb332017-06-27 12:10:18 +0800113 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800114 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800115
Peter Xue5cb7e72017-06-27 12:10:13 +0800116 /* This can only be called once. */
117 assert(!current_migration);
118 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800119
Peter Xu8b0b29d2017-07-18 11:39:06 +0800120 if (!migration_object_check(current_migration, &err)) {
121 error_report_err(err);
122 exit(1);
123 }
124
Peter Xu4ffdb332017-06-27 12:10:18 +0800125 /*
126 * We cannot really do this in migration_instance_init() since at
127 * that time global properties are not yet applied, then this
128 * value will be definitely replaced by something else.
129 */
130 if (ms->enforce_config_section) {
131 current_migration->send_configuration = true;
132 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800133}
134
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300135void migration_object_finalize(void)
136{
137 object_unref(OBJECT(current_migration));
138}
139
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100140/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200141MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200142{
Peter Xue5cb7e72017-06-27 12:10:13 +0800143 /* This can only be called after the object created. */
144 assert(current_migration);
145 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200146}
147
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100148MigrationIncomingState *migration_incoming_get_current(void)
149{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100150 static bool once;
151 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100152
Juan Quintelab4b076d2017-01-23 22:32:06 +0100153 if (!once) {
154 mis_current.state = MIGRATION_STATUS_NONE;
155 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100156 qemu_mutex_init(&mis_current.rp_mutex);
157 qemu_event_init(&mis_current.main_thread_load_event, false);
158 once = true;
159 }
160 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100161}
162
163void migration_incoming_state_destroy(void)
164{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100165 struct MigrationIncomingState *mis = migration_incoming_get_current();
166
Peter Xu34826552017-05-19 14:43:29 +0800167 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800168 /* Tell source that we are done */
169 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800170 qemu_fclose(mis->to_src_file);
171 mis->to_src_file = NULL;
172 }
173
Peter Xu660819b2017-05-19 14:43:30 +0800174 if (mis->from_src_file) {
175 qemu_fclose(mis->from_src_file);
176 mis->from_src_file = NULL;
177 }
178
Dr. David Alan Gilbert5089e182017-08-25 15:19:39 +0100179 qemu_event_reset(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100180}
181
Juan Quintelab05dc722015-07-07 14:44:05 +0200182static void migrate_generate_event(int new_state)
183{
184 if (migrate_use_events()) {
185 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200186 }
187}
188
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000189/*
190 * Called on -incoming with a defer: uri.
191 * The migration can be started later after any parameters have been
192 * changed.
193 */
194static void deferred_incoming_migration(Error **errp)
195{
196 if (deferred_incoming) {
197 error_setg(errp, "Incoming migration already deferred");
198 }
199 deferred_incoming = true;
200}
201
Juan Quintelada6f1792017-04-24 17:37:14 +0200202/*
203 * Send a message on the return channel back to the source
204 * of the migration.
205 */
206static void migrate_send_rp_message(MigrationIncomingState *mis,
207 enum mig_rp_message_type message_type,
208 uint16_t len, void *data)
209{
210 trace_migrate_send_rp_message((int)message_type, len);
211 qemu_mutex_lock(&mis->rp_mutex);
212 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
213 qemu_put_be16(mis->to_src_file, len);
214 qemu_put_buffer(mis->to_src_file, data, len);
215 qemu_fflush(mis->to_src_file);
216 qemu_mutex_unlock(&mis->rp_mutex);
217}
218
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000219/* Request a range of pages from the source VM at the given
220 * start address.
221 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
222 * as the last request (a name must have been given previously)
223 * Start: Address offset within the RB
224 * Len: Length in bytes required - must be a multiple of pagesize
225 */
226void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
227 ram_addr_t start, size_t len)
228{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100229 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000230 size_t msglen = 12; /* start + len */
231
232 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
233 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
234
235 if (rbname) {
236 int rbname_len = strlen(rbname);
237 assert(rbname_len < 256);
238
239 bufc[msglen++] = rbname_len;
240 memcpy(bufc + msglen, rbname, rbname_len);
241 msglen += rbname_len;
242 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
243 } else {
244 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
245 }
246}
247
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200248void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000249{
aliguori34c9dd82008-10-13 03:14:31 +0000250 const char *p;
251
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200252 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000253 if (!strcmp(uri, "defer")) {
254 deferred_incoming_migration(errp);
255 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200256 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400257#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000258 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400259 rdma_start_incoming_migration(p, errp);
260#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000261 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200262 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000263 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200264 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000265 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200266 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000267 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100268 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200269 }
aliguori5bb79102008-10-13 03:12:02 +0000270}
271
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300272static void process_incoming_migration_bh(void *opaque)
273{
274 Error *local_err = NULL;
275 MigrationIncomingState *mis = opaque;
276
Kevin Wolface21a52017-05-04 18:52:36 +0200277 /* Make sure all file formats flush their mutable metadata.
278 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300279 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200280 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200281 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200282 local_err = NULL;
283 autostart = false;
284 }
285
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300286 /*
287 * This must happen after all error conditions are dealt with and
288 * we're sure the VM is going to be running on this host.
289 */
290 qemu_announce_self();
291
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100292 if (multifd_load_cleanup(&local_err) != 0) {
293 error_report_err(local_err);
294 autostart = false;
295 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300296 /* If global state section was not received or we are in running
297 state, we need to obey autostart. Any other state is set with
298 runstate_set. */
299
300 if (!global_state_received() ||
301 global_state_get_runstate() == RUN_STATE_RUNNING) {
302 if (autostart) {
303 vm_start();
304 } else {
305 runstate_set(RUN_STATE_PAUSED);
306 }
307 } else {
308 runstate_set(global_state_get_runstate());
309 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300310 /*
311 * This must happen after any state changes since as soon as an external
312 * observer sees this event they might start to prod at the VM assuming
313 * it's ready to use.
314 */
315 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
316 MIGRATION_STATUS_COMPLETED);
317 qemu_bh_delete(mis->bh);
318 migration_incoming_state_destroy();
319}
320
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200321static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200322{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100323 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000324 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200325 int ret;
326
Juan Quintela4f0fae72017-07-24 12:42:02 +0200327 assert(mis->from_src_file);
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000328 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000329 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000330 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
331 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200332 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100333
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000334 ps = postcopy_state_get();
335 trace_process_incoming_migration_co_end(ret, ps);
336 if (ps != POSTCOPY_INCOMING_NONE) {
337 if (ps == POSTCOPY_INCOMING_ADVISE) {
338 /*
339 * Where a migration had postcopy enabled (and thus went to advise)
340 * but managed to complete within the precopy period, we can use
341 * the normal exit.
342 */
343 postcopy_ram_incoming_cleanup(mis);
344 } else if (ret >= 0) {
345 /*
346 * Postcopy was started, cleanup should happen at the end of the
347 * postcopy thread.
348 */
349 trace_process_incoming_migration_co_postcopy_end_main();
350 return;
351 }
352 /* Else if something went wrong then just fall out of the normal exit */
353 }
354
zhanghailiang25d0c162016-10-27 14:42:55 +0800355 /* we get COLO info, and know if we are in COLO mode */
356 if (!ret && migration_incoming_enable_colo()) {
357 mis->migration_incoming_co = qemu_coroutine_self();
358 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
359 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
360 mis->have_colo_incoming_thread = true;
361 qemu_coroutine_yield();
362
363 /* Wait checkpoint incoming thread exit before free resource */
364 qemu_thread_join(&mis->colo_incoming_thread);
365 }
366
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200367 if (ret < 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100368 Error *local_err = NULL;
369
zhanghailiang93d7af62015-12-16 11:47:34 +0000370 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
371 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200372 error_report("load of migration failed: %s", strerror(-ret));
Dr. David Alan Gilbert3a0f2ce2017-07-17 12:09:32 +0100373 qemu_fclose(mis->from_src_file);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100374 if (multifd_load_cleanup(&local_err) != 0) {
375 error_report_err(local_err);
376 }
Eric Blake4aead692013-04-16 15:50:41 -0600377 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200378 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300379 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
380 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200381}
382
Juan Quintelae595a012017-07-17 12:30:25 +0200383static void migration_incoming_setup(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200384{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200385 MigrationIncomingState *mis = migration_incoming_get_current();
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200386
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100387 if (multifd_load_setup() != 0) {
388 /* We haven't been able to create multifd threads
389 nothing better to do */
390 exit(EXIT_FAILURE);
391 }
392
Juan Quintela4f0fae72017-07-24 12:42:02 +0200393 if (!mis->from_src_file) {
394 mis->from_src_file = f;
395 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100396 qemu_file_set_blocking(f, false);
Juan Quintelae595a012017-07-17 12:30:25 +0200397}
398
399static void migration_incoming_process(void)
400{
401 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200402 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200403}
404
Juan Quintelae595a012017-07-17 12:30:25 +0200405void migration_fd_process_incoming(QEMUFile *f)
406{
407 migration_incoming_setup(f);
408 migration_incoming_process();
409}
410
Juan Quintela4f0fae72017-07-24 12:42:02 +0200411void migration_ioc_process_incoming(QIOChannel *ioc)
412{
413 MigrationIncomingState *mis = migration_incoming_get_current();
414
415 if (!mis->from_src_file) {
416 QEMUFile *f = qemu_fopen_channel_input(ioc);
417 migration_fd_process_incoming(f);
418 }
419 /* We still only have a single channel. Nothing to do here yet */
420}
421
Juan Quintela428d8902017-07-24 13:06:25 +0200422/**
423 * @migration_has_all_channels: We have received all channels that we need
424 *
425 * Returns true when we have got connections to all the channels that
426 * we need for migration.
427 */
428bool migration_has_all_channels(void)
429{
430 return true;
431}
432
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000433/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000434 * Send a 'SHUT' message on the return channel with the given value
435 * to indicate that we've finished with the RP. Non-0 value indicates
436 * error.
437 */
438void migrate_send_rp_shut(MigrationIncomingState *mis,
439 uint32_t value)
440{
441 uint32_t buf;
442
443 buf = cpu_to_be32(value);
444 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
445}
446
447/*
448 * Send a 'PONG' message on the return channel with the given value
449 * (normally in response to a 'PING')
450 */
451void migrate_send_rp_pong(MigrationIncomingState *mis,
452 uint32_t value)
453{
454 uint32_t buf;
455
456 buf = cpu_to_be32(value);
457 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
458}
459
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300460MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
461{
462 MigrationCapabilityStatusList *head = NULL;
463 MigrationCapabilityStatusList *caps;
464 MigrationState *s = migrate_get_current();
465 int i;
466
Michael Tokarev387eede2013-10-05 13:18:28 +0400467 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700468 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100469#ifndef CONFIG_LIVE_BLOCK_MIGRATION
470 if (i == MIGRATION_CAPABILITY_BLOCK) {
471 continue;
472 }
473#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300474 if (head == NULL) {
475 head = g_malloc0(sizeof(*caps));
476 caps = head;
477 } else {
478 caps->next = g_malloc0(sizeof(*caps));
479 caps = caps->next;
480 }
481 caps->value =
482 g_malloc(sizeof(*caps->value));
483 caps->value->capability = i;
484 caps->value->state = s->enabled_capabilities[i];
485 }
486
487 return head;
488}
489
Liang Li85de8322015-03-23 16:32:28 +0800490MigrationParameters *qmp_query_migrate_parameters(Error **errp)
491{
492 MigrationParameters *params;
493 MigrationState *s = migrate_get_current();
494
Markus Armbrustere87fae42017-07-18 12:57:38 +0200495 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800496 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500497 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100498 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500499 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100500 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500501 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100502 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500503 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100504 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500505 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100506 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200507 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100508 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200509 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100510 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530511 params->has_max_bandwidth = true;
512 params->max_bandwidth = s->parameters.max_bandwidth;
513 params->has_downtime_limit = true;
514 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800515 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800516 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200517 params->has_block_incremental = true;
518 params->block_incremental = s->parameters.block_incremental;
Juan Quintela4075fb12016-01-15 08:56:17 +0100519 params->has_x_multifd_channels = true;
520 params->x_multifd_channels = s->parameters.x_multifd_channels;
Juan Quintela0fb86602017-04-27 10:48:25 +0200521 params->has_x_multifd_page_count = true;
522 params->x_multifd_page_count = s->parameters.x_multifd_page_count;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200523 params->has_xbzrle_cache_size = true;
524 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Liang Li85de8322015-03-23 16:32:28 +0800525
526 return params;
527}
528
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000529/*
530 * Return true if we're already in the middle of a migration
531 * (i.e. any of the active or setup states)
532 */
533static bool migration_is_setup_or_active(int state)
534{
535 switch (state) {
536 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000537 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000538 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100539 case MIGRATION_STATUS_PRE_SWITCHOVER:
540 case MIGRATION_STATUS_DEVICE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000541 return true;
542
543 default:
544 return false;
545
546 }
547}
548
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100549static void populate_ram_info(MigrationInfo *info, MigrationState *s)
550{
551 info->has_ram = true;
552 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200553 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100554 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200555 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100556 /* legacy value. It is not used anymore */
557 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200558 info->ram->normal = ram_counters.normal;
559 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100560 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100561 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200562 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
563 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800564 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100565
Juan Quintela114f5ae2017-05-04 10:09:21 +0200566 if (migrate_use_xbzrle()) {
567 info->has_xbzrle_cache = true;
568 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
569 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200570 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
571 info->xbzrle_cache->pages = xbzrle_counters.pages;
572 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
573 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
574 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200575 }
576
Juan Quintela338182c2017-05-03 13:16:38 +0200577 if (cpu_throttle_active()) {
578 info->has_cpu_throttle_percentage = true;
579 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
580 }
581
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100582 if (s->state != MIGRATION_STATUS_COMPLETED) {
583 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200584 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100585 }
586}
587
Juan Quintela930ac042017-05-04 10:21:46 +0200588static void populate_disk_info(MigrationInfo *info)
589{
590 if (blk_mig_active()) {
591 info->has_disk = true;
592 info->disk = g_malloc0(sizeof(*info->disk));
593 info->disk->transferred = blk_mig_bytes_transferred();
594 info->disk->remaining = blk_mig_bytes_remaining();
595 info->disk->total = blk_mig_bytes_total();
596 }
597}
598
Peter Maydellee869812018-01-22 13:20:14 +0000599MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000600{
Peter Maydellee869812018-01-22 13:20:14 +0000601 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200602 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000603
Juan Quintela17549e82011-10-05 13:50:43 +0200604 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800605 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200606 /* no migration has happened ever */
607 break;
zhanghailiang31194732015-03-13 16:08:38 +0800608 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400609 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400610 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400611 break;
zhanghailiang31194732015-03-13 16:08:38 +0800612 case MIGRATION_STATUS_ACTIVE:
613 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000614 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100615 case MIGRATION_STATUS_PRE_SWITCHOVER:
616 case MIGRATION_STATUS_DEVICE:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200617 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000618 info->has_status = true;
619 info->has_total_time = true;
620 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Peter Xu4af246a2018-01-03 20:20:08 +0800621 - s->start_time;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000622 info->has_expected_downtime = true;
623 info->expected_downtime = s->expected_downtime;
624 info->has_setup_time = true;
625 info->setup_time = s->setup_time;
626
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100627 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200628 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000629 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800630 case MIGRATION_STATUS_COLO:
631 info->has_status = true;
632 /* TODO: display COLO specific information (checkpoint info etc.) */
633 break;
zhanghailiang31194732015-03-13 16:08:38 +0800634 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300635 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900636 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200637 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200638 info->has_downtime = true;
639 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400640 info->has_setup_time = true;
641 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200642
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100643 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200644 break;
zhanghailiang31194732015-03-13 16:08:38 +0800645 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300646 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100647 if (s->error) {
648 info->has_error_desc = true;
649 info->error_desc = g_strdup(error_get_pretty(s->error));
650 }
Juan Quintela17549e82011-10-05 13:50:43 +0200651 break;
zhanghailiang31194732015-03-13 16:08:38 +0800652 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300653 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200654 break;
aliguori5bb79102008-10-13 03:12:02 +0000655 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800656 info->status = s->state;
Peter Maydellee869812018-01-22 13:20:14 +0000657
658 return info;
aliguori5bb79102008-10-13 03:12:02 +0000659}
660
Peter Xu4a842142017-07-18 11:39:08 +0800661/**
662 * @migration_caps_check - check capability validity
663 *
664 * @cap_list: old capability list, array of bool
665 * @params: new capabilities to be applied soon
666 * @errp: set *errp if the check failed, with reason
667 *
668 * Returns true if check passed, otherwise false.
669 */
670static bool migrate_caps_check(bool *cap_list,
671 MigrationCapabilityStatusList *params,
672 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +0300673{
Orit Wasserman00458432012-08-06 21:42:48 +0300674 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +0800675 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300676 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +0300677
Peter Xu4a842142017-07-18 11:39:08 +0800678 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +0300679
680 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +0800681 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +0300682 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000683
Peter Xu4a842142017-07-18 11:39:08 +0800684#ifndef CONFIG_LIVE_BLOCK_MIGRATION
685 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
686 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
687 "block migration");
688 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
689 return false;
690 }
691#endif
692
693 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
694 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000695 /* The decompression threads asynchronously write into RAM
696 * rather than use the atomic copies needed to avoid
697 * userfaulting. It should be possible to fix the decompression
698 * threads for compatibility in future.
699 */
Peter Xu4a842142017-07-18 11:39:08 +0800700 error_setg(errp, "Postcopy is not currently compatible "
701 "with compression");
702 return false;
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000703 }
Peter Xu4a842142017-07-18 11:39:08 +0800704
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100705 /* This check is reasonably expensive, so only when it's being
706 * set the first time, also it's only the destination that needs
707 * special support.
708 */
709 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300710 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100711 /* postcopy_ram_supported_by_host will have emitted a more
712 * detailed message
713 */
Peter Xu4a842142017-07-18 11:39:08 +0800714 error_setg(errp, "Postcopy is not supported");
715 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100716 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000717 }
Peter Xu4a842142017-07-18 11:39:08 +0800718
719 return true;
720}
721
722void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
723 Error **errp)
724{
725 MigrationState *s = migrate_get_current();
726 MigrationCapabilityStatusList *cap;
727
728 if (migration_is_setup_or_active(s->state)) {
729 error_setg(errp, QERR_MIGRATION_ACTIVE);
730 return;
731 }
732
733 if (!migrate_caps_check(s->enabled_capabilities, params, errp)) {
734 return;
735 }
736
737 for (cap = params; cap; cap = cap->next) {
738 s->enabled_capabilities[cap->value->capability] = cap->value->state;
739 }
Orit Wasserman00458432012-08-06 21:42:48 +0300740}
741
Peter Xu16d063b2017-07-18 11:39:04 +0800742/*
743 * Check whether the parameters are valid. Error will be put into errp
744 * (if provided). Return true if valid, otherwise false.
745 */
746static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800747{
Eric Blake7f375e02016-09-08 22:14:16 -0500748 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +0100749 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100750 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
751 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +0800752 return false;
Liang Li85de8322015-03-23 16:32:28 +0800753 }
Peter Xu16d063b2017-07-18 11:39:04 +0800754
Juan Quintela741d4082017-12-01 13:08:38 +0100755 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100756 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
757 "compress_threads",
758 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800759 return false;
Liang Li85de8322015-03-23 16:32:28 +0800760 }
Peter Xu16d063b2017-07-18 11:39:04 +0800761
Juan Quintela741d4082017-12-01 13:08:38 +0100762 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100763 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
764 "decompress_threads",
765 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800766 return false;
Liang Li85de8322015-03-23 16:32:28 +0800767 }
Peter Xu16d063b2017-07-18 11:39:04 +0800768
Eric Blake7f375e02016-09-08 22:14:16 -0500769 if (params->has_cpu_throttle_initial &&
770 (params->cpu_throttle_initial < 1 ||
771 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400772 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400773 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400774 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800775 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400776 }
Peter Xu16d063b2017-07-18 11:39:04 +0800777
Eric Blake7f375e02016-09-08 22:14:16 -0500778 if (params->has_cpu_throttle_increment &&
779 (params->cpu_throttle_increment < 1 ||
780 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400781 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400782 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400783 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800784 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400785 }
Peter Xu16d063b2017-07-18 11:39:04 +0800786
Juan Quintela741d4082017-12-01 13:08:38 +0100787 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530788 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
789 " range of 0 to %zu bytes/second", SIZE_MAX);
Peter Xu16d063b2017-07-18 11:39:04 +0800790 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530791 }
Peter Xu16d063b2017-07-18 11:39:04 +0800792
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530793 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +0100794 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300795 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
796 "the range of 0 to %d milliseconds",
797 MAX_MIGRATE_DOWNTIME);
Peter Xu16d063b2017-07-18 11:39:04 +0800798 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530799 }
Peter Xu16d063b2017-07-18 11:39:04 +0800800
Juan Quintela741d4082017-12-01 13:08:38 +0100801 /* x_checkpoint_delay is now always positive */
802
803 if (params->has_x_multifd_channels && (params->x_multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +0100804 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
805 "multifd_channels",
806 "is invalid, it should be in the range of 1 to 255");
807 return false;
808 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200809 if (params->has_x_multifd_page_count &&
Juan Quintela741d4082017-12-01 13:08:38 +0100810 (params->x_multifd_page_count < 1 ||
811 params->x_multifd_page_count > 10000)) {
Juan Quintela0fb86602017-04-27 10:48:25 +0200812 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
813 "multifd_page_count",
814 "is invalid, it should be in the range of 1 to 10000");
815 return false;
816 }
Peter Xu16d063b2017-07-18 11:39:04 +0800817
Juan Quintela73af8dd2017-10-05 21:30:10 +0200818 if (params->has_xbzrle_cache_size &&
819 (params->xbzrle_cache_size < qemu_target_page_size() ||
820 !is_power_of_2(params->xbzrle_cache_size))) {
821 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
822 "xbzrle_cache_size",
823 "is invalid, it should be bigger than target page size"
824 " and a power of two");
825 return false;
826 }
827
Peter Xu16d063b2017-07-18 11:39:04 +0800828 return true;
829}
830
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200831static void migrate_params_test_apply(MigrateSetParameters *params,
832 MigrationParameters *dest)
833{
834 *dest = migrate_get_current()->parameters;
835
836 /* TODO use QAPI_CLONE() instead of duplicating it inline */
837
838 if (params->has_compress_level) {
839 dest->compress_level = params->compress_level;
840 }
841
842 if (params->has_compress_threads) {
843 dest->compress_threads = params->compress_threads;
844 }
845
846 if (params->has_decompress_threads) {
847 dest->decompress_threads = params->decompress_threads;
848 }
849
850 if (params->has_cpu_throttle_initial) {
851 dest->cpu_throttle_initial = params->cpu_throttle_initial;
852 }
853
854 if (params->has_cpu_throttle_increment) {
855 dest->cpu_throttle_increment = params->cpu_throttle_increment;
856 }
857
858 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +0200859 assert(params->tls_creds->type == QTYPE_QSTRING);
860 dest->tls_creds = g_strdup(params->tls_creds->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200861 }
862
863 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +0200864 assert(params->tls_hostname->type == QTYPE_QSTRING);
865 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200866 }
867
868 if (params->has_max_bandwidth) {
869 dest->max_bandwidth = params->max_bandwidth;
870 }
871
872 if (params->has_downtime_limit) {
873 dest->downtime_limit = params->downtime_limit;
874 }
875
876 if (params->has_x_checkpoint_delay) {
877 dest->x_checkpoint_delay = params->x_checkpoint_delay;
878 }
879
880 if (params->has_block_incremental) {
881 dest->block_incremental = params->block_incremental;
882 }
Juan Quintela5e7577a2017-10-09 18:07:56 +0200883 if (params->has_x_multifd_channels) {
884 dest->x_multifd_channels = params->x_multifd_channels;
885 }
886 if (params->has_x_multifd_page_count) {
887 dest->x_multifd_page_count = params->x_multifd_page_count;
888 }
Juan Quintela73af8dd2017-10-05 21:30:10 +0200889 if (params->has_xbzrle_cache_size) {
890 dest->xbzrle_cache_size = params->xbzrle_cache_size;
891 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200892}
893
Juan Quintela73af8dd2017-10-05 21:30:10 +0200894static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +0800895{
896 MigrationState *s = migrate_get_current();
897
Markus Armbrustere87fae42017-07-18 12:57:38 +0200898 /* TODO use QAPI_CLONE() instead of duplicating it inline */
899
Eric Blake7f375e02016-09-08 22:14:16 -0500900 if (params->has_compress_level) {
901 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800902 }
Peter Xu476c72a2017-07-18 11:39:05 +0800903
Eric Blake7f375e02016-09-08 22:14:16 -0500904 if (params->has_compress_threads) {
905 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800906 }
Peter Xu476c72a2017-07-18 11:39:05 +0800907
Eric Blake7f375e02016-09-08 22:14:16 -0500908 if (params->has_decompress_threads) {
909 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800910 }
Peter Xu476c72a2017-07-18 11:39:05 +0800911
Eric Blake7f375e02016-09-08 22:14:16 -0500912 if (params->has_cpu_throttle_initial) {
913 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400914 }
Peter Xu476c72a2017-07-18 11:39:05 +0800915
Eric Blake7f375e02016-09-08 22:14:16 -0500916 if (params->has_cpu_throttle_increment) {
917 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400918 }
Peter Xu476c72a2017-07-18 11:39:05 +0800919
Eric Blake7f375e02016-09-08 22:14:16 -0500920 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100921 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +0200922 assert(params->tls_creds->type == QTYPE_QSTRING);
923 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100924 }
Peter Xu476c72a2017-07-18 11:39:05 +0800925
Eric Blake7f375e02016-09-08 22:14:16 -0500926 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100927 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +0200928 assert(params->tls_hostname->type == QTYPE_QSTRING);
929 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100930 }
Peter Xu476c72a2017-07-18 11:39:05 +0800931
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530932 if (params->has_max_bandwidth) {
933 s->parameters.max_bandwidth = params->max_bandwidth;
934 if (s->to_dst_file) {
935 qemu_file_set_rate_limit(s->to_dst_file,
936 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
937 }
938 }
Peter Xu476c72a2017-07-18 11:39:05 +0800939
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530940 if (params->has_downtime_limit) {
941 s->parameters.downtime_limit = params->downtime_limit;
942 }
zhanghailiang68b53592016-10-27 14:43:01 +0800943
944 if (params->has_x_checkpoint_delay) {
945 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800946 if (migration_in_colo_state()) {
947 colo_checkpoint_notify(s);
948 }
zhanghailiang68b53592016-10-27 14:43:01 +0800949 }
Peter Xu476c72a2017-07-18 11:39:05 +0800950
Juan Quintela2833c592017-04-05 18:32:37 +0200951 if (params->has_block_incremental) {
952 s->parameters.block_incremental = params->block_incremental;
953 }
Juan Quintela4075fb12016-01-15 08:56:17 +0100954 if (params->has_x_multifd_channels) {
955 s->parameters.x_multifd_channels = params->x_multifd_channels;
956 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200957 if (params->has_x_multifd_page_count) {
958 s->parameters.x_multifd_page_count = params->x_multifd_page_count;
959 }
Juan Quintela73af8dd2017-10-05 21:30:10 +0200960 if (params->has_xbzrle_cache_size) {
961 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
962 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
963 }
Liang Li85de8322015-03-23 16:32:28 +0800964}
965
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200966void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +0800967{
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200968 MigrationParameters tmp;
969
Markus Armbruster01fa5592017-07-18 14:42:04 +0200970 /* TODO Rewrite "" to null instead */
971 if (params->has_tls_creds
972 && params->tls_creds->type == QTYPE_QNULL) {
973 QDECREF(params->tls_creds->u.n);
974 params->tls_creds->type = QTYPE_QSTRING;
975 params->tls_creds->u.s = strdup("");
976 }
977 /* TODO Rewrite "" to null instead */
978 if (params->has_tls_hostname
979 && params->tls_hostname->type == QTYPE_QNULL) {
980 QDECREF(params->tls_hostname->u.n);
981 params->tls_hostname->type = QTYPE_QSTRING;
982 params->tls_hostname->u.s = strdup("");
983 }
984
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200985 migrate_params_test_apply(params, &tmp);
986
987 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +0800988 /* Invalid parameter */
989 return;
990 }
991
Juan Quintela73af8dd2017-10-05 21:30:10 +0200992 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +0800993}
994
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100995
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000996void qmp_migrate_start_postcopy(Error **errp)
997{
998 MigrationState *s = migrate_get_current();
999
1000 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001001 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001002 " the start of migration");
1003 return;
1004 }
1005
1006 if (s->state == MIGRATION_STATUS_NONE) {
1007 error_setg(errp, "Postcopy must be started after migration has been"
1008 " started");
1009 return;
1010 }
1011 /*
1012 * we don't error if migration has finished since that would be racy
1013 * with issuing this command.
1014 */
1015 atomic_set(&s->start_postcopy, true);
1016}
1017
aliguori065e2812008-11-11 16:46:33 +00001018/* shared migration helpers */
1019
zhanghailiang48781e52015-12-16 11:47:33 +00001020void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001021{
Peter Xua31fede2017-08-30 16:32:01 +08001022 assert(new_state < MIGRATION_STATUS__MAX);
zhanghailiang48781e52015-12-16 11:47:33 +00001023 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001024 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001025 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001026 }
1027}
1028
Peter Xu4e4a3d32017-07-18 11:39:09 +08001029static MigrationCapabilityStatusList *migrate_cap_add(
1030 MigrationCapabilityStatusList *list,
1031 MigrationCapability index,
1032 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001033{
1034 MigrationCapabilityStatusList *cap;
1035
1036 cap = g_new0(MigrationCapabilityStatusList, 1);
1037 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001038 cap->value->capability = index;
1039 cap->value->state = state;
1040 cap->next = list;
1041
1042 return cap;
1043}
1044
1045void migrate_set_block_enabled(bool value, Error **errp)
1046{
1047 MigrationCapabilityStatusList *cap;
1048
1049 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001050 qmp_migrate_set_capabilities(cap, errp);
1051 qapi_free_MigrationCapabilityStatusList(cap);
1052}
1053
1054static void migrate_set_block_incremental(MigrationState *s, bool value)
1055{
1056 s->parameters.block_incremental = value;
1057}
1058
1059static void block_cleanup_parameters(MigrationState *s)
1060{
1061 if (s->must_remove_block_options) {
1062 /* setting to false can never fail */
1063 migrate_set_block_enabled(false, &error_abort);
1064 migrate_set_block_incremental(s, false);
1065 s->must_remove_block_options = false;
1066 }
1067}
1068
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001069static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +00001070{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001071 MigrationState *s = opaque;
1072
1073 qemu_bh_delete(s->cleanup_bh);
1074 s->cleanup_bh = NULL;
1075
Peter Xu0ceccd82018-01-03 20:20:06 +08001076 qemu_savevm_state_cleanup();
1077
zhanghailiang89a02a92016-01-15 11:37:42 +08001078 if (s->to_dst_file) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001079 Error *local_err = NULL;
1080
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001081 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001082 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001083 if (s->migration_thread_running) {
1084 qemu_thread_join(&s->thread);
1085 s->migration_thread_running = false;
1086 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001087 qemu_mutex_lock_iothread();
1088
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001089 if (multifd_save_cleanup(&local_err) != 0) {
1090 error_report_err(local_err);
1091 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001092 qemu_fclose(s->to_dst_file);
1093 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +00001094 }
1095
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001096 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
1097 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001098
Liang Li94f5a432015-11-02 15:37:00 +08001099 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001100 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001101 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001102 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001103
Juan Quintela87db1a72017-09-05 12:50:22 +02001104 if (s->error) {
1105 /* It is used on info migrate. We can't free it */
1106 error_report_err(error_copy(s->error));
1107 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001108 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001109 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001110}
1111
Juan Quintela87db1a72017-09-05 12:50:22 +02001112void migrate_set_error(MigrationState *s, const Error *error)
1113{
1114 qemu_mutex_lock(&s->error_mutex);
1115 if (!s->error) {
1116 s->error = error_copy(error);
1117 }
1118 qemu_mutex_unlock(&s->error_mutex);
1119}
1120
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001121void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001122{
Peter Maydell25174052016-10-21 18:41:45 +01001123 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001124 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001125 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1126 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001127 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001128}
1129
Juan Quintela0edda1c2010-05-11 16:28:39 +02001130static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001131{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001132 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001133 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001134 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001135
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001136 if (s->rp_state.from_dst_file) {
1137 /* shutdown the rp socket, so causing the rp thread to shutdown */
1138 qemu_file_shutdown(s->rp_state.from_dst_file);
1139 }
1140
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001141 do {
1142 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001143 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001144 break;
1145 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001146 /* If the migration is paused, kick it out of the pause */
1147 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1148 qemu_sem_post(&s->pause_sem);
1149 }
zhanghailiang48781e52015-12-16 11:47:33 +00001150 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001151 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001152
1153 /*
1154 * If we're unlucky the migration code might be stuck somewhere in a
1155 * send/write while the network has failed and is waiting to timeout;
1156 * if we've got shutdown(2) available then we can force it to quit.
1157 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1158 * called in a bh, so there is no race against this cancel.
1159 */
zhanghailiang31194732015-03-13 16:08:38 +08001160 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001161 qemu_file_shutdown(f);
1162 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001163 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1164 Error *local_err = NULL;
1165
1166 bdrv_invalidate_cache_all(&local_err);
1167 if (local_err) {
1168 error_report_err(local_err);
1169 } else {
1170 s->block_inactive = false;
1171 }
1172 }
aliguori065e2812008-11-11 16:46:33 +00001173}
1174
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001175void add_migration_state_change_notifier(Notifier *notify)
1176{
1177 notifier_list_add(&migration_state_notifiers, notify);
1178}
1179
1180void remove_migration_state_change_notifier(Notifier *notify)
1181{
Paolo Bonzini31552522012-01-13 17:34:01 +01001182 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001183}
1184
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001185bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001186{
zhanghailiang31194732015-03-13 16:08:38 +08001187 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001188}
1189
Juan Quintela70736932011-02-23 00:43:59 +01001190bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001191{
zhanghailiang31194732015-03-13 16:08:38 +08001192 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001193}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001194
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001195bool migration_has_failed(MigrationState *s)
1196{
zhanghailiang31194732015-03-13 16:08:38 +08001197 return (s->state == MIGRATION_STATUS_CANCELLED ||
1198 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001199}
1200
Juan Quintela57273092017-03-20 22:25:28 +01001201bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001202{
Juan Quintela57273092017-03-20 22:25:28 +01001203 MigrationState *s = migrate_get_current();
1204
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001205 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1206}
1207
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001208bool migration_in_postcopy_after_devices(MigrationState *s)
1209{
Juan Quintela57273092017-03-20 22:25:28 +01001210 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001211}
1212
Juan Quintelafab35002017-03-22 17:36:57 +01001213bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301214{
Juan Quintelafab35002017-03-22 17:36:57 +01001215 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301216
1217 switch (s->state) {
1218 case MIGRATION_STATUS_NONE:
1219 case MIGRATION_STATUS_CANCELLED:
1220 case MIGRATION_STATUS_COMPLETED:
1221 case MIGRATION_STATUS_FAILED:
1222 return true;
1223 case MIGRATION_STATUS_SETUP:
1224 case MIGRATION_STATUS_CANCELLING:
1225 case MIGRATION_STATUS_ACTIVE:
1226 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1227 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001228 case MIGRATION_STATUS_PRE_SWITCHOVER:
1229 case MIGRATION_STATUS_DEVICE:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301230 return false;
1231 case MIGRATION_STATUS__MAX:
1232 g_assert_not_reached();
1233 }
1234
1235 return false;
1236}
1237
Juan Quintelaa0762d92017-04-05 21:00:09 +02001238MigrationState *migrate_init(void)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001239{
Juan Quintela17549e82011-10-05 13:50:43 +02001240 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +03001241
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001242 /*
1243 * Reinitialise all migration state, except
1244 * parameters/capabilities that the user set, and
1245 * locks.
1246 */
1247 s->bytes_xfer = 0;
1248 s->xfer_limit = 0;
1249 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001250 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001251 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001252 s->rp_state.from_dst_file = NULL;
1253 s->rp_state.error = false;
1254 s->mbps = 0.0;
1255 s->downtime = 0;
1256 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001257 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001258 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001259 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001260 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001261 error_free(s->error);
1262 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001263
zhanghailiang48781e52015-12-16 11:47:33 +00001264 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001265
Peter Xu4af246a2018-01-03 20:20:08 +08001266 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1267 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001268 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001269 s->iteration_initial_bytes = 0;
1270 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001271 return s;
1272}
Juan Quintelacab30142011-02-22 23:54:21 +01001273
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001274static GSList *migration_blockers;
1275
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301276int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001277{
Peter Xu3df663e2017-06-27 12:10:15 +08001278 if (migrate_get_current()->only_migratable) {
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301279 error_propagate(errp, error_copy(reason));
1280 error_prepend(errp, "disallowing migration blocker "
1281 "(--only_migratable) for: ");
1282 return -EACCES;
1283 }
1284
Juan Quintelafab35002017-03-22 17:36:57 +01001285 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301286 migration_blockers = g_slist_prepend(migration_blockers, reason);
1287 return 0;
1288 }
1289
1290 error_propagate(errp, error_copy(reason));
1291 error_prepend(errp, "disallowing migration blocker (migration in "
1292 "progress) for: ");
1293 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001294}
1295
1296void migrate_del_blocker(Error *reason)
1297{
1298 migration_blockers = g_slist_remove(migration_blockers, reason);
1299}
1300
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001301void qmp_migrate_incoming(const char *uri, Error **errp)
1302{
1303 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001304 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001305
1306 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001307 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001308 return;
1309 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001310 if (!once) {
1311 error_setg(errp, "The incoming migration has already been started");
1312 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001313
1314 qemu_start_incoming_migration(uri, &local_err);
1315
1316 if (local_err) {
1317 error_propagate(errp, local_err);
1318 return;
1319 }
1320
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001321 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001322}
1323
Greg Kurz24f39022016-05-04 21:44:19 +02001324bool migration_is_blocked(Error **errp)
1325{
1326 if (qemu_savevm_state_blocked(errp)) {
1327 return true;
1328 }
1329
1330 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001331 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001332 return true;
1333 }
1334
1335 return false;
1336}
1337
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001338void qmp_migrate(const char *uri, bool has_blk, bool blk,
1339 bool has_inc, bool inc, bool has_detach, bool detach,
1340 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001341{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001342 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001343 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001344 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001345
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001346 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001347 s->state == MIGRATION_STATUS_CANCELLING ||
1348 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001349 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001350 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001351 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001352 if (runstate_check(RUN_STATE_INMIGRATE)) {
1353 error_setg(errp, "Guest is waiting for an incoming migration");
1354 return;
1355 }
1356
Greg Kurz24f39022016-05-04 21:44:19 +02001357 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001358 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001359 }
1360
Juan Quintela2833c592017-04-05 18:32:37 +02001361 if ((has_blk && blk) || (has_inc && inc)) {
1362 if (migrate_use_block() || migrate_use_block_incremental()) {
1363 error_setg(errp, "Command options are incompatible with "
1364 "current migration capabilities");
1365 return;
1366 }
1367 migrate_set_block_enabled(true, &local_err);
1368 if (local_err) {
1369 error_propagate(errp, local_err);
1370 return;
1371 }
1372 s->must_remove_block_options = true;
1373 }
1374
1375 if (has_inc && inc) {
1376 migrate_set_block_incremental(s, true);
1377 }
1378
Juan Quintelaa0762d92017-04-05 21:00:09 +02001379 s = migrate_init();
Juan Quintelacab30142011-02-22 23:54:21 +01001380
1381 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001382 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001383#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001384 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001385 rdma_start_outgoing_migration(s, p, &local_err);
1386#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001387 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001388 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001389 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001390 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001391 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001392 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001393 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001394 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1395 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001396 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1397 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001398 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001399 }
1400
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001401 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001402 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001403 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001404 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001405 }
Juan Quintelacab30142011-02-22 23:54:21 +01001406}
1407
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001408void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001409{
Juan Quintela17549e82011-10-05 13:50:43 +02001410 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001411}
1412
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01001413void qmp_migrate_continue(MigrationStatus state, Error **errp)
1414{
1415 MigrationState *s = migrate_get_current();
1416 if (s->state != state) {
1417 error_setg(errp, "Migration not in expected state: %s",
1418 MigrationStatus_str(s->state));
1419 return;
1420 }
1421 qemu_sem_post(&s->pause_sem);
1422}
1423
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001424void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1425{
Juan Quintela73af8dd2017-10-05 21:30:10 +02001426 MigrateSetParameters p = {
1427 .has_xbzrle_cache_size = true,
1428 .xbzrle_cache_size = value,
1429 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001430
Juan Quintela73af8dd2017-10-05 21:30:10 +02001431 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001432}
1433
1434int64_t qmp_query_migrate_cache_size(Error **errp)
1435{
1436 return migrate_xbzrle_cache_size();
1437}
1438
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001439void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001440{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001441 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301442 .has_max_bandwidth = true,
1443 .max_bandwidth = value,
1444 };
Juan Quintelacab30142011-02-22 23:54:21 +01001445
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301446 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001447}
1448
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001449void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001450{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001451 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1452 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1453 "the range of 0 to %d seconds",
1454 MAX_MIGRATE_DOWNTIME_SECONDS);
1455 return;
1456 }
1457
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301458 value *= 1000; /* Convert to milliseconds */
1459 value = MAX(0, MIN(INT64_MAX, value));
1460
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001461 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301462 .has_downtime_limit = true,
1463 .downtime_limit = value,
1464 };
1465
1466 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001467}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001468
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001469bool migrate_release_ram(void)
1470{
1471 MigrationState *s;
1472
1473 s = migrate_get_current();
1474
1475 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1476}
1477
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001478bool migrate_postcopy_ram(void)
1479{
1480 MigrationState *s;
1481
1482 s = migrate_get_current();
1483
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001484 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001485}
1486
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001487bool migrate_postcopy(void)
1488{
1489 return migrate_postcopy_ram();
1490}
1491
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001492bool migrate_auto_converge(void)
1493{
1494 MigrationState *s;
1495
1496 s = migrate_get_current();
1497
1498 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1499}
1500
Peter Lieven323004a2013-07-18 09:48:50 +02001501bool migrate_zero_blocks(void)
1502{
1503 MigrationState *s;
1504
1505 s = migrate_get_current();
1506
1507 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1508}
1509
Liang Li8706d2d2015-03-23 16:32:17 +08001510bool migrate_use_compression(void)
1511{
Liang Lidde4e692015-03-23 16:32:26 +08001512 MigrationState *s;
1513
1514 s = migrate_get_current();
1515
1516 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001517}
1518
1519int migrate_compress_level(void)
1520{
1521 MigrationState *s;
1522
1523 s = migrate_get_current();
1524
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001525 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001526}
1527
1528int migrate_compress_threads(void)
1529{
1530 MigrationState *s;
1531
1532 s = migrate_get_current();
1533
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001534 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001535}
1536
Liang Li3fcb38c2015-03-23 16:32:18 +08001537int migrate_decompress_threads(void)
1538{
1539 MigrationState *s;
1540
1541 s = migrate_get_current();
1542
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001543 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001544}
1545
Juan Quintelab05dc722015-07-07 14:44:05 +02001546bool migrate_use_events(void)
1547{
1548 MigrationState *s;
1549
1550 s = migrate_get_current();
1551
1552 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1553}
1554
Juan Quintela30126bb2016-01-14 12:23:00 +01001555bool migrate_use_multifd(void)
1556{
1557 MigrationState *s;
1558
1559 s = migrate_get_current();
1560
1561 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD];
1562}
1563
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01001564bool migrate_pause_before_switchover(void)
1565{
1566 MigrationState *s;
1567
1568 s = migrate_get_current();
1569
1570 return s->enabled_capabilities[
1571 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
1572}
1573
Juan Quintela4075fb12016-01-15 08:56:17 +01001574int migrate_multifd_channels(void)
1575{
1576 MigrationState *s;
1577
1578 s = migrate_get_current();
1579
1580 return s->parameters.x_multifd_channels;
1581}
1582
Juan Quintela0fb86602017-04-27 10:48:25 +02001583int migrate_multifd_page_count(void)
1584{
1585 MigrationState *s;
1586
1587 s = migrate_get_current();
1588
1589 return s->parameters.x_multifd_page_count;
1590}
1591
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001592int migrate_use_xbzrle(void)
1593{
1594 MigrationState *s;
1595
1596 s = migrate_get_current();
1597
1598 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1599}
1600
1601int64_t migrate_xbzrle_cache_size(void)
1602{
1603 MigrationState *s;
1604
1605 s = migrate_get_current();
1606
Juan Quintela73af8dd2017-10-05 21:30:10 +02001607 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001608}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001609
Juan Quintela2833c592017-04-05 18:32:37 +02001610bool migrate_use_block(void)
1611{
1612 MigrationState *s;
1613
1614 s = migrate_get_current();
1615
1616 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1617}
1618
Peter Xuc788ada2017-06-26 18:28:55 +08001619bool migrate_use_return_path(void)
1620{
1621 MigrationState *s;
1622
1623 s = migrate_get_current();
1624
1625 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1626}
1627
Juan Quintela2833c592017-04-05 18:32:37 +02001628bool migrate_use_block_incremental(void)
1629{
1630 MigrationState *s;
1631
1632 s = migrate_get_current();
1633
1634 return s->parameters.block_incremental;
1635}
1636
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001637/* migration thread support */
1638/*
1639 * Something bad happened to the RP stream, mark an error
1640 * The caller shall print or trace something to indicate why
1641 */
1642static void mark_source_rp_bad(MigrationState *s)
1643{
1644 s->rp_state.error = true;
1645}
1646
1647static struct rp_cmd_args {
1648 ssize_t len; /* -1 = variable */
1649 const char *name;
1650} rp_cmd_args[] = {
1651 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1652 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1653 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001654 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1655 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001656 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1657};
1658
1659/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001660 * Process a request for pages received on the return path,
1661 * We're allowed to send more than requested (e.g. to round to our page size)
1662 * and we don't need to send pages that have already been sent.
1663 */
1664static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1665 ram_addr_t start, size_t len)
1666{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001667 long our_host_ps = getpagesize();
1668
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001669 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001670
1671 /*
1672 * Since we currently insist on matching page sizes, just sanity check
1673 * we're being asked for whole host pages.
1674 */
1675 if (start & (our_host_ps-1) ||
1676 (len & (our_host_ps-1))) {
1677 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1678 " len: %zd", __func__, start, len);
1679 mark_source_rp_bad(ms);
1680 return;
1681 }
1682
Juan Quintela96506892017-03-14 18:41:03 +01001683 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001684 mark_source_rp_bad(ms);
1685 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001686}
1687
1688/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001689 * Handles messages sent on the return path towards the source VM
1690 *
1691 */
1692static void *source_return_path_thread(void *opaque)
1693{
1694 MigrationState *ms = opaque;
1695 QEMUFile *rp = ms->rp_state.from_dst_file;
1696 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001697 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001698 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001699 ram_addr_t start = 0; /* =0 to silence warning */
1700 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001701 int res;
1702
1703 trace_source_return_path_thread_entry();
1704 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1705 migration_is_setup_or_active(ms->state)) {
1706 trace_source_return_path_thread_loop_top();
1707 header_type = qemu_get_be16(rp);
1708 header_len = qemu_get_be16(rp);
1709
1710 if (header_type >= MIG_RP_MSG_MAX ||
1711 header_type == MIG_RP_MSG_INVALID) {
1712 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1713 header_type, header_len);
1714 mark_source_rp_bad(ms);
1715 goto out;
1716 }
1717
1718 if ((rp_cmd_args[header_type].len != -1 &&
1719 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001720 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001721 error_report("RP: Received '%s' message (0x%04x) with"
1722 "incorrect length %d expecting %zu",
1723 rp_cmd_args[header_type].name, header_type, header_len,
1724 (size_t)rp_cmd_args[header_type].len);
1725 mark_source_rp_bad(ms);
1726 goto out;
1727 }
1728
1729 /* We know we've got a valid header by this point */
1730 res = qemu_get_buffer(rp, buf, header_len);
1731 if (res != header_len) {
1732 error_report("RP: Failed reading data for message 0x%04x"
1733 " read %d expected %d",
1734 header_type, res, header_len);
1735 mark_source_rp_bad(ms);
1736 goto out;
1737 }
1738
1739 /* OK, we have the message and the data */
1740 switch (header_type) {
1741 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001742 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001743 trace_source_return_path_thread_shut(sibling_error);
1744 if (sibling_error) {
1745 error_report("RP: Sibling indicated error %d", sibling_error);
1746 mark_source_rp_bad(ms);
1747 }
1748 /*
1749 * We'll let the main thread deal with closing the RP
1750 * we could do a shutdown(2) on it, but we're the only user
1751 * anyway, so there's nothing gained.
1752 */
1753 goto out;
1754
1755 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001756 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001757 trace_source_return_path_thread_pong(tmp32);
1758 break;
1759
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001760 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001761 start = ldq_be_p(buf);
1762 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001763 migrate_handle_rp_req_pages(ms, NULL, start, len);
1764 break;
1765
1766 case MIG_RP_MSG_REQ_PAGES_ID:
1767 expected_len = 12 + 1; /* header + termination */
1768
1769 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001770 start = ldq_be_p(buf);
1771 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001772 /* Now we expect an idstr */
1773 tmp32 = buf[12]; /* Length of the following idstr */
1774 buf[13 + tmp32] = '\0';
1775 expected_len += tmp32;
1776 }
1777 if (header_len != expected_len) {
1778 error_report("RP: Req_Page_id with length %d expecting %zd",
1779 header_len, expected_len);
1780 mark_source_rp_bad(ms);
1781 goto out;
1782 }
1783 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1784 break;
1785
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001786 default:
1787 break;
1788 }
1789 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001790 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001791 trace_source_return_path_thread_bad_end();
1792 mark_source_rp_bad(ms);
1793 }
1794
1795 trace_source_return_path_thread_end();
1796out:
1797 ms->rp_state.from_dst_file = NULL;
1798 qemu_fclose(rp);
1799 return NULL;
1800}
1801
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001802static int open_return_path_on_source(MigrationState *ms)
1803{
1804
zhanghailiang89a02a92016-01-15 11:37:42 +08001805 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001806 if (!ms->rp_state.from_dst_file) {
1807 return -1;
1808 }
1809
1810 trace_open_return_path_on_source();
1811 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1812 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1813
1814 trace_open_return_path_on_source_continue();
1815
1816 return 0;
1817}
1818
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001819/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1820static int await_return_path_close_on_source(MigrationState *ms)
1821{
1822 /*
1823 * If this is a normal exit then the destination will send a SHUT and the
1824 * rp_thread will exit, however if there's an error we need to cause
1825 * it to exit.
1826 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001827 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001828 /*
1829 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1830 * waiting for the destination.
1831 */
1832 qemu_file_shutdown(ms->rp_state.from_dst_file);
1833 mark_source_rp_bad(ms);
1834 }
1835 trace_await_return_path_close_on_source_joining();
1836 qemu_thread_join(&ms->rp_state.rp_thread);
1837 trace_await_return_path_close_on_source_close();
1838 return ms->rp_state.error;
1839}
1840
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001841/*
1842 * Switch from normal iteration to postcopy
1843 * Returns non-0 on error
1844 */
Peter Xu7287cbd2018-01-03 20:20:09 +08001845static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001846{
1847 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001848 QIOChannelBuffer *bioc;
1849 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001850 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001851 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01001852 int cur_state = MIGRATION_STATUS_ACTIVE;
1853 if (!migrate_pause_before_switchover()) {
1854 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
1855 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1856 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001857
1858 trace_postcopy_start();
1859 qemu_mutex_lock_iothread();
1860 trace_postcopy_start_set_run();
1861
1862 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001863 global_state_store();
1864 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001865 if (ret < 0) {
1866 goto fail;
1867 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001868
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01001869 ret = migration_maybe_pause(ms, &cur_state,
1870 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1871 if (ret < 0) {
1872 goto fail;
1873 }
1874
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001875 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001876 if (ret < 0) {
1877 goto fail;
1878 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001879 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001880
1881 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001882 * Cause any non-postcopiable, but iterative devices to
1883 * send out their final data.
1884 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08001885 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001886
1887 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001888 * in Finish migrate and with the io-lock held everything should
1889 * be quiet, but we've potentially still got dirty pages and we
1890 * need to tell the destination to throw any pages it's already received
1891 * that are dirty
1892 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001893 if (migrate_postcopy_ram()) {
1894 if (ram_postcopy_send_discard_bitmap(ms)) {
1895 error_report("postcopy send discard bitmap failed");
1896 goto fail;
1897 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001898 }
1899
1900 /*
1901 * send rest of state - note things that are doing postcopy
1902 * will notice we're in POSTCOPY_ACTIVE and not actually
1903 * wrap their state up here
1904 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001905 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001906 if (migrate_postcopy_ram()) {
1907 /* Ping just for debugging, helps line traces up */
1908 qemu_savevm_send_ping(ms->to_dst_file, 2);
1909 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001910
1911 /*
1912 * While loading the device state we may trigger page transfer
1913 * requests and the fd must be free to process those, and thus
1914 * the destination must read the whole device state off the fd before
1915 * it starts processing it. Unfortunately the ad-hoc migration format
1916 * doesn't allow the destination to know the size to read without fully
1917 * parsing it through each devices load-state code (especially the open
1918 * coded devices that use get/put).
1919 * So we wrap the device state up in a package with a length at the start;
1920 * to do this we use a qemu_buf to hold the whole of the device state.
1921 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001922 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001923 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001924 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1925 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001926
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001927 /*
1928 * Make sure the receiver can get incoming pages before we send the rest
1929 * of the state
1930 */
1931 qemu_savevm_send_postcopy_listen(fb);
1932
Fam Zhenga1fbe752017-06-17 00:06:58 +08001933 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001934 if (migrate_postcopy_ram()) {
1935 qemu_savevm_send_ping(fb, 3);
1936 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001937
1938 qemu_savevm_send_postcopy_run(fb);
1939
1940 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001941
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001942 /* Last point of recovery; as soon as we send the package the destination
1943 * can open devices and potentially start running.
1944 * Lets just check again we've not got any errors.
1945 */
1946 ret = qemu_file_get_error(ms->to_dst_file);
1947 if (ret) {
1948 error_report("postcopy_start: Migration stream errored (pre package)");
1949 goto fail_closefb;
1950 }
1951
1952 restart_block = false;
1953
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001954 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001955 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001956 goto fail_closefb;
1957 }
1958 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001959
1960 /* Send a notify to give a chance for anything that needs to happen
1961 * at the transition to postcopy and after the device state; in particular
1962 * spice needs to trigger a transition now
1963 */
1964 ms->postcopy_after_devices = true;
1965 notifier_list_notify(&migration_state_notifiers, ms);
1966
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001967 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1968
1969 qemu_mutex_unlock_iothread();
1970
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001971 if (migrate_postcopy_ram()) {
1972 /*
1973 * Although this ping is just for debug, it could potentially be
1974 * used for getting a better measurement of downtime at the source.
1975 */
1976 qemu_savevm_send_ping(ms->to_dst_file, 4);
1977 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001978
Pavel Butsykinced1c612017-02-03 18:23:21 +03001979 if (migrate_release_ram()) {
1980 ram_postcopy_migrated_memory_release(ms);
1981 }
1982
zhanghailiang89a02a92016-01-15 11:37:42 +08001983 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001984 if (ret) {
1985 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001986 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001987 MIGRATION_STATUS_FAILED);
1988 }
1989
1990 return ret;
1991
1992fail_closefb:
1993 qemu_fclose(fb);
1994fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001995 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001996 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001997 if (restart_block) {
1998 /* A failure happened early enough that we know the destination hasn't
1999 * accessed block devices, so we're safe to recover.
2000 */
2001 Error *local_err = NULL;
2002
2003 bdrv_invalidate_cache_all(&local_err);
2004 if (local_err) {
2005 error_report_err(local_err);
2006 }
2007 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002008 qemu_mutex_unlock_iothread();
2009 return -1;
2010}
2011
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002012/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002013 * migration_maybe_pause: Pause if required to by
2014 * migrate_pause_before_switchover called with the iothread locked
2015 * Returns: 0 on success
2016 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002017static int migration_maybe_pause(MigrationState *s,
2018 int *current_active_state,
2019 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002020{
2021 if (!migrate_pause_before_switchover()) {
2022 return 0;
2023 }
2024
2025 /* Since leaving this state is not atomic with posting the semaphore
2026 * it's possible that someone could have issued multiple migrate_continue
2027 * and the semaphore is incorrectly positive at this point;
2028 * the docs say it's undefined to reinit a semaphore that's already
2029 * init'd, so use timedwait to eat up any existing posts.
2030 */
2031 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2032 /* This block intentionally left blank */
2033 }
2034
2035 qemu_mutex_unlock_iothread();
2036 migrate_set_state(&s->state, *current_active_state,
2037 MIGRATION_STATUS_PRE_SWITCHOVER);
2038 qemu_sem_wait(&s->pause_sem);
2039 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002040 new_state);
2041 *current_active_state = new_state;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002042 qemu_mutex_lock_iothread();
2043
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002044 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002045}
2046
2047/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002048 * migration_completion: Used by migration_thread when there's not much left.
2049 * The caller 'breaks' the loop when this returns.
2050 *
2051 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002052 */
Peter Xu2ad87302018-01-03 20:20:14 +08002053static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002054{
2055 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08002056 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002057
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002058 if (s->state == MIGRATION_STATUS_ACTIVE) {
2059 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08002060 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002061 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Peter Xu7287cbd2018-01-03 20:20:09 +08002062 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002063 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002064
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002065 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08002066 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002067 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002068 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002069 ret = migration_maybe_pause(s, &current_active_state,
2070 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002071 }
2072 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002073 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08002074 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2075 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002076 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08002077 if (inactivate && ret >= 0) {
2078 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002079 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002080 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002081 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002082
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002083 if (ret < 0) {
2084 goto fail;
2085 }
2086 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2087 trace_migration_completion_postcopy_end();
2088
zhanghailiang89a02a92016-01-15 11:37:42 +08002089 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002090 trace_migration_completion_postcopy_end_after_complete();
2091 }
2092
2093 /*
2094 * If rp was opened we must clean up the thread before
2095 * cleaning everything else up (since if there are no failures
2096 * it will wait for the destination to send it's status in
2097 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002098 */
Peter Xu0425dc92017-05-31 18:35:34 +08002099 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002100 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08002101 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002102 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08002103 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002104 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002105 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002106 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002107 }
2108
zhanghailiang89a02a92016-01-15 11:37:42 +08002109 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002110 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002111 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002112 }
2113
zhanghailiang0b827d52016-10-27 14:42:54 +08002114 if (!migrate_colo_enabled()) {
2115 migrate_set_state(&s->state, current_active_state,
2116 MIGRATION_STATUS_COMPLETED);
2117 }
2118
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002119 return;
2120
Greg Kurzfe904ea2016-05-18 15:44:36 +02002121fail_invalidate:
2122 /* If not doing postcopy, vm_start() will be called: let's regain
2123 * control on images.
2124 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00002125 if (s->state == MIGRATION_STATUS_ACTIVE ||
2126 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002127 Error *local_err = NULL;
2128
zhanghailiang1d2acc32017-01-24 15:59:52 +08002129 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002130 bdrv_invalidate_cache_all(&local_err);
2131 if (local_err) {
2132 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08002133 } else {
2134 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02002135 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08002136 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002137 }
2138
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002139fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002140 migrate_set_state(&s->state, current_active_state,
2141 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002142}
2143
zhanghailiang35a6ed42016-10-27 14:42:52 +08002144bool migrate_colo_enabled(void)
2145{
2146 MigrationState *s = migrate_get_current();
2147 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2148}
2149
Peter Xucf011f02018-01-03 20:20:11 +08002150static void migration_calculate_complete(MigrationState *s)
2151{
2152 uint64_t bytes = qemu_ftell(s->to_dst_file);
2153 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2154
2155 s->total_time = end_time - s->start_time;
2156 if (!s->downtime) {
2157 /*
2158 * It's still not set, so we are precopy migration. For
2159 * postcopy, downtime is calculated during postcopy_start().
2160 */
2161 s->downtime = end_time - s->downtime_start;
2162 }
2163
2164 if (s->total_time) {
2165 s->mbps = ((double) bytes * 8.0) / s->total_time / 1000;
2166 }
2167}
2168
Peter Xub15df1a2018-01-03 20:20:13 +08002169static void migration_update_counters(MigrationState *s,
2170 int64_t current_time)
2171{
2172 uint64_t transferred, time_spent;
Peter Xub15df1a2018-01-03 20:20:13 +08002173 double bandwidth;
2174
2175 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2176 return;
2177 }
2178
2179 transferred = qemu_ftell(s->to_dst_file) - s->iteration_initial_bytes;
2180 time_spent = current_time - s->iteration_start_time;
2181 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08002182 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08002183
2184 s->mbps = (((double) transferred * 8.0) /
2185 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2186
2187 /*
2188 * if we haven't sent anything, we don't want to
2189 * recalculate. 10000 is a small enough number for our purposes
2190 */
2191 if (ram_counters.dirty_pages_rate && transferred > 10000) {
2192 s->expected_downtime = ram_counters.dirty_pages_rate *
2193 qemu_target_page_size() / bandwidth;
2194 }
2195
2196 qemu_file_reset_rate_limit(s->to_dst_file);
2197
2198 s->iteration_start_time = current_time;
2199 s->iteration_initial_bytes = qemu_ftell(s->to_dst_file);
2200
2201 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08002202 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08002203}
2204
Peter Xu2ad87302018-01-03 20:20:14 +08002205/* Migration thread iteration status */
2206typedef enum {
2207 MIG_ITERATE_RESUME, /* Resume current iteration */
2208 MIG_ITERATE_SKIP, /* Skip current iteration */
2209 MIG_ITERATE_BREAK, /* Break the loop */
2210} MigIterateState;
2211
2212/*
2213 * Return true if continue to the next iteration directly, false
2214 * otherwise.
2215 */
2216static MigIterateState migration_iteration_run(MigrationState *s)
2217{
2218 uint64_t pending_size, pend_post, pend_nonpost;
2219 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2220
2221 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size,
2222 &pend_nonpost, &pend_post);
2223 pending_size = pend_nonpost + pend_post;
2224
2225 trace_migrate_pending(pending_size, s->threshold_size,
2226 pend_post, pend_nonpost);
2227
2228 if (pending_size && pending_size >= s->threshold_size) {
2229 /* Still a significant amount to transfer */
2230 if (migrate_postcopy() && !in_postcopy &&
2231 pend_nonpost <= s->threshold_size &&
2232 atomic_read(&s->start_postcopy)) {
2233 if (postcopy_start(s)) {
2234 error_report("%s: postcopy failed to start", __func__);
2235 }
2236 return MIG_ITERATE_SKIP;
2237 }
2238 /* Just another iteration step */
2239 qemu_savevm_state_iterate(s->to_dst_file,
2240 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2241 } else {
2242 trace_migration_thread_low_pending(pending_size);
2243 migration_completion(s);
2244 return MIG_ITERATE_BREAK;
2245 }
2246
2247 return MIG_ITERATE_RESUME;
2248}
2249
Peter Xu199aa6d2018-01-03 20:20:15 +08002250static void migration_iteration_finish(MigrationState *s)
2251{
2252 /* If we enabled cpu throttling for auto-converge, turn it off. */
2253 cpu_throttle_stop();
2254
2255 qemu_mutex_lock_iothread();
2256 switch (s->state) {
2257 case MIGRATION_STATUS_COMPLETED:
2258 migration_calculate_complete(s);
2259 runstate_set(RUN_STATE_POSTMIGRATE);
2260 break;
2261
2262 case MIGRATION_STATUS_ACTIVE:
2263 /*
2264 * We should really assert here, but since it's during
2265 * migration, let's try to reduce the usage of assertions.
2266 */
2267 if (!migrate_colo_enabled()) {
2268 error_report("%s: critical error: calling COLO code without "
2269 "COLO enabled", __func__);
2270 }
2271 migrate_start_colo_process(s);
2272 /*
2273 * Fixme: we will run VM in COLO no matter its old running state.
2274 * After exited COLO, we will keep running.
2275 */
2276 s->vm_was_running = true;
2277 /* Fallthrough */
2278 case MIGRATION_STATUS_FAILED:
2279 case MIGRATION_STATUS_CANCELLED:
2280 if (s->vm_was_running) {
2281 vm_start();
2282 } else {
2283 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2284 runstate_set(RUN_STATE_POSTMIGRATE);
2285 }
2286 }
2287 break;
2288
2289 default:
2290 /* Should not reach here, but if so, forgive the VM. */
2291 error_report("%s: Unknown ending state %d", __func__, s->state);
2292 break;
2293 }
2294 qemu_bh_schedule(s->cleanup_bh);
2295 qemu_mutex_unlock_iothread();
2296}
2297
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002298/*
2299 * Master migration thread on the source VM.
2300 * It drives the migration and pumps the data down the outgoing channel.
2301 */
Juan Quintela5f496a12013-02-22 17:36:30 +01002302static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002303{
Juan Quintela9848a402012-12-19 09:55:50 +01002304 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01002305 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Juan Quintela76f59332012-10-03 20:16:24 +02002306
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002307 rcu_register_thread();
2308
Peter Xub15df1a2018-01-03 20:20:13 +08002309 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2310
zhanghailiang89a02a92016-01-15 11:37:42 +08002311 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002312
Peter Xu62a02652017-06-14 15:55:58 +08002313 /*
2314 * If we opened the return path, we need to make sure dst has it
2315 * opened as well.
2316 */
2317 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002318 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08002319 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002320
2321 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08002322 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08002323 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002324
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002325 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002326 /*
2327 * Tell the destination that we *might* want to do postcopy later;
2328 * if the other end can't do postcopy it should fail now, nice and
2329 * early.
2330 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002331 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002332 }
2333
Juan Quintela9907e842017-06-28 11:52:24 +02002334 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002335
Alex Blighbc72ad62013-08-21 16:03:08 +01002336 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00002337 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2338 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04002339
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00002340 trace_migration_thread_setup_complete();
2341
2342 while (s->state == MIGRATION_STATUS_ACTIVE ||
2343 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01002344 int64_t current_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002345
zhanghailiang89a02a92016-01-15 11:37:42 +08002346 if (!qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08002347 MigIterateState iter_state = migration_iteration_run(s);
2348 if (iter_state == MIG_ITERATE_SKIP) {
2349 continue;
2350 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002351 break;
Juan Quintelac369f402012-10-03 20:33:34 +02002352 }
2353 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002354
zhanghailiang89a02a92016-01-15 11:37:42 +08002355 if (qemu_file_get_error(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08002356 if (migration_is_setup_or_active(s->state)) {
2357 migrate_set_state(&s->state, s->state,
2358 MIGRATION_STATUS_FAILED);
2359 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002360 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002361 break;
2362 }
Peter Xub15df1a2018-01-03 20:20:13 +08002363
Alex Blighbc72ad62013-08-21 16:03:08 +01002364 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002365
Peter Xub15df1a2018-01-03 20:20:13 +08002366 migration_update_counters(s, current_time);
Michael R. Hines7e114f82013-06-25 21:35:30 -04002367
zhanghailiang89a02a92016-01-15 11:37:42 +08002368 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002369 /* usleep expects microseconds */
Peter Xub15df1a2018-01-03 20:20:13 +08002370 g_usleep((s->iteration_start_time + BUFFER_DELAY -
2371 current_time) * 1000);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002372 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002373 }
2374
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002375 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08002376 migration_iteration_finish(s);
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002377 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002378 return NULL;
2379}
2380
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002381void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002382{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302383 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002384 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002385 if (error_in) {
2386 migrate_fd_error(s, error_in);
2387 migrate_fd_cleanup(s);
2388 return;
2389 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002390
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01002391 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08002392 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302393 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002394
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002395 /* Notify before starting migration thread */
2396 notifier_list_notify(&migration_state_notifiers, s);
2397
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002398 /*
Peter Xuc788ada2017-06-26 18:28:55 +08002399 * Open the return path. For postcopy, it is used exclusively. For
2400 * precopy, only if user specified "return-path" capability would
2401 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002402 */
Peter Xuc788ada2017-06-26 18:28:55 +08002403 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002404 if (open_return_path_on_source(s)) {
2405 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00002406 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002407 MIGRATION_STATUS_FAILED);
2408 migrate_fd_cleanup(s);
2409 return;
2410 }
2411 }
2412
Juan Quintelaf986c3d2016-01-14 16:52:55 +01002413 if (multifd_save_setup() != 0) {
2414 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2415 MIGRATION_STATUS_FAILED);
2416 migrate_fd_cleanup(s);
2417 return;
2418 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05302419 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002420 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002421 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002422}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002423
Peter Xu9d18af92017-06-27 12:10:19 +08002424void migration_global_dump(Monitor *mon)
2425{
2426 MigrationState *ms = migrate_get_current();
2427
Juan Quintela6f0f6422017-10-26 11:49:57 +02002428 monitor_printf(mon, "globals:\n");
2429 monitor_printf(mon, "store-global-state: %s\n",
2430 ms->store_global_state ? "on" : "off");
2431 monitor_printf(mon, "only-migratable: %s\n",
2432 ms->only_migratable ? "on" : "off");
2433 monitor_printf(mon, "send-configuration: %s\n",
2434 ms->send_configuration ? "on" : "off");
2435 monitor_printf(mon, "send-section-footer: %s\n",
2436 ms->send_section_footer ? "on" : "off");
Peter Xu9d18af92017-06-27 12:10:19 +08002437}
2438
Peter Xu20814752017-07-18 11:39:03 +08002439#define DEFINE_PROP_MIG_CAP(name, x) \
2440 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2441
Peter Xu52722982017-06-27 12:10:14 +08002442static Property migration_properties[] = {
2443 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2444 store_global_state, true),
Peter Xu3df663e2017-06-27 12:10:15 +08002445 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
Peter Xu71dd4c12017-06-27 12:10:16 +08002446 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2447 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08002448 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2449 send_section_footer, true),
Peter Xu89632fa2017-07-18 11:39:02 +08002450
2451 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01002452 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002453 parameters.compress_level,
2454 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01002455 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002456 parameters.compress_threads,
2457 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002458 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002459 parameters.decompress_threads,
2460 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002461 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002462 parameters.cpu_throttle_initial,
2463 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01002464 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002465 parameters.cpu_throttle_increment,
2466 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Juan Quintela741d4082017-12-01 13:08:38 +01002467 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002468 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01002469 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002470 parameters.downtime_limit,
2471 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01002472 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002473 parameters.x_checkpoint_delay,
2474 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintela741d4082017-12-01 13:08:38 +01002475 DEFINE_PROP_UINT8("x-multifd-channels", MigrationState,
Juan Quintela4075fb12016-01-15 08:56:17 +01002476 parameters.x_multifd_channels,
2477 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela741d4082017-12-01 13:08:38 +01002478 DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState,
Juan Quintela0fb86602017-04-27 10:48:25 +02002479 parameters.x_multifd_page_count,
2480 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
Juan Quintela73af8dd2017-10-05 21:30:10 +02002481 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
2482 parameters.xbzrle_cache_size,
2483 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Peter Xu20814752017-07-18 11:39:03 +08002484
2485 /* Migration capabilities */
2486 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2487 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2488 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2489 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2490 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2491 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2492 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2493 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2494 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2495 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2496 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintela30126bb2016-01-14 12:23:00 +01002497 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08002498
Peter Xu52722982017-06-27 12:10:14 +08002499 DEFINE_PROP_END_OF_LIST(),
2500};
2501
Peter Xue5cb7e72017-06-27 12:10:13 +08002502static void migration_class_init(ObjectClass *klass, void *data)
2503{
2504 DeviceClass *dc = DEVICE_CLASS(klass);
2505
2506 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08002507 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08002508}
2509
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002510static void migration_instance_finalize(Object *obj)
2511{
2512 MigrationState *ms = MIGRATION_OBJ(obj);
2513 MigrationParameters *params = &ms->parameters;
2514
Juan Quintela87db1a72017-09-05 12:50:22 +02002515 qemu_mutex_destroy(&ms->error_mutex);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002516 g_free(params->tls_hostname);
2517 g_free(params->tls_creds);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002518 qemu_sem_destroy(&ms->pause_sem);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002519}
2520
Peter Xue5cb7e72017-06-27 12:10:13 +08002521static void migration_instance_init(Object *obj)
2522{
2523 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002524 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08002525
2526 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08002527 ms->mbps = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002528 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02002529 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002530
2531 params->tls_hostname = g_strdup("");
2532 params->tls_creds = g_strdup("");
2533
2534 /* Set has_* up only for parameter checks */
2535 params->has_compress_level = true;
2536 params->has_compress_threads = true;
2537 params->has_decompress_threads = true;
2538 params->has_cpu_throttle_initial = true;
2539 params->has_cpu_throttle_increment = true;
2540 params->has_max_bandwidth = true;
2541 params->has_downtime_limit = true;
2542 params->has_x_checkpoint_delay = true;
2543 params->has_block_incremental = true;
Juan Quintela4075fb12016-01-15 08:56:17 +01002544 params->has_x_multifd_channels = true;
Juan Quintela0fb86602017-04-27 10:48:25 +02002545 params->has_x_multifd_page_count = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02002546 params->has_xbzrle_cache_size = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08002547}
2548
2549/*
2550 * Return true if check pass, false otherwise. Error will be put
2551 * inside errp if provided.
2552 */
2553static bool migration_object_check(MigrationState *ms, Error **errp)
2554{
Peter Xu6b19a7d2017-07-18 11:39:10 +08002555 MigrationCapabilityStatusList *head = NULL;
2556 /* Assuming all off */
2557 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2558 int i;
2559
Peter Xu8b0b29d2017-07-18 11:39:06 +08002560 if (!migrate_params_check(&ms->parameters, errp)) {
2561 return false;
2562 }
2563
Peter Xu6b19a7d2017-07-18 11:39:10 +08002564 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2565 if (ms->enabled_capabilities[i]) {
2566 head = migrate_cap_add(head, i, true);
2567 }
2568 }
2569
2570 ret = migrate_caps_check(cap_list, head, errp);
2571
2572 /* It works with head == NULL */
2573 qapi_free_MigrationCapabilityStatusList(head);
2574
2575 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08002576}
2577
2578static const TypeInfo migration_type = {
2579 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08002580 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08002581 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2582 * not created using qdev_create(), it is not attached to the qdev
2583 * device tree, and it is never realized.
2584 *
2585 * TODO: Make this TYPE_OBJECT once QOM provides something like
2586 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08002587 */
Peter Xue5cb7e72017-06-27 12:10:13 +08002588 .parent = TYPE_DEVICE,
2589 .class_init = migration_class_init,
2590 .class_size = sizeof(MigrationClass),
2591 .instance_size = sizeof(MigrationState),
2592 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002593 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08002594};
2595
2596static void register_migration_types(void)
2597{
2598 type_register_static(&migration_type);
2599}
2600
2601type_init(register_migration_types);