File:  [Qemu by Fabrice Bellard] / qemu / migration.c
Revision 1.1.1.9 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 19:17:55 2018 UTC (3 years, 1 month ago) by root
Branches: qemu, MAIN
CVS tags: qemu1001, HEAD
qemu 1.0.1

    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:  */
   13: 
   14: #include "qemu-common.h"
   15: #include "migration.h"
   16: #include "monitor.h"
   17: #include "buffered_file.h"
   18: #include "sysemu.h"
   19: #include "block.h"
   20: #include "qemu_socket.h"
   21: #include "block-migration.h"
   22: #include "qmp-commands.h"
   23: 
   24: //#define DEBUG_MIGRATION
   25: 
   26: #ifdef DEBUG_MIGRATION
   27: #define DPRINTF(fmt, ...) \
   28:     do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
   29: #else
   30: #define DPRINTF(fmt, ...) \
   31:     do { } while (0)
   32: #endif
   33: 
   34: enum {
   35:     MIG_STATE_ERROR,
   36:     MIG_STATE_SETUP,
   37:     MIG_STATE_CANCELLED,
   38:     MIG_STATE_ACTIVE,
   39:     MIG_STATE_COMPLETED,
   40: };
   41: 
   42: #define MAX_THROTTLE  (32 << 20)      /* Migration speed throttling */
   43: 
   44: static NotifierList migration_state_notifiers =
   45:     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
   46: 
   47: /* When we add fault tolerance, we could have several
   48:    migrations at once.  For now we don't need to add
   49:    dynamic creation of migration */
   50: 
   51: static MigrationState *migrate_get_current(void)
   52: {
   53:     static MigrationState current_migration = {
   54:         .state = MIG_STATE_SETUP,
   55:         .bandwidth_limit = MAX_THROTTLE,
   56:     };
   57: 
   58:     return &current_migration;
   59: }
   60: 
   61: int qemu_start_incoming_migration(const char *uri)
   62: {
   63:     const char *p;
   64:     int ret;
   65: 
   66:     if (strstart(uri, "tcp:", &p))
   67:         ret = tcp_start_incoming_migration(p);
   68: #if !defined(WIN32)
   69:     else if (strstart(uri, "exec:", &p))
   70:         ret =  exec_start_incoming_migration(p);
   71:     else if (strstart(uri, "unix:", &p))
   72:         ret = unix_start_incoming_migration(p);
   73:     else if (strstart(uri, "fd:", &p))
   74:         ret = fd_start_incoming_migration(p);
   75: #endif
   76:     else {
   77:         fprintf(stderr, "unknown migration protocol: %s\n", uri);
   78:         ret = -EPROTONOSUPPORT;
   79:     }
   80:     return ret;
   81: }
   82: 
   83: void process_incoming_migration(QEMUFile *f)
   84: {
   85:     if (qemu_loadvm_state(f) < 0) {
   86:         fprintf(stderr, "load of migration failed\n");
   87:         exit(0);
   88:     }
   89:     qemu_announce_self();
   90:     DPRINTF("successfully loaded vm state\n");
   91: 
   92:     /* Make sure all file formats flush their mutable metadata */
   93:     bdrv_invalidate_cache_all();
   94: 
   95:     if (autostart) {
   96:         vm_start();
   97:     } else {
   98:         runstate_set(RUN_STATE_PRELAUNCH);
   99:     }
  100: }
  101: 
  102: /* amount of nanoseconds we are willing to wait for migration to be down.
  103:  * the choice of nanoseconds is because it is the maximum resolution that
  104:  * get_clock() can achieve. It is an internal measure. All user-visible
  105:  * units must be in seconds */
  106: static uint64_t max_downtime = 30000000;
  107: 
  108: uint64_t migrate_max_downtime(void)
  109: {
  110:     return max_downtime;
  111: }
  112: 
  113: MigrationInfo *qmp_query_migrate(Error **errp)
  114: {
  115:     MigrationInfo *info = g_malloc0(sizeof(*info));
  116:     MigrationState *s = migrate_get_current();
  117: 
  118:     switch (s->state) {
  119:     case MIG_STATE_SETUP:
  120:         /* no migration has happened ever */
  121:         break;
  122:     case MIG_STATE_ACTIVE:
  123:         info->has_status = true;
  124:         info->status = g_strdup("active");
  125: 
  126:         info->has_ram = true;
  127:         info->ram = g_malloc0(sizeof(*info->ram));
  128:         info->ram->transferred = ram_bytes_transferred();
  129:         info->ram->remaining = ram_bytes_remaining();
  130:         info->ram->total = ram_bytes_total();
  131: 
  132:         if (blk_mig_active()) {
  133:             info->has_disk = true;
  134:             info->disk = g_malloc0(sizeof(*info->disk));
  135:             info->disk->transferred = blk_mig_bytes_transferred();
  136:             info->disk->remaining = blk_mig_bytes_remaining();
  137:             info->disk->total = blk_mig_bytes_total();
  138:         }
  139:         break;
  140:     case MIG_STATE_COMPLETED:
  141:         info->has_status = true;
  142:         info->status = g_strdup("completed");
  143:         break;
  144:     case MIG_STATE_ERROR:
  145:         info->has_status = true;
  146:         info->status = g_strdup("failed");
  147:         break;
  148:     case MIG_STATE_CANCELLED:
  149:         info->has_status = true;
  150:         info->status = g_strdup("cancelled");
  151:         break;
  152:     }
  153: 
  154:     return info;
  155: }
  156: 
  157: /* shared migration helpers */
  158: 
  159: static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon)
  160: {
  161:     if (monitor_suspend(mon) == 0) {
  162:         DPRINTF("suspending monitor\n");
  163:     } else {
  164:         monitor_printf(mon, "terminal does not allow synchronous "
  165:                        "migration, continuing detached\n");
  166:     }
  167: }
  168: 
  169: static int migrate_fd_cleanup(MigrationState *s)
  170: {
  171:     int ret = 0;
  172: 
  173:     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  174: 
  175:     if (s->file) {
  176:         DPRINTF("closing file\n");
  177:         if (qemu_fclose(s->file) != 0) {
  178:             ret = -1;
  179:         }
  180:         s->file = NULL;
  181:     } else {
  182:         if (s->mon) {
  183:             monitor_resume(s->mon);
  184:         }
  185:     }
  186: 
  187:     if (s->fd != -1) {
  188:         close(s->fd);
  189:         s->fd = -1;
  190:     }
  191: 
  192:     return ret;
  193: }
  194: 
  195: void migrate_fd_error(MigrationState *s)
  196: {
  197:     DPRINTF("setting error state\n");
  198:     s->state = MIG_STATE_ERROR;
  199:     notifier_list_notify(&migration_state_notifiers, s);
  200:     migrate_fd_cleanup(s);
  201: }
  202: 
  203: static void migrate_fd_completed(MigrationState *s)
  204: {
  205:     DPRINTF("setting completed state\n");
  206:     if (migrate_fd_cleanup(s) < 0) {
  207:         s->state = MIG_STATE_ERROR;
  208:     } else {
  209:         s->state = MIG_STATE_COMPLETED;
  210:         runstate_set(RUN_STATE_POSTMIGRATE);
  211:     }
  212:     notifier_list_notify(&migration_state_notifiers, s);
  213: }
  214: 
  215: static void migrate_fd_put_notify(void *opaque)
  216: {
  217:     MigrationState *s = opaque;
  218: 
  219:     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  220:     qemu_file_put_notify(s->file);
  221:     if (s->file && qemu_file_get_error(s->file)) {
  222:         migrate_fd_error(s);
  223:     }
  224: }
  225: 
  226: static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
  227:                                      size_t size)
  228: {
  229:     MigrationState *s = opaque;
  230:     ssize_t ret;
  231: 
  232:     if (s->state != MIG_STATE_ACTIVE) {
  233:         return -EIO;
  234:     }
  235: 
  236:     do {
  237:         ret = s->write(s, data, size);
  238:     } while (ret == -1 && ((s->get_error(s)) == EINTR));
  239: 
  240:     if (ret == -1)
  241:         ret = -(s->get_error(s));
  242: 
  243:     if (ret == -EAGAIN) {
  244:         qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
  245:     }
  246: 
  247:     return ret;
  248: }
  249: 
  250: static void migrate_fd_put_ready(void *opaque)
  251: {
  252:     MigrationState *s = opaque;
  253:     int ret;
  254: 
  255:     if (s->state != MIG_STATE_ACTIVE) {
  256:         DPRINTF("put_ready returning because of non-active state\n");
  257:         return;
  258:     }
  259: 
  260:     DPRINTF("iterate\n");
  261:     ret = qemu_savevm_state_iterate(s->mon, s->file);
  262:     if (ret < 0) {
  263:         migrate_fd_error(s);
  264:     } else if (ret == 1) {
  265:         int old_vm_running = runstate_is_running();
  266: 
  267:         DPRINTF("done iterating\n");
  268:         vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
  269: 
  270:         if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
  271:             migrate_fd_error(s);
  272:         } else {
  273:             migrate_fd_completed(s);
  274:         }
  275:         if (s->state != MIG_STATE_COMPLETED) {
  276:             if (old_vm_running) {
  277:                 vm_start();
  278:             }
  279:         }
  280:     }
  281: }
  282: 
  283: static void migrate_fd_cancel(MigrationState *s)
  284: {
  285:     if (s->state != MIG_STATE_ACTIVE)
  286:         return;
  287: 
  288:     DPRINTF("cancelling migration\n");
  289: 
  290:     s->state = MIG_STATE_CANCELLED;
  291:     notifier_list_notify(&migration_state_notifiers, s);
  292:     qemu_savevm_state_cancel(s->mon, s->file);
  293: 
  294:     migrate_fd_cleanup(s);
  295: }
  296: 
  297: static void migrate_fd_wait_for_unfreeze(void *opaque)
  298: {
  299:     MigrationState *s = opaque;
  300:     int ret;
  301: 
  302:     DPRINTF("wait for unfreeze\n");
  303:     if (s->state != MIG_STATE_ACTIVE)
  304:         return;
  305: 
  306:     do {
  307:         fd_set wfds;
  308: 
  309:         FD_ZERO(&wfds);
  310:         FD_SET(s->fd, &wfds);
  311: 
  312:         ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
  313:     } while (ret == -1 && (s->get_error(s)) == EINTR);
  314: 
  315:     if (ret == -1) {
  316:         qemu_file_set_error(s->file, -s->get_error(s));
  317:     }
  318: }
  319: 
  320: static int migrate_fd_close(void *opaque)
  321: {
  322:     MigrationState *s = opaque;
  323: 
  324:     if (s->mon) {
  325:         monitor_resume(s->mon);
  326:     }
  327:     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  328:     return s->close(s);
  329: }
  330: 
  331: void add_migration_state_change_notifier(Notifier *notify)
  332: {
  333:     notifier_list_add(&migration_state_notifiers, notify);
  334: }
  335: 
  336: void remove_migration_state_change_notifier(Notifier *notify)
  337: {
  338:     notifier_list_remove(&migration_state_notifiers, notify);
  339: }
  340: 
  341: bool migration_is_active(MigrationState *s)
  342: {
  343:     return s->state == MIG_STATE_ACTIVE;
  344: }
  345: 
  346: bool migration_has_finished(MigrationState *s)
  347: {
  348:     return s->state == MIG_STATE_COMPLETED;
  349: }
  350: 
  351: bool migration_has_failed(MigrationState *s)
  352: {
  353:     return (s->state == MIG_STATE_CANCELLED ||
  354:             s->state == MIG_STATE_ERROR);
  355: }
  356: 
  357: void migrate_fd_connect(MigrationState *s)
  358: {
  359:     int ret;
  360: 
  361:     s->state = MIG_STATE_ACTIVE;
  362:     s->file = qemu_fopen_ops_buffered(s,
  363:                                       s->bandwidth_limit,
  364:                                       migrate_fd_put_buffer,
  365:                                       migrate_fd_put_ready,
  366:                                       migrate_fd_wait_for_unfreeze,
  367:                                       migrate_fd_close);
  368: 
  369:     DPRINTF("beginning savevm\n");
  370:     ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
  371:     if (ret < 0) {
  372:         DPRINTF("failed, %d\n", ret);
  373:         migrate_fd_error(s);
  374:         return;
  375:     }
  376:     migrate_fd_put_ready(s);
  377: }
  378: 
  379: static MigrationState *migrate_init(Monitor *mon, int detach, int blk, int inc)
  380: {
  381:     MigrationState *s = migrate_get_current();
  382:     int64_t bandwidth_limit = s->bandwidth_limit;
  383: 
  384:     memset(s, 0, sizeof(*s));
  385:     s->bandwidth_limit = bandwidth_limit;
  386:     s->blk = blk;
  387:     s->shared = inc;
  388: 
  389:     /* s->mon is used for two things:
  390:        - pass fd in fd migration
  391:        - suspend/resume monitor for not detached migration
  392:     */
  393:     s->mon = mon;
  394:     s->bandwidth_limit = bandwidth_limit;
  395:     s->state = MIG_STATE_SETUP;
  396: 
  397:     if (!detach) {
  398:         migrate_fd_monitor_suspend(s, mon);
  399:     }
  400: 
  401:     return s;
  402: }
  403: 
  404: static GSList *migration_blockers;
  405: 
  406: void migrate_add_blocker(Error *reason)
  407: {
  408:     migration_blockers = g_slist_prepend(migration_blockers, reason);
  409: }
  410: 
  411: void migrate_del_blocker(Error *reason)
  412: {
  413:     migration_blockers = g_slist_remove(migration_blockers, reason);
  414: }
  415: 
  416: int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
  417: {
  418:     MigrationState *s = migrate_get_current();
  419:     const char *p;
  420:     int detach = qdict_get_try_bool(qdict, "detach", 0);
  421:     int blk = qdict_get_try_bool(qdict, "blk", 0);
  422:     int inc = qdict_get_try_bool(qdict, "inc", 0);
  423:     const char *uri = qdict_get_str(qdict, "uri");
  424:     int ret;
  425: 
  426:     if (s->state == MIG_STATE_ACTIVE) {
  427:         monitor_printf(mon, "migration already in progress\n");
  428:         return -1;
  429:     }
  430: 
  431:     if (qemu_savevm_state_blocked(mon)) {
  432:         return -1;
  433:     }
  434: 
  435:     if (migration_blockers) {
  436:         Error *err = migration_blockers->data;
  437:         qerror_report_err(err);
  438:         return -1;
  439:     }
  440: 
  441:     s = migrate_init(mon, detach, blk, inc);
  442: 
  443:     if (strstart(uri, "tcp:", &p)) {
  444:         ret = tcp_start_outgoing_migration(s, p);
  445: #if !defined(WIN32)
  446:     } else if (strstart(uri, "exec:", &p)) {
  447:         ret = exec_start_outgoing_migration(s, p);
  448:     } else if (strstart(uri, "unix:", &p)) {
  449:         ret = unix_start_outgoing_migration(s, p);
  450:     } else if (strstart(uri, "fd:", &p)) {
  451:         ret = fd_start_outgoing_migration(s, p);
  452: #endif
  453:     } else {
  454:         monitor_printf(mon, "unknown migration protocol: %s\n", uri);
  455:         ret  = -EINVAL;
  456:     }
  457: 
  458:     if (ret < 0) {
  459:         monitor_printf(mon, "migration failed: %s\n", strerror(-ret));
  460:         return ret;
  461:     }
  462: 
  463:     if (detach) {
  464:         s->mon = NULL;
  465:     }
  466: 
  467:     notifier_list_notify(&migration_state_notifiers, s);
  468:     return 0;
  469: }
  470: 
  471: int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
  472: {
  473:     migrate_fd_cancel(migrate_get_current());
  474:     return 0;
  475: }
  476: 
  477: int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
  478: {
  479:     int64_t d;
  480:     MigrationState *s;
  481: 
  482:     d = qdict_get_int(qdict, "value");
  483:     if (d < 0) {
  484:         d = 0;
  485:     }
  486: 
  487:     s = migrate_get_current();
  488:     s->bandwidth_limit = d;
  489:     qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
  490: 
  491:     return 0;
  492: }
  493: 
  494: int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
  495:                             QObject **ret_data)
  496: {
  497:     double d;
  498: 
  499:     d = qdict_get_double(qdict, "value") * 1e9;
  500:     d = MAX(0, MIN(UINT64_MAX, d));
  501:     max_downtime = (uint64_t)d;
  502: 
  503:     return 0;
  504: }

unix.superglobalmegacorp.com