blob: 45719a0f5f8264cdcb7124a7dc8a7952b992da94 [file] [log] [blame]
aliguori5bb79102008-10-13 03:12:02 +00001/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori5bb79102008-10-13 03:12:02 +000014 */
15
16#include "qemu-common.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010017#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010018#include "qemu/main-loop.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010019#include "migration/migration.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020020#include "migration/qemu-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010021#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010023#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/sockets.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010025#include "migration/block.h"
Juan Quintela766bd172012-07-23 05:45:29 +020026#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030027#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010028#include "trace.h"
Juan Quinteladf4b1022014-10-08 10:58:10 +020029#include "qapi/util.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020030#include "qapi-event.h"
aliguori065e2812008-11-11 16:46:33 +000031
Juan Quintelad0ae46c2011-02-23 00:33:19 +010032#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000033
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010034/* Amount of time to allocate to each "chunk" of bandwidth-throttled
35 * data. */
36#define BUFFER_DELAY 100
37#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
38
Liang Li8706d2d2015-03-23 16:32:17 +080039/* Default compression thread count */
40#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080041/* Default decompression thread count, usually decompression is at
42 * least 4 times as fast as compression.*/
43#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080044/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
45#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
46
Orit Wasserman17ad9b32012-08-06 21:42:53 +030047/* Migration XBZRLE default cache size */
48#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
49
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010050static NotifierList migration_state_notifiers =
51 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
52
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000053static bool deferred_incoming;
54
Juan Quintela17549e82011-10-05 13:50:43 +020055/* When we add fault tolerance, we could have several
56 migrations at once. For now we don't need to add
57 dynamic creation of migration */
58
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010059/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020060MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020061{
62 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080063 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010064 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030065 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040066 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080067 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
68 DEFAULT_MIGRATE_COMPRESS_LEVEL,
69 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
70 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
71 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
72 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Juan Quintela17549e82011-10-05 13:50:43 +020073 };
74
75 return &current_migration;
76}
77
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010078/* For incoming */
79static MigrationIncomingState *mis_current;
80
81MigrationIncomingState *migration_incoming_get_current(void)
82{
83 return mis_current;
84}
85
86MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
87{
88 mis_current = g_malloc0(sizeof(MigrationIncomingState));
89 mis_current->file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010090 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010091
92 return mis_current;
93}
94
95void migration_incoming_state_destroy(void)
96{
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010097 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010098 g_free(mis_current);
99 mis_current = NULL;
100}
101
Juan Quinteladf4b1022014-10-08 10:58:10 +0200102
103typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200104 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200105 uint32_t size;
106 uint8_t runstate[100];
107} GlobalState;
108
109static GlobalState global_state;
110
111static int global_state_store(void)
112{
113 if (!runstate_store((char *)global_state.runstate,
114 sizeof(global_state.runstate))) {
115 error_report("runstate name too big: %s", global_state.runstate);
116 trace_migrate_state_too_big();
117 return -EINVAL;
118 }
119 return 0;
120}
121
122static char *global_state_get_runstate(void)
123{
124 return (char *)global_state.runstate;
125}
126
Juan Quintela13d16812014-10-08 13:58:24 +0200127void global_state_set_optional(void)
128{
129 global_state.optional = true;
130}
131
132static bool global_state_needed(void *opaque)
133{
134 GlobalState *s = opaque;
135 char *runstate = (char *)s->runstate;
136
137 /* If it is not optional, it is mandatory */
138
139 if (s->optional == false) {
140 return true;
141 }
142
143 /* If state is running or paused, it is not needed */
144
145 if (strcmp(runstate, "running") == 0 ||
146 strcmp(runstate, "paused") == 0) {
147 return false;
148 }
149
150 /* for any other state it is needed */
151 return true;
152}
153
Juan Quinteladf4b1022014-10-08 10:58:10 +0200154static int global_state_post_load(void *opaque, int version_id)
155{
156 GlobalState *s = opaque;
157 int ret = 0;
158 char *runstate = (char *)s->runstate;
159
160 trace_migrate_global_state_post_load(runstate);
161
162 if (strcmp(runstate, "running") != 0) {
163 Error *local_err = NULL;
164 int r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
165 -1, &local_err);
166
167 if (r == -1) {
168 if (local_err) {
169 error_report_err(local_err);
170 }
171 return -EINVAL;
172 }
173 ret = vm_stop_force_state(r);
174 }
175
176 return ret;
177}
178
179static void global_state_pre_save(void *opaque)
180{
181 GlobalState *s = opaque;
182
183 trace_migrate_global_state_pre_save((char *)s->runstate);
184 s->size = strlen((char *)s->runstate) + 1;
185}
186
187static const VMStateDescription vmstate_globalstate = {
188 .name = "globalstate",
189 .version_id = 1,
190 .minimum_version_id = 1,
191 .post_load = global_state_post_load,
192 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200193 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200194 .fields = (VMStateField[]) {
195 VMSTATE_UINT32(size, GlobalState),
196 VMSTATE_BUFFER(runstate, GlobalState),
197 VMSTATE_END_OF_LIST()
198 },
199};
200
201void register_global_state(void)
202{
203 /* We would use it independently that we receive it */
204 strcpy((char *)&global_state.runstate, "");
205 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
206}
207
Juan Quintelab05dc722015-07-07 14:44:05 +0200208static void migrate_generate_event(int new_state)
209{
210 if (migrate_use_events()) {
211 qapi_event_send_migration(new_state, &error_abort);
212 trace_migrate_set_state(new_state);
213 }
214}
215
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000216/*
217 * Called on -incoming with a defer: uri.
218 * The migration can be started later after any parameters have been
219 * changed.
220 */
221static void deferred_incoming_migration(Error **errp)
222{
223 if (deferred_incoming) {
224 error_setg(errp, "Incoming migration already deferred");
225 }
226 deferred_incoming = true;
227}
228
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200229void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000230{
aliguori34c9dd82008-10-13 03:14:31 +0000231 const char *p;
232
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200233 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000234 if (!strcmp(uri, "defer")) {
235 deferred_incoming_migration(errp);
236 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200237 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400238#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000239 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400240 rdma_start_incoming_migration(p, errp);
241#endif
aliguori065e2812008-11-11 16:46:33 +0000242#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000243 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200244 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000245 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200246 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000247 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200248 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000249#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000250 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100251 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200252 }
aliguori5bb79102008-10-13 03:12:02 +0000253}
254
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200255static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200256{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200257 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100258 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200259 int ret;
260
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100261 migration_incoming_state_new(f);
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200262 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200263 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100264
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200265 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200266 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100267 migration_incoming_state_destroy();
268
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200269 if (ret < 0) {
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200270 migrate_generate_event(MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200271 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800272 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600273 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200274 }
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200275 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
Juan Quintela511c0232010-06-09 14:10:55 +0200276 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200277
Anthony Liguori0f154232011-11-14 15:09:45 -0600278 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100279 bdrv_invalidate_cache_all(&local_err);
280 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100281 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800282 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100283 exit(EXIT_FAILURE);
284 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600285
Juan Quinteladf4b1022014-10-08 10:58:10 +0200286 /* runstate == "" means that we haven't received it through the
287 * wire, so we obey autostart. runstate == runing means that we
288 * need to run it, we need to make sure that we do it after
289 * everything else has finished. Every other state change is done
290 * at the post_load function */
291
292 if (strcmp(global_state_get_runstate(), "running") == 0) {
Juan Quintela511c0232010-06-09 14:10:55 +0200293 vm_start();
Juan Quinteladf4b1022014-10-08 10:58:10 +0200294 } else if (strcmp(global_state_get_runstate(), "") == 0) {
295 if (autostart) {
296 vm_start();
297 } else {
298 runstate_set(RUN_STATE_PAUSED);
299 }
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300300 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800301 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200302}
303
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200304void process_incoming_migration(QEMUFile *f)
305{
306 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
307 int fd = qemu_get_fd(f);
308
309 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800310 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100311 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200312 qemu_coroutine_enter(co, f);
313}
314
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400315/* amount of nanoseconds we are willing to wait for migration to be down.
316 * the choice of nanoseconds is because it is the maximum resolution that
317 * get_clock() can achieve. It is an internal measure. All user-visible
318 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100319static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400320
321uint64_t migrate_max_downtime(void)
322{
323 return max_downtime;
324}
325
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300326MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
327{
328 MigrationCapabilityStatusList *head = NULL;
329 MigrationCapabilityStatusList *caps;
330 MigrationState *s = migrate_get_current();
331 int i;
332
Michael Tokarev387eede2013-10-05 13:18:28 +0400333 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300334 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
335 if (head == NULL) {
336 head = g_malloc0(sizeof(*caps));
337 caps = head;
338 } else {
339 caps->next = g_malloc0(sizeof(*caps));
340 caps = caps->next;
341 }
342 caps->value =
343 g_malloc(sizeof(*caps->value));
344 caps->value->capability = i;
345 caps->value->state = s->enabled_capabilities[i];
346 }
347
348 return head;
349}
350
Liang Li85de8322015-03-23 16:32:28 +0800351MigrationParameters *qmp_query_migrate_parameters(Error **errp)
352{
353 MigrationParameters *params;
354 MigrationState *s = migrate_get_current();
355
356 params = g_malloc0(sizeof(*params));
357 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
358 params->compress_threads =
359 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
360 params->decompress_threads =
361 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
362
363 return params;
364}
365
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300366static void get_xbzrle_cache_stats(MigrationInfo *info)
367{
368 if (migrate_use_xbzrle()) {
369 info->has_xbzrle_cache = true;
370 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
371 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
372 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
373 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
374 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800375 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300376 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
377 }
378}
379
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300380MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000381{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300382 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200383 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000384
Juan Quintela17549e82011-10-05 13:50:43 +0200385 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800386 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200387 /* no migration has happened ever */
388 break;
zhanghailiang31194732015-03-13 16:08:38 +0800389 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400390 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400391 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400392 break;
zhanghailiang31194732015-03-13 16:08:38 +0800393 case MIGRATION_STATUS_ACTIVE:
394 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300395 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200396 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100397 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200398 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200399 info->has_expected_downtime = true;
400 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400401 info->has_setup_time = true;
402 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200403
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300404 info->has_ram = true;
405 info->ram = g_malloc0(sizeof(*info->ram));
406 info->ram->transferred = ram_bytes_transferred();
407 info->ram->remaining = ram_bytes_remaining();
408 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300409 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100410 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300411 info->ram->normal = norm_mig_pages_transferred();
412 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200413 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400414 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800415 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200416
Juan Quintela17549e82011-10-05 13:50:43 +0200417 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300418 info->has_disk = true;
419 info->disk = g_malloc0(sizeof(*info->disk));
420 info->disk->transferred = blk_mig_bytes_transferred();
421 info->disk->remaining = blk_mig_bytes_remaining();
422 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000423 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300424
425 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200426 break;
zhanghailiang31194732015-03-13 16:08:38 +0800427 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300428 get_xbzrle_cache_stats(info);
429
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300430 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900431 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200432 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200433 info->has_downtime = true;
434 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400435 info->has_setup_time = true;
436 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200437
438 info->has_ram = true;
439 info->ram = g_malloc0(sizeof(*info->ram));
440 info->ram->transferred = ram_bytes_transferred();
441 info->ram->remaining = 0;
442 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300443 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100444 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300445 info->ram->normal = norm_mig_pages_transferred();
446 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400447 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800448 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200449 break;
zhanghailiang31194732015-03-13 16:08:38 +0800450 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300451 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200452 break;
zhanghailiang31194732015-03-13 16:08:38 +0800453 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300454 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200455 break;
aliguori5bb79102008-10-13 03:12:02 +0000456 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800457 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300458
459 return info;
aliguori5bb79102008-10-13 03:12:02 +0000460}
461
Orit Wasserman00458432012-08-06 21:42:48 +0300462void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
463 Error **errp)
464{
465 MigrationState *s = migrate_get_current();
466 MigrationCapabilityStatusList *cap;
467
zhanghailiang31194732015-03-13 16:08:38 +0800468 if (s->state == MIGRATION_STATUS_ACTIVE ||
469 s->state == MIGRATION_STATUS_SETUP) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100470 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300471 return;
472 }
473
474 for (cap = params; cap; cap = cap->next) {
475 s->enabled_capabilities[cap->value->capability] = cap->value->state;
476 }
477}
478
Liang Li85de8322015-03-23 16:32:28 +0800479void qmp_migrate_set_parameters(bool has_compress_level,
480 int64_t compress_level,
481 bool has_compress_threads,
482 int64_t compress_threads,
483 bool has_decompress_threads,
484 int64_t decompress_threads, Error **errp)
485{
486 MigrationState *s = migrate_get_current();
487
488 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100489 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
490 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800491 return;
492 }
493 if (has_compress_threads &&
494 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100495 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
496 "compress_threads",
497 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800498 return;
499 }
500 if (has_decompress_threads &&
501 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100502 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
503 "decompress_threads",
504 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800505 return;
506 }
507
508 if (has_compress_level) {
509 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
510 }
511 if (has_compress_threads) {
512 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
513 }
514 if (has_decompress_threads) {
515 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
516 decompress_threads;
517 }
518}
519
aliguori065e2812008-11-11 16:46:33 +0000520/* shared migration helpers */
521
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000522static void migrate_set_state(MigrationState *s, int old_state, int new_state)
523{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200524 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintelab05dc722015-07-07 14:44:05 +0200525 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000526 }
527}
528
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100529static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000530{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100531 MigrationState *s = opaque;
532
533 qemu_bh_delete(s->cleanup_bh);
534 s->cleanup_bh = NULL;
535
aliguori065e2812008-11-11 16:46:33 +0000536 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100537 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100538 qemu_mutex_unlock_iothread();
539 qemu_thread_join(&s->thread);
540 qemu_mutex_lock_iothread();
541
Liang Li8706d2d2015-03-23 16:32:17 +0800542 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100543 qemu_fclose(s->file);
544 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000545 }
546
zhanghailiang31194732015-03-13 16:08:38 +0800547 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100548
zhanghailiang31194732015-03-13 16:08:38 +0800549 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100550 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800551 if (s->state == MIGRATION_STATUS_CANCELLING) {
552 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
553 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000554 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100555 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100556
557 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000558}
559
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200560void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000561{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100562 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100563 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200564 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100565 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100566}
567
Juan Quintela0edda1c2010-05-11 16:28:39 +0200568static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000569{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000570 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000571 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100572 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000573
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000574 do {
575 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800576 if (old_state != MIGRATION_STATUS_SETUP &&
577 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000578 break;
579 }
zhanghailiang31194732015-03-13 16:08:38 +0800580 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
581 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000582
583 /*
584 * If we're unlucky the migration code might be stuck somewhere in a
585 * send/write while the network has failed and is waiting to timeout;
586 * if we've got shutdown(2) available then we can force it to quit.
587 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
588 * called in a bh, so there is no race against this cancel.
589 */
zhanghailiang31194732015-03-13 16:08:38 +0800590 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000591 qemu_file_shutdown(f);
592 }
aliguori065e2812008-11-11 16:46:33 +0000593}
594
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100595void add_migration_state_change_notifier(Notifier *notify)
596{
597 notifier_list_add(&migration_state_notifiers, notify);
598}
599
600void remove_migration_state_change_notifier(Notifier *notify)
601{
Paolo Bonzini31552522012-01-13 17:34:01 +0100602 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100603}
604
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200605bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200606{
zhanghailiang31194732015-03-13 16:08:38 +0800607 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200608}
609
Juan Quintela70736932011-02-23 00:43:59 +0100610bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100611{
zhanghailiang31194732015-03-13 16:08:38 +0800612 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100613}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200614
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200615bool migration_has_failed(MigrationState *s)
616{
zhanghailiang31194732015-03-13 16:08:38 +0800617 return (s->state == MIGRATION_STATUS_CANCELLED ||
618 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200619}
620
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300621static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200622{
Juan Quintela17549e82011-10-05 13:50:43 +0200623 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100624 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300625 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300626 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800627 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
628 int compress_thread_count =
629 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
630 int decompress_thread_count =
631 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300632
633 memcpy(enabled_capabilities, s->enabled_capabilities,
634 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200635
Juan Quintela17549e82011-10-05 13:50:43 +0200636 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300637 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300638 memcpy(s->enabled_capabilities, enabled_capabilities,
639 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300640 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100641
Liang Li43c60a82015-03-23 16:32:27 +0800642 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
643 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
644 compress_thread_count;
645 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
646 decompress_thread_count;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200647 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200648 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200649
Alex Blighbc72ad62013-08-21 16:03:08 +0100650 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200651 return s;
652}
Juan Quintelacab30142011-02-22 23:54:21 +0100653
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600654static GSList *migration_blockers;
655
656void migrate_add_blocker(Error *reason)
657{
658 migration_blockers = g_slist_prepend(migration_blockers, reason);
659}
660
661void migrate_del_blocker(Error *reason)
662{
663 migration_blockers = g_slist_remove(migration_blockers, reason);
664}
665
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000666void qmp_migrate_incoming(const char *uri, Error **errp)
667{
668 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000669 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000670
671 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000672 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000673 return;
674 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000675 if (!once) {
676 error_setg(errp, "The incoming migration has already been started");
677 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000678
679 qemu_start_incoming_migration(uri, &local_err);
680
681 if (local_err) {
682 error_propagate(errp, local_err);
683 return;
684 }
685
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000686 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000687}
688
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200689void qmp_migrate(const char *uri, bool has_blk, bool blk,
690 bool has_inc, bool inc, bool has_detach, bool detach,
691 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100692{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200693 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200694 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300695 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100696 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100697
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900698 params.blk = has_blk && blk;
699 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300700
zhanghailiang31194732015-03-13 16:08:38 +0800701 if (s->state == MIGRATION_STATUS_ACTIVE ||
702 s->state == MIGRATION_STATUS_SETUP ||
703 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100704 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200705 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100706 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100707 if (runstate_check(RUN_STATE_INMIGRATE)) {
708 error_setg(errp, "Guest is waiting for an incoming migration");
709 return;
710 }
711
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200712 if (qemu_savevm_state_blocked(errp)) {
713 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100714 }
715
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600716 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200717 *errp = error_copy(migration_blockers->data);
718 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600719 }
720
Juan Quintela656a2332015-07-01 09:32:29 +0200721 /* We are starting a new migration, so we want to start in a clean
722 state. This change is only needed if previous migration
723 failed/was cancelled. We don't use migrate_set_state() because
724 we are setting the initial state, not changing it. */
725 s->state = MIGRATION_STATUS_NONE;
726
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300727 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100728
729 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200730 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400731#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800732 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400733 rdma_start_outgoing_migration(s, p, &local_err);
734#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100735#if !defined(WIN32)
736 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200737 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100738 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200739 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100740 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200741 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100742#endif
743 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100744 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
745 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +0200746 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200747 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100748 }
749
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200750 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200751 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200752 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200753 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100754 }
Juan Quintelacab30142011-02-22 23:54:21 +0100755}
756
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200757void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100758{
Juan Quintela17549e82011-10-05 13:50:43 +0200759 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100760}
761
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300762void qmp_migrate_set_cache_size(int64_t value, Error **errp)
763{
764 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200765 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300766
767 /* Check for truncation */
768 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100769 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
770 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300771 return;
772 }
773
Orit Wassermana5615b12014-01-30 20:08:36 +0200774 /* Cache should not be larger than guest ram size */
775 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100776 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
777 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +0200778 return;
779 }
780
Orit Wassermanc91e6812014-01-30 20:08:34 +0200781 new_size = xbzrle_cache_resize(value);
782 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100783 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
784 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +0200785 return;
786 }
787
788 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300789}
790
791int64_t qmp_query_migrate_cache_size(Error **errp)
792{
793 return migrate_xbzrle_cache_size();
794}
795
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200796void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100797{
Juan Quintelacab30142011-02-22 23:54:21 +0100798 MigrationState *s;
799
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200800 if (value < 0) {
801 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100802 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100803 if (value > SIZE_MAX) {
804 value = SIZE_MAX;
805 }
Juan Quintelacab30142011-02-22 23:54:21 +0100806
Juan Quintela17549e82011-10-05 13:50:43 +0200807 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200808 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100809 if (s->file) {
810 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
811 }
Juan Quintelacab30142011-02-22 23:54:21 +0100812}
813
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200814void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100815{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200816 value *= 1e9;
817 value = MAX(0, MIN(UINT64_MAX, value));
818 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000819}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300820
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600821bool migrate_auto_converge(void)
822{
823 MigrationState *s;
824
825 s = migrate_get_current();
826
827 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
828}
829
Peter Lieven323004a2013-07-18 09:48:50 +0200830bool migrate_zero_blocks(void)
831{
832 MigrationState *s;
833
834 s = migrate_get_current();
835
836 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
837}
838
Liang Li8706d2d2015-03-23 16:32:17 +0800839bool migrate_use_compression(void)
840{
Liang Lidde4e692015-03-23 16:32:26 +0800841 MigrationState *s;
842
843 s = migrate_get_current();
844
845 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800846}
847
848int migrate_compress_level(void)
849{
850 MigrationState *s;
851
852 s = migrate_get_current();
853
Liang Li43c60a82015-03-23 16:32:27 +0800854 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800855}
856
857int migrate_compress_threads(void)
858{
859 MigrationState *s;
860
861 s = migrate_get_current();
862
Liang Li43c60a82015-03-23 16:32:27 +0800863 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800864}
865
Liang Li3fcb38c2015-03-23 16:32:18 +0800866int migrate_decompress_threads(void)
867{
868 MigrationState *s;
869
870 s = migrate_get_current();
871
Liang Li43c60a82015-03-23 16:32:27 +0800872 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800873}
874
Juan Quintelab05dc722015-07-07 14:44:05 +0200875bool migrate_use_events(void)
876{
877 MigrationState *s;
878
879 s = migrate_get_current();
880
881 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
882}
883
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300884int migrate_use_xbzrle(void)
885{
886 MigrationState *s;
887
888 s = migrate_get_current();
889
890 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
891}
892
893int64_t migrate_xbzrle_cache_size(void)
894{
895 MigrationState *s;
896
897 s = migrate_get_current();
898
899 return s->xbzrle_cache_size;
900}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200901
902/* migration thread support */
903
Juan Quintela5f496a12013-02-22 17:36:30 +0100904static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200905{
Juan Quintela9848a402012-12-19 09:55:50 +0100906 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100907 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
908 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100909 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200910 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100911 int64_t start_time = initial_time;
912 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +0200913
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +0100914 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100915 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200916
Alex Blighbc72ad62013-08-21 16:03:08 +0100917 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +0800918 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400919
zhanghailiang31194732015-03-13 16:08:38 +0800920 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +0100921 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +0200922 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200923
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100924 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +0200925 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100926 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +0200927 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100928 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +0200929 } else {
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200930 int ret;
931
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100932 qemu_mutex_lock_iothread();
Alex Blighbc72ad62013-08-21 16:03:08 +0100933 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintelac369f402012-10-03 20:33:34 +0200934 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100935 old_vm_running = runstate_is_running();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200936
Juan Quinteladf4b1022014-10-08 10:58:10 +0200937 ret = global_state_store();
938 if (!ret) {
939 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
940 if (ret >= 0) {
941 qemu_file_set_rate_limit(s->file, INT64_MAX);
942 qemu_savevm_state_complete(s->file);
943 }
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200944 }
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100945 qemu_mutex_unlock_iothread();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200946
947 if (ret < 0) {
zhanghailiang31194732015-03-13 16:08:38 +0800948 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
949 MIGRATION_STATUS_FAILED);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200950 break;
951 }
952
Paolo Bonzini059f8962013-02-22 17:36:32 +0100953 if (!qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800954 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
955 MIGRATION_STATUS_COMPLETED);
Paolo Bonzini059f8962013-02-22 17:36:32 +0100956 break;
957 }
Juan Quintelac369f402012-10-03 20:33:34 +0200958 }
959 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100960
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100961 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800962 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
963 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100964 break;
965 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100966 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200967 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100968 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -0500969 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200970 double bandwidth = transferred_bytes / time_spent;
971 max_size = bandwidth * migrate_max_downtime() / 1000000;
972
Michael R. Hines7e114f82013-06-25 21:35:30 -0400973 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
974 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
975
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100976 trace_migrate_transferred(transferred_bytes, time_spent,
977 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100978 /* if we haven't sent anything, we don't want to recalculate
979 10000 is a small enough number for our purposes */
980 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
981 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
982 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200983
Paolo Bonzini1964a392013-02-22 17:36:45 +0100984 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200985 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100986 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200987 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100988 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200989 /* usleep expects microseconds */
990 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
991 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100992 }
993
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100994 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +0800995 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100996 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +0200997 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100998 s->total_time = end_time - s->total_time;
999 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +02001000 if (s->total_time) {
1001 s->mbps = (((double) transferred_bytes * 8.0) /
1002 ((double) s->total_time)) / 1000;
1003 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001004 runstate_set(RUN_STATE_POSTMIGRATE);
1005 } else {
1006 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001007 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001008 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001009 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001010 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001011 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001012
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001013 return NULL;
1014}
1015
Juan Quintela9848a402012-12-19 09:55:50 +01001016void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001017{
Juan Quintelacc283e32013-02-01 11:12:26 +01001018 /* This is a best 1st approximation. ns to ms */
1019 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001020 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001021
Paolo Bonzini442773c2013-02-22 17:36:44 +01001022 qemu_file_set_rate_limit(s->file,
1023 s->bandwidth_limit / XFER_LIMIT_RATIO);
1024
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001025 /* Notify before starting migration thread */
1026 notifier_list_notify(&migration_state_notifiers, s);
1027
Liang Li8706d2d2015-03-23 16:32:17 +08001028 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001029 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001030 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001031}