blob: c5c977e7375298606b7421a5334cfd3e1d433328 [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
16#include "qemu-common.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010017#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010018#include "qemu/main-loop.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010019#include "migration/migration.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020020#include "migration/qemu-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010021#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010023#include "qapi/qmp/qerror.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000024#include "qapi/util.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/sockets.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020026#include "qemu/rcu.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010027#include "migration/block.h"
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000028#include "migration/postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020029#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030030#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010031#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020032#include "qapi-event.h"
Jason J. Herne070afca2015-09-08 13:12:35 -040033#include "qom/cpu.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000034#include "exec/memory.h"
35#include "exec/address-spaces.h"
aliguori065e2812008-11-11 16:46:33 +000036
Jason J. Hernedc325622015-09-08 13:12:37 -040037#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000038
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010039/* Amount of time to allocate to each "chunk" of bandwidth-throttled
40 * data. */
41#define BUFFER_DELAY 100
42#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
43
Liang Li8706d2d2015-03-23 16:32:17 +080044/* Default compression thread count */
45#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080046/* Default decompression thread count, usually decompression is at
47 * least 4 times as fast as compression.*/
48#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080049/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
50#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040051/* Define default autoconverge cpu throttle migration parameters */
52#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20
53#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080054
Orit Wasserman17ad9b32012-08-06 21:42:53 +030055/* Migration XBZRLE default cache size */
56#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
57
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010058static NotifierList migration_state_notifiers =
59 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
60
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000061static bool deferred_incoming;
62
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000063/*
64 * Current state of incoming postcopy; note this is not part of
65 * MigrationIncomingState since it's state is used during cleanup
66 * at the end as MIS is being freed.
67 */
68static PostcopyState incoming_postcopy_state;
69
Juan Quintela17549e82011-10-05 13:50:43 +020070/* When we add fault tolerance, we could have several
71 migrations at once. For now we don't need to add
72 dynamic creation of migration */
73
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010074/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020075MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020076{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000077 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +020078 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080079 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010080 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030081 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040082 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080083 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
84 DEFAULT_MIGRATE_COMPRESS_LEVEL,
85 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
86 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
87 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
88 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Jason J. Herne1626fee2015-09-08 13:12:34 -040089 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
90 DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL,
91 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
92 DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT,
Juan Quintela17549e82011-10-05 13:50:43 +020093 };
94
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000095 if (!once) {
96 qemu_mutex_init(&current_migration.src_page_req_mutex);
97 once = true;
98 }
Juan Quintela17549e82011-10-05 13:50:43 +020099 return &current_migration;
100}
101
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100102/* For incoming */
103static MigrationIncomingState *mis_current;
104
105MigrationIncomingState *migration_incoming_get_current(void)
106{
107 return mis_current;
108}
109
110MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
111{
Markus Armbruster97f3ad32015-09-14 13:51:31 +0200112 mis_current = g_new0(MigrationIncomingState, 1);
Dr. David Alan Gilbert42e2aa52015-11-05 18:10:34 +0000113 mis_current->from_src_file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100114 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000115 qemu_mutex_init(&mis_current->rp_mutex);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000116 qemu_event_init(&mis_current->main_thread_load_event, false);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100117
118 return mis_current;
119}
120
121void migration_incoming_state_destroy(void)
122{
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000123 qemu_event_destroy(&mis_current->main_thread_load_event);
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100124 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100125 g_free(mis_current);
126 mis_current = NULL;
127}
128
Juan Quinteladf4b1022014-10-08 10:58:10 +0200129
130typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200131 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200132 uint32_t size;
133 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200134 RunState state;
135 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200136} GlobalState;
137
138static GlobalState global_state;
139
Juan Quintela560d0272015-07-15 09:53:46 +0200140int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200141{
142 if (!runstate_store((char *)global_state.runstate,
143 sizeof(global_state.runstate))) {
144 error_report("runstate name too big: %s", global_state.runstate);
145 trace_migrate_state_too_big();
146 return -EINVAL;
147 }
148 return 0;
149}
150
Anthony PERARDc69adea2015-08-03 15:29:19 +0100151void global_state_store_running(void)
152{
153 const char *state = RunState_lookup[RUN_STATE_RUNNING];
154 strncpy((char *)global_state.runstate,
155 state, sizeof(global_state.runstate));
156}
157
Juan Quintela172c4352015-07-08 13:56:26 +0200158static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200159{
Juan Quintela172c4352015-07-08 13:56:26 +0200160 return global_state.received;
161}
162
163static RunState global_state_get_runstate(void)
164{
165 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200166}
167
Juan Quintela13d16812014-10-08 13:58:24 +0200168void global_state_set_optional(void)
169{
170 global_state.optional = true;
171}
172
173static bool global_state_needed(void *opaque)
174{
175 GlobalState *s = opaque;
176 char *runstate = (char *)s->runstate;
177
178 /* If it is not optional, it is mandatory */
179
180 if (s->optional == false) {
181 return true;
182 }
183
184 /* If state is running or paused, it is not needed */
185
186 if (strcmp(runstate, "running") == 0 ||
187 strcmp(runstate, "paused") == 0) {
188 return false;
189 }
190
191 /* for any other state it is needed */
192 return true;
193}
194
Juan Quinteladf4b1022014-10-08 10:58:10 +0200195static int global_state_post_load(void *opaque, int version_id)
196{
197 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200198 Error *local_err = NULL;
199 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200200 char *runstate = (char *)s->runstate;
201
Juan Quintela172c4352015-07-08 13:56:26 +0200202 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200203 trace_migrate_global_state_post_load(runstate);
204
Juan Quintela172c4352015-07-08 13:56:26 +0200205 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200206 -1, &local_err);
207
Juan Quintela172c4352015-07-08 13:56:26 +0200208 if (r == -1) {
209 if (local_err) {
210 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200211 }
Juan Quintela172c4352015-07-08 13:56:26 +0200212 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200213 }
Juan Quintela172c4352015-07-08 13:56:26 +0200214 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200215
Juan Quintela172c4352015-07-08 13:56:26 +0200216 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200217}
218
219static void global_state_pre_save(void *opaque)
220{
221 GlobalState *s = opaque;
222
223 trace_migrate_global_state_pre_save((char *)s->runstate);
224 s->size = strlen((char *)s->runstate) + 1;
225}
226
227static const VMStateDescription vmstate_globalstate = {
228 .name = "globalstate",
229 .version_id = 1,
230 .minimum_version_id = 1,
231 .post_load = global_state_post_load,
232 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200233 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200234 .fields = (VMStateField[]) {
235 VMSTATE_UINT32(size, GlobalState),
236 VMSTATE_BUFFER(runstate, GlobalState),
237 VMSTATE_END_OF_LIST()
238 },
239};
240
241void register_global_state(void)
242{
243 /* We would use it independently that we receive it */
244 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200245 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200246 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
247}
248
Juan Quintelab05dc722015-07-07 14:44:05 +0200249static void migrate_generate_event(int new_state)
250{
251 if (migrate_use_events()) {
252 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200253 }
254}
255
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000256/*
257 * Called on -incoming with a defer: uri.
258 * The migration can be started later after any parameters have been
259 * changed.
260 */
261static void deferred_incoming_migration(Error **errp)
262{
263 if (deferred_incoming) {
264 error_setg(errp, "Incoming migration already deferred");
265 }
266 deferred_incoming = true;
267}
268
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000269/* Request a range of pages from the source VM at the given
270 * start address.
271 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
272 * as the last request (a name must have been given previously)
273 * Start: Address offset within the RB
274 * Len: Length in bytes required - must be a multiple of pagesize
275 */
276void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
277 ram_addr_t start, size_t len)
278{
279 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname upto 256 */
280 size_t msglen = 12; /* start + len */
281
282 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
283 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
284
285 if (rbname) {
286 int rbname_len = strlen(rbname);
287 assert(rbname_len < 256);
288
289 bufc[msglen++] = rbname_len;
290 memcpy(bufc + msglen, rbname, rbname_len);
291 msglen += rbname_len;
292 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
293 } else {
294 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
295 }
296}
297
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200298void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000299{
aliguori34c9dd82008-10-13 03:14:31 +0000300 const char *p;
301
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200302 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000303 if (!strcmp(uri, "defer")) {
304 deferred_incoming_migration(errp);
305 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200306 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400307#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000308 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400309 rdma_start_incoming_migration(p, errp);
310#endif
aliguori065e2812008-11-11 16:46:33 +0000311#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000312 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200313 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000314 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200315 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000316 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200317 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000318#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000319 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100320 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200321 }
aliguori5bb79102008-10-13 03:12:02 +0000322}
323
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200324static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200325{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200326 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100327 Error *local_err = NULL;
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000328 MigrationIncomingState *mis;
329 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200330 int ret;
331
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000332 mis = migration_incoming_state_new(f);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000333 postcopy_state_set(POSTCOPY_INCOMING_NONE);
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200334 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000335
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200336 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100337
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000338 ps = postcopy_state_get();
339 trace_process_incoming_migration_co_end(ret, ps);
340 if (ps != POSTCOPY_INCOMING_NONE) {
341 if (ps == POSTCOPY_INCOMING_ADVISE) {
342 /*
343 * Where a migration had postcopy enabled (and thus went to advise)
344 * but managed to complete within the precopy period, we can use
345 * the normal exit.
346 */
347 postcopy_ram_incoming_cleanup(mis);
348 } else if (ret >= 0) {
349 /*
350 * Postcopy was started, cleanup should happen at the end of the
351 * postcopy thread.
352 */
353 trace_process_incoming_migration_co_postcopy_end_main();
354 return;
355 }
356 /* Else if something went wrong then just fall out of the normal exit */
357 }
358
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200359 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200360 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100361 migration_incoming_state_destroy();
362
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200363 if (ret < 0) {
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200364 migrate_generate_event(MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200365 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800366 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600367 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200368 }
Juan Quintela511c0232010-06-09 14:10:55 +0200369
Anthony Liguori0f154232011-11-14 15:09:45 -0600370 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100371 bdrv_invalidate_cache_all(&local_err);
372 if (local_err) {
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100373 migrate_generate_event(MIGRATION_STATUS_FAILED);
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100374 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800375 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100376 exit(EXIT_FAILURE);
377 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600378
Amit Shah92e37622015-10-14 17:37:19 +0530379 /*
380 * This must happen after all error conditions are dealt with and
381 * we're sure the VM is going to be running on this host.
382 */
383 qemu_announce_self();
384
Juan Quintela172c4352015-07-08 13:56:26 +0200385 /* If global state section was not received or we are in running
386 state, we need to obey autostart. Any other state is set with
387 runstate_set. */
Juan Quinteladf4b1022014-10-08 10:58:10 +0200388
Juan Quintela172c4352015-07-08 13:56:26 +0200389 if (!global_state_received() ||
390 global_state_get_runstate() == RUN_STATE_RUNNING) {
Juan Quinteladf4b1022014-10-08 10:58:10 +0200391 if (autostart) {
392 vm_start();
393 } else {
394 runstate_set(RUN_STATE_PAUSED);
395 }
Juan Quintela172c4352015-07-08 13:56:26 +0200396 } else {
397 runstate_set(global_state_get_runstate());
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300398 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800399 migrate_decompress_threads_join();
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100400 /*
401 * This must happen after any state changes since as soon as an external
402 * observer sees this event they might start to prod at the VM assuming
403 * it's ready to use.
404 */
405 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
Juan Quintela511c0232010-06-09 14:10:55 +0200406}
407
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200408void process_incoming_migration(QEMUFile *f)
409{
410 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
411 int fd = qemu_get_fd(f);
412
413 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800414 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100415 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200416 qemu_coroutine_enter(co, f);
417}
418
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000419/*
420 * Send a message on the return channel back to the source
421 * of the migration.
422 */
423void migrate_send_rp_message(MigrationIncomingState *mis,
424 enum mig_rp_message_type message_type,
425 uint16_t len, void *data)
426{
427 trace_migrate_send_rp_message((int)message_type, len);
428 qemu_mutex_lock(&mis->rp_mutex);
429 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
430 qemu_put_be16(mis->to_src_file, len);
431 qemu_put_buffer(mis->to_src_file, data, len);
432 qemu_fflush(mis->to_src_file);
433 qemu_mutex_unlock(&mis->rp_mutex);
434}
435
436/*
437 * Send a 'SHUT' message on the return channel with the given value
438 * to indicate that we've finished with the RP. Non-0 value indicates
439 * error.
440 */
441void migrate_send_rp_shut(MigrationIncomingState *mis,
442 uint32_t value)
443{
444 uint32_t buf;
445
446 buf = cpu_to_be32(value);
447 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
448}
449
450/*
451 * Send a 'PONG' message on the return channel with the given value
452 * (normally in response to a 'PING')
453 */
454void migrate_send_rp_pong(MigrationIncomingState *mis,
455 uint32_t value)
456{
457 uint32_t buf;
458
459 buf = cpu_to_be32(value);
460 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
461}
462
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400463/* amount of nanoseconds we are willing to wait for migration to be down.
464 * the choice of nanoseconds is because it is the maximum resolution that
465 * get_clock() can achieve. It is an internal measure. All user-visible
466 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100467static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400468
469uint64_t migrate_max_downtime(void)
470{
471 return max_downtime;
472}
473
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300474MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
475{
476 MigrationCapabilityStatusList *head = NULL;
477 MigrationCapabilityStatusList *caps;
478 MigrationState *s = migrate_get_current();
479 int i;
480
Michael Tokarev387eede2013-10-05 13:18:28 +0400481 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300482 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
483 if (head == NULL) {
484 head = g_malloc0(sizeof(*caps));
485 caps = head;
486 } else {
487 caps->next = g_malloc0(sizeof(*caps));
488 caps = caps->next;
489 }
490 caps->value =
491 g_malloc(sizeof(*caps->value));
492 caps->value->capability = i;
493 caps->value->state = s->enabled_capabilities[i];
494 }
495
496 return head;
497}
498
Liang Li85de8322015-03-23 16:32:28 +0800499MigrationParameters *qmp_query_migrate_parameters(Error **errp)
500{
501 MigrationParameters *params;
502 MigrationState *s = migrate_get_current();
503
504 params = g_malloc0(sizeof(*params));
505 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
506 params->compress_threads =
507 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
508 params->decompress_threads =
509 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400510 params->x_cpu_throttle_initial =
511 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
512 params->x_cpu_throttle_increment =
513 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Liang Li85de8322015-03-23 16:32:28 +0800514
515 return params;
516}
517
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000518/*
519 * Return true if we're already in the middle of a migration
520 * (i.e. any of the active or setup states)
521 */
522static bool migration_is_setup_or_active(int state)
523{
524 switch (state) {
525 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000526 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000527 case MIGRATION_STATUS_SETUP:
528 return true;
529
530 default:
531 return false;
532
533 }
534}
535
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300536static void get_xbzrle_cache_stats(MigrationInfo *info)
537{
538 if (migrate_use_xbzrle()) {
539 info->has_xbzrle_cache = true;
540 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
541 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
542 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
543 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
544 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800545 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300546 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
547 }
548}
549
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300550MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000551{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300552 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200553 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000554
Juan Quintela17549e82011-10-05 13:50:43 +0200555 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800556 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200557 /* no migration has happened ever */
558 break;
zhanghailiang31194732015-03-13 16:08:38 +0800559 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400560 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400561 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400562 break;
zhanghailiang31194732015-03-13 16:08:38 +0800563 case MIGRATION_STATUS_ACTIVE:
564 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300565 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200566 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100567 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200568 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200569 info->has_expected_downtime = true;
570 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400571 info->has_setup_time = true;
572 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200573
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300574 info->has_ram = true;
575 info->ram = g_malloc0(sizeof(*info->ram));
576 info->ram->transferred = ram_bytes_transferred();
577 info->ram->remaining = ram_bytes_remaining();
578 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300579 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100580 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300581 info->ram->normal = norm_mig_pages_transferred();
582 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200583 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400584 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800585 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200586
Juan Quintela17549e82011-10-05 13:50:43 +0200587 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300588 info->has_disk = true;
589 info->disk = g_malloc0(sizeof(*info->disk));
590 info->disk->transferred = blk_mig_bytes_transferred();
591 info->disk->remaining = blk_mig_bytes_remaining();
592 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000593 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300594
Jason J. Herne47828932015-09-08 13:12:36 -0400595 if (cpu_throttle_active()) {
596 info->has_x_cpu_throttle_percentage = true;
597 info->x_cpu_throttle_percentage = cpu_throttle_get_percentage();
598 }
599
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300600 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200601 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000602 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
603 /* Mostly the same as active; TODO add some postcopy stats */
604 info->has_status = true;
605 info->has_total_time = true;
606 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
607 - s->total_time;
608 info->has_expected_downtime = true;
609 info->expected_downtime = s->expected_downtime;
610 info->has_setup_time = true;
611 info->setup_time = s->setup_time;
612
613 info->has_ram = true;
614 info->ram = g_malloc0(sizeof(*info->ram));
615 info->ram->transferred = ram_bytes_transferred();
616 info->ram->remaining = ram_bytes_remaining();
617 info->ram->total = ram_bytes_total();
618 info->ram->duplicate = dup_mig_pages_transferred();
619 info->ram->skipped = skipped_mig_pages_transferred();
620 info->ram->normal = norm_mig_pages_transferred();
621 info->ram->normal_bytes = norm_mig_bytes_transferred();
622 info->ram->dirty_pages_rate = s->dirty_pages_rate;
623 info->ram->mbps = s->mbps;
624
625 if (blk_mig_active()) {
626 info->has_disk = true;
627 info->disk = g_malloc0(sizeof(*info->disk));
628 info->disk->transferred = blk_mig_bytes_transferred();
629 info->disk->remaining = blk_mig_bytes_remaining();
630 info->disk->total = blk_mig_bytes_total();
631 }
632
633 get_xbzrle_cache_stats(info);
634 break;
zhanghailiang31194732015-03-13 16:08:38 +0800635 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300636 get_xbzrle_cache_stats(info);
637
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300638 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900639 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200640 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200641 info->has_downtime = true;
642 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400643 info->has_setup_time = true;
644 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200645
646 info->has_ram = true;
647 info->ram = g_malloc0(sizeof(*info->ram));
648 info->ram->transferred = ram_bytes_transferred();
649 info->ram->remaining = 0;
650 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300651 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100652 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300653 info->ram->normal = norm_mig_pages_transferred();
654 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400655 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800656 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200657 break;
zhanghailiang31194732015-03-13 16:08:38 +0800658 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300659 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200660 break;
zhanghailiang31194732015-03-13 16:08:38 +0800661 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300662 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200663 break;
aliguori5bb79102008-10-13 03:12:02 +0000664 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800665 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300666
667 return info;
aliguori5bb79102008-10-13 03:12:02 +0000668}
669
Orit Wasserman00458432012-08-06 21:42:48 +0300670void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
671 Error **errp)
672{
673 MigrationState *s = migrate_get_current();
674 MigrationCapabilityStatusList *cap;
675
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000676 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100677 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300678 return;
679 }
680
681 for (cap = params; cap; cap = cap->next) {
682 s->enabled_capabilities[cap->value->capability] = cap->value->state;
683 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000684
685 if (migrate_postcopy_ram()) {
686 if (migrate_use_compression()) {
687 /* The decompression threads asynchronously write into RAM
688 * rather than use the atomic copies needed to avoid
689 * userfaulting. It should be possible to fix the decompression
690 * threads for compatibility in future.
691 */
692 error_report("Postcopy is not currently compatible with "
693 "compression");
694 s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM] =
695 false;
696 }
697 }
Orit Wasserman00458432012-08-06 21:42:48 +0300698}
699
Liang Li85de8322015-03-23 16:32:28 +0800700void qmp_migrate_set_parameters(bool has_compress_level,
701 int64_t compress_level,
702 bool has_compress_threads,
703 int64_t compress_threads,
704 bool has_decompress_threads,
Jason J. Herne1626fee2015-09-08 13:12:34 -0400705 int64_t decompress_threads,
706 bool has_x_cpu_throttle_initial,
707 int64_t x_cpu_throttle_initial,
708 bool has_x_cpu_throttle_increment,
709 int64_t x_cpu_throttle_increment, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800710{
711 MigrationState *s = migrate_get_current();
712
713 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100714 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
715 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800716 return;
717 }
718 if (has_compress_threads &&
719 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100720 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
721 "compress_threads",
722 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800723 return;
724 }
725 if (has_decompress_threads &&
726 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100727 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
728 "decompress_threads",
729 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800730 return;
731 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400732 if (has_x_cpu_throttle_initial &&
733 (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
734 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
735 "x_cpu_throttle_initial",
736 "an integer in the range of 1 to 99");
737 }
738 if (has_x_cpu_throttle_increment &&
739 (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
740 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
741 "x_cpu_throttle_increment",
742 "an integer in the range of 1 to 99");
743 }
Liang Li85de8322015-03-23 16:32:28 +0800744
745 if (has_compress_level) {
746 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
747 }
748 if (has_compress_threads) {
749 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
750 }
751 if (has_decompress_threads) {
752 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
753 decompress_threads;
754 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400755 if (has_x_cpu_throttle_initial) {
756 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
757 x_cpu_throttle_initial;
758 }
759
760 if (has_x_cpu_throttle_increment) {
761 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
762 x_cpu_throttle_increment;
763 }
Liang Li85de8322015-03-23 16:32:28 +0800764}
765
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000766void qmp_migrate_start_postcopy(Error **errp)
767{
768 MigrationState *s = migrate_get_current();
769
770 if (!migrate_postcopy_ram()) {
771 error_setg(errp, "Enable postcopy with migration_set_capability before"
772 " the start of migration");
773 return;
774 }
775
776 if (s->state == MIGRATION_STATUS_NONE) {
777 error_setg(errp, "Postcopy must be started after migration has been"
778 " started");
779 return;
780 }
781 /*
782 * we don't error if migration has finished since that would be racy
783 * with issuing this command.
784 */
785 atomic_set(&s->start_postcopy, true);
786}
787
aliguori065e2812008-11-11 16:46:33 +0000788/* shared migration helpers */
789
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000790static void migrate_set_state(MigrationState *s, int old_state, int new_state)
791{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200792 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200793 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200794 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000795 }
796}
797
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100798static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000799{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100800 MigrationState *s = opaque;
801
802 qemu_bh_delete(s->cleanup_bh);
803 s->cleanup_bh = NULL;
804
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000805 flush_page_queue(s);
806
aliguori065e2812008-11-11 16:46:33 +0000807 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100808 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100809 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000810 if (s->migration_thread_running) {
811 qemu_thread_join(&s->thread);
812 s->migration_thread_running = false;
813 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100814 qemu_mutex_lock_iothread();
815
Liang Li8706d2d2015-03-23 16:32:17 +0800816 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100817 qemu_fclose(s->file);
818 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000819 }
820
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000821 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
822 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100823
Liang Li94f5a432015-11-02 15:37:00 +0800824 if (s->state == MIGRATION_STATUS_CANCELLING) {
825 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
826 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100827 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100828
829 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000830}
831
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200832void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000833{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100834 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100835 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200836 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100837 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100838}
839
Juan Quintela0edda1c2010-05-11 16:28:39 +0200840static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000841{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000842 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000843 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100844 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000845
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000846 if (s->rp_state.from_dst_file) {
847 /* shutdown the rp socket, so causing the rp thread to shutdown */
848 qemu_file_shutdown(s->rp_state.from_dst_file);
849 }
850
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000851 do {
852 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000853 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000854 break;
855 }
zhanghailiang31194732015-03-13 16:08:38 +0800856 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
857 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000858
859 /*
860 * If we're unlucky the migration code might be stuck somewhere in a
861 * send/write while the network has failed and is waiting to timeout;
862 * if we've got shutdown(2) available then we can force it to quit.
863 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
864 * called in a bh, so there is no race against this cancel.
865 */
zhanghailiang31194732015-03-13 16:08:38 +0800866 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000867 qemu_file_shutdown(f);
868 }
aliguori065e2812008-11-11 16:46:33 +0000869}
870
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100871void add_migration_state_change_notifier(Notifier *notify)
872{
873 notifier_list_add(&migration_state_notifiers, notify);
874}
875
876void remove_migration_state_change_notifier(Notifier *notify)
877{
Paolo Bonzini31552522012-01-13 17:34:01 +0100878 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100879}
880
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200881bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200882{
zhanghailiang31194732015-03-13 16:08:38 +0800883 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200884}
885
Juan Quintela70736932011-02-23 00:43:59 +0100886bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100887{
zhanghailiang31194732015-03-13 16:08:38 +0800888 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100889}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200890
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200891bool migration_has_failed(MigrationState *s)
892{
zhanghailiang31194732015-03-13 16:08:38 +0800893 return (s->state == MIGRATION_STATUS_CANCELLED ||
894 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200895}
896
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000897bool migration_in_postcopy(MigrationState *s)
898{
899 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
900}
901
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +0000902MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200903{
Juan Quintela17549e82011-10-05 13:50:43 +0200904 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100905 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300906 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300907 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800908 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
909 int compress_thread_count =
910 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
911 int decompress_thread_count =
912 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400913 int x_cpu_throttle_initial =
914 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
915 int x_cpu_throttle_increment =
916 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300917
918 memcpy(enabled_capabilities, s->enabled_capabilities,
919 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200920
Juan Quintela17549e82011-10-05 13:50:43 +0200921 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300922 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300923 memcpy(s->enabled_capabilities, enabled_capabilities,
924 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300925 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100926
Liang Li43c60a82015-03-23 16:32:27 +0800927 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
928 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
929 compress_thread_count;
930 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
931 decompress_thread_count;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400932 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
933 x_cpu_throttle_initial;
934 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
935 x_cpu_throttle_increment;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200936 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200937 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200938
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000939 QSIMPLEQ_INIT(&s->src_page_requests);
940
Alex Blighbc72ad62013-08-21 16:03:08 +0100941 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200942 return s;
943}
Juan Quintelacab30142011-02-22 23:54:21 +0100944
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600945static GSList *migration_blockers;
946
947void migrate_add_blocker(Error *reason)
948{
949 migration_blockers = g_slist_prepend(migration_blockers, reason);
950}
951
952void migrate_del_blocker(Error *reason)
953{
954 migration_blockers = g_slist_remove(migration_blockers, reason);
955}
956
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000957void qmp_migrate_incoming(const char *uri, Error **errp)
958{
959 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000960 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000961
962 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000963 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000964 return;
965 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000966 if (!once) {
967 error_setg(errp, "The incoming migration has already been started");
968 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000969
970 qemu_start_incoming_migration(uri, &local_err);
971
972 if (local_err) {
973 error_propagate(errp, local_err);
974 return;
975 }
976
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000977 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000978}
979
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200980void qmp_migrate(const char *uri, bool has_blk, bool blk,
981 bool has_inc, bool inc, bool has_detach, bool detach,
982 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100983{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200984 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200985 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300986 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100987 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100988
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900989 params.blk = has_blk && blk;
990 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300991
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000992 if (migration_is_setup_or_active(s->state) ||
zhanghailiang31194732015-03-13 16:08:38 +0800993 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100994 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200995 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100996 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100997 if (runstate_check(RUN_STATE_INMIGRATE)) {
998 error_setg(errp, "Guest is waiting for an incoming migration");
999 return;
1000 }
1001
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001002 if (qemu_savevm_state_blocked(errp)) {
1003 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001004 }
1005
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001006 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001007 *errp = error_copy(migration_blockers->data);
1008 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001009 }
1010
Juan Quintela656a2332015-07-01 09:32:29 +02001011 /* We are starting a new migration, so we want to start in a clean
1012 state. This change is only needed if previous migration
1013 failed/was cancelled. We don't use migrate_set_state() because
1014 we are setting the initial state, not changing it. */
1015 s->state = MIGRATION_STATUS_NONE;
1016
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001017 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +01001018
1019 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001020 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001021#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001022 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001023 rdma_start_outgoing_migration(s, p, &local_err);
1024#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001025#if !defined(WIN32)
1026 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001027 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001028 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001029 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001030 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001031 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001032#endif
1033 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001034 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1035 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +02001036 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001037 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001038 }
1039
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001040 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +02001041 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001042 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001043 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001044 }
Juan Quintelacab30142011-02-22 23:54:21 +01001045}
1046
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001047void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001048{
Juan Quintela17549e82011-10-05 13:50:43 +02001049 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001050}
1051
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001052void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1053{
1054 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001055 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001056
1057 /* Check for truncation */
1058 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001059 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1060 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001061 return;
1062 }
1063
Orit Wassermana5615b12014-01-30 20:08:36 +02001064 /* Cache should not be larger than guest ram size */
1065 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001066 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1067 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001068 return;
1069 }
1070
Orit Wassermanc91e6812014-01-30 20:08:34 +02001071 new_size = xbzrle_cache_resize(value);
1072 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001073 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1074 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001075 return;
1076 }
1077
1078 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001079}
1080
1081int64_t qmp_query_migrate_cache_size(Error **errp)
1082{
1083 return migrate_xbzrle_cache_size();
1084}
1085
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001086void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001087{
Juan Quintelacab30142011-02-22 23:54:21 +01001088 MigrationState *s;
1089
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001090 if (value < 0) {
1091 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +01001092 }
Paolo Bonzini442773c2013-02-22 17:36:44 +01001093 if (value > SIZE_MAX) {
1094 value = SIZE_MAX;
1095 }
Juan Quintelacab30142011-02-22 23:54:21 +01001096
Juan Quintela17549e82011-10-05 13:50:43 +02001097 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001098 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +01001099 if (s->file) {
1100 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
1101 }
Juan Quintelacab30142011-02-22 23:54:21 +01001102}
1103
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001104void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001105{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001106 value *= 1e9;
1107 value = MAX(0, MIN(UINT64_MAX, value));
1108 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +00001109}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001110
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001111bool migrate_postcopy_ram(void)
1112{
1113 MigrationState *s;
1114
1115 s = migrate_get_current();
1116
1117 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM];
1118}
1119
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001120bool migrate_auto_converge(void)
1121{
1122 MigrationState *s;
1123
1124 s = migrate_get_current();
1125
1126 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1127}
1128
Peter Lieven323004a2013-07-18 09:48:50 +02001129bool migrate_zero_blocks(void)
1130{
1131 MigrationState *s;
1132
1133 s = migrate_get_current();
1134
1135 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1136}
1137
Liang Li8706d2d2015-03-23 16:32:17 +08001138bool migrate_use_compression(void)
1139{
Liang Lidde4e692015-03-23 16:32:26 +08001140 MigrationState *s;
1141
1142 s = migrate_get_current();
1143
1144 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001145}
1146
1147int migrate_compress_level(void)
1148{
1149 MigrationState *s;
1150
1151 s = migrate_get_current();
1152
Liang Li43c60a82015-03-23 16:32:27 +08001153 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +08001154}
1155
1156int migrate_compress_threads(void)
1157{
1158 MigrationState *s;
1159
1160 s = migrate_get_current();
1161
Liang Li43c60a82015-03-23 16:32:27 +08001162 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +08001163}
1164
Liang Li3fcb38c2015-03-23 16:32:18 +08001165int migrate_decompress_threads(void)
1166{
1167 MigrationState *s;
1168
1169 s = migrate_get_current();
1170
Liang Li43c60a82015-03-23 16:32:27 +08001171 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +08001172}
1173
Juan Quintelab05dc722015-07-07 14:44:05 +02001174bool migrate_use_events(void)
1175{
1176 MigrationState *s;
1177
1178 s = migrate_get_current();
1179
1180 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1181}
1182
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001183int migrate_use_xbzrle(void)
1184{
1185 MigrationState *s;
1186
1187 s = migrate_get_current();
1188
1189 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1190}
1191
1192int64_t migrate_xbzrle_cache_size(void)
1193{
1194 MigrationState *s;
1195
1196 s = migrate_get_current();
1197
1198 return s->xbzrle_cache_size;
1199}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001200
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001201/* migration thread support */
1202/*
1203 * Something bad happened to the RP stream, mark an error
1204 * The caller shall print or trace something to indicate why
1205 */
1206static void mark_source_rp_bad(MigrationState *s)
1207{
1208 s->rp_state.error = true;
1209}
1210
1211static struct rp_cmd_args {
1212 ssize_t len; /* -1 = variable */
1213 const char *name;
1214} rp_cmd_args[] = {
1215 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1216 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1217 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001218 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1219 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001220 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1221};
1222
1223/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001224 * Process a request for pages received on the return path,
1225 * We're allowed to send more than requested (e.g. to round to our page size)
1226 * and we don't need to send pages that have already been sent.
1227 */
1228static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1229 ram_addr_t start, size_t len)
1230{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001231 long our_host_ps = getpagesize();
1232
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001233 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001234
1235 /*
1236 * Since we currently insist on matching page sizes, just sanity check
1237 * we're being asked for whole host pages.
1238 */
1239 if (start & (our_host_ps-1) ||
1240 (len & (our_host_ps-1))) {
1241 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1242 " len: %zd", __func__, start, len);
1243 mark_source_rp_bad(ms);
1244 return;
1245 }
1246
1247 if (ram_save_queue_pages(ms, rbname, start, len)) {
1248 mark_source_rp_bad(ms);
1249 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001250}
1251
1252/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001253 * Handles messages sent on the return path towards the source VM
1254 *
1255 */
1256static void *source_return_path_thread(void *opaque)
1257{
1258 MigrationState *ms = opaque;
1259 QEMUFile *rp = ms->rp_state.from_dst_file;
1260 uint16_t header_len, header_type;
1261 const int max_len = 512;
1262 uint8_t buf[max_len];
1263 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001264 ram_addr_t start = 0; /* =0 to silence warning */
1265 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001266 int res;
1267
1268 trace_source_return_path_thread_entry();
1269 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1270 migration_is_setup_or_active(ms->state)) {
1271 trace_source_return_path_thread_loop_top();
1272 header_type = qemu_get_be16(rp);
1273 header_len = qemu_get_be16(rp);
1274
1275 if (header_type >= MIG_RP_MSG_MAX ||
1276 header_type == MIG_RP_MSG_INVALID) {
1277 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1278 header_type, header_len);
1279 mark_source_rp_bad(ms);
1280 goto out;
1281 }
1282
1283 if ((rp_cmd_args[header_type].len != -1 &&
1284 header_len != rp_cmd_args[header_type].len) ||
1285 header_len > max_len) {
1286 error_report("RP: Received '%s' message (0x%04x) with"
1287 "incorrect length %d expecting %zu",
1288 rp_cmd_args[header_type].name, header_type, header_len,
1289 (size_t)rp_cmd_args[header_type].len);
1290 mark_source_rp_bad(ms);
1291 goto out;
1292 }
1293
1294 /* We know we've got a valid header by this point */
1295 res = qemu_get_buffer(rp, buf, header_len);
1296 if (res != header_len) {
1297 error_report("RP: Failed reading data for message 0x%04x"
1298 " read %d expected %d",
1299 header_type, res, header_len);
1300 mark_source_rp_bad(ms);
1301 goto out;
1302 }
1303
1304 /* OK, we have the message and the data */
1305 switch (header_type) {
1306 case MIG_RP_MSG_SHUT:
1307 sibling_error = be32_to_cpup((uint32_t *)buf);
1308 trace_source_return_path_thread_shut(sibling_error);
1309 if (sibling_error) {
1310 error_report("RP: Sibling indicated error %d", sibling_error);
1311 mark_source_rp_bad(ms);
1312 }
1313 /*
1314 * We'll let the main thread deal with closing the RP
1315 * we could do a shutdown(2) on it, but we're the only user
1316 * anyway, so there's nothing gained.
1317 */
1318 goto out;
1319
1320 case MIG_RP_MSG_PONG:
1321 tmp32 = be32_to_cpup((uint32_t *)buf);
1322 trace_source_return_path_thread_pong(tmp32);
1323 break;
1324
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001325 case MIG_RP_MSG_REQ_PAGES:
1326 start = be64_to_cpup((uint64_t *)buf);
1327 len = be32_to_cpup((uint32_t *)(buf + 8));
1328 migrate_handle_rp_req_pages(ms, NULL, start, len);
1329 break;
1330
1331 case MIG_RP_MSG_REQ_PAGES_ID:
1332 expected_len = 12 + 1; /* header + termination */
1333
1334 if (header_len >= expected_len) {
1335 start = be64_to_cpup((uint64_t *)buf);
1336 len = be32_to_cpup((uint32_t *)(buf + 8));
1337 /* Now we expect an idstr */
1338 tmp32 = buf[12]; /* Length of the following idstr */
1339 buf[13 + tmp32] = '\0';
1340 expected_len += tmp32;
1341 }
1342 if (header_len != expected_len) {
1343 error_report("RP: Req_Page_id with length %d expecting %zd",
1344 header_len, expected_len);
1345 mark_source_rp_bad(ms);
1346 goto out;
1347 }
1348 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1349 break;
1350
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001351 default:
1352 break;
1353 }
1354 }
1355 if (rp && qemu_file_get_error(rp)) {
1356 trace_source_return_path_thread_bad_end();
1357 mark_source_rp_bad(ms);
1358 }
1359
1360 trace_source_return_path_thread_end();
1361out:
1362 ms->rp_state.from_dst_file = NULL;
1363 qemu_fclose(rp);
1364 return NULL;
1365}
1366
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001367static int open_return_path_on_source(MigrationState *ms)
1368{
1369
1370 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file);
1371 if (!ms->rp_state.from_dst_file) {
1372 return -1;
1373 }
1374
1375 trace_open_return_path_on_source();
1376 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1377 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1378
1379 trace_open_return_path_on_source_continue();
1380
1381 return 0;
1382}
1383
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001384/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1385static int await_return_path_close_on_source(MigrationState *ms)
1386{
1387 /*
1388 * If this is a normal exit then the destination will send a SHUT and the
1389 * rp_thread will exit, however if there's an error we need to cause
1390 * it to exit.
1391 */
1392 if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) {
1393 /*
1394 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1395 * waiting for the destination.
1396 */
1397 qemu_file_shutdown(ms->rp_state.from_dst_file);
1398 mark_source_rp_bad(ms);
1399 }
1400 trace_await_return_path_close_on_source_joining();
1401 qemu_thread_join(&ms->rp_state.rp_thread);
1402 trace_await_return_path_close_on_source_close();
1403 return ms->rp_state.error;
1404}
1405
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001406/*
1407 * Switch from normal iteration to postcopy
1408 * Returns non-0 on error
1409 */
1410static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1411{
1412 int ret;
1413 const QEMUSizedBuffer *qsb;
1414 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1415 migrate_set_state(ms, MIGRATION_STATUS_ACTIVE,
1416 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1417
1418 trace_postcopy_start();
1419 qemu_mutex_lock_iothread();
1420 trace_postcopy_start_set_run();
1421
1422 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1423 *old_vm_running = runstate_is_running();
1424 global_state_store();
1425 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1426
1427 if (ret < 0) {
1428 goto fail;
1429 }
1430
1431 /*
1432 * in Finish migrate and with the io-lock held everything should
1433 * be quiet, but we've potentially still got dirty pages and we
1434 * need to tell the destination to throw any pages it's already received
1435 * that are dirty
1436 */
1437 if (ram_postcopy_send_discard_bitmap(ms)) {
1438 error_report("postcopy send discard bitmap failed");
1439 goto fail;
1440 }
1441
1442 /*
1443 * send rest of state - note things that are doing postcopy
1444 * will notice we're in POSTCOPY_ACTIVE and not actually
1445 * wrap their state up here
1446 */
1447 qemu_file_set_rate_limit(ms->file, INT64_MAX);
1448 /* Ping just for debugging, helps line traces up */
1449 qemu_savevm_send_ping(ms->file, 2);
1450
1451 /*
1452 * While loading the device state we may trigger page transfer
1453 * requests and the fd must be free to process those, and thus
1454 * the destination must read the whole device state off the fd before
1455 * it starts processing it. Unfortunately the ad-hoc migration format
1456 * doesn't allow the destination to know the size to read without fully
1457 * parsing it through each devices load-state code (especially the open
1458 * coded devices that use get/put).
1459 * So we wrap the device state up in a package with a length at the start;
1460 * to do this we use a qemu_buf to hold the whole of the device state.
1461 */
1462 QEMUFile *fb = qemu_bufopen("w", NULL);
1463 if (!fb) {
1464 error_report("Failed to create buffered file");
1465 goto fail;
1466 }
1467
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001468 /*
1469 * Make sure the receiver can get incoming pages before we send the rest
1470 * of the state
1471 */
1472 qemu_savevm_send_postcopy_listen(fb);
1473
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001474 qemu_savevm_state_complete_precopy(fb);
1475 qemu_savevm_send_ping(fb, 3);
1476
1477 qemu_savevm_send_postcopy_run(fb);
1478
1479 /* <><> end of stuff going into the package */
1480 qsb = qemu_buf_get(fb);
1481
1482 /* Now send that blob */
1483 if (qemu_savevm_send_packaged(ms->file, qsb)) {
1484 goto fail_closefb;
1485 }
1486 qemu_fclose(fb);
1487 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1488
1489 qemu_mutex_unlock_iothread();
1490
1491 /*
1492 * Although this ping is just for debug, it could potentially be
1493 * used for getting a better measurement of downtime at the source.
1494 */
1495 qemu_savevm_send_ping(ms->file, 4);
1496
1497 ret = qemu_file_get_error(ms->file);
1498 if (ret) {
1499 error_report("postcopy_start: Migration stream errored");
1500 migrate_set_state(ms, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1501 MIGRATION_STATUS_FAILED);
1502 }
1503
1504 return ret;
1505
1506fail_closefb:
1507 qemu_fclose(fb);
1508fail:
1509 migrate_set_state(ms, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1510 MIGRATION_STATUS_FAILED);
1511 qemu_mutex_unlock_iothread();
1512 return -1;
1513}
1514
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001515/**
1516 * migration_completion: Used by migration_thread when there's not much left.
1517 * The caller 'breaks' the loop when this returns.
1518 *
1519 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001520 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001521 * @*old_vm_running: Pointer to old_vm_running flag
1522 * @*start_time: Pointer to time to update
1523 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001524static void migration_completion(MigrationState *s, int current_active_state,
1525 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001526 int64_t *start_time)
1527{
1528 int ret;
1529
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001530 if (s->state == MIGRATION_STATUS_ACTIVE) {
1531 qemu_mutex_lock_iothread();
1532 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1533 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1534 *old_vm_running = runstate_is_running();
1535 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001536
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001537 if (!ret) {
1538 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1539 if (ret >= 0) {
1540 qemu_file_set_rate_limit(s->file, INT64_MAX);
1541 qemu_savevm_state_complete_precopy(s->file);
1542 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001543 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001544 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001545
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001546 if (ret < 0) {
1547 goto fail;
1548 }
1549 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1550 trace_migration_completion_postcopy_end();
1551
1552 qemu_savevm_state_complete_postcopy(s->file);
1553 trace_migration_completion_postcopy_end_after_complete();
1554 }
1555
1556 /*
1557 * If rp was opened we must clean up the thread before
1558 * cleaning everything else up (since if there are no failures
1559 * it will wait for the destination to send it's status in
1560 * a SHUT command).
1561 * Postcopy opens rp if enabled (even if it's not avtivated)
1562 */
1563 if (migrate_postcopy_ram()) {
1564 int rp_error;
1565 trace_migration_completion_postcopy_end_before_rp();
1566 rp_error = await_return_path_close_on_source(s);
1567 trace_migration_completion_postcopy_end_after_rp(rp_error);
1568 if (rp_error) {
1569 goto fail;
1570 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001571 }
1572
1573 if (qemu_file_get_error(s->file)) {
1574 trace_migration_completion_file_err();
1575 goto fail;
1576 }
1577
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001578 migrate_set_state(s, current_active_state, MIGRATION_STATUS_COMPLETED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001579 return;
1580
1581fail:
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001582 migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001583}
1584
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001585/*
1586 * Master migration thread on the source VM.
1587 * It drives the migration and pumps the data down the outgoing channel.
1588 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001589static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001590{
Juan Quintela9848a402012-12-19 09:55:50 +01001591 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001592 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001593 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1594 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001595 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001596 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001597 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001598 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001599 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001600 bool entered_postcopy = false;
1601 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1602 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
Juan Quintela76f59332012-10-03 20:16:24 +02001603
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001604 rcu_register_thread();
1605
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001606 qemu_savevm_state_header(s->file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001607
1608 if (migrate_postcopy_ram()) {
1609 /* Now tell the dest that it should open its end so it can reply */
1610 qemu_savevm_send_open_return_path(s->file);
1611
1612 /* And do a ping that will make stuff easier to debug */
1613 qemu_savevm_send_ping(s->file, 1);
1614
1615 /*
1616 * Tell the destination that we *might* want to do postcopy later;
1617 * if the other end can't do postcopy it should fail now, nice and
1618 * early.
1619 */
1620 qemu_savevm_send_postcopy_advise(s->file);
1621 }
1622
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001623 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001624
Alex Blighbc72ad62013-08-21 16:03:08 +01001625 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001626 current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang31194732015-03-13 16:08:38 +08001627 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001628
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001629 trace_migration_thread_setup_complete();
1630
1631 while (s->state == MIGRATION_STATUS_ACTIVE ||
1632 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001633 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001634 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001635
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001636 if (!qemu_file_rate_limit(s->file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001637 uint64_t pend_post, pend_nonpost;
1638
1639 qemu_savevm_state_pending(s->file, max_size, &pend_nonpost,
1640 &pend_post);
1641 pending_size = pend_nonpost + pend_post;
1642 trace_migrate_pending(pending_size, max_size,
1643 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001644 if (pending_size && pending_size >= max_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001645 /* Still a significant amount to transfer */
1646
1647 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1648 if (migrate_postcopy_ram() &&
1649 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1650 pend_nonpost <= max_size &&
1651 atomic_read(&s->start_postcopy)) {
1652
1653 if (!postcopy_start(s, &old_vm_running)) {
1654 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1655 entered_postcopy = true;
1656 }
1657
1658 continue;
1659 }
1660 /* Just another iteration step */
Dr. David Alan Gilbert35ecd942015-11-05 18:11:14 +00001661 qemu_savevm_state_iterate(s->file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001662 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001663 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001664 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001665 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001666 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001667 }
1668 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001669
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001670 if (qemu_file_get_error(s->file)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001671 migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED);
1672 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001673 break;
1674 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001675 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001676 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001677 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001678 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001679 double bandwidth = transferred_bytes / time_spent;
1680 max_size = bandwidth * migrate_max_downtime() / 1000000;
1681
Michael R. Hines7e114f82013-06-25 21:35:30 -04001682 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1683 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1684
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001685 trace_migrate_transferred(transferred_bytes, time_spent,
1686 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001687 /* if we haven't sent anything, we don't want to recalculate
1688 10000 is a small enough number for our purposes */
1689 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1690 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1691 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001692
Paolo Bonzini1964a392013-02-22 17:36:45 +01001693 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001694 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001695 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001696 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001697 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001698 /* usleep expects microseconds */
1699 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1700 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001701 }
1702
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001703 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001704 /* If we enabled cpu throttling for auto-converge, turn it off. */
1705 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001706 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001707
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001708 qemu_mutex_lock_iothread();
Liang Liea7415f2015-11-02 15:37:01 +08001709 qemu_savevm_state_cleanup();
zhanghailiang31194732015-03-13 16:08:38 +08001710 if (s->state == MIGRATION_STATUS_COMPLETED) {
Peter Lievend6ed7312014-05-12 10:46:00 +02001711 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001712 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001713 if (!entered_postcopy) {
1714 s->downtime = end_time - start_time;
1715 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001716 if (s->total_time) {
1717 s->mbps = (((double) transferred_bytes * 8.0) /
1718 ((double) s->total_time)) / 1000;
1719 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001720 runstate_set(RUN_STATE_POSTMIGRATE);
1721 } else {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001722 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001723 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001724 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001725 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001726 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001727 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001728
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001729 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001730 return NULL;
1731}
1732
Juan Quintela9848a402012-12-19 09:55:50 +01001733void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001734{
Juan Quintelacc283e32013-02-01 11:12:26 +01001735 /* This is a best 1st approximation. ns to ms */
1736 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001737 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001738
Paolo Bonzini442773c2013-02-22 17:36:44 +01001739 qemu_file_set_rate_limit(s->file,
1740 s->bandwidth_limit / XFER_LIMIT_RATIO);
1741
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001742 /* Notify before starting migration thread */
1743 notifier_list_notify(&migration_state_notifiers, s);
1744
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001745 /*
1746 * Open the return path; currently for postcopy but other things might
1747 * also want it.
1748 */
1749 if (migrate_postcopy_ram()) {
1750 if (open_return_path_on_source(s)) {
1751 error_report("Unable to open return-path for postcopy");
1752 migrate_set_state(s, MIGRATION_STATUS_SETUP,
1753 MIGRATION_STATUS_FAILED);
1754 migrate_fd_cleanup(s);
1755 return;
1756 }
1757 }
1758
Liang Li8706d2d2015-03-23 16:32:17 +08001759 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001760 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001761 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001762 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001763}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001764
1765PostcopyState postcopy_state_get(void)
1766{
1767 return atomic_mb_read(&incoming_postcopy_state);
1768}
1769
1770/* Set the state and return the old state */
1771PostcopyState postcopy_state_set(PostcopyState new_state)
1772{
1773 return atomic_xchg(&incoming_postcopy_state, new_state);
1774}
1775