qemu/migration-exec.c
<<
>>
Prefs
   1/*
   2 * QEMU live migration
   3 *
   4 * Copyright IBM, Corp. 2008
   5 * Copyright Dell MessageOne 2008
   6 *
   7 * Authors:
   8 *  Anthony Liguori   <aliguori@us.ibm.com>
   9 *  Charles Duffy     <charles_duffy@messageone.com>
  10 *
  11 * This work is licensed under the terms of the GNU GPL, version 2.  See
  12 * the COPYING file in the top-level directory.
  13 *
  14 */
  15
  16#include "qemu-common.h"
  17#include "qemu_socket.h"
  18#include "migration.h"
  19#include "qemu-char.h"
  20#include "sysemu.h"
  21#include "buffered_file.h"
  22#include "block.h"
  23
  24//#define DEBUG_MIGRATION_EXEC
  25
  26#ifdef DEBUG_MIGRATION_EXEC
  27#define dprintf(fmt, ...) \
  28    do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0)
  29#else
  30#define dprintf(fmt, ...) \
  31    do { } while (0)
  32#endif
  33
  34static int file_errno(FdMigrationState *s)
  35{
  36    return errno;
  37}
  38
  39static int file_write(FdMigrationState *s, const void * buf, size_t size)
  40{
  41    return write(s->fd, buf, size);
  42}
  43
  44static int exec_close(FdMigrationState *s)
  45{
  46    dprintf("exec_close\n");
  47    if (s->opaque) {
  48        qemu_fclose(s->opaque);
  49        s->opaque = NULL;
  50        s->fd = -1;
  51    }
  52    return 0;
  53}
  54
  55MigrationState *exec_start_outgoing_migration(const char *command,
  56                                             int64_t bandwidth_limit,
  57                                             int detach)
  58{
  59    FdMigrationState *s;
  60    FILE *f;
  61
  62    s = qemu_mallocz(sizeof(*s));
  63
  64    f = popen(command, "w");
  65    if (f == NULL) {
  66        dprintf("Unable to popen exec target\n");
  67        goto err_after_alloc;
  68    }
  69
  70    s->fd = fileno(f);
  71    if (s->fd == -1) {
  72        dprintf("Unable to retrieve file descriptor for popen'd handle\n");
  73        goto err_after_open;
  74    }
  75
  76    socket_set_nonblock(s->fd);
  77
  78    s->opaque = qemu_popen(f, "w");
  79
  80    s->close = exec_close;
  81    s->get_error = file_errno;
  82    s->write = file_write;
  83    s->mig_state.cancel = migrate_fd_cancel;
  84    s->mig_state.get_status = migrate_fd_get_status;
  85    s->mig_state.release = migrate_fd_release;
  86
  87    s->state = MIG_STATE_ACTIVE;
  88    s->mon_resume = NULL;
  89    s->bandwidth_limit = bandwidth_limit;
  90
  91    if (!detach)
  92        migrate_fd_monitor_suspend(s);
  93
  94    migrate_fd_connect(s);
  95    return &s->mig_state;
  96
  97err_after_open:
  98    pclose(f);
  99err_after_alloc:
 100    qemu_free(s);
 101    return NULL;
 102}
 103
 104static void exec_accept_incoming_migration(void *opaque)
 105{
 106    QEMUFile *f = opaque;
 107    int ret;
 108
 109    ret = qemu_loadvm_state(f);
 110    if (ret < 0) {
 111        fprintf(stderr, "load of migration failed\n");
 112        goto err;
 113    }
 114    qemu_announce_self();
 115    dprintf("successfully loaded vm state\n");
 116    /* we've successfully migrated, close the fd */
 117    qemu_set_fd_handler2(qemu_popen_fd(f), NULL, NULL, NULL, NULL);
 118    if (autostart)
 119        vm_start();
 120
 121err:
 122    qemu_fclose(f);
 123}
 124
 125int exec_start_incoming_migration(const char *command)
 126{
 127    QEMUFile *f;
 128
 129    dprintf("Attempting to start an incoming migration\n");
 130    f = qemu_popen_cmd(command, "r");
 131    if(f == NULL) {
 132        dprintf("Unable to apply qemu wrapper to popen file\n");
 133        return -errno;
 134    }
 135
 136    qemu_set_fd_handler2(qemu_popen_fd(f), NULL,
 137                         exec_accept_incoming_migration, NULL,
 138                         (void *)(unsigned long)f);
 139
 140    return 0;
 141}
 142