From patchwork Wed Feb 11 03:06:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Liang Z" X-Patchwork-Id: 438655 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 0E7D51401D0 for ; Wed, 11 Feb 2015 14:15:56 +1100 (AEDT) Received: from localhost ([::1]:42858 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YLNmM-0005cH-46 for incoming@patchwork.ozlabs.org; Tue, 10 Feb 2015 22:15:54 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40864) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YLNlV-0004In-FI for qemu-devel@nongnu.org; Tue, 10 Feb 2015 22:15:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YLNlS-0000GM-9W for qemu-devel@nongnu.org; Tue, 10 Feb 2015 22:15:01 -0500 Received: from mga11.intel.com ([192.55.52.93]:60077) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YLNlS-0000GB-3s for qemu-devel@nongnu.org; Tue, 10 Feb 2015 22:14:58 -0500 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga102.fm.intel.com with ESMTP; 10 Feb 2015 19:14:57 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.09,555,1418112000"; d="scan'208";a="650402873" Received: from lil.sh.intel.com (HELO localhost) ([10.239.159.167]) by orsmga001.jf.intel.com with ESMTP; 10 Feb 2015 19:14:55 -0800 From: Liang Li To: qemu-devel@nongnu.org Date: Wed, 11 Feb 2015 11:06:18 +0800 Message-Id: <1423623986-590-5-git-send-email-liang.z.li@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1423623986-590-1-git-send-email-liang.z.li@intel.com> References: <1423623986-590-1-git-send-email-liang.z.li@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 Cc: quintela@redhat.com, Liang Li , armbru@redhat.com, lcapitulino@redhat.com, Yang Zhang , amit.shah@redhat.com, dgilbert@redhat.com Subject: [Qemu-devel] [v5 04/12] qemu-file: Add compression functions to QEMUFile 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org qemu_put_compression_data() compress the data and put it to QEMUFile. qemu_put_qemu_file() put the data in the buffer of source QEMUFile to destination QEMUFile. Signed-off-by: Liang Li Signed-off-by: Yang Zhang Reviewed-by: Juan Quintela --- include/migration/qemu-file.h | 3 +++ migration/qemu-file.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h index a923cec..52192ad 100644 --- a/include/migration/qemu-file.h +++ b/include/migration/qemu-file.h @@ -160,6 +160,9 @@ void qemu_put_be32(QEMUFile *f, unsigned int v); void qemu_put_be64(QEMUFile *f, uint64_t v); int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset); int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size); +ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size, + int level); +int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src); /* * Note that you can only peek continuous bytes from where the current pointer * is; you aren't guaranteed to be able to peak to +n bytes unless you've diff --git a/migration/qemu-file.c b/migration/qemu-file.c index e66e557..3e54f6d 100644 --- a/migration/qemu-file.c +++ b/migration/qemu-file.c @@ -21,6 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ +#include #include "qemu-common.h" #include "qemu/iov.h" #include "qemu/sockets.h" @@ -545,3 +546,41 @@ uint64_t qemu_get_be64(QEMUFile *f) v |= qemu_get_be32(f); return v; } + +/* compress size bytes of data start at p with specific compression + * level and store the compressed data to the buffer of f. + */ + +ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size, + int level) +{ + ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t); + + if (blen < compressBound(size)) { + return 0; + } + if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen, + (Bytef *)p, size, level) != Z_OK) { + error_report("Compress Failed!"); + return 0; + } + qemu_put_be32(f, blen); + f->buf_index += blen; + return blen + sizeof(int32_t); +} + +/* Put the data in the buffer of f_src to the buffer of f_des, and + * then reset the buf_index of f_src to 0. + */ + +int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) +{ + int len = 0; + + if (f_src->buf_index > 0) { + len = f_src->buf_index; + qemu_put_buffer(f_des, f_src->buf, f_src->buf_index); + f_src->buf_index = 0; + } + return len; +}