blob: 156e70791a63112fe00ad295bc5198620080458c [file] [log] [blame]
aliguori5bb79102008-10-13 03:12:02 +00001/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori5bb79102008-10-13 03:12:02 +000014 */
15
Peter Maydell1393a482016-01-26 18:16:54 +000016#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010019#include "qemu/main-loop.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010020#include "migration/migration.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020021#include "migration/qemu-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010023#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010024#include "qapi/qmp/qerror.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000025#include "qapi/util.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/sockets.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020027#include "qemu/rcu.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010028#include "migration/block.h"
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000029#include "migration/postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020030#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030031#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010032#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020033#include "qapi-event.h"
Jason J. Herne070afca2015-09-08 13:12:35 -040034#include "qom/cpu.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000035#include "exec/memory.h"
36#include "exec/address-spaces.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010037#include "io/channel-buffer.h"
Daniel P. Berrangee1226362016-04-27 11:05:16 +010038#include "io/channel-tls.h"
aliguori065e2812008-11-11 16:46:33 +000039
Jason J. Hernedc325622015-09-08 13:12:37 -040040#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000041
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010042/* Amount of time to allocate to each "chunk" of bandwidth-throttled
43 * data. */
44#define BUFFER_DELAY 100
45#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
46
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053047/* Time in milliseconds we are allowed to stop the source,
48 * for sending the last part */
49#define DEFAULT_MIGRATE_SET_DOWNTIME 300
50
Liang Li8706d2d2015-03-23 16:32:17 +080051/* Default compression thread count */
52#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080053/* Default decompression thread count, usually decompression is at
54 * least 4 times as fast as compression.*/
55#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080056/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
57#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040058/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040059#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
60#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080061
Orit Wasserman17ad9b32012-08-06 21:42:53 +030062/* Migration XBZRLE default cache size */
63#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
64
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010065static NotifierList migration_state_notifiers =
66 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
67
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000068static bool deferred_incoming;
69
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000070/*
71 * Current state of incoming postcopy; note this is not part of
72 * MigrationIncomingState since it's state is used during cleanup
73 * at the end as MIS is being freed.
74 */
75static PostcopyState incoming_postcopy_state;
76
Juan Quintela17549e82011-10-05 13:50:43 +020077/* When we add fault tolerance, we could have several
78 migrations at once. For now we don't need to add
79 dynamic creation of migration */
80
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010081/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020082MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020083{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000084 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +020085 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080086 .state = MIGRATION_STATUS_NONE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030087 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040088 .mbps = -1,
Daniel P. Berrange2594f562016-04-27 11:05:14 +010089 .parameters = {
90 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
91 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
92 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
93 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
94 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053095 .max_bandwidth = MAX_THROTTLE,
96 .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME,
Daniel P. Berrange2594f562016-04-27 11:05:14 +010097 },
Juan Quintela17549e82011-10-05 13:50:43 +020098 };
99
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000100 if (!once) {
101 qemu_mutex_init(&current_migration.src_page_req_mutex);
102 once = true;
103 }
Juan Quintela17549e82011-10-05 13:50:43 +0200104 return &current_migration;
105}
106
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100107/* For incoming */
108static MigrationIncomingState *mis_current;
109
110MigrationIncomingState *migration_incoming_get_current(void)
111{
112 return mis_current;
113}
114
115MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
116{
Markus Armbruster97f3ad32015-09-14 13:51:31 +0200117 mis_current = g_new0(MigrationIncomingState, 1);
Dr. David Alan Gilbert42e2aa52015-11-05 18:10:34 +0000118 mis_current->from_src_file = f;
zhanghailiang93d7af62015-12-16 11:47:34 +0000119 mis_current->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100120 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000121 qemu_mutex_init(&mis_current->rp_mutex);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000122 qemu_event_init(&mis_current->main_thread_load_event, false);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100123
124 return mis_current;
125}
126
127void migration_incoming_state_destroy(void)
128{
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000129 qemu_event_destroy(&mis_current->main_thread_load_event);
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100130 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100131 g_free(mis_current);
132 mis_current = NULL;
133}
134
Juan Quinteladf4b1022014-10-08 10:58:10 +0200135
136typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200137 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200138 uint32_t size;
139 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200140 RunState state;
141 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200142} GlobalState;
143
144static GlobalState global_state;
145
Juan Quintela560d0272015-07-15 09:53:46 +0200146int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200147{
148 if (!runstate_store((char *)global_state.runstate,
149 sizeof(global_state.runstate))) {
150 error_report("runstate name too big: %s", global_state.runstate);
151 trace_migrate_state_too_big();
152 return -EINVAL;
153 }
154 return 0;
155}
156
Anthony PERARDc69adea2015-08-03 15:29:19 +0100157void global_state_store_running(void)
158{
159 const char *state = RunState_lookup[RUN_STATE_RUNNING];
160 strncpy((char *)global_state.runstate,
161 state, sizeof(global_state.runstate));
162}
163
Juan Quintela172c4352015-07-08 13:56:26 +0200164static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200165{
Juan Quintela172c4352015-07-08 13:56:26 +0200166 return global_state.received;
167}
168
169static RunState global_state_get_runstate(void)
170{
171 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200172}
173
Juan Quintela13d16812014-10-08 13:58:24 +0200174void global_state_set_optional(void)
175{
176 global_state.optional = true;
177}
178
179static bool global_state_needed(void *opaque)
180{
181 GlobalState *s = opaque;
182 char *runstate = (char *)s->runstate;
183
184 /* If it is not optional, it is mandatory */
185
186 if (s->optional == false) {
187 return true;
188 }
189
190 /* If state is running or paused, it is not needed */
191
192 if (strcmp(runstate, "running") == 0 ||
193 strcmp(runstate, "paused") == 0) {
194 return false;
195 }
196
197 /* for any other state it is needed */
198 return true;
199}
200
Juan Quinteladf4b1022014-10-08 10:58:10 +0200201static int global_state_post_load(void *opaque, int version_id)
202{
203 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200204 Error *local_err = NULL;
205 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200206 char *runstate = (char *)s->runstate;
207
Juan Quintela172c4352015-07-08 13:56:26 +0200208 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200209 trace_migrate_global_state_post_load(runstate);
210
Eric Blake7fb1cf12015-11-18 01:52:57 -0700211 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200212 -1, &local_err);
213
Juan Quintela172c4352015-07-08 13:56:26 +0200214 if (r == -1) {
215 if (local_err) {
216 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200217 }
Juan Quintela172c4352015-07-08 13:56:26 +0200218 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200219 }
Juan Quintela172c4352015-07-08 13:56:26 +0200220 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200221
Juan Quintela172c4352015-07-08 13:56:26 +0200222 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200223}
224
225static void global_state_pre_save(void *opaque)
226{
227 GlobalState *s = opaque;
228
229 trace_migrate_global_state_pre_save((char *)s->runstate);
230 s->size = strlen((char *)s->runstate) + 1;
231}
232
233static const VMStateDescription vmstate_globalstate = {
234 .name = "globalstate",
235 .version_id = 1,
236 .minimum_version_id = 1,
237 .post_load = global_state_post_load,
238 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200239 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200240 .fields = (VMStateField[]) {
241 VMSTATE_UINT32(size, GlobalState),
242 VMSTATE_BUFFER(runstate, GlobalState),
243 VMSTATE_END_OF_LIST()
244 },
245};
246
247void register_global_state(void)
248{
249 /* We would use it independently that we receive it */
250 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200251 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200252 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
253}
254
Juan Quintelab05dc722015-07-07 14:44:05 +0200255static void migrate_generate_event(int new_state)
256{
257 if (migrate_use_events()) {
258 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200259 }
260}
261
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000262/*
263 * Called on -incoming with a defer: uri.
264 * The migration can be started later after any parameters have been
265 * changed.
266 */
267static void deferred_incoming_migration(Error **errp)
268{
269 if (deferred_incoming) {
270 error_setg(errp, "Incoming migration already deferred");
271 }
272 deferred_incoming = true;
273}
274
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000275/* Request a range of pages from the source VM at the given
276 * start address.
277 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
278 * as the last request (a name must have been given previously)
279 * Start: Address offset within the RB
280 * Len: Length in bytes required - must be a multiple of pagesize
281 */
282void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
283 ram_addr_t start, size_t len)
284{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100285 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000286 size_t msglen = 12; /* start + len */
287
288 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
289 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
290
291 if (rbname) {
292 int rbname_len = strlen(rbname);
293 assert(rbname_len < 256);
294
295 bufc[msglen++] = rbname_len;
296 memcpy(bufc + msglen, rbname, rbname_len);
297 msglen += rbname_len;
298 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
299 } else {
300 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
301 }
302}
303
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200304void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000305{
aliguori34c9dd82008-10-13 03:14:31 +0000306 const char *p;
307
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200308 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000309 if (!strcmp(uri, "defer")) {
310 deferred_incoming_migration(errp);
311 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200312 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400313#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000314 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400315 rdma_start_incoming_migration(p, errp);
316#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000317 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200318 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000319 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200320 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000321 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200322 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000323 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100324 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200325 }
aliguori5bb79102008-10-13 03:12:02 +0000326}
327
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300328static void process_incoming_migration_bh(void *opaque)
329{
330 Error *local_err = NULL;
331 MigrationIncomingState *mis = opaque;
332
333 /* Make sure all file formats flush their mutable metadata */
334 bdrv_invalidate_cache_all(&local_err);
335 if (local_err) {
336 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
337 MIGRATION_STATUS_FAILED);
338 error_report_err(local_err);
339 migrate_decompress_threads_join();
340 exit(EXIT_FAILURE);
341 }
342
343 /*
344 * This must happen after all error conditions are dealt with and
345 * we're sure the VM is going to be running on this host.
346 */
347 qemu_announce_self();
348
349 /* If global state section was not received or we are in running
350 state, we need to obey autostart. Any other state is set with
351 runstate_set. */
352
353 if (!global_state_received() ||
354 global_state_get_runstate() == RUN_STATE_RUNNING) {
355 if (autostart) {
356 vm_start();
357 } else {
358 runstate_set(RUN_STATE_PAUSED);
359 }
360 } else {
361 runstate_set(global_state_get_runstate());
362 }
363 migrate_decompress_threads_join();
364 /*
365 * This must happen after any state changes since as soon as an external
366 * observer sees this event they might start to prod at the VM assuming
367 * it's ready to use.
368 */
369 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
370 MIGRATION_STATUS_COMPLETED);
371 qemu_bh_delete(mis->bh);
372 migration_incoming_state_destroy();
373}
374
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200375static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200376{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200377 QEMUFile *f = opaque;
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000378 MigrationIncomingState *mis;
379 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200380 int ret;
381
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000382 mis = migration_incoming_state_new(f);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000383 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000384 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
385 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200386 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100387
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000388 ps = postcopy_state_get();
389 trace_process_incoming_migration_co_end(ret, ps);
390 if (ps != POSTCOPY_INCOMING_NONE) {
391 if (ps == POSTCOPY_INCOMING_ADVISE) {
392 /*
393 * Where a migration had postcopy enabled (and thus went to advise)
394 * but managed to complete within the precopy period, we can use
395 * the normal exit.
396 */
397 postcopy_ram_incoming_cleanup(mis);
398 } else if (ret >= 0) {
399 /*
400 * Postcopy was started, cleanup should happen at the end of the
401 * postcopy thread.
402 */
403 trace_process_incoming_migration_co_postcopy_end_main();
404 return;
405 }
406 /* Else if something went wrong then just fall out of the normal exit */
407 }
408
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200409 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200410 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100411
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200412 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000413 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
414 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200415 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800416 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600417 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200418 }
Juan Quintela511c0232010-06-09 14:10:55 +0200419
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300420 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
421 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200422}
423
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100424void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200425{
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200426 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200427
Liang Li3fcb38c2015-03-23 16:32:18 +0800428 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100429 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200430 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200431}
432
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100433
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100434void migration_channel_process_incoming(MigrationState *s,
435 QIOChannel *ioc)
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100436{
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100437 trace_migration_set_incoming_channel(
438 ioc, object_get_typename(OBJECT(ioc)));
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100439
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100440 if (s->parameters.tls_creds &&
441 !object_dynamic_cast(OBJECT(ioc),
442 TYPE_QIO_CHANNEL_TLS)) {
443 Error *local_err = NULL;
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100444 migration_tls_channel_process_incoming(s, ioc, &local_err);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100445 if (local_err) {
446 error_report_err(local_err);
447 }
448 } else {
449 QEMUFile *f = qemu_fopen_channel_input(ioc);
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100450 migration_fd_process_incoming(f);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100451 }
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100452}
453
454
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100455void migration_channel_connect(MigrationState *s,
456 QIOChannel *ioc,
457 const char *hostname)
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100458{
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100459 trace_migration_set_outgoing_channel(
460 ioc, object_get_typename(OBJECT(ioc)), hostname);
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100461
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100462 if (s->parameters.tls_creds &&
463 !object_dynamic_cast(OBJECT(ioc),
464 TYPE_QIO_CHANNEL_TLS)) {
465 Error *local_err = NULL;
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100466 migration_tls_channel_connect(s, ioc, hostname, &local_err);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100467 if (local_err) {
468 migrate_fd_error(s, local_err);
469 error_free(local_err);
470 }
471 } else {
472 QEMUFile *f = qemu_fopen_channel_output(ioc);
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100473
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100474 s->to_dst_file = f;
475
476 migrate_fd_connect(s);
477 }
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100478}
479
480
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000481/*
482 * Send a message on the return channel back to the source
483 * of the migration.
484 */
485void migrate_send_rp_message(MigrationIncomingState *mis,
486 enum mig_rp_message_type message_type,
487 uint16_t len, void *data)
488{
489 trace_migrate_send_rp_message((int)message_type, len);
490 qemu_mutex_lock(&mis->rp_mutex);
491 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
492 qemu_put_be16(mis->to_src_file, len);
493 qemu_put_buffer(mis->to_src_file, data, len);
494 qemu_fflush(mis->to_src_file);
495 qemu_mutex_unlock(&mis->rp_mutex);
496}
497
498/*
499 * Send a 'SHUT' message on the return channel with the given value
500 * to indicate that we've finished with the RP. Non-0 value indicates
501 * error.
502 */
503void migrate_send_rp_shut(MigrationIncomingState *mis,
504 uint32_t value)
505{
506 uint32_t buf;
507
508 buf = cpu_to_be32(value);
509 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
510}
511
512/*
513 * Send a 'PONG' message on the return channel with the given value
514 * (normally in response to a 'PING')
515 */
516void migrate_send_rp_pong(MigrationIncomingState *mis,
517 uint32_t value)
518{
519 uint32_t buf;
520
521 buf = cpu_to_be32(value);
522 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
523}
524
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300525MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
526{
527 MigrationCapabilityStatusList *head = NULL;
528 MigrationCapabilityStatusList *caps;
529 MigrationState *s = migrate_get_current();
530 int i;
531
Michael Tokarev387eede2013-10-05 13:18:28 +0400532 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700533 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300534 if (head == NULL) {
535 head = g_malloc0(sizeof(*caps));
536 caps = head;
537 } else {
538 caps->next = g_malloc0(sizeof(*caps));
539 caps = caps->next;
540 }
541 caps->value =
542 g_malloc(sizeof(*caps->value));
543 caps->value->capability = i;
544 caps->value->state = s->enabled_capabilities[i];
545 }
546
547 return head;
548}
549
Liang Li85de8322015-03-23 16:32:28 +0800550MigrationParameters *qmp_query_migrate_parameters(Error **errp)
551{
552 MigrationParameters *params;
553 MigrationState *s = migrate_get_current();
554
555 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500556 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100557 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500558 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100559 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500560 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100561 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500562 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100563 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500564 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100565 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Eric Blakede63ab62016-09-08 22:14:15 -0500566 params->has_tls_creds = !!s->parameters.tls_creds;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100567 params->tls_creds = g_strdup(s->parameters.tls_creds);
Eric Blakede63ab62016-09-08 22:14:15 -0500568 params->has_tls_hostname = !!s->parameters.tls_hostname;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100569 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530570 params->has_max_bandwidth = true;
571 params->max_bandwidth = s->parameters.max_bandwidth;
572 params->has_downtime_limit = true;
573 params->downtime_limit = s->parameters.downtime_limit;
Liang Li85de8322015-03-23 16:32:28 +0800574
575 return params;
576}
577
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000578/*
579 * Return true if we're already in the middle of a migration
580 * (i.e. any of the active or setup states)
581 */
582static bool migration_is_setup_or_active(int state)
583{
584 switch (state) {
585 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000586 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000587 case MIGRATION_STATUS_SETUP:
588 return true;
589
590 default:
591 return false;
592
593 }
594}
595
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300596static void get_xbzrle_cache_stats(MigrationInfo *info)
597{
598 if (migrate_use_xbzrle()) {
599 info->has_xbzrle_cache = true;
600 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
601 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
602 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
603 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
604 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800605 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300606 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
607 }
608}
609
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100610static void populate_ram_info(MigrationInfo *info, MigrationState *s)
611{
612 info->has_ram = true;
613 info->ram = g_malloc0(sizeof(*info->ram));
614 info->ram->transferred = ram_bytes_transferred();
615 info->ram->total = ram_bytes_total();
616 info->ram->duplicate = dup_mig_pages_transferred();
617 info->ram->skipped = skipped_mig_pages_transferred();
618 info->ram->normal = norm_mig_pages_transferred();
619 info->ram->normal_bytes = norm_mig_bytes_transferred();
620 info->ram->mbps = s->mbps;
621 info->ram->dirty_sync_count = s->dirty_sync_count;
Dr. David Alan Gilbertd3bf5412016-06-13 12:16:42 +0100622 info->ram->postcopy_requests = s->postcopy_requests;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100623
624 if (s->state != MIGRATION_STATUS_COMPLETED) {
625 info->ram->remaining = ram_bytes_remaining();
626 info->ram->dirty_pages_rate = s->dirty_pages_rate;
627 }
628}
629
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300630MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000631{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300632 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200633 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000634
Juan Quintela17549e82011-10-05 13:50:43 +0200635 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800636 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200637 /* no migration has happened ever */
638 break;
zhanghailiang31194732015-03-13 16:08:38 +0800639 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400640 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400641 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400642 break;
zhanghailiang31194732015-03-13 16:08:38 +0800643 case MIGRATION_STATUS_ACTIVE:
644 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300645 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200646 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100647 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200648 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200649 info->has_expected_downtime = true;
650 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400651 info->has_setup_time = true;
652 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200653
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100654 populate_ram_info(info, s);
Juan Quintela8d017192012-08-13 12:31:25 +0200655
Juan Quintela17549e82011-10-05 13:50:43 +0200656 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300657 info->has_disk = true;
658 info->disk = g_malloc0(sizeof(*info->disk));
659 info->disk->transferred = blk_mig_bytes_transferred();
660 info->disk->remaining = blk_mig_bytes_remaining();
661 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000662 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300663
Jason J. Herne47828932015-09-08 13:12:36 -0400664 if (cpu_throttle_active()) {
Jason J. Herned85a31d2016-04-21 14:07:18 -0400665 info->has_cpu_throttle_percentage = true;
666 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
Jason J. Herne47828932015-09-08 13:12:36 -0400667 }
668
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300669 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200670 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000671 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
672 /* Mostly the same as active; TODO add some postcopy stats */
673 info->has_status = true;
674 info->has_total_time = true;
675 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
676 - s->total_time;
677 info->has_expected_downtime = true;
678 info->expected_downtime = s->expected_downtime;
679 info->has_setup_time = true;
680 info->setup_time = s->setup_time;
681
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100682 populate_ram_info(info, s);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000683
684 if (blk_mig_active()) {
685 info->has_disk = true;
686 info->disk = g_malloc0(sizeof(*info->disk));
687 info->disk->transferred = blk_mig_bytes_transferred();
688 info->disk->remaining = blk_mig_bytes_remaining();
689 info->disk->total = blk_mig_bytes_total();
690 }
691
692 get_xbzrle_cache_stats(info);
693 break;
zhanghailiang31194732015-03-13 16:08:38 +0800694 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300695 get_xbzrle_cache_stats(info);
696
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300697 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900698 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200699 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200700 info->has_downtime = true;
701 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400702 info->has_setup_time = true;
703 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200704
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100705 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200706 break;
zhanghailiang31194732015-03-13 16:08:38 +0800707 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300708 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100709 if (s->error) {
710 info->has_error_desc = true;
711 info->error_desc = g_strdup(error_get_pretty(s->error));
712 }
Juan Quintela17549e82011-10-05 13:50:43 +0200713 break;
zhanghailiang31194732015-03-13 16:08:38 +0800714 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300715 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200716 break;
aliguori5bb79102008-10-13 03:12:02 +0000717 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800718 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300719
720 return info;
aliguori5bb79102008-10-13 03:12:02 +0000721}
722
Orit Wasserman00458432012-08-06 21:42:48 +0300723void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
724 Error **errp)
725{
726 MigrationState *s = migrate_get_current();
727 MigrationCapabilityStatusList *cap;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100728 bool old_postcopy_cap = migrate_postcopy_ram();
Orit Wasserman00458432012-08-06 21:42:48 +0300729
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000730 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100731 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300732 return;
733 }
734
735 for (cap = params; cap; cap = cap->next) {
736 s->enabled_capabilities[cap->value->capability] = cap->value->state;
737 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000738
739 if (migrate_postcopy_ram()) {
740 if (migrate_use_compression()) {
741 /* The decompression threads asynchronously write into RAM
742 * rather than use the atomic copies needed to avoid
743 * userfaulting. It should be possible to fix the decompression
744 * threads for compatibility in future.
745 */
746 error_report("Postcopy is not currently compatible with "
747 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000748 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000749 false;
750 }
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100751 /* This check is reasonably expensive, so only when it's being
752 * set the first time, also it's only the destination that needs
753 * special support.
754 */
755 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
756 !postcopy_ram_supported_by_host()) {
757 /* postcopy_ram_supported_by_host will have emitted a more
758 * detailed message
759 */
760 error_report("Postcopy is not supported");
761 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
762 false;
763 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000764 }
Orit Wasserman00458432012-08-06 21:42:48 +0300765}
766
Eric Blake7f375e02016-09-08 22:14:16 -0500767void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800768{
769 MigrationState *s = migrate_get_current();
770
Eric Blake7f375e02016-09-08 22:14:16 -0500771 if (params->has_compress_level &&
772 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100773 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
774 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800775 return;
776 }
Eric Blake7f375e02016-09-08 22:14:16 -0500777 if (params->has_compress_threads &&
778 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100779 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
780 "compress_threads",
781 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800782 return;
783 }
Eric Blake7f375e02016-09-08 22:14:16 -0500784 if (params->has_decompress_threads &&
785 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100786 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
787 "decompress_threads",
788 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800789 return;
790 }
Eric Blake7f375e02016-09-08 22:14:16 -0500791 if (params->has_cpu_throttle_initial &&
792 (params->cpu_throttle_initial < 1 ||
793 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400794 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400795 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400796 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530797 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400798 }
Eric Blake7f375e02016-09-08 22:14:16 -0500799 if (params->has_cpu_throttle_increment &&
800 (params->cpu_throttle_increment < 1 ||
801 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400802 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400803 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400804 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530805 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400806 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530807 if (params->has_max_bandwidth &&
808 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
809 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
810 " range of 0 to %zu bytes/second", SIZE_MAX);
811 return;
812 }
813 if (params->has_downtime_limit &&
814 (params->downtime_limit < 0 || params->downtime_limit > 2000000)) {
815 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
816 "downtime_limit",
817 "an integer in the range of 0 to 2000000 milliseconds");
818 return;
819 }
Liang Li85de8322015-03-23 16:32:28 +0800820
Eric Blake7f375e02016-09-08 22:14:16 -0500821 if (params->has_compress_level) {
822 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800823 }
Eric Blake7f375e02016-09-08 22:14:16 -0500824 if (params->has_compress_threads) {
825 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800826 }
Eric Blake7f375e02016-09-08 22:14:16 -0500827 if (params->has_decompress_threads) {
828 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800829 }
Eric Blake7f375e02016-09-08 22:14:16 -0500830 if (params->has_cpu_throttle_initial) {
831 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400832 }
Eric Blake7f375e02016-09-08 22:14:16 -0500833 if (params->has_cpu_throttle_increment) {
834 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400835 }
Eric Blake7f375e02016-09-08 22:14:16 -0500836 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100837 g_free(s->parameters.tls_creds);
Eric Blake7f375e02016-09-08 22:14:16 -0500838 s->parameters.tls_creds = g_strdup(params->tls_creds);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100839 }
Eric Blake7f375e02016-09-08 22:14:16 -0500840 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100841 g_free(s->parameters.tls_hostname);
Eric Blake7f375e02016-09-08 22:14:16 -0500842 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100843 }
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530844 if (params->has_max_bandwidth) {
845 s->parameters.max_bandwidth = params->max_bandwidth;
846 if (s->to_dst_file) {
847 qemu_file_set_rate_limit(s->to_dst_file,
848 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
849 }
850 }
851 if (params->has_downtime_limit) {
852 s->parameters.downtime_limit = params->downtime_limit;
853 }
Liang Li85de8322015-03-23 16:32:28 +0800854}
855
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100856
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000857void qmp_migrate_start_postcopy(Error **errp)
858{
859 MigrationState *s = migrate_get_current();
860
861 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000862 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000863 " the start of migration");
864 return;
865 }
866
867 if (s->state == MIGRATION_STATUS_NONE) {
868 error_setg(errp, "Postcopy must be started after migration has been"
869 " started");
870 return;
871 }
872 /*
873 * we don't error if migration has finished since that would be racy
874 * with issuing this command.
875 */
876 atomic_set(&s->start_postcopy, true);
877}
878
aliguori065e2812008-11-11 16:46:33 +0000879/* shared migration helpers */
880
zhanghailiang48781e52015-12-16 11:47:33 +0000881void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000882{
zhanghailiang48781e52015-12-16 11:47:33 +0000883 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200884 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200885 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000886 }
887}
888
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100889static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000890{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100891 MigrationState *s = opaque;
892
893 qemu_bh_delete(s->cleanup_bh);
894 s->cleanup_bh = NULL;
895
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000896 flush_page_queue(s);
897
zhanghailiang89a02a92016-01-15 11:37:42 +0800898 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100899 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100900 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000901 if (s->migration_thread_running) {
902 qemu_thread_join(&s->thread);
903 s->migration_thread_running = false;
904 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100905 qemu_mutex_lock_iothread();
906
Liang Li8706d2d2015-03-23 16:32:17 +0800907 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800908 qemu_fclose(s->to_dst_file);
909 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000910 }
911
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000912 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
913 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100914
Liang Li94f5a432015-11-02 15:37:00 +0800915 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000916 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800917 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100918 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100919
920 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000921}
922
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100923void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000924{
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100925 trace_migrate_fd_error(error ? error_get_pretty(error) : "");
zhanghailiang89a02a92016-01-15 11:37:42 +0800926 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000927 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
928 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100929 if (!s->error) {
930 s->error = error_copy(error);
931 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100932 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100933}
934
Juan Quintela0edda1c2010-05-11 16:28:39 +0200935static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000936{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000937 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800938 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100939 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000940
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000941 if (s->rp_state.from_dst_file) {
942 /* shutdown the rp socket, so causing the rp thread to shutdown */
943 qemu_file_shutdown(s->rp_state.from_dst_file);
944 }
945
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000946 do {
947 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000948 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000949 break;
950 }
zhanghailiang48781e52015-12-16 11:47:33 +0000951 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +0800952 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000953
954 /*
955 * If we're unlucky the migration code might be stuck somewhere in a
956 * send/write while the network has failed and is waiting to timeout;
957 * if we've got shutdown(2) available then we can force it to quit.
958 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
959 * called in a bh, so there is no race against this cancel.
960 */
zhanghailiang31194732015-03-13 16:08:38 +0800961 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000962 qemu_file_shutdown(f);
963 }
aliguori065e2812008-11-11 16:46:33 +0000964}
965
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100966void add_migration_state_change_notifier(Notifier *notify)
967{
968 notifier_list_add(&migration_state_notifiers, notify);
969}
970
971void remove_migration_state_change_notifier(Notifier *notify)
972{
Paolo Bonzini31552522012-01-13 17:34:01 +0100973 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100974}
975
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200976bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200977{
zhanghailiang31194732015-03-13 16:08:38 +0800978 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200979}
980
Juan Quintela70736932011-02-23 00:43:59 +0100981bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100982{
zhanghailiang31194732015-03-13 16:08:38 +0800983 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100984}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200985
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200986bool migration_has_failed(MigrationState *s)
987{
zhanghailiang31194732015-03-13 16:08:38 +0800988 return (s->state == MIGRATION_STATUS_CANCELLED ||
989 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200990}
991
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000992bool migration_in_postcopy(MigrationState *s)
993{
994 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
995}
996
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000997bool migration_in_postcopy_after_devices(MigrationState *s)
998{
999 return migration_in_postcopy(s) && s->postcopy_after_devices;
1000}
1001
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +00001002MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001003{
Juan Quintela17549e82011-10-05 13:50:43 +02001004 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +03001005
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001006 /*
1007 * Reinitialise all migration state, except
1008 * parameters/capabilities that the user set, and
1009 * locks.
1010 */
1011 s->bytes_xfer = 0;
1012 s->xfer_limit = 0;
1013 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001014 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001015 s->state = MIGRATION_STATUS_NONE;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001016 s->params = *params;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001017 s->rp_state.from_dst_file = NULL;
1018 s->rp_state.error = false;
1019 s->mbps = 0.0;
1020 s->downtime = 0;
1021 s->expected_downtime = 0;
1022 s->dirty_pages_rate = 0;
1023 s->dirty_bytes_rate = 0;
1024 s->setup_time = 0;
1025 s->dirty_sync_count = 0;
1026 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001027 s->postcopy_after_devices = false;
Dr. David Alan Gilbertd3bf5412016-06-13 12:16:42 +01001028 s->postcopy_requests = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001029 s->migration_thread_running = false;
1030 s->last_req_rb = NULL;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001031 error_free(s->error);
1032 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001033
zhanghailiang48781e52015-12-16 11:47:33 +00001034 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001035
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001036 QSIMPLEQ_INIT(&s->src_page_requests);
1037
Alex Blighbc72ad62013-08-21 16:03:08 +01001038 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001039 return s;
1040}
Juan Quintelacab30142011-02-22 23:54:21 +01001041
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001042static GSList *migration_blockers;
1043
1044void migrate_add_blocker(Error *reason)
1045{
1046 migration_blockers = g_slist_prepend(migration_blockers, reason);
1047}
1048
1049void migrate_del_blocker(Error *reason)
1050{
1051 migration_blockers = g_slist_remove(migration_blockers, reason);
1052}
1053
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001054void qmp_migrate_incoming(const char *uri, Error **errp)
1055{
1056 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001057 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001058
1059 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001060 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001061 return;
1062 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001063 if (!once) {
1064 error_setg(errp, "The incoming migration has already been started");
1065 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001066
1067 qemu_start_incoming_migration(uri, &local_err);
1068
1069 if (local_err) {
1070 error_propagate(errp, local_err);
1071 return;
1072 }
1073
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001074 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001075}
1076
Greg Kurz24f39022016-05-04 21:44:19 +02001077bool migration_is_blocked(Error **errp)
1078{
1079 if (qemu_savevm_state_blocked(errp)) {
1080 return true;
1081 }
1082
1083 if (migration_blockers) {
1084 *errp = error_copy(migration_blockers->data);
1085 return true;
1086 }
1087
1088 return false;
1089}
1090
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001091void qmp_migrate(const char *uri, bool has_blk, bool blk,
1092 bool has_inc, bool inc, bool has_detach, bool detach,
1093 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001094{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001095 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001096 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001097 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +01001098 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001099
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +09001100 params.blk = has_blk && blk;
1101 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001102
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001103 if (migration_is_setup_or_active(s->state) ||
zhanghailiang31194732015-03-13 16:08:38 +08001104 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001105 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001106 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001107 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001108 if (runstate_check(RUN_STATE_INMIGRATE)) {
1109 error_setg(errp, "Guest is waiting for an incoming migration");
1110 return;
1111 }
1112
Greg Kurz24f39022016-05-04 21:44:19 +02001113 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001114 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001115 }
1116
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001117 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +01001118
1119 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001120 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001121#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001122 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001123 rdma_start_outgoing_migration(s, p, &local_err);
1124#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001125 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001126 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001127 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001128 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001129 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001130 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001131 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001132 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1133 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001134 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1135 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001136 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001137 }
1138
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001139 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001140 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001141 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001142 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001143 }
Juan Quintelacab30142011-02-22 23:54:21 +01001144}
1145
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001146void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001147{
Juan Quintela17549e82011-10-05 13:50:43 +02001148 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001149}
1150
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001151void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1152{
1153 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001154 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001155
1156 /* Check for truncation */
1157 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001158 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1159 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001160 return;
1161 }
1162
Orit Wassermana5615b12014-01-30 20:08:36 +02001163 /* Cache should not be larger than guest ram size */
1164 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001165 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1166 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001167 return;
1168 }
1169
Orit Wassermanc91e6812014-01-30 20:08:34 +02001170 new_size = xbzrle_cache_resize(value);
1171 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001172 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1173 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001174 return;
1175 }
1176
1177 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001178}
1179
1180int64_t qmp_query_migrate_cache_size(Error **errp)
1181{
1182 return migrate_xbzrle_cache_size();
1183}
1184
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001185void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001186{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301187 MigrationParameters p = {
1188 .has_max_bandwidth = true,
1189 .max_bandwidth = value,
1190 };
Juan Quintelacab30142011-02-22 23:54:21 +01001191
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301192 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001193}
1194
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001195void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001196{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301197 value *= 1000; /* Convert to milliseconds */
1198 value = MAX(0, MIN(INT64_MAX, value));
1199
1200 MigrationParameters p = {
1201 .has_downtime_limit = true,
1202 .downtime_limit = value,
1203 };
1204
1205 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001206}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001207
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001208bool migrate_postcopy_ram(void)
1209{
1210 MigrationState *s;
1211
1212 s = migrate_get_current();
1213
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001214 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001215}
1216
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001217bool migrate_auto_converge(void)
1218{
1219 MigrationState *s;
1220
1221 s = migrate_get_current();
1222
1223 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1224}
1225
Peter Lieven323004a2013-07-18 09:48:50 +02001226bool migrate_zero_blocks(void)
1227{
1228 MigrationState *s;
1229
1230 s = migrate_get_current();
1231
1232 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1233}
1234
Liang Li8706d2d2015-03-23 16:32:17 +08001235bool migrate_use_compression(void)
1236{
Liang Lidde4e692015-03-23 16:32:26 +08001237 MigrationState *s;
1238
1239 s = migrate_get_current();
1240
1241 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001242}
1243
1244int migrate_compress_level(void)
1245{
1246 MigrationState *s;
1247
1248 s = migrate_get_current();
1249
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001250 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001251}
1252
1253int migrate_compress_threads(void)
1254{
1255 MigrationState *s;
1256
1257 s = migrate_get_current();
1258
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001259 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001260}
1261
Liang Li3fcb38c2015-03-23 16:32:18 +08001262int migrate_decompress_threads(void)
1263{
1264 MigrationState *s;
1265
1266 s = migrate_get_current();
1267
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001268 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001269}
1270
Juan Quintelab05dc722015-07-07 14:44:05 +02001271bool migrate_use_events(void)
1272{
1273 MigrationState *s;
1274
1275 s = migrate_get_current();
1276
1277 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1278}
1279
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001280int migrate_use_xbzrle(void)
1281{
1282 MigrationState *s;
1283
1284 s = migrate_get_current();
1285
1286 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1287}
1288
1289int64_t migrate_xbzrle_cache_size(void)
1290{
1291 MigrationState *s;
1292
1293 s = migrate_get_current();
1294
1295 return s->xbzrle_cache_size;
1296}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001297
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001298/* migration thread support */
1299/*
1300 * Something bad happened to the RP stream, mark an error
1301 * The caller shall print or trace something to indicate why
1302 */
1303static void mark_source_rp_bad(MigrationState *s)
1304{
1305 s->rp_state.error = true;
1306}
1307
1308static struct rp_cmd_args {
1309 ssize_t len; /* -1 = variable */
1310 const char *name;
1311} rp_cmd_args[] = {
1312 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1313 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1314 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001315 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1316 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001317 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1318};
1319
1320/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001321 * Process a request for pages received on the return path,
1322 * We're allowed to send more than requested (e.g. to round to our page size)
1323 * and we don't need to send pages that have already been sent.
1324 */
1325static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1326 ram_addr_t start, size_t len)
1327{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001328 long our_host_ps = getpagesize();
1329
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001330 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001331
1332 /*
1333 * Since we currently insist on matching page sizes, just sanity check
1334 * we're being asked for whole host pages.
1335 */
1336 if (start & (our_host_ps-1) ||
1337 (len & (our_host_ps-1))) {
1338 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1339 " len: %zd", __func__, start, len);
1340 mark_source_rp_bad(ms);
1341 return;
1342 }
1343
1344 if (ram_save_queue_pages(ms, rbname, start, len)) {
1345 mark_source_rp_bad(ms);
1346 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001347}
1348
1349/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001350 * Handles messages sent on the return path towards the source VM
1351 *
1352 */
1353static void *source_return_path_thread(void *opaque)
1354{
1355 MigrationState *ms = opaque;
1356 QEMUFile *rp = ms->rp_state.from_dst_file;
1357 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001358 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001359 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001360 ram_addr_t start = 0; /* =0 to silence warning */
1361 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001362 int res;
1363
1364 trace_source_return_path_thread_entry();
1365 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1366 migration_is_setup_or_active(ms->state)) {
1367 trace_source_return_path_thread_loop_top();
1368 header_type = qemu_get_be16(rp);
1369 header_len = qemu_get_be16(rp);
1370
1371 if (header_type >= MIG_RP_MSG_MAX ||
1372 header_type == MIG_RP_MSG_INVALID) {
1373 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1374 header_type, header_len);
1375 mark_source_rp_bad(ms);
1376 goto out;
1377 }
1378
1379 if ((rp_cmd_args[header_type].len != -1 &&
1380 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001381 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001382 error_report("RP: Received '%s' message (0x%04x) with"
1383 "incorrect length %d expecting %zu",
1384 rp_cmd_args[header_type].name, header_type, header_len,
1385 (size_t)rp_cmd_args[header_type].len);
1386 mark_source_rp_bad(ms);
1387 goto out;
1388 }
1389
1390 /* We know we've got a valid header by this point */
1391 res = qemu_get_buffer(rp, buf, header_len);
1392 if (res != header_len) {
1393 error_report("RP: Failed reading data for message 0x%04x"
1394 " read %d expected %d",
1395 header_type, res, header_len);
1396 mark_source_rp_bad(ms);
1397 goto out;
1398 }
1399
1400 /* OK, we have the message and the data */
1401 switch (header_type) {
1402 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001403 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001404 trace_source_return_path_thread_shut(sibling_error);
1405 if (sibling_error) {
1406 error_report("RP: Sibling indicated error %d", sibling_error);
1407 mark_source_rp_bad(ms);
1408 }
1409 /*
1410 * We'll let the main thread deal with closing the RP
1411 * we could do a shutdown(2) on it, but we're the only user
1412 * anyway, so there's nothing gained.
1413 */
1414 goto out;
1415
1416 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001417 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001418 trace_source_return_path_thread_pong(tmp32);
1419 break;
1420
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001421 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001422 start = ldq_be_p(buf);
1423 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001424 migrate_handle_rp_req_pages(ms, NULL, start, len);
1425 break;
1426
1427 case MIG_RP_MSG_REQ_PAGES_ID:
1428 expected_len = 12 + 1; /* header + termination */
1429
1430 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001431 start = ldq_be_p(buf);
1432 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001433 /* Now we expect an idstr */
1434 tmp32 = buf[12]; /* Length of the following idstr */
1435 buf[13 + tmp32] = '\0';
1436 expected_len += tmp32;
1437 }
1438 if (header_len != expected_len) {
1439 error_report("RP: Req_Page_id with length %d expecting %zd",
1440 header_len, expected_len);
1441 mark_source_rp_bad(ms);
1442 goto out;
1443 }
1444 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1445 break;
1446
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001447 default:
1448 break;
1449 }
1450 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001451 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001452 trace_source_return_path_thread_bad_end();
1453 mark_source_rp_bad(ms);
1454 }
1455
1456 trace_source_return_path_thread_end();
1457out:
1458 ms->rp_state.from_dst_file = NULL;
1459 qemu_fclose(rp);
1460 return NULL;
1461}
1462
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001463static int open_return_path_on_source(MigrationState *ms)
1464{
1465
zhanghailiang89a02a92016-01-15 11:37:42 +08001466 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001467 if (!ms->rp_state.from_dst_file) {
1468 return -1;
1469 }
1470
1471 trace_open_return_path_on_source();
1472 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1473 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1474
1475 trace_open_return_path_on_source_continue();
1476
1477 return 0;
1478}
1479
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001480/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1481static int await_return_path_close_on_source(MigrationState *ms)
1482{
1483 /*
1484 * If this is a normal exit then the destination will send a SHUT and the
1485 * rp_thread will exit, however if there's an error we need to cause
1486 * it to exit.
1487 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001488 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001489 /*
1490 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1491 * waiting for the destination.
1492 */
1493 qemu_file_shutdown(ms->rp_state.from_dst_file);
1494 mark_source_rp_bad(ms);
1495 }
1496 trace_await_return_path_close_on_source_joining();
1497 qemu_thread_join(&ms->rp_state.rp_thread);
1498 trace_await_return_path_close_on_source_close();
1499 return ms->rp_state.error;
1500}
1501
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001502/*
1503 * Switch from normal iteration to postcopy
1504 * Returns non-0 on error
1505 */
1506static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1507{
1508 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001509 QIOChannelBuffer *bioc;
1510 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001511 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
zhanghailiang48781e52015-12-16 11:47:33 +00001512 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001513 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1514
1515 trace_postcopy_start();
1516 qemu_mutex_lock_iothread();
1517 trace_postcopy_start_set_run();
1518
1519 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1520 *old_vm_running = runstate_is_running();
1521 global_state_store();
1522 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001523 if (ret < 0) {
1524 goto fail;
1525 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001526
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001527 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001528 if (ret < 0) {
1529 goto fail;
1530 }
1531
1532 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001533 * Cause any non-postcopiable, but iterative devices to
1534 * send out their final data.
1535 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001536 qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001537
1538 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001539 * in Finish migrate and with the io-lock held everything should
1540 * be quiet, but we've potentially still got dirty pages and we
1541 * need to tell the destination to throw any pages it's already received
1542 * that are dirty
1543 */
1544 if (ram_postcopy_send_discard_bitmap(ms)) {
1545 error_report("postcopy send discard bitmap failed");
1546 goto fail;
1547 }
1548
1549 /*
1550 * send rest of state - note things that are doing postcopy
1551 * will notice we're in POSTCOPY_ACTIVE and not actually
1552 * wrap their state up here
1553 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001554 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001555 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001556 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001557
1558 /*
1559 * While loading the device state we may trigger page transfer
1560 * requests and the fd must be free to process those, and thus
1561 * the destination must read the whole device state off the fd before
1562 * it starts processing it. Unfortunately the ad-hoc migration format
1563 * doesn't allow the destination to know the size to read without fully
1564 * parsing it through each devices load-state code (especially the open
1565 * coded devices that use get/put).
1566 * So we wrap the device state up in a package with a length at the start;
1567 * to do this we use a qemu_buf to hold the whole of the device state.
1568 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001569 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001570 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001571 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1572 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001573
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001574 /*
1575 * Make sure the receiver can get incoming pages before we send the rest
1576 * of the state
1577 */
1578 qemu_savevm_send_postcopy_listen(fb);
1579
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001580 qemu_savevm_state_complete_precopy(fb, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001581 qemu_savevm_send_ping(fb, 3);
1582
1583 qemu_savevm_send_postcopy_run(fb);
1584
1585 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001586
1587 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001588 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001589 goto fail_closefb;
1590 }
1591 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001592
1593 /* Send a notify to give a chance for anything that needs to happen
1594 * at the transition to postcopy and after the device state; in particular
1595 * spice needs to trigger a transition now
1596 */
1597 ms->postcopy_after_devices = true;
1598 notifier_list_notify(&migration_state_notifiers, ms);
1599
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001600 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1601
1602 qemu_mutex_unlock_iothread();
1603
1604 /*
1605 * Although this ping is just for debug, it could potentially be
1606 * used for getting a better measurement of downtime at the source.
1607 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001608 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001609
zhanghailiang89a02a92016-01-15 11:37:42 +08001610 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001611 if (ret) {
1612 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001613 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001614 MIGRATION_STATUS_FAILED);
1615 }
1616
1617 return ret;
1618
1619fail_closefb:
1620 qemu_fclose(fb);
1621fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001622 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001623 MIGRATION_STATUS_FAILED);
1624 qemu_mutex_unlock_iothread();
1625 return -1;
1626}
1627
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001628/**
1629 * migration_completion: Used by migration_thread when there's not much left.
1630 * The caller 'breaks' the loop when this returns.
1631 *
1632 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001633 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001634 * @*old_vm_running: Pointer to old_vm_running flag
1635 * @*start_time: Pointer to time to update
1636 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001637static void migration_completion(MigrationState *s, int current_active_state,
1638 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001639 int64_t *start_time)
1640{
1641 int ret;
1642
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001643 if (s->state == MIGRATION_STATUS_ACTIVE) {
1644 qemu_mutex_lock_iothread();
1645 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1646 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1647 *old_vm_running = runstate_is_running();
1648 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001649
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001650 if (!ret) {
1651 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1652 if (ret >= 0) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001653 ret = bdrv_inactivate_all();
1654 }
1655 if (ret >= 0) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001656 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1657 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001658 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001659 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001660 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001661
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001662 if (ret < 0) {
1663 goto fail;
1664 }
1665 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1666 trace_migration_completion_postcopy_end();
1667
zhanghailiang89a02a92016-01-15 11:37:42 +08001668 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001669 trace_migration_completion_postcopy_end_after_complete();
1670 }
1671
1672 /*
1673 * If rp was opened we must clean up the thread before
1674 * cleaning everything else up (since if there are no failures
1675 * it will wait for the destination to send it's status in
1676 * a SHUT command).
1677 * Postcopy opens rp if enabled (even if it's not avtivated)
1678 */
1679 if (migrate_postcopy_ram()) {
1680 int rp_error;
1681 trace_migration_completion_postcopy_end_before_rp();
1682 rp_error = await_return_path_close_on_source(s);
1683 trace_migration_completion_postcopy_end_after_rp(rp_error);
1684 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001685 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001686 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001687 }
1688
zhanghailiang89a02a92016-01-15 11:37:42 +08001689 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001690 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001691 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001692 }
1693
zhanghailiang48781e52015-12-16 11:47:33 +00001694 migrate_set_state(&s->state, current_active_state,
1695 MIGRATION_STATUS_COMPLETED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001696 return;
1697
Greg Kurzfe904ea2016-05-18 15:44:36 +02001698fail_invalidate:
1699 /* If not doing postcopy, vm_start() will be called: let's regain
1700 * control on images.
1701 */
1702 if (s->state == MIGRATION_STATUS_ACTIVE) {
1703 Error *local_err = NULL;
1704
1705 bdrv_invalidate_cache_all(&local_err);
1706 if (local_err) {
1707 error_report_err(local_err);
1708 }
1709 }
1710
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001711fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001712 migrate_set_state(&s->state, current_active_state,
1713 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001714}
1715
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001716/*
1717 * Master migration thread on the source VM.
1718 * It drives the migration and pumps the data down the outgoing channel.
1719 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001720static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001721{
Juan Quintela9848a402012-12-19 09:55:50 +01001722 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001723 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001724 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1725 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001726 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001727 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001728 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001729 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001730 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001731 bool entered_postcopy = false;
1732 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1733 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
Juan Quintela76f59332012-10-03 20:16:24 +02001734
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001735 rcu_register_thread();
1736
zhanghailiang89a02a92016-01-15 11:37:42 +08001737 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001738
1739 if (migrate_postcopy_ram()) {
1740 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001741 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001742
1743 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001744 qemu_savevm_send_ping(s->to_dst_file, 1);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001745
1746 /*
1747 * Tell the destination that we *might* want to do postcopy later;
1748 * if the other end can't do postcopy it should fail now, nice and
1749 * early.
1750 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001751 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001752 }
1753
zhanghailiang89a02a92016-01-15 11:37:42 +08001754 qemu_savevm_state_begin(s->to_dst_file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001755
Alex Blighbc72ad62013-08-21 16:03:08 +01001756 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001757 current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang48781e52015-12-16 11:47:33 +00001758 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1759 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001760
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001761 trace_migration_thread_setup_complete();
1762
1763 while (s->state == MIGRATION_STATUS_ACTIVE ||
1764 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001765 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001766 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001767
zhanghailiang89a02a92016-01-15 11:37:42 +08001768 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001769 uint64_t pend_post, pend_nonpost;
1770
zhanghailiang89a02a92016-01-15 11:37:42 +08001771 qemu_savevm_state_pending(s->to_dst_file, max_size, &pend_nonpost,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001772 &pend_post);
1773 pending_size = pend_nonpost + pend_post;
1774 trace_migrate_pending(pending_size, max_size,
1775 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001776 if (pending_size && pending_size >= max_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001777 /* Still a significant amount to transfer */
1778
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001779 if (migrate_postcopy_ram() &&
1780 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1781 pend_nonpost <= max_size &&
1782 atomic_read(&s->start_postcopy)) {
1783
1784 if (!postcopy_start(s, &old_vm_running)) {
1785 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1786 entered_postcopy = true;
1787 }
1788
1789 continue;
1790 }
1791 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001792 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001793 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001794 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001795 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001796 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001797 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001798 }
1799 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001800
zhanghailiang89a02a92016-01-15 11:37:42 +08001801 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001802 migrate_set_state(&s->state, current_active_state,
1803 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001804 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001805 break;
1806 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001807 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001808 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001809 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1810 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001811 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001812 double bandwidth = (double)transferred_bytes / time_spent;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301813 max_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001814
Wei Yang5b648de2016-01-24 14:09:57 +00001815 s->mbps = (((double) transferred_bytes * 8.0) /
1816 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04001817
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001818 trace_migrate_transferred(transferred_bytes, time_spent,
1819 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001820 /* if we haven't sent anything, we don't want to recalculate
1821 10000 is a small enough number for our purposes */
1822 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1823 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1824 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001825
zhanghailiang89a02a92016-01-15 11:37:42 +08001826 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001827 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08001828 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001829 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001830 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001831 /* usleep expects microseconds */
1832 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1833 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001834 }
1835
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001836 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001837 /* If we enabled cpu throttling for auto-converge, turn it off. */
1838 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001839 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001840
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001841 qemu_mutex_lock_iothread();
Liang Liea7415f2015-11-02 15:37:01 +08001842 qemu_savevm_state_cleanup();
zhanghailiang31194732015-03-13 16:08:38 +08001843 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001844 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001845 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001846 if (!entered_postcopy) {
1847 s->downtime = end_time - start_time;
1848 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001849 if (s->total_time) {
1850 s->mbps = (((double) transferred_bytes * 8.0) /
1851 ((double) s->total_time)) / 1000;
1852 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001853 runstate_set(RUN_STATE_POSTMIGRATE);
1854 } else {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001855 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001856 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01001857 } else {
1858 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
1859 runstate_set(RUN_STATE_POSTMIGRATE);
1860 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001861 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001862 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001863 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001864 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001865
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001866 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001867 return NULL;
1868}
1869
Juan Quintela9848a402012-12-19 09:55:50 +01001870void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001871{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301872 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001873 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001874
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01001875 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08001876 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301877 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01001878
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001879 /* Notify before starting migration thread */
1880 notifier_list_notify(&migration_state_notifiers, s);
1881
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001882 /*
1883 * Open the return path; currently for postcopy but other things might
1884 * also want it.
1885 */
1886 if (migrate_postcopy_ram()) {
1887 if (open_return_path_on_source(s)) {
1888 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00001889 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001890 MIGRATION_STATUS_FAILED);
1891 migrate_fd_cleanup(s);
1892 return;
1893 }
1894 }
1895
Liang Li8706d2d2015-03-23 16:32:17 +08001896 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001897 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001898 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001899 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001900}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001901
1902PostcopyState postcopy_state_get(void)
1903{
1904 return atomic_mb_read(&incoming_postcopy_state);
1905}
1906
1907/* Set the state and return the old state */
1908PostcopyState postcopy_state_set(PostcopyState new_state)
1909{
1910 return atomic_xchg(&incoming_postcopy_state, new_state);
1911}
1912