]> git.proxmox.com Git - mirror_qemu.git/blame - migration/migration.c
migration_is_setup_or_active
[mirror_qemu.git] / migration / migration.c
CommitLineData
5bb7910a
AL
1/*
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 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
5bb7910a
AL
14 */
15
16#include "qemu-common.h"
d49b6836 17#include "qemu/error-report.h"
6a1751b7 18#include "qemu/main-loop.h"
caf71f86 19#include "migration/migration.h"
0d82d0e8 20#include "migration/qemu-file.h"
9c17d615 21#include "sysemu/sysemu.h"
737e150e 22#include "block/block.h"
cc7a8ea7 23#include "qapi/qmp/qerror.h"
1de7afc9 24#include "qemu/sockets.h"
ab28bd23 25#include "qemu/rcu.h"
caf71f86 26#include "migration/block.h"
766bd176 27#include "qemu/thread.h"
791e7c82 28#include "qmp-commands.h"
c09e5bb1 29#include "trace.h"
df4b1024 30#include "qapi/util.h"
598cd2bd 31#include "qapi-event.h"
070afca2 32#include "qom/cpu.h"
065e2813 33
dc325627 34#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
5bb7910a 35
5b4e1eb7
JQ
36/* Amount of time to allocate to each "chunk" of bandwidth-throttled
37 * data. */
38#define BUFFER_DELAY 100
39#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
40
8706d2d5
LL
41/* Default compression thread count */
42#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
3fcb38c2
LL
43/* Default decompression thread count, usually decompression is at
44 * least 4 times as fast as compression.*/
45#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
8706d2d5
LL
46/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
47#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
1626fee3
JH
48/* Define default autoconverge cpu throttle migration parameters */
49#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20
50#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10
8706d2d5 51
17ad9b35
OW
52/* Migration XBZRLE default cache size */
53#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
54
99a0db9b
GH
55static NotifierList migration_state_notifiers =
56 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
57
adde220a
DDAG
58static bool deferred_incoming;
59
17549e84
JQ
60/* When we add fault tolerance, we could have several
61 migrations at once. For now we don't need to add
62 dynamic creation of migration */
63
bca7856a 64/* For outgoing */
859bc756 65MigrationState *migrate_get_current(void)
17549e84
JQ
66{
67 static MigrationState current_migration = {
31194731 68 .state = MIGRATION_STATUS_NONE,
d0ae46c1 69 .bandwidth_limit = MAX_THROTTLE,
17ad9b35 70 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
7e114f8c 71 .mbps = -1,
43c60a81
LL
72 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
73 DEFAULT_MIGRATE_COMPRESS_LEVEL,
74 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
75 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
76 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
77 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
1626fee3
JH
78 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
79 DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL,
80 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
81 DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT,
17549e84
JQ
82 };
83
84 return &current_migration;
85}
86
bca7856a
DDAG
87/* For incoming */
88static MigrationIncomingState *mis_current;
89
90MigrationIncomingState *migration_incoming_get_current(void)
91{
92 return mis_current;
93}
94
95MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
96{
97f3ad35 97 mis_current = g_new0(MigrationIncomingState, 1);
42e2aa56 98 mis_current->from_src_file = f;
1a8f46f8 99 QLIST_INIT(&mis_current->loadvm_handlers);
6decec93 100 qemu_mutex_init(&mis_current->rp_mutex);
bca7856a
DDAG
101
102 return mis_current;
103}
104
105void migration_incoming_state_destroy(void)
106{
1a8f46f8 107 loadvm_free_handlers(mis_current);
bca7856a
DDAG
108 g_free(mis_current);
109 mis_current = NULL;
110}
111
df4b1024
JQ
112
113typedef struct {
13d16814 114 bool optional;
df4b1024
JQ
115 uint32_t size;
116 uint8_t runstate[100];
172c4356
JQ
117 RunState state;
118 bool received;
df4b1024
JQ
119} GlobalState;
120
121static GlobalState global_state;
122
560d027b 123int global_state_store(void)
df4b1024
JQ
124{
125 if (!runstate_store((char *)global_state.runstate,
126 sizeof(global_state.runstate))) {
127 error_report("runstate name too big: %s", global_state.runstate);
128 trace_migrate_state_too_big();
129 return -EINVAL;
130 }
131 return 0;
132}
133
c69adea4
AP
134void global_state_store_running(void)
135{
136 const char *state = RunState_lookup[RUN_STATE_RUNNING];
137 strncpy((char *)global_state.runstate,
138 state, sizeof(global_state.runstate));
139}
140
172c4356 141static bool global_state_received(void)
df4b1024 142{
172c4356
JQ
143 return global_state.received;
144}
145
146static RunState global_state_get_runstate(void)
147{
148 return global_state.state;
df4b1024
JQ
149}
150
13d16814
JQ
151void global_state_set_optional(void)
152{
153 global_state.optional = true;
154}
155
156static bool global_state_needed(void *opaque)
157{
158 GlobalState *s = opaque;
159 char *runstate = (char *)s->runstate;
160
161 /* If it is not optional, it is mandatory */
162
163 if (s->optional == false) {
164 return true;
165 }
166
167 /* If state is running or paused, it is not needed */
168
169 if (strcmp(runstate, "running") == 0 ||
170 strcmp(runstate, "paused") == 0) {
171 return false;
172 }
173
174 /* for any other state it is needed */
175 return true;
176}
177
df4b1024
JQ
178static int global_state_post_load(void *opaque, int version_id)
179{
180 GlobalState *s = opaque;
172c4356
JQ
181 Error *local_err = NULL;
182 int r;
df4b1024
JQ
183 char *runstate = (char *)s->runstate;
184
172c4356 185 s->received = true;
df4b1024
JQ
186 trace_migrate_global_state_post_load(runstate);
187
172c4356 188 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
df4b1024
JQ
189 -1, &local_err);
190
172c4356
JQ
191 if (r == -1) {
192 if (local_err) {
193 error_report_err(local_err);
df4b1024 194 }
172c4356 195 return -EINVAL;
df4b1024 196 }
172c4356 197 s->state = r;
df4b1024 198
172c4356 199 return 0;
df4b1024
JQ
200}
201
202static void global_state_pre_save(void *opaque)
203{
204 GlobalState *s = opaque;
205
206 trace_migrate_global_state_pre_save((char *)s->runstate);
207 s->size = strlen((char *)s->runstate) + 1;
208}
209
210static const VMStateDescription vmstate_globalstate = {
211 .name = "globalstate",
212 .version_id = 1,
213 .minimum_version_id = 1,
214 .post_load = global_state_post_load,
215 .pre_save = global_state_pre_save,
13d16814 216 .needed = global_state_needed,
df4b1024
JQ
217 .fields = (VMStateField[]) {
218 VMSTATE_UINT32(size, GlobalState),
219 VMSTATE_BUFFER(runstate, GlobalState),
220 VMSTATE_END_OF_LIST()
221 },
222};
223
224void register_global_state(void)
225{
226 /* We would use it independently that we receive it */
227 strcpy((char *)&global_state.runstate, "");
172c4356 228 global_state.received = false;
df4b1024
JQ
229 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
230}
231
b05dc723
JQ
232static void migrate_generate_event(int new_state)
233{
234 if (migrate_use_events()) {
235 qapi_event_send_migration(new_state, &error_abort);
b05dc723
JQ
236 }
237}
238
adde220a
DDAG
239/*
240 * Called on -incoming with a defer: uri.
241 * The migration can be started later after any parameters have been
242 * changed.
243 */
244static void deferred_incoming_migration(Error **errp)
245{
246 if (deferred_incoming) {
247 error_setg(errp, "Incoming migration already deferred");
248 }
249 deferred_incoming = true;
250}
251
43eaae28 252void qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 253{
34c9dd8e
AL
254 const char *p;
255
7cf1fe6d 256 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
adde220a
DDAG
257 if (!strcmp(uri, "defer")) {
258 deferred_incoming_migration(errp);
259 } else if (strstart(uri, "tcp:", &p)) {
43eaae28 260 tcp_start_incoming_migration(p, errp);
2da776db 261#ifdef CONFIG_RDMA
adde220a 262 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
263 rdma_start_incoming_migration(p, errp);
264#endif
065e2813 265#if !defined(WIN32)
adde220a 266 } else if (strstart(uri, "exec:", &p)) {
43eaae28 267 exec_start_incoming_migration(p, errp);
adde220a 268 } else if (strstart(uri, "unix:", &p)) {
43eaae28 269 unix_start_incoming_migration(p, errp);
adde220a 270 } else if (strstart(uri, "fd:", &p)) {
43eaae28 271 fd_start_incoming_migration(p, errp);
065e2813 272#endif
adde220a 273 } else {
312fd5f2 274 error_setg(errp, "unknown migration protocol: %s", uri);
8ca5e801 275 }
5bb7910a
AL
276}
277
82a4da79 278static void process_incoming_migration_co(void *opaque)
511c0231 279{
82a4da79 280 QEMUFile *f = opaque;
5a8a30db 281 Error *local_err = NULL;
1c12e1f5
PB
282 int ret;
283
bca7856a 284 migration_incoming_state_new(f);
7cf1fe6d 285 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
1c12e1f5 286 ret = qemu_loadvm_state(f);
bca7856a 287
1c12e1f5 288 qemu_fclose(f);
905f26f2 289 free_xbzrle_decoded_buf();
bca7856a
DDAG
290 migration_incoming_state_destroy();
291
1c12e1f5 292 if (ret < 0) {
7cf1fe6d 293 migrate_generate_event(MIGRATION_STATUS_FAILED);
db80face 294 error_report("load of migration failed: %s", strerror(-ret));
3fcb38c2 295 migrate_decompress_threads_join();
4aead692 296 exit(EXIT_FAILURE);
511c0231 297 }
511c0231 298
0f15423c 299 /* Make sure all file formats flush their mutable metadata */
5a8a30db
KW
300 bdrv_invalidate_cache_all(&local_err);
301 if (local_err) {
ed1f3e00 302 migrate_generate_event(MIGRATION_STATUS_FAILED);
97baf9d9 303 error_report_err(local_err);
3fcb38c2 304 migrate_decompress_threads_join();
5a8a30db
KW
305 exit(EXIT_FAILURE);
306 }
0f15423c 307
92e37622
AS
308 /*
309 * This must happen after all error conditions are dealt with and
310 * we're sure the VM is going to be running on this host.
311 */
312 qemu_announce_self();
313
172c4356
JQ
314 /* If global state section was not received or we are in running
315 state, we need to obey autostart. Any other state is set with
316 runstate_set. */
df4b1024 317
172c4356
JQ
318 if (!global_state_received() ||
319 global_state_get_runstate() == RUN_STATE_RUNNING) {
df4b1024
JQ
320 if (autostart) {
321 vm_start();
322 } else {
323 runstate_set(RUN_STATE_PAUSED);
324 }
172c4356
JQ
325 } else {
326 runstate_set(global_state_get_runstate());
f5bbfba1 327 }
3fcb38c2 328 migrate_decompress_threads_join();
ed1f3e00
DDAG
329 /*
330 * This must happen after any state changes since as soon as an external
331 * observer sees this event they might start to prod at the VM assuming
332 * it's ready to use.
333 */
334 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
511c0231
JQ
335}
336
82a4da79
PB
337void process_incoming_migration(QEMUFile *f)
338{
339 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
340 int fd = qemu_get_fd(f);
341
342 assert(fd != -1);
3fcb38c2 343 migrate_decompress_threads_create();
f9e8cacc 344 qemu_set_nonblock(fd);
82a4da79
PB
345 qemu_coroutine_enter(co, f);
346}
347
6decec93
DDAG
348/*
349 * Send a message on the return channel back to the source
350 * of the migration.
351 */
352void migrate_send_rp_message(MigrationIncomingState *mis,
353 enum mig_rp_message_type message_type,
354 uint16_t len, void *data)
355{
356 trace_migrate_send_rp_message((int)message_type, len);
357 qemu_mutex_lock(&mis->rp_mutex);
358 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
359 qemu_put_be16(mis->to_src_file, len);
360 qemu_put_buffer(mis->to_src_file, data, len);
361 qemu_fflush(mis->to_src_file);
362 qemu_mutex_unlock(&mis->rp_mutex);
363}
364
365/*
366 * Send a 'SHUT' message on the return channel with the given value
367 * to indicate that we've finished with the RP. Non-0 value indicates
368 * error.
369 */
370void migrate_send_rp_shut(MigrationIncomingState *mis,
371 uint32_t value)
372{
373 uint32_t buf;
374
375 buf = cpu_to_be32(value);
376 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
377}
378
379/*
380 * Send a 'PONG' message on the return channel with the given value
381 * (normally in response to a 'PING')
382 */
383void migrate_send_rp_pong(MigrationIncomingState *mis,
384 uint32_t value)
385{
386 uint32_t buf;
387
388 buf = cpu_to_be32(value);
389 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
390}
391
a0a3fd60
GC
392/* amount of nanoseconds we are willing to wait for migration to be down.
393 * the choice of nanoseconds is because it is the maximum resolution that
394 * get_clock() can achieve. It is an internal measure. All user-visible
395 * units must be in seconds */
f7cd55a0 396static uint64_t max_downtime = 300000000;
a0a3fd60
GC
397
398uint64_t migrate_max_downtime(void)
399{
400 return max_downtime;
401}
402
bbf6da32
OW
403MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
404{
405 MigrationCapabilityStatusList *head = NULL;
406 MigrationCapabilityStatusList *caps;
407 MigrationState *s = migrate_get_current();
408 int i;
409
387eedeb 410 caps = NULL; /* silence compiler warning */
bbf6da32
OW
411 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
412 if (head == NULL) {
413 head = g_malloc0(sizeof(*caps));
414 caps = head;
415 } else {
416 caps->next = g_malloc0(sizeof(*caps));
417 caps = caps->next;
418 }
419 caps->value =
420 g_malloc(sizeof(*caps->value));
421 caps->value->capability = i;
422 caps->value->state = s->enabled_capabilities[i];
423 }
424
425 return head;
426}
427
85de8323
LL
428MigrationParameters *qmp_query_migrate_parameters(Error **errp)
429{
430 MigrationParameters *params;
431 MigrationState *s = migrate_get_current();
432
433 params = g_malloc0(sizeof(*params));
434 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
435 params->compress_threads =
436 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
437 params->decompress_threads =
438 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
1626fee3
JH
439 params->x_cpu_throttle_initial =
440 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
441 params->x_cpu_throttle_increment =
442 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
85de8323
LL
443
444 return params;
445}
446
f6844b99
DDAG
447/*
448 * Return true if we're already in the middle of a migration
449 * (i.e. any of the active or setup states)
450 */
451static bool migration_is_setup_or_active(int state)
452{
453 switch (state) {
454 case MIGRATION_STATUS_ACTIVE:
455 case MIGRATION_STATUS_SETUP:
456 return true;
457
458 default:
459 return false;
460
461 }
462}
463
f36d55af
OW
464static void get_xbzrle_cache_stats(MigrationInfo *info)
465{
466 if (migrate_use_xbzrle()) {
467 info->has_xbzrle_cache = true;
468 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
469 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
470 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
471 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
472 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
8bc39233 473 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
f36d55af
OW
474 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
475 }
476}
477
791e7c82 478MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 479{
791e7c82 480 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
481 MigrationState *s = migrate_get_current();
482
483 switch (s->state) {
31194731 484 case MIGRATION_STATUS_NONE:
17549e84
JQ
485 /* no migration has happened ever */
486 break;
31194731 487 case MIGRATION_STATUS_SETUP:
29ae8a41 488 info->has_status = true;
ed4fbd10 489 info->has_total_time = false;
29ae8a41 490 break;
31194731
HZ
491 case MIGRATION_STATUS_ACTIVE:
492 case MIGRATION_STATUS_CANCELLING:
791e7c82 493 info->has_status = true;
7aa939af 494 info->has_total_time = true;
bc72ad67 495 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
7aa939af 496 - s->total_time;
2c52ddf1
JQ
497 info->has_expected_downtime = true;
498 info->expected_downtime = s->expected_downtime;
ed4fbd10
MH
499 info->has_setup_time = true;
500 info->setup_time = s->setup_time;
17549e84 501
791e7c82
LC
502 info->has_ram = true;
503 info->ram = g_malloc0(sizeof(*info->ram));
504 info->ram->transferred = ram_bytes_transferred();
505 info->ram->remaining = ram_bytes_remaining();
506 info->ram->total = ram_bytes_total();
004d4c10 507 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 508 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
509 info->ram->normal = norm_mig_pages_transferred();
510 info->ram->normal_bytes = norm_mig_bytes_transferred();
8d017193 511 info->ram->dirty_pages_rate = s->dirty_pages_rate;
7e114f8c 512 info->ram->mbps = s->mbps;
58570ed8 513 info->ram->dirty_sync_count = s->dirty_sync_count;
8d017193 514
17549e84 515 if (blk_mig_active()) {
791e7c82
LC
516 info->has_disk = true;
517 info->disk = g_malloc0(sizeof(*info->disk));
518 info->disk->transferred = blk_mig_bytes_transferred();
519 info->disk->remaining = blk_mig_bytes_remaining();
520 info->disk->total = blk_mig_bytes_total();
ff8d81d8 521 }
f36d55af 522
4782893e
JH
523 if (cpu_throttle_active()) {
524 info->has_x_cpu_throttle_percentage = true;
525 info->x_cpu_throttle_percentage = cpu_throttle_get_percentage();
526 }
527
f36d55af 528 get_xbzrle_cache_stats(info);
17549e84 529 break;
31194731 530 case MIGRATION_STATUS_COMPLETED:
f36d55af
OW
531 get_xbzrle_cache_stats(info);
532
791e7c82 533 info->has_status = true;
00c14997 534 info->has_total_time = true;
7aa939af 535 info->total_time = s->total_time;
9c5a9fcf
JQ
536 info->has_downtime = true;
537 info->downtime = s->downtime;
ed4fbd10
MH
538 info->has_setup_time = true;
539 info->setup_time = s->setup_time;
d5f8a570
JQ
540
541 info->has_ram = true;
542 info->ram = g_malloc0(sizeof(*info->ram));
543 info->ram->transferred = ram_bytes_transferred();
544 info->ram->remaining = 0;
545 info->ram->total = ram_bytes_total();
004d4c10 546 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 547 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
548 info->ram->normal = norm_mig_pages_transferred();
549 info->ram->normal_bytes = norm_mig_bytes_transferred();
7e114f8c 550 info->ram->mbps = s->mbps;
58570ed8 551 info->ram->dirty_sync_count = s->dirty_sync_count;
17549e84 552 break;
31194731 553 case MIGRATION_STATUS_FAILED:
791e7c82 554 info->has_status = true;
17549e84 555 break;
31194731 556 case MIGRATION_STATUS_CANCELLED:
791e7c82 557 info->has_status = true;
17549e84 558 break;
5bb7910a 559 }
cde63fbe 560 info->status = s->state;
791e7c82
LC
561
562 return info;
5bb7910a
AL
563}
564
00458433
OW
565void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
566 Error **errp)
567{
568 MigrationState *s = migrate_get_current();
569 MigrationCapabilityStatusList *cap;
570
f6844b99 571 if (migration_is_setup_or_active(s->state)) {
c6bd8c70 572 error_setg(errp, QERR_MIGRATION_ACTIVE);
00458433
OW
573 return;
574 }
575
576 for (cap = params; cap; cap = cap->next) {
577 s->enabled_capabilities[cap->value->capability] = cap->value->state;
578 }
579}
580
85de8323
LL
581void qmp_migrate_set_parameters(bool has_compress_level,
582 int64_t compress_level,
583 bool has_compress_threads,
584 int64_t compress_threads,
585 bool has_decompress_threads,
1626fee3
JH
586 int64_t decompress_threads,
587 bool has_x_cpu_throttle_initial,
588 int64_t x_cpu_throttle_initial,
589 bool has_x_cpu_throttle_increment,
590 int64_t x_cpu_throttle_increment, Error **errp)
85de8323
LL
591{
592 MigrationState *s = migrate_get_current();
593
594 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
c6bd8c70
MA
595 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
596 "is invalid, it should be in the range of 0 to 9");
85de8323
LL
597 return;
598 }
599 if (has_compress_threads &&
600 (compress_threads < 1 || compress_threads > 255)) {
c6bd8c70
MA
601 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
602 "compress_threads",
603 "is invalid, it should be in the range of 1 to 255");
85de8323
LL
604 return;
605 }
606 if (has_decompress_threads &&
607 (decompress_threads < 1 || decompress_threads > 255)) {
c6bd8c70
MA
608 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
609 "decompress_threads",
610 "is invalid, it should be in the range of 1 to 255");
85de8323
LL
611 return;
612 }
1626fee3
JH
613 if (has_x_cpu_throttle_initial &&
614 (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
615 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
616 "x_cpu_throttle_initial",
617 "an integer in the range of 1 to 99");
618 }
619 if (has_x_cpu_throttle_increment &&
620 (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
621 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
622 "x_cpu_throttle_increment",
623 "an integer in the range of 1 to 99");
624 }
85de8323
LL
625
626 if (has_compress_level) {
627 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
628 }
629 if (has_compress_threads) {
630 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
631 }
632 if (has_decompress_threads) {
633 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
634 decompress_threads;
635 }
1626fee3
JH
636 if (has_x_cpu_throttle_initial) {
637 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
638 x_cpu_throttle_initial;
639 }
640
641 if (has_x_cpu_throttle_increment) {
642 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
643 x_cpu_throttle_increment;
644 }
85de8323
LL
645}
646
065e2813
AL
647/* shared migration helpers */
648
51cf4c1a
Z
649static void migrate_set_state(MigrationState *s, int old_state, int new_state)
650{
a5c17b5f 651 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
4ba4bc5e 652 trace_migrate_set_state(new_state);
b05dc723 653 migrate_generate_event(new_state);
51cf4c1a
Z
654 }
655}
656
bb1fadc4 657static void migrate_fd_cleanup(void *opaque)
065e2813 658{
bb1fadc4
PB
659 MigrationState *s = opaque;
660
661 qemu_bh_delete(s->cleanup_bh);
662 s->cleanup_bh = NULL;
663
065e2813 664 if (s->file) {
9013dca5 665 trace_migrate_fd_cleanup();
404a7c05
PB
666 qemu_mutex_unlock_iothread();
667 qemu_thread_join(&s->thread);
668 qemu_mutex_lock_iothread();
669
8706d2d5 670 migrate_compress_threads_join();
6f190a06
PB
671 qemu_fclose(s->file);
672 s->file = NULL;
065e2813
AL
673 }
674
31194731 675 assert(s->state != MIGRATION_STATUS_ACTIVE);
7a2c1721 676
94f5a437
LL
677 if (s->state == MIGRATION_STATUS_CANCELLING) {
678 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
679 MIGRATION_STATUS_CANCELLED);
7a2c1721 680 }
a3fa1d78
PB
681
682 notifier_list_notify(&migration_state_notifiers, s);
065e2813
AL
683}
684
8b6b99b3 685void migrate_fd_error(MigrationState *s)
065e2813 686{
9013dca5 687 trace_migrate_fd_error();
bb1fadc4 688 assert(s->file == NULL);
7844337d 689 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
bb1fadc4 690 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
691}
692
0edda1c4 693static void migrate_fd_cancel(MigrationState *s)
065e2813 694{
6f2b811a 695 int old_state ;
a26ba26e 696 QEMUFile *f = migrate_get_current()->file;
9013dca5 697 trace_migrate_fd_cancel();
065e2813 698
6f2b811a
Z
699 do {
700 old_state = s->state;
f6844b99 701 if (!migration_is_setup_or_active(old_state)) {
6f2b811a
Z
702 break;
703 }
31194731
HZ
704 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
705 } while (s->state != MIGRATION_STATUS_CANCELLING);
a26ba26e
DDAG
706
707 /*
708 * If we're unlucky the migration code might be stuck somewhere in a
709 * send/write while the network has failed and is waiting to timeout;
710 * if we've got shutdown(2) available then we can force it to quit.
711 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
712 * called in a bh, so there is no race against this cancel.
713 */
31194731 714 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
a26ba26e
DDAG
715 qemu_file_shutdown(f);
716 }
065e2813
AL
717}
718
99a0db9b
GH
719void add_migration_state_change_notifier(Notifier *notify)
720{
721 notifier_list_add(&migration_state_notifiers, notify);
722}
723
724void remove_migration_state_change_notifier(Notifier *notify)
725{
31552529 726 notifier_remove(notify);
99a0db9b
GH
727}
728
02edd2e7 729bool migration_in_setup(MigrationState *s)
afe2df69 730{
31194731 731 return s->state == MIGRATION_STATUS_SETUP;
afe2df69
GH
732}
733
7073693b 734bool migration_has_finished(MigrationState *s)
99a0db9b 735{
31194731 736 return s->state == MIGRATION_STATUS_COMPLETED;
99a0db9b 737}
0edda1c4 738
afe2df69
GH
739bool migration_has_failed(MigrationState *s)
740{
31194731
HZ
741 return (s->state == MIGRATION_STATUS_CANCELLED ||
742 s->state == MIGRATION_STATUS_FAILED);
afe2df69
GH
743}
744
aefeb18b 745MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 746{
17549e84 747 MigrationState *s = migrate_get_current();
d0ae46c1 748 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 749 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 750 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
43c60a81
LL
751 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
752 int compress_thread_count =
753 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
754 int decompress_thread_count =
755 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
1626fee3
JH
756 int x_cpu_throttle_initial =
757 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
758 int x_cpu_throttle_increment =
759 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
bbf6da32
OW
760
761 memcpy(enabled_capabilities, s->enabled_capabilities,
762 sizeof(enabled_capabilities));
0edda1c4 763
17549e84 764 memset(s, 0, sizeof(*s));
6607ae23 765 s->params = *params;
bbf6da32
OW
766 memcpy(s->enabled_capabilities, enabled_capabilities,
767 sizeof(enabled_capabilities));
17ad9b35 768 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 769
43c60a81
LL
770 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
771 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
772 compress_thread_count;
773 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
774 decompress_thread_count;
1626fee3
JH
775 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
776 x_cpu_throttle_initial;
777 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
778 x_cpu_throttle_increment;
0edda1c4 779 s->bandwidth_limit = bandwidth_limit;
7844337d 780 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
0edda1c4 781
bc72ad67 782 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0edda1c4
JQ
783 return s;
784}
cab30143 785
fa2756b7
AL
786static GSList *migration_blockers;
787
788void migrate_add_blocker(Error *reason)
789{
790 migration_blockers = g_slist_prepend(migration_blockers, reason);
791}
792
793void migrate_del_blocker(Error *reason)
794{
795 migration_blockers = g_slist_remove(migration_blockers, reason);
796}
797
bf1ae1f4
DDAG
798void qmp_migrate_incoming(const char *uri, Error **errp)
799{
800 Error *local_err = NULL;
4debb5f5 801 static bool once = true;
bf1ae1f4
DDAG
802
803 if (!deferred_incoming) {
4debb5f5 804 error_setg(errp, "For use with '-incoming defer'");
bf1ae1f4
DDAG
805 return;
806 }
4debb5f5
DDAG
807 if (!once) {
808 error_setg(errp, "The incoming migration has already been started");
809 }
bf1ae1f4
DDAG
810
811 qemu_start_incoming_migration(uri, &local_err);
812
813 if (local_err) {
814 error_propagate(errp, local_err);
815 return;
816 }
817
4debb5f5 818 once = false;
bf1ae1f4
DDAG
819}
820
e1c37d0e
LC
821void qmp_migrate(const char *uri, bool has_blk, bool blk,
822 bool has_inc, bool inc, bool has_detach, bool detach,
823 Error **errp)
cab30143 824{
be7059cd 825 Error *local_err = NULL;
17549e84 826 MigrationState *s = migrate_get_current();
6607ae23 827 MigrationParams params;
cab30143 828 const char *p;
cab30143 829
8c0426ae
PP
830 params.blk = has_blk && blk;
831 params.shared = has_inc && inc;
6607ae23 832
f6844b99 833 if (migration_is_setup_or_active(s->state) ||
31194731 834 s->state == MIGRATION_STATUS_CANCELLING) {
c6bd8c70 835 error_setg(errp, QERR_MIGRATION_ACTIVE);
e1c37d0e 836 return;
cab30143 837 }
ca99993a
DDAG
838 if (runstate_check(RUN_STATE_INMIGRATE)) {
839 error_setg(errp, "Guest is waiting for an incoming migration");
840 return;
841 }
842
e1c37d0e
LC
843 if (qemu_savevm_state_blocked(errp)) {
844 return;
cab30143
JQ
845 }
846
fa2756b7 847 if (migration_blockers) {
e1c37d0e
LC
848 *errp = error_copy(migration_blockers->data);
849 return;
fa2756b7
AL
850 }
851
656a2334
JQ
852 /* We are starting a new migration, so we want to start in a clean
853 state. This change is only needed if previous migration
854 failed/was cancelled. We don't use migrate_set_state() because
855 we are setting the initial state, not changing it. */
856 s->state = MIGRATION_STATUS_NONE;
857
6607ae23 858 s = migrate_init(&params);
cab30143
JQ
859
860 if (strstart(uri, "tcp:", &p)) {
f37afb5a 861 tcp_start_outgoing_migration(s, p, &local_err);
2da776db 862#ifdef CONFIG_RDMA
41310c68 863 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
864 rdma_start_outgoing_migration(s, p, &local_err);
865#endif
cab30143
JQ
866#if !defined(WIN32)
867 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 868 exec_start_outgoing_migration(s, p, &local_err);
cab30143 869 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 870 unix_start_outgoing_migration(s, p, &local_err);
cab30143 871 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 872 fd_start_outgoing_migration(s, p, &local_err);
cab30143 873#endif
99a0db9b 874 } else {
c6bd8c70
MA
875 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
876 "a valid migration protocol");
7844337d 877 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
e1c37d0e 878 return;
cab30143
JQ
879 }
880
f37afb5a 881 if (local_err) {
342ab8d1 882 migrate_fd_error(s);
f37afb5a 883 error_propagate(errp, local_err);
e1c37d0e 884 return;
1299c631 885 }
cab30143
JQ
886}
887
6cdedb07 888void qmp_migrate_cancel(Error **errp)
cab30143 889{
17549e84 890 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
891}
892
9e1ba4cc
OW
893void qmp_migrate_set_cache_size(int64_t value, Error **errp)
894{
895 MigrationState *s = migrate_get_current();
c91e681a 896 int64_t new_size;
9e1ba4cc
OW
897
898 /* Check for truncation */
899 if (value != (size_t)value) {
c6bd8c70
MA
900 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
901 "exceeding address space");
9e1ba4cc
OW
902 return;
903 }
904
a5615b14
OW
905 /* Cache should not be larger than guest ram size */
906 if (value > ram_bytes_total()) {
c6bd8c70
MA
907 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
908 "exceeds guest ram size ");
a5615b14
OW
909 return;
910 }
911
c91e681a
OW
912 new_size = xbzrle_cache_resize(value);
913 if (new_size < 0) {
c6bd8c70
MA
914 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
915 "is smaller than page size");
c91e681a
OW
916 return;
917 }
918
919 s->xbzrle_cache_size = new_size;
9e1ba4cc
OW
920}
921
922int64_t qmp_query_migrate_cache_size(Error **errp)
923{
924 return migrate_xbzrle_cache_size();
925}
926
3dc85383 927void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 928{
cab30143
JQ
929 MigrationState *s;
930
3dc85383
LC
931 if (value < 0) {
932 value = 0;
99a0db9b 933 }
442773ce
PB
934 if (value > SIZE_MAX) {
935 value = SIZE_MAX;
936 }
cab30143 937
17549e84 938 s = migrate_get_current();
3dc85383 939 s->bandwidth_limit = value;
442773ce
PB
940 if (s->file) {
941 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
942 }
cab30143
JQ
943}
944
4f0a993b 945void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 946{
4f0a993b
LC
947 value *= 1e9;
948 value = MAX(0, MIN(UINT64_MAX, value));
949 max_downtime = (uint64_t)value;
99a0db9b 950}
17ad9b35 951
bde1e2ec
CV
952bool migrate_auto_converge(void)
953{
954 MigrationState *s;
955
956 s = migrate_get_current();
957
958 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
959}
960
323004a3
PL
961bool migrate_zero_blocks(void)
962{
963 MigrationState *s;
964
965 s = migrate_get_current();
966
967 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
968}
969
8706d2d5
LL
970bool migrate_use_compression(void)
971{
dde4e694
LL
972 MigrationState *s;
973
974 s = migrate_get_current();
975
976 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
8706d2d5
LL
977}
978
979int migrate_compress_level(void)
980{
981 MigrationState *s;
982
983 s = migrate_get_current();
984
43c60a81 985 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
8706d2d5
LL
986}
987
988int migrate_compress_threads(void)
989{
990 MigrationState *s;
991
992 s = migrate_get_current();
993
43c60a81 994 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
8706d2d5
LL
995}
996
3fcb38c2
LL
997int migrate_decompress_threads(void)
998{
999 MigrationState *s;
1000
1001 s = migrate_get_current();
1002
43c60a81 1003 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
3fcb38c2
LL
1004}
1005
b05dc723
JQ
1006bool migrate_use_events(void)
1007{
1008 MigrationState *s;
1009
1010 s = migrate_get_current();
1011
1012 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1013}
1014
17ad9b35
OW
1015int migrate_use_xbzrle(void)
1016{
1017 MigrationState *s;
1018
1019 s = migrate_get_current();
1020
1021 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1022}
1023
1024int64_t migrate_xbzrle_cache_size(void)
1025{
1026 MigrationState *s;
1027
1028 s = migrate_get_current();
1029
1030 return s->xbzrle_cache_size;
1031}
0d82d0e8 1032
09f6c85e
DDAG
1033/**
1034 * migration_completion: Used by migration_thread when there's not much left.
1035 * The caller 'breaks' the loop when this returns.
1036 *
1037 * @s: Current migration state
1038 * @*old_vm_running: Pointer to old_vm_running flag
1039 * @*start_time: Pointer to time to update
1040 */
1041static void migration_completion(MigrationState *s, bool *old_vm_running,
1042 int64_t *start_time)
1043{
1044 int ret;
1045
1046 qemu_mutex_lock_iothread();
1047 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1048 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1049 *old_vm_running = runstate_is_running();
1050
1051 ret = global_state_store();
1052 if (!ret) {
1053 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1054 if (ret >= 0) {
1055 qemu_file_set_rate_limit(s->file, INT64_MAX);
a3e06c3d 1056 qemu_savevm_state_complete_precopy(s->file);
09f6c85e
DDAG
1057 }
1058 }
1059 qemu_mutex_unlock_iothread();
1060
1061 if (ret < 0) {
1062 goto fail;
1063 }
1064
1065 if (qemu_file_get_error(s->file)) {
1066 trace_migration_completion_file_err();
1067 goto fail;
1068 }
1069
1070 migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_COMPLETED);
1071 return;
1072
1073fail:
1074 migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_FAILED);
1075}
1076
0d82d0e8
JQ
1077/* migration thread support */
1078
5f496a1b 1079static void *migration_thread(void *opaque)
0d82d0e8 1080{
9848a404 1081 MigrationState *s = opaque;
bc72ad67
AB
1082 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1083 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
be7172e2 1084 int64_t initial_bytes = 0;
0d82d0e8 1085 int64_t max_size = 0;
a3fa1d78 1086 int64_t start_time = initial_time;
94f5a437 1087 int64_t end_time;
a3fa1d78 1088 bool old_vm_running = false;
76f5933a 1089
ab28bd23
PB
1090 rcu_register_thread();
1091
f796baa1 1092 qemu_savevm_state_header(s->file);
dba433c0 1093 qemu_savevm_state_begin(s->file, &s->params);
0d82d0e8 1094
bc72ad67 1095 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
31194731 1096 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
29ae8a41 1097
31194731 1098 while (s->state == MIGRATION_STATUS_ACTIVE) {
a3e879cd 1099 int64_t current_time;
c369f40d 1100 uint64_t pending_size;
0d82d0e8 1101
a0ff044b 1102 if (!qemu_file_rate_limit(s->file)) {
c369f40d 1103 pending_size = qemu_savevm_state_pending(s->file, max_size);
9013dca5 1104 trace_migrate_pending(pending_size, max_size);
b22ff1fb 1105 if (pending_size && pending_size >= max_size) {
dba433c0 1106 qemu_savevm_state_iterate(s->file);
c369f40d 1107 } else {
09f6c85e
DDAG
1108 trace_migration_thread_low_pending(pending_size);
1109 migration_completion(s, &old_vm_running, &start_time);
1110 break;
c369f40d
JQ
1111 }
1112 }
f4410a5d 1113
fd45ee2c 1114 if (qemu_file_get_error(s->file)) {
31194731
HZ
1115 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
1116 MIGRATION_STATUS_FAILED);
fd45ee2c
PB
1117 break;
1118 }
bc72ad67 1119 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0d82d0e8 1120 if (current_time >= initial_time + BUFFER_DELAY) {
be7172e2 1121 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
77417f10 1122 uint64_t time_spent = current_time - initial_time;
0d82d0e8
JQ
1123 double bandwidth = transferred_bytes / time_spent;
1124 max_size = bandwidth * migrate_max_downtime() / 1000000;
1125
7e114f8c
MH
1126 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1127 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1128
9013dca5
AK
1129 trace_migrate_transferred(transferred_bytes, time_spent,
1130 bandwidth, max_size);
90f8ae72
JQ
1131 /* if we haven't sent anything, we don't want to recalculate
1132 10000 is a small enough number for our purposes */
1133 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1134 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1135 }
0d82d0e8 1136
1964a397 1137 qemu_file_reset_rate_limit(s->file);
0d82d0e8 1138 initial_time = current_time;
be7172e2 1139 initial_bytes = qemu_ftell(s->file);
0d82d0e8 1140 }
a0ff044b 1141 if (qemu_file_rate_limit(s->file)) {
0d82d0e8
JQ
1142 /* usleep expects microseconds */
1143 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1144 }
a3fa1d78
PB
1145 }
1146
070afca2
JH
1147 /* If we enabled cpu throttling for auto-converge, turn it off. */
1148 cpu_throttle_stop();
94f5a437 1149 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
070afca2 1150
f4410a5d 1151 qemu_mutex_lock_iothread();
ea7415fa 1152 qemu_savevm_state_cleanup();
31194731 1153 if (s->state == MIGRATION_STATUS_COMPLETED) {
d6ed7312 1154 uint64_t transferred_bytes = qemu_ftell(s->file);
a3fa1d78
PB
1155 s->total_time = end_time - s->total_time;
1156 s->downtime = end_time - start_time;
d6ed7312
PL
1157 if (s->total_time) {
1158 s->mbps = (((double) transferred_bytes * 8.0) /
1159 ((double) s->total_time)) / 1000;
1160 }
a3fa1d78
PB
1161 runstate_set(RUN_STATE_POSTMIGRATE);
1162 } else {
1163 if (old_vm_running) {
a3fa1d78 1164 vm_start();
dba433c0 1165 }
0d82d0e8 1166 }
bb1fadc4 1167 qemu_bh_schedule(s->cleanup_bh);
dba433c0 1168 qemu_mutex_unlock_iothread();
f4410a5d 1169
ab28bd23 1170 rcu_unregister_thread();
0d82d0e8
JQ
1171 return NULL;
1172}
1173
9848a404 1174void migrate_fd_connect(MigrationState *s)
0d82d0e8 1175{
cc283e3b
JQ
1176 /* This is a best 1st approximation. ns to ms */
1177 s->expected_downtime = max_downtime/1000000;
bb1fadc4 1178 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
0d82d0e8 1179
442773ce
PB
1180 qemu_file_set_rate_limit(s->file,
1181 s->bandwidth_limit / XFER_LIMIT_RATIO);
1182
9287ac27
SH
1183 /* Notify before starting migration thread */
1184 notifier_list_notify(&migration_state_notifiers, s);
1185
8706d2d5 1186 migrate_compress_threads_create();
4900116e 1187 qemu_thread_create(&s->thread, "migration", migration_thread, s,
bb1fadc4 1188 QEMU_THREAD_JOINABLE);
0d82d0e8 1189}