blob: ad29e534007759bb564547f5e6c8e43b81259607 [file] [log] [blame]
aliguori5bb79102008-10-13 03:12:02 +00001/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori5bb79102008-10-13 03:12:02 +000014 */
15
Peter Maydell1393a482016-01-26 18:16:54 +000016#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010019#include "qemu/main-loop.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020020#include "migration/blocker.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010021#include "migration/migration.h"
Juan Quintela40014d82017-04-17 19:34:36 +020022#include "qemu-file-channel.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020023#include "migration/qemu-file.h"
Juan Quintela987772d2017-04-17 19:02:59 +020024#include "migration/vmstate.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010026#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010027#include "qapi/qmp/qerror.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000028#include "qapi/util.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/sockets.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020030#include "qemu/rcu.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010031#include "migration/block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020032#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020033#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030034#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010035#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020036#include "qapi-event.h"
Jason J. Herne070afca2015-09-08 13:12:35 -040037#include "qom/cpu.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000038#include "exec/memory.h"
39#include "exec/address-spaces.h"
Juan Quintela51180422017-04-24 20:50:19 +020040#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010041#include "io/channel-buffer.h"
Daniel P. Berrangee1226362016-04-27 11:05:16 +010042#include "io/channel-tls.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080043#include "migration/colo.h"
aliguori065e2812008-11-11 16:46:33 +000044
Jason J. Hernedc325622015-09-08 13:12:37 -040045#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000046
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010047/* Amount of time to allocate to each "chunk" of bandwidth-throttled
48 * data. */
49#define BUFFER_DELAY 100
50#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
51
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053052/* Time in milliseconds we are allowed to stop the source,
53 * for sending the last part */
54#define DEFAULT_MIGRATE_SET_DOWNTIME 300
55
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030056/* Maximum migrate downtime set to 2000 seconds */
57#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
58#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
59
Liang Li8706d2d2015-03-23 16:32:17 +080060/* Default compression thread count */
61#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080062/* Default decompression thread count, usually decompression is at
63 * least 4 times as fast as compression.*/
64#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080065/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
66#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040067/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040068#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
69#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080070
Orit Wasserman17ad9b32012-08-06 21:42:53 +030071/* Migration XBZRLE default cache size */
72#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
73
zhanghailiang68b53592016-10-27 14:43:01 +080074/* The delay time (in ms) between two COLO checkpoints
75 * Note: Please change this default value to 10000 when we support hybrid mode.
76 */
77#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
78
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010079static NotifierList migration_state_notifiers =
80 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
81
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000082static bool deferred_incoming;
83
Juan Quintela17549e82011-10-05 13:50:43 +020084/* When we add fault tolerance, we could have several
85 migrations at once. For now we don't need to add
86 dynamic creation of migration */
87
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010088/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020089MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020090{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000091 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +020092 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080093 .state = MIGRATION_STATUS_NONE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030094 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040095 .mbps = -1,
Daniel P. Berrange2594f562016-04-27 11:05:14 +010096 .parameters = {
97 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
98 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
99 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
100 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
101 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530102 .max_bandwidth = MAX_THROTTLE,
103 .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME,
zhanghailiang68b53592016-10-27 14:43:01 +0800104 .x_checkpoint_delay = DEFAULT_MIGRATE_X_CHECKPOINT_DELAY,
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100105 },
Juan Quintela17549e82011-10-05 13:50:43 +0200106 };
107
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000108 if (!once) {
Daniel P. Berrange4af245d2017-03-15 16:16:03 +0000109 current_migration.parameters.tls_creds = g_strdup("");
110 current_migration.parameters.tls_hostname = g_strdup("");
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000111 once = true;
112 }
Juan Quintela17549e82011-10-05 13:50:43 +0200113 return &current_migration;
114}
115
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100116MigrationIncomingState *migration_incoming_get_current(void)
117{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100118 static bool once;
119 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100120
Juan Quintelab4b076d2017-01-23 22:32:06 +0100121 if (!once) {
122 mis_current.state = MIGRATION_STATUS_NONE;
123 memset(&mis_current, 0, sizeof(MigrationIncomingState));
124 QLIST_INIT(&mis_current.loadvm_handlers);
125 qemu_mutex_init(&mis_current.rp_mutex);
126 qemu_event_init(&mis_current.main_thread_load_event, false);
127 once = true;
128 }
129 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100130}
131
132void migration_incoming_state_destroy(void)
133{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100134 struct MigrationIncomingState *mis = migration_incoming_get_current();
135
136 qemu_event_destroy(&mis->main_thread_load_event);
137 loadvm_free_handlers(mis);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100138}
139
Juan Quinteladf4b1022014-10-08 10:58:10 +0200140
141typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200142 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200143 uint32_t size;
144 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200145 RunState state;
146 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200147} GlobalState;
148
149static GlobalState global_state;
150
Juan Quintela560d0272015-07-15 09:53:46 +0200151int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200152{
153 if (!runstate_store((char *)global_state.runstate,
154 sizeof(global_state.runstate))) {
155 error_report("runstate name too big: %s", global_state.runstate);
156 trace_migrate_state_too_big();
157 return -EINVAL;
158 }
159 return 0;
160}
161
Anthony PERARDc69adea2015-08-03 15:29:19 +0100162void global_state_store_running(void)
163{
164 const char *state = RunState_lookup[RUN_STATE_RUNNING];
165 strncpy((char *)global_state.runstate,
166 state, sizeof(global_state.runstate));
167}
168
Juan Quintela172c4352015-07-08 13:56:26 +0200169static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200170{
Juan Quintela172c4352015-07-08 13:56:26 +0200171 return global_state.received;
172}
173
174static RunState global_state_get_runstate(void)
175{
176 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200177}
178
Juan Quintela13d16812014-10-08 13:58:24 +0200179void global_state_set_optional(void)
180{
181 global_state.optional = true;
182}
183
184static bool global_state_needed(void *opaque)
185{
186 GlobalState *s = opaque;
187 char *runstate = (char *)s->runstate;
188
189 /* If it is not optional, it is mandatory */
190
191 if (s->optional == false) {
192 return true;
193 }
194
195 /* If state is running or paused, it is not needed */
196
197 if (strcmp(runstate, "running") == 0 ||
198 strcmp(runstate, "paused") == 0) {
199 return false;
200 }
201
202 /* for any other state it is needed */
203 return true;
204}
205
Juan Quinteladf4b1022014-10-08 10:58:10 +0200206static int global_state_post_load(void *opaque, int version_id)
207{
208 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200209 Error *local_err = NULL;
210 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200211 char *runstate = (char *)s->runstate;
212
Juan Quintela172c4352015-07-08 13:56:26 +0200213 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200214 trace_migrate_global_state_post_load(runstate);
215
Eric Blake7fb1cf12015-11-18 01:52:57 -0700216 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200217 -1, &local_err);
218
Juan Quintela172c4352015-07-08 13:56:26 +0200219 if (r == -1) {
220 if (local_err) {
221 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200222 }
Juan Quintela172c4352015-07-08 13:56:26 +0200223 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200224 }
Juan Quintela172c4352015-07-08 13:56:26 +0200225 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200226
Juan Quintela172c4352015-07-08 13:56:26 +0200227 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200228}
229
230static void global_state_pre_save(void *opaque)
231{
232 GlobalState *s = opaque;
233
234 trace_migrate_global_state_pre_save((char *)s->runstate);
235 s->size = strlen((char *)s->runstate) + 1;
236}
237
238static const VMStateDescription vmstate_globalstate = {
239 .name = "globalstate",
240 .version_id = 1,
241 .minimum_version_id = 1,
242 .post_load = global_state_post_load,
243 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200244 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200245 .fields = (VMStateField[]) {
246 VMSTATE_UINT32(size, GlobalState),
247 VMSTATE_BUFFER(runstate, GlobalState),
248 VMSTATE_END_OF_LIST()
249 },
250};
251
252void register_global_state(void)
253{
254 /* We would use it independently that we receive it */
255 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200256 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200257 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
258}
259
Juan Quintelab05dc722015-07-07 14:44:05 +0200260static void migrate_generate_event(int new_state)
261{
262 if (migrate_use_events()) {
263 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200264 }
265}
266
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000267/*
268 * Called on -incoming with a defer: uri.
269 * The migration can be started later after any parameters have been
270 * changed.
271 */
272static void deferred_incoming_migration(Error **errp)
273{
274 if (deferred_incoming) {
275 error_setg(errp, "Incoming migration already deferred");
276 }
277 deferred_incoming = true;
278}
279
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000280/* Request a range of pages from the source VM at the given
281 * start address.
282 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
283 * as the last request (a name must have been given previously)
284 * Start: Address offset within the RB
285 * Len: Length in bytes required - must be a multiple of pagesize
286 */
287void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
288 ram_addr_t start, size_t len)
289{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100290 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000291 size_t msglen = 12; /* start + len */
292
293 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
294 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
295
296 if (rbname) {
297 int rbname_len = strlen(rbname);
298 assert(rbname_len < 256);
299
300 bufc[msglen++] = rbname_len;
301 memcpy(bufc + msglen, rbname, rbname_len);
302 msglen += rbname_len;
303 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
304 } else {
305 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
306 }
307}
308
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200309void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000310{
aliguori34c9dd82008-10-13 03:14:31 +0000311 const char *p;
312
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200313 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000314 if (!strcmp(uri, "defer")) {
315 deferred_incoming_migration(errp);
316 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200317 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400318#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000319 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400320 rdma_start_incoming_migration(p, errp);
321#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000322 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200323 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000324 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200325 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000326 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200327 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000328 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100329 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200330 }
aliguori5bb79102008-10-13 03:12:02 +0000331}
332
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300333static void process_incoming_migration_bh(void *opaque)
334{
335 Error *local_err = NULL;
336 MigrationIncomingState *mis = opaque;
337
Kevin Wolface21a52017-05-04 18:52:36 +0200338 /* Make sure all file formats flush their mutable metadata.
339 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300340 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200341 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200342 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200343 local_err = NULL;
344 autostart = false;
345 }
346
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300347 /*
348 * This must happen after all error conditions are dealt with and
349 * we're sure the VM is going to be running on this host.
350 */
351 qemu_announce_self();
352
353 /* If global state section was not received or we are in running
354 state, we need to obey autostart. Any other state is set with
355 runstate_set. */
356
357 if (!global_state_received() ||
358 global_state_get_runstate() == RUN_STATE_RUNNING) {
359 if (autostart) {
360 vm_start();
361 } else {
362 runstate_set(RUN_STATE_PAUSED);
363 }
364 } else {
365 runstate_set(global_state_get_runstate());
366 }
367 migrate_decompress_threads_join();
368 /*
369 * This must happen after any state changes since as soon as an external
370 * observer sees this event they might start to prod at the VM assuming
371 * it's ready to use.
372 */
373 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
374 MIGRATION_STATUS_COMPLETED);
375 qemu_bh_delete(mis->bh);
376 migration_incoming_state_destroy();
377}
378
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200379static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200380{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200381 QEMUFile *f = opaque;
Juan Quintelab4b076d2017-01-23 22:32:06 +0100382 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000383 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200384 int ret;
385
Juan Quintelab4b076d2017-01-23 22:32:06 +0100386 mis->from_src_file = f;
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000387 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000388 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000389 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
390 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200391 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100392
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000393 ps = postcopy_state_get();
394 trace_process_incoming_migration_co_end(ret, ps);
395 if (ps != POSTCOPY_INCOMING_NONE) {
396 if (ps == POSTCOPY_INCOMING_ADVISE) {
397 /*
398 * Where a migration had postcopy enabled (and thus went to advise)
399 * but managed to complete within the precopy period, we can use
400 * the normal exit.
401 */
402 postcopy_ram_incoming_cleanup(mis);
403 } else if (ret >= 0) {
404 /*
405 * Postcopy was started, cleanup should happen at the end of the
406 * postcopy thread.
407 */
408 trace_process_incoming_migration_co_postcopy_end_main();
409 return;
410 }
411 /* Else if something went wrong then just fall out of the normal exit */
412 }
413
zhanghailiang25d0c162016-10-27 14:42:55 +0800414 /* we get COLO info, and know if we are in COLO mode */
415 if (!ret && migration_incoming_enable_colo()) {
416 mis->migration_incoming_co = qemu_coroutine_self();
417 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
418 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
419 mis->have_colo_incoming_thread = true;
420 qemu_coroutine_yield();
421
422 /* Wait checkpoint incoming thread exit before free resource */
423 qemu_thread_join(&mis->colo_incoming_thread);
424 }
425
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200426 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000427 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
428 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200429 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800430 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600431 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200432 }
Juan Quintela511c0232010-06-09 14:10:55 +0200433
Laurent Viviere8199e42017-04-12 15:53:11 +0200434 qemu_fclose(f);
435 free_xbzrle_decoded_buf();
436
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300437 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
438 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200439}
440
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100441void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200442{
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200443 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200444
Liang Li3fcb38c2015-03-23 16:32:18 +0800445 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100446 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200447 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200448}
449
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000450/*
451 * Send a message on the return channel back to the source
452 * of the migration.
453 */
454void migrate_send_rp_message(MigrationIncomingState *mis,
455 enum mig_rp_message_type message_type,
456 uint16_t len, void *data)
457{
458 trace_migrate_send_rp_message((int)message_type, len);
459 qemu_mutex_lock(&mis->rp_mutex);
460 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
461 qemu_put_be16(mis->to_src_file, len);
462 qemu_put_buffer(mis->to_src_file, data, len);
463 qemu_fflush(mis->to_src_file);
464 qemu_mutex_unlock(&mis->rp_mutex);
465}
466
467/*
468 * Send a 'SHUT' message on the return channel with the given value
469 * to indicate that we've finished with the RP. Non-0 value indicates
470 * error.
471 */
472void migrate_send_rp_shut(MigrationIncomingState *mis,
473 uint32_t value)
474{
475 uint32_t buf;
476
477 buf = cpu_to_be32(value);
478 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
479}
480
481/*
482 * Send a 'PONG' message on the return channel with the given value
483 * (normally in response to a 'PING')
484 */
485void migrate_send_rp_pong(MigrationIncomingState *mis,
486 uint32_t value)
487{
488 uint32_t buf;
489
490 buf = cpu_to_be32(value);
491 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
492}
493
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300494MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
495{
496 MigrationCapabilityStatusList *head = NULL;
497 MigrationCapabilityStatusList *caps;
498 MigrationState *s = migrate_get_current();
499 int i;
500
Michael Tokarev387eede2013-10-05 13:18:28 +0400501 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700502 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100503#ifndef CONFIG_LIVE_BLOCK_MIGRATION
504 if (i == MIGRATION_CAPABILITY_BLOCK) {
505 continue;
506 }
507#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800508 if (i == MIGRATION_CAPABILITY_X_COLO && !colo_supported()) {
509 continue;
510 }
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300511 if (head == NULL) {
512 head = g_malloc0(sizeof(*caps));
513 caps = head;
514 } else {
515 caps->next = g_malloc0(sizeof(*caps));
516 caps = caps->next;
517 }
518 caps->value =
519 g_malloc(sizeof(*caps->value));
520 caps->value->capability = i;
521 caps->value->state = s->enabled_capabilities[i];
522 }
523
524 return head;
525}
526
Liang Li85de8322015-03-23 16:32:28 +0800527MigrationParameters *qmp_query_migrate_parameters(Error **errp)
528{
529 MigrationParameters *params;
530 MigrationState *s = migrate_get_current();
531
532 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500533 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100534 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500535 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100536 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500537 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100538 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500539 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100540 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500541 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100542 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Eric Blakede63ab62016-09-08 22:14:15 -0500543 params->has_tls_creds = !!s->parameters.tls_creds;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100544 params->tls_creds = g_strdup(s->parameters.tls_creds);
Eric Blakede63ab62016-09-08 22:14:15 -0500545 params->has_tls_hostname = !!s->parameters.tls_hostname;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100546 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530547 params->has_max_bandwidth = true;
548 params->max_bandwidth = s->parameters.max_bandwidth;
549 params->has_downtime_limit = true;
550 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800551 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800552 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200553 params->has_block_incremental = true;
554 params->block_incremental = s->parameters.block_incremental;
Liang Li85de8322015-03-23 16:32:28 +0800555
556 return params;
557}
558
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000559/*
560 * Return true if we're already in the middle of a migration
561 * (i.e. any of the active or setup states)
562 */
563static bool migration_is_setup_or_active(int state)
564{
565 switch (state) {
566 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000567 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000568 case MIGRATION_STATUS_SETUP:
569 return true;
570
571 default:
572 return false;
573
574 }
575}
576
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300577static void get_xbzrle_cache_stats(MigrationInfo *info)
578{
579 if (migrate_use_xbzrle()) {
580 info->has_xbzrle_cache = true;
581 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
582 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
583 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
584 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
585 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800586 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300587 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
588 }
589}
590
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100591static void populate_ram_info(MigrationInfo *info, MigrationState *s)
592{
593 info->has_ram = true;
594 info->ram = g_malloc0(sizeof(*info->ram));
595 info->ram->transferred = ram_bytes_transferred();
596 info->ram->total = ram_bytes_total();
597 info->ram->duplicate = dup_mig_pages_transferred();
Juan Quintelabedf53c2017-03-13 20:35:54 +0100598 /* legacy value. It is not used anymore */
599 info->ram->skipped = 0;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100600 info->ram->normal = norm_mig_pages_transferred();
Juan Quintela29cc3d82017-03-13 20:43:34 +0100601 info->ram->normal_bytes = norm_mig_pages_transferred() *
Juan Quintela20afaed2017-03-21 09:09:14 +0100602 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100603 info->ram->mbps = s->mbps;
Juan Quintela42d219d2017-03-14 18:01:38 +0100604 info->ram->dirty_sync_count = ram_dirty_sync_count();
Juan Quintela96506892017-03-14 18:41:03 +0100605 info->ram->postcopy_requests = ram_postcopy_requests();
Chao Fan030ce1f2017-03-21 10:22:43 +0800606 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100607
608 if (s->state != MIGRATION_STATUS_COMPLETED) {
609 info->ram->remaining = ram_bytes_remaining();
Juan Quintela47ad8612017-03-14 18:20:30 +0100610 info->ram->dirty_pages_rate = ram_dirty_pages_rate();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100611 }
612}
613
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300614MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000615{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300616 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200617 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000618
Juan Quintela17549e82011-10-05 13:50:43 +0200619 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800620 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200621 /* no migration has happened ever */
622 break;
zhanghailiang31194732015-03-13 16:08:38 +0800623 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400624 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400625 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400626 break;
zhanghailiang31194732015-03-13 16:08:38 +0800627 case MIGRATION_STATUS_ACTIVE:
628 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300629 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200630 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100631 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200632 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200633 info->has_expected_downtime = true;
634 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400635 info->has_setup_time = true;
636 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200637
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100638 populate_ram_info(info, s);
Juan Quintela8d017192012-08-13 12:31:25 +0200639
Juan Quintela17549e82011-10-05 13:50:43 +0200640 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300641 info->has_disk = true;
642 info->disk = g_malloc0(sizeof(*info->disk));
643 info->disk->transferred = blk_mig_bytes_transferred();
644 info->disk->remaining = blk_mig_bytes_remaining();
645 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000646 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300647
Jason J. Herne47828932015-09-08 13:12:36 -0400648 if (cpu_throttle_active()) {
Jason J. Herned85a31d2016-04-21 14:07:18 -0400649 info->has_cpu_throttle_percentage = true;
650 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
Jason J. Herne47828932015-09-08 13:12:36 -0400651 }
652
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300653 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200654 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000655 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
656 /* Mostly the same as active; TODO add some postcopy stats */
657 info->has_status = true;
658 info->has_total_time = true;
659 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
660 - s->total_time;
661 info->has_expected_downtime = true;
662 info->expected_downtime = s->expected_downtime;
663 info->has_setup_time = true;
664 info->setup_time = s->setup_time;
665
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100666 populate_ram_info(info, s);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000667
668 if (blk_mig_active()) {
669 info->has_disk = true;
670 info->disk = g_malloc0(sizeof(*info->disk));
671 info->disk->transferred = blk_mig_bytes_transferred();
672 info->disk->remaining = blk_mig_bytes_remaining();
673 info->disk->total = blk_mig_bytes_total();
674 }
675
676 get_xbzrle_cache_stats(info);
677 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800678 case MIGRATION_STATUS_COLO:
679 info->has_status = true;
680 /* TODO: display COLO specific information (checkpoint info etc.) */
681 break;
zhanghailiang31194732015-03-13 16:08:38 +0800682 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300683 get_xbzrle_cache_stats(info);
684
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300685 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900686 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200687 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200688 info->has_downtime = true;
689 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400690 info->has_setup_time = true;
691 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200692
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100693 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200694 break;
zhanghailiang31194732015-03-13 16:08:38 +0800695 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300696 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100697 if (s->error) {
698 info->has_error_desc = true;
699 info->error_desc = g_strdup(error_get_pretty(s->error));
700 }
Juan Quintela17549e82011-10-05 13:50:43 +0200701 break;
zhanghailiang31194732015-03-13 16:08:38 +0800702 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300703 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200704 break;
aliguori5bb79102008-10-13 03:12:02 +0000705 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800706 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300707
708 return info;
aliguori5bb79102008-10-13 03:12:02 +0000709}
710
Orit Wasserman00458432012-08-06 21:42:48 +0300711void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
712 Error **errp)
713{
714 MigrationState *s = migrate_get_current();
715 MigrationCapabilityStatusList *cap;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100716 bool old_postcopy_cap = migrate_postcopy_ram();
Orit Wasserman00458432012-08-06 21:42:48 +0300717
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000718 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100719 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300720 return;
721 }
722
723 for (cap = params; cap; cap = cap->next) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100724#ifndef CONFIG_LIVE_BLOCK_MIGRATION
725 if (cap->value->capability == MIGRATION_CAPABILITY_BLOCK
726 && cap->value->state) {
727 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
728 "block migration");
729 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
730 continue;
731 }
732#endif
zhanghailiang35a6ed42016-10-27 14:42:52 +0800733 if (cap->value->capability == MIGRATION_CAPABILITY_X_COLO) {
734 if (!colo_supported()) {
735 error_setg(errp, "COLO is not currently supported, please"
736 " configure with --enable-colo option in order to"
737 " support COLO feature");
738 continue;
739 }
740 }
Orit Wasserman00458432012-08-06 21:42:48 +0300741 s->enabled_capabilities[cap->value->capability] = cap->value->state;
742 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000743
744 if (migrate_postcopy_ram()) {
745 if (migrate_use_compression()) {
746 /* The decompression threads asynchronously write into RAM
747 * rather than use the atomic copies needed to avoid
748 * userfaulting. It should be possible to fix the decompression
749 * threads for compatibility in future.
750 */
751 error_report("Postcopy is not currently compatible with "
752 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000753 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000754 false;
755 }
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100756 /* This check is reasonably expensive, so only when it's being
757 * set the first time, also it's only the destination that needs
758 * special support.
759 */
760 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
761 !postcopy_ram_supported_by_host()) {
762 /* postcopy_ram_supported_by_host will have emitted a more
763 * detailed message
764 */
765 error_report("Postcopy is not supported");
766 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
767 false;
768 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000769 }
Orit Wasserman00458432012-08-06 21:42:48 +0300770}
771
Eric Blake7f375e02016-09-08 22:14:16 -0500772void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800773{
774 MigrationState *s = migrate_get_current();
775
Eric Blake7f375e02016-09-08 22:14:16 -0500776 if (params->has_compress_level &&
777 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100778 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
779 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800780 return;
781 }
Eric Blake7f375e02016-09-08 22:14:16 -0500782 if (params->has_compress_threads &&
783 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100784 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
785 "compress_threads",
786 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800787 return;
788 }
Eric Blake7f375e02016-09-08 22:14:16 -0500789 if (params->has_decompress_threads &&
790 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100791 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
792 "decompress_threads",
793 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800794 return;
795 }
Eric Blake7f375e02016-09-08 22:14:16 -0500796 if (params->has_cpu_throttle_initial &&
797 (params->cpu_throttle_initial < 1 ||
798 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400799 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400800 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400801 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530802 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400803 }
Eric Blake7f375e02016-09-08 22:14:16 -0500804 if (params->has_cpu_throttle_increment &&
805 (params->cpu_throttle_increment < 1 ||
806 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400807 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400808 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400809 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530810 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400811 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530812 if (params->has_max_bandwidth &&
813 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
814 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
815 " range of 0 to %zu bytes/second", SIZE_MAX);
816 return;
817 }
818 if (params->has_downtime_limit &&
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300819 (params->downtime_limit < 0 ||
820 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
821 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
822 "the range of 0 to %d milliseconds",
823 MAX_MIGRATE_DOWNTIME);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530824 return;
825 }
zhanghailiang68b53592016-10-27 14:43:01 +0800826 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
827 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
828 "x_checkpoint_delay",
829 "is invalid, it should be positive");
830 }
Liang Li85de8322015-03-23 16:32:28 +0800831
Eric Blake7f375e02016-09-08 22:14:16 -0500832 if (params->has_compress_level) {
833 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800834 }
Eric Blake7f375e02016-09-08 22:14:16 -0500835 if (params->has_compress_threads) {
836 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800837 }
Eric Blake7f375e02016-09-08 22:14:16 -0500838 if (params->has_decompress_threads) {
839 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800840 }
Eric Blake7f375e02016-09-08 22:14:16 -0500841 if (params->has_cpu_throttle_initial) {
842 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400843 }
Eric Blake7f375e02016-09-08 22:14:16 -0500844 if (params->has_cpu_throttle_increment) {
845 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400846 }
Eric Blake7f375e02016-09-08 22:14:16 -0500847 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100848 g_free(s->parameters.tls_creds);
Eric Blake7f375e02016-09-08 22:14:16 -0500849 s->parameters.tls_creds = g_strdup(params->tls_creds);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100850 }
Eric Blake7f375e02016-09-08 22:14:16 -0500851 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100852 g_free(s->parameters.tls_hostname);
Eric Blake7f375e02016-09-08 22:14:16 -0500853 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100854 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530855 if (params->has_max_bandwidth) {
856 s->parameters.max_bandwidth = params->max_bandwidth;
857 if (s->to_dst_file) {
858 qemu_file_set_rate_limit(s->to_dst_file,
859 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
860 }
861 }
862 if (params->has_downtime_limit) {
863 s->parameters.downtime_limit = params->downtime_limit;
864 }
zhanghailiang68b53592016-10-27 14:43:01 +0800865
866 if (params->has_x_checkpoint_delay) {
867 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800868 if (migration_in_colo_state()) {
869 colo_checkpoint_notify(s);
870 }
zhanghailiang68b53592016-10-27 14:43:01 +0800871 }
Juan Quintela2833c592017-04-05 18:32:37 +0200872 if (params->has_block_incremental) {
873 s->parameters.block_incremental = params->block_incremental;
874 }
Liang Li85de8322015-03-23 16:32:28 +0800875}
876
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100877
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000878void qmp_migrate_start_postcopy(Error **errp)
879{
880 MigrationState *s = migrate_get_current();
881
882 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000883 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000884 " the start of migration");
885 return;
886 }
887
888 if (s->state == MIGRATION_STATUS_NONE) {
889 error_setg(errp, "Postcopy must be started after migration has been"
890 " started");
891 return;
892 }
893 /*
894 * we don't error if migration has finished since that would be racy
895 * with issuing this command.
896 */
897 atomic_set(&s->start_postcopy, true);
898}
899
aliguori065e2812008-11-11 16:46:33 +0000900/* shared migration helpers */
901
zhanghailiang48781e52015-12-16 11:47:33 +0000902void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000903{
zhanghailiang48781e52015-12-16 11:47:33 +0000904 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200905 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200906 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000907 }
908}
909
Juan Quintela2833c592017-04-05 18:32:37 +0200910void migrate_set_block_enabled(bool value, Error **errp)
911{
912 MigrationCapabilityStatusList *cap;
913
914 cap = g_new0(MigrationCapabilityStatusList, 1);
915 cap->value = g_new0(MigrationCapabilityStatus, 1);
916 cap->value->capability = MIGRATION_CAPABILITY_BLOCK;
917 cap->value->state = value;
918 qmp_migrate_set_capabilities(cap, errp);
919 qapi_free_MigrationCapabilityStatusList(cap);
920}
921
922static void migrate_set_block_incremental(MigrationState *s, bool value)
923{
924 s->parameters.block_incremental = value;
925}
926
927static void block_cleanup_parameters(MigrationState *s)
928{
929 if (s->must_remove_block_options) {
930 /* setting to false can never fail */
931 migrate_set_block_enabled(false, &error_abort);
932 migrate_set_block_incremental(s, false);
933 s->must_remove_block_options = false;
934 }
935}
936
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100937static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000938{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100939 MigrationState *s = opaque;
940
941 qemu_bh_delete(s->cleanup_bh);
942 s->cleanup_bh = NULL;
943
Juan Quintelaec481c62017-03-20 22:12:40 +0100944 migration_page_queue_free();
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000945
zhanghailiang89a02a92016-01-15 11:37:42 +0800946 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100947 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100948 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000949 if (s->migration_thread_running) {
950 qemu_thread_join(&s->thread);
951 s->migration_thread_running = false;
952 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100953 qemu_mutex_lock_iothread();
954
Liang Li8706d2d2015-03-23 16:32:17 +0800955 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800956 qemu_fclose(s->to_dst_file);
957 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000958 }
959
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000960 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
961 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100962
Liang Li94f5a432015-11-02 15:37:00 +0800963 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000964 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800965 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100966 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100967
968 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200969 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +0000970}
971
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100972void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000973{
Peter Maydell25174052016-10-21 18:41:45 +0100974 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +0800975 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000976 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
977 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100978 if (!s->error) {
979 s->error = error_copy(error);
980 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100981 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +0200982 block_cleanup_parameters(s);
Juan Quintela458cf282011-02-22 23:32:54 +0100983}
984
Juan Quintela0edda1c2010-05-11 16:28:39 +0200985static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000986{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000987 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800988 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100989 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000990
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000991 if (s->rp_state.from_dst_file) {
992 /* shutdown the rp socket, so causing the rp thread to shutdown */
993 qemu_file_shutdown(s->rp_state.from_dst_file);
994 }
995
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000996 do {
997 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000998 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000999 break;
1000 }
zhanghailiang48781e52015-12-16 11:47:33 +00001001 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001002 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001003
1004 /*
1005 * If we're unlucky the migration code might be stuck somewhere in a
1006 * send/write while the network has failed and is waiting to timeout;
1007 * if we've got shutdown(2) available then we can force it to quit.
1008 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1009 * called in a bh, so there is no race against this cancel.
1010 */
zhanghailiang31194732015-03-13 16:08:38 +08001011 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001012 qemu_file_shutdown(f);
1013 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001014 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1015 Error *local_err = NULL;
1016
1017 bdrv_invalidate_cache_all(&local_err);
1018 if (local_err) {
1019 error_report_err(local_err);
1020 } else {
1021 s->block_inactive = false;
1022 }
1023 }
Juan Quintela2833c592017-04-05 18:32:37 +02001024 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001025}
1026
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001027void add_migration_state_change_notifier(Notifier *notify)
1028{
1029 notifier_list_add(&migration_state_notifiers, notify);
1030}
1031
1032void remove_migration_state_change_notifier(Notifier *notify)
1033{
Paolo Bonzini31552522012-01-13 17:34:01 +01001034 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001035}
1036
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001037bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001038{
zhanghailiang31194732015-03-13 16:08:38 +08001039 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001040}
1041
Juan Quintela70736932011-02-23 00:43:59 +01001042bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001043{
zhanghailiang31194732015-03-13 16:08:38 +08001044 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001045}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001046
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001047bool migration_has_failed(MigrationState *s)
1048{
zhanghailiang31194732015-03-13 16:08:38 +08001049 return (s->state == MIGRATION_STATUS_CANCELLED ||
1050 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001051}
1052
Juan Quintela57273092017-03-20 22:25:28 +01001053bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001054{
Juan Quintela57273092017-03-20 22:25:28 +01001055 MigrationState *s = migrate_get_current();
1056
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001057 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1058}
1059
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001060bool migration_in_postcopy_after_devices(MigrationState *s)
1061{
Juan Quintela57273092017-03-20 22:25:28 +01001062 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001063}
1064
Juan Quintelafab35002017-03-22 17:36:57 +01001065bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301066{
Juan Quintelafab35002017-03-22 17:36:57 +01001067 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301068
1069 switch (s->state) {
1070 case MIGRATION_STATUS_NONE:
1071 case MIGRATION_STATUS_CANCELLED:
1072 case MIGRATION_STATUS_COMPLETED:
1073 case MIGRATION_STATUS_FAILED:
1074 return true;
1075 case MIGRATION_STATUS_SETUP:
1076 case MIGRATION_STATUS_CANCELLING:
1077 case MIGRATION_STATUS_ACTIVE:
1078 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1079 case MIGRATION_STATUS_COLO:
1080 return false;
1081 case MIGRATION_STATUS__MAX:
1082 g_assert_not_reached();
1083 }
1084
1085 return false;
1086}
1087
Juan Quintelaa0762d92017-04-05 21:00:09 +02001088MigrationState *migrate_init(void)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001089{
Juan Quintela17549e82011-10-05 13:50:43 +02001090 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +03001091
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001092 /*
1093 * Reinitialise all migration state, except
1094 * parameters/capabilities that the user set, and
1095 * locks.
1096 */
1097 s->bytes_xfer = 0;
1098 s->xfer_limit = 0;
1099 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001100 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001101 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001102 s->rp_state.from_dst_file = NULL;
1103 s->rp_state.error = false;
1104 s->mbps = 0.0;
1105 s->downtime = 0;
1106 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001107 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001108 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001109 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001110 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001111 error_free(s->error);
1112 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001113
zhanghailiang48781e52015-12-16 11:47:33 +00001114 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001115
Alex Blighbc72ad62013-08-21 16:03:08 +01001116 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001117 return s;
1118}
Juan Quintelacab30142011-02-22 23:54:21 +01001119
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001120static GSList *migration_blockers;
1121
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301122int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001123{
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301124 if (only_migratable) {
1125 error_propagate(errp, error_copy(reason));
1126 error_prepend(errp, "disallowing migration blocker "
1127 "(--only_migratable) for: ");
1128 return -EACCES;
1129 }
1130
Juan Quintelafab35002017-03-22 17:36:57 +01001131 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301132 migration_blockers = g_slist_prepend(migration_blockers, reason);
1133 return 0;
1134 }
1135
1136 error_propagate(errp, error_copy(reason));
1137 error_prepend(errp, "disallowing migration blocker (migration in "
1138 "progress) for: ");
1139 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001140}
1141
1142void migrate_del_blocker(Error *reason)
1143{
1144 migration_blockers = g_slist_remove(migration_blockers, reason);
1145}
1146
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001147void qmp_migrate_incoming(const char *uri, Error **errp)
1148{
1149 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001150 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001151
1152 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001153 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001154 return;
1155 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001156 if (!once) {
1157 error_setg(errp, "The incoming migration has already been started");
1158 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001159
1160 qemu_start_incoming_migration(uri, &local_err);
1161
1162 if (local_err) {
1163 error_propagate(errp, local_err);
1164 return;
1165 }
1166
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001167 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001168}
1169
Greg Kurz24f39022016-05-04 21:44:19 +02001170bool migration_is_blocked(Error **errp)
1171{
1172 if (qemu_savevm_state_blocked(errp)) {
1173 return true;
1174 }
1175
1176 if (migration_blockers) {
1177 *errp = error_copy(migration_blockers->data);
1178 return true;
1179 }
1180
1181 return false;
1182}
1183
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001184void qmp_migrate(const char *uri, bool has_blk, bool blk,
1185 bool has_inc, bool inc, bool has_detach, bool detach,
1186 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001187{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001188 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001189 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001190 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001191
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001192 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001193 s->state == MIGRATION_STATUS_CANCELLING ||
1194 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001195 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001196 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001197 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001198 if (runstate_check(RUN_STATE_INMIGRATE)) {
1199 error_setg(errp, "Guest is waiting for an incoming migration");
1200 return;
1201 }
1202
Greg Kurz24f39022016-05-04 21:44:19 +02001203 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001204 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001205 }
1206
Juan Quintela2833c592017-04-05 18:32:37 +02001207 if ((has_blk && blk) || (has_inc && inc)) {
1208 if (migrate_use_block() || migrate_use_block_incremental()) {
1209 error_setg(errp, "Command options are incompatible with "
1210 "current migration capabilities");
1211 return;
1212 }
1213 migrate_set_block_enabled(true, &local_err);
1214 if (local_err) {
1215 error_propagate(errp, local_err);
1216 return;
1217 }
1218 s->must_remove_block_options = true;
1219 }
1220
1221 if (has_inc && inc) {
1222 migrate_set_block_incremental(s, true);
1223 }
1224
Juan Quintelaa0762d92017-04-05 21:00:09 +02001225 s = migrate_init();
Juan Quintelacab30142011-02-22 23:54:21 +01001226
1227 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001228 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001229#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001230 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001231 rdma_start_outgoing_migration(s, p, &local_err);
1232#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001233 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001234 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001235 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001236 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001237 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001238 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001239 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001240 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1241 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001242 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1243 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001244 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001245 }
1246
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001247 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001248 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001249 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001250 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001251 }
Juan Quintelacab30142011-02-22 23:54:21 +01001252}
1253
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001254void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001255{
Juan Quintela17549e82011-10-05 13:50:43 +02001256 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001257}
1258
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001259void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1260{
1261 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001262 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001263
1264 /* Check for truncation */
1265 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001266 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1267 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001268 return;
1269 }
1270
Orit Wassermana5615b12014-01-30 20:08:36 +02001271 /* Cache should not be larger than guest ram size */
1272 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001273 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1274 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001275 return;
1276 }
1277
Orit Wassermanc91e6812014-01-30 20:08:34 +02001278 new_size = xbzrle_cache_resize(value);
1279 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001280 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1281 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001282 return;
1283 }
1284
1285 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001286}
1287
1288int64_t qmp_query_migrate_cache_size(Error **errp)
1289{
1290 return migrate_xbzrle_cache_size();
1291}
1292
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001293void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001294{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301295 MigrationParameters p = {
1296 .has_max_bandwidth = true,
1297 .max_bandwidth = value,
1298 };
Juan Quintelacab30142011-02-22 23:54:21 +01001299
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301300 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001301}
1302
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001303void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001304{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001305 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1306 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1307 "the range of 0 to %d seconds",
1308 MAX_MIGRATE_DOWNTIME_SECONDS);
1309 return;
1310 }
1311
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301312 value *= 1000; /* Convert to milliseconds */
1313 value = MAX(0, MIN(INT64_MAX, value));
1314
1315 MigrationParameters p = {
1316 .has_downtime_limit = true,
1317 .downtime_limit = value,
1318 };
1319
1320 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001321}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001322
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001323bool migrate_release_ram(void)
1324{
1325 MigrationState *s;
1326
1327 s = migrate_get_current();
1328
1329 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1330}
1331
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001332bool migrate_postcopy_ram(void)
1333{
1334 MigrationState *s;
1335
1336 s = migrate_get_current();
1337
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001338 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001339}
1340
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001341bool migrate_auto_converge(void)
1342{
1343 MigrationState *s;
1344
1345 s = migrate_get_current();
1346
1347 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1348}
1349
Peter Lieven323004a2013-07-18 09:48:50 +02001350bool migrate_zero_blocks(void)
1351{
1352 MigrationState *s;
1353
1354 s = migrate_get_current();
1355
1356 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1357}
1358
Liang Li8706d2d2015-03-23 16:32:17 +08001359bool migrate_use_compression(void)
1360{
Liang Lidde4e692015-03-23 16:32:26 +08001361 MigrationState *s;
1362
1363 s = migrate_get_current();
1364
1365 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001366}
1367
1368int migrate_compress_level(void)
1369{
1370 MigrationState *s;
1371
1372 s = migrate_get_current();
1373
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001374 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001375}
1376
1377int migrate_compress_threads(void)
1378{
1379 MigrationState *s;
1380
1381 s = migrate_get_current();
1382
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001383 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001384}
1385
Liang Li3fcb38c2015-03-23 16:32:18 +08001386int migrate_decompress_threads(void)
1387{
1388 MigrationState *s;
1389
1390 s = migrate_get_current();
1391
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001392 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001393}
1394
Juan Quintelab05dc722015-07-07 14:44:05 +02001395bool migrate_use_events(void)
1396{
1397 MigrationState *s;
1398
1399 s = migrate_get_current();
1400
1401 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1402}
1403
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001404int migrate_use_xbzrle(void)
1405{
1406 MigrationState *s;
1407
1408 s = migrate_get_current();
1409
1410 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1411}
1412
1413int64_t migrate_xbzrle_cache_size(void)
1414{
1415 MigrationState *s;
1416
1417 s = migrate_get_current();
1418
1419 return s->xbzrle_cache_size;
1420}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001421
Juan Quintela2833c592017-04-05 18:32:37 +02001422bool migrate_use_block(void)
1423{
1424 MigrationState *s;
1425
1426 s = migrate_get_current();
1427
1428 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1429}
1430
1431bool migrate_use_block_incremental(void)
1432{
1433 MigrationState *s;
1434
1435 s = migrate_get_current();
1436
1437 return s->parameters.block_incremental;
1438}
1439
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001440/* migration thread support */
1441/*
1442 * Something bad happened to the RP stream, mark an error
1443 * The caller shall print or trace something to indicate why
1444 */
1445static void mark_source_rp_bad(MigrationState *s)
1446{
1447 s->rp_state.error = true;
1448}
1449
1450static struct rp_cmd_args {
1451 ssize_t len; /* -1 = variable */
1452 const char *name;
1453} rp_cmd_args[] = {
1454 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1455 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1456 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001457 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1458 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001459 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1460};
1461
1462/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001463 * Process a request for pages received on the return path,
1464 * We're allowed to send more than requested (e.g. to round to our page size)
1465 * and we don't need to send pages that have already been sent.
1466 */
1467static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1468 ram_addr_t start, size_t len)
1469{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001470 long our_host_ps = getpagesize();
1471
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001472 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001473
1474 /*
1475 * Since we currently insist on matching page sizes, just sanity check
1476 * we're being asked for whole host pages.
1477 */
1478 if (start & (our_host_ps-1) ||
1479 (len & (our_host_ps-1))) {
1480 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1481 " len: %zd", __func__, start, len);
1482 mark_source_rp_bad(ms);
1483 return;
1484 }
1485
Juan Quintela96506892017-03-14 18:41:03 +01001486 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001487 mark_source_rp_bad(ms);
1488 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001489}
1490
1491/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001492 * Handles messages sent on the return path towards the source VM
1493 *
1494 */
1495static void *source_return_path_thread(void *opaque)
1496{
1497 MigrationState *ms = opaque;
1498 QEMUFile *rp = ms->rp_state.from_dst_file;
1499 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001500 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001501 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001502 ram_addr_t start = 0; /* =0 to silence warning */
1503 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001504 int res;
1505
1506 trace_source_return_path_thread_entry();
1507 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1508 migration_is_setup_or_active(ms->state)) {
1509 trace_source_return_path_thread_loop_top();
1510 header_type = qemu_get_be16(rp);
1511 header_len = qemu_get_be16(rp);
1512
1513 if (header_type >= MIG_RP_MSG_MAX ||
1514 header_type == MIG_RP_MSG_INVALID) {
1515 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1516 header_type, header_len);
1517 mark_source_rp_bad(ms);
1518 goto out;
1519 }
1520
1521 if ((rp_cmd_args[header_type].len != -1 &&
1522 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001523 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001524 error_report("RP: Received '%s' message (0x%04x) with"
1525 "incorrect length %d expecting %zu",
1526 rp_cmd_args[header_type].name, header_type, header_len,
1527 (size_t)rp_cmd_args[header_type].len);
1528 mark_source_rp_bad(ms);
1529 goto out;
1530 }
1531
1532 /* We know we've got a valid header by this point */
1533 res = qemu_get_buffer(rp, buf, header_len);
1534 if (res != header_len) {
1535 error_report("RP: Failed reading data for message 0x%04x"
1536 " read %d expected %d",
1537 header_type, res, header_len);
1538 mark_source_rp_bad(ms);
1539 goto out;
1540 }
1541
1542 /* OK, we have the message and the data */
1543 switch (header_type) {
1544 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001545 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001546 trace_source_return_path_thread_shut(sibling_error);
1547 if (sibling_error) {
1548 error_report("RP: Sibling indicated error %d", sibling_error);
1549 mark_source_rp_bad(ms);
1550 }
1551 /*
1552 * We'll let the main thread deal with closing the RP
1553 * we could do a shutdown(2) on it, but we're the only user
1554 * anyway, so there's nothing gained.
1555 */
1556 goto out;
1557
1558 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001559 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001560 trace_source_return_path_thread_pong(tmp32);
1561 break;
1562
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001563 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001564 start = ldq_be_p(buf);
1565 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001566 migrate_handle_rp_req_pages(ms, NULL, start, len);
1567 break;
1568
1569 case MIG_RP_MSG_REQ_PAGES_ID:
1570 expected_len = 12 + 1; /* header + termination */
1571
1572 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001573 start = ldq_be_p(buf);
1574 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001575 /* Now we expect an idstr */
1576 tmp32 = buf[12]; /* Length of the following idstr */
1577 buf[13 + tmp32] = '\0';
1578 expected_len += tmp32;
1579 }
1580 if (header_len != expected_len) {
1581 error_report("RP: Req_Page_id with length %d expecting %zd",
1582 header_len, expected_len);
1583 mark_source_rp_bad(ms);
1584 goto out;
1585 }
1586 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1587 break;
1588
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001589 default:
1590 break;
1591 }
1592 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001593 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001594 trace_source_return_path_thread_bad_end();
1595 mark_source_rp_bad(ms);
1596 }
1597
1598 trace_source_return_path_thread_end();
1599out:
1600 ms->rp_state.from_dst_file = NULL;
1601 qemu_fclose(rp);
1602 return NULL;
1603}
1604
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001605static int open_return_path_on_source(MigrationState *ms)
1606{
1607
zhanghailiang89a02a92016-01-15 11:37:42 +08001608 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001609 if (!ms->rp_state.from_dst_file) {
1610 return -1;
1611 }
1612
1613 trace_open_return_path_on_source();
1614 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1615 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1616
1617 trace_open_return_path_on_source_continue();
1618
1619 return 0;
1620}
1621
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001622/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1623static int await_return_path_close_on_source(MigrationState *ms)
1624{
1625 /*
1626 * If this is a normal exit then the destination will send a SHUT and the
1627 * rp_thread will exit, however if there's an error we need to cause
1628 * it to exit.
1629 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001630 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001631 /*
1632 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1633 * waiting for the destination.
1634 */
1635 qemu_file_shutdown(ms->rp_state.from_dst_file);
1636 mark_source_rp_bad(ms);
1637 }
1638 trace_await_return_path_close_on_source_joining();
1639 qemu_thread_join(&ms->rp_state.rp_thread);
1640 trace_await_return_path_close_on_source_close();
1641 return ms->rp_state.error;
1642}
1643
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001644/*
1645 * Switch from normal iteration to postcopy
1646 * Returns non-0 on error
1647 */
1648static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1649{
1650 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001651 QIOChannelBuffer *bioc;
1652 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001653 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001654 bool restart_block = false;
zhanghailiang48781e52015-12-16 11:47:33 +00001655 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001656 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1657
1658 trace_postcopy_start();
1659 qemu_mutex_lock_iothread();
1660 trace_postcopy_start_set_run();
1661
1662 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1663 *old_vm_running = runstate_is_running();
1664 global_state_store();
1665 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001666 if (ret < 0) {
1667 goto fail;
1668 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001669
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001670 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001671 if (ret < 0) {
1672 goto fail;
1673 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001674 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001675
1676 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001677 * Cause any non-postcopiable, but iterative devices to
1678 * send out their final data.
1679 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001680 qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001681
1682 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001683 * in Finish migrate and with the io-lock held everything should
1684 * be quiet, but we've potentially still got dirty pages and we
1685 * need to tell the destination to throw any pages it's already received
1686 * that are dirty
1687 */
1688 if (ram_postcopy_send_discard_bitmap(ms)) {
1689 error_report("postcopy send discard bitmap failed");
1690 goto fail;
1691 }
1692
1693 /*
1694 * send rest of state - note things that are doing postcopy
1695 * will notice we're in POSTCOPY_ACTIVE and not actually
1696 * wrap their state up here
1697 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001698 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001699 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001700 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001701
1702 /*
1703 * While loading the device state we may trigger page transfer
1704 * requests and the fd must be free to process those, and thus
1705 * the destination must read the whole device state off the fd before
1706 * it starts processing it. Unfortunately the ad-hoc migration format
1707 * doesn't allow the destination to know the size to read without fully
1708 * parsing it through each devices load-state code (especially the open
1709 * coded devices that use get/put).
1710 * So we wrap the device state up in a package with a length at the start;
1711 * to do this we use a qemu_buf to hold the whole of the device state.
1712 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001713 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001714 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001715 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1716 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001717
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001718 /*
1719 * Make sure the receiver can get incoming pages before we send the rest
1720 * of the state
1721 */
1722 qemu_savevm_send_postcopy_listen(fb);
1723
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001724 qemu_savevm_state_complete_precopy(fb, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001725 qemu_savevm_send_ping(fb, 3);
1726
1727 qemu_savevm_send_postcopy_run(fb);
1728
1729 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001730
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001731 /* Last point of recovery; as soon as we send the package the destination
1732 * can open devices and potentially start running.
1733 * Lets just check again we've not got any errors.
1734 */
1735 ret = qemu_file_get_error(ms->to_dst_file);
1736 if (ret) {
1737 error_report("postcopy_start: Migration stream errored (pre package)");
1738 goto fail_closefb;
1739 }
1740
1741 restart_block = false;
1742
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001743 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001744 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001745 goto fail_closefb;
1746 }
1747 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001748
1749 /* Send a notify to give a chance for anything that needs to happen
1750 * at the transition to postcopy and after the device state; in particular
1751 * spice needs to trigger a transition now
1752 */
1753 ms->postcopy_after_devices = true;
1754 notifier_list_notify(&migration_state_notifiers, ms);
1755
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001756 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1757
1758 qemu_mutex_unlock_iothread();
1759
1760 /*
1761 * Although this ping is just for debug, it could potentially be
1762 * used for getting a better measurement of downtime at the source.
1763 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001764 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001765
Pavel Butsykinced1c612017-02-03 18:23:21 +03001766 if (migrate_release_ram()) {
1767 ram_postcopy_migrated_memory_release(ms);
1768 }
1769
zhanghailiang89a02a92016-01-15 11:37:42 +08001770 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001771 if (ret) {
1772 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001773 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001774 MIGRATION_STATUS_FAILED);
1775 }
1776
1777 return ret;
1778
1779fail_closefb:
1780 qemu_fclose(fb);
1781fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001782 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001783 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001784 if (restart_block) {
1785 /* A failure happened early enough that we know the destination hasn't
1786 * accessed block devices, so we're safe to recover.
1787 */
1788 Error *local_err = NULL;
1789
1790 bdrv_invalidate_cache_all(&local_err);
1791 if (local_err) {
1792 error_report_err(local_err);
1793 }
1794 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001795 qemu_mutex_unlock_iothread();
1796 return -1;
1797}
1798
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001799/**
1800 * migration_completion: Used by migration_thread when there's not much left.
1801 * The caller 'breaks' the loop when this returns.
1802 *
1803 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001804 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001805 * @*old_vm_running: Pointer to old_vm_running flag
1806 * @*start_time: Pointer to time to update
1807 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001808static void migration_completion(MigrationState *s, int current_active_state,
1809 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001810 int64_t *start_time)
1811{
1812 int ret;
1813
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001814 if (s->state == MIGRATION_STATUS_ACTIVE) {
1815 qemu_mutex_lock_iothread();
1816 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1817 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1818 *old_vm_running = runstate_is_running();
1819 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001820
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001821 if (!ret) {
1822 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
zhanghailiang0b827d52016-10-27 14:42:54 +08001823 /*
1824 * Don't mark the image with BDRV_O_INACTIVE flag if
1825 * we will go into COLO stage later.
1826 */
1827 if (ret >= 0 && !migrate_colo_enabled()) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001828 ret = bdrv_inactivate_all();
1829 }
1830 if (ret >= 0) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001831 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1832 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001833 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001834 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001835 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001836 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001837
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001838 if (ret < 0) {
1839 goto fail;
1840 }
1841 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1842 trace_migration_completion_postcopy_end();
1843
zhanghailiang89a02a92016-01-15 11:37:42 +08001844 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001845 trace_migration_completion_postcopy_end_after_complete();
1846 }
1847
1848 /*
1849 * If rp was opened we must clean up the thread before
1850 * cleaning everything else up (since if there are no failures
1851 * it will wait for the destination to send it's status in
1852 * a SHUT command).
1853 * Postcopy opens rp if enabled (even if it's not avtivated)
1854 */
1855 if (migrate_postcopy_ram()) {
1856 int rp_error;
1857 trace_migration_completion_postcopy_end_before_rp();
1858 rp_error = await_return_path_close_on_source(s);
1859 trace_migration_completion_postcopy_end_after_rp(rp_error);
1860 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001861 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001862 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001863 }
1864
zhanghailiang89a02a92016-01-15 11:37:42 +08001865 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001866 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001867 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001868 }
1869
zhanghailiang0b827d52016-10-27 14:42:54 +08001870 if (!migrate_colo_enabled()) {
1871 migrate_set_state(&s->state, current_active_state,
1872 MIGRATION_STATUS_COMPLETED);
1873 }
1874
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001875 return;
1876
Greg Kurzfe904ea2016-05-18 15:44:36 +02001877fail_invalidate:
1878 /* If not doing postcopy, vm_start() will be called: let's regain
1879 * control on images.
1880 */
1881 if (s->state == MIGRATION_STATUS_ACTIVE) {
1882 Error *local_err = NULL;
1883
zhanghailiang1d2acc32017-01-24 15:59:52 +08001884 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001885 bdrv_invalidate_cache_all(&local_err);
1886 if (local_err) {
1887 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001888 } else {
1889 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02001890 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001891 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001892 }
1893
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001894fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001895 migrate_set_state(&s->state, current_active_state,
1896 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001897}
1898
zhanghailiang35a6ed42016-10-27 14:42:52 +08001899bool migrate_colo_enabled(void)
1900{
1901 MigrationState *s = migrate_get_current();
1902 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
1903}
1904
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001905/*
1906 * Master migration thread on the source VM.
1907 * It drives the migration and pumps the data down the outgoing channel.
1908 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001909static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001910{
Juan Quintela9848a402012-12-19 09:55:50 +01001911 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001912 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001913 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1914 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001915 int64_t initial_bytes = 0;
Peter Xufaec0662017-04-01 16:18:43 +08001916 /*
1917 * The final stage happens when the remaining data is smaller than
1918 * this threshold; it's calculated from the requested downtime and
1919 * measured bandwidth
1920 */
1921 int64_t threshold_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001922 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001923 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001924 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001925 bool entered_postcopy = false;
1926 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1927 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang0b827d52016-10-27 14:42:54 +08001928 bool enable_colo = migrate_colo_enabled();
Juan Quintela76f59332012-10-03 20:16:24 +02001929
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001930 rcu_register_thread();
1931
zhanghailiang89a02a92016-01-15 11:37:42 +08001932 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001933
1934 if (migrate_postcopy_ram()) {
1935 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001936 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001937
1938 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001939 qemu_savevm_send_ping(s->to_dst_file, 1);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001940
1941 /*
1942 * Tell the destination that we *might* want to do postcopy later;
1943 * if the other end can't do postcopy it should fail now, nice and
1944 * early.
1945 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001946 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001947 }
1948
Juan Quintelaa0762d92017-04-05 21:00:09 +02001949 qemu_savevm_state_begin(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001950
Alex Blighbc72ad62013-08-21 16:03:08 +01001951 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00001952 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1953 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001954
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001955 trace_migration_thread_setup_complete();
1956
1957 while (s->state == MIGRATION_STATUS_ACTIVE ||
1958 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001959 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001960 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001961
zhanghailiang89a02a92016-01-15 11:37:42 +08001962 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001963 uint64_t pend_post, pend_nonpost;
1964
Peter Xufaec0662017-04-01 16:18:43 +08001965 qemu_savevm_state_pending(s->to_dst_file, threshold_size,
1966 &pend_nonpost, &pend_post);
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001967 pending_size = pend_nonpost + pend_post;
Peter Xufaec0662017-04-01 16:18:43 +08001968 trace_migrate_pending(pending_size, threshold_size,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001969 pend_post, pend_nonpost);
Peter Xufaec0662017-04-01 16:18:43 +08001970 if (pending_size && pending_size >= threshold_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001971 /* Still a significant amount to transfer */
1972
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001973 if (migrate_postcopy_ram() &&
1974 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
Peter Xufaec0662017-04-01 16:18:43 +08001975 pend_nonpost <= threshold_size &&
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001976 atomic_read(&s->start_postcopy)) {
1977
1978 if (!postcopy_start(s, &old_vm_running)) {
1979 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1980 entered_postcopy = true;
1981 }
1982
1983 continue;
1984 }
1985 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001986 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001987 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001988 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001989 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001990 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001991 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001992 }
1993 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001994
zhanghailiang89a02a92016-01-15 11:37:42 +08001995 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001996 migrate_set_state(&s->state, current_active_state,
1997 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001998 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001999 break;
2000 }
Alex Blighbc72ad62013-08-21 16:03:08 +01002001 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002002 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08002003 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
2004 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05002005 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01002006 double bandwidth = (double)transferred_bytes / time_spent;
Peter Xufaec0662017-04-01 16:18:43 +08002007 threshold_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002008
Wei Yang5b648de2016-01-24 14:09:57 +00002009 s->mbps = (((double) transferred_bytes * 8.0) /
2010 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04002011
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11002012 trace_migrate_transferred(transferred_bytes, time_spent,
Peter Xufaec0662017-04-01 16:18:43 +08002013 bandwidth, threshold_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01002014 /* if we haven't sent anything, we don't want to recalculate
2015 10000 is a small enough number for our purposes */
Juan Quintela47ad8612017-03-14 18:20:30 +01002016 if (ram_dirty_pages_rate() && transferred_bytes > 10000) {
2017 s->expected_downtime = ram_dirty_pages_rate() *
Juan Quintela20afaed2017-03-21 09:09:14 +01002018 qemu_target_page_size() / bandwidth;
Juan Quintela90f8ae72013-02-01 13:22:37 +01002019 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002020
zhanghailiang89a02a92016-01-15 11:37:42 +08002021 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002022 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08002023 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002024 }
zhanghailiang89a02a92016-01-15 11:37:42 +08002025 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002026 /* usleep expects microseconds */
2027 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
2028 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002029 }
2030
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002031 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04002032 /* If we enabled cpu throttling for auto-converge, turn it off. */
2033 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08002034 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04002035
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002036 qemu_mutex_lock_iothread();
zhanghailiang0b827d52016-10-27 14:42:54 +08002037 /*
2038 * The resource has been allocated by migration will be reused in COLO
2039 * process, so don't release them.
2040 */
2041 if (!enable_colo) {
2042 qemu_savevm_state_cleanup();
2043 }
zhanghailiang31194732015-03-13 16:08:38 +08002044 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08002045 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002046 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002047 if (!entered_postcopy) {
2048 s->downtime = end_time - start_time;
2049 }
Peter Lievend6ed7312014-05-12 10:46:00 +02002050 if (s->total_time) {
2051 s->mbps = (((double) transferred_bytes * 8.0) /
2052 ((double) s->total_time)) / 1000;
2053 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002054 runstate_set(RUN_STATE_POSTMIGRATE);
2055 } else {
zhanghailiang0b827d52016-10-27 14:42:54 +08002056 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
2057 migrate_start_colo_process(s);
2058 qemu_savevm_state_cleanup();
2059 /*
2060 * Fixme: we will run VM in COLO no matter its old running state.
2061 * After exited COLO, we will keep running.
2062 */
2063 old_vm_running = true;
2064 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002065 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002066 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01002067 } else {
2068 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2069 runstate_set(RUN_STATE_POSTMIGRATE);
2070 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01002071 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002072 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002073 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01002074 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002075
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002076 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002077 return NULL;
2078}
2079
Juan Quintela9848a402012-12-19 09:55:50 +01002080void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002081{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302082 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002083 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002084
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01002085 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08002086 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302087 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002088
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002089 /* Notify before starting migration thread */
2090 notifier_list_notify(&migration_state_notifiers, s);
2091
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002092 /*
2093 * Open the return path; currently for postcopy but other things might
2094 * also want it.
2095 */
2096 if (migrate_postcopy_ram()) {
2097 if (open_return_path_on_source(s)) {
2098 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00002099 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002100 MIGRATION_STATUS_FAILED);
2101 migrate_fd_cleanup(s);
2102 return;
2103 }
2104 }
2105
Liang Li8706d2d2015-03-23 16:32:17 +08002106 migrate_compress_threads_create();
Pankaj Gupta009fad72017-01-23 19:12:56 +05302107 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002108 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002109 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002110}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002111