migration.c 18.9 KB
Newer Older
aliguori's avatar
aliguori committed
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.
aliguori's avatar
aliguori committed
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"
Luiz Capitulino's avatar
Luiz Capitulino committed
25
#include "qmp-commands.h"
26
#include "trace.h"
27
28
29
30

//#define DEBUG_MIGRATION

#ifdef DEBUG_MIGRATION
malc's avatar
malc committed
31
#define DPRINTF(fmt, ...) \
32
33
    do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
#else
malc's avatar
malc committed
34
#define DPRINTF(fmt, ...) \
35
36
    do { } while (0)
#endif
aliguori's avatar
aliguori committed
37

38
39
40
41
42
43
44
enum {
    MIG_STATE_ERROR,
    MIG_STATE_SETUP,
    MIG_STATE_CANCELLED,
    MIG_STATE_ACTIVE,
    MIG_STATE_COMPLETED,
};
aliguori's avatar
aliguori committed
45

46
#define MAX_THROTTLE  (32 << 20)      /* Migration speed throttling */
aliguori's avatar
aliguori committed
47

48
49
50
51
52
/* Amount of time to allocate to each "chunk" of bandwidth-throttled
 * data. */
#define BUFFER_DELAY     100
#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)

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

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

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

63
MigrationState *migrate_get_current(void)
64
65
66
{
    static MigrationState current_migration = {
        .state = MIG_STATE_SETUP,
67
        .bandwidth_limit = MAX_THROTTLE,
68
        .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
69
70
71
72
73
    };

    return &current_migration;
}

74
void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori's avatar
aliguori committed
75
{
76
77
78
    const char *p;

    if (strstart(uri, "tcp:", &p))
79
        tcp_start_incoming_migration(p, errp);
80
81
#if !defined(WIN32)
    else if (strstart(uri, "exec:", &p))
82
        exec_start_incoming_migration(p, errp);
Chris Lalancette's avatar
Chris Lalancette committed
83
    else if (strstart(uri, "unix:", &p))
84
        unix_start_incoming_migration(p, errp);
Paolo Bonzini's avatar
Paolo Bonzini committed
85
    else if (strstart(uri, "fd:", &p))
86
        fd_start_incoming_migration(p, errp);
87
#endif
88
    else {
89
        error_setg(errp, "unknown migration protocol: %s", uri);
90
    }
aliguori's avatar
aliguori committed
91
92
}

93
static void process_incoming_migration_co(void *opaque)
94
{
95
    QEMUFile *f = opaque;
96
97
98
99
100
    int ret;

    ret = qemu_loadvm_state(f);
    qemu_fclose(f);
    if (ret < 0) {
101
102
103
104
105
106
        fprintf(stderr, "load of migration failed\n");
        exit(0);
    }
    qemu_announce_self();
    DPRINTF("successfully loaded vm state\n");

107
    bdrv_clear_incoming_migration_all();
108
109
110
    /* Make sure all file formats flush their mutable metadata */
    bdrv_invalidate_cache_all();

111
    if (autostart) {
112
        vm_start();
113
    } else {
114
        runstate_set(RUN_STATE_PAUSED);
115
    }
116
117
}

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

    assert(fd != -1);
    socket_set_nonblock(fd);
    qemu_coroutine_enter(co, f);
}

128
129
130
131
132
133
134
135
136
137
138
/* 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;
}

Orit Wasserman's avatar
Orit Wasserman committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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;
}

Orit Wasserman's avatar
Orit Wasserman committed
163
164
165
166
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();
        info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
    }
}

Luiz Capitulino's avatar
Luiz Capitulino committed
176
MigrationInfo *qmp_query_migrate(Error **errp)
aliguori's avatar
aliguori committed
177
{
Luiz Capitulino's avatar
Luiz Capitulino committed
178
    MigrationInfo *info = g_malloc0(sizeof(*info));
179
180
181
182
183
184
185
    MigrationState *s = migrate_get_current();

    switch (s->state) {
    case MIG_STATE_SETUP:
        /* no migration has happened ever */
        break;
    case MIG_STATE_ACTIVE:
