migration.c 19.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * QEMU live migration
 *
 * Copyright IBM, Corp. 2008
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
12 13
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
14 15 16
 */

#include "qemu-common.h"
17
#include "qemu/main-loop.h"
18
#include "migration/migration.h"
19
#include "monitor/monitor.h"
20
#include "migration/qemu-file.h"
21
#include "sysemu/sysemu.h"
22
#include "block/block.h"
23
#include "qemu/sockets.h"
24
#include "migration/block.h"
25
#include "qemu/thread.h"
26
#include "qmp-commands.h"
27
#include "trace.h"
28

29
enum {
30 31
    MIG_STATE_ERROR = -1,
    MIG_STATE_NONE,
32
    MIG_STATE_SETUP,
33
    MIG_STATE_CANCELLING,
34 35 36 37
    MIG_STATE_CANCELLED,
    MIG_STATE_ACTIVE,
    MIG_STATE_COMPLETED,
};
38

39
#define MAX_THROTTLE  (32 << 20)      /* Migration speed throttling */
40

41 42 43 44 45
/* Amount of time to allocate to each "chunk" of bandwidth-throttled
 * data. */
#define BUFFER_DELAY     100
#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)

46 47 48
/* Migration XBZRLE default cache size */
#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)

49 50 51
static NotifierList migration_state_notifiers =
    NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);

52 53 54 55
/* When we add fault tolerance, we could have several
   migrations at once.  For now we don't need to add
   dynamic creation of migration */

56
MigrationState *migrate_get_current(void)
57 58
{
    static MigrationState current_migration = {
59
        .state = MIG_STATE_NONE,
60
        .bandwidth_limit = MAX_THROTTLE,
61
        .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
62
        .mbps = -1,
63 64 65 66 67
    };

    return &current_migration;
}

68
void qemu_start_incoming_migration(const char *uri, Error **errp)
69
{
70 71 72
    const char *p;

    if (strstart(uri, "tcp:", &p))
73
        tcp_start_incoming_migration(p, errp);
Michael R. Hines's avatar
Michael R. Hines committed
74
#ifdef CONFIG_RDMA
75
    else if (strstart(uri, "rdma:", &p))
Michael R. Hines's avatar
Michael R. Hines committed
76 77
        rdma_start_incoming_migration(p, errp);
#endif
78 79
#if !defined(WIN32)
    else if (strstart(uri, "exec:", &p))
80
        exec_start_incoming_migration(p, errp);
81
    else if (strstart(uri, "unix:", &p))
82
        unix_start_incoming_migration(p, errp);
83
    else if (strstart(uri, "fd:", &p))
84
        fd_start_incoming_migration(p, errp);
85
#endif
86
    else {
87
        error_setg(errp, "unknown migration protocol: %s", uri);
88
    }
89 90
}

91
static void process_incoming_migration_co(void *opaque)
92
{
93
    QEMUFile *f = opaque;
94
    Error *local_err = NULL;
95 96 97 98
    int ret;

    ret = qemu_loadvm_state(f);
    qemu_fclose(f);
99
    free_xbzrle_decoded_buf();
100
    if (ret < 0) {
101
        error_report("load of migration failed: %s", strerror(-ret));
102
        exit(EXIT_FAILURE);
103 104 105
    }
    qemu_announce_self();

106
    /* Make sure all file formats flush their mutable metadata */
107 108 109 110 111 112
    bdrv_invalidate_cache_all(&local_err);
    if (local_err) {
        qerror_report_err(local_err);
        error_free(local_err);
        exit(EXIT_FAILURE);
    }
113

114
    if (autostart) {
115
        vm_start();
116
    } else {
117
        runstate_set(RUN_STATE_PAUSED);
118
    }
119 120
}

121 122 123 124 125 126
void process_incoming_migration(QEMUFile *f)
{
    Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
    int fd = qemu_get_fd(f);

    assert(fd != -1);
127
    qemu_set_nonblock(fd);
128 129 130
    qemu_coroutine_enter(co, f);
}

131 132 133 134
/* amount of nanoseconds we are willing to wait for migration to be down.
 * the choice of nanoseconds is because it is the maximum resolution that
 * get_clock() can achieve. It is an internal measure. All user-visible
 * units must be in seconds */
135
static uint64_t max_downtime = 300000000;
136 137 138 139 140 141

uint64_t migrate_max_downtime(void)
{
    return max_downtime;
}

