qemu/migration.c
<<
>>
Prefs
   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.h"
  18#include "monitor.h"
  19#include "buffered_file.h"
  20#include "sysemu.h"
  21#include "block.h"
  22#include "qemu_socket.h"
  23#include "block-migration.h"
  24#include "qmp-commands.h"
  25
  26//#define DEBUG_MIGRATION
  27
  28#ifdef DEBUG_MIGRATION
  29#define DPRINTF(fmt, ...) \
  30    do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
  31#else
  32#define DPRINTF(fmt, ...) \
  33    do { } while (0)
  34#endif
  35
  36enum {
  37    MIG_STATE_ERROR,
  38    MIG_STATE_SETUP,
  39    MIG_STATE_CANCELLED,
  40    MIG_STATE_ACTIVE,
  41    MIG_STATE_COMPLETED,
  42};
  43
  44#define MAX_THROTTLE  (32 << 20)      /* Migration speed throttling */
  45
  46/* Migration XBZRLE default cache size */
  47#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
  48
  49static NotifierList migration_state_notifiers =
  50    NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
  51
  52/* When we add fault tolerance, we could have several
  53   migrations at once.  For now we don't need to add
  54   dynamic creation of migration */
  55
  56MigrationState *migrate_get_current(void)
  57{
  58    static MigrationState current_migration = {
  59        .state = MIG_STATE_SETUP,
  60        .bandwidth_limit = MAX_THROTTLE,
  61        .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
  62    };
  63
  64    return &current_migration;
  65}
  66
  67void qemu_start_incoming_migration(const char *uri, Error **errp)
  68{
  69    const char *p;
  70
  71    if (strstart(uri, "tcp:", &p))
  72        tcp_start_incoming_migration(p, errp);
  73#if !defined(WIN32)
  74    else if (strstart(uri, "exec:", &p))
  75        exec_start_incoming_migration(p, errp);
  76    else if (strstart(uri, "unix:", &p))
  77        unix_start_incoming_migration(p, errp);
  78    else if (strstart(uri, "fd:", &p))
  79        fd_start_incoming_migration(p, errp);
  80#endif
  81    else {
  82        error_setg(errp, "unknown migration protocol: %s\n", uri);
  83    }
  84}
  85
  86static void process_incoming_migration_co(void *opaque)
  87{
  88    QEMUFile *f = opaque;
  89    int ret;
  90
  91    ret = qemu_loadvm_state(f);
  92    qemu_set_fd_handler(qemu_get_fd(f), NULL, NULL, NULL);
  93    qemu_fclose(f);
  94    if (ret < 0) {
  95        fprintf(stderr, "load of migration failed\n");
  96        exit(0);
  97    }
  98    qemu_announce_self();
  99    DPRINTF("successfully loaded vm state\n");
 100
 101    bdrv_clear_incoming_migration_all();
 102    /* Make sure all file formats flush their mutable metadata */
 103    bdrv_invalidate_cache_all();
 104
 105    if (autostart) {
 106        vm_start();
 107    } else {
 108        runstate_set(RUN_STATE_PAUSED);
 109    }
 110}
 111
 112static void enter_migration_coroutine(void *opaque)
 113{
 114    Coroutine *co = opaque;
 115    qemu_coroutine_enter(co, NULL);
 116}
 117
 118void process_incoming_migration(QEMUFile *f)
 119{
 120    Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
 121    int fd = qemu_get_fd(f);
 122
 123    assert(fd != -1);
 124    socket_set_nonblock(fd);
 125    qemu_set_fd_handler(fd, enter_migration_coroutine, NULL, co);
 126    qemu_coroutine_enter(co, f);
 127}
 128
 129/* amount of nanoseconds we are willing to wait for migration to be down.
 130 * the choice of nanoseconds is because it is the maximum resolution that
 131 * get_clock() can achieve. It is an internal measure. All user-visible
 132 * units must be in seconds */
 133static uint64_t max_downtime = 30000000;
 134
 135uint64_t migrate_max_downtime(void)
 136{
 137    return max_downtime;
 138}
 139
 140MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
 141{
 142    MigrationCapabilityStatusList *head = NULL;
 143    MigrationCapabilityStatusList *caps;
 144    MigrationState *s = migrate_get_current();
 145    int i;
 146
 147    for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
 148        if (head == NULL) {
 149            head = g_malloc0(sizeof(*caps));
 150            caps = head;
 151        } else {
 152            caps->next = g_malloc0(sizeof(*caps));
 153            caps = caps->next;
 154        }
 155        caps->value =
 156            g_malloc(sizeof(*caps->value));
 157        caps->value->capability = i;
 158        caps->value->state = s->enabled_capabilities[i];
 159    }
 160
 161    return head;
 162}
 163
 164static void get_xbzrle_cache_stats(MigrationInfo *info)
 165{
 166    if (migrate_use_xbzrle()) {
 167        info->has_xbzrle_cache = true;
 168        info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
 169        info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
 170        info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
 171        info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
 172        info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
 173        info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
 174    }
 175}
 176
 177MigrationInfo *qmp_query_migrate(Error **errp)
 178{
 179    MigrationInfo *info = g_malloc0(sizeof(*info));
 180    MigrationState *s = migrate_get_current();
 181
 182    switch (s->state) {
 183    case MIG_STATE_SETUP:
 184        /* no migration has happened ever */
 185        break;
 186    case MIG_STATE_ACTIVE:
 187        info->has_status = true;
 188        info->status = g_strdup("active");
 189        info->has_total_time = true;
 190        info->total_time = qemu_get_clock_ms(rt_clock)
 191            - s->total_time;
 192        info->has_expected_downtime = true;
 193        info->expected_downtime = s->expected_downtime;
 194
 195        info->has_ram = true;
 196        info->ram = g_malloc0(sizeof(*info->ram));
 197        info->ram->transferred = ram_bytes_transferred();
 198        info->ram->remaining = ram_bytes_remaining();
 199        info->ram->total = ram_bytes_total();
 200        info->ram->duplicate = dup_mig_pages_transferred();
 201        info->ram->normal = norm_mig_pages_transferred();
 202        info->ram->normal_bytes = norm_mig_bytes_transferred();
 203        info->ram->dirty_pages_rate = s->dirty_pages_rate;
 204
 205
 206        if (blk_mig_active()) {
 207            info->has_disk = true;
 208            info->disk = g_malloc0(sizeof(*info->disk));
 209            info->disk->transferred = blk_mig_bytes_transferred();
 210            info->disk->remaining = blk_mig_bytes_remaining();
 211            info->disk->total = blk_mig_bytes_total();
 212        }
 213
 214        get_xbzrle_cache_stats(info);
 215        break;
 216    case MIG_STATE_COMPLETED:
 217        get_xbzrle_cache_stats(info);
 218
 219        info->has_status = true;
 220        info->status = g_strdup("completed");
 221        info->total_time = s->total_time;
 222        info->has_downtime = true;
 223        info->downtime = s->downtime;
 224
 225        info->has_ram = true;
 226        info->ram = g_malloc0(sizeof(*info->ram));
 227        info->ram->transferred = ram_bytes_transferred();
 228        info->ram->remaining = 0;
 229        info->ram->total = ram_bytes_total();
 230        info->ram->duplicate = dup_mig_pages_transferred();
 231        info->ram->normal = norm_mig_pages_transferred();
 232        info->ram->normal_bytes = norm_mig_bytes_transferred();
 233        break;
 234    case MIG_STATE_ERROR:
 235        info->has_status = true;
 236        info->status = g_strdup("failed");
 237        break;
 238    case MIG_STATE_CANCELLED:
 239        info->has_status = true;
 240        info->status = g_strdup("cancelled");
 241        break;
 242    }
 243
 244    return info;
 245}
 246
 247void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
 248                                  Error **errp)
 249{
 250    MigrationState *s = migrate_get_current();
 251    MigrationCapabilityStatusList *cap;
 252
 253    if (s->state == MIG_STATE_ACTIVE) {
 254        error_set(errp, QERR_MIGRATION_ACTIVE);
 255        return;
 256    }
 257
 258    for (cap = params; cap; cap = cap->next) {
 259        s->enabled_capabilities[cap->value->capability] = cap->value->state;
 260    }
 261}
 262
 263/* shared migration helpers */
 264
 265static int migrate_fd_cleanup(MigrationState *s)
 266{
 267    int ret = 0;
 268
 269    if (s->file) {
 270        DPRINTF("closing file\n");
 271        ret = qemu_fclose(s->file);
 272        s->file = NULL;
 273    }
 274
 275    migrate_fd_close(s);
 276    return ret;
 277}
 278
 279void migrate_fd_error(MigrationState *s)
 280{
 281    DPRINTF("setting error state\n");
 282    s->state = MIG_STATE_ERROR;
 283    notifier_list_notify(&migration_state_notifiers, s);
 284    migrate_fd_cleanup(s);
 285}
 286
 287static void migrate_fd_completed(MigrationState *s)
 288{
 289    DPRINTF("setting completed state\n");
 290    if (migrate_fd_cleanup(s) < 0) {
 291        s->state = MIG_STATE_ERROR;
 292    } else {
 293        s->state = MIG_STATE_COMPLETED;
 294        runstate_set(RUN_STATE_POSTMIGRATE);
 295    }
 296    notifier_list_notify(&migration_state_notifiers, s);
 297}
 298
 299static void migrate_fd_put_notify(void *opaque)
 300{
 301    MigrationState *s = opaque;
 302    int ret;
 303
 304    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
 305    ret = qemu_file_put_notify(s->file);
 306    if (ret) {
 307        migrate_fd_error(s);
 308    }
 309}
 310
 311ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
 312                              size_t size)
 313{
 314    ssize_t ret;
 315
 316    if (s->state != MIG_STATE_ACTIVE) {
 317        return -EIO;
 318    }
 319
 320    do {
 321        ret = s->write(s, data, size);
 322    } while (ret == -1 && ((s->get_error(s)) == EINTR));
 323
 324    if (ret == -1)
 325        ret = -(s->get_error(s));
 326
 327    if (ret == -EAGAIN) {
 328        qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
 329    }
 330
 331    return ret;
 332}
 333
 334void migrate_fd_put_ready(MigrationState *s)
 335{
 336    int ret;
 337
 338    if (s->state != MIG_STATE_ACTIVE) {
 339        DPRINTF("put_ready returning because of non-active state\n");
 340        return;
 341    }
 342
 343    DPRINTF("iterate\n");
 344    ret = qemu_savevm_state_iterate(s->file);
 345    if (ret < 0) {
 346        migrate_fd_error(s);
 347    } else if (ret == 1) {
 348        int old_vm_running = runstate_is_running();
 349        int64_t start_time, end_time;
 350
 351        DPRINTF("done iterating\n");
 352        start_time = qemu_get_clock_ms(rt_clock);
 353        qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
 354        vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
 355
 356        if (qemu_savevm_state_complete(s->file) < 0) {
 357            migrate_fd_error(s);
 358        } else {
 359            migrate_fd_completed(s);
 360        }
 361        end_time = qemu_get_clock_ms(rt_clock);
 362        s->total_time = end_time - s->total_time;
 363        s->downtime = end_time - start_time;
 364        if (s->state != MIG_STATE_COMPLETED) {
 365            if (old_vm_running) {
 366                vm_start();
 367            }
 368        }
 369    }
 370}
 371
 372static void migrate_fd_cancel(MigrationState *s)
 373{
 374    if (s->state != MIG_STATE_ACTIVE)
 375        return;
 376
 377    DPRINTF("cancelling migration\n");
 378
 379    s->state = MIG_STATE_CANCELLED;
 380    notifier_list_notify(&migration_state_notifiers, s);
 381    qemu_savevm_state_cancel(s->file);
 382
 383    migrate_fd_cleanup(s);
 384}
 385
 386int migrate_fd_wait_for_unfreeze(MigrationState *s)
 387{
 388    int ret;
 389
 390    DPRINTF("wait for unfreeze\n");
 391    if (s->state != MIG_STATE_ACTIVE)
 392        return -EINVAL;
 393
 394    do {
 395        fd_set wfds;
 396
 397        FD_ZERO(&wfds);
 398        FD_SET(s->fd, &wfds);
 399
 400        ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
 401    } while (ret == -1 && (s->get_error(s)) == EINTR);
 402
 403    if (ret == -1) {
 404        return -s->get_error(s);
 405    }
 406    return 0;
 407}
 408
 409int migrate_fd_close(MigrationState *s)
 410{
 411    int rc = 0;
 412    if (s->fd != -1) {
 413        qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
 414        rc = s->close(s);
 415        s->fd = -1;
 416    }
 417    return rc;
 418}
 419
 420void add_migration_state_change_notifier(Notifier *notify)
 421{
 422    notifier_list_add(&migration_state_notifiers, notify);
 423}
 424
 425void remove_migration_state_change_notifier(Notifier *notify)
 426{
 427    notifier_remove(notify);
 428}
 429
 430bool migration_is_active(MigrationState *s)
 431{
 432    return s->state == MIG_STATE_ACTIVE;
 433}
 434
 435bool migration_has_finished(MigrationState *s)
 436{
 437    return s->state == MIG_STATE_COMPLETED;
 438}
 439
 440bool migration_has_failed(MigrationState *s)
 441{
 442    return (s->state == MIG_STATE_CANCELLED ||
 443            s->state == MIG_STATE_ERROR);
 444}
 445
 446void migrate_fd_connect(MigrationState *s)
 447{
 448    int ret;
 449
 450    s->state = MIG_STATE_ACTIVE;
 451    s->file = qemu_fopen_ops_buffered(s);
 452
 453    DPRINTF("beginning savevm\n");
 454    ret = qemu_savevm_state_begin(s->file, &s->params);
 455    if (ret < 0) {
 456        DPRINTF("failed, %d\n", ret);
 457        migrate_fd_error(s);
 458        return;
 459    }
 460    migrate_fd_put_ready(s);
 461}
 462
 463static MigrationState *migrate_init(const MigrationParams *params)
 464{
 465    MigrationState *s = migrate_get_current();
 466    int64_t bandwidth_limit = s->bandwidth_limit;
 467    bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
 468    int64_t xbzrle_cache_size = s->xbzrle_cache_size;
 469
 470    memcpy(enabled_capabilities, s->enabled_capabilities,
 471           sizeof(enabled_capabilities));
 472
 473    memset(s, 0, sizeof(*s));
 474    s->bandwidth_limit = bandwidth_limit;
 475    s->params = *params;
 476    memcpy(s->enabled_capabilities, enabled_capabilities,
 477           sizeof(enabled_capabilities));
 478    s->xbzrle_cache_size = xbzrle_cache_size;
 479
 480    s->bandwidth_limit = bandwidth_limit;
 481    s->state = MIG_STATE_SETUP;
 482    s->total_time = qemu_get_clock_ms(rt_clock);
 483
 484    return s;
 485}
 486
 487static GSList *migration_blockers;
 488
 489void migrate_add_blocker(Error *reason)
 490{
 491    migration_blockers = g_slist_prepend(migration_blockers, reason);
 492}
 493
 494void migrate_del_blocker(Error *reason)
 495{
 496    migration_blockers = g_slist_remove(migration_blockers, reason);
 497}
 498
 499void qmp_migrate(const char *uri, bool has_blk, bool blk,
 500                 bool has_inc, bool inc, bool has_detach, bool detach,
 501                 Error **errp)
 502{
 503    Error *local_err = NULL;
 504    MigrationState *s = migrate_get_current();
 505    MigrationParams params;
 506    const char *p;
 507
 508    params.blk = blk;
 509    params.shared = inc;
 510
 511    if (s->state == MIG_STATE_ACTIVE) {
 512        error_set(errp, QERR_MIGRATION_ACTIVE);
 513        return;
 514    }
 515
 516    if (qemu_savevm_state_blocked(errp)) {
 517        return;
 518    }
 519
 520    if (migration_blockers) {
 521        *errp = error_copy(migration_blockers->data);
 522        return;
 523    }
 524
 525    s = migrate_init(&params);
 526
 527    if (strstart(uri, "tcp:", &p)) {
 528        tcp_start_outgoing_migration(s, p, &local_err);
 529#if !defined(WIN32)
 530    } else if (strstart(uri, "exec:", &p)) {
 531        exec_start_outgoing_migration(s, p, &local_err);
 532    } else if (strstart(uri, "unix:", &p)) {
 533        unix_start_outgoing_migration(s, p, &local_err);
 534    } else if (strstart(uri, "fd:", &p)) {
 535        fd_start_outgoing_migration(s, p, &local_err);
 536#endif
 537    } else {
 538        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
 539        return;
 540    }
 541
 542    if (local_err) {
 543        migrate_fd_error(s);
 544        error_propagate(errp, local_err);
 545        return;
 546    }
 547
 548    notifier_list_notify(&migration_state_notifiers, s);
 549}
 550
 551void qmp_migrate_cancel(Error **errp)
 552{
 553    migrate_fd_cancel(migrate_get_current());
 554}
 555
 556void qmp_migrate_set_cache_size(int64_t value, Error **errp)
 557{
 558    MigrationState *s = migrate_get_current();
 559
 560    /* Check for truncation */
 561    if (value != (size_t)value) {
 562        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
 563                  "exceeding address space");
 564        return;
 565    }
 566
 567    s->xbzrle_cache_size = xbzrle_cache_resize(value);
 568}
 569
 570int64_t qmp_query_migrate_cache_size(Error **errp)
 571{
 572    return migrate_xbzrle_cache_size();
 573}
 574
 575void qmp_migrate_set_speed(int64_t value, Error **errp)
 576{
 577    MigrationState *s;
 578
 579    if (value < 0) {
 580        value = 0;
 581    }
 582
 583    s = migrate_get_current();
 584    s->bandwidth_limit = value;
 585    qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
 586}
 587
 588void qmp_migrate_set_downtime(double value, Error **errp)
 589{
 590    value *= 1e9;
 591    value = MAX(0, MIN(UINT64_MAX, value));
 592    max_downtime = (uint64_t)value;
 593}
 594
 595int migrate_use_xbzrle(void)
 596{
 597    MigrationState *s;
 598
 599    s = migrate_get_current();
 600
 601    return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
 602}
 603
 604int64_t migrate_xbzrle_cache_size(void)
 605{
 606    MigrationState *s;
 607
 608    s = migrate_get_current();
 609
 610    return s->xbzrle_cache_size;
 611}
 612