Luiz Capitulino's avatar
Luiz Capitulino committed
186
187
        info->has_status = true;
        info->status = g_strdup("active");
188
189
190
        info->has_total_time = true;
        info->total_time = qemu_get_clock_ms(rt_clock)
            - s->total_time;
191
192
        info->has_expected_downtime = true;
        info->expected_downtime = s->expected_downtime;
193

Luiz Capitulino's avatar
Luiz Capitulino committed
194
195
196
197
198
        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();
199
200
201
        info->ram->duplicate = dup_mig_pages_transferred();
        info->ram->normal = norm_mig_pages_transferred();
        info->ram->normal_bytes = norm_mig_bytes_transferred();
202
203
        info->ram->dirty_pages_rate = s->dirty_pages_rate;

204
205

        if (blk_mig_active()) {
Luiz Capitulino's avatar
Luiz Capitulino committed
206
207
208
209
210
            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();
aliguori's avatar
aliguori committed
211
        }
Orit Wasserman's avatar
Orit Wasserman committed
212
213

        get_xbzrle_cache_stats(info);
214
215
        break;
    case MIG_STATE_COMPLETED:
Orit Wasserman's avatar
Orit Wasserman committed
216
217
        get_xbzrle_cache_stats(info);

Luiz Capitulino's avatar
Luiz Capitulino committed
218
219
        info->has_status = true;
        info->status = g_strdup("completed");
220
        info->total_time = s->total_time;
221
222
        info->has_downtime = true;
        info->downtime = s->downtime;
Juan Quintela's avatar
Juan Quintela committed
223
224
225
226
227
228

        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();
229
230
231
        info->ram->duplicate = dup_mig_pages_transferred();
        info->ram->normal = norm_mig_pages_transferred();
        info->ram->normal_bytes = norm_mig_bytes_transferred();
232
233
        break;
    case MIG_STATE_ERROR:
Luiz Capitulino's avatar
Luiz Capitulino committed
234
235
        info->has_status = true;
        info->status = g_strdup("failed");
236
237
        break;
    case MIG_STATE_CANCELLED:
Luiz Capitulino's avatar
Luiz Capitulino committed
238
239
        info->has_status = true;
        info->status = g_strdup("cancelled");
240
        break;
aliguori's avatar
aliguori committed
241
    }
Luiz Capitulino's avatar
Luiz Capitulino committed
242
243

    return info;
aliguori's avatar
aliguori committed
244
245
}

