From patchwork Mon Feb 22 02:40:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhanghailiang X-Patchwork-Id: 8370431 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 5A2B0C0553 for ; Mon, 22 Feb 2016 02:44:03 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A71A720382 for ; Mon, 22 Feb 2016 02:44:02 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D403820375 for ; Mon, 22 Feb 2016 02:44:01 +0000 (UTC) Received: from localhost ([::1]:46063 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXgTh-00068T-09 for patchwork-qemu-devel@patchwork.kernel.org; Sun, 21 Feb 2016 21:44:01 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59588) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXgRA-0000jG-5G for qemu-devel@nongnu.org; Sun, 21 Feb 2016 21:41:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aXgR7-0007PN-8q for qemu-devel@nongnu.org; Sun, 21 Feb 2016 21:41:24 -0500 Received: from szxga01-in.huawei.com ([58.251.152.64]:63989) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXgR6-0007Ky-Ia for qemu-devel@nongnu.org; Sun, 21 Feb 2016 21:41:21 -0500 Received: from 172.24.1.51 (EHLO szxeml422-hub.china.huawei.com) ([172.24.1.51]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DEY15353; Mon, 22 Feb 2016 10:41:07 +0800 (CST) Received: from localhost (10.177.24.212) by szxeml422-hub.china.huawei.com (10.82.67.152) with Microsoft SMTP Server id 14.3.235.1; Mon, 22 Feb 2016 10:40:56 +0800 From: zhanghailiang To: Date: Mon, 22 Feb 2016 10:40:03 +0800 Message-ID: <1456108832-24212-10-git-send-email-zhang.zhanghailiang@huawei.com> X-Mailer: git-send-email 1.9.0.msysgit.0 In-Reply-To: <1456108832-24212-1-git-send-email-zhang.zhanghailiang@huawei.com> References: <1456108832-24212-1-git-send-email-zhang.zhanghailiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.24.212] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.56CA7543.00AC, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 2387daa4761995629a949263cee39b79 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 58.251.152.64 Cc: xiecl.fnst@cn.fujitsu.com, lizhijian@cn.fujitsu.com, quintela@redhat.com, armbru@redhat.com, yunhong.jiang@intel.com, eddie.dong@intel.com, peter.huangpeng@huawei.com, dgilbert@redhat.com, zhanghailiang , arei.gonglei@huawei.com, stefanha@redhat.com, amit.shah@redhat.com, zhangchen.fnst@cn.fujitsu.com, hongyang.yang@easystack.cn Subject: [Qemu-devel] [PATCH COLO-Frame v15 09/38] QEMUSizedBuffer: Introduce two help functions for qsb X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Introduce two new QEMUSizedBuffer APIs which will be used by COLO to buffer VM state: One is qsb_put_buffer(), which put the content of a given QEMUSizedBuffer into QEMUFile, this is used to send buffered VM state to secondary. Another is qsb_fill_buffer(), read 'size' bytes of data from the file into qsb, this is used to get VM state from socket into a buffer. Signed-off-by: zhanghailiang Signed-off-by: Li Zhijian Reviewed-by: Dr. David Alan Gilbert --- v11: - size_t'ify these two help functions (Dave's suggestion) --- include/migration/qemu-file.h | 3 ++- migration/qemu-file-buf.c | 61 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h index b5d08d2..ca6a582 100644 --- a/include/migration/qemu-file.h +++ b/include/migration/qemu-file.h @@ -150,7 +150,8 @@ ssize_t qsb_get_buffer(const QEMUSizedBuffer *, off_t start, size_t count, uint8_t *buf); ssize_t qsb_write_at(QEMUSizedBuffer *qsb, const uint8_t *buf, off_t pos, size_t count); - +void qsb_put_buffer(QEMUFile *f, QEMUSizedBuffer *qsb, size_t size); +size_t qsb_fill_buffer(QEMUSizedBuffer *qsb, QEMUFile *f, size_t size); /* * For use on files opened with qemu_bufopen diff --git a/migration/qemu-file-buf.c b/migration/qemu-file-buf.c index 7b8e78e..7801780 100644 --- a/migration/qemu-file-buf.c +++ b/migration/qemu-file-buf.c @@ -367,6 +367,67 @@ ssize_t qsb_write_at(QEMUSizedBuffer *qsb, const uint8_t *source, return count; } +/** + * Put the content of a given QEMUSizedBuffer into QEMUFile. + * + * @f: A QEMUFile + * @qsb: A QEMUSizedBuffer + * @size: size of content to write + */ +void qsb_put_buffer(QEMUFile *f, QEMUSizedBuffer *qsb, size_t size) +{ + size_t l; + int i; + + for (i = 0; i < qsb->n_iov && size > 0; i++) { + l = MIN(qsb->iov[i].iov_len, size); + qemu_put_buffer(f, qsb->iov[i].iov_base, l); + size -= l; + } +} + +/* + * Read 'size' bytes of data from the file into qsb. + * always fill from pos 0 and used after qsb_create(). + * + * It will return size bytes unless there was an error, in which case it will + * return as many as it managed to read (assuming blocking fd's which + * all current QEMUFile are) + */ +size_t qsb_fill_buffer(QEMUSizedBuffer *qsb, QEMUFile *f, size_t size) +{ + ssize_t rc = qsb_grow(qsb, size); + ssize_t pending = size; + int i; + uint8_t *buf = NULL; + + qsb->used = 0; + + if (rc < 0) { + return rc; + } + + for (i = 0; i < qsb->n_iov && pending > 0; i++) { + size_t doneone = 0; + /* read until iov full */ + while (doneone < qsb->iov[i].iov_len && pending > 0) { + size_t readone = 0; + + buf = qsb->iov[i].iov_base; + readone = qemu_get_buffer(f, buf, + MIN(qsb->iov[i].iov_len - doneone, pending)); + if (readone == 0) { + return qsb->used; + } + buf += readone; + doneone += readone; + pending -= readone; + qsb->used += readone; + } + } + return qsb->used; +} + typedef struct QEMUBuffer { QEMUSizedBuffer *qsb; QEMUFile *file;