blob: bc611e453bd54405989a9f438f0568ab3e07b7ff [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;
zhanghailiang93d7af62015-12-16 11:47:34 +0000114 mis_current->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100115 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000116 qemu_mutex_init(&mis_current->rp_mutex);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000117 qemu_event_init(&mis_current->main_thread_load_event, false);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100118
119 return mis_current;
120}
121
122void migration_incoming_state_destroy(void)
123{
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000124 qemu_event_destroy(&mis_current->main_thread_load_event);
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100125 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100126 g_free(mis_current);
127 mis_current = NULL;
128}
129
Juan Quinteladf4b1022014-10-08 10:58:10 +0200130
131typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200132 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200133 uint32_t size;
134 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200135 RunState state;
136 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200137} GlobalState;
138
139static GlobalState global_state;
140
Juan Quintela560d0272015-07-15 09:53:46 +0200141int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200142{
143 if (!runstate_store((char *)global_state.runstate,
144 sizeof(global_state.runstate))) {
145 error_report("runstate name too big: %s", global_state.runstate);
146 trace_migrate_state_too_big();
147 return -EINVAL;
148 }
149 return 0;
150}
151
Anthony PERARDc69adea2015-08-03 15:29:19 +0100152void global_state_store_running(void)
153{
154 const char *state = RunState_lookup[RUN_STATE_RUNNING];
155 strncpy((char *)global_state.runstate,
156 state, sizeof(global_state.runstate));
157}
158
Juan Quintela172c4352015-07-08 13:56:26 +0200159static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200160{
Juan Quintela172c4352015-07-08 13:56:26 +0200161 return global_state.received;
162}
163
164static RunState global_state_get_runstate(void)
165{
166 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200167}
168
Juan Quintela13d16812014-10-08 13:58:24 +0200169void global_state_set_optional(void)
170{
171 global_state.optional = true;
172}
173
174static bool global_state_needed(void *opaque)
175{
176 GlobalState *s = opaque;
177 char *runstate = (char *)s->runstate;
178
179 /* If it is not optional, it is mandatory */
180
181 if (s->optional == false) {
182 return true;
183 }
184
185 /* If state is running or paused, it is not needed */
186
187 if (strcmp(runstate, "running") == 0 ||
188 strcmp(runstate, "paused") == 0) {
189 return false;
190 }
191
192 /* for any other state it is needed */
193 return true;
194}
195
Juan Quinteladf4b1022014-10-08 10:58:10 +0200196static int global_state_post_load(void *opaque, int version_id)
197{
198 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200199 Error *local_err = NULL;
200 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200201 char *runstate = (char *)s->runstate;
202
Juan Quintela172c4352015-07-08 13:56:26 +0200203 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200204 trace_migrate_global_state_post_load(runstate);
205
Eric Blake7fb1cf12015-11-18 01:52:57 -0700206 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200207 -1, &local_err);
208
Juan Quintela172c4352015-07-08 13:56:26 +0200209 if (r == -1) {
210 if (local_err) {
211 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200212 }
Juan Quintela172c4352015-07-08 13:56:26 +0200213 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200214 }
Juan Quintela172c4352015-07-08 13:56:26 +0200215 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200216
Juan Quintela172c4352015-07-08 13:56:26 +0200217 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200218}
219
220static void global_state_pre_save(void *opaque)
221{
222 GlobalState *s = opaque;
223
224 trace_migrate_global_state_pre_save((char *)s->runstate);
225 s->size = strlen((char *)s->runstate) + 1;
226}
227
228static const VMStateDescription vmstate_globalstate = {
229 .name = "globalstate",
230 .version_id = 1,
231 .minimum_version_id = 1,
232 .post_load = global_state_post_load,
233 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200234 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200235 .fields = (VMStateField[]) {
236 VMSTATE_UINT32(size, GlobalState),
237 VMSTATE_BUFFER(runstate, GlobalState),
238 VMSTATE_END_OF_LIST()
239 },
240};
241
242void register_global_state(void)
243{
244 /* We would use it independently that we receive it */
245 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200246 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200247 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
248}
249
Juan Quintelab05dc722015-07-07 14:44:05 +0200250static void migrate_generate_event(int new_state)
251{
252 if (migrate_use_events()) {
253 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200254 }
255}
256
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000257/*
258 * Called on -incoming with a defer: uri.
259 * The migration can be started later after any parameters have been
260 * changed.
261 */
262static void deferred_incoming_migration(Error **errp)
263{
264 if (deferred_incoming) {
265 error_setg(errp, "Incoming migration already deferred");
266 }
267 deferred_incoming = true;
268}
269
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000270/* Request a range of pages from the source VM at the given
271 * start address.
272 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
273 * as the last request (a name must have been given previously)
274 * Start: Address offset within the RB
275 * Len: Length in bytes required - must be a multiple of pagesize
276 */
277void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
278 ram_addr_t start, size_t len)
279{
280 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname upto 256 */
281 size_t msglen = 12; /* start + len */
282
283 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
284 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
285
286 if (rbname) {
287 int rbname_len = strlen(rbname);
288 assert(rbname_len < 256);
289
290 bufc[msglen++] = rbname_len;
291 memcpy(bufc + msglen, rbname, rbname_len);
292 msglen += rbname_len;
293 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
294 } else {
295 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
296 }
297}
298
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200299void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000300{
aliguori34c9dd82008-10-13 03:14:31 +0000301 const char *p;
302
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200303 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000304 if (!strcmp(uri, "defer")) {
305 deferred_incoming_migration(errp);
306 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200307 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400308#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000309 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400310 rdma_start_incoming_migration(p, errp);
311#endif
aliguori065e2812008-11-11 16:46:33 +0000312#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000313 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200314 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000315 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200316 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000317 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200318 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000319#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000320 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100321 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200322 }
aliguori5bb79102008-10-13 03:12:02 +0000323}
324
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200325static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200326{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200327 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100328 Error *local_err = NULL;
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000329 MigrationIncomingState *mis;
330 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200331 int ret;
332
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000333 mis = migration_incoming_state_new(f);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000334 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000335 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
336 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200337 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100338
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000339 ps = postcopy_state_get();
340 trace_process_incoming_migration_co_end(ret, ps);
341 if (ps != POSTCOPY_INCOMING_NONE) {
342 if (ps == POSTCOPY_INCOMING_ADVISE) {
343 /*
344 * Where a migration had postcopy enabled (and thus went to advise)
345 * but managed to complete within the precopy period, we can use
346 * the normal exit.
347 */
348 postcopy_ram_incoming_cleanup(mis);
349 } else if (ret >= 0) {
350 /*
351 * Postcopy was started, cleanup should happen at the end of the
352 * postcopy thread.
353 */
354 trace_process_incoming_migration_co_postcopy_end_main();
355 return;
356 }
357 /* Else if something went wrong then just fall out of the normal exit */
358 }
359
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200360 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200361 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100362
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200363 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000364 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
365 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200366 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800367 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600368 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200369 }
Juan Quintela511c0232010-06-09 14:10:55 +0200370
Anthony Liguori0f154232011-11-14 15:09:45 -0600371 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100372 bdrv_invalidate_cache_all(&local_err);
373 if (local_err) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000374 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
375 MIGRATION_STATUS_FAILED);
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100376 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800377 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100378 exit(EXIT_FAILURE);
379 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600380
Amit Shah92e37622015-10-14 17:37:19 +0530381 /*
382 * This must happen after all error conditions are dealt with and
383 * we're sure the VM is going to be running on this host.
384 */
385 qemu_announce_self();
386
Juan Quintela172c4352015-07-08 13:56:26 +0200387 /* If global state section was not received or we are in running
388 state, we need to obey autostart. Any other state is set with
389 runstate_set. */
Juan Quinteladf4b1022014-10-08 10:58:10 +0200390
Juan Quintela172c4352015-07-08 13:56:26 +0200391 if (!global_state_received() ||
392 global_state_get_runstate() == RUN_STATE_RUNNING) {
Juan Quinteladf4b1022014-10-08 10:58:10 +0200393 if (autostart) {
394 vm_start();
395 } else {
396 runstate_set(RUN_STATE_PAUSED);
397 }
Juan Quintela172c4352015-07-08 13:56:26 +0200398 } else {
399 runstate_set(global_state_get_runstate());
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300400 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800401 migrate_decompress_threads_join();
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100402 /*
403 * This must happen after any state changes since as soon as an external
404 * observer sees this event they might start to prod at the VM assuming
405 * it's ready to use.
406 */
zhanghailiang93d7af62015-12-16 11:47:34 +0000407 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
408 MIGRATION_STATUS_COMPLETED);
409 migration_incoming_state_destroy();
Juan Quintela511c0232010-06-09 14:10:55 +0200410}
411
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200412void process_incoming_migration(QEMUFile *f)
413{
414 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
415 int fd = qemu_get_fd(f);
416
417 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800418 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100419 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200420 qemu_coroutine_enter(co, f);
421}
422
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000423/*
424 * Send a message on the return channel back to the source
425 * of the migration.
426 */
427void migrate_send_rp_message(MigrationIncomingState *mis,
428 enum mig_rp_message_type message_type,
429 uint16_t len, void *data)
430{
431 trace_migrate_send_rp_message((int)message_type, len);
432 qemu_mutex_lock(&mis->rp_mutex);
433 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
434 qemu_put_be16(mis->to_src_file, len);
435 qemu_put_buffer(mis->to_src_file, data, len);
436 qemu_fflush(mis->to_src_file);
437 qemu_mutex_unlock(&mis->rp_mutex);
438}
439
440/*
441 * Send a 'SHUT' message on the return channel with the given value
442 * to indicate that we've finished with the RP. Non-0 value indicates
443 * error.
444 */
445void migrate_send_rp_shut(MigrationIncomingState *mis,
446 uint32_t value)
447{
448 uint32_t buf;
449
450 buf = cpu_to_be32(value);
451 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
452}
453
454/*
455 * Send a 'PONG' message on the return channel with the given value
456 * (normally in response to a 'PING')
457 */
458void migrate_send_rp_pong(MigrationIncomingState *mis,
459 uint32_t value)
460{
461 uint32_t buf;
462
463 buf = cpu_to_be32(value);
464 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
465}
466
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400467/* amount of nanoseconds we are willing to wait for migration to be down.
468 * the choice of nanoseconds is because it is the maximum resolution that
469 * get_clock() can achieve. It is an internal measure. All user-visible
470 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100471static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400472
473uint64_t migrate_max_downtime(void)
474{
475 return max_downtime;
476}
477
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300478MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
479{
480 MigrationCapabilityStatusList *head = NULL;
481 MigrationCapabilityStatusList *caps;
482 MigrationState *s = migrate_get_current();
483 int i;
484
Michael Tokarev387eede2013-10-05 13:18:28 +0400485 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700486 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300487 if (head == NULL) {
488 head = g_malloc0(sizeof(*caps));
489 caps = head;
490 } else {
491 caps->next = g_malloc0(sizeof(*caps));
492 caps = caps->next;
493 }
494 caps->value =
495 g_malloc(sizeof(*caps->value));
496 caps->value->capability = i;
497 caps->value->state = s->enabled_capabilities[i];
498 }
499
500 return head;
501}
502
Liang Li85de8322015-03-23 16:32:28 +0800503MigrationParameters *qmp_query_migrate_parameters(Error **errp)
504{
505 MigrationParameters *params;
506 MigrationState *s = migrate_get_current();
507
508 params = g_malloc0(sizeof(*params));
509 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
510 params->compress_threads =
511 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
512 params->decompress_threads =
513 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400514 params->x_cpu_throttle_initial =
515 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
516 params->x_cpu_throttle_increment =
517 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Liang Li85de8322015-03-23 16:32:28 +0800518
519 return params;
520}
521
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000522/*
523 * Return true if we're already in the middle of a migration
524 * (i.e. any of the active or setup states)
525 */
526static bool migration_is_setup_or_active(int state)
527{
528 switch (state) {
529 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000530 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000531 case MIGRATION_STATUS_SETUP:
532 return true;
533
534 default:
535 return false;
536
537 }
538}
539
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300540static void get_xbzrle_cache_stats(MigrationInfo *info)
541{
542 if (migrate_use_xbzrle()) {
543 info->has_xbzrle_cache = true;
544 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
545 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
546 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
547 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
548 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800549 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300550 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
551 }
552}
553
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300554MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000555{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300556 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200557 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000558
Juan Quintela17549e82011-10-05 13:50:43 +0200559 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800560 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200561 /* no migration has happened ever */
562 break;
zhanghailiang31194732015-03-13 16:08:38 +0800563 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400564 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400565 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400566 break;
zhanghailiang31194732015-03-13 16:08:38 +0800567 case MIGRATION_STATUS_ACTIVE:
568 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300569 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200570 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100571 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200572 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200573 info->has_expected_downtime = true;
574 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400575 info->has_setup_time = true;
576 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200577
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300578 info->has_ram = true;
579 info->ram = g_malloc0(sizeof(*info->ram));
580 info->ram->transferred = ram_bytes_transferred();
581 info->ram->remaining = ram_bytes_remaining();
582 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300583 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100584 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300585 info->ram->normal = norm_mig_pages_transferred();
586 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200587 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400588 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800589 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200590
Juan Quintela17549e82011-10-05 13:50:43 +0200591 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300592 info->has_disk = true;
593 info->disk = g_malloc0(sizeof(*info->disk));
594 info->disk->transferred = blk_mig_bytes_transferred();
595 info->disk->remaining = blk_mig_bytes_remaining();
596 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000597 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300598
Jason J. Herne47828932015-09-08 13:12:36 -0400599 if (cpu_throttle_active()) {
600 info->has_x_cpu_throttle_percentage = true;
601 info->x_cpu_throttle_percentage = cpu_throttle_get_percentage();
602 }
603
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300604 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200605 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000606 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
607 /* Mostly the same as active; TODO add some postcopy stats */
608 info->has_status = true;
609 info->has_total_time = true;
610 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
611 - s->total_time;
612 info->has_expected_downtime = true;
613 info->expected_downtime = s->expected_downtime;
614 info->has_setup_time = true;
615 info->setup_time = s->setup_time;
616
617 info->has_ram = true;
618 info->ram = g_malloc0(sizeof(*info->ram));
619 info->ram->transferred = ram_bytes_transferred();
620 info->ram->remaining = ram_bytes_remaining();
621 info->ram->total = ram_bytes_total();
622 info->ram->duplicate = dup_mig_pages_transferred();
623 info->ram->skipped = skipped_mig_pages_transferred();
624 info->ram->normal = norm_mig_pages_transferred();
625 info->ram->normal_bytes = norm_mig_bytes_transferred();
626 info->ram->dirty_pages_rate = s->dirty_pages_rate;
627 info->ram->mbps = s->mbps;
628
629 if (blk_mig_active()) {
630 info->has_disk = true;
631 info->disk = g_malloc0(sizeof(*info->disk));
632 info->disk->transferred = blk_mig_bytes_transferred();
633 info->disk->remaining = blk_mig_bytes_remaining();
634 info->disk->total = blk_mig_bytes_total();
635 }
636
637 get_xbzrle_cache_stats(info);
638 break;
zhanghailiang31194732015-03-13 16:08:38 +0800639 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300640 get_xbzrle_cache_stats(info);
641
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300642 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900643 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200644 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200645 info->has_downtime = true;
646 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400647 info->has_setup_time = true;
648 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200649
650 info->has_ram = true;
651 info->ram = g_malloc0(sizeof(*info->ram));
652 info->ram->transferred = ram_bytes_transferred();
653 info->ram->remaining = 0;
654 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300655 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100656 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300657 info->ram->normal = norm_mig_pages_transferred();
658 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400659 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800660 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200661 break;
zhanghailiang31194732015-03-13 16:08:38 +0800662 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300663 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200664 break;
zhanghailiang31194732015-03-13 16:08:38 +0800665 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300666 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200667 break;
aliguori5bb79102008-10-13 03:12:02 +0000668 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800669 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300670
671 return info;
aliguori5bb79102008-10-13 03:12:02 +0000672}
673
Orit Wasserman00458432012-08-06 21:42:48 +0300674void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
675 Error **errp)
676{
677 MigrationState *s = migrate_get_current();
678 MigrationCapabilityStatusList *cap;
679
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000680 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100681 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300682 return;
683 }
684
685 for (cap = params; cap; cap = cap->next) {
686 s->enabled_capabilities[cap->value->capability] = cap->value->state;
687 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000688
689 if (migrate_postcopy_ram()) {
690 if (migrate_use_compression()) {
691 /* The decompression threads asynchronously write into RAM
692 * rather than use the atomic copies needed to avoid
693 * userfaulting. It should be possible to fix the decompression
694 * threads for compatibility in future.
695 */
696 error_report("Postcopy is not currently compatible with "
697 "compression");
698 s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM] =
699 false;
700 }
701 }
Orit Wasserman00458432012-08-06 21:42:48 +0300702}
703
Liang Li85de8322015-03-23 16:32:28 +0800704void qmp_migrate_set_parameters(bool has_compress_level,
705 int64_t compress_level,
706 bool has_compress_threads,
707 int64_t compress_threads,
708 bool has_decompress_threads,
Jason J. Herne1626fee2015-09-08 13:12:34 -0400709 int64_t decompress_threads,
710 bool has_x_cpu_throttle_initial,
711 int64_t x_cpu_throttle_initial,
712 bool has_x_cpu_throttle_increment,
713 int64_t x_cpu_throttle_increment, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800714{
715 MigrationState *s = migrate_get_current();
716
717 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100718 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
719 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800720 return;
721 }
722 if (has_compress_threads &&
723 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100724 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
725 "compress_threads",
726 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800727 return;
728 }
729 if (has_decompress_threads &&
730 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100731 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
732 "decompress_threads",
733 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800734 return;
735 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400736 if (has_x_cpu_throttle_initial &&
737 (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
738 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
739 "x_cpu_throttle_initial",
740 "an integer in the range of 1 to 99");
741 }
742 if (has_x_cpu_throttle_increment &&
743 (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
744 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
745 "x_cpu_throttle_increment",
746 "an integer in the range of 1 to 99");
747 }
Liang Li85de8322015-03-23 16:32:28 +0800748
749 if (has_compress_level) {
750 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
751 }
752 if (has_compress_threads) {
753 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
754 }
755 if (has_decompress_threads) {
756 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
757 decompress_threads;
758 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400759 if (has_x_cpu_throttle_initial) {
760 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
761 x_cpu_throttle_initial;
762 }
763
764 if (has_x_cpu_throttle_increment) {
765 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
766 x_cpu_throttle_increment;
767 }
Liang Li85de8322015-03-23 16:32:28 +0800768}
769
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000770void qmp_migrate_start_postcopy(Error **errp)
771{
772 MigrationState *s = migrate_get_current();
773
774 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000775 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000776 " the start of migration");
777 return;
778 }
779
780 if (s->state == MIGRATION_STATUS_NONE) {
781 error_setg(errp, "Postcopy must be started after migration has been"
782 " started");
783 return;
784 }
785 /*
786 * we don't error if migration has finished since that would be racy
787 * with issuing this command.
788 */
789 atomic_set(&s->start_postcopy, true);
790}
791
aliguori065e2812008-11-11 16:46:33 +0000792/* shared migration helpers */
793
zhanghailiang48781e52015-12-16 11:47:33 +0000794void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000795{
zhanghailiang48781e52015-12-16 11:47:33 +0000796 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200797 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200798 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000799 }
800}
801
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100802static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000803{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100804 MigrationState *s = opaque;
805
806 qemu_bh_delete(s->cleanup_bh);
807 s->cleanup_bh = NULL;
808
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000809 flush_page_queue(s);
810
aliguori065e2812008-11-11 16:46:33 +0000811 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100812 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100813 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000814 if (s->migration_thread_running) {
815 qemu_thread_join(&s->thread);
816 s->migration_thread_running = false;
817 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100818 qemu_mutex_lock_iothread();
819
Liang Li8706d2d2015-03-23 16:32:17 +0800820 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100821 qemu_fclose(s->file);
822 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000823 }
824
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000825 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
826 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100827
Liang Li94f5a432015-11-02 15:37:00 +0800828 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000829 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800830 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100831 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100832
833 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000834}
835
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200836void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000837{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100838 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100839 assert(s->file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000840 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
841 MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100842 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100843}
844
Juan Quintela0edda1c2010-05-11 16:28:39 +0200845static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000846{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000847 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000848 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100849 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000850
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000851 if (s->rp_state.from_dst_file) {
852 /* shutdown the rp socket, so causing the rp thread to shutdown */
853 qemu_file_shutdown(s->rp_state.from_dst_file);
854 }
855
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000856 do {
857 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000858 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000859 break;
860 }
zhanghailiang48781e52015-12-16 11:47:33 +0000861 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +0800862 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000863
864 /*
865 * If we're unlucky the migration code might be stuck somewhere in a
866 * send/write while the network has failed and is waiting to timeout;
867 * if we've got shutdown(2) available then we can force it to quit.
868 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
869 * called in a bh, so there is no race against this cancel.
870 */
zhanghailiang31194732015-03-13 16:08:38 +0800871 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000872 qemu_file_shutdown(f);
873 }
aliguori065e2812008-11-11 16:46:33 +0000874}
875
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100876void add_migration_state_change_notifier(Notifier *notify)
877{
878 notifier_list_add(&migration_state_notifiers, notify);
879}
880
881void remove_migration_state_change_notifier(Notifier *notify)
882{
Paolo Bonzini31552522012-01-13 17:34:01 +0100883 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100884}
885
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200886bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200887{
zhanghailiang31194732015-03-13 16:08:38 +0800888 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200889}
890
Juan Quintela70736932011-02-23 00:43:59 +0100891bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100892{
zhanghailiang31194732015-03-13 16:08:38 +0800893 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100894}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200895
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200896bool migration_has_failed(MigrationState *s)
897{
zhanghailiang31194732015-03-13 16:08:38 +0800898 return (s->state == MIGRATION_STATUS_CANCELLED ||
899 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200900}
901
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000902bool migration_in_postcopy(MigrationState *s)
903{
904 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
905}
906
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +0000907MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200908{
Juan Quintela17549e82011-10-05 13:50:43 +0200909 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300910
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000911 /*
912 * Reinitialise all migration state, except
913 * parameters/capabilities that the user set, and
914 * locks.
915 */
916 s->bytes_xfer = 0;
917 s->xfer_limit = 0;
918 s->cleanup_bh = 0;
919 s->file = NULL;
920 s->state = MIGRATION_STATUS_NONE;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300921 s->params = *params;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000922 s->rp_state.from_dst_file = NULL;
923 s->rp_state.error = false;
924 s->mbps = 0.0;
925 s->downtime = 0;
926 s->expected_downtime = 0;
927 s->dirty_pages_rate = 0;
928 s->dirty_bytes_rate = 0;
929 s->setup_time = 0;
930 s->dirty_sync_count = 0;
931 s->start_postcopy = false;
932 s->migration_thread_running = false;
933 s->last_req_rb = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +0100934
zhanghailiang48781e52015-12-16 11:47:33 +0000935 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200936
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000937 QSIMPLEQ_INIT(&s->src_page_requests);
938
Alex Blighbc72ad62013-08-21 16:03:08 +0100939 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200940 return s;
941}
Juan Quintelacab30142011-02-22 23:54:21 +0100942
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600943static GSList *migration_blockers;
944
945void migrate_add_blocker(Error *reason)
946{
947 migration_blockers = g_slist_prepend(migration_blockers, reason);
948}
949
950void migrate_del_blocker(Error *reason)
951{
952 migration_blockers = g_slist_remove(migration_blockers, reason);
953}
954
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000955void qmp_migrate_incoming(const char *uri, Error **errp)
956{
957 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000958 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000959
960 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000961 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000962 return;
963 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000964 if (!once) {
965 error_setg(errp, "The incoming migration has already been started");
966 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000967
968 qemu_start_incoming_migration(uri, &local_err);
969
970 if (local_err) {
971 error_propagate(errp, local_err);
972 return;
973 }
974
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000975 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000976}
977
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200978void qmp_migrate(const char *uri, bool has_blk, bool blk,
979 bool has_inc, bool inc, bool has_detach, bool detach,
980 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100981{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200982 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200983 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300984 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100985 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100986
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900987 params.blk = has_blk && blk;
988 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300989
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000990 if (migration_is_setup_or_active(s->state) ||
zhanghailiang31194732015-03-13 16:08:38 +0800991 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100992 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200993 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100994 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100995 if (runstate_check(RUN_STATE_INMIGRATE)) {
996 error_setg(errp, "Guest is waiting for an incoming migration");
997 return;
998 }
999
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001000 if (qemu_savevm_state_blocked(errp)) {
1001 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001002 }
1003
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001004 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001005 *errp = error_copy(migration_blockers->data);
1006 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001007 }
1008
Juan Quintela656a2332015-07-01 09:32:29 +02001009 /* We are starting a new migration, so we want to start in a clean
1010 state. This change is only needed if previous migration
1011 failed/was cancelled. We don't use migrate_set_state() because
1012 we are setting the initial state, not changing it. */
1013 s->state = MIGRATION_STATUS_NONE;
1014
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001015 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +01001016
1017 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001018 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001019#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001020 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001021 rdma_start_outgoing_migration(s, p, &local_err);
1022#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001023#if !defined(WIN32)
1024 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001025 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001026 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001027 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001028 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001029 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001030#endif
1031 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001032 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1033 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001034 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1035 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001036 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001037 }
1038
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001039 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +02001040 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001041 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001042 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001043 }
Juan Quintelacab30142011-02-22 23:54:21 +01001044}
1045
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001046void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001047{
Juan Quintela17549e82011-10-05 13:50:43 +02001048 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001049}
1050
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001051void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1052{
1053 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001054 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001055
1056 /* Check for truncation */
1057 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001058 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1059 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001060 return;
1061 }
1062
Orit Wassermana5615b12014-01-30 20:08:36 +02001063 /* Cache should not be larger than guest ram size */
1064 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001065 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1066 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001067 return;
1068 }
1069
Orit Wassermanc91e6812014-01-30 20:08:34 +02001070 new_size = xbzrle_cache_resize(value);
1071 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001072 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1073 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001074 return;
1075 }
1076
1077 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001078}
1079
1080int64_t qmp_query_migrate_cache_size(Error **errp)
1081{
1082 return migrate_xbzrle_cache_size();
1083}
1084
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001085void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001086{
Juan Quintelacab30142011-02-22 23:54:21 +01001087 MigrationState *s;
1088
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001089 if (value < 0) {
1090 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +01001091 }
Paolo Bonzini442773c2013-02-22 17:36:44 +01001092 if (value > SIZE_MAX) {
1093 value = SIZE_MAX;
1094 }
Juan Quintelacab30142011-02-22 23:54:21 +01001095
Juan Quintela17549e82011-10-05 13:50:43 +02001096 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001097 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +01001098 if (s->file) {
1099 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
1100 }
Juan Quintelacab30142011-02-22 23:54:21 +01001101}
1102
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001103void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001104{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001105 value *= 1e9;
1106 value = MAX(0, MIN(UINT64_MAX, value));
1107 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +00001108}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001109
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001110bool migrate_postcopy_ram(void)
1111{
1112 MigrationState *s;
1113
1114 s = migrate_get_current();
1115
1116 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM];
1117}
1118
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001119bool migrate_auto_converge(void)
1120{
1121 MigrationState *s;
1122
1123 s = migrate_get_current();
1124
1125 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1126}
1127
Peter Lieven323004a2013-07-18 09:48:50 +02001128bool migrate_zero_blocks(void)
1129{
1130 MigrationState *s;
1131
1132 s = migrate_get_current();
1133
1134 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1135}
1136
Liang Li8706d2d2015-03-23 16:32:17 +08001137bool migrate_use_compression(void)
1138{
Liang Lidde4e692015-03-23 16:32:26 +08001139 MigrationState *s;
1140
1141 s = migrate_get_current();
1142
1143 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001144}
1145
1146int migrate_compress_level(void)
1147{
1148 MigrationState *s;
1149
1150 s = migrate_get_current();
1151
Liang Li43c60a82015-03-23 16:32:27 +08001152 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +08001153}
1154
1155int migrate_compress_threads(void)
1156{
1157 MigrationState *s;
1158
1159 s = migrate_get_current();
1160
Liang Li43c60a82015-03-23 16:32:27 +08001161 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +08001162}
1163
Liang Li3fcb38c2015-03-23 16:32:18 +08001164int migrate_decompress_threads(void)
1165{
1166 MigrationState *s;
1167
1168 s = migrate_get_current();
1169
Liang Li43c60a82015-03-23 16:32:27 +08001170 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +08001171}
1172
Juan Quintelab05dc722015-07-07 14:44:05 +02001173bool migrate_use_events(void)
1174{
1175 MigrationState *s;
1176
1177 s = migrate_get_current();
1178
1179 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1180}
1181
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001182int migrate_use_xbzrle(void)
1183{
1184 MigrationState *s;
1185
1186 s = migrate_get_current();
1187
1188 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1189}
1190
1191int64_t migrate_xbzrle_cache_size(void)
1192{
1193 MigrationState *s;
1194
1195 s = migrate_get_current();
1196
1197 return s->xbzrle_cache_size;
1198}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001199
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001200/* migration thread support */
1201/*
1202 * Something bad happened to the RP stream, mark an error
1203 * The caller shall print or trace something to indicate why
1204 */
1205static void mark_source_rp_bad(MigrationState *s)
1206{
1207 s->rp_state.error = true;
1208}
1209
1210static struct rp_cmd_args {
1211 ssize_t len; /* -1 = variable */
1212 const char *name;
1213} rp_cmd_args[] = {
1214 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1215 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1216 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001217 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1218 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001219 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1220};
1221
1222/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001223 * Process a request for pages received on the return path,
1224 * We're allowed to send more than requested (e.g. to round to our page size)
1225 * and we don't need to send pages that have already been sent.
1226 */
1227static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1228 ram_addr_t start, size_t len)
1229{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001230 long our_host_ps = getpagesize();
1231
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001232 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001233
1234 /*
1235 * Since we currently insist on matching page sizes, just sanity check
1236 * we're being asked for whole host pages.
1237 */
1238 if (start & (our_host_ps-1) ||
1239 (len & (our_host_ps-1))) {
1240 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1241 " len: %zd", __func__, start, len);
1242 mark_source_rp_bad(ms);
1243 return;
1244 }
1245
1246 if (ram_save_queue_pages(ms, rbname, start, len)) {
1247 mark_source_rp_bad(ms);
1248 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001249}
1250
1251/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001252 * Handles messages sent on the return path towards the source VM
1253 *
1254 */
1255static void *source_return_path_thread(void *opaque)
1256{
1257 MigrationState *ms = opaque;
1258 QEMUFile *rp = ms->rp_state.from_dst_file;
1259 uint16_t header_len, header_type;
1260 const int max_len = 512;
1261 uint8_t buf[max_len];
1262 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001263 ram_addr_t start = 0; /* =0 to silence warning */
1264 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001265 int res;
1266
1267 trace_source_return_path_thread_entry();
1268 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1269 migration_is_setup_or_active(ms->state)) {
1270 trace_source_return_path_thread_loop_top();
1271 header_type = qemu_get_be16(rp);
1272 header_len = qemu_get_be16(rp);
1273
1274 if (header_type >= MIG_RP_MSG_MAX ||
1275 header_type == MIG_RP_MSG_INVALID) {
1276 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1277 header_type, header_len);
1278 mark_source_rp_bad(ms);
1279 goto out;
1280 }
1281
1282 if ((rp_cmd_args[header_type].len != -1 &&
1283 header_len != rp_cmd_args[header_type].len) ||
1284 header_len > max_len) {
1285 error_report("RP: Received '%s' message (0x%04x) with"
1286 "incorrect length %d expecting %zu",
1287 rp_cmd_args[header_type].name, header_type, header_len,
1288 (size_t)rp_cmd_args[header_type].len);
1289 mark_source_rp_bad(ms);
1290 goto out;
1291 }
1292
1293 /* We know we've got a valid header by this point */
1294 res = qemu_get_buffer(rp, buf, header_len);
1295 if (res != header_len) {
1296 error_report("RP: Failed reading data for message 0x%04x"
1297 " read %d expected %d",
1298 header_type, res, header_len);
1299 mark_source_rp_bad(ms);
1300 goto out;
1301 }
1302
1303 /* OK, we have the message and the data */
1304 switch (header_type) {
1305 case MIG_RP_MSG_SHUT:
1306 sibling_error = be32_to_cpup((uint32_t *)buf);
1307 trace_source_return_path_thread_shut(sibling_error);
1308 if (sibling_error) {
1309 error_report("RP: Sibling indicated error %d", sibling_error);
1310 mark_source_rp_bad(ms);
1311 }
1312 /*
1313 * We'll let the main thread deal with closing the RP
1314 * we could do a shutdown(2) on it, but we're the only user
1315 * anyway, so there's nothing gained.
1316 */
1317 goto out;
1318
1319 case MIG_RP_MSG_PONG:
1320 tmp32 = be32_to_cpup((uint32_t *)buf);
1321 trace_source_return_path_thread_pong(tmp32);
1322 break;
1323
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001324 case MIG_RP_MSG_REQ_PAGES:
1325 start = be64_to_cpup((uint64_t *)buf);
1326 len = be32_to_cpup((uint32_t *)(buf + 8));
1327 migrate_handle_rp_req_pages(ms, NULL, start, len);
1328 break;
1329
1330 case MIG_RP_MSG_REQ_PAGES_ID:
1331 expected_len = 12 + 1; /* header + termination */
1332
1333 if (header_len >= expected_len) {
1334 start = be64_to_cpup((uint64_t *)buf);
1335 len = be32_to_cpup((uint32_t *)(buf + 8));
1336 /* Now we expect an idstr */
1337 tmp32 = buf[12]; /* Length of the following idstr */
1338 buf[13 + tmp32] = '\0';
1339 expected_len += tmp32;
1340 }
1341 if (header_len != expected_len) {
1342 error_report("RP: Req_Page_id with length %d expecting %zd",
1343 header_len, expected_len);
1344 mark_source_rp_bad(ms);
1345 goto out;
1346 }
1347 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1348 break;
1349
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001350 default:
1351 break;
1352 }
1353 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001354 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001355 trace_source_return_path_thread_bad_end();
1356 mark_source_rp_bad(ms);
1357 }
1358
1359 trace_source_return_path_thread_end();
1360out:
1361 ms->rp_state.from_dst_file = NULL;
1362 qemu_fclose(rp);
1363 return NULL;
1364}
1365
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001366static int open_return_path_on_source(MigrationState *ms)
1367{
1368
1369 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file);
1370 if (!ms->rp_state.from_dst_file) {
1371 return -1;
1372 }
1373
1374 trace_open_return_path_on_source();
1375 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1376 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1377
1378 trace_open_return_path_on_source_continue();
1379
1380 return 0;
1381}
1382
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001383/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1384static int await_return_path_close_on_source(MigrationState *ms)
1385{
1386 /*
1387 * If this is a normal exit then the destination will send a SHUT and the
1388 * rp_thread will exit, however if there's an error we need to cause
1389 * it to exit.
1390 */
1391 if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) {
1392 /*
1393 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1394 * waiting for the destination.
1395 */
1396 qemu_file_shutdown(ms->rp_state.from_dst_file);
1397 mark_source_rp_bad(ms);
1398 }
1399 trace_await_return_path_close_on_source_joining();
1400 qemu_thread_join(&ms->rp_state.rp_thread);
1401 trace_await_return_path_close_on_source_close();
1402 return ms->rp_state.error;
1403}
1404
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001405/*
1406 * Switch from normal iteration to postcopy
1407 * Returns non-0 on error
1408 */
1409static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1410{
1411 int ret;
1412 const QEMUSizedBuffer *qsb;
1413 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
zhanghailiang48781e52015-12-16 11:47:33 +00001414 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001415 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1416
1417 trace_postcopy_start();
1418 qemu_mutex_lock_iothread();
1419 trace_postcopy_start_set_run();
1420
1421 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1422 *old_vm_running = runstate_is_running();
1423 global_state_store();
1424 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1425
1426 if (ret < 0) {
1427 goto fail;
1428 }
1429
1430 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001431 * Cause any non-postcopiable, but iterative devices to
1432 * send out their final data.
1433 */
1434 qemu_savevm_state_complete_precopy(ms->file, true);
1435
1436 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001437 * in Finish migrate and with the io-lock held everything should
1438 * be quiet, but we've potentially still got dirty pages and we
1439 * need to tell the destination to throw any pages it's already received
1440 * that are dirty
1441 */
1442 if (ram_postcopy_send_discard_bitmap(ms)) {
1443 error_report("postcopy send discard bitmap failed");
1444 goto fail;
1445 }
1446
1447 /*
1448 * send rest of state - note things that are doing postcopy
1449 * will notice we're in POSTCOPY_ACTIVE and not actually
1450 * wrap their state up here
1451 */
1452 qemu_file_set_rate_limit(ms->file, INT64_MAX);
1453 /* Ping just for debugging, helps line traces up */
1454 qemu_savevm_send_ping(ms->file, 2);
1455
1456 /*
1457 * While loading the device state we may trigger page transfer
1458 * requests and the fd must be free to process those, and thus
1459 * the destination must read the whole device state off the fd before
1460 * it starts processing it. Unfortunately the ad-hoc migration format
1461 * doesn't allow the destination to know the size to read without fully
1462 * parsing it through each devices load-state code (especially the open
1463 * coded devices that use get/put).
1464 * So we wrap the device state up in a package with a length at the start;
1465 * to do this we use a qemu_buf to hold the whole of the device state.
1466 */
1467 QEMUFile *fb = qemu_bufopen("w", NULL);
1468 if (!fb) {
1469 error_report("Failed to create buffered file");
1470 goto fail;
1471 }
1472
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001473 /*
1474 * Make sure the receiver can get incoming pages before we send the rest
1475 * of the state
1476 */
1477 qemu_savevm_send_postcopy_listen(fb);
1478
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001479 qemu_savevm_state_complete_precopy(fb, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001480 qemu_savevm_send_ping(fb, 3);
1481
1482 qemu_savevm_send_postcopy_run(fb);
1483
1484 /* <><> end of stuff going into the package */
1485 qsb = qemu_buf_get(fb);
1486
1487 /* Now send that blob */
1488 if (qemu_savevm_send_packaged(ms->file, qsb)) {
1489 goto fail_closefb;
1490 }
1491 qemu_fclose(fb);
1492 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1493
1494 qemu_mutex_unlock_iothread();
1495
1496 /*
1497 * Although this ping is just for debug, it could potentially be
1498 * used for getting a better measurement of downtime at the source.
1499 */
1500 qemu_savevm_send_ping(ms->file, 4);
1501
1502 ret = qemu_file_get_error(ms->file);
1503 if (ret) {
1504 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001505 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001506 MIGRATION_STATUS_FAILED);
1507 }
1508
1509 return ret;
1510
1511fail_closefb:
1512 qemu_fclose(fb);
1513fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001514 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001515 MIGRATION_STATUS_FAILED);
1516 qemu_mutex_unlock_iothread();
1517 return -1;
1518}
1519
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001520/**
1521 * migration_completion: Used by migration_thread when there's not much left.
1522 * The caller 'breaks' the loop when this returns.
1523 *
1524 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001525 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001526 * @*old_vm_running: Pointer to old_vm_running flag
1527 * @*start_time: Pointer to time to update
1528 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001529static void migration_completion(MigrationState *s, int current_active_state,
1530 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001531 int64_t *start_time)
1532{
1533 int ret;
1534
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001535 if (s->state == MIGRATION_STATUS_ACTIVE) {
1536 qemu_mutex_lock_iothread();
1537 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1538 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1539 *old_vm_running = runstate_is_running();
1540 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001541
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001542 if (!ret) {
1543 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1544 if (ret >= 0) {
1545 qemu_file_set_rate_limit(s->file, INT64_MAX);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001546 qemu_savevm_state_complete_precopy(s->file, false);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001547 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001548 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001549 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001550
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001551 if (ret < 0) {
1552 goto fail;
1553 }
1554 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1555 trace_migration_completion_postcopy_end();
1556
1557 qemu_savevm_state_complete_postcopy(s->file);
1558 trace_migration_completion_postcopy_end_after_complete();
1559 }
1560
1561 /*
1562 * If rp was opened we must clean up the thread before
1563 * cleaning everything else up (since if there are no failures
1564 * it will wait for the destination to send it's status in
1565 * a SHUT command).
1566 * Postcopy opens rp if enabled (even if it's not avtivated)
1567 */
1568 if (migrate_postcopy_ram()) {
1569 int rp_error;
1570 trace_migration_completion_postcopy_end_before_rp();
1571 rp_error = await_return_path_close_on_source(s);
1572 trace_migration_completion_postcopy_end_after_rp(rp_error);
1573 if (rp_error) {
1574 goto fail;
1575 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001576 }
1577
1578 if (qemu_file_get_error(s->file)) {
1579 trace_migration_completion_file_err();
1580 goto fail;
1581 }
1582
zhanghailiang48781e52015-12-16 11:47:33 +00001583 migrate_set_state(&s->state, current_active_state,
1584 MIGRATION_STATUS_COMPLETED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001585 return;
1586
1587fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001588 migrate_set_state(&s->state, current_active_state,
1589 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001590}
1591
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001592/*
1593 * Master migration thread on the source VM.
1594 * It drives the migration and pumps the data down the outgoing channel.
1595 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001596static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001597{
Juan Quintela9848a402012-12-19 09:55:50 +01001598 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001599 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001600 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1601 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001602 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001603 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001604 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001605 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001606 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001607 bool entered_postcopy = false;
1608 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1609 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
Juan Quintela76f59332012-10-03 20:16:24 +02001610
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001611 rcu_register_thread();
1612
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001613 qemu_savevm_state_header(s->file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001614
1615 if (migrate_postcopy_ram()) {
1616 /* Now tell the dest that it should open its end so it can reply */
1617 qemu_savevm_send_open_return_path(s->file);
1618
1619 /* And do a ping that will make stuff easier to debug */
1620 qemu_savevm_send_ping(s->file, 1);
1621
1622 /*
1623 * Tell the destination that we *might* want to do postcopy later;
1624 * if the other end can't do postcopy it should fail now, nice and
1625 * early.
1626 */
1627 qemu_savevm_send_postcopy_advise(s->file);
1628 }
1629
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001630 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001631
Alex Blighbc72ad62013-08-21 16:03:08 +01001632 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001633 current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang48781e52015-12-16 11:47:33 +00001634 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1635 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001636
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001637 trace_migration_thread_setup_complete();
1638
1639 while (s->state == MIGRATION_STATUS_ACTIVE ||
1640 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001641 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001642 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001643
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001644 if (!qemu_file_rate_limit(s->file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001645 uint64_t pend_post, pend_nonpost;
1646
1647 qemu_savevm_state_pending(s->file, max_size, &pend_nonpost,
1648 &pend_post);
1649 pending_size = pend_nonpost + pend_post;
1650 trace_migrate_pending(pending_size, max_size,
1651 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001652 if (pending_size && pending_size >= max_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001653 /* Still a significant amount to transfer */
1654
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001655 if (migrate_postcopy_ram() &&
1656 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1657 pend_nonpost <= max_size &&
1658 atomic_read(&s->start_postcopy)) {
1659
1660 if (!postcopy_start(s, &old_vm_running)) {
1661 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1662 entered_postcopy = true;
1663 }
1664
1665 continue;
1666 }
1667 /* Just another iteration step */
Dr. David Alan Gilbert35ecd942015-11-05 18:11:14 +00001668 qemu_savevm_state_iterate(s->file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001669 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001670 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001671 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001672 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001673 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001674 }
1675 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001676
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001677 if (qemu_file_get_error(s->file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001678 migrate_set_state(&s->state, current_active_state,
1679 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001680 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001681 break;
1682 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001683 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001684 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001685 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001686 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001687 double bandwidth = (double)transferred_bytes / time_spent;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001688 max_size = bandwidth * migrate_max_downtime() / 1000000;
1689
Michael R. Hines7e114f82013-06-25 21:35:30 -04001690 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1691 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1692
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001693 trace_migrate_transferred(transferred_bytes, time_spent,
1694 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001695 /* if we haven't sent anything, we don't want to recalculate
1696 10000 is a small enough number for our purposes */
1697 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1698 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1699 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001700
Paolo Bonzini1964a392013-02-22 17:36:45 +01001701 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001702 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001703 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001704 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001705 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001706 /* usleep expects microseconds */
1707 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1708 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001709 }
1710
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001711 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001712 /* If we enabled cpu throttling for auto-converge, turn it off. */
1713 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001714 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001715
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001716 qemu_mutex_lock_iothread();
Liang Liea7415f2015-11-02 15:37:01 +08001717 qemu_savevm_state_cleanup();
zhanghailiang31194732015-03-13 16:08:38 +08001718 if (s->state == MIGRATION_STATUS_COMPLETED) {
Peter Lievend6ed7312014-05-12 10:46:00 +02001719 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001720 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001721 if (!entered_postcopy) {
1722 s->downtime = end_time - start_time;
1723 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001724 if (s->total_time) {
1725 s->mbps = (((double) transferred_bytes * 8.0) /
1726 ((double) s->total_time)) / 1000;
1727 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001728 runstate_set(RUN_STATE_POSTMIGRATE);
1729 } else {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001730 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001731 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001732 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001733 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001734 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001735 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001736
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001737 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001738 return NULL;
1739}
1740
Juan Quintela9848a402012-12-19 09:55:50 +01001741void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001742{
Juan Quintelacc283e32013-02-01 11:12:26 +01001743 /* This is a best 1st approximation. ns to ms */
1744 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001745 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001746
Paolo Bonzini442773c2013-02-22 17:36:44 +01001747 qemu_file_set_rate_limit(s->file,
1748 s->bandwidth_limit / XFER_LIMIT_RATIO);
1749
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001750 /* Notify before starting migration thread */
1751 notifier_list_notify(&migration_state_notifiers, s);
1752
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001753 /*
1754 * Open the return path; currently for postcopy but other things might
1755 * also want it.
1756 */
1757 if (migrate_postcopy_ram()) {
1758 if (open_return_path_on_source(s)) {
1759 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00001760 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001761 MIGRATION_STATUS_FAILED);
1762 migrate_fd_cleanup(s);
1763 return;
1764 }
1765 }
1766
Liang Li8706d2d2015-03-23 16:32:17 +08001767 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001768 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001769 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001770 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001771}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001772
1773PostcopyState postcopy_state_get(void)
1774{
1775 return atomic_mb_read(&incoming_postcopy_state);
1776}
1777
1778/* Set the state and return the old state */
1779PostcopyState postcopy_state_set(PostcopyState new_state)
1780{
1781 return atomic_xchg(&incoming_postcopy_state, new_state);
1782}
1783