migration.c 18.6 KB
Newer Older
A
aliguori 已提交
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.
A
aliguori 已提交
14 15 16
 */

#include "qemu-common.h"
17
#include "migration/migration.h"
18
#include "monitor/monitor.h"
19
#include "migration/qemu-file.h"
20
#include "sysemu/sysemu.h"
21
#include "block/block.h"
22
#include "qemu/sockets.h"
23
#include "migration/block.h"
24
#include "qemu/thread.h"
L
Luiz Capitulino 已提交
25
#include "qmp-commands.h"
26
#include "trace.h"
27 28 29 30

//#define DEBUG_MIGRATION

#ifdef DEBUG_MIGRATION
M
malc 已提交
31
#define DPRINTF(fmt, ...) \
32 33
    do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
#else
M
malc 已提交
34
#define DPRINTF(fmt, ...) \
35 36
    do { } while (0)
#endif
A
aliguori 已提交
37

38
enum {
39 40
    MIG_STATE_ERROR = -1,
    MIG_STATE_NONE,
41 42 43 44 45
    MIG_STATE_SETUP,
    MIG_STATE_CANCELLED,
    MIG_STATE_ACTIVE,
    MIG_STATE_COMPLETED,
};
A
aliguori 已提交
46

47
#define MAX_THROTTLE  (32 << 20)      /* Migration speed throttling */
A
aliguori 已提交
48

J
Juan Quintela 已提交
49 50 51 52 53
/* Amount of time to allocate to each "chunk" of bandwidth-throttled
 * data. */
#define BUFFER_DELAY     100
#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)

54 55 56
/* Migration XBZRLE default cache size */
#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)

57 58 59
static NotifierList migration_state_notifiers =
    NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);

60 61 62 63
/* When we add fault tolerance, we could have several
   migrations at once.  For now we don't need to add
   dynamic creation of migration */

64
MigrationState *migrate_get_current(void)
65 66
{
    static MigrationState current_migration = {
67
        .state = MIG_STATE_NONE,
68
        .bandwidth_limit = MAX_THROTTLE,
69
        .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
70
        .mbps = -1,
71 72 73 74 75
    };

    return &current_migration;
}

76
void qemu_start_incoming_migration(const char *uri, Error **errp)
A
aliguori 已提交
77
{
A
aliguori 已提交
78 79 80
    const char *p;

    if (strstart(uri, "tcp:", &p))
81
        tcp_start_incoming_migration(p, errp);
M
Michael R. Hines 已提交
82 83 84 85
#ifdef CONFIG_RDMA
    else if (strstart(uri, "x-rdma:", &p))
        rdma_start_incoming_migration(p, errp);
#endif
86 87
#if !defined(WIN32)
    else if (strstart(uri, "exec:", &p))
88
        exec_start_incoming_migration(p, errp);
C
Chris Lalancette 已提交
89
    else if (strstart(uri, "unix:", &p))
90
        unix_start_incoming_migration(p, errp);
P
Paolo Bonzini 已提交
91
    else if (strstart(uri, "fd:", &p))
92
        fd_start_incoming_migration(p, errp);
93
#endif
J
Juan Quintela 已提交
94
    else {
95
        error_setg(errp, "unknown migration protocol: %s", uri);
J
Juan Quintela 已提交
96
    }
A
aliguori 已提交
97 98
}

99
static void process_incoming_migration_co(void *opaque)
100
{
101
    QEMUFile *f = opaque;
102 103 104 105 106
    int ret;

    ret = qemu_loadvm_state(f);
    qemu_fclose(f);
    if (ret < 0) {
107
        fprintf(stderr, "load of migration failed\n");
108
        exit(EXIT_FAILURE);
109 110 111 112
    }
    qemu_announce_self();
    DPRINTF("successfully loaded vm state\n");

113
    bdrv_clear_incoming_migration_all();
114 115 116
    /* Make sure all file formats flush their mutable metadata */
    bdrv_invalidate_cache_all();

117
    if (autostart) {
118
        vm_start();
119
    } else {
120
        runstate_set(RUN_STATE_PAUSED);
121
    }
122 123
}