142 143 144 145 146 147 148
MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
{
    MigrationCapabilityStatusList *head = NULL;
    MigrationCapabilityStatusList *caps;
    MigrationState *s = migrate_get_current();
    int i;

149
    caps = NULL; /* silence compiler warning */
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
        if (head == NULL) {
            head = g_malloc0(sizeof(*caps));
            caps = head;
        } else {
            caps->next = g_malloc0(sizeof(*caps));
            caps = caps->next;
        }
        caps->value =
            g_malloc(sizeof(*caps->value));
        caps->value->capability = i;
        caps->value->state = s->enabled_capabilities[i];
    }

    return head;
}

Orit Wasserman's avatar
Orit Wasserman committed
167 168 169 170 171 172 173 174 175
static void get_xbzrle_cache_stats(MigrationInfo *info)
{
    if (migrate_use_xbzrle()) {
        info->has_xbzrle_cache = true;
        info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
        info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
        info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
        info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
        info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
176
        info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wasserman's avatar
Orit Wasserman committed
177 178 179 180
        info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
    }
}

181
MigrationInfo *qmp_query_migrate(Error **errp)
182
{
183
    MigrationInfo *info = g_malloc0(sizeof(*info));
184 185 186
    MigrationState *s = migrate_get_current();

    switch (s->state) {
187
    case MIG_STATE_NONE:
188 189
        /* no migration has happened ever */
        break;
190 191 192
    case MIG_STATE_SETUP:
        info->has_status = true;
        info->status = g_strdup("setup");
193
        info->has_total_time = false;
194
        break;
195
    case MIG_STATE_ACTIVE:
196
    case MIG_STATE_CANCELLING:
197 198
        info->has_status = true;
        info->status = g_strdup("active");
199
        info->has_total_time = true;
200
        info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
201
            - s->total_time;
202 203
        info->has_expected_downtime = true;
        info->expected_downtime = s->expected_downtime;
204 205
        info->has_setup_time = true;
        info->setup_time = s->setup_time;
206

207 208 209 210 211
        info->has_ram = true;
        info->ram = g_malloc0(sizeof(*info->ram));
        info->ram->transferred = ram_bytes_transferred();
        info->ram->remaining = ram_bytes_remaining();
        info->ram->total = ram_bytes_total();
212
        info->ram->duplicate = dup_mig_pages_transferred();
213
        info->ram->skipped = skipped_mig_pages_transferred();
214 215
        info->ram->normal = norm_mig_pages_transferred();
        info->ram->normal_bytes = norm_mig_bytes_transferred();
216
        info->ram->dirty_pages_rate = s->dirty_pages_rate;
217
        info->ram->mbps = s->mbps;
218
        info->ram->dirty_sync_count = s->dirty_sync_count;
219

220
        if (blk_mig_active()) {
221 222 223 224 225
            info->has_disk = true;
            info->disk = g_malloc0(sizeof(*info->disk));
            info->disk->transferred = blk_mig_bytes_transferred();
            info->disk->remaining = blk_mig_bytes_remaining();
            info->disk->total = blk_mig_bytes_total();
226
        }
Orit Wasserman's avatar
Orit Wasserman committed
227 228

        get_xbzrle_cache_stats(info);
229 230
        break;
    case MIG_STATE_COMPLETED:
Orit Wasserman's avatar
Orit Wasserman committed
231 232
        get_xbzrle_cache_stats(info);

233 234
        info->has_status = true;
        info->status = g_strdup("completed");
235
        info->has_total_time = true;
236
        info->total_time = s->total_time;
237 238
        info->has_downtime = true;
        info->downtime = s->downtime;
239 240
        info->has_setup_time = true;
        info->setup_time = s->setup_time;
241 242 243 244 245 246

        info->has_ram = true;
        info->ram = g_malloc0(sizeof(*info->ram));
        info->ram->transferred = ram_bytes_transferred();
        info->ram->remaining = 0;
        info->ram->total = ram_bytes_total();
247
        info->ram->duplicate = dup_mig_pages_transferred();
248
        info->ram->skipped = skipped_mig_pages_transferred();
249 250
        info->ram->normal = norm_mig_pages_transferred();
        info->ram->normal_bytes = norm_mig_bytes_transferred();
251
        info->ram->mbps = s->mbps;
252
        info->ram->dirty_sync_count = s->dirty_sync_count;
253 254
        break;
    case MIG_STATE_ERROR:
255 256
        info->has_status = true;
        info->status = g_strdup("failed");
257 258
        break;
    case MIG_STATE_CANCELLED:
259 260
        info->has_status = true;
        info->status = g_strdup("cancelled");
261
        break;
262
    }
263 264

    return info;
265 266
}