Orit Wasserman's avatar
Orit Wasserman committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
                                  Error **errp)
{
    MigrationState *s = migrate_get_current();
    MigrationCapabilityStatusList *cap;

    if (s->state == MIG_STATE_ACTIVE) {
        error_set(errp, QERR_MIGRATION_ACTIVE);
        return;
    }

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

262
263
/* shared migration helpers */

264
static void migrate_fd_cleanup(void *opaque)
265
{
266
267
268
269
270
    MigrationState *s = opaque;

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

271
    if (s->file) {
malc's avatar
malc committed
272
        DPRINTF("closing file\n");
273
        qemu_fclose(s->file);
274
        s->file = NULL;
275
276
    }

277
    assert(s->migration_file == NULL);
278
    assert(s->state != MIG_STATE_ACTIVE);
279

280
    if (s->state != MIG_STATE_COMPLETED) {
281
282
        qemu_savevm_state_cancel();
    }
283
284

    notifier_list_notify(&migration_state_notifiers, s);
285
286
}

287
288
289
290
291
292
293
294
static void migrate_finish_set_state(MigrationState *s, int new_state)
{
    if (__sync_val_compare_and_swap(&s->state, MIG_STATE_ACTIVE,
                                    new_state) == new_state) {
        trace_migrate_set_state(new_state);
    }
}

295
void migrate_fd_error(MigrationState *s)
296
{
297
    DPRINTF("setting error state\n");
298
299
300
301
    assert(s->file == NULL);
    s->state = MIG_STATE_ERROR;
    trace_migrate_set_state(MIG_STATE_ERROR);
    notifier_list_notify(&migration_state_notifiers, s);
302
303
}

304
static void migrate_fd_cancel(MigrationState *s)
305
{
malc's avatar
malc committed
306
    DPRINTF("cancelling migration\n");
307

308
    migrate_finish_set_state(s, MIG_STATE_CANCELLED);
309
310
}

311
int migrate_fd_close(MigrationState *s)
312
{
313
    int rc = 0;
314
315
316
    if (s->migration_file != NULL) {
        rc = qemu_fclose(s->migration_file);
        s->migration_file = NULL;
317
318
    }
    return rc;
319
}
320
321
322
323
324
325
326
327

void add_migration_state_change_notifier(Notifier *notify)
{
    notifier_list_add(&migration_state_notifiers, notify);
}

void remove_migration_state_change_notifier(Notifier *notify)
{
Paolo Bonzini's avatar
Paolo Bonzini committed
328
    notifier_remove(notify);
329
330
}

331
332
333
334
335
bool migration_is_active(MigrationState *s)
{
    return s->state == MIG_STATE_ACTIVE;
}

336
bool migration_has_finished(MigrationState *s)
337
{
338
    return s->state == MIG_STATE_COMPLETED;
339
}
340

341
342
343
344
345
346
bool migration_has_failed(MigrationState *s)
{
    return (s->state == MIG_STATE_CANCELLED ||
            s->state == MIG_STATE_ERROR);
}

Isaku Yamahata's avatar
Isaku Yamahata committed
347
static MigrationState *migrate_init(const MigrationParams *params)
348
{
349
    MigrationState *s = migrate_get_current();
350
    int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wasserman's avatar
Orit Wasserman committed
351
    bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
352
    int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Orit Wasserman's avatar
Orit Wasserman committed
353
354
355

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

357
    memset(s, 0, sizeof(*s));
358
    s->bandwidth_limit = bandwidth_limit;
Isaku Yamahata's avatar
Isaku Yamahata committed
359
    s->params = *params;
Orit Wasserman's avatar
Orit Wasserman committed
360
361
    memcpy(s->enabled_capabilities, enabled_capabilities,
           sizeof(enabled_capabilities));
362
    s->xbzrle_cache_size = xbzrle_cache_size;
363

364
    s->bandwidth_limit = bandwidth_limit;
365
    s->state = MIG_STATE_SETUP;
366
    trace_migrate_set_state(MIG_STATE_SETUP);
367

368
    s->total_time = qemu_get_clock_ms(rt_clock);
369
370
    return s;
}
371

372
373
374
375
376
377
378
379
380
381
382
383
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
384
385
386
void qmp_migrate(const char *uri, bool has_blk, bool blk,
                 bool has_inc, bool inc, bool has_detach, bool detach,
                 Error **errp)
387
{
388
    Error *local_err = NULL;
389
    MigrationState *s = migrate_get_current();
Isaku Yamahata's avatar
Isaku Yamahata committed
390
    MigrationParams params;
391
392
    const char *p;

Isaku Yamahata's avatar
Isaku Yamahata committed
393
394
395
    params.blk = blk;
    params.shared = inc;

396
    if (s->state == MIG_STATE_ACTIVE) {
Luiz Capitulino's avatar
Luiz Capitulino committed
397
398
        error_set(errp, QERR_MIGRATION_ACTIVE);
        return;
399
400
    }

Luiz Capitulino's avatar
Luiz Capitulino committed
401
402
    if (qemu_savevm_state_blocked(errp)) {
        return;
403
404
    }

405
    if (migration_blockers) {
Luiz Capitulino's avatar
Luiz Capitulino committed
406
407
        *errp = error_copy(migration_blockers->data);
        return;
408
409
    }

Isaku Yamahata's avatar
Isaku Yamahata committed
410
    s = migrate_init(&params);
411
412

    if (strstart(uri, "tcp:", &p)) {
413
        tcp_start_outgoing_migration(s, p, &local_err);
414
415
#if !defined(WIN32)
    } else if (strstart(uri, "exec:", &p)) {
416
        exec_start_outgoing_migration(s, p, &local_err);
417
    } else if (strstart(uri, "unix:", &p)) {
418
        unix_start_outgoing_migration(s, p, &local_err);
419
    } else if (strstart(uri, "fd:", &p)) {
420
        fd_start_outgoing_migration(s, p, &local_err);
421
#endif
422
    } else {
Luiz Capitulino's avatar
Luiz Capitulino committed
423
424
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
        return;
425
426
    }

427
    if (local_err) {
428
        migrate_fd_error(s);
429
        error_propagate(errp, local_err);
Luiz Capitulino's avatar
Luiz Capitulino committed
430
        return;
431
    }
432
433
}

Luiz Capitulino's avatar
Luiz Capitulino committed
434
void qmp_migrate_cancel(Error **errp)
435
{
436
    migrate_fd_cancel(migrate_get_current());
437
438
}

439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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();
}