124 125 126 127 128 129
void process_incoming_migration(QEMUFile *f)
{
    Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
    int fd = qemu_get_fd(f);

    assert(fd != -1);
130
    qemu_set_nonblock(fd);
131 132 133
    qemu_coroutine_enter(co, f);
}

134 135 136 137 138 139 140 141 142 143 144
/* 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 */
static uint64_t max_downtime = 30000000;

uint64_t migrate_max_downtime(void)
{
    return max_downtime;
}

O
Orit Wasserman 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
{
    MigrationCapabilityStatusList *head = NULL;
    MigrationCapabilityStatusList *caps;
    MigrationState *s = migrate_get_current();
    int i;

    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;
}

O
Orit Wasserman 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181
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();
        info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
    }
}

L
Luiz Capitulino 已提交
182
MigrationInfo *qmp_query_migrate(Error **errp)
A
aliguori 已提交
183
{
L
Luiz Capitulino 已提交
184
    MigrationInfo *info = g_malloc0(sizeof(*info));
185 186 187
    MigrationState *s = migrate_get_current();

    switch (s->state) {
188
    case MIG_STATE_NONE:
189 190
        /* no migration has happened ever */
        break;
191 192 193
    case MIG_STATE_SETUP:
        info->has_status = true;
        info->status = g_strdup("setup");
194
        info->has_total_time = false;
195
        break;
196
    case MIG_STATE_ACTIVE:
L
Luiz Capitulino 已提交
197 198
        info->has_status = true;
        info->status = g_strdup("active");
199 200 201
        info->has_total_time = true;
        info->total_time = qemu_get_clock_ms(rt_clock)
            - 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

L
Luiz Capitulino 已提交
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

219
        if (blk_mig_active()) {
L
Luiz Capitulino 已提交
220 221 222 223 224
            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();
A
aliguori 已提交
225
        }
O
Orit Wasserman 已提交
226 227

        get_xbzrle_cache_stats(info);
228 229
        break;
    case MIG_STATE_COMPLETED:
O
Orit Wasserman 已提交
230 231
        get_xbzrle_cache_stats(info);

L
Luiz Capitulino 已提交
232 233
        info->has_status = true;
        info->status = g_strdup("completed");
234
        info->has_total_time = true;
235
        info->total_time = s->total_time;
236 237
        info->has_downtime = true;
        info->downtime = s->downtime;
238 239
        info->has_setup_time = true;
        info->setup_time = s->setup_time;
J
Juan Quintela 已提交
240 241 242 243 244 245

        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();
246
        info->ram->duplicate = dup_mig_pages_transferred();
247
        info->ram->skipped = skipped_mig_pages_transferred();
248 249
        info->ram->normal = norm_mig_pages_transferred();
        info->ram->normal_bytes = norm_mig_bytes_transferred();
250
        info->ram->mbps = s->mbps;
251 252
        break;
    case MIG_STATE_ERROR:
L
Luiz Capitulino 已提交
253 254
        info->has_status = true;
        info->status = g_strdup("failed");
255 256
        break;
    case MIG_STATE_CANCELLED:
L
Luiz Capitulino 已提交
257 258
        info->has_status = true;
        info->status = g_strdup("cancelled");
259
        break;
A
aliguori 已提交
260
    }
L
Luiz Capitulino 已提交
261 262

    return info;
A
aliguori 已提交
263 264
}

O
Orit Wasserman 已提交
265 266 267 268 269 270
void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
                                  Error **errp)
{
    MigrationState *s = migrate_get_current();
    MigrationCapabilityStatusList *cap;

271
    if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
O
Orit Wasserman 已提交
272 273 274 275 276 277 278 279 280
        error_set(errp, QERR_MIGRATION_ACTIVE);
        return;
    }

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

281 282
/* shared migration helpers */

