From patchwork Thu Nov 25 06:06:47 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yoshiaki Tamura X-Patchwork-Id: 356192 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id oAP6rt2J000713 for ; Thu, 25 Nov 2010 06:53:56 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751973Ab0KYGxZ (ORCPT ); Thu, 25 Nov 2010 01:53:25 -0500 Received: from sh.osrg.net ([192.16.179.4]:33424 "EHLO sh.osrg.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751504Ab0KYGwy (ORCPT ); Thu, 25 Nov 2010 01:52:54 -0500 Received: from fs.osrg.net (postfix@fs.osrg.net [10.0.0.12]) by sh.osrg.net (8.14.3/8.14.3/OSRG-NET) with ESMTP id oAP6q0JQ003039; Thu, 25 Nov 2010 15:52:01 +0900 Received: from localhost (hype-nh0.osrg.net [10.72.1.48]) by fs.osrg.net (Postfix) with ESMTP id 756DD3E042B; Thu, 25 Nov 2010 15:51:57 +0900 (JST) From: Yoshiaki Tamura To: kvm@vger.kernel.org, qemu-devel@nongnu.org Cc: avi@redhat.com, anthony@codemonkey.ws, aliguori@us.ibm.com, mtosatti@redhat.com, dlaor@redhat.com, ananth@in.ibm.com, psuriset@linux.vnet.ibm.com, vatsa@linux.vnet.ibm.com, stefanha@linux.vnet.ibm.com, ohmura.kei@lab.ntt.co.jp, Yoshiaki Tamura Subject: [PATCH 08/21] savevm: introduce util functions to control ft_trans_file from savevm layer. Date: Thu, 25 Nov 2010 15:06:47 +0900 Message-Id: <1290665220-26478-9-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> X-Mailer: git-send-email 1.7.1.2 In-Reply-To: <1290665220-26478-1-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> References: <1290665220-26478-1-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> X-Dispatcher: imput version 20070423(IM149) Lines: 260 X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Thu, 25 Nov 2010 06:53:57 +0000 (UTC) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-3.0 (sh.osrg.net [192.16.179.4]); Thu, 25 Nov 2010 15:52:02 +0900 (JST) X-Virus-Scanned: clamav-milter 0.96.4 at sh X-Virus-Status: Clean Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org diff --git a/hw/hw.h b/hw/hw.h index b67f504..e9f71bc 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 8917416..afd4046 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,21 @@ 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 +235,87 @@ 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"); + ft_mode = FT_ERROR; + goto out; + } + + if (ft_mode == FT_OFF) { + /* if migrate_cancel was called at the sender */ + goto out; + } + + if (ft_mode == FT_ERROR) { + qemu_announce_self(); + goto out; + } + + return 0; + +out: + qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); + qemu_fclose(f); + return -1; +} + +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 +438,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) { @@ -471,6 +595,39 @@ void qemu_clear_buffer(QEMUFile *f) memset(f->buf, 0, f->buf_max_size); } +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; @@ -506,6 +663,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;