]> git.proxmox.com Git - qemu.git/commitdiff
migration: use QEMUFile for migration channel lifetime
authorPaolo Bonzini <pbonzini@redhat.com>
Fri, 22 Feb 2013 16:36:41 +0000 (17:36 +0100)
committerJuan Quintela <quintela@redhat.com>
Mon, 11 Mar 2013 12:32:02 +0000 (13:32 +0100)
As a start, use QEMUFile to store the destination and close it.
qemu_get_fd gets a file descriptor that will be used by the write
callbacks.

Reviewed-by: Orit Wasserman <owasserm@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
include/migration/migration.h
migration-exec.c
migration-fd.c
migration-tcp.c
migration-unix.c
migration.c
savevm.c

index cec864387027401ec914ae5eacb3675166581540..1f8f305ac96d33b385204e82c7ae88edc72cef28 100644 (file)
@@ -38,12 +38,13 @@ struct MigrationState
     QEMUBH *cleanup_bh;
 
     QEMUFile *file;
+    QEMUFile *migration_file;
+
     int fd;
-    int state;
     int (*get_error)(MigrationState *s);
-    int (*close)(MigrationState *s);
     int (*write)(MigrationState *s, const void *buff, size_t size);
-    void *opaque;
+
+    int state;
     MigrationParams params;
     int64_t total_time;
     int64_t downtime;
index a2b5f8d7293ab59da74e9c1a392280da2373a6f6..8c3f72050a0563ce0d82a733bb2aefec93b96479 100644 (file)
@@ -43,33 +43,16 @@ static int file_write(MigrationState *s, const void * buf, size_t size)
     return write(s->fd, buf, size);
 }
 
-static int exec_close(MigrationState *s)
-{
-    int ret = 0;
-    DPRINTF("exec_close\n");
-    ret = qemu_fclose(s->opaque);
-    s->opaque = NULL;
-    s->fd = -1;
-    return ret;
-}
-
 void exec_start_outgoing_migration(MigrationState *s, const char *command, Error **errp)
 {
-    QEMUFile *f;
-    f = qemu_popen_cmd(command, "w");
-    if (f == NULL) {
+    s->migration_file = qemu_popen_cmd(command, "w");
+    if (s->migration_file == NULL) {
         error_setg_errno(errp, errno, "failed to popen the migration target");
         return;
     }
 
-    s->opaque = f;
-    s->fd = qemu_get_fd(f);
-    assert(s->fd != -1);
-
-    s->close = exec_close;
     s->get_error = file_errno;
     s->write = file_write;
-
     migrate_fd_connect(s);
 }
 
index a99e0e3971350bbe89929e477ae6810460fe266a..463645794c94c27320611200683b81cc6506e3e1 100644 (file)
@@ -40,45 +40,16 @@ static int fd_write(MigrationState *s, const void * buf, size_t size)
     return write(s->fd, buf, size);
 }
 
-static int fd_close(MigrationState *s)
-{
-    struct stat st;
-    int ret;
-
-    DPRINTF("fd_close\n");
-    ret = fstat(s->fd, &st);
-    if (ret == 0 && S_ISREG(st.st_mode)) {
-        /*
-         * If the file handle is a regular file make sure the
-         * data is flushed to disk before signaling success.
-         */
-        ret = fsync(s->fd);
-        if (ret != 0) {
-            ret = -errno;
-            perror("migration-fd: fsync");
-            return ret;
-        }
-    }
-    ret = close(s->fd);
-    s->fd = -1;
-    if (ret != 0) {
-        ret = -errno;
-        perror("migration-fd: close");
-    }
-    return ret;
-}
-
 void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
 {
-    s->fd = monitor_get_fd(cur_mon, fdname, errp);
-    if (s->fd == -1) {
+    int fd = monitor_get_fd(cur_mon, fdname, errp);
+    if (fd == -1) {
         return;
     }
+    s->migration_file = qemu_fdopen(fd, "wb");
 
     s->get_error = fd_errno;
     s->write = fd_write;
-    s->close = fd_close;
-
     migrate_fd_connect(s);
 }
 
index 7d975b5e80fa9dd1ca21164665b865bf13c1e03e..1e8e00411b49d1d45e3105de45720439f5330566 100644 (file)
@@ -39,28 +39,17 @@ static int socket_write(MigrationState *s, const void * buf, size_t size)
     return send(s->fd, buf, size, 0);
 }
 
-static int tcp_close(MigrationState *s)
-{
-    int r = 0;
-    DPRINTF("tcp_close\n");
-    if (closesocket(s->fd) < 0) {
-        r = -socket_error();
-    }
-    return r;
-}
-
 static void tcp_wait_for_connect(int fd, void *opaque)
 {
     MigrationState *s = opaque;
 
     if (fd < 0) {
         DPRINTF("migrate connect error\n");
-        s->fd = -1;
+        s->migration_file = NULL;
         migrate_fd_error(s);
     } else {
         DPRINTF("migrate connect success\n");
-        s->fd = fd;
-        socket_set_block(s->fd);
+        s->migration_file = qemu_fopen_socket(fd, "wb");
         migrate_fd_connect(s);
     }
 }
@@ -69,9 +58,7 @@ void tcp_start_outgoing_migration(MigrationState *s, const char *host_port, Erro
 {
     s->get_error = socket_errno;
     s->write = socket_write;
-    s->close = tcp_close;
-
-    s->fd = inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, errp);
+    inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, errp);
 }
 
 static void tcp_accept_incoming_migration(void *opaque)
index 4693b43d90597b8178f10beaebc92fb28845621a..11917f4f2a0389ed73dab5a412cfd2b0c936fa8b 100644 (file)
@@ -39,28 +39,17 @@ static int unix_write(MigrationState *s, const void * buf, size_t size)
     return write(s->fd, buf, size);
 }
 
-static int unix_close(MigrationState *s)
-{
-    int r = 0;
-    DPRINTF("unix_close\n");
-    if (close(s->fd) < 0) {
-        r = -errno;
-    }
-    return r;
-}
-
 static void unix_wait_for_connect(int fd, void *opaque)
 {
     MigrationState *s = opaque;
 
     if (fd < 0) {
         DPRINTF("migrate connect error\n");
-        s->fd = -1;
+        s->migration_file = NULL;
         migrate_fd_error(s);
     } else {
         DPRINTF("migrate connect success\n");
-        s->fd = fd;
-        socket_set_block(s->fd);
+        s->migration_file = qemu_fopen_socket(fd, "wb");
         migrate_fd_connect(s);
     }
 }
@@ -69,9 +58,7 @@ void unix_start_outgoing_migration(MigrationState *s, const char *path, Error **
 {
     s->get_error = unix_errno;
     s->write = unix_write;
-    s->close = unix_close;
-
-    s->fd = unix_nonblocking_connect(path, unix_wait_for_connect, s, errp);
+    unix_nonblocking_connect(path, unix_wait_for_connect, s, errp);
 }
 
 static void unix_accept_incoming_migration(void *opaque)
index f35728da8d97617f82a88592a8ee855dd80846c9..52126d86cc03f7a8cdeb9d5947890e9117a016ed 100644 (file)
@@ -274,7 +274,7 @@ static void migrate_fd_cleanup(void *opaque)
         s->file = NULL;
     }
 
-    assert(s->fd == -1);
+    assert(s->migration_file == NULL);
     assert(s->state != MIG_STATE_ACTIVE);
 
     if (s->state != MIG_STATE_COMPLETED) {
@@ -330,8 +330,9 @@ static void migrate_fd_cancel(MigrationState *s)
 int migrate_fd_close(MigrationState *s)
 {
     int rc = 0;
-    if (s->fd != -1) {
-        rc = s->close(s);
+    if (s->migration_file != NULL) {
+        rc = qemu_fclose(s->migration_file);
+        s->migration_file = NULL;
         s->fd = -1;
     }
     return rc;
@@ -720,6 +721,7 @@ void migrate_fd_connect(MigrationState *s)
     s->xfer_limit = s->bandwidth_limit / XFER_LIMIT_RATIO;
 
     s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
+    s->fd = qemu_get_fd(s->migration_file);
     s->file = qemu_fopen_ops(s, &migration_file_ops);
 
     qemu_thread_create(&s->thread, migration_thread, s,
index c60ace321876d9b6d8e1296734a16e99d77b556f..1414f08c1accdabf1625bba40d7830ff8e33f876 100644 (file)
--- a/savevm.c
+++ b/savevm.c
@@ -400,6 +400,7 @@ QEMUFile *qemu_fopen_socket(int fd, const char *mode)
 
     s->fd = fd;
     if (mode[0] == 'w') {
+        socket_set_block(s->fd);
         s->file = qemu_fopen_ops(s, &socket_write_ops);
     } else {
         s->file = qemu_fopen_ops(s, &socket_read_ops);