283
static void migrate_fd_cleanup(void *opaque)
284
{
285 286 287 288 289
    MigrationState *s = opaque;

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

290
    if (s->file) {
M
malc 已提交
291
        DPRINTF("closing file\n");
292 293 294 295
        qemu_mutex_unlock_iothread();
        qemu_thread_join(&s->thread);
        qemu_mutex_lock_iothread();

296 297
        qemu_fclose(s->file);
        s->file = NULL;
298 299
    }

300
    assert(s->state != MIG_STATE_ACTIVE);
301

302
    if (s->state != MIG_STATE_COMPLETED) {
303 304
        qemu_savevm_state_cancel();
    }
305 306

    notifier_list_notify(&migration_state_notifiers, s);
307 308
}

309
static void migrate_set_state(MigrationState *s, int old_state, int new_state)
310
{
311
    if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
312 313 314 315
        trace_migrate_set_state(new_state);
    }
}

316
void migrate_fd_error(MigrationState *s)
317
{
318
    DPRINTF("setting error state\n");
319 320 321 322
    assert(s->file == NULL);
    s->state = MIG_STATE_ERROR;
    trace_migrate_set_state(MIG_STATE_ERROR);
    notifier_list_notify(&migration_state_notifiers, s);
323 324
}

325
static void migrate_fd_cancel(MigrationState *s)
326
{
M
malc 已提交
327
    DPRINTF("cancelling migration\n");
328

329
    migrate_set_state(s, s->state, MIG_STATE_CANCELLED);
330 331
}

332 333 334 335 336 337 338
void add_migration_state_change_notifier(Notifier *notify)
{
    notifier_list_add(&migration_state_notifiers, notify);
}

void remove_migration_state_change_notifier(Notifier *notify)
{
P
Paolo Bonzini 已提交
339
    notifier_remove(notify);
340 341
}

S
Stefan Hajnoczi 已提交
342
bool migration_in_setup(MigrationState *s)
343
{
S
Stefan Hajnoczi 已提交
344
    return s->state == MIG_STATE_SETUP;
345 346
}

347
bool migration_has_finished(MigrationState *s)
348
{
349
    return s->state == MIG_STATE_COMPLETED;
350
}
351

352 353 354 355 356 357
bool migration_has_failed(MigrationState *s)
{
    return (s->state == MIG_STATE_CANCELLED ||
            s->state == MIG_STATE_ERROR);
}

I
Isaku Yamahata 已提交
358
static MigrationState *migrate_init(const MigrationParams *params)
359
{
360
    MigrationState *s = migrate_get_current();
361
    int64_t bandwidth_limit = s->bandwidth_limit;
O
Orit Wasserman 已提交
362
    bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
363
    int64_t xbzrle_cache_size = s->xbzrle_cache_size;
O
Orit Wasserman 已提交
364 365 366

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

368
    memset(s, 0, sizeof(*s));
I
Isaku Yamahata 已提交
369
    s->params = *params;
O
Orit Wasserman 已提交
370 371
    memcpy(s->enabled_capabilities, enabled_capabilities,
           sizeof(enabled_capabilities));
372
    s->xbzrle_cache_size = xbzrle_cache_size;
373

374
    s->bandwidth_limit = bandwidth_limit;
375
    s->state = MIG_STATE_SETUP;
376
    trace_migrate_set_state(MIG_STATE_SETUP);
377

378
    s->total_time = qemu_get_clock_ms(rt_clock);
379 380
    return s;
}
381

A
Anthony Liguori 已提交
382 383 384 385 386 387 388 389 390 391 392 393
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);
}

L
Luiz Capitulino 已提交
394 395 396
void qmp_migrate(const char *uri, bool has_blk, bool blk,
                 bool has_inc, bool inc, bool has_detach, bool detach,
                 Error **errp)
