Patchwork [v3,09/35] savevm/QEMUFile: introduce qemu_fopen_fd

login
register
mail settings
Submitter Isaku Yamahata
Date Oct. 30, 2012, 8:32 a.m.
Message ID <8debc2a867fdfa1f605a2f9d300267b552c8eb6b.1351582535.git.yamahata@valinux.co.jp>
Download mbox | patch
Permalink /patch/195322/
State New
Headers show

Comments

Isaku Yamahata - Oct. 30, 2012, 8:32 a.m.
Introduce fd read/write backend of QEMUFile whose fd can be non-blocking
This will be used by postcopy live migration.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
---
 qemu-file.h |    1 +
 savevm.c    |   35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

Patch

diff --git a/qemu-file.h b/qemu-file.h
index bc222dc..94557ea 100644
--- a/qemu-file.h
+++ b/qemu-file.h
@@ -68,6 +68,7 @@  QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
 QEMUFile *qemu_fopen(const char *filename, const char *mode);
 QEMUFile *qemu_fdopen(int fd, const char *mode);
 QEMUFile *qemu_fopen_socket(int fd);
+QEMUFile *qemu_fopen_fd(int fd, const char *mode);
 QEMUFile *qemu_popen(FILE *popen_file, const char *mode);
 QEMUFile *qemu_popen_cmd(const char *command, const char *mode);
 int qemu_file_fd(QEMUFile *f);
diff --git a/savevm.c b/savevm.c
index e24041b..712b7ae 100644
--- a/savevm.c
+++ b/savevm.c
@@ -207,6 +207,19 @@  static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
     return len;
 }
 
+static int fd_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
+{
+    QEMUFileFD *s = opaque;
+    return qemu_read_full(s->file->fd, buf, size);
+}
+
+static int fd_put_buffer(void *opaque,
+                         const uint8_t *buf, int64_t pos, int size)
+{
+    QEMUFileFD *s = opaque;
+    return qemu_write_full(s->file->fd, buf, size);
+}
+
 static int fd_close(void *opaque)
 {
     QEMUFileFD *s = opaque;
@@ -333,6 +346,28 @@  QEMUFile *qemu_fopen_socket(int fd)
     return s->file;
 }
 
+QEMUFile *qemu_fopen_fd(int fd, const char *mode)
+{
+    QEMUFileFD *s;
+
+    if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
+        fprintf(stderr, "qemu_fopen_fd: Argument validity check failed\n");
+        return NULL;
+    }
+
+    s = g_malloc0(sizeof(*s));
+    if (mode[0] == 'r') {
+        s->file = qemu_fopen_ops(s, NULL, fd_get_buffer, fd_close,
+                                 NULL, NULL, NULL);
+    } else {
+        s->file = qemu_fopen_ops(s, fd_put_buffer, NULL, fd_close,
+                                 NULL, NULL, NULL);
+    }
+
+    s->file->fd = fd;
+    return s->file;
+}
+
 static int file_put_buffer(void *opaque, const uint8_t *buf,
                             int64_t pos, int size)
 {