458
void qmp_migrate_set_speed(int64_t value, Error **errp)
459
460
461
{
    MigrationState *s;

462
463
    if (value < 0) {
        value = 0;
464
    }
465
466
467
    if (value > SIZE_MAX) {
        value = SIZE_MAX;
    }
468

469
    s = migrate_get_current();
470
    s->bandwidth_limit = value;
471
472
473
    if (s->file) {
        qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
    }
474
475
}

476
void qmp_migrate_set_downtime(double value, Error **errp)
477
{
478
479
480
    value *= 1e9;
    value = MAX(0, MIN(UINT64_MAX, value));
    max_downtime = (uint64_t)value;
481
}
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

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;
}
500
501
502

/* migration thread support */

Juan Quintela's avatar
Juan Quintela committed
503
static int migration_put_buffer(void *opaque, const uint8_t *buf,
504
505
                               int64_t pos, int size)
{
506
    MigrationState *s = opaque;
507
    int ret;
508
509
510
511
512
513
514

    DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);

    if (size <= 0) {
        return size;
    }

515
516
517
518
    qemu_put_buffer(s->migration_file, buf, size);
    ret = qemu_file_get_error(s->migration_file);
    if (ret) {
        return ret;
519
520
    }

521
522
    s->bytes_xfer += size;
    return size;
523
524
}

Juan Quintela's avatar
Juan Quintela committed
525
static int migration_close(void *opaque)
526
{
527
    MigrationState *s = opaque;
528
529
530

    DPRINTF("closing\n");

531
532
533
534
535
    qemu_mutex_unlock_iothread();
    qemu_thread_join(&s->thread);
    qemu_mutex_lock_iothread();
    assert(s->state != MIG_STATE_ACTIVE);

536
    return migrate_fd_close(s);
537
538
}

Juan Quintela's avatar
Juan Quintela committed
539
static int migration_get_fd(void *opaque)
540
{
541
    MigrationState *s = opaque;
542

543
    return qemu_get_fd(s->migration_file);
544
545
546
547
548
549
550
551
}

/*
 * The meaning of the return values is:
 *   0: We can continue sending
 *   1: Time to stop
 *   negative: There has been an error
 */
Juan Quintela's avatar
Juan Quintela committed
552
static int migration_rate_limit(void *opaque)
553
{
554
    MigrationState *s = opaque;
555
556
557
558
559
560
561
    int ret;

    ret = qemu_file_get_error(s->file);
    if (ret) {
        return ret;
    }

562
    if (s->bytes_xfer >= s->xfer_limit) {
563
564
565
566
567
568
        return 1;
    }

    return 0;
}

Juan Quintela's avatar
Juan Quintela committed
569
static int64_t migration_set_rate_limit(void *opaque, int64_t new_rate)
570
{
571
    MigrationState *s = opaque;
572
573
574
575
    if (qemu_file_get_error(s->file)) {
        goto out;
    }

576
    s->xfer_limit = new_rate;
577
578
579
580
581

out:
    return s->xfer_limit;
}

