Annotation of qemu/migration.c, revision 1.1.1.10

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

unix.superglobalmegacorp.com