267 268 269 270 271 272
void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
                                  Error **errp)
{
    MigrationState *s = migrate_get_current();
    MigrationCapabilityStatusList *cap;

273
    if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
274 275 276 277 278 279 280 281 282
        error_set(errp, QERR_MIGRATION_ACTIVE);
        return;
    }

    for (cap = params; cap; cap = cap->next) {
        s->enabled_capabilities[cap->value->capability] = cap->value->state;
    }
}

283 284
/* shared migration helpers */

285 286 287 288 289 290 291
static void migrate_set_state(MigrationState *s, int old_state, int new_state)
{
    if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
        trace_migrate_set_state(new_state);
    }
}

292
static void migrate_fd_cleanup(void *opaque)
293
{
294 295 296 297 298
    MigrationState *s = opaque;

    qemu_bh_delete(s->cleanup_bh);
    s->cleanup_bh = NULL;

299
    if (s->file) {
300
        trace_migrate_fd_cleanup();
301 302 303 304
        qemu_mutex_unlock_iothread();
        qemu_thread_join(&s->thread);
        qemu_mutex_lock_iothread();

305 306
        qemu_fclose(s->file);
        s->file = NULL;
307 308
    }

309
    assert(s->state != MIG_STATE_ACTIVE);
310

311
    if (s->state != MIG_STATE_COMPLETED) {
312
        qemu_savevm_state_cancel();
313 314 315
        if (s->state == MIG_STATE_CANCELLING) {
            migrate_set_state(s, MIG_STATE_CANCELLING, MIG_STATE_CANCELLED);
        }
316
    }
317 318

    notifier_list_notify(&migration_state_notifiers, s);
319 320
}

321
void migrate_fd_error(MigrationState *s)
322
{
323
    trace_migrate_fd_error();
324 325 326 327
    assert(s->file == NULL);
    s->state = MIG_STATE_ERROR;
    trace_migrate_set_state(MIG_STATE_ERROR);
    notifier_list_notify(&migration_state_notifiers, s);
328 329
}

330
static void migrate_fd_cancel(MigrationState *s)
331
{
332
    int old_state ;
333
    trace_migrate_fd_cancel();
334

335 336 337 338 339
    do {
        old_state = s->state;
        if (old_state != MIG_STATE_SETUP && old_state != MIG_STATE_ACTIVE) {
            break;
        }
340 341
        migrate_set_state(s, old_state, MIG_STATE_CANCELLING);
    } while (s->state != MIG_STATE_CANCELLING);
342 343
}

344 345 346 347 348 349 350
void add_migration_state_change_notifier(Notifier *notify)
{
    notifier_list_add(&migration_state_notifiers, notify);
}

void remove_migration_state_change_notifier(Notifier *notify)
{
351
    notifier_remove(notify);
352 353
}

354
bool migration_in_setup(MigrationState *s)
355
{
356
    return s->state == MIG_STATE_SETUP;
357 358
}

359
bool migration_has_finished(MigrationState *s)
360
{
361
    return s->state == MIG_STATE_COMPLETED;
362
}
363

364 365 366 367 368 369
bool migration_has_failed(MigrationState *s)
{
    return (s->state == MIG_STATE_CANCELLED ||
            s->state == MIG_STATE_ERROR);
}

370
static MigrationState *migrate_init(const MigrationParams *params)
371
{
372
    MigrationState *s = migrate_get_current();
373
    int64_t bandwidth_limit = s->bandwidth_limit;
374
    bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
375
    int64_t xbzrle_cache_size = s->xbzrle_cache_size;
376 377 378

    memcpy(enabled_capabilities, s->enabled_capabilities,
           sizeof(enabled_capabilities));
379

380
    memset(s, 0, sizeof(*s));
381
    s->params = *params;
382 383
    memcpy(s->enabled_capabilities, enabled_capabilities,
           sizeof(enabled_capabilities));
384
    s->xbzrle_cache_size = xbzrle_cache_size;
385

386
    s->bandwidth_limit = bandwidth_limit;
387
    s->state = MIG_STATE_SETUP;
388
    trace_migrate_set_state(MIG_STATE_SETUP);
389

390
    s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
391 392
    return s;
}
393

394 395 396 397 398 399 400 401 402 403 404 405
static GSList *migration_blockers;

void migrate_add_blocker(Error *reason)
{
    migration_blockers = g_slist_prepend(migration_blockers, reason);
}

void migrate_del_blocker(Error *reason)
{
    migration_blockers = g_slist_remove(migration_blockers, reason);
}