Juan Quintela's avatar
Juan Quintela committed
582
static int64_t migration_get_rate_limit(void *opaque)
583
{
584
    MigrationState *s = opaque;
585
586
587
588

    return s->xfer_limit;
}

Juan Quintela's avatar
Juan Quintela committed
589
static void *migration_thread(void *opaque)
590
{
591
    MigrationState *s = opaque;
592
    int64_t initial_time = qemu_get_clock_ms(rt_clock);
593
    int64_t sleep_time = 0;
594
    int64_t initial_bytes = 0;
595
    int64_t max_size = 0;
596
597
    int64_t start_time = initial_time;
    bool old_vm_running = false;
598
599

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

602
    while (s->state == MIG_STATE_ACTIVE) {
603
        int64_t current_time;
604
        uint64_t pending_size;
605

606
        if (!qemu_file_rate_limit(s->file)) {
607
608
609
            DPRINTF("iterate\n");
            pending_size = qemu_savevm_state_pending(s->file, max_size);
            DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
610
            if (pending_size && pending_size >= max_size) {
611
                qemu_savevm_state_iterate(s->file);
612
613
            } else {
                DPRINTF("done iterating\n");
614
                qemu_mutex_lock_iothread();
615
616
                start_time = qemu_get_clock_ms(rt_clock);
                qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
617
                old_vm_running = runstate_is_running();
618
                vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
619
                qemu_file_set_rate_limit(s->file, INT_MAX);
620
                qemu_savevm_state_complete(s->file);
621
                qemu_mutex_unlock_iothread();
622
623
624
625
                if (!qemu_file_get_error(s->file)) {
                    migrate_finish_set_state(s, MIG_STATE_COMPLETED);
                    break;
                }
626
627
            }
        }
628

629
630
631
632
        if (qemu_file_get_error(s->file)) {
            migrate_finish_set_state(s, MIG_STATE_ERROR);
            break;
        }
633
        current_time = qemu_get_clock_ms(rt_clock);
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 - sleep_time;
637
638
639
640
641
642
            double bandwidth = transferred_bytes / time_spent;
            max_size = bandwidth * migrate_max_downtime() / 1000000;

            DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
                    " bandwidth %g max_size %" PRId64 "\n",
                    transferred_bytes, time_spent, bandwidth, max_size);
643
644
645
646
647
            /* 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;
            }
648
649

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

661
    qemu_mutex_lock_iothread();
662
663
664
665
666
667
668
669
    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();
670
        }
671
    }
672
    qemu_bh_schedule(s->cleanup_bh);
673
    qemu_mutex_unlock_iothread();
674

675
676
677
    return NULL;
}

Juan Quintela's avatar
Juan Quintela committed
678
679
680
681
682
683
684
static const QEMUFileOps migration_file_ops = {
    .get_fd =         migration_get_fd,
    .put_buffer =     migration_put_buffer,
    .close =          migration_close,
    .rate_limit =     migration_rate_limit,
    .get_rate_limit = migration_get_rate_limit,
    .set_rate_limit = migration_set_rate_limit,
685
686
};

687
void migrate_fd_connect(MigrationState *s)
688
{
689
    s->state = MIG_STATE_ACTIVE;
690
691
    trace_migrate_set_state(MIG_STATE_ACTIVE);

692
    s->bytes_xfer = 0;
693
694
    /* This is a best 1st approximation. ns to ms */
    s->expected_downtime = max_downtime/1000000;
695

696
    s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela's avatar
Juan Quintela committed
697
    s->file = qemu_fopen_ops(s, &migration_file_ops);
698

699
700
701
    qemu_file_set_rate_limit(s->file,
                             s->bandwidth_limit / XFER_LIMIT_RATIO);

Juan Quintela's avatar
Juan Quintela committed
702
    qemu_thread_create(&s->thread, migration_thread, s,
703
                       QEMU_THREAD_JOINABLE);
704
    notifier_list_notify(&migration_state_notifiers, s);
705
}