From patchwork Tue Jan 11 10:59:43 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yoshiaki Tamura X-Patchwork-Id: 470841 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 p0BB6NSN031269 for ; Tue, 11 Jan 2011 11:06:44 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755811Ab1AKLG1 (ORCPT ); Tue, 11 Jan 2011 06:06:27 -0500 Received: from sh.osrg.net ([192.16.179.4]:39444 "EHLO sh.osrg.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755758Ab1AKLF7 (ORCPT ); Tue, 11 Jan 2011 06:05:59 -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 p0BB5SX5027577; Tue, 11 Jan 2011 20:05:28 +0900 Received: from localhost (hype-nh0.osrg.net [10.72.1.48]) by fs.osrg.net (Postfix) with ESMTP id 80BB21B8008; Tue, 11 Jan 2011 20:05:25 +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, 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, ohmura.kei@lab.ntt.co.jp, Yoshiaki Tamura Subject: [PATCH 08/19] savevm: introduce util functions to control ft_trans_file from savevm layer. Date: Tue, 11 Jan 2011 19:59:43 +0900 Message-Id: <1294743594-19193-9-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> X-Mailer: git-send-email 1.7.1.2 In-Reply-To: <1294743594-19193-1-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> References: <1294743594-19193-1-git-send-email-tamura.yoshiaki@lab.ntt.co.jp> X-Dispatcher: imput version 20070423(IM149) Lines: 243 X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Tue, 11 Jan 2011 11:06:44 +0000 (UTC) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-3.0 (sh.osrg.net [192.16.179.4]); Tue, 11 Jan 2011 20:05:30 +0900 (JST) X-Virus-Scanned: clamav-milter 0.96.5 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 a506688..ace1744 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 7bc3699..ebb3ef8 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,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 +421,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 +577,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 +645,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;