Luiz Capitulino's avatar
Luiz Capitulino committed
406 407 408
void qmp_migrate(const char *uri, bool has_blk, bool blk,
                 bool has_inc, bool inc, bool has_detach, bool detach,
                 Error **errp)
409
{
410
    Error *local_err = NULL;
411
    MigrationState *s = migrate_get_current();
412
    MigrationParams params;
413 414
    const char *p;

415 416
    params.blk = has_blk && blk;
    params.shared = has_inc && inc;
417

418 419
    if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP ||
        s->state == MIG_STATE_CANCELLING) {
Luiz Capitulino's avatar
Luiz Capitulino committed
420 421
        error_set(errp, QERR_MIGRATION_ACTIVE);
        return;
422 423
    }

424 425 426 427 428
    if (runstate_check(RUN_STATE_INMIGRATE)) {
        error_setg(errp, "Guest is waiting for an incoming migration");
        return;
    }

Luiz Capitulino's avatar
Luiz Capitulino committed
429 430
    if (qemu_savevm_state_blocked(errp)) {
        return;
431 432
    }

433
    if (migration_blockers) {
Luiz Capitulino's avatar
Luiz Capitulino committed
434 435
        *errp = error_copy(migration_blockers->data);
        return;
436 437
    }

438
    s = migrate_init(&params);
439 440

    if (strstart(uri, "tcp:", &p)) {
441
        tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines's avatar
Michael R. Hines committed
442
#ifdef CONFIG_RDMA
443
    } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines's avatar
Michael R. Hines committed
444 445
        rdma_start_outgoing_migration(s, p, &local_err);
#endif
446 447
#if !defined(WIN32)
    } else if (strstart(uri, "exec:", &p)) {
448
        exec_start_outgoing_migration(s, p, &local_err);
449
    } else if (strstart(uri, "unix:", &p)) {
450
        unix_start_outgoing_migration(s, p, &local_err);
451
    } else if (strstart(uri, "fd:", &p)) {
452
        fd_start_outgoing_migration(s, p, &local_err);
453
#endif
454
    } else {
Luiz Capitulino's avatar
Luiz Capitulino committed
455
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
456
        s->state = MIG_STATE_ERROR;
Luiz Capitulino's avatar
Luiz Capitulino committed
457
        return;
458 459
    }

460
    if (local_err) {
461
        migrate_fd_error(s);
462
        error_propagate(errp, local_err);
Luiz Capitulino's avatar
Luiz Capitulino committed
463
        return;
464
    }
465 466
}

467
void qmp_migrate_cancel(Error **errp)
468
{
469
    migrate_fd_cancel(migrate_get_current());
470 471
}

472 473 474
void qmp_migrate_set_cache_size(int64_t value, Error **errp)
{
    MigrationState *s = migrate_get_current();
475
    int64_t new_size;
476 477 478 479 480 481 482 483

    /* Check for truncation */
    if (value != (size_t)value) {
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
                  "exceeding address space");
        return;
    }

484 485 486 487 488 489 490
    /* Cache should not be larger than guest ram size */
    if (value > ram_bytes_total()) {
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
                  "exceeds guest ram size ");
        return;
    }

491 492 493 494 495 496 497 498
    new_size = xbzrle_cache_resize(value);
    if (new_size < 0) {
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
                  "is smaller than page size");
        return;
    }

    s->xbzrle_cache_size = new_size;
499 500 501 502 503 504 505
}

int64_t qmp_query_migrate_cache_size(Error **errp)
{
    return migrate_xbzrle_cache_size();
}

506
void qmp_migrate_set_speed(int64_t value, Error **errp)
507 508 509
{
    MigrationState *s;

510 511
    if (value < 0) {
        value = 0;
512
    }
513 514 515
    if (value > SIZE_MAX) {
        value = SIZE_MAX;
    }
516

517
    s = migrate_get_current();
518
    s->bandwidth_limit = value;
519 520 521
    if (s->file) {
        qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
    }
522 523
}

524
void qmp_migrate_set_downtime(double value, Error **errp)
525
{
526 527 528
    value *= 1e9;
    value = MAX(0, MIN(UINT64_MAX, value));
    max_downtime = (uint64_t)value;
529
}
530

531 532 533 534 535 536
bool migrate_rdma_pin_all(void)
{
    MigrationState *s;

    s = migrate_get_current();

537
    return s->enabled_capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
538 539
}

540 541 542 543 544 545 546 547 548
bool migrate_auto_converge(void)
{
    MigrationState *s;

    s = migrate_get_current();

    return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
}

