blob: c041ec7aedf8b0cde0ffd3468c292f8f0ede3648 [file] [log] [blame]
aliguori5bb79102008-10-13 03:12:02 +00001/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori5bb79102008-10-13 03:12:02 +000014 */
15
Peter Maydell1393a482016-01-26 18:16:54 +000016#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020019#include "migration/blocker.h"
Juan Quintelaf4dbe1b2017-04-05 15:54:10 +020020#include "exec.h"
Juan Quintela7fcac4a2017-04-05 15:58:29 +020021#include "fd.h"
Juan Quintela61e8b142017-04-05 17:40:11 +020022#include "socket.h"
Juan Quintelae1a3ece2017-04-17 20:32:36 +020023#include "rdma.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020024#include "ram.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020025#include "migration/global_state.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020026#include "migration/misc.h"
Juan Quintela6666c962017-04-24 20:07:27 +020027#include "migration.h"
Juan Quintela20a519a2017-04-20 14:48:46 +020028#include "savevm.h"
Juan Quintela40014d82017-04-17 19:34:36 +020029#include "qemu-file-channel.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020030#include "qemu-file.h"
Juan Quintela987772d2017-04-17 19:02:59 +020031#include "migration/vmstate.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010032#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010033#include "qapi/qmp/qerror.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020034#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020035#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020036#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020037#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030038#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010039#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020040#include "qapi-event.h"
Juan Quintela51180422017-04-24 20:50:19 +020041#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010042#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080043#include "migration/colo.h"
Peter Xu4ffdb332017-06-27 12:10:18 +080044#include "hw/boards.h"
Peter Xu9d18af92017-06-27 12:10:19 +080045#include "monitor/monitor.h"
aliguori065e2812008-11-11 16:46:33 +000046
Jason J. Hernedc325622015-09-08 13:12:37 -040047#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000048
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010049/* Amount of time to allocate to each "chunk" of bandwidth-throttled
50 * data. */
51#define BUFFER_DELAY 100
52#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
53
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053054/* Time in milliseconds we are allowed to stop the source,
55 * for sending the last part */
56#define DEFAULT_MIGRATE_SET_DOWNTIME 300
57
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030058/* Maximum migrate downtime set to 2000 seconds */
59#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
60#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
61
Liang Li8706d2d2015-03-23 16:32:17 +080062/* Default compression thread count */
63#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080064/* Default decompression thread count, usually decompression is at
65 * least 4 times as fast as compression.*/
66#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080067/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
68#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040069/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040070#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
71#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080072
Orit Wasserman17ad9b32012-08-06 21:42:53 +030073/* Migration XBZRLE default cache size */
74#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
75
zhanghailiang68b53592016-10-27 14:43:01 +080076/* The delay time (in ms) between two COLO checkpoints
77 * Note: Please change this default value to 10000 when we support hybrid mode.
78 */
79#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
Juan Quintela4075fb12016-01-15 08:56:17 +010080#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
Juan Quintela0fb86602017-04-27 10:48:25 +020081#define DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT 16
zhanghailiang68b53592016-10-27 14:43:01 +080082
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010083static NotifierList migration_state_notifiers =
84 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
85
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000086static bool deferred_incoming;
87
Juan Quintelada6f1792017-04-24 17:37:14 +020088/* Messages sent on the return path from destination to source */
89enum mig_rp_message_type {
90 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
91 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
92 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
93
94 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
95 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
96
97 MIG_RP_MSG_MAX
98};
99
Juan Quintela17549e82011-10-05 13:50:43 +0200100/* When we add fault tolerance, we could have several
101 migrations at once. For now we don't need to add
102 dynamic creation of migration */
103
Peter Xue5cb7e72017-06-27 12:10:13 +0800104static MigrationState *current_migration;
105
Peter Xu8b0b29d2017-07-18 11:39:06 +0800106static bool migration_object_check(MigrationState *ms, Error **errp);
107
Peter Xue5cb7e72017-06-27 12:10:13 +0800108void migration_object_init(void)
109{
Peter Xu4ffdb332017-06-27 12:10:18 +0800110 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800111 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800112
Peter Xue5cb7e72017-06-27 12:10:13 +0800113 /* This can only be called once. */
114 assert(!current_migration);
115 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800116
Peter Xu8b0b29d2017-07-18 11:39:06 +0800117 if (!migration_object_check(current_migration, &err)) {
118 error_report_err(err);
119 exit(1);
120 }
121
Peter Xu4ffdb332017-06-27 12:10:18 +0800122 /*
123 * We cannot really do this in migration_instance_init() since at
124 * that time global properties are not yet applied, then this
125 * value will be definitely replaced by something else.
126 */
127 if (ms->enforce_config_section) {
128 current_migration->send_configuration = true;
129 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800130}
131
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100132/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200133MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200134{
Peter Xue5cb7e72017-06-27 12:10:13 +0800135 /* This can only be called after the object created. */
136 assert(current_migration);
137 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200138}
139
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100140MigrationIncomingState *migration_incoming_get_current(void)
141{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100142 static bool once;
143 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100144
Juan Quintelab4b076d2017-01-23 22:32:06 +0100145 if (!once) {
146 mis_current.state = MIGRATION_STATUS_NONE;
147 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100148 qemu_mutex_init(&mis_current.rp_mutex);
149 qemu_event_init(&mis_current.main_thread_load_event, false);
150 once = true;
151 }
152 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100153}
154
155void migration_incoming_state_destroy(void)
156{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100157 struct MigrationIncomingState *mis = migration_incoming_get_current();
158
Peter Xu34826552017-05-19 14:43:29 +0800159 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800160 /* Tell source that we are done */
161 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800162 qemu_fclose(mis->to_src_file);
163 mis->to_src_file = NULL;
164 }
165
Peter Xu660819b2017-05-19 14:43:30 +0800166 if (mis->from_src_file) {
167 qemu_fclose(mis->from_src_file);
168 mis->from_src_file = NULL;
169 }
170
Dr. David Alan Gilbert5089e182017-08-25 15:19:39 +0100171 qemu_event_reset(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100172}
173
Juan Quintelab05dc722015-07-07 14:44:05 +0200174static void migrate_generate_event(int new_state)
175{
176 if (migrate_use_events()) {
177 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200178 }
179}
180
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000181/*
182 * Called on -incoming with a defer: uri.
183 * The migration can be started later after any parameters have been
184 * changed.
185 */
186static void deferred_incoming_migration(Error **errp)
187{
188 if (deferred_incoming) {
189 error_setg(errp, "Incoming migration already deferred");
190 }
191 deferred_incoming = true;
192}
193
Juan Quintelada6f1792017-04-24 17:37:14 +0200194/*
195 * Send a message on the return channel back to the source
196 * of the migration.
197 */
198static void migrate_send_rp_message(MigrationIncomingState *mis,
199 enum mig_rp_message_type message_type,
200 uint16_t len, void *data)
201{
202 trace_migrate_send_rp_message((int)message_type, len);
203 qemu_mutex_lock(&mis->rp_mutex);
204 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
205 qemu_put_be16(mis->to_src_file, len);
206 qemu_put_buffer(mis->to_src_file, data, len);
207 qemu_fflush(mis->to_src_file);
208 qemu_mutex_unlock(&mis->rp_mutex);
209}
210
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000211/* Request a range of pages from the source VM at the given
212 * start address.
213 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
214 * as the last request (a name must have been given previously)
215 * Start: Address offset within the RB
216 * Len: Length in bytes required - must be a multiple of pagesize
217 */
218void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
219 ram_addr_t start, size_t len)
220{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100221 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000222 size_t msglen = 12; /* start + len */
223
224 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
225 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
226
227 if (rbname) {
228 int rbname_len = strlen(rbname);
229 assert(rbname_len < 256);
230
231 bufc[msglen++] = rbname_len;
232 memcpy(bufc + msglen, rbname, rbname_len);
233 msglen += rbname_len;
234 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
235 } else {
236 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
237 }
238}
239
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200240void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000241{
aliguori34c9dd82008-10-13 03:14:31 +0000242 const char *p;
243
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200244 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000245 if (!strcmp(uri, "defer")) {
246 deferred_incoming_migration(errp);
247 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200248 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400249#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000250 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400251 rdma_start_incoming_migration(p, errp);
252#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000253 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200254 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000255 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200256 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000257 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200258 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000259 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100260 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200261 }
aliguori5bb79102008-10-13 03:12:02 +0000262}
263
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300264static void process_incoming_migration_bh(void *opaque)
265{
266 Error *local_err = NULL;
267 MigrationIncomingState *mis = opaque;
268
Kevin Wolface21a52017-05-04 18:52:36 +0200269 /* Make sure all file formats flush their mutable metadata.
270 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300271 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200272 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200273 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200274 local_err = NULL;
275 autostart = false;
276 }
277
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300278 /*
279 * This must happen after all error conditions are dealt with and
280 * we're sure the VM is going to be running on this host.
281 */
282 qemu_announce_self();
283
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100284 if (multifd_load_cleanup(&local_err) != 0) {
285 error_report_err(local_err);
286 autostart = false;
287 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300288 /* If global state section was not received or we are in running
289 state, we need to obey autostart. Any other state is set with
290 runstate_set. */
291
292 if (!global_state_received() ||
293 global_state_get_runstate() == RUN_STATE_RUNNING) {
294 if (autostart) {
295 vm_start();
296 } else {
297 runstate_set(RUN_STATE_PAUSED);
298 }
299 } else {
300 runstate_set(global_state_get_runstate());
301 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300302 /*
303 * This must happen after any state changes since as soon as an external
304 * observer sees this event they might start to prod at the VM assuming
305 * it's ready to use.
306 */
307 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
308 MIGRATION_STATUS_COMPLETED);
309 qemu_bh_delete(mis->bh);
310 migration_incoming_state_destroy();
311}
312
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200313static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200314{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100315 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000316 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200317 int ret;
318
Juan Quintela4f0fae72017-07-24 12:42:02 +0200319 assert(mis->from_src_file);
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000320 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000321 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000322 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
323 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200324 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100325
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000326 ps = postcopy_state_get();
327 trace_process_incoming_migration_co_end(ret, ps);
328 if (ps != POSTCOPY_INCOMING_NONE) {
329 if (ps == POSTCOPY_INCOMING_ADVISE) {
330 /*
331 * Where a migration had postcopy enabled (and thus went to advise)
332 * but managed to complete within the precopy period, we can use
333 * the normal exit.
334 */
335 postcopy_ram_incoming_cleanup(mis);
336 } else if (ret >= 0) {
337 /*
338 * Postcopy was started, cleanup should happen at the end of the
339 * postcopy thread.
340 */
341 trace_process_incoming_migration_co_postcopy_end_main();
342 return;
343 }
344 /* Else if something went wrong then just fall out of the normal exit */
345 }
346
zhanghailiang25d0c162016-10-27 14:42:55 +0800347 /* we get COLO info, and know if we are in COLO mode */
348 if (!ret && migration_incoming_enable_colo()) {
349 mis->migration_incoming_co = qemu_coroutine_self();
350 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
351 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
352 mis->have_colo_incoming_thread = true;
353 qemu_coroutine_yield();
354
355 /* Wait checkpoint incoming thread exit before free resource */
356 qemu_thread_join(&mis->colo_incoming_thread);
357 }
358
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200359 if (ret < 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100360 Error *local_err = NULL;
361
zhanghailiang93d7af62015-12-16 11:47:34 +0000362 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
363 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200364 error_report("load of migration failed: %s", strerror(-ret));
Dr. David Alan Gilbert3a0f2ce2017-07-17 12:09:32 +0100365 qemu_fclose(mis->from_src_file);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100366 if (multifd_load_cleanup(&local_err) != 0) {
367 error_report_err(local_err);
368 }
Eric Blake4aead692013-04-16 15:50:41 -0600369 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200370 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300371 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
372 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200373}
374
Juan Quintelae595a012017-07-17 12:30:25 +0200375static void migration_incoming_setup(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200376{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200377 MigrationIncomingState *mis = migration_incoming_get_current();
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200378
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100379 if (multifd_load_setup() != 0) {
380 /* We haven't been able to create multifd threads
381 nothing better to do */
382 exit(EXIT_FAILURE);
383 }
384
Juan Quintela4f0fae72017-07-24 12:42:02 +0200385 if (!mis->from_src_file) {
386 mis->from_src_file = f;
387 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100388 qemu_file_set_blocking(f, false);
Juan Quintelae595a012017-07-17 12:30:25 +0200389}
390
391static void migration_incoming_process(void)
392{
393 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200394 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200395}
396
Juan Quintelae595a012017-07-17 12:30:25 +0200397void migration_fd_process_incoming(QEMUFile *f)
398{
399 migration_incoming_setup(f);
400 migration_incoming_process();
401}
402
Juan Quintela4f0fae72017-07-24 12:42:02 +0200403void migration_ioc_process_incoming(QIOChannel *ioc)
404{
405 MigrationIncomingState *mis = migration_incoming_get_current();
406
407 if (!mis->from_src_file) {
408 QEMUFile *f = qemu_fopen_channel_input(ioc);
409 migration_fd_process_incoming(f);
410 }
411 /* We still only have a single channel. Nothing to do here yet */
412}
413
Juan Quintela428d8902017-07-24 13:06:25 +0200414/**
415 * @migration_has_all_channels: We have received all channels that we need
416 *
417 * Returns true when we have got connections to all the channels that
418 * we need for migration.
419 */
420bool migration_has_all_channels(void)
421{
422 return true;
423}
424
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000425/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000426 * Send a 'SHUT' message on the return channel with the given value
427 * to indicate that we've finished with the RP. Non-0 value indicates
428 * error.
429 */
430void migrate_send_rp_shut(MigrationIncomingState *mis,
431 uint32_t value)
432{
433 uint32_t buf;
434
435 buf = cpu_to_be32(value);
436 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
437}
438
439/*
440 * Send a 'PONG' message on the return channel with the given value
441 * (normally in response to a 'PING')
442 */
443void migrate_send_rp_pong(MigrationIncomingState *mis,
444 uint32_t value)
445{
446 uint32_t buf;
447
448 buf = cpu_to_be32(value);
449 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
450}
451
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300452MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
453{
454 MigrationCapabilityStatusList *head = NULL;
455 MigrationCapabilityStatusList *caps;
456 MigrationState *s = migrate_get_current();
457 int i;
458
Michael Tokarev387eede2013-10-05 13:18:28 +0400459 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700460 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100461#ifndef CONFIG_LIVE_BLOCK_MIGRATION
462 if (i == MIGRATION_CAPABILITY_BLOCK) {
463 continue;
464 }
465#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300466 if (head == NULL) {
467 head = g_malloc0(sizeof(*caps));
468 caps = head;
469 } else {
470 caps->next = g_malloc0(sizeof(*caps));
471 caps = caps->next;
472 }
473 caps->value =
474 g_malloc(sizeof(*caps->value));
475 caps->value->capability = i;
476 caps->value->state = s->enabled_capabilities[i];
477 }
478
479 return head;
480}
481
Liang Li85de8322015-03-23 16:32:28 +0800482MigrationParameters *qmp_query_migrate_parameters(Error **errp)
483{
484 MigrationParameters *params;
485 MigrationState *s = migrate_get_current();
486
Markus Armbrustere87fae42017-07-18 12:57:38 +0200487 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800488 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500489 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100490 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500491 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100492 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500493 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100494 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500495 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100496 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500497 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100498 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200499 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100500 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200501 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100502 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530503 params->has_max_bandwidth = true;
504 params->max_bandwidth = s->parameters.max_bandwidth;
505 params->has_downtime_limit = true;
506 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800507 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800508 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200509 params->has_block_incremental = true;
510 params->block_incremental = s->parameters.block_incremental;
Juan Quintela4075fb12016-01-15 08:56:17 +0100511 params->has_x_multifd_channels = true;
512 params->x_multifd_channels = s->parameters.x_multifd_channels;
Juan Quintela0fb86602017-04-27 10:48:25 +0200513 params->has_x_multifd_page_count = true;
514 params->x_multifd_page_count = s->parameters.x_multifd_page_count;
Liang Li85de8322015-03-23 16:32:28 +0800515
516 return params;
517}
518
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000519/*
520 * Return true if we're already in the middle of a migration
521 * (i.e. any of the active or setup states)
522 */
523static bool migration_is_setup_or_active(int state)
524{
525 switch (state) {
526 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000527 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000528 case MIGRATION_STATUS_SETUP:
529 return true;
530
531 default:
532 return false;
533
534 }
535}
536
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100537static void populate_ram_info(MigrationInfo *info, MigrationState *s)
538{
539 info->has_ram = true;
540 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200541 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100542 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200543 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100544 /* legacy value. It is not used anymore */
545 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200546 info->ram->normal = ram_counters.normal;
547 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100548 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100549 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200550 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
551 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800552 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100553
Juan Quintela114f5ae2017-05-04 10:09:21 +0200554 if (migrate_use_xbzrle()) {
555 info->has_xbzrle_cache = true;
556 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
557 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200558 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
559 info->xbzrle_cache->pages = xbzrle_counters.pages;
560 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
561 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
562 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200563 }
564
Juan Quintela338182c2017-05-03 13:16:38 +0200565 if (cpu_throttle_active()) {
566 info->has_cpu_throttle_percentage = true;
567 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
568 }
569
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100570 if (s->state != MIGRATION_STATUS_COMPLETED) {
571 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200572 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100573 }
574}
575
Juan Quintela930ac042017-05-04 10:21:46 +0200576static void populate_disk_info(MigrationInfo *info)
577{
578 if (blk_mig_active()) {
579 info->has_disk = true;
580 info->disk = g_malloc0(sizeof(*info->disk));
581 info->disk->transferred = blk_mig_bytes_transferred();
582 info->disk->remaining = blk_mig_bytes_remaining();
583 info->disk->total = blk_mig_bytes_total();
584 }
585}
586
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300587MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000588{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300589 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200590 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000591
Juan Quintela17549e82011-10-05 13:50:43 +0200592 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800593 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200594 /* no migration has happened ever */
595 break;
zhanghailiang31194732015-03-13 16:08:38 +0800596 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400597 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400598 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400599 break;
zhanghailiang31194732015-03-13 16:08:38 +0800600 case MIGRATION_STATUS_ACTIVE:
601 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000602 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200603 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000604 info->has_status = true;
605 info->has_total_time = true;
606 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
607 - s->total_time;
608 info->has_expected_downtime = true;
609 info->expected_downtime = s->expected_downtime;
610 info->has_setup_time = true;
611 info->setup_time = s->setup_time;
612
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100613 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200614 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000615 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800616 case MIGRATION_STATUS_COLO:
617 info->has_status = true;
618 /* TODO: display COLO specific information (checkpoint info etc.) */
619 break;
zhanghailiang31194732015-03-13 16:08:38 +0800620 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300621 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900622 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200623 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200624 info->has_downtime = true;
625 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400626 info->has_setup_time = true;
627 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200628
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100629 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200630 break;
zhanghailiang31194732015-03-13 16:08:38 +0800631 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300632 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100633 if (s->error) {
634 info->has_error_desc = true;
635 info->error_desc = g_strdup(error_get_pretty(s->error));
636 }
Juan Quintela17549e82011-10-05 13:50:43 +0200637 break;
zhanghailiang31194732015-03-13 16:08:38 +0800638 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300639 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200640 break;
aliguori5bb79102008-10-13 03:12:02 +0000641 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800642 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300643
644 return info;
aliguori5bb79102008-10-13 03:12:02 +0000645}
646
Peter Xu4a842142017-07-18 11:39:08 +0800647/**
648 * @migration_caps_check - check capability validity
649 *
650 * @cap_list: old capability list, array of bool
651 * @params: new capabilities to be applied soon
652 * @errp: set *errp if the check failed, with reason
653 *
654 * Returns true if check passed, otherwise false.
655 */
656static bool migrate_caps_check(bool *cap_list,
657 MigrationCapabilityStatusList *params,
658 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +0300659{
Orit Wasserman00458432012-08-06 21:42:48 +0300660 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +0800661 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300662 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +0300663
Peter Xu4a842142017-07-18 11:39:08 +0800664 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +0300665
666 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +0800667 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +0300668 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000669
Peter Xu4a842142017-07-18 11:39:08 +0800670#ifndef CONFIG_LIVE_BLOCK_MIGRATION
671 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
672 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
673 "block migration");
674 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
675 return false;
676 }
677#endif
678
679 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
680 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000681 /* The decompression threads asynchronously write into RAM
682 * rather than use the atomic copies needed to avoid
683 * userfaulting. It should be possible to fix the decompression
684 * threads for compatibility in future.
685 */
Peter Xu4a842142017-07-18 11:39:08 +0800686 error_setg(errp, "Postcopy is not currently compatible "
687 "with compression");
688 return false;
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000689 }
Peter Xu4a842142017-07-18 11:39:08 +0800690
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100691 /* This check is reasonably expensive, so only when it's being
692 * set the first time, also it's only the destination that needs
693 * special support.
694 */
695 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300696 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100697 /* postcopy_ram_supported_by_host will have emitted a more
698 * detailed message
699 */
Peter Xu4a842142017-07-18 11:39:08 +0800700 error_setg(errp, "Postcopy is not supported");
701 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100702 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000703 }
Peter Xu4a842142017-07-18 11:39:08 +0800704
705 return true;
706}
707
708void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
709 Error **errp)
710{
711 MigrationState *s = migrate_get_current();
712 MigrationCapabilityStatusList *cap;
713
714 if (migration_is_setup_or_active(s->state)) {
715 error_setg(errp, QERR_MIGRATION_ACTIVE);
716 return;
717 }
718
719 if (!migrate_caps_check(s->enabled_capabilities, params, errp)) {
720 return;
721 }
722
723 for (cap = params; cap; cap = cap->next) {
724 s->enabled_capabilities[cap->value->capability] = cap->value->state;
725 }
Orit Wasserman00458432012-08-06 21:42:48 +0300726}
727
Peter Xu16d063b2017-07-18 11:39:04 +0800728/*
729 * Check whether the parameters are valid. Error will be put into errp
730 * (if provided). Return true if valid, otherwise false.
731 */
732static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800733{
Eric Blake7f375e02016-09-08 22:14:16 -0500734 if (params->has_compress_level &&
735 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100736 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
737 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +0800738 return false;
Liang Li85de8322015-03-23 16:32:28 +0800739 }
Peter Xu16d063b2017-07-18 11:39:04 +0800740
Eric Blake7f375e02016-09-08 22:14:16 -0500741 if (params->has_compress_threads &&
742 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100743 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
744 "compress_threads",
745 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800746 return false;
Liang Li85de8322015-03-23 16:32:28 +0800747 }
Peter Xu16d063b2017-07-18 11:39:04 +0800748
Eric Blake7f375e02016-09-08 22:14:16 -0500749 if (params->has_decompress_threads &&
750 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100751 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
752 "decompress_threads",
753 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800754 return false;
Liang Li85de8322015-03-23 16:32:28 +0800755 }
Peter Xu16d063b2017-07-18 11:39:04 +0800756
Eric Blake7f375e02016-09-08 22:14:16 -0500757 if (params->has_cpu_throttle_initial &&
758 (params->cpu_throttle_initial < 1 ||
759 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400760 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400761 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400762 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800763 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400764 }
Peter Xu16d063b2017-07-18 11:39:04 +0800765
Eric Blake7f375e02016-09-08 22:14:16 -0500766 if (params->has_cpu_throttle_increment &&
767 (params->cpu_throttle_increment < 1 ||
768 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400769 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400770 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400771 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800772 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400773 }
Peter Xu16d063b2017-07-18 11:39:04 +0800774
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530775 if (params->has_max_bandwidth &&
776 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
777 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
778 " range of 0 to %zu bytes/second", SIZE_MAX);
Peter Xu16d063b2017-07-18 11:39:04 +0800779 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530780 }
Peter Xu16d063b2017-07-18 11:39:04 +0800781
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530782 if (params->has_downtime_limit &&
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300783 (params->downtime_limit < 0 ||
784 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
785 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
786 "the range of 0 to %d milliseconds",
787 MAX_MIGRATE_DOWNTIME);
Peter Xu16d063b2017-07-18 11:39:04 +0800788 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530789 }
Peter Xu16d063b2017-07-18 11:39:04 +0800790
zhanghailiang68b53592016-10-27 14:43:01 +0800791 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
792 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
793 "x_checkpoint_delay",
794 "is invalid, it should be positive");
Peter Xu16d063b2017-07-18 11:39:04 +0800795 return false;
796 }
Juan Quintela4075fb12016-01-15 08:56:17 +0100797 if (params->has_x_multifd_channels &&
798 (params->x_multifd_channels < 1 || params->x_multifd_channels > 255)) {
799 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
800 "multifd_channels",
801 "is invalid, it should be in the range of 1 to 255");
802 return false;
803 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200804 if (params->has_x_multifd_page_count &&
805 (params->x_multifd_page_count < 1 ||
806 params->x_multifd_page_count > 10000)) {
807 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
808 "multifd_page_count",
809 "is invalid, it should be in the range of 1 to 10000");
810 return false;
811 }
Peter Xu16d063b2017-07-18 11:39:04 +0800812
813 return true;
814}
815
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200816static void migrate_params_test_apply(MigrateSetParameters *params,
817 MigrationParameters *dest)
818{
819 *dest = migrate_get_current()->parameters;
820
821 /* TODO use QAPI_CLONE() instead of duplicating it inline */
822
823 if (params->has_compress_level) {
824 dest->compress_level = params->compress_level;
825 }
826
827 if (params->has_compress_threads) {
828 dest->compress_threads = params->compress_threads;
829 }
830
831 if (params->has_decompress_threads) {
832 dest->decompress_threads = params->decompress_threads;
833 }
834
835 if (params->has_cpu_throttle_initial) {
836 dest->cpu_throttle_initial = params->cpu_throttle_initial;
837 }
838
839 if (params->has_cpu_throttle_increment) {
840 dest->cpu_throttle_increment = params->cpu_throttle_increment;
841 }
842
843 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +0200844 assert(params->tls_creds->type == QTYPE_QSTRING);
845 dest->tls_creds = g_strdup(params->tls_creds->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200846 }
847
848 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +0200849 assert(params->tls_hostname->type == QTYPE_QSTRING);
850 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200851 }
852
853 if (params->has_max_bandwidth) {
854 dest->max_bandwidth = params->max_bandwidth;
855 }
856
857 if (params->has_downtime_limit) {
858 dest->downtime_limit = params->downtime_limit;
859 }
860
861 if (params->has_x_checkpoint_delay) {
862 dest->x_checkpoint_delay = params->x_checkpoint_delay;
863 }
864
865 if (params->has_block_incremental) {
866 dest->block_incremental = params->block_incremental;
867 }
Juan Quintela5e7577a2017-10-09 18:07:56 +0200868 if (params->has_x_multifd_channels) {
869 dest->x_multifd_channels = params->x_multifd_channels;
870 }
871 if (params->has_x_multifd_page_count) {
872 dest->x_multifd_page_count = params->x_multifd_page_count;
873 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200874}
875
876static void migrate_params_apply(MigrateSetParameters *params)
Peter Xu16d063b2017-07-18 11:39:04 +0800877{
878 MigrationState *s = migrate_get_current();
879
Markus Armbrustere87fae42017-07-18 12:57:38 +0200880 /* TODO use QAPI_CLONE() instead of duplicating it inline */
881
Eric Blake7f375e02016-09-08 22:14:16 -0500882 if (params->has_compress_level) {
883 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800884 }
Peter Xu476c72a2017-07-18 11:39:05 +0800885
Eric Blake7f375e02016-09-08 22:14:16 -0500886 if (params->has_compress_threads) {
887 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800888 }
Peter Xu476c72a2017-07-18 11:39:05 +0800889
Eric Blake7f375e02016-09-08 22:14:16 -0500890 if (params->has_decompress_threads) {
891 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800892 }
Peter Xu476c72a2017-07-18 11:39:05 +0800893
Eric Blake7f375e02016-09-08 22:14:16 -0500894 if (params->has_cpu_throttle_initial) {
895 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400896 }
Peter Xu476c72a2017-07-18 11:39:05 +0800897
Eric Blake7f375e02016-09-08 22:14:16 -0500898 if (params->has_cpu_throttle_increment) {
899 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400900 }
Peter Xu476c72a2017-07-18 11:39:05 +0800901
Eric Blake7f375e02016-09-08 22:14:16 -0500902 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100903 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +0200904 assert(params->tls_creds->type == QTYPE_QSTRING);
905 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100906 }
Peter Xu476c72a2017-07-18 11:39:05 +0800907
Eric Blake7f375e02016-09-08 22:14:16 -0500908 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100909 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +0200910 assert(params->tls_hostname->type == QTYPE_QSTRING);
911 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100912 }
Peter Xu476c72a2017-07-18 11:39:05 +0800913
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530914 if (params->has_max_bandwidth) {
915 s->parameters.max_bandwidth = params->max_bandwidth;
916 if (s->to_dst_file) {
917 qemu_file_set_rate_limit(s->to_dst_file,
918 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
919 }
920 }
Peter Xu476c72a2017-07-18 11:39:05 +0800921
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530922 if (params->has_downtime_limit) {
923 s->parameters.downtime_limit = params->downtime_limit;
924 }
zhanghailiang68b53592016-10-27 14:43:01 +0800925
926 if (params->has_x_checkpoint_delay) {
927 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800928 if (migration_in_colo_state()) {
929 colo_checkpoint_notify(s);
930 }
zhanghailiang68b53592016-10-27 14:43:01 +0800931 }
Peter Xu476c72a2017-07-18 11:39:05 +0800932
Juan Quintela2833c592017-04-05 18:32:37 +0200933 if (params->has_block_incremental) {
934 s->parameters.block_incremental = params->block_incremental;
935 }
Juan Quintela4075fb12016-01-15 08:56:17 +0100936 if (params->has_x_multifd_channels) {
937 s->parameters.x_multifd_channels = params->x_multifd_channels;
938 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200939 if (params->has_x_multifd_page_count) {
940 s->parameters.x_multifd_page_count = params->x_multifd_page_count;
941 }
Liang Li85de8322015-03-23 16:32:28 +0800942}
943
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200944void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +0800945{
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200946 MigrationParameters tmp;
947
Markus Armbruster01fa5592017-07-18 14:42:04 +0200948 /* TODO Rewrite "" to null instead */
949 if (params->has_tls_creds
950 && params->tls_creds->type == QTYPE_QNULL) {
951 QDECREF(params->tls_creds->u.n);
952 params->tls_creds->type = QTYPE_QSTRING;
953 params->tls_creds->u.s = strdup("");
954 }
955 /* TODO Rewrite "" to null instead */
956 if (params->has_tls_hostname
957 && params->tls_hostname->type == QTYPE_QNULL) {
958 QDECREF(params->tls_hostname->u.n);
959 params->tls_hostname->type = QTYPE_QSTRING;
960 params->tls_hostname->u.s = strdup("");
961 }
962
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200963 migrate_params_test_apply(params, &tmp);
964
965 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +0800966 /* Invalid parameter */
967 return;
968 }
969
970 migrate_params_apply(params);
971}
972
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100973
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000974void qmp_migrate_start_postcopy(Error **errp)
975{
976 MigrationState *s = migrate_get_current();
977
978 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000979 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000980 " the start of migration");
981 return;
982 }
983
984 if (s->state == MIGRATION_STATUS_NONE) {
985 error_setg(errp, "Postcopy must be started after migration has been"
986 " started");
987 return;
988 }
989 /*
990 * we don't error if migration has finished since that would be racy
991 * with issuing this command.
992 */
993 atomic_set(&s->start_postcopy, true);
994}
995
aliguori065e2812008-11-11 16:46:33 +0000996/* shared migration helpers */
997
zhanghailiang48781e52015-12-16 11:47:33 +0000998void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000999{
Peter Xua31fede2017-08-30 16:32:01 +08001000 assert(new_state < MIGRATION_STATUS__MAX);
zhanghailiang48781e52015-12-16 11:47:33 +00001001 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001002 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001003 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001004 }
1005}
1006
Peter Xu4e4a3d32017-07-18 11:39:09 +08001007static MigrationCapabilityStatusList *migrate_cap_add(
1008 MigrationCapabilityStatusList *list,
1009 MigrationCapability index,
1010 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001011{
1012 MigrationCapabilityStatusList *cap;
1013
1014 cap = g_new0(MigrationCapabilityStatusList, 1);
1015 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001016 cap->value->capability = index;
1017 cap->value->state = state;
1018 cap->next = list;
1019
1020 return cap;
1021}
1022
1023void migrate_set_block_enabled(bool value, Error **errp)
1024{
1025 MigrationCapabilityStatusList *cap;
1026
1027 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001028 qmp_migrate_set_capabilities(cap, errp);
1029 qapi_free_MigrationCapabilityStatusList(cap);
1030}
1031
1032static void migrate_set_block_incremental(MigrationState *s, bool value)
1033{
1034 s->parameters.block_incremental = value;
1035}
1036
1037static void block_cleanup_parameters(MigrationState *s)
1038{
1039 if (s->must_remove_block_options) {
1040 /* setting to false can never fail */
1041 migrate_set_block_enabled(false, &error_abort);
1042 migrate_set_block_incremental(s, false);
1043 s->must_remove_block_options = false;
1044 }
1045}
1046
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001047static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +00001048{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001049 MigrationState *s = opaque;
1050
1051 qemu_bh_delete(s->cleanup_bh);
1052 s->cleanup_bh = NULL;
1053
zhanghailiang89a02a92016-01-15 11:37:42 +08001054 if (s->to_dst_file) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001055 Error *local_err = NULL;
1056
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001057 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001058 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001059 if (s->migration_thread_running) {
1060 qemu_thread_join(&s->thread);
1061 s->migration_thread_running = false;
1062 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001063 qemu_mutex_lock_iothread();
1064
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001065 if (multifd_save_cleanup(&local_err) != 0) {
1066 error_report_err(local_err);
1067 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001068 qemu_fclose(s->to_dst_file);
1069 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +00001070 }
1071
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001072 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
1073 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001074
Liang Li94f5a432015-11-02 15:37:00 +08001075 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001076 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001077 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001078 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001079
1080 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001081 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001082}
1083
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001084void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001085{
Peter Maydell25174052016-10-21 18:41:45 +01001086 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001087 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001088 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1089 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001090 if (!s->error) {
1091 s->error = error_copy(error);
1092 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001093 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001094 block_cleanup_parameters(s);
Juan Quintela458cf282011-02-22 23:32:54 +01001095}
1096
Juan Quintela0edda1c2010-05-11 16:28:39 +02001097static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001098{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001099 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001100 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001101 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001102
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001103 if (s->rp_state.from_dst_file) {
1104 /* shutdown the rp socket, so causing the rp thread to shutdown */
1105 qemu_file_shutdown(s->rp_state.from_dst_file);
1106 }
1107
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001108 do {
1109 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001110 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001111 break;
1112 }
zhanghailiang48781e52015-12-16 11:47:33 +00001113 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001114 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001115
1116 /*
1117 * If we're unlucky the migration code might be stuck somewhere in a
1118 * send/write while the network has failed and is waiting to timeout;
1119 * if we've got shutdown(2) available then we can force it to quit.
1120 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1121 * called in a bh, so there is no race against this cancel.
1122 */
zhanghailiang31194732015-03-13 16:08:38 +08001123 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001124 qemu_file_shutdown(f);
1125 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001126 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1127 Error *local_err = NULL;
1128
1129 bdrv_invalidate_cache_all(&local_err);
1130 if (local_err) {
1131 error_report_err(local_err);
1132 } else {
1133 s->block_inactive = false;
1134 }
1135 }
Juan Quintela2833c592017-04-05 18:32:37 +02001136 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001137}
1138
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001139void add_migration_state_change_notifier(Notifier *notify)
1140{
1141 notifier_list_add(&migration_state_notifiers, notify);
1142}
1143
1144void remove_migration_state_change_notifier(Notifier *notify)
1145{
Paolo Bonzini31552522012-01-13 17:34:01 +01001146 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001147}
1148
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001149bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001150{
zhanghailiang31194732015-03-13 16:08:38 +08001151 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001152}
1153
Juan Quintela70736932011-02-23 00:43:59 +01001154bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001155{
zhanghailiang31194732015-03-13 16:08:38 +08001156 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001157}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001158
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001159bool migration_has_failed(MigrationState *s)
1160{
zhanghailiang31194732015-03-13 16:08:38 +08001161 return (s->state == MIGRATION_STATUS_CANCELLED ||
1162 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001163}
1164
Juan Quintela57273092017-03-20 22:25:28 +01001165bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001166{
Juan Quintela57273092017-03-20 22:25:28 +01001167 MigrationState *s = migrate_get_current();
1168
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001169 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1170}
1171
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001172bool migration_in_postcopy_after_devices(MigrationState *s)
1173{
Juan Quintela57273092017-03-20 22:25:28 +01001174 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001175}
1176
Juan Quintelafab35002017-03-22 17:36:57 +01001177bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301178{
Juan Quintelafab35002017-03-22 17:36:57 +01001179 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301180
1181 switch (s->state) {
1182 case MIGRATION_STATUS_NONE:
1183 case MIGRATION_STATUS_CANCELLED:
1184 case MIGRATION_STATUS_COMPLETED:
1185 case MIGRATION_STATUS_FAILED:
1186 return true;
1187 case MIGRATION_STATUS_SETUP:
1188 case MIGRATION_STATUS_CANCELLING:
1189 case MIGRATION_STATUS_ACTIVE:
1190 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1191 case MIGRATION_STATUS_COLO:
1192 return false;
1193 case MIGRATION_STATUS__MAX:
1194 g_assert_not_reached();
1195 }
1196
1197 return false;
1198}
1199
Juan Quintelaa0762d92017-04-05 21:00:09 +02001200MigrationState *migrate_init(void)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001201{
Juan Quintela17549e82011-10-05 13:50:43 +02001202 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +03001203
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001204 /*
1205 * Reinitialise all migration state, except
1206 * parameters/capabilities that the user set, and
1207 * locks.
1208 */
1209 s->bytes_xfer = 0;
1210 s->xfer_limit = 0;
1211 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001212 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001213 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001214 s->rp_state.from_dst_file = NULL;
1215 s->rp_state.error = false;
1216 s->mbps = 0.0;
1217 s->downtime = 0;
1218 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001219 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001220 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001221 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001222 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001223 error_free(s->error);
1224 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001225
zhanghailiang48781e52015-12-16 11:47:33 +00001226 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001227
Alex Blighbc72ad62013-08-21 16:03:08 +01001228 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001229 return s;
1230}
Juan Quintelacab30142011-02-22 23:54:21 +01001231
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001232static GSList *migration_blockers;
1233
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301234int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001235{
Peter Xu3df663e2017-06-27 12:10:15 +08001236 if (migrate_get_current()->only_migratable) {
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301237 error_propagate(errp, error_copy(reason));
1238 error_prepend(errp, "disallowing migration blocker "
1239 "(--only_migratable) for: ");
1240 return -EACCES;
1241 }
1242
Juan Quintelafab35002017-03-22 17:36:57 +01001243 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301244 migration_blockers = g_slist_prepend(migration_blockers, reason);
1245 return 0;
1246 }
1247
1248 error_propagate(errp, error_copy(reason));
1249 error_prepend(errp, "disallowing migration blocker (migration in "
1250 "progress) for: ");
1251 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001252}
1253
1254void migrate_del_blocker(Error *reason)
1255{
1256 migration_blockers = g_slist_remove(migration_blockers, reason);
1257}
1258
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001259void qmp_migrate_incoming(const char *uri, Error **errp)
1260{
1261 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001262 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001263
1264 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001265 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001266 return;
1267 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001268 if (!once) {
1269 error_setg(errp, "The incoming migration has already been started");
1270 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001271
1272 qemu_start_incoming_migration(uri, &local_err);
1273
1274 if (local_err) {
1275 error_propagate(errp, local_err);
1276 return;
1277 }
1278
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001279 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001280}
1281
Greg Kurz24f39022016-05-04 21:44:19 +02001282bool migration_is_blocked(Error **errp)
1283{
1284 if (qemu_savevm_state_blocked(errp)) {
1285 return true;
1286 }
1287
1288 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001289 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001290 return true;
1291 }
1292
1293 return false;
1294}
1295
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001296void qmp_migrate(const char *uri, bool has_blk, bool blk,
1297 bool has_inc, bool inc, bool has_detach, bool detach,
1298 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001299{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001300 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001301 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001302 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001303
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001304 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001305 s->state == MIGRATION_STATUS_CANCELLING ||
1306 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001307 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001308 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001309 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001310 if (runstate_check(RUN_STATE_INMIGRATE)) {
1311 error_setg(errp, "Guest is waiting for an incoming migration");
1312 return;
1313 }
1314
Greg Kurz24f39022016-05-04 21:44:19 +02001315 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001316 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001317 }
1318
Juan Quintela2833c592017-04-05 18:32:37 +02001319 if ((has_blk && blk) || (has_inc && inc)) {
1320 if (migrate_use_block() || migrate_use_block_incremental()) {
1321 error_setg(errp, "Command options are incompatible with "
1322 "current migration capabilities");
1323 return;
1324 }
1325 migrate_set_block_enabled(true, &local_err);
1326 if (local_err) {
1327 error_propagate(errp, local_err);
1328 return;
1329 }
1330 s->must_remove_block_options = true;
1331 }
1332
1333 if (has_inc && inc) {
1334 migrate_set_block_incremental(s, true);
1335 }
1336
Juan Quintelaa0762d92017-04-05 21:00:09 +02001337 s = migrate_init();
Juan Quintelacab30142011-02-22 23:54:21 +01001338
1339 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001340 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001341#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001342 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001343 rdma_start_outgoing_migration(s, p, &local_err);
1344#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001345 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001346 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001347 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001348 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001349 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001350 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001351 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001352 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1353 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001354 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1355 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001356 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001357 }
1358
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001359 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001360 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001361 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001362 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001363 }
Juan Quintelacab30142011-02-22 23:54:21 +01001364}
1365
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001366void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001367{
Juan Quintela17549e82011-10-05 13:50:43 +02001368 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001369}
1370
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001371void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1372{
1373 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001374 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001375
Juan Quintela8acabf62017-10-05 22:00:31 +02001376 new_size = xbzrle_cache_resize(value, errp);
Orit Wassermanc91e6812014-01-30 20:08:34 +02001377 if (new_size < 0) {
Orit Wassermanc91e6812014-01-30 20:08:34 +02001378 return;
1379 }
1380
1381 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001382}
1383
1384int64_t qmp_query_migrate_cache_size(Error **errp)
1385{
1386 return migrate_xbzrle_cache_size();
1387}
1388
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001389void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001390{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001391 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301392 .has_max_bandwidth = true,
1393 .max_bandwidth = value,
1394 };
Juan Quintelacab30142011-02-22 23:54:21 +01001395
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301396 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001397}
1398
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001399void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001400{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001401 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1402 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1403 "the range of 0 to %d seconds",
1404 MAX_MIGRATE_DOWNTIME_SECONDS);
1405 return;
1406 }
1407
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301408 value *= 1000; /* Convert to milliseconds */
1409 value = MAX(0, MIN(INT64_MAX, value));
1410
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001411 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301412 .has_downtime_limit = true,
1413 .downtime_limit = value,
1414 };
1415
1416 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001417}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001418
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001419bool migrate_release_ram(void)
1420{
1421 MigrationState *s;
1422
1423 s = migrate_get_current();
1424
1425 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1426}
1427
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001428bool migrate_postcopy_ram(void)
1429{
1430 MigrationState *s;
1431
1432 s = migrate_get_current();
1433
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001434 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001435}
1436
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001437bool migrate_postcopy(void)
1438{
1439 return migrate_postcopy_ram();
1440}
1441
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001442bool migrate_auto_converge(void)
1443{
1444 MigrationState *s;
1445
1446 s = migrate_get_current();
1447
1448 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1449}
1450
Peter Lieven323004a2013-07-18 09:48:50 +02001451bool migrate_zero_blocks(void)
1452{
1453 MigrationState *s;
1454
1455 s = migrate_get_current();
1456
1457 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1458}
1459
Liang Li8706d2d2015-03-23 16:32:17 +08001460bool migrate_use_compression(void)
1461{
Liang Lidde4e692015-03-23 16:32:26 +08001462 MigrationState *s;
1463
1464 s = migrate_get_current();
1465
1466 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001467}
1468
1469int migrate_compress_level(void)
1470{
1471 MigrationState *s;
1472
1473 s = migrate_get_current();
1474
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001475 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001476}
1477
1478int migrate_compress_threads(void)
1479{
1480 MigrationState *s;
1481
1482 s = migrate_get_current();
1483
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001484 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001485}
1486
Liang Li3fcb38c2015-03-23 16:32:18 +08001487int migrate_decompress_threads(void)
1488{
1489 MigrationState *s;
1490
1491 s = migrate_get_current();
1492
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001493 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001494}
1495
Juan Quintelab05dc722015-07-07 14:44:05 +02001496bool migrate_use_events(void)
1497{
1498 MigrationState *s;
1499
1500 s = migrate_get_current();
1501
1502 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1503}
1504
Juan Quintela30126bb2016-01-14 12:23:00 +01001505bool migrate_use_multifd(void)
1506{
1507 MigrationState *s;
1508
1509 s = migrate_get_current();
1510
1511 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD];
1512}
1513
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01001514bool migrate_pause_before_switchover(void)
1515{
1516 MigrationState *s;
1517
1518 s = migrate_get_current();
1519
1520 return s->enabled_capabilities[
1521 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
1522}
1523
Juan Quintela4075fb12016-01-15 08:56:17 +01001524int migrate_multifd_channels(void)
1525{
1526 MigrationState *s;
1527
1528 s = migrate_get_current();
1529
1530 return s->parameters.x_multifd_channels;
1531}
1532
Juan Quintela0fb86602017-04-27 10:48:25 +02001533int migrate_multifd_page_count(void)
1534{
1535 MigrationState *s;
1536
1537 s = migrate_get_current();
1538
1539 return s->parameters.x_multifd_page_count;
1540}
1541
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001542int migrate_use_xbzrle(void)
1543{
1544 MigrationState *s;
1545
1546 s = migrate_get_current();
1547
1548 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1549}
1550
1551int64_t migrate_xbzrle_cache_size(void)
1552{
1553 MigrationState *s;
1554
1555 s = migrate_get_current();
1556
1557 return s->xbzrle_cache_size;
1558}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001559
Juan Quintela2833c592017-04-05 18:32:37 +02001560bool migrate_use_block(void)
1561{
1562 MigrationState *s;
1563
1564 s = migrate_get_current();
1565
1566 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1567}
1568
Peter Xuc788ada2017-06-26 18:28:55 +08001569bool migrate_use_return_path(void)
1570{
1571 MigrationState *s;
1572
1573 s = migrate_get_current();
1574
1575 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1576}
1577
Juan Quintela2833c592017-04-05 18:32:37 +02001578bool migrate_use_block_incremental(void)
1579{
1580 MigrationState *s;
1581
1582 s = migrate_get_current();
1583
1584 return s->parameters.block_incremental;
1585}
1586
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001587/* migration thread support */
1588/*
1589 * Something bad happened to the RP stream, mark an error
1590 * The caller shall print or trace something to indicate why
1591 */
1592static void mark_source_rp_bad(MigrationState *s)
1593{
1594 s->rp_state.error = true;
1595}
1596
1597static struct rp_cmd_args {
1598 ssize_t len; /* -1 = variable */
1599 const char *name;
1600} rp_cmd_args[] = {
1601 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1602 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1603 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001604 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1605 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001606 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1607};
1608
1609/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001610 * Process a request for pages received on the return path,
1611 * We're allowed to send more than requested (e.g. to round to our page size)
1612 * and we don't need to send pages that have already been sent.
1613 */
1614static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1615 ram_addr_t start, size_t len)
1616{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001617 long our_host_ps = getpagesize();
1618
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001619 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001620
1621 /*
1622 * Since we currently insist on matching page sizes, just sanity check
1623 * we're being asked for whole host pages.
1624 */
1625 if (start & (our_host_ps-1) ||
1626 (len & (our_host_ps-1))) {
1627 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1628 " len: %zd", __func__, start, len);
1629 mark_source_rp_bad(ms);
1630 return;
1631 }
1632
Juan Quintela96506892017-03-14 18:41:03 +01001633 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001634 mark_source_rp_bad(ms);
1635 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001636}
1637
1638/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001639 * Handles messages sent on the return path towards the source VM
1640 *
1641 */
1642static void *source_return_path_thread(void *opaque)
1643{
1644 MigrationState *ms = opaque;
1645 QEMUFile *rp = ms->rp_state.from_dst_file;
1646 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001647 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001648 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001649 ram_addr_t start = 0; /* =0 to silence warning */
1650 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001651 int res;
1652
1653 trace_source_return_path_thread_entry();
1654 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1655 migration_is_setup_or_active(ms->state)) {
1656 trace_source_return_path_thread_loop_top();
1657 header_type = qemu_get_be16(rp);
1658 header_len = qemu_get_be16(rp);
1659
1660 if (header_type >= MIG_RP_MSG_MAX ||
1661 header_type == MIG_RP_MSG_INVALID) {
1662 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1663 header_type, header_len);
1664 mark_source_rp_bad(ms);
1665 goto out;
1666 }
1667
1668 if ((rp_cmd_args[header_type].len != -1 &&
1669 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001670 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001671 error_report("RP: Received '%s' message (0x%04x) with"
1672 "incorrect length %d expecting %zu",
1673 rp_cmd_args[header_type].name, header_type, header_len,
1674 (size_t)rp_cmd_args[header_type].len);
1675 mark_source_rp_bad(ms);
1676 goto out;
1677 }
1678
1679 /* We know we've got a valid header by this point */
1680 res = qemu_get_buffer(rp, buf, header_len);
1681 if (res != header_len) {
1682 error_report("RP: Failed reading data for message 0x%04x"
1683 " read %d expected %d",
1684 header_type, res, header_len);
1685 mark_source_rp_bad(ms);
1686 goto out;
1687 }
1688
1689 /* OK, we have the message and the data */
1690 switch (header_type) {
1691 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001692 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001693 trace_source_return_path_thread_shut(sibling_error);
1694 if (sibling_error) {
1695 error_report("RP: Sibling indicated error %d", sibling_error);
1696 mark_source_rp_bad(ms);
1697 }
1698 /*
1699 * We'll let the main thread deal with closing the RP
1700 * we could do a shutdown(2) on it, but we're the only user
1701 * anyway, so there's nothing gained.
1702 */
1703 goto out;
1704
1705 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001706 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001707 trace_source_return_path_thread_pong(tmp32);
1708 break;
1709
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001710 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001711 start = ldq_be_p(buf);
1712 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001713 migrate_handle_rp_req_pages(ms, NULL, start, len);
1714 break;
1715
1716 case MIG_RP_MSG_REQ_PAGES_ID:
1717 expected_len = 12 + 1; /* header + termination */
1718
1719 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001720 start = ldq_be_p(buf);
1721 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001722 /* Now we expect an idstr */
1723 tmp32 = buf[12]; /* Length of the following idstr */
1724 buf[13 + tmp32] = '\0';
1725 expected_len += tmp32;
1726 }
1727 if (header_len != expected_len) {
1728 error_report("RP: Req_Page_id with length %d expecting %zd",
1729 header_len, expected_len);
1730 mark_source_rp_bad(ms);
1731 goto out;
1732 }
1733 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1734 break;
1735
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001736 default:
1737 break;
1738 }
1739 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001740 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001741 trace_source_return_path_thread_bad_end();
1742 mark_source_rp_bad(ms);
1743 }
1744
1745 trace_source_return_path_thread_end();
1746out:
1747 ms->rp_state.from_dst_file = NULL;
1748 qemu_fclose(rp);
1749 return NULL;
1750}
1751
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001752static int open_return_path_on_source(MigrationState *ms)
1753{
1754
zhanghailiang89a02a92016-01-15 11:37:42 +08001755 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001756 if (!ms->rp_state.from_dst_file) {
1757 return -1;
1758 }
1759
1760 trace_open_return_path_on_source();
1761 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1762 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1763
1764 trace_open_return_path_on_source_continue();
1765
1766 return 0;
1767}
1768
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001769/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1770static int await_return_path_close_on_source(MigrationState *ms)
1771{
1772 /*
1773 * If this is a normal exit then the destination will send a SHUT and the
1774 * rp_thread will exit, however if there's an error we need to cause
1775 * it to exit.
1776 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001777 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001778 /*
1779 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1780 * waiting for the destination.
1781 */
1782 qemu_file_shutdown(ms->rp_state.from_dst_file);
1783 mark_source_rp_bad(ms);
1784 }
1785 trace_await_return_path_close_on_source_joining();
1786 qemu_thread_join(&ms->rp_state.rp_thread);
1787 trace_await_return_path_close_on_source_close();
1788 return ms->rp_state.error;
1789}
1790
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001791/*
1792 * Switch from normal iteration to postcopy
1793 * Returns non-0 on error
1794 */
1795static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1796{
1797 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001798 QIOChannelBuffer *bioc;
1799 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001800 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001801 bool restart_block = false;
zhanghailiang48781e52015-12-16 11:47:33 +00001802 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001803 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1804
1805 trace_postcopy_start();
1806 qemu_mutex_lock_iothread();
1807 trace_postcopy_start_set_run();
1808
1809 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1810 *old_vm_running = runstate_is_running();
1811 global_state_store();
1812 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001813 if (ret < 0) {
1814 goto fail;
1815 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001816
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001817 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001818 if (ret < 0) {
1819 goto fail;
1820 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001821 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001822
1823 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001824 * Cause any non-postcopiable, but iterative devices to
1825 * send out their final data.
1826 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08001827 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001828
1829 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001830 * in Finish migrate and with the io-lock held everything should
1831 * be quiet, but we've potentially still got dirty pages and we
1832 * need to tell the destination to throw any pages it's already received
1833 * that are dirty
1834 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001835 if (migrate_postcopy_ram()) {
1836 if (ram_postcopy_send_discard_bitmap(ms)) {
1837 error_report("postcopy send discard bitmap failed");
1838 goto fail;
1839 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001840 }
1841
1842 /*
1843 * send rest of state - note things that are doing postcopy
1844 * will notice we're in POSTCOPY_ACTIVE and not actually
1845 * wrap their state up here
1846 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001847 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001848 if (migrate_postcopy_ram()) {
1849 /* Ping just for debugging, helps line traces up */
1850 qemu_savevm_send_ping(ms->to_dst_file, 2);
1851 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001852
1853 /*
1854 * While loading the device state we may trigger page transfer
1855 * requests and the fd must be free to process those, and thus
1856 * the destination must read the whole device state off the fd before
1857 * it starts processing it. Unfortunately the ad-hoc migration format
1858 * doesn't allow the destination to know the size to read without fully
1859 * parsing it through each devices load-state code (especially the open
1860 * coded devices that use get/put).
1861 * So we wrap the device state up in a package with a length at the start;
1862 * to do this we use a qemu_buf to hold the whole of the device state.
1863 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001864 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001865 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001866 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1867 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001868
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001869 /*
1870 * Make sure the receiver can get incoming pages before we send the rest
1871 * of the state
1872 */
1873 qemu_savevm_send_postcopy_listen(fb);
1874
Fam Zhenga1fbe752017-06-17 00:06:58 +08001875 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001876 if (migrate_postcopy_ram()) {
1877 qemu_savevm_send_ping(fb, 3);
1878 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001879
1880 qemu_savevm_send_postcopy_run(fb);
1881
1882 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001883
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001884 /* Last point of recovery; as soon as we send the package the destination
1885 * can open devices and potentially start running.
1886 * Lets just check again we've not got any errors.
1887 */
1888 ret = qemu_file_get_error(ms->to_dst_file);
1889 if (ret) {
1890 error_report("postcopy_start: Migration stream errored (pre package)");
1891 goto fail_closefb;
1892 }
1893
1894 restart_block = false;
1895
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001896 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001897 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001898 goto fail_closefb;
1899 }
1900 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001901
1902 /* Send a notify to give a chance for anything that needs to happen
1903 * at the transition to postcopy and after the device state; in particular
1904 * spice needs to trigger a transition now
1905 */
1906 ms->postcopy_after_devices = true;
1907 notifier_list_notify(&migration_state_notifiers, ms);
1908
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001909 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1910
1911 qemu_mutex_unlock_iothread();
1912
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001913 if (migrate_postcopy_ram()) {
1914 /*
1915 * Although this ping is just for debug, it could potentially be
1916 * used for getting a better measurement of downtime at the source.
1917 */
1918 qemu_savevm_send_ping(ms->to_dst_file, 4);
1919 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001920
Pavel Butsykinced1c612017-02-03 18:23:21 +03001921 if (migrate_release_ram()) {
1922 ram_postcopy_migrated_memory_release(ms);
1923 }
1924
zhanghailiang89a02a92016-01-15 11:37:42 +08001925 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001926 if (ret) {
1927 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001928 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001929 MIGRATION_STATUS_FAILED);
1930 }
1931
1932 return ret;
1933
1934fail_closefb:
1935 qemu_fclose(fb);
1936fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001937 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001938 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001939 if (restart_block) {
1940 /* A failure happened early enough that we know the destination hasn't
1941 * accessed block devices, so we're safe to recover.
1942 */
1943 Error *local_err = NULL;
1944
1945 bdrv_invalidate_cache_all(&local_err);
1946 if (local_err) {
1947 error_report_err(local_err);
1948 }
1949 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001950 qemu_mutex_unlock_iothread();
1951 return -1;
1952}
1953
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001954/**
1955 * migration_completion: Used by migration_thread when there's not much left.
1956 * The caller 'breaks' the loop when this returns.
1957 *
1958 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001959 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001960 * @*old_vm_running: Pointer to old_vm_running flag
1961 * @*start_time: Pointer to time to update
1962 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001963static void migration_completion(MigrationState *s, int current_active_state,
1964 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001965 int64_t *start_time)
1966{
1967 int ret;
1968
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001969 if (s->state == MIGRATION_STATUS_ACTIVE) {
1970 qemu_mutex_lock_iothread();
1971 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1972 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1973 *old_vm_running = runstate_is_running();
1974 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001975
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001976 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08001977 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001978 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001979 if (ret >= 0) {
1980 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08001981 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
1982 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001983 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08001984 if (inactivate && ret >= 0) {
1985 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001986 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001987 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001988 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001989
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001990 if (ret < 0) {
1991 goto fail;
1992 }
1993 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1994 trace_migration_completion_postcopy_end();
1995
zhanghailiang89a02a92016-01-15 11:37:42 +08001996 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001997 trace_migration_completion_postcopy_end_after_complete();
1998 }
1999
2000 /*
2001 * If rp was opened we must clean up the thread before
2002 * cleaning everything else up (since if there are no failures
2003 * it will wait for the destination to send it's status in
2004 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002005 */
Peter Xu0425dc92017-05-31 18:35:34 +08002006 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002007 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08002008 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002009 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08002010 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002011 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002012 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002013 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002014 }
2015
zhanghailiang89a02a92016-01-15 11:37:42 +08002016 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002017 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002018 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002019 }
2020
zhanghailiang0b827d52016-10-27 14:42:54 +08002021 if (!migrate_colo_enabled()) {
2022 migrate_set_state(&s->state, current_active_state,
2023 MIGRATION_STATUS_COMPLETED);
2024 }
2025
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002026 return;
2027
Greg Kurzfe904ea2016-05-18 15:44:36 +02002028fail_invalidate:
2029 /* If not doing postcopy, vm_start() will be called: let's regain
2030 * control on images.
2031 */
2032 if (s->state == MIGRATION_STATUS_ACTIVE) {
2033 Error *local_err = NULL;
2034
zhanghailiang1d2acc32017-01-24 15:59:52 +08002035 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002036 bdrv_invalidate_cache_all(&local_err);
2037 if (local_err) {
2038 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08002039 } else {
2040 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02002041 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08002042 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002043 }
2044
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002045fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002046 migrate_set_state(&s->state, current_active_state,
2047 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002048}
2049
zhanghailiang35a6ed42016-10-27 14:42:52 +08002050bool migrate_colo_enabled(void)
2051{
2052 MigrationState *s = migrate_get_current();
2053 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2054}
2055
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002056/*
2057 * Master migration thread on the source VM.
2058 * It drives the migration and pumps the data down the outgoing channel.
2059 */
Juan Quintela5f496a12013-02-22 17:36:30 +01002060static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002061{
Juan Quintela9848a402012-12-19 09:55:50 +01002062 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002063 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01002064 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2065 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01002066 int64_t initial_bytes = 0;
Peter Xufaec0662017-04-01 16:18:43 +08002067 /*
2068 * The final stage happens when the remaining data is smaller than
2069 * this threshold; it's calculated from the requested downtime and
2070 * measured bandwidth
2071 */
2072 int64_t threshold_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002073 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08002074 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002075 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002076 bool entered_postcopy = false;
2077 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
2078 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang0b827d52016-10-27 14:42:54 +08002079 bool enable_colo = migrate_colo_enabled();
Juan Quintela76f59332012-10-03 20:16:24 +02002080
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002081 rcu_register_thread();
2082
zhanghailiang89a02a92016-01-15 11:37:42 +08002083 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002084
Peter Xu62a02652017-06-14 15:55:58 +08002085 /*
2086 * If we opened the return path, we need to make sure dst has it
2087 * opened as well.
2088 */
2089 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002090 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08002091 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002092
2093 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08002094 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08002095 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002096
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002097 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002098 /*
2099 * Tell the destination that we *might* want to do postcopy later;
2100 * if the other end can't do postcopy it should fail now, nice and
2101 * early.
2102 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002103 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002104 }
2105
Juan Quintela9907e842017-06-28 11:52:24 +02002106 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002107
Alex Blighbc72ad62013-08-21 16:03:08 +01002108 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00002109 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2110 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04002111
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00002112 trace_migration_thread_setup_complete();
2113
2114 while (s->state == MIGRATION_STATUS_ACTIVE ||
2115 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01002116 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02002117 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002118
zhanghailiang89a02a92016-01-15 11:37:42 +08002119 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00002120 uint64_t pend_post, pend_nonpost;
2121
Peter Xufaec0662017-04-01 16:18:43 +08002122 qemu_savevm_state_pending(s->to_dst_file, threshold_size,
2123 &pend_nonpost, &pend_post);
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00002124 pending_size = pend_nonpost + pend_post;
Peter Xufaec0662017-04-01 16:18:43 +08002125 trace_migrate_pending(pending_size, threshold_size,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00002126 pend_post, pend_nonpost);
Peter Xufaec0662017-04-01 16:18:43 +08002127 if (pending_size && pending_size >= threshold_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002128 /* Still a significant amount to transfer */
2129
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002130 if (migrate_postcopy() &&
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002131 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
Peter Xufaec0662017-04-01 16:18:43 +08002132 pend_nonpost <= threshold_size &&
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002133 atomic_read(&s->start_postcopy)) {
2134
2135 if (!postcopy_start(s, &old_vm_running)) {
2136 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
2137 entered_postcopy = true;
2138 }
2139
2140 continue;
2141 }
2142 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08002143 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02002144 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002145 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002146 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00002147 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002148 break;
Juan Quintelac369f402012-10-03 20:33:34 +02002149 }
2150 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002151
zhanghailiang89a02a92016-01-15 11:37:42 +08002152 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00002153 migrate_set_state(&s->state, current_active_state,
2154 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002155 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002156 break;
2157 }
Alex Blighbc72ad62013-08-21 16:03:08 +01002158 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002159 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08002160 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
2161 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05002162 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01002163 double bandwidth = (double)transferred_bytes / time_spent;
Peter Xufaec0662017-04-01 16:18:43 +08002164 threshold_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002165
Wei Yang5b648de2016-01-24 14:09:57 +00002166 s->mbps = (((double) transferred_bytes * 8.0) /
2167 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04002168
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11002169 trace_migrate_transferred(transferred_bytes, time_spent,
Peter Xufaec0662017-04-01 16:18:43 +08002170 bandwidth, threshold_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01002171 /* if we haven't sent anything, we don't want to recalculate
2172 10000 is a small enough number for our purposes */
Juan Quintela93604472017-06-06 19:49:03 +02002173 if (ram_counters.dirty_pages_rate && transferred_bytes > 10000) {
2174 s->expected_downtime = ram_counters.dirty_pages_rate *
Juan Quintela20afaed2017-03-21 09:09:14 +01002175 qemu_target_page_size() / bandwidth;
Juan Quintela90f8ae72013-02-01 13:22:37 +01002176 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002177
zhanghailiang89a02a92016-01-15 11:37:42 +08002178 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002179 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08002180 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002181 }
zhanghailiang89a02a92016-01-15 11:37:42 +08002182 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002183 /* usleep expects microseconds */
2184 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
2185 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002186 }
2187
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002188 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04002189 /* If we enabled cpu throttling for auto-converge, turn it off. */
2190 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08002191 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04002192
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002193 qemu_mutex_lock_iothread();
zhanghailiang0b827d52016-10-27 14:42:54 +08002194 /*
2195 * The resource has been allocated by migration will be reused in COLO
2196 * process, so don't release them.
2197 */
2198 if (!enable_colo) {
2199 qemu_savevm_state_cleanup();
2200 }
zhanghailiang31194732015-03-13 16:08:38 +08002201 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08002202 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002203 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002204 if (!entered_postcopy) {
2205 s->downtime = end_time - start_time;
2206 }
Peter Lievend6ed7312014-05-12 10:46:00 +02002207 if (s->total_time) {
2208 s->mbps = (((double) transferred_bytes * 8.0) /
2209 ((double) s->total_time)) / 1000;
2210 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002211 runstate_set(RUN_STATE_POSTMIGRATE);
2212 } else {
zhanghailiang0b827d52016-10-27 14:42:54 +08002213 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
2214 migrate_start_colo_process(s);
2215 qemu_savevm_state_cleanup();
2216 /*
2217 * Fixme: we will run VM in COLO no matter its old running state.
2218 * After exited COLO, we will keep running.
2219 */
2220 old_vm_running = true;
2221 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002222 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002223 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01002224 } else {
2225 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2226 runstate_set(RUN_STATE_POSTMIGRATE);
2227 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01002228 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002229 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002230 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01002231 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002232
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002233 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002234 return NULL;
2235}
2236
Juan Quintela9848a402012-12-19 09:55:50 +01002237void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002238{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302239 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002240 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002241
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01002242 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08002243 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302244 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002245
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002246 /* Notify before starting migration thread */
2247 notifier_list_notify(&migration_state_notifiers, s);
2248
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002249 /*
Peter Xuc788ada2017-06-26 18:28:55 +08002250 * Open the return path. For postcopy, it is used exclusively. For
2251 * precopy, only if user specified "return-path" capability would
2252 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002253 */
Peter Xuc788ada2017-06-26 18:28:55 +08002254 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002255 if (open_return_path_on_source(s)) {
2256 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00002257 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002258 MIGRATION_STATUS_FAILED);
2259 migrate_fd_cleanup(s);
2260 return;
2261 }
2262 }
2263
Juan Quintelaf986c3d2016-01-14 16:52:55 +01002264 if (multifd_save_setup() != 0) {
2265 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2266 MIGRATION_STATUS_FAILED);
2267 migrate_fd_cleanup(s);
2268 return;
2269 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05302270 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002271 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002272 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002273}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002274
Peter Xu9d18af92017-06-27 12:10:19 +08002275void migration_global_dump(Monitor *mon)
2276{
2277 MigrationState *ms = migrate_get_current();
2278
2279 monitor_printf(mon, "globals: store-global-state=%d, only_migratable=%d, "
2280 "send-configuration=%d, send-section-footer=%d\n",
2281 ms->store_global_state, ms->only_migratable,
2282 ms->send_configuration, ms->send_section_footer);
2283}
2284
Peter Xu20814752017-07-18 11:39:03 +08002285#define DEFINE_PROP_MIG_CAP(name, x) \
2286 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2287
Peter Xu52722982017-06-27 12:10:14 +08002288static Property migration_properties[] = {
2289 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2290 store_global_state, true),
Peter Xu3df663e2017-06-27 12:10:15 +08002291 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
Peter Xu71dd4c12017-06-27 12:10:16 +08002292 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2293 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08002294 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2295 send_section_footer, true),
Peter Xu89632fa2017-07-18 11:39:02 +08002296
2297 /* Migration parameters */
2298 DEFINE_PROP_INT64("x-compress-level", MigrationState,
2299 parameters.compress_level,
2300 DEFAULT_MIGRATE_COMPRESS_LEVEL),
2301 DEFINE_PROP_INT64("x-compress-threads", MigrationState,
2302 parameters.compress_threads,
2303 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
2304 DEFINE_PROP_INT64("x-decompress-threads", MigrationState,
2305 parameters.decompress_threads,
2306 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
2307 DEFINE_PROP_INT64("x-cpu-throttle-initial", MigrationState,
2308 parameters.cpu_throttle_initial,
2309 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
2310 DEFINE_PROP_INT64("x-cpu-throttle-increment", MigrationState,
2311 parameters.cpu_throttle_increment,
2312 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
2313 DEFINE_PROP_INT64("x-max-bandwidth", MigrationState,
2314 parameters.max_bandwidth, MAX_THROTTLE),
2315 DEFINE_PROP_INT64("x-downtime-limit", MigrationState,
2316 parameters.downtime_limit,
2317 DEFAULT_MIGRATE_SET_DOWNTIME),
2318 DEFINE_PROP_INT64("x-checkpoint-delay", MigrationState,
2319 parameters.x_checkpoint_delay,
2320 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintela4075fb12016-01-15 08:56:17 +01002321 DEFINE_PROP_INT64("x-multifd-channels", MigrationState,
2322 parameters.x_multifd_channels,
2323 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela0fb86602017-04-27 10:48:25 +02002324 DEFINE_PROP_INT64("x-multifd-page-count", MigrationState,
2325 parameters.x_multifd_page_count,
2326 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
Peter Xu20814752017-07-18 11:39:03 +08002327
2328 /* Migration capabilities */
2329 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2330 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2331 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2332 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2333 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2334 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2335 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2336 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2337 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2338 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2339 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintela30126bb2016-01-14 12:23:00 +01002340 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08002341
Peter Xu52722982017-06-27 12:10:14 +08002342 DEFINE_PROP_END_OF_LIST(),
2343};
2344
Peter Xue5cb7e72017-06-27 12:10:13 +08002345static void migration_class_init(ObjectClass *klass, void *data)
2346{
2347 DeviceClass *dc = DEVICE_CLASS(klass);
2348
2349 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08002350 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08002351}
2352
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002353static void migration_instance_finalize(Object *obj)
2354{
2355 MigrationState *ms = MIGRATION_OBJ(obj);
2356 MigrationParameters *params = &ms->parameters;
2357
2358 g_free(params->tls_hostname);
2359 g_free(params->tls_creds);
2360}
2361
Peter Xue5cb7e72017-06-27 12:10:13 +08002362static void migration_instance_init(Object *obj)
2363{
2364 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002365 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08002366
2367 ms->state = MIGRATION_STATUS_NONE;
2368 ms->xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE;
2369 ms->mbps = -1;
Peter Xu8b0b29d2017-07-18 11:39:06 +08002370
2371 params->tls_hostname = g_strdup("");
2372 params->tls_creds = g_strdup("");
2373
2374 /* Set has_* up only for parameter checks */
2375 params->has_compress_level = true;
2376 params->has_compress_threads = true;
2377 params->has_decompress_threads = true;
2378 params->has_cpu_throttle_initial = true;
2379 params->has_cpu_throttle_increment = true;
2380 params->has_max_bandwidth = true;
2381 params->has_downtime_limit = true;
2382 params->has_x_checkpoint_delay = true;
2383 params->has_block_incremental = true;
Juan Quintela4075fb12016-01-15 08:56:17 +01002384 params->has_x_multifd_channels = true;
Juan Quintela0fb86602017-04-27 10:48:25 +02002385 params->has_x_multifd_page_count = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08002386}
2387
2388/*
2389 * Return true if check pass, false otherwise. Error will be put
2390 * inside errp if provided.
2391 */
2392static bool migration_object_check(MigrationState *ms, Error **errp)
2393{
Peter Xu6b19a7d2017-07-18 11:39:10 +08002394 MigrationCapabilityStatusList *head = NULL;
2395 /* Assuming all off */
2396 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2397 int i;
2398
Peter Xu8b0b29d2017-07-18 11:39:06 +08002399 if (!migrate_params_check(&ms->parameters, errp)) {
2400 return false;
2401 }
2402
Peter Xu6b19a7d2017-07-18 11:39:10 +08002403 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2404 if (ms->enabled_capabilities[i]) {
2405 head = migrate_cap_add(head, i, true);
2406 }
2407 }
2408
2409 ret = migrate_caps_check(cap_list, head, errp);
2410
2411 /* It works with head == NULL */
2412 qapi_free_MigrationCapabilityStatusList(head);
2413
2414 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08002415}
2416
2417static const TypeInfo migration_type = {
2418 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08002419 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08002420 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2421 * not created using qdev_create(), it is not attached to the qdev
2422 * device tree, and it is never realized.
2423 *
2424 * TODO: Make this TYPE_OBJECT once QOM provides something like
2425 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08002426 */
Peter Xue5cb7e72017-06-27 12:10:13 +08002427 .parent = TYPE_DEVICE,
2428 .class_init = migration_class_init,
2429 .class_size = sizeof(MigrationClass),
2430 .instance_size = sizeof(MigrationState),
2431 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002432 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08002433};
2434
2435static void register_migration_types(void)
2436{
2437 type_register_static(&migration_type);
2438}
2439
2440type_init(register_migration_types);