397
{
398
    Error *local_err = NULL;
399
    MigrationState *s = migrate_get_current();
I
Isaku Yamahata 已提交
400
    MigrationParams params;
401 402
    const char *p;

403 404
    params.blk = has_blk && blk;
    params.shared = has_inc && inc;
I
Isaku Yamahata 已提交
405

406
    if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
L
Luiz Capitulino 已提交
407 408
        error_set(errp, QERR_MIGRATION_ACTIVE);
        return;
409 410
    }

L
Luiz Capitulino 已提交
411 412
    if (qemu_savevm_state_blocked(errp)) {
        return;
413 414
    }

A
Anthony Liguori 已提交
415
    if (migration_blockers) {
L
Luiz Capitulino 已提交
416 417
        *errp = error_copy(migration_blockers->data);
        return;
A
Anthony Liguori 已提交
418 419
    }

I
Isaku Yamahata 已提交
420
    s = migrate_init(&params);
421 422

    if (strstart(uri, "tcp:", &p)) {
423
        tcp_start_outgoing_migration(s, p, &local_err);
M
Michael R. Hines 已提交
424 425 426 427
#ifdef CONFIG_RDMA
    } else if (strstart(uri, "x-rdma:", &p)) {
        rdma_start_outgoing_migration(s, p, &local_err);
#endif
428 429
#if !defined(WIN32)
    } else if (strstart(uri, "exec:", &p)) {
430
        exec_start_outgoing_migration(s, p, &local_err);
431
    } else if (strstart(uri, "unix:", &p)) {
432
        unix_start_outgoing_migration(s, p, &local_err);
433
    } else if (strstart(uri, "fd:", &p)) {
434
        fd_start_outgoing_migration(s, p, &local_err);
435
#endif
436
    } else {
L
Luiz Capitulino 已提交
437 438
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
        return;
439 440
    }

441
    if (local_err) {
442
        migrate_fd_error(s);
443
        error_propagate(errp, local_err);
L
Luiz Capitulino 已提交
444
        return;
445
    }
446 447
}

L
Luiz Capitulino 已提交
448
void qmp_migrate_cancel(Error **errp)
449
{
450
    migrate_fd_cancel(migrate_get_current());
451 452
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
void qmp_migrate_set_cache_size(int64_t value, Error **errp)
{
    MigrationState *s = migrate_get_current();

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

    s->xbzrle_cache_size = xbzrle_cache_resize(value);
}

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

L
Luiz Capitulino 已提交
472
void qmp_migrate_set_speed(int64_t value, Error **errp)
473 474 475
{
    MigrationState *s;

L
Luiz Capitulino 已提交
476 477
    if (value < 0) {
        value = 0;
478
    }
479 480 481
    if (value > SIZE_MAX) {
        value = SIZE_MAX;
    }
482

483
    s = migrate_get_current();
L
Luiz Capitulino 已提交
484
    s->bandwidth_limit = value;
485 486 487
    if (s->file) {
        qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
    }
488 489
}

490
void qmp_migrate_set_downtime(double value, Error **errp)
491
{
492 493 494
    value *= 1e9;
    value = MAX(0, MIN(UINT64_MAX, value));
    max_downtime = (uint64_t)value;
495
}
496

497 498 499 500 501 502 503 504 505
bool migrate_rdma_pin_all(void)
{
    MigrationState *s;

    s = migrate_get_current();

    return s->enabled_capabilities[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL];
}

506 507 508 509 510 511 512 513 514
bool migrate_auto_converge(void)
{
    MigrationState *s;

    s = migrate_get_current();

    return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
}

515 516 517 518 519 520 521 522 523
bool migrate_zero_blocks(void)
{
    MigrationState *s;

    s = migrate_get_current();

    return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
}

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
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;
}
541 542 543

/* migration thread support */

J
Juan Quintela 已提交
544
static void *migration_thread(void *opaque)
545
{
546
    MigrationState *s = opaque;
547
    int64_t initial_time = qemu_get_clock_ms(rt_clock);
548
    int64_t setup_start = qemu_get_clock_ms(host_clock);
549
    int64_t initial_bytes = 0;
550
    int64_t max_size = 0;
551 552
    int64_t start_time = initial_time;
    bool old_vm_running = false;
553 554

    DPRINTF("beginning savevm\n");
555
    qemu_savevm_state_begin(s->file, &s->params);
556

557
    s->setup_time = qemu_get_clock_ms(host_clock) - setup_start;
558 559
    migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);