549 550 551 552 553 554 555 556 557
bool migrate_zero_blocks(void)
{
    MigrationState *s;

    s = migrate_get_current();

    return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
}

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
int migrate_use_xbzrle(void)
{
    MigrationState *s;

    s = migrate_get_current();

    return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
}

int64_t migrate_xbzrle_cache_size(void)
{
    MigrationState *s;

    s = migrate_get_current();

    return s->xbzrle_cache_size;
}
575 576 577

/* migration thread support */

578
static void *migration_thread(void *opaque)
579
{
580
    MigrationState *s = opaque;
581 582
    int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
    int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
583
    int64_t initial_bytes = 0;
584
    int64_t max_size = 0;
585 586
    int64_t start_time = initial_time;
    bool old_vm_running = false;
587

588
    qemu_savevm_state_begin(s->file, &s->params);
589

590
    s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
591 592
    migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);

593
    while (s->state == MIG_STATE_ACTIVE) {
594
        int64_t current_time;
595
        uint64_t pending_size;
596

597
        if (!qemu_file_rate_limit(s->file)) {
598
            pending_size = qemu_savevm_state_pending(s->file, max_size);
599
            trace_migrate_pending(pending_size, max_size);
600
            if (pending_size && pending_size >= max_size) {
601
                qemu_savevm_state_iterate(s->file);
602
            } else {
603 604
                int ret;

605
                qemu_mutex_lock_iothread();
606
                start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
607
                qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
608
                old_vm_running = runstate_is_running();
609 610 611

                ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
                if (ret >= 0) {
612
                    qemu_file_set_rate_limit(s->file, INT64_MAX);
613 614
                    qemu_savevm_state_complete(s->file);
                }
615
                qemu_mutex_unlock_iothread();
616 617

                if (ret < 0) {
618
                    migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
619 620 621
                    break;
                }

622
                if (!qemu_file_get_error(s->file)) {
623
                    migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
624 625
                    break;
                }
626 627
            }
        }
628

629
        if (qemu_file_get_error(s->file)) {
630
            migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
631 632
            break;
        }
633
        current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
634
        if (current_time >= initial_time + BUFFER_DELAY) {
635
            uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
636
            uint64_t time_spent = current_time - initial_time;
637 638 639
            double bandwidth = transferred_bytes / time_spent;
            max_size = bandwidth * migrate_max_downtime() / 1000000;

640 641 642
            s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
                    ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;

643 644
            trace_migrate_transferred(transferred_bytes, time_spent,
                                      bandwidth, max_size);
645 646 647 648 649
            /* if we haven't sent anything, we don't want to recalculate
               10000 is a small enough number for our purposes */
            if (s->dirty_bytes_rate && transferred_bytes > 10000) {
                s->expected_downtime = s->dirty_bytes_rate / bandwidth;
            }
650

651
            qemu_file_reset_rate_limit(s->file);
652
            initial_time = current_time;
653
            initial_bytes = qemu_ftell(s->file);
654
        }
655
        if (qemu_file_rate_limit(s->file)) {
656 657 658
            /* usleep expects microseconds */
            g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
        }
659 660
    }

661
    qemu_mutex_lock_iothread();
662
    if (s->state == MIG_STATE_COMPLETED) {
663
        int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
664
        uint64_t transferred_bytes = qemu_ftell(s->file);
665 666
        s->total_time = end_time - s->total_time;
        s->downtime = end_time - start_time;
667 668 669 670
        if (s->total_time) {
            s->mbps = (((double) transferred_bytes * 8.0) /
                       ((double) s->total_time)) / 1000;
        }
671 672 673 674
        runstate_set(RUN_STATE_POSTMIGRATE);
    } else {
        if (old_vm_running) {
            vm_start();
675
        }
676
    }
677
    qemu_bh_schedule(s->cleanup_bh);
678
    qemu_mutex_unlock_iothread();
679

680 681 682
    return NULL;
}

683
void migrate_fd_connect(MigrationState *s)
684
{
685 686
    s->state = MIG_STATE_SETUP;
    trace_migrate_set_state(MIG_STATE_SETUP);
687

688 689
    /* This is a best 1st approximation. ns to ms */
    s->expected_downtime = max_downtime/1000000;
690
    s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
691

692 693 694
    qemu_file_set_rate_limit(s->file,
                             s->bandwidth_limit / XFER_LIMIT_RATIO);

695 696 697
    /* Notify before starting migration thread */
    notifier_list_notify(&migration_state_notifiers, s);

698
    qemu_thread_create(&s->thread, "migration", migration_thread, s,
699
                       QEMU_THREAD_JOINABLE);
700
}