From mboxrd@z Thu Jan 1 00:00:00 1970 From: Yoshiaki Tamura Subject: [PATCH 08/19] savevm: introduce util functions to control ft_trans_file from savevm layer. Date: Wed, 26 Jan 2011 18:42:00 +0900 Message-ID: <1296034931-10843-9-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> References: <1296034931-10843-1-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> Cc: avi@redhat.com, anthony@codemonkey.ws, aliguori@us.ibm.com, mtosatti@redhat.com, dlaor@redhat.com, mst@redhat.com, kwolf@redhat.com, ananth@in.ibm.com, psuriset@linux.vnet.ibm.com, vatsa@linux.vnet.ibm.com, stefanha@linux.vnet.ibm.com, blauwirbel@gmail.com, ohmura.kei@lab.ntt.co.jp, Yoshiaki Tamura To: kvm@vger.kernel.org, qemu-devel@nongnu.org Return-path: Received: from sh.osrg.net ([192.16.179.4]:49683 "EHLO sh.osrg.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752286Ab1AZJny (ORCPT ); Wed, 26 Jan 2011 04:43:54 -0500 In-Reply-To: <1296034931-10843-1-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> Sender: kvm-owner@vger.kernel.org List-ID: To utilize ft_trans_file function, savevm needs interfaces to be exported. Signed-off-by: Yoshiaki Tamura --- hw/hw.h | 5 ++ savevm.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 0 deletions(-) diff --git a/hw/hw.h b/hw/hw.h index 7f05830..52e807c 100644 --- a/hw/hw.h +++ b/hw/hw.h @@ -51,6 +51,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_ft_trans(int s_fd, int c_fd); QEMUFile *qemu_popen(FILE *popen_file, const char *mode); QEMUFile *qemu_popen_cmd(const char *command, const char *mode); int qemu_stdio_fd(QEMUFile *f); @@ -60,6 +61,9 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size); void qemu_put_byte(QEMUFile *f, int v); void *qemu_realloc_buffer(QEMUFile *f, int size); void qemu_clear_buffer(QEMUFile *f); +int qemu_ft_trans_begin(QEMUFile *f); +int qemu_ft_trans_commit(QEMUFile *f); +int qemu_ft_trans_cancel(QEMUFile *f); static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v) { @@ -94,6 +98,7 @@ void qemu_file_set_error(QEMUFile *f); * halted due to rate limiting or EAGAIN errors occur as it can be used to * resume output. */ void qemu_file_put_notify(QEMUFile *f); +void qemu_file_get_notify(void *opaque); static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv) { diff --git a/savevm.c b/savevm.c index dc15c03..5418280 100644 --- a/savevm.c +++ b/savevm.c @@ -83,6 +83,7 @@ #include "migration.h" #include "qemu_socket.h" #include "qemu-queue.h" +#include "ft_trans_file.h" #define SELF_ANNOUNCE_ROUNDS 5 @@ -190,6 +191,13 @@ typedef struct QEMUFileSocket QEMUFile *file; } QEMUFileSocket; +typedef struct QEMUFileSocketTrans +{ + int fd; + QEMUFileSocket *s; + VMChangeStateEntry *e; +} QEMUFileSocketTrans; + static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) { QEMUFileSocket *s = opaque; @@ -205,6 +213,22 @@ static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) return len; } +static ssize_t socket_put_buffer(void *opaque, const void *buf, size_t size) +{ + QEMUFileSocket *s = opaque; + ssize_t len; + + do { + len = send(s->fd, (void *)buf, size, 0); + } while (len == -1 && socket_error() == EINTR); + + if (len == -1) { + len = -socket_error(); + } + + return len; +} + static int socket_close(void *opaque) { QEMUFileSocket *s = opaque; @@ -212,6 +236,70 @@ static int socket_close(void *opaque) return 0; } +static int socket_trans_get_buffer(void *opaque, uint8_t *buf, int64_t pos, size_t size) +{ + QEMUFileSocketTrans *t = opaque; + QEMUFileSocket *s = t->s; + ssize_t len; + + len = socket_get_buffer(s, buf, pos, size); + + return len; +} + +static ssize_t socket_trans_put_buffer(void *opaque, const void *buf, size_t size) +{ + QEMUFileSocketTrans *t = opaque; + + return socket_put_buffer(t->s, buf, size); +} + + +static int socket_trans_get_ready(void *opaque) +{ + QEMUFileSocketTrans *t = opaque; + QEMUFileSocket *s = t->s; + QEMUFile *f = s->file; + int ret = 0; + + ret = qemu_loadvm_state(f, 1); + if (ret < 0) { + fprintf(stderr, + "socket_trans_get_ready: error while loading vmstate\n"); + } + + return ret; +} + +static int socket_trans_close(void *opaque) +{ + QEMUFileSocketTrans *t = opaque; + QEMUFileSocket *s = t->s; + + qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); + qemu_set_fd_handler2(t->fd, NULL, NULL, NULL, NULL); + qemu_del_vm_change_state_handler(t->e); + close(s->fd); + close(t->fd); + qemu_free(s); + qemu_free(t); + + return 0; +} + +static void socket_trans_resume(void *opaque, int running, int reason) +{ + QEMUFileSocketTrans *t = opaque; + QEMUFileSocket *s = t->s; + + if (!running) { + return; + } + + qemu_announce_self(); + qemu_fclose(s->file); +} + static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size) { QEMUFileStdio *s = opaque; @@ -334,6 +422,26 @@ QEMUFile *qemu_fopen_socket(int fd) return s->file; } +QEMUFile *qemu_fopen_ft_trans(int s_fd, int c_fd) +{ + QEMUFileSocketTrans *t = qemu_mallocz(sizeof(QEMUFileSocketTrans)); + QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket)); + + t->s = s; + t->fd = s_fd; + t->e = qemu_add_vm_change_state_handler(socket_trans_resume, t); + + s->fd = c_fd; + s->file = qemu_fopen_ops_ft_trans(t, socket_trans_put_buffer, + socket_trans_get_buffer, NULL, + socket_trans_get_ready, + migrate_fd_wait_for_unfreeze, + socket_trans_close, 0); + socket_set_nonblock(s->fd); + + return s->file; +} + static int file_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size) { @@ -470,6 +578,39 @@ void qemu_clear_buffer(QEMUFile *f) f->buf_size = f->buf_index = f->buf_offset = 0; } +int qemu_ft_trans_begin(QEMUFile *f) +{ + int ret; + ret = ft_trans_begin(f->opaque); + if (ret < 0) { + f->has_error = 1; + } + return ret; +} + +int qemu_ft_trans_commit(QEMUFile *f) +{ + int ret; + ret = ft_trans_commit(f->opaque); + if (ret == -EAGAIN) { + return 1; + } + if (ret < 0) { + f->has_error = 1; + } + return ret; +} + +int qemu_ft_trans_cancel(QEMUFile *f) +{ + int ret; + ret = ft_trans_cancel(f->opaque); + if (ret < 0) { + f->has_error = 1; + } + return ret; +} + static void qemu_fill_buffer(QEMUFile *f) { int len; @@ -505,6 +646,14 @@ void qemu_file_put_notify(QEMUFile *f) f->put_buffer(f->opaque, NULL, 0, 0); } +void qemu_file_get_notify(void *opaque) +{ + QEMUFile *f = opaque; + if (f->get_buffer(f->opaque, f->buf, 0, 0) < 0) { + f->has_error = 1; + } +} + void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) { int l; -- 1.7.1.2 From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=51270 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pi1vJ-0004LJ-O6 for qemu-devel@nongnu.org; Wed, 26 Jan 2011 04:45:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Pi1ud-00069I-WF for qemu-devel@nongnu.org; Wed, 26 Jan 2011 04:43:44 -0500 Received: from sh.osrg.net ([192.16.179.4]:45532) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Pi1ud-00067r-DU for qemu-devel@nongnu.org; Wed, 26 Jan 2011 04:43:39 -0500 From: Yoshiaki Tamura Date: Wed, 26 Jan 2011 18:42:00 +0900 Message-Id: <1296034931-10843-9-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> In-Reply-To: <1296034931-10843-1-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> References: <1296034931-10843-1-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> Subject: [Qemu-devel] [PATCH 08/19] savevm: introduce util functions to control ft_trans_file from savevm layer. List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: kvm@vger.kernel.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, aliguori@us.ibm.com, mtosatti@redhat.com, ananth@in.ibm.com, mst@redhat.com, dlaor@redhat.com, vatsa@linux.vnet.ibm.com, Yoshiaki Tamura , blauwirbel@gmail.com, ohmura.kei@lab.ntt.co.jp, avi@redhat.com, psuriset@linux.vnet.ibm.com, stefanha@linux.vnet.ibm.com To utilize ft_trans_file function, savevm needs interfaces to be exported. Signed-off-by: Yoshiaki Tamura --- hw/hw.h | 5 ++ savevm.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 0 deletions(-) diff --git a/hw/hw.h b/hw/hw.h index 7f05830..52e807c 100644 --- a/hw/hw.h +++ b/hw/hw.h @@ -51,6 +51,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_ft_trans(int s_fd, int c_fd); QEMUFile *qemu_popen(FILE *popen_file, const char *mode); QEMUFile *qemu_popen_cmd(const char *command, const char *mode); int qemu_stdio_fd(QEMUFile *f); @@ -60,6 +61,9 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size); void qemu_put_byte(QEMUFile *f, int v); void *qemu_realloc_buffer(QEMUFile *f, int size); void qemu_clear_buffer(QEMUFile *f); +int qemu_ft_trans_begin(QEMUFile *f); +int qemu_ft_trans_commit(QEMUFile *f); +int qemu_ft_trans_cancel(QEMUFile *f); static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v) { @@ -94,6 +98,7 @@ void qemu_file_set_error(QEMUFile *f); * halted due to rate limiting or EAGAIN errors occur as it can be used to * resume output. */ void qemu_file_put_notify(QEMUFile *f); +void qemu_file_get_notify(void *opaque); static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv) { diff --git a/savevm.c b/savevm.c index dc15c03..5418280 100644 --- a/savevm.c +++ b/savevm.c @@ -83,6 +83,7 @@ #include "migration.h" #include "qemu_socket.h" #include "qemu-queue.h" +#include "ft_trans_file.h" #define SELF_ANNOUNCE_ROUNDS 5 @@ -190,6 +191,13 @@ typedef struct QEMUFileSocket QEMUFile *file; } QEMUFileSocket; +typedef struct QEMUFileSocketTrans +{ + int fd; + QEMUFileSocket *s; + VMChangeStateEntry *e; +} QEMUFileSocketTrans; + static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) { QEMUFileSocket *s = opaque; @@ -205,6 +213,22 @@ static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) return len; } +static ssize_t socket_put_buffer(void *opaque, const void *buf, size_t size) +{ + QEMUFileSocket *s = opaque; + ssize_t len; + + do { + len = send(s->fd, (void *)buf, size, 0); + } while (len == -1 && socket_error() == EINTR); + + if (len == -1) { + len = -socket_error(); + } + + return len; +} + static int socket_close(void *opaque) { QEMUFileSocket *s = opaque; @@ -212,6 +236,70 @@ static int socket_close(void *opaque) return 0; } +static int socket_trans_get_buffer(void *opaque, uint8_t *buf, int64_t pos, size_t size) +{ + QEMUFileSocketTrans *t = opaque; + QEMUFileSocket *s = t->s; + ssize_t len; + + len = socket_get_buffer(s, buf, pos, size); + + return len; +} + +static ssize_t socket_trans_put_buffer(void *opaque, const void *buf, size_t size) +{ + QEMUFileSocketTrans *t = opaque; + + return socket_put_buffer(t->s, buf, size); +} + + +static int socket_trans_get_ready(void *opaque) +{ + QEMUFileSocketTrans *t = opaque; + QEMUFileSocket *s = t->s; + QEMUFile *f = s->file; + int ret = 0; + + ret = qemu_loadvm_state(f, 1); + if (ret < 0) { + fprintf(stderr, + "socket_trans_get_ready: error while loading vmstate\n"); + } + + return ret; +} + +static int socket_trans_close(void *opaque) +{ + QEMUFileSocketTrans *t = opaque; + QEMUFileSocket *s = t->s; + + qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); + qemu_set_fd_handler2(t->fd, NULL, NULL, NULL, NULL); + qemu_del_vm_change_state_handler(t->e); + close(s->fd); + close(t->fd); + qemu_free(s); + qemu_free(t); + + return 0; +} + +static void socket_trans_resume(void *opaque, int running, int reason) +{ + QEMUFileSocketTrans *t = opaque; + QEMUFileSocket *s = t->s; + + if (!running) { + return; + } + + qemu_announce_self(); + qemu_fclose(s->file); +} + static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size) { QEMUFileStdio *s = opaque; @@ -334,6 +422,26 @@ QEMUFile *qemu_fopen_socket(int fd) return s->file; } +QEMUFile *qemu_fopen_ft_trans(int s_fd, int c_fd) +{ + QEMUFileSocketTrans *t = qemu_mallocz(sizeof(QEMUFileSocketTrans)); + QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket)); + + t->s = s; + t->fd = s_fd; + t->e = qemu_add_vm_change_state_handler(socket_trans_resume, t); + + s->fd = c_fd; + s->file = qemu_fopen_ops_ft_trans(t, socket_trans_put_buffer, + socket_trans_get_buffer, NULL, + socket_trans_get_ready, + migrate_fd_wait_for_unfreeze, + socket_trans_close, 0); + socket_set_nonblock(s->fd); + + return s->file; +} + static int file_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size) { @@ -470,6 +578,39 @@ void qemu_clear_buffer(QEMUFile *f) f->buf_size = f->buf_index = f->buf_offset = 0; } +int qemu_ft_trans_begin(QEMUFile *f) +{ + int ret; + ret = ft_trans_begin(f->opaque); + if (ret < 0) { + f->has_error = 1; + } + return ret; +} + +int qemu_ft_trans_commit(QEMUFile *f) +{ + int ret; + ret = ft_trans_commit(f->opaque); + if (ret == -EAGAIN) { + return 1; + } + if (ret < 0) { + f->has_error = 1; + } + return ret; +} + +int qemu_ft_trans_cancel(QEMUFile *f) +{ + int ret; + ret = ft_trans_cancel(f->opaque); + if (ret < 0) { + f->has_error = 1; + } + return ret; +} + static void qemu_fill_buffer(QEMUFile *f) { int len; @@ -505,6 +646,14 @@ void qemu_file_put_notify(QEMUFile *f) f->put_buffer(f->opaque, NULL, 0, 0); } +void qemu_file_get_notify(void *opaque) +{ + QEMUFile *f = opaque; + if (f->get_buffer(f->opaque, f->buf, 0, 0) < 0) { + f->has_error = 1; + } +} + void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) { int l; -- 1.7.1.2