blob: 438bf91b54937ed3c7292212cf2e5129b316be8b [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"
Alex Bligh6a1751b2013-08-21 16:02:47 +010017#include "qemu/main-loop.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010018#include "migration/migration.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010019#include "monitor/monitor.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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/sockets.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010024#include "migration/block.h"
Juan Quintela766bd172012-07-23 05:45:29 +020025#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030026#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010027#include "trace.h"
aliguori065e2812008-11-11 16:46:33 +000028
Juan Quintelad0ae46c2011-02-23 00:33:19 +010029#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000030
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010031/* Amount of time to allocate to each "chunk" of bandwidth-throttled
32 * data. */
33#define BUFFER_DELAY 100
34#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
35
Liang Li8706d2d2015-03-23 16:32:17 +080036/* Default compression thread count */
37#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080038/* Default decompression thread count, usually decompression is at
39 * least 4 times as fast as compression.*/
40#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080041/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
42#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
43
Orit Wasserman17ad9b32012-08-06 21:42:53 +030044/* Migration XBZRLE default cache size */
45#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
46
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010047static NotifierList migration_state_notifiers =
48 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
49
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000050static bool deferred_incoming;
51
Juan Quintela17549e82011-10-05 13:50:43 +020052/* When we add fault tolerance, we could have several
53 migrations at once. For now we don't need to add
54 dynamic creation of migration */
55
Juan Quintela859bc752012-08-13 09:42:49 +020056MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020057{
58 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080059 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010060 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030061 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040062 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080063 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
64 DEFAULT_MIGRATE_COMPRESS_LEVEL,
65 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
66 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
67 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
68 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Juan Quintela17549e82011-10-05 13:50:43 +020069 };
70
71 return &current_migration;
72}
73
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000074/*
75 * Called on -incoming with a defer: uri.
76 * The migration can be started later after any parameters have been
77 * changed.
78 */
79static void deferred_incoming_migration(Error **errp)
80{
81 if (deferred_incoming) {
82 error_setg(errp, "Incoming migration already deferred");
83 }
84 deferred_incoming = true;
85}
86
Paolo Bonzini43eaae22012-10-02 18:21:18 +020087void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +000088{
aliguori34c9dd82008-10-13 03:14:31 +000089 const char *p;
90
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000091 if (!strcmp(uri, "defer")) {
92 deferred_incoming_migration(errp);
93 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +020094 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -040095#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000096 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -040097 rdma_start_incoming_migration(p, errp);
98#endif
aliguori065e2812008-11-11 16:46:33 +000099#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000100 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200101 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000102 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200103 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000104 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200105 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000106#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000107 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100108 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200109 }
aliguori5bb79102008-10-13 03:12:02 +0000110}
111
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200112static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200113{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200114 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100115 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200116 int ret;
117
118 ret = qemu_loadvm_state(f);
119 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200120 free_xbzrle_decoded_buf();
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200121 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200122 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800123 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600124 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200125 }
126 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200127
Anthony Liguori0f154232011-11-14 15:09:45 -0600128 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100129 bdrv_invalidate_cache_all(&local_err);
130 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100131 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800132 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100133 exit(EXIT_FAILURE);
134 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600135
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300136 if (autostart) {
Juan Quintela511c0232010-06-09 14:10:55 +0200137 vm_start();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300138 } else {
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200139 runstate_set(RUN_STATE_PAUSED);
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300140 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800141 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200142}
143
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200144void process_incoming_migration(QEMUFile *f)
145{
146 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
147 int fd = qemu_get_fd(f);
148
149 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800150 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100151 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200152 qemu_coroutine_enter(co, f);
153}
154
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400155/* amount of nanoseconds we are willing to wait for migration to be down.
156 * the choice of nanoseconds is because it is the maximum resolution that
157 * get_clock() can achieve. It is an internal measure. All user-visible
158 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100159static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400160
161uint64_t migrate_max_downtime(void)
162{
163 return max_downtime;
164}
165
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300166MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
167{
168 MigrationCapabilityStatusList *head = NULL;
169 MigrationCapabilityStatusList *caps;
170 MigrationState *s = migrate_get_current();
171 int i;
172
Michael Tokarev387eede2013-10-05 13:18:28 +0400173 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300174 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
175 if (head == NULL) {
176 head = g_malloc0(sizeof(*caps));
177 caps = head;
178 } else {
179 caps->next = g_malloc0(sizeof(*caps));
180 caps = caps->next;
181 }
182 caps->value =
183 g_malloc(sizeof(*caps->value));
184 caps->value->capability = i;
185 caps->value->state = s->enabled_capabilities[i];
186 }
187
188 return head;
189}
190
Liang Li85de8322015-03-23 16:32:28 +0800191MigrationParameters *qmp_query_migrate_parameters(Error **errp)
192{
193 MigrationParameters *params;
194 MigrationState *s = migrate_get_current();
195
196 params = g_malloc0(sizeof(*params));
197 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
198 params->compress_threads =
199 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
200 params->decompress_threads =
201 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
202
203 return params;
204}
205
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300206static void get_xbzrle_cache_stats(MigrationInfo *info)
207{
208 if (migrate_use_xbzrle()) {
209 info->has_xbzrle_cache = true;
210 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
211 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
212 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
213 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
214 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800215 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300216 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
217 }
218}
219
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300220MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000221{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300222 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200223 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000224
Juan Quintela17549e82011-10-05 13:50:43 +0200225 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800226 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200227 /* no migration has happened ever */
228 break;
zhanghailiang31194732015-03-13 16:08:38 +0800229 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400230 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400231 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400232 break;
zhanghailiang31194732015-03-13 16:08:38 +0800233 case MIGRATION_STATUS_ACTIVE:
234 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300235 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200236 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100237 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200238 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200239 info->has_expected_downtime = true;
240 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400241 info->has_setup_time = true;
242 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200243
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300244 info->has_ram = true;
245 info->ram = g_malloc0(sizeof(*info->ram));
246 info->ram->transferred = ram_bytes_transferred();
247 info->ram->remaining = ram_bytes_remaining();
248 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300249 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100250 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300251 info->ram->normal = norm_mig_pages_transferred();
252 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200253 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400254 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800255 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200256
Juan Quintela17549e82011-10-05 13:50:43 +0200257 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300258 info->has_disk = true;
259 info->disk = g_malloc0(sizeof(*info->disk));
260 info->disk->transferred = blk_mig_bytes_transferred();
261 info->disk->remaining = blk_mig_bytes_remaining();
262 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000263 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300264
265 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200266 break;
zhanghailiang31194732015-03-13 16:08:38 +0800267 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300268 get_xbzrle_cache_stats(info);
269
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300270 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900271 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200272 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200273 info->has_downtime = true;
274 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400275 info->has_setup_time = true;
276 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200277
278 info->has_ram = true;
279 info->ram = g_malloc0(sizeof(*info->ram));
280 info->ram->transferred = ram_bytes_transferred();
281 info->ram->remaining = 0;
282 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300283 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100284 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300285 info->ram->normal = norm_mig_pages_transferred();
286 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400287 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800288 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200289 break;
zhanghailiang31194732015-03-13 16:08:38 +0800290 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300291 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200292 break;
zhanghailiang31194732015-03-13 16:08:38 +0800293 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300294 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200295 break;
aliguori5bb79102008-10-13 03:12:02 +0000296 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800297 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300298
299 return info;
aliguori5bb79102008-10-13 03:12:02 +0000300}
301
Orit Wasserman00458432012-08-06 21:42:48 +0300302void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
303 Error **errp)
304{
305 MigrationState *s = migrate_get_current();
306 MigrationCapabilityStatusList *cap;
307
zhanghailiang31194732015-03-13 16:08:38 +0800308 if (s->state == MIGRATION_STATUS_ACTIVE ||
309 s->state == MIGRATION_STATUS_SETUP) {
Orit Wasserman00458432012-08-06 21:42:48 +0300310 error_set(errp, QERR_MIGRATION_ACTIVE);
311 return;
312 }
313
314 for (cap = params; cap; cap = cap->next) {
315 s->enabled_capabilities[cap->value->capability] = cap->value->state;
316 }
317}
318
Liang Li85de8322015-03-23 16:32:28 +0800319void qmp_migrate_set_parameters(bool has_compress_level,
320 int64_t compress_level,
321 bool has_compress_threads,
322 int64_t compress_threads,
323 bool has_decompress_threads,
324 int64_t decompress_threads, Error **errp)
325{
326 MigrationState *s = migrate_get_current();
327
328 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
329 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
330 "is invalid, it should be in the range of 0 to 9");
331 return;
332 }
333 if (has_compress_threads &&
334 (compress_threads < 1 || compress_threads > 255)) {
335 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
336 "compress_threads",
337 "is invalid, it should be in the range of 1 to 255");
338 return;
339 }
340 if (has_decompress_threads &&
341 (decompress_threads < 1 || decompress_threads > 255)) {
342 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
343 "decompress_threads",
344 "is invalid, it should be in the range of 1 to 255");
345 return;
346 }
347
348 if (has_compress_level) {
349 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
350 }
351 if (has_compress_threads) {
352 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
353 }
354 if (has_decompress_threads) {
355 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
356 decompress_threads;
357 }
358}
359
aliguori065e2812008-11-11 16:46:33 +0000360/* shared migration helpers */
361
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000362static void migrate_set_state(MigrationState *s, int old_state, int new_state)
363{
364 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
365 trace_migrate_set_state(new_state);
366 }
367}
368
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100369static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000370{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100371 MigrationState *s = opaque;
372
373 qemu_bh_delete(s->cleanup_bh);
374 s->cleanup_bh = NULL;
375
aliguori065e2812008-11-11 16:46:33 +0000376 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100377 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100378 qemu_mutex_unlock_iothread();
379 qemu_thread_join(&s->thread);
380 qemu_mutex_lock_iothread();
381
Liang Li8706d2d2015-03-23 16:32:17 +0800382 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100383 qemu_fclose(s->file);
384 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000385 }
386
zhanghailiang31194732015-03-13 16:08:38 +0800387 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100388
zhanghailiang31194732015-03-13 16:08:38 +0800389 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100390 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800391 if (s->state == MIGRATION_STATUS_CANCELLING) {
392 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
393 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000394 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100395 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100396
397 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000398}
399
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200400void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000401{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100402 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100403 assert(s->file == NULL);
zhanghailiang31194732015-03-13 16:08:38 +0800404 s->state = MIGRATION_STATUS_FAILED;
405 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100406 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100407}
408
Juan Quintela0edda1c2010-05-11 16:28:39 +0200409static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000410{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000411 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000412 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100413 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000414
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000415 do {
416 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800417 if (old_state != MIGRATION_STATUS_SETUP &&
418 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000419 break;
420 }
zhanghailiang31194732015-03-13 16:08:38 +0800421 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
422 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000423
424 /*
425 * If we're unlucky the migration code might be stuck somewhere in a
426 * send/write while the network has failed and is waiting to timeout;
427 * if we've got shutdown(2) available then we can force it to quit.
428 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
429 * called in a bh, so there is no race against this cancel.
430 */
zhanghailiang31194732015-03-13 16:08:38 +0800431 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000432 qemu_file_shutdown(f);
433 }
aliguori065e2812008-11-11 16:46:33 +0000434}
435
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100436void add_migration_state_change_notifier(Notifier *notify)
437{
438 notifier_list_add(&migration_state_notifiers, notify);
439}
440
441void remove_migration_state_change_notifier(Notifier *notify)
442{
Paolo Bonzini31552522012-01-13 17:34:01 +0100443 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100444}
445
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200446bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200447{
zhanghailiang31194732015-03-13 16:08:38 +0800448 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200449}
450
Juan Quintela70736932011-02-23 00:43:59 +0100451bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100452{
zhanghailiang31194732015-03-13 16:08:38 +0800453 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100454}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200455
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200456bool migration_has_failed(MigrationState *s)
457{
zhanghailiang31194732015-03-13 16:08:38 +0800458 return (s->state == MIGRATION_STATUS_CANCELLED ||
459 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200460}
461
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300462static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200463{
Juan Quintela17549e82011-10-05 13:50:43 +0200464 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100465 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300466 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300467 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800468 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
469 int compress_thread_count =
470 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
471 int decompress_thread_count =
472 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300473
474 memcpy(enabled_capabilities, s->enabled_capabilities,
475 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200476
Juan Quintela17549e82011-10-05 13:50:43 +0200477 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300478 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300479 memcpy(s->enabled_capabilities, enabled_capabilities,
480 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300481 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100482
Liang Li43c60a82015-03-23 16:32:27 +0800483 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
484 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
485 compress_thread_count;
486 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
487 decompress_thread_count;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200488 s->bandwidth_limit = bandwidth_limit;
zhanghailiang31194732015-03-13 16:08:38 +0800489 s->state = MIGRATION_STATUS_SETUP;
490 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200491
Alex Blighbc72ad62013-08-21 16:03:08 +0100492 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200493 return s;
494}
Juan Quintelacab30142011-02-22 23:54:21 +0100495
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600496static GSList *migration_blockers;
497
498void migrate_add_blocker(Error *reason)
499{
500 migration_blockers = g_slist_prepend(migration_blockers, reason);
501}
502
503void migrate_del_blocker(Error *reason)
504{
505 migration_blockers = g_slist_remove(migration_blockers, reason);
506}
507
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000508void qmp_migrate_incoming(const char *uri, Error **errp)
509{
510 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000511 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000512
513 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000514 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000515 return;
516 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000517 if (!once) {
518 error_setg(errp, "The incoming migration has already been started");
519 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000520
521 qemu_start_incoming_migration(uri, &local_err);
522
523 if (local_err) {
524 error_propagate(errp, local_err);
525 return;
526 }
527
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000528 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000529}
530
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200531void qmp_migrate(const char *uri, bool has_blk, bool blk,
532 bool has_inc, bool inc, bool has_detach, bool detach,
533 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100534{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200535 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200536 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300537 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100538 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100539
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900540 params.blk = has_blk && blk;
541 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300542
zhanghailiang31194732015-03-13 16:08:38 +0800543 if (s->state == MIGRATION_STATUS_ACTIVE ||
544 s->state == MIGRATION_STATUS_SETUP ||
545 s->state == MIGRATION_STATUS_CANCELLING) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200546 error_set(errp, QERR_MIGRATION_ACTIVE);
547 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100548 }
549
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100550 if (runstate_check(RUN_STATE_INMIGRATE)) {
551 error_setg(errp, "Guest is waiting for an incoming migration");
552 return;
553 }
554
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200555 if (qemu_savevm_state_blocked(errp)) {
556 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100557 }
558
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600559 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200560 *errp = error_copy(migration_blockers->data);
561 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600562 }
563
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300564 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100565
566 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200567 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400568#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800569 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400570 rdma_start_outgoing_migration(s, p, &local_err);
571#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100572#if !defined(WIN32)
573 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200574 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100575 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200576 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100577 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200578 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100579#endif
580 } else {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200581 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
zhanghailiang31194732015-03-13 16:08:38 +0800582 s->state = MIGRATION_STATUS_FAILED;
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200583 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100584 }
585
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200586 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200587 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200588 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200589 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100590 }
Juan Quintelacab30142011-02-22 23:54:21 +0100591}
592
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200593void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100594{
Juan Quintela17549e82011-10-05 13:50:43 +0200595 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100596}
597
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300598void qmp_migrate_set_cache_size(int64_t value, Error **errp)
599{
600 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200601 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300602
603 /* Check for truncation */
604 if (value != (size_t)value) {
605 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
606 "exceeding address space");
607 return;
608 }
609
Orit Wassermana5615b12014-01-30 20:08:36 +0200610 /* Cache should not be larger than guest ram size */
611 if (value > ram_bytes_total()) {
612 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
613 "exceeds guest ram size ");
614 return;
615 }
616
Orit Wassermanc91e6812014-01-30 20:08:34 +0200617 new_size = xbzrle_cache_resize(value);
618 if (new_size < 0) {
619 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
620 "is smaller than page size");
621 return;
622 }
623
624 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300625}
626
627int64_t qmp_query_migrate_cache_size(Error **errp)
628{
629 return migrate_xbzrle_cache_size();
630}
631
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200632void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100633{
Juan Quintelacab30142011-02-22 23:54:21 +0100634 MigrationState *s;
635
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200636 if (value < 0) {
637 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100638 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100639 if (value > SIZE_MAX) {
640 value = SIZE_MAX;
641 }
Juan Quintelacab30142011-02-22 23:54:21 +0100642
Juan Quintela17549e82011-10-05 13:50:43 +0200643 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200644 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100645 if (s->file) {
646 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
647 }
Juan Quintelacab30142011-02-22 23:54:21 +0100648}
649
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200650void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100651{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200652 value *= 1e9;
653 value = MAX(0, MIN(UINT64_MAX, value));
654 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000655}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300656
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600657bool migrate_auto_converge(void)
658{
659 MigrationState *s;
660
661 s = migrate_get_current();
662
663 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
664}
665
Peter Lieven323004a2013-07-18 09:48:50 +0200666bool migrate_zero_blocks(void)
667{
668 MigrationState *s;
669
670 s = migrate_get_current();
671
672 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
673}
674
Liang Li8706d2d2015-03-23 16:32:17 +0800675bool migrate_use_compression(void)
676{
Liang Lidde4e692015-03-23 16:32:26 +0800677 MigrationState *s;
678
679 s = migrate_get_current();
680
681 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800682}
683
684int migrate_compress_level(void)
685{
686 MigrationState *s;
687
688 s = migrate_get_current();
689
Liang Li43c60a82015-03-23 16:32:27 +0800690 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800691}
692
693int migrate_compress_threads(void)
694{
695 MigrationState *s;
696
697 s = migrate_get_current();
698
Liang Li43c60a82015-03-23 16:32:27 +0800699 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800700}
701
Liang Li3fcb38c2015-03-23 16:32:18 +0800702int migrate_decompress_threads(void)
703{
704 MigrationState *s;
705
706 s = migrate_get_current();
707
Liang Li43c60a82015-03-23 16:32:27 +0800708 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800709}
710
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300711int migrate_use_xbzrle(void)
712{
713 MigrationState *s;
714
715 s = migrate_get_current();
716
717 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
718}
719
720int64_t migrate_xbzrle_cache_size(void)
721{
722 MigrationState *s;
723
724 s = migrate_get_current();
725
726 return s->xbzrle_cache_size;
727}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200728
729/* migration thread support */
730
Juan Quintela5f496a12013-02-22 17:36:30 +0100731static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200732{
Juan Quintela9848a402012-12-19 09:55:50 +0100733 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100734 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
735 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100736 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200737 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100738 int64_t start_time = initial_time;
739 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +0200740
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +0100741 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100742 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200743
Alex Blighbc72ad62013-08-21 16:03:08 +0100744 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +0800745 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400746
zhanghailiang31194732015-03-13 16:08:38 +0800747 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +0100748 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +0200749 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200750
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100751 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +0200752 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100753 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +0200754 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100755 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +0200756 } else {
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200757 int ret;
758
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100759 qemu_mutex_lock_iothread();
Alex Blighbc72ad62013-08-21 16:03:08 +0100760 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintelac369f402012-10-03 20:33:34 +0200761 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100762 old_vm_running = runstate_is_running();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200763
764 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
765 if (ret >= 0) {
Matthew Garrett40596832013-11-25 14:42:43 -0500766 qemu_file_set_rate_limit(s->file, INT64_MAX);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200767 qemu_savevm_state_complete(s->file);
768 }
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100769 qemu_mutex_unlock_iothread();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200770
771 if (ret < 0) {
zhanghailiang31194732015-03-13 16:08:38 +0800772 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
773 MIGRATION_STATUS_FAILED);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200774 break;
775 }
776
Paolo Bonzini059f8962013-02-22 17:36:32 +0100777 if (!qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800778 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
779 MIGRATION_STATUS_COMPLETED);
Paolo Bonzini059f8962013-02-22 17:36:32 +0100780 break;
781 }
Juan Quintelac369f402012-10-03 20:33:34 +0200782 }
783 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100784
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100785 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800786 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
787 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100788 break;
789 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100790 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200791 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100792 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -0500793 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200794 double bandwidth = transferred_bytes / time_spent;
795 max_size = bandwidth * migrate_max_downtime() / 1000000;
796
Michael R. Hines7e114f82013-06-25 21:35:30 -0400797 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
798 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
799
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100800 trace_migrate_transferred(transferred_bytes, time_spent,
801 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100802 /* if we haven't sent anything, we don't want to recalculate
803 10000 is a small enough number for our purposes */
804 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
805 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
806 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200807
Paolo Bonzini1964a392013-02-22 17:36:45 +0100808 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200809 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100810 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200811 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100812 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200813 /* usleep expects microseconds */
814 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
815 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100816 }
817
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100818 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +0800819 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100820 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +0200821 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100822 s->total_time = end_time - s->total_time;
823 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +0200824 if (s->total_time) {
825 s->mbps = (((double) transferred_bytes * 8.0) /
826 ((double) s->total_time)) / 1000;
827 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100828 runstate_set(RUN_STATE_POSTMIGRATE);
829 } else {
830 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100831 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100832 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200833 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100834 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100835 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100836
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200837 return NULL;
838}
839
Juan Quintela9848a402012-12-19 09:55:50 +0100840void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200841{
Juan Quintelacc283e32013-02-01 11:12:26 +0100842 /* This is a best 1st approximation. ns to ms */
843 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100844 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200845
Paolo Bonzini442773c2013-02-22 17:36:44 +0100846 qemu_file_set_rate_limit(s->file,
847 s->bandwidth_limit / XFER_LIMIT_RATIO);
848
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +0200849 /* Notify before starting migration thread */
850 notifier_list_notify(&migration_state_notifiers, s);
851
Liang Li8706d2d2015-03-23 16:32:17 +0800852 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +0000853 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100854 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200855}