560 561
    DPRINTF("setup complete\n");

562
    while (s->state == MIG_STATE_ACTIVE) {
563
        int64_t current_time;
564
        uint64_t pending_size;
565

566
        if (!qemu_file_rate_limit(s->file)) {
567 568 569
            DPRINTF("iterate\n");
            pending_size = qemu_savevm_state_pending(s->file, max_size);
            DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
570
            if (pending_size && pending_size >= max_size) {
571
                qemu_savevm_state_iterate(s->file);
572
            } else {
573 574
                int ret;

575
                DPRINTF("done iterating\n");
576
                qemu_mutex_lock_iothread();
577 578
                start_time = qemu_get_clock_ms(rt_clock);
                qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
579
                old_vm_running = runstate_is_running();
580 581 582 583 584 585

                ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
                if (ret >= 0) {
                    qemu_file_set_rate_limit(s->file, INT_MAX);
                    qemu_savevm_state_complete(s->file);
                }
586
                qemu_mutex_unlock_iothread();
587 588

                if (ret < 0) {
589
                    migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
590 591 592
                    break;
                }

P
Paolo Bonzini 已提交
593
                if (!qemu_file_get_error(s->file)) {
594
                    migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
P
Paolo Bonzini 已提交
595 596
                    break;
                }
597 598
            }
        }
599

600
        if (qemu_file_get_error(s->file)) {
601
            migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
602 603
            break;
        }
604
        current_time = qemu_get_clock_ms(rt_clock);
605
        if (current_time >= initial_time + BUFFER_DELAY) {
606
            uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
607
            uint64_t time_spent = current_time - initial_time;
608 609 610
            double bandwidth = transferred_bytes / time_spent;
            max_size = bandwidth * migrate_max_downtime() / 1000000;

611 612 613
            s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
                    ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;

614 615 616
            DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
                    " bandwidth %g max_size %" PRId64 "\n",
                    transferred_bytes, time_spent, bandwidth, max_size);
617 618 619 620 621
            /* 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;
            }
622

623
            qemu_file_reset_rate_limit(s->file);
624
            initial_time = current_time;
625
            initial_bytes = qemu_ftell(s->file);
626
        }
627
        if (qemu_file_rate_limit(s->file)) {
628 629 630
            /* usleep expects microseconds */
            g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
        }
631 632
    }

633
    qemu_mutex_lock_iothread();
634 635 636 637 638 639 640 641
    if (s->state == MIG_STATE_COMPLETED) {
        int64_t end_time = qemu_get_clock_ms(rt_clock);
        s->total_time = end_time - s->total_time;
        s->downtime = end_time - start_time;
        runstate_set(RUN_STATE_POSTMIGRATE);
    } else {
        if (old_vm_running) {
            vm_start();
642
        }
643
    }
644
    qemu_bh_schedule(s->cleanup_bh);
645
    qemu_mutex_unlock_iothread();
646

647 648 649
    return NULL;
}

650
void migrate_fd_connect(MigrationState *s)
651
{
652 653
    s->state = MIG_STATE_SETUP;
    trace_migrate_set_state(MIG_STATE_SETUP);
654

655 656
    /* This is a best 1st approximation. ns to ms */
    s->expected_downtime = max_downtime/1000000;
657
    s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
658

659 660 661
    qemu_file_set_rate_limit(s->file,
                             s->bandwidth_limit / XFER_LIMIT_RATIO);

662 663 664
    /* Notify before starting migration thread */
    notifier_list_notify(&migration_state_notifiers, s);

J
Juan Quintela 已提交
665
    qemu_thread_create(&s->thread, migration_thread, s,
666
                       QEMU_THREAD_JOINABLE);
667
}