Statistics
| Branch: | Revision:

root / migration.c @ 2da776db

History | View | Annotate | Download (17.9 kB)

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
 *
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.
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"
25
#include "qmp-commands.h"
26
#include "trace.h"
27

    
28
//#define DEBUG_MIGRATION
29

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

    
38
enum {
39
    MIG_STATE_ERROR,
40
    MIG_STATE_SETUP,
41
    MIG_STATE_CANCELLED,
42
    MIG_STATE_ACTIVE,
43
    MIG_STATE_COMPLETED,
44
};
45

    
46
#define MAX_THROTTLE  (32 << 20)      /* Migration speed throttling */
47

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

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

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

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

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

    
72
    return &current_migration;
73
}
74

    
75
void qemu_start_incoming_migration(const char *uri, Error **errp)
76
{
77
    const char *p;
78

    
79
    if (strstart(uri, "tcp:", &p))
80
        tcp_start_incoming_migration(p, errp);
81
#ifdef CONFIG_RDMA
82
    else if (strstart(uri, "x-rdma:", &p))
83
        rdma_start_incoming_migration(p, errp);
84
#endif
85
#if !defined(WIN32)
86
    else if (strstart(uri, "exec:", &p))
87
        exec_start_incoming_migration(p, errp);
88
    else if (strstart(uri, "unix:", &p))
89
        unix_start_incoming_migration(p, errp);
90
    else if (strstart(uri, "fd:", &p))
91
        fd_start_incoming_migration(p, errp);
92
#endif
93
    else {
94
        error_setg(errp, "unknown migration protocol: %s", uri);
95
    }
96
}
97

    
98
static void process_incoming_migration_co(void *opaque)
99
{
100
    QEMUFile *f = opaque;
101
    int ret;
102

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

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

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

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

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

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

    
139
uint64_t migrate_max_downtime(void)
140
{
141
    return max_downtime;
142
}
143

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

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

    
165
    return head;
166
}
167

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

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

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

    
199
        info->has_ram = true;
200
        info->ram = g_malloc0(sizeof(*info->ram));
201
        info->ram->transferred = ram_bytes_transferred();
202
        info->ram->remaining = ram_bytes_remaining();
203
        info->ram->total = ram_bytes_total();
204
        info->ram->duplicate = dup_mig_pages_transferred();
205
        info->ram->skipped = skipped_mig_pages_transferred();
206
        info->ram->normal = norm_mig_pages_transferred();
207
        info->ram->normal_bytes = norm_mig_bytes_transferred();
208
        info->ram->dirty_pages_rate = s->dirty_pages_rate;
209
        info->ram->mbps = s->mbps;
210

    
211
        if (blk_mig_active()) {
212
            info->has_disk = true;
213
            info->disk = g_malloc0(sizeof(*info->disk));
214
            info->disk->transferred = blk_mig_bytes_transferred();
215
            info->disk->remaining = blk_mig_bytes_remaining();
216
            info->disk->total = blk_mig_bytes_total();
217
        }
218

    
219
        get_xbzrle_cache_stats(info);
220
        break;
221
    case MIG_STATE_COMPLETED:
222
        get_xbzrle_cache_stats(info);
223

    
224
        info->has_status = true;
225
        info->status = g_strdup("completed");
226
        info->total_time = s->total_time;
227
        info->has_downtime = true;
228
        info->downtime = s->downtime;
229

    
230
        info->has_ram = true;
231
        info->ram = g_malloc0(sizeof(*info->ram));
232
        info->ram->transferred = ram_bytes_transferred();
233
        info->ram->remaining = 0;
234
        info->ram->total = ram_bytes_total();
235
        info->ram->duplicate = dup_mig_pages_transferred();
236
        info->ram->skipped = skipped_mig_pages_transferred();
237
        info->ram->normal = norm_mig_pages_transferred();
238
        info->ram->normal_bytes = norm_mig_bytes_transferred();
239
        info->ram->mbps = s->mbps;
240
        break;
241
    case MIG_STATE_ERROR:
242
        info->has_status = true;
243
        info->status = g_strdup("failed");
244
        break;
245
    case MIG_STATE_CANCELLED:
246
        info->has_status = true;
247
        info->status = g_strdup("cancelled");
248
        break;
249
    }
250

    
251
    return info;
252
}
253

    
254
void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
255
                                  Error **errp)
256
{
257
    MigrationState *s = migrate_get_current();
258
    MigrationCapabilityStatusList *cap;
259

    
260
    if (s->state == MIG_STATE_ACTIVE) {
261
        error_set(errp, QERR_MIGRATION_ACTIVE);
262
        return;
263
    }
264

    
265
    for (cap = params; cap; cap = cap->next) {
266
        s->enabled_capabilities[cap->value->capability] = cap->value->state;
267
    }
268
}
269

    
270
/* shared migration helpers */
271

    
272
static void migrate_fd_cleanup(void *opaque)
273
{
274
    MigrationState *s = opaque;
275

    
276
    qemu_bh_delete(s->cleanup_bh);
277
    s->cleanup_bh = NULL;
278

    
279
    if (s->file) {
280
        DPRINTF("closing file\n");
281
        qemu_mutex_unlock_iothread();
282
        qemu_thread_join(&s->thread);
283
        qemu_mutex_lock_iothread();
284

    
285
        qemu_fclose(s->file);
286
        s->file = NULL;
287
    }
288

    
289
    assert(s->state != MIG_STATE_ACTIVE);
290

    
291
    if (s->state != MIG_STATE_COMPLETED) {
292
        qemu_savevm_state_cancel();
293
    }
294

    
295
    notifier_list_notify(&migration_state_notifiers, s);
296
}
297

    
298
static void migrate_finish_set_state(MigrationState *s, int new_state)
299
{
300
    if (atomic_cmpxchg(&s->state, MIG_STATE_ACTIVE, new_state) == new_state) {
301
        trace_migrate_set_state(new_state);
302
    }
303
}
304

    
305
void migrate_fd_error(MigrationState *s)
306
{
307
    DPRINTF("setting error state\n");
308
    assert(s->file == NULL);
309
    s->state = MIG_STATE_ERROR;
310
    trace_migrate_set_state(MIG_STATE_ERROR);
311
    notifier_list_notify(&migration_state_notifiers, s);
312
}
313

    
314
static void migrate_fd_cancel(MigrationState *s)
315
{
316
    DPRINTF("cancelling migration\n");
317

    
318
    migrate_finish_set_state(s, MIG_STATE_CANCELLED);
319
}
320

    
321
void add_migration_state_change_notifier(Notifier *notify)
322
{
323
    notifier_list_add(&migration_state_notifiers, notify);
324
}
325

    
326
void remove_migration_state_change_notifier(Notifier *notify)
327
{
328
    notifier_remove(notify);
329
}
330

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

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

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

    
347
static MigrationState *migrate_init(const MigrationParams *params)
348
{
349
    MigrationState *s = migrate_get_current();
350
    int64_t bandwidth_limit = s->bandwidth_limit;
351
    bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
352
    int64_t xbzrle_cache_size = s->xbzrle_cache_size;
353

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

    
357
    memset(s, 0, sizeof(*s));
358
    s->params = *params;
359
    memcpy(s->enabled_capabilities, enabled_capabilities,
360
           sizeof(enabled_capabilities));
361
    s->xbzrle_cache_size = xbzrle_cache_size;
362

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

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

    
371
static GSList *migration_blockers;
372

    
373
void migrate_add_blocker(Error *reason)
374
{
375
    migration_blockers = g_slist_prepend(migration_blockers, reason);
376
}
377

    
378
void migrate_del_blocker(Error *reason)
379
{
380
    migration_blockers = g_slist_remove(migration_blockers, reason);
381
}
382

    
383
void qmp_migrate(const char *uri, bool has_blk, bool blk,
384
                 bool has_inc, bool inc, bool has_detach, bool detach,
385
                 Error **errp)
386
{
387
    Error *local_err = NULL;
388
    MigrationState *s = migrate_get_current();
389
    MigrationParams params;
390
    const char *p;
391

    
392
    params.blk = blk;
393
    params.shared = inc;
394

    
395
    if (s->state == MIG_STATE_ACTIVE) {
396
        error_set(errp, QERR_MIGRATION_ACTIVE);
397
        return;
398
    }
399

    
400
    if (qemu_savevm_state_blocked(errp)) {
401
        return;
402
    }
403

    
404
    if (migration_blockers) {
405
        *errp = error_copy(migration_blockers->data);
406
        return;
407
    }
408

    
409
    s = migrate_init(&params);
410

    
411
    if (strstart(uri, "tcp:", &p)) {
412
        tcp_start_outgoing_migration(s, p, &local_err);
413
#ifdef CONFIG_RDMA
414
    } else if (strstart(uri, "x-rdma:", &p)) {
415
        rdma_start_outgoing_migration(s, p, &local_err);
416
#endif
417
#if !defined(WIN32)
418
    } else if (strstart(uri, "exec:", &p)) {
419
        exec_start_outgoing_migration(s, p, &local_err);
420
    } else if (strstart(uri, "unix:", &p)) {
421
        unix_start_outgoing_migration(s, p, &local_err);
422
    } else if (strstart(uri, "fd:", &p)) {
423
        fd_start_outgoing_migration(s, p, &local_err);
424
#endif
425
    } else {
426
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
427
        return;
428
    }
429

    
430
    if (local_err) {
431
        migrate_fd_error(s);
432
        error_propagate(errp, local_err);
433
        return;
434
    }
435
}
436

    
437
void qmp_migrate_cancel(Error **errp)
438
{
439
    migrate_fd_cancel(migrate_get_current());
440
}
441

    
442
void qmp_migrate_set_cache_size(int64_t value, Error **errp)
443
{
444
    MigrationState *s = migrate_get_current();
445

    
446
    /* Check for truncation */
447
    if (value != (size_t)value) {
448
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
449
                  "exceeding address space");
450
        return;
451
    }
452

    
453
    s->xbzrle_cache_size = xbzrle_cache_resize(value);
454
}
455

    
456
int64_t qmp_query_migrate_cache_size(Error **errp)
457
{
458
    return migrate_xbzrle_cache_size();
459
}
460

    
461
void qmp_migrate_set_speed(int64_t value, Error **errp)
462
{
463
    MigrationState *s;
464

    
465
    if (value < 0) {
466
        value = 0;
467
    }
468
    if (value > SIZE_MAX) {
469
        value = SIZE_MAX;
470
    }
471

    
472
    s = migrate_get_current();
473
    s->bandwidth_limit = value;
474
    if (s->file) {
475
        qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
476
    }
477
}
478

    
479
void qmp_migrate_set_downtime(double value, Error **errp)
480
{
481
    value *= 1e9;
482
    value = MAX(0, MIN(UINT64_MAX, value));
483
    max_downtime = (uint64_t)value;
484
}
485

    
486
bool migrate_rdma_pin_all(void)
487
{
488
    MigrationState *s;
489

    
490
    s = migrate_get_current();
491

    
492
    return s->enabled_capabilities[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL];
493
}
494

    
495
bool migrate_auto_converge(void)
496
{
497
    MigrationState *s;
498

    
499
    s = migrate_get_current();
500

    
501
    return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
502
}
503

    
504
bool migrate_zero_blocks(void)
505
{
506
    MigrationState *s;
507

    
508
    s = migrate_get_current();
509

    
510
    return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
511
}
512

    
513
int migrate_use_xbzrle(void)
514
{
515
    MigrationState *s;
516

    
517
    s = migrate_get_current();
518

    
519
    return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
520
}
521

    
522
int64_t migrate_xbzrle_cache_size(void)
523
{
524
    MigrationState *s;
525

    
526
    s = migrate_get_current();
527

    
528
    return s->xbzrle_cache_size;
529
}
530

    
531
/* migration thread support */
532

    
533
static void *migration_thread(void *opaque)
534
{
535
    MigrationState *s = opaque;
536
    int64_t initial_time = qemu_get_clock_ms(rt_clock);
537
    int64_t initial_bytes = 0;
538
    int64_t max_size = 0;
539
    int64_t start_time = initial_time;
540
    bool old_vm_running = false;
541

    
542
    DPRINTF("beginning savevm\n");
543
    qemu_savevm_state_begin(s->file, &s->params);
544

    
545
    while (s->state == MIG_STATE_ACTIVE) {
546
        int64_t current_time;
547
        uint64_t pending_size;
548

    
549
        if (!qemu_file_rate_limit(s->file)) {
550
            DPRINTF("iterate\n");
551
            pending_size = qemu_savevm_state_pending(s->file, max_size);
552
            DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
553
            if (pending_size && pending_size >= max_size) {
554
                qemu_savevm_state_iterate(s->file);
555
            } else {
556
                int ret;
557

    
558
                DPRINTF("done iterating\n");
559
                qemu_mutex_lock_iothread();
560
                start_time = qemu_get_clock_ms(rt_clock);
561
                qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
562
                old_vm_running = runstate_is_running();
563

    
564
                ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
565
                if (ret >= 0) {
566
                    qemu_file_set_rate_limit(s->file, INT_MAX);
567
                    qemu_savevm_state_complete(s->file);
568
                }
569
                qemu_mutex_unlock_iothread();
570

    
571
                if (ret < 0) {
572
                    migrate_finish_set_state(s, MIG_STATE_ERROR);
573
                    break;
574
                }
575

    
576
                if (!qemu_file_get_error(s->file)) {
577
                    migrate_finish_set_state(s, MIG_STATE_COMPLETED);
578
                    break;
579
                }
580
            }
581
        }
582

    
583
        if (qemu_file_get_error(s->file)) {
584
            migrate_finish_set_state(s, MIG_STATE_ERROR);
585
            break;
586
        }
587
        current_time = qemu_get_clock_ms(rt_clock);
588
        if (current_time >= initial_time + BUFFER_DELAY) {
589
            uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
590
            uint64_t time_spent = current_time - initial_time;
591
            double bandwidth = transferred_bytes / time_spent;
592
            max_size = bandwidth * migrate_max_downtime() / 1000000;
593

    
594
            s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
595
                    ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
596

    
597
            DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
598
                    " bandwidth %g max_size %" PRId64 "\n",
599
                    transferred_bytes, time_spent, bandwidth, max_size);
600
            /* if we haven't sent anything, we don't want to recalculate
601
               10000 is a small enough number for our purposes */
602
            if (s->dirty_bytes_rate && transferred_bytes > 10000) {
603
                s->expected_downtime = s->dirty_bytes_rate / bandwidth;
604
            }
605

    
606
            qemu_file_reset_rate_limit(s->file);
607
            initial_time = current_time;
608
            initial_bytes = qemu_ftell(s->file);
609
        }
610
        if (qemu_file_rate_limit(s->file)) {
611
            /* usleep expects microseconds */
612
            g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
613
        }
614
    }
615

    
616
    qemu_mutex_lock_iothread();
617
    if (s->state == MIG_STATE_COMPLETED) {
618
        int64_t end_time = qemu_get_clock_ms(rt_clock);
619
        s->total_time = end_time - s->total_time;
620
        s->downtime = end_time - start_time;
621
        runstate_set(RUN_STATE_POSTMIGRATE);
622
    } else {
623
        if (old_vm_running) {
624
            vm_start();
625
        }
626
    }
627
    qemu_bh_schedule(s->cleanup_bh);
628
    qemu_mutex_unlock_iothread();
629

    
630
    return NULL;
631
}
632

    
633
void migrate_fd_connect(MigrationState *s)
634
{
635
    s->state = MIG_STATE_ACTIVE;
636
    trace_migrate_set_state(MIG_STATE_ACTIVE);
637

    
638
    /* This is a best 1st approximation. ns to ms */
639
    s->expected_downtime = max_downtime/1000000;
640
    s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
641

    
642
    qemu_file_set_rate_limit(s->file,
643
                             s->bandwidth_limit / XFER_LIMIT_RATIO);
644

    
645
    qemu_thread_create(&s->thread, migration_thread, s,
646
                       QEMU_THREAD_JOINABLE);
647
    notifier_list_notify(&migration_state_notifiers, s);
648
}