blob: cd32eacc6963192f131fea67b24eeec30927f115 [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
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000233 if (!strcmp(uri, "defer")) {
234 deferred_incoming_migration(errp);
235 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200236 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400237#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000238 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400239 rdma_start_incoming_migration(p, errp);
240#endif
aliguori065e2812008-11-11 16:46:33 +0000241#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000242 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200243 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000244 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200245 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000246 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200247 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000248#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000249 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100250 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200251 }
aliguori5bb79102008-10-13 03:12:02 +0000252}
253
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200254static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200255{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200256 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100257 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200258 int ret;
259
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100260 migration_incoming_state_new(f);
261
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200262 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100263
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200264 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200265 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100266 migration_incoming_state_destroy();
267
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200268 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200269 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800270 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600271 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200272 }
273 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200274
Anthony Liguori0f154232011-11-14 15:09:45 -0600275 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100276 bdrv_invalidate_cache_all(&local_err);
277 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100278 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800279 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100280 exit(EXIT_FAILURE);
281 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600282
Juan Quinteladf4b1022014-10-08 10:58:10 +0200283 /* runstate == "" means that we haven't received it through the
284 * wire, so we obey autostart. runstate == runing means that we
285 * need to run it, we need to make sure that we do it after
286 * everything else has finished. Every other state change is done
287 * at the post_load function */
288
289 if (strcmp(global_state_get_runstate(), "running") == 0) {
Juan Quintela511c0232010-06-09 14:10:55 +0200290 vm_start();
Juan Quinteladf4b1022014-10-08 10:58:10 +0200291 } else if (strcmp(global_state_get_runstate(), "") == 0) {
292 if (autostart) {
293 vm_start();
294 } else {
295 runstate_set(RUN_STATE_PAUSED);
296 }
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300297 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800298 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200299}
300
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200301void process_incoming_migration(QEMUFile *f)
302{
303 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
304 int fd = qemu_get_fd(f);
305
306 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800307 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100308 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200309 qemu_coroutine_enter(co, f);
310}
311
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400312/* amount of nanoseconds we are willing to wait for migration to be down.
313 * the choice of nanoseconds is because it is the maximum resolution that
314 * get_clock() can achieve. It is an internal measure. All user-visible
315 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100316static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400317
318uint64_t migrate_max_downtime(void)
319{
320 return max_downtime;
321}
322
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300323MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
324{
325 MigrationCapabilityStatusList *head = NULL;
326 MigrationCapabilityStatusList *caps;
327 MigrationState *s = migrate_get_current();
328 int i;
329
Michael Tokarev387eede2013-10-05 13:18:28 +0400330 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300331 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
332 if (head == NULL) {
333 head = g_malloc0(sizeof(*caps));
334 caps = head;
335 } else {
336 caps->next = g_malloc0(sizeof(*caps));
337 caps = caps->next;
338 }
339 caps->value =
340 g_malloc(sizeof(*caps->value));
341 caps->value->capability = i;
342 caps->value->state = s->enabled_capabilities[i];
343 }
344
345 return head;
346}
347
Liang Li85de8322015-03-23 16:32:28 +0800348MigrationParameters *qmp_query_migrate_parameters(Error **errp)
349{
350 MigrationParameters *params;
351 MigrationState *s = migrate_get_current();
352
353 params = g_malloc0(sizeof(*params));
354 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
355 params->compress_threads =
356 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
357 params->decompress_threads =
358 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
359
360 return params;
361}
362
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300363static void get_xbzrle_cache_stats(MigrationInfo *info)
364{
365 if (migrate_use_xbzrle()) {
366 info->has_xbzrle_cache = true;
367 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
368 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
369 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
370 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
371 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800372 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300373 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
374 }
375}
376
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300377MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000378{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300379 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200380 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000381
Juan Quintela17549e82011-10-05 13:50:43 +0200382 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800383 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200384 /* no migration has happened ever */
385 break;
zhanghailiang31194732015-03-13 16:08:38 +0800386 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400387 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400388 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400389 break;
zhanghailiang31194732015-03-13 16:08:38 +0800390 case MIGRATION_STATUS_ACTIVE:
391 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300392 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200393 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100394 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200395 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200396 info->has_expected_downtime = true;
397 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400398 info->has_setup_time = true;
399 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200400
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300401 info->has_ram = true;
402 info->ram = g_malloc0(sizeof(*info->ram));
403 info->ram->transferred = ram_bytes_transferred();
404 info->ram->remaining = ram_bytes_remaining();
405 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300406 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100407 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300408 info->ram->normal = norm_mig_pages_transferred();
409 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200410 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400411 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800412 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200413
Juan Quintela17549e82011-10-05 13:50:43 +0200414 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300415 info->has_disk = true;
416 info->disk = g_malloc0(sizeof(*info->disk));
417 info->disk->transferred = blk_mig_bytes_transferred();
418 info->disk->remaining = blk_mig_bytes_remaining();
419 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000420 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300421
422 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200423 break;
zhanghailiang31194732015-03-13 16:08:38 +0800424 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300425 get_xbzrle_cache_stats(info);
426
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300427 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900428 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200429 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200430 info->has_downtime = true;
431 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400432 info->has_setup_time = true;
433 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200434
435 info->has_ram = true;
436 info->ram = g_malloc0(sizeof(*info->ram));
437 info->ram->transferred = ram_bytes_transferred();
438 info->ram->remaining = 0;
439 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300440 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100441 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300442 info->ram->normal = norm_mig_pages_transferred();
443 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400444 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800445 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200446 break;
zhanghailiang31194732015-03-13 16:08:38 +0800447 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300448 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200449 break;
zhanghailiang31194732015-03-13 16:08:38 +0800450 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300451 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200452 break;
aliguori5bb79102008-10-13 03:12:02 +0000453 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800454 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300455
456 return info;
aliguori5bb79102008-10-13 03:12:02 +0000457}
458
Orit Wasserman00458432012-08-06 21:42:48 +0300459void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
460 Error **errp)
461{
462 MigrationState *s = migrate_get_current();
463 MigrationCapabilityStatusList *cap;
464
zhanghailiang31194732015-03-13 16:08:38 +0800465 if (s->state == MIGRATION_STATUS_ACTIVE ||
466 s->state == MIGRATION_STATUS_SETUP) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100467 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300468 return;
469 }
470
471 for (cap = params; cap; cap = cap->next) {
472 s->enabled_capabilities[cap->value->capability] = cap->value->state;
473 }
474}
475
Liang Li85de8322015-03-23 16:32:28 +0800476void qmp_migrate_set_parameters(bool has_compress_level,
477 int64_t compress_level,
478 bool has_compress_threads,
479 int64_t compress_threads,
480 bool has_decompress_threads,
481 int64_t decompress_threads, Error **errp)
482{
483 MigrationState *s = migrate_get_current();
484
485 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100486 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
487 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800488 return;
489 }
490 if (has_compress_threads &&
491 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100492 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
493 "compress_threads",
494 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800495 return;
496 }
497 if (has_decompress_threads &&
498 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100499 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
500 "decompress_threads",
501 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800502 return;
503 }
504
505 if (has_compress_level) {
506 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
507 }
508 if (has_compress_threads) {
509 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
510 }
511 if (has_decompress_threads) {
512 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
513 decompress_threads;
514 }
515}
516
aliguori065e2812008-11-11 16:46:33 +0000517/* shared migration helpers */
518
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000519static void migrate_set_state(MigrationState *s, int old_state, int new_state)
520{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200521 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintelab05dc722015-07-07 14:44:05 +0200522 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000523 }
524}
525
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100526static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000527{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100528 MigrationState *s = opaque;
529
530 qemu_bh_delete(s->cleanup_bh);
531 s->cleanup_bh = NULL;
532
aliguori065e2812008-11-11 16:46:33 +0000533 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100534 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100535 qemu_mutex_unlock_iothread();
536 qemu_thread_join(&s->thread);
537 qemu_mutex_lock_iothread();
538
Liang Li8706d2d2015-03-23 16:32:17 +0800539 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100540 qemu_fclose(s->file);
541 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000542 }
543
zhanghailiang31194732015-03-13 16:08:38 +0800544 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100545
zhanghailiang31194732015-03-13 16:08:38 +0800546 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100547 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800548 if (s->state == MIGRATION_STATUS_CANCELLING) {
549 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
550 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000551 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100552 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100553
554 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000555}
556
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200557void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000558{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100559 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100560 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200561 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100562 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100563}
564
Juan Quintela0edda1c2010-05-11 16:28:39 +0200565static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000566{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000567 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000568 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100569 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000570
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000571 do {
572 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800573 if (old_state != MIGRATION_STATUS_SETUP &&
574 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000575 break;
576 }
zhanghailiang31194732015-03-13 16:08:38 +0800577 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
578 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000579
580 /*
581 * If we're unlucky the migration code might be stuck somewhere in a
582 * send/write while the network has failed and is waiting to timeout;
583 * if we've got shutdown(2) available then we can force it to quit.
584 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
585 * called in a bh, so there is no race against this cancel.
586 */
zhanghailiang31194732015-03-13 16:08:38 +0800587 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000588 qemu_file_shutdown(f);
589 }
aliguori065e2812008-11-11 16:46:33 +0000590}
591
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100592void add_migration_state_change_notifier(Notifier *notify)
593{
594 notifier_list_add(&migration_state_notifiers, notify);
595}
596
597void remove_migration_state_change_notifier(Notifier *notify)
598{
Paolo Bonzini31552522012-01-13 17:34:01 +0100599 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100600}
601
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200602bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200603{
zhanghailiang31194732015-03-13 16:08:38 +0800604 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200605}
606
Juan Quintela70736932011-02-23 00:43:59 +0100607bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100608{
zhanghailiang31194732015-03-13 16:08:38 +0800609 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100610}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200611
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200612bool migration_has_failed(MigrationState *s)
613{
zhanghailiang31194732015-03-13 16:08:38 +0800614 return (s->state == MIGRATION_STATUS_CANCELLED ||
615 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200616}
617
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300618static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200619{
Juan Quintela17549e82011-10-05 13:50:43 +0200620 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100621 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300622 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300623 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800624 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
625 int compress_thread_count =
626 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
627 int decompress_thread_count =
628 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300629
630 memcpy(enabled_capabilities, s->enabled_capabilities,
631 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200632
Juan Quintela17549e82011-10-05 13:50:43 +0200633 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300634 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300635 memcpy(s->enabled_capabilities, enabled_capabilities,
636 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300637 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100638
Liang Li43c60a82015-03-23 16:32:27 +0800639 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
640 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
641 compress_thread_count;
642 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
643 decompress_thread_count;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200644 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200645 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200646
Alex Blighbc72ad62013-08-21 16:03:08 +0100647 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200648 return s;
649}
Juan Quintelacab30142011-02-22 23:54:21 +0100650
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600651static GSList *migration_blockers;
652
653void migrate_add_blocker(Error *reason)
654{
655 migration_blockers = g_slist_prepend(migration_blockers, reason);
656}
657
658void migrate_del_blocker(Error *reason)
659{
660 migration_blockers = g_slist_remove(migration_blockers, reason);
661}
662
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000663void qmp_migrate_incoming(const char *uri, Error **errp)
664{
665 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000666 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000667
668 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000669 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000670 return;
671 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000672 if (!once) {
673 error_setg(errp, "The incoming migration has already been started");
674 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000675
676 qemu_start_incoming_migration(uri, &local_err);
677
678 if (local_err) {
679 error_propagate(errp, local_err);
680 return;
681 }
682
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000683 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000684}
685
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200686void qmp_migrate(const char *uri, bool has_blk, bool blk,
687 bool has_inc, bool inc, bool has_detach, bool detach,
688 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100689{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200690 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200691 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300692 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100693 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100694
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900695 params.blk = has_blk && blk;
696 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300697
zhanghailiang31194732015-03-13 16:08:38 +0800698 if (s->state == MIGRATION_STATUS_ACTIVE ||
699 s->state == MIGRATION_STATUS_SETUP ||
700 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100701 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200702 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100703 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100704 if (runstate_check(RUN_STATE_INMIGRATE)) {
705 error_setg(errp, "Guest is waiting for an incoming migration");
706 return;
707 }
708
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200709 if (qemu_savevm_state_blocked(errp)) {
710 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100711 }
712
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600713 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200714 *errp = error_copy(migration_blockers->data);
715 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600716 }
717
Juan Quintela656a2332015-07-01 09:32:29 +0200718 /* We are starting a new migration, so we want to start in a clean
719 state. This change is only needed if previous migration
720 failed/was cancelled. We don't use migrate_set_state() because
721 we are setting the initial state, not changing it. */
722 s->state = MIGRATION_STATUS_NONE;
723
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300724 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100725
726 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200727 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400728#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800729 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400730 rdma_start_outgoing_migration(s, p, &local_err);
731#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100732#if !defined(WIN32)
733 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200734 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100735 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200736 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100737 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200738 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100739#endif
740 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100741 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
742 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +0200743 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200744 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100745 }
746
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200747 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200748 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200749 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200750 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100751 }
Juan Quintelacab30142011-02-22 23:54:21 +0100752}
753
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200754void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100755{
Juan Quintela17549e82011-10-05 13:50:43 +0200756 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100757}
758
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300759void qmp_migrate_set_cache_size(int64_t value, Error **errp)
760{
761 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200762 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300763
764 /* Check for truncation */
765 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100766 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
767 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300768 return;
769 }
770
Orit Wassermana5615b12014-01-30 20:08:36 +0200771 /* Cache should not be larger than guest ram size */
772 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100773 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
774 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +0200775 return;
776 }
777
Orit Wassermanc91e6812014-01-30 20:08:34 +0200778 new_size = xbzrle_cache_resize(value);
779 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100780 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
781 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +0200782 return;
783 }
784
785 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300786}
787
788int64_t qmp_query_migrate_cache_size(Error **errp)
789{
790 return migrate_xbzrle_cache_size();
791}
792
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200793void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100794{
Juan Quintelacab30142011-02-22 23:54:21 +0100795 MigrationState *s;
796
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200797 if (value < 0) {
798 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100799 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100800 if (value > SIZE_MAX) {
801 value = SIZE_MAX;
802 }
Juan Quintelacab30142011-02-22 23:54:21 +0100803
Juan Quintela17549e82011-10-05 13:50:43 +0200804 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200805 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100806 if (s->file) {
807 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
808 }
Juan Quintelacab30142011-02-22 23:54:21 +0100809}
810
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200811void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100812{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200813 value *= 1e9;
814 value = MAX(0, MIN(UINT64_MAX, value));
815 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000816}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300817
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600818bool migrate_auto_converge(void)
819{
820 MigrationState *s;
821
822 s = migrate_get_current();
823
824 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
825}
826
Peter Lieven323004a2013-07-18 09:48:50 +0200827bool migrate_zero_blocks(void)
828{
829 MigrationState *s;
830
831 s = migrate_get_current();
832
833 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
834}
835
Liang Li8706d2d2015-03-23 16:32:17 +0800836bool migrate_use_compression(void)
837{
Liang Lidde4e692015-03-23 16:32:26 +0800838 MigrationState *s;
839
840 s = migrate_get_current();
841
842 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800843}
844
845int migrate_compress_level(void)
846{
847 MigrationState *s;
848
849 s = migrate_get_current();
850
Liang Li43c60a82015-03-23 16:32:27 +0800851 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800852}
853
854int migrate_compress_threads(void)
855{
856 MigrationState *s;
857
858 s = migrate_get_current();
859
Liang Li43c60a82015-03-23 16:32:27 +0800860 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800861}
862
Liang Li3fcb38c2015-03-23 16:32:18 +0800863int migrate_decompress_threads(void)
864{
865 MigrationState *s;
866
867 s = migrate_get_current();
868
Liang Li43c60a82015-03-23 16:32:27 +0800869 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800870}
871
Juan Quintelab05dc722015-07-07 14:44:05 +0200872bool migrate_use_events(void)
873{
874 MigrationState *s;
875
876 s = migrate_get_current();
877
878 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
879}
880
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300881int migrate_use_xbzrle(void)
882{
883 MigrationState *s;
884
885 s = migrate_get_current();
886
887 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
888}
889
890int64_t migrate_xbzrle_cache_size(void)
891{
892 MigrationState *s;
893
894 s = migrate_get_current();
895
896 return s->xbzrle_cache_size;
897}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200898
899/* migration thread support */
900
Juan Quintela5f496a12013-02-22 17:36:30 +0100901static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200902{
Juan Quintela9848a402012-12-19 09:55:50 +0100903 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100904 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
905 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100906 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200907 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100908 int64_t start_time = initial_time;
909 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +0200910
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +0100911 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100912 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200913
Alex Blighbc72ad62013-08-21 16:03:08 +0100914 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +0800915 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400916
zhanghailiang31194732015-03-13 16:08:38 +0800917 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +0100918 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +0200919 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200920
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100921 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +0200922 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100923 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +0200924 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100925 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +0200926 } else {
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200927 int ret;
928
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100929 qemu_mutex_lock_iothread();
Alex Blighbc72ad62013-08-21 16:03:08 +0100930 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintelac369f402012-10-03 20:33:34 +0200931 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100932 old_vm_running = runstate_is_running();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200933
Juan Quinteladf4b1022014-10-08 10:58:10 +0200934 ret = global_state_store();
935 if (!ret) {
936 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
937 if (ret >= 0) {
938 qemu_file_set_rate_limit(s->file, INT64_MAX);
939 qemu_savevm_state_complete(s->file);
940 }
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200941 }
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100942 qemu_mutex_unlock_iothread();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200943
944 if (ret < 0) {
zhanghailiang31194732015-03-13 16:08:38 +0800945 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
946 MIGRATION_STATUS_FAILED);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200947 break;
948 }
949
Paolo Bonzini059f8962013-02-22 17:36:32 +0100950 if (!qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800951 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
952 MIGRATION_STATUS_COMPLETED);
Paolo Bonzini059f8962013-02-22 17:36:32 +0100953 break;
954 }
Juan Quintelac369f402012-10-03 20:33:34 +0200955 }
956 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100957
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100958 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800959 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
960 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100961 break;
962 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100963 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200964 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100965 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -0500966 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200967 double bandwidth = transferred_bytes / time_spent;
968 max_size = bandwidth * migrate_max_downtime() / 1000000;
969
Michael R. Hines7e114f82013-06-25 21:35:30 -0400970 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
971 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
972
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100973 trace_migrate_transferred(transferred_bytes, time_spent,
974 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100975 /* if we haven't sent anything, we don't want to recalculate
976 10000 is a small enough number for our purposes */
977 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
978 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
979 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200980
Paolo Bonzini1964a392013-02-22 17:36:45 +0100981 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200982 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100983 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200984 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100985 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200986 /* usleep expects microseconds */
987 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
988 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100989 }
990
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100991 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +0800992 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100993 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +0200994 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100995 s->total_time = end_time - s->total_time;
996 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +0200997 if (s->total_time) {
998 s->mbps = (((double) transferred_bytes * 8.0) /
999 ((double) s->total_time)) / 1000;
1000 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001001 runstate_set(RUN_STATE_POSTMIGRATE);
1002 } else {
1003 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001004 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001005 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001006 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001007 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001008 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001009
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001010 return NULL;
1011}
1012
Juan Quintela9848a402012-12-19 09:55:50 +01001013void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001014{
Juan Quintelacc283e32013-02-01 11:12:26 +01001015 /* This is a best 1st approximation. ns to ms */
1016 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001017 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001018
Paolo Bonzini442773c2013-02-22 17:36:44 +01001019 qemu_file_set_rate_limit(s->file,
1020 s->bandwidth_limit / XFER_LIMIT_RATIO);
1021
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001022 /* Notify before starting migration thread */
1023 notifier_list_notify(&migration_state_notifiers, s);
1024
Liang Li8706d2d2015-03-23 16:32:17 +08001025 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001026 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001027 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001028}