a7b9285
From e15601079ef83c1fed53fb82d938bb2e4d6104ca Mon Sep 17 00:00:00 2001
Alon Levy 408bdb5
From: Paolo Bonzini <pbonzini@redhat.com>
Alon Levy 408bdb5
Date: Fri, 22 Feb 2013 17:36:42 +0100
a7b9285
Subject: [PATCH] migration: use QEMUFile for writing outgoing migration data
Alon Levy 408bdb5
Alon Levy 408bdb5
Second, drop the file descriptor indirection, and write directly to the
Alon Levy 408bdb5
QEMUFile.
Alon Levy 408bdb5
Alon Levy 408bdb5
Reviewed-by: Orit Wasserman <owasserm@redhat.com>
Alon Levy 408bdb5
Reviewed-by: Juan Quintela <quintela@redhat.com>
Alon Levy 408bdb5
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Alon Levy 408bdb5
Signed-off-by: Juan Quintela <quintela@redhat.com>
a7b9285
(cherry picked from commit e6a1cf21328802f3a83e84e893b8cb8a468141cc)
Alon Levy 408bdb5
---
Alon Levy 408bdb5
 include/migration/migration.h |  4 ----
Alon Levy 408bdb5
 migration-exec.c              | 12 -----------
Alon Levy 408bdb5
 migration-fd.c                | 12 -----------
Alon Levy 408bdb5
 migration-tcp.c               | 12 -----------
Alon Levy 408bdb5
 migration-unix.c              | 12 -----------
Alon Levy 408bdb5
 migration.c                   | 46 ++++++++-----------------------------------
Alon Levy 408bdb5
 6 files changed, 8 insertions(+), 90 deletions(-)
Alon Levy 408bdb5
Alon Levy 408bdb5
diff --git a/include/migration/migration.h b/include/migration/migration.h
Alon Levy 408bdb5
index 1f8f305..ae94706 100644
Alon Levy 408bdb5
--- a/include/migration/migration.h
Alon Levy 408bdb5
+++ b/include/migration/migration.h
Alon Levy 408bdb5
@@ -40,10 +40,6 @@ struct MigrationState
Alon Levy 408bdb5
     QEMUFile *file;
Alon Levy 408bdb5
     QEMUFile *migration_file;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-    int fd;
Alon Levy 408bdb5
-    int (*get_error)(MigrationState *s);
Alon Levy 408bdb5
-    int (*write)(MigrationState *s, const void *buff, size_t size);
Alon Levy 408bdb5
-
Alon Levy 408bdb5
     int state;
Alon Levy 408bdb5
     MigrationParams params;
Alon Levy 408bdb5
     int64_t total_time;
Alon Levy 408bdb5
diff --git a/migration-exec.c b/migration-exec.c
Alon Levy 408bdb5
index 8c3f720..1c539de 100644
Alon Levy 408bdb5
--- a/migration-exec.c
Alon Levy 408bdb5
+++ b/migration-exec.c
Alon Levy 408bdb5
@@ -33,16 +33,6 @@
Alon Levy 408bdb5
     do { } while (0)
Alon Levy 408bdb5
 #endif
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-static int file_errno(MigrationState *s)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    return errno;
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-static int file_write(MigrationState *s, const void * buf, size_t size)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    return write(s->fd, buf, size);
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
 void exec_start_outgoing_migration(MigrationState *s, const char *command, Error **errp)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     s->migration_file = qemu_popen_cmd(command, "w");
Alon Levy 408bdb5
@@ -51,8 +41,6 @@ void exec_start_outgoing_migration(MigrationState *s, const char *command, Error
Alon Levy 408bdb5
         return;
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-    s->get_error = file_errno;
Alon Levy 408bdb5
-    s->write = file_write;
Alon Levy 408bdb5
     migrate_fd_connect(s);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
diff --git a/migration-fd.c b/migration-fd.c
Alon Levy 408bdb5
index 4636457..07c758a 100644
Alon Levy 408bdb5
--- a/migration-fd.c
Alon Levy 408bdb5
+++ b/migration-fd.c
Alon Levy 408bdb5
@@ -30,16 +30,6 @@
Alon Levy 408bdb5
     do { } while (0)
Alon Levy 408bdb5
 #endif
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-static int fd_errno(MigrationState *s)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    return errno;
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-static int fd_write(MigrationState *s, const void * buf, size_t size)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    return write(s->fd, buf, size);
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
 void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     int fd = monitor_get_fd(cur_mon, fdname, errp);
Alon Levy 408bdb5
@@ -48,8 +38,6 @@ void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
     s->migration_file = qemu_fdopen(fd, "wb");
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-    s->get_error = fd_errno;
Alon Levy 408bdb5
-    s->write = fd_write;
Alon Levy 408bdb5
     migrate_fd_connect(s);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
diff --git a/migration-tcp.c b/migration-tcp.c
Alon Levy 408bdb5
index 1e8e004..5ea4f3d 100644
Alon Levy 408bdb5
--- a/migration-tcp.c
Alon Levy 408bdb5
+++ b/migration-tcp.c
Alon Levy 408bdb5
@@ -29,16 +29,6 @@
Alon Levy 408bdb5
     do { } while (0)
Alon Levy 408bdb5
 #endif
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-static int socket_errno(MigrationState *s)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    return socket_error();
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-static int socket_write(MigrationState *s, const void * buf, size_t size)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    return send(s->fd, buf, size, 0);
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
 static void tcp_wait_for_connect(int fd, void *opaque)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     MigrationState *s = opaque;
Alon Levy 408bdb5
@@ -56,8 +46,6 @@ static void tcp_wait_for_connect(int fd, void *opaque)
Alon Levy 408bdb5
 
Alon Levy 408bdb5
 void tcp_start_outgoing_migration(MigrationState *s, const char *host_port, Error **errp)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
-    s->get_error = socket_errno;
Alon Levy 408bdb5
-    s->write = socket_write;
Alon Levy 408bdb5
     inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, errp);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
diff --git a/migration-unix.c b/migration-unix.c
Alon Levy 408bdb5
index 11917f4..64bfa31 100644
Alon Levy 408bdb5
--- a/migration-unix.c
Alon Levy 408bdb5
+++ b/migration-unix.c
Alon Levy 408bdb5
@@ -29,16 +29,6 @@
Alon Levy 408bdb5
     do { } while (0)
Alon Levy 408bdb5
 #endif
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-static int unix_errno(MigrationState *s)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    return errno;
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-static int unix_write(MigrationState *s, const void * buf, size_t size)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    return write(s->fd, buf, size);
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
 static void unix_wait_for_connect(int fd, void *opaque)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     MigrationState *s = opaque;
Alon Levy 408bdb5
@@ -56,8 +46,6 @@ static void unix_wait_for_connect(int fd, void *opaque)
Alon Levy 408bdb5
 
Alon Levy 408bdb5
 void unix_start_outgoing_migration(MigrationState *s, const char *path, Error **errp)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
-    s->get_error = unix_errno;
Alon Levy 408bdb5
-    s->write = unix_write;
Alon Levy 408bdb5
     unix_nonblocking_connect(path, unix_wait_for_connect, s, errp);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
diff --git a/migration.c b/migration.c
Alon Levy 408bdb5
index fa630ed..26ba6c9 100644
Alon Levy 408bdb5
--- a/migration.c
Alon Levy 408bdb5
+++ b/migration.c
Alon Levy 408bdb5
@@ -301,25 +301,6 @@ void migrate_fd_error(MigrationState *s)
Alon Levy 408bdb5
     notifier_list_notify(&migration_state_notifiers, s);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-static ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
Alon Levy 408bdb5
-                                     size_t size)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    ssize_t ret;
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-    if (s->state != MIG_STATE_ACTIVE) {
Alon Levy 408bdb5
-        return -EIO;
Alon Levy 408bdb5
-    }
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-    do {
Alon Levy 408bdb5
-        ret = s->write(s, data, size);
Alon Levy 408bdb5
-    } while (ret == -1 && ((s->get_error(s)) == EINTR));
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-    if (ret == -1)
Alon Levy 408bdb5
-        ret = -(s->get_error(s));
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-    return ret;
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
 static void migrate_fd_cancel(MigrationState *s)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     DPRINTF("cancelling migration\n");
Alon Levy 408bdb5
@@ -333,7 +314,6 @@ int migrate_fd_close(MigrationState *s)
Alon Levy 408bdb5
     if (s->migration_file != NULL) {
Alon Levy 408bdb5
         rc = qemu_fclose(s->migration_file);
Alon Levy 408bdb5
         s->migration_file = NULL;
Alon Levy 408bdb5
-        s->fd = -1;
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
     return rc;
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
@@ -519,8 +499,7 @@ static int migration_put_buffer(void *opaque, const uint8_t *buf,
Alon Levy 408bdb5
                                int64_t pos, int size)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     MigrationState *s = opaque;
Alon Levy 408bdb5
-    ssize_t ret;
Alon Levy 408bdb5
-    size_t sent;
Alon Levy 408bdb5
+    int ret;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);
Alon Levy 408bdb5
 
Alon Levy 408bdb5
@@ -528,22 +507,14 @@ static int migration_put_buffer(void *opaque, const uint8_t *buf,
Alon Levy 408bdb5
         return size;
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-    sent = 0;
Alon Levy 408bdb5
-    while (size) {
Alon Levy 408bdb5
-        ret = migrate_fd_put_buffer(s, buf, size);
Alon Levy 408bdb5
-        if (ret <= 0) {
Alon Levy 408bdb5
-            DPRINTF("error flushing data, %zd\n", ret);
Alon Levy 408bdb5
-            return ret;
Alon Levy 408bdb5
-        } else {
Alon Levy 408bdb5
-            DPRINTF("flushed %zd byte(s)\n", ret);
Alon Levy 408bdb5
-            sent += ret;
Alon Levy 408bdb5
-            buf += ret;
Alon Levy 408bdb5
-            size -= ret;
Alon Levy 408bdb5
-            s->bytes_xfer += ret;
Alon Levy 408bdb5
-        }
Alon Levy 408bdb5
+    qemu_put_buffer(s->migration_file, buf, size);
Alon Levy 408bdb5
+    ret = qemu_file_get_error(s->migration_file);
Alon Levy 408bdb5
+    if (ret) {
Alon Levy 408bdb5
+        return ret;
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-    return sent;
Alon Levy 408bdb5
+    s->bytes_xfer += size;
Alon Levy 408bdb5
+    return size;
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
 static int migration_close(void *opaque)
Alon Levy 408bdb5
@@ -564,7 +535,7 @@ static int migration_get_fd(void *opaque)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     MigrationState *s = opaque;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-    return s->fd;
Alon Levy 408bdb5
+    return qemu_get_fd(s->migration_file);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
 /*
Alon Levy 408bdb5
@@ -721,7 +692,6 @@ void migrate_fd_connect(MigrationState *s)
Alon Levy 408bdb5
     s->xfer_limit = s->bandwidth_limit / XFER_LIMIT_RATIO;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Alon Levy 408bdb5
-    s->fd = qemu_get_fd(s->migration_file);
Alon Levy 408bdb5
     s->file = qemu_fopen_ops(s, &migration_file_ops);
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     qemu_thread_create(&s->thread, migration_thread, s,