From patchwork Mon Oct 25 20:06:43 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 69137 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 12FA3B6EF1 for ; Tue, 26 Oct 2010 07:08:32 +1100 (EST) Received: from localhost ([127.0.0.1]:34286 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PATLJ-0004y6-1K for incoming@patchwork.ozlabs.org; Mon, 25 Oct 2010 16:08:29 -0400 Received: from [140.186.70.92] (port=36743 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PATJp-0004uf-6M for qemu-devel@nongnu.org; Mon, 25 Oct 2010 16:06:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PATJn-0004vY-R1 for qemu-devel@nongnu.org; Mon, 25 Oct 2010 16:06:56 -0400 Received: from mx1.redhat.com ([209.132.183.28]:65322) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PATJn-0004vK-IV for qemu-devel@nongnu.org; Mon, 25 Oct 2010 16:06:55 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o9PK6tVh026288 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 25 Oct 2010 16:06:55 -0400 Received: from localhost (ovpn-113-92.phx2.redhat.com [10.3.113.92]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o9PK6sBl016147; Mon, 25 Oct 2010 16:06:54 -0400 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Mon, 25 Oct 2010 18:06:43 -0200 Message-Id: <1288037204-27768-2-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1288037204-27768-1-git-send-email-lcapitulino@redhat.com> References: <1288037204-27768-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: aliguori@us.ibm.com, armbru@redhat.com Subject: [Qemu-devel] [PATCH 1/2] qemu-char: Introduce Buffered driver X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This driver handles in-memory qemu-char driver operations, that's, all writes to this driver are stored in an internal buffer. The driver doesn't talk to the external world in any way. Right now it's very simple: it supports only writes. But it can be easily extended to support more operations. This driver is going to be used by the monitor subsystem, which needs to 'emulate' a qemu-char device, so that monitor handlers can be ran without a backing device and have their output stored. TODO: Move buffer growth logic to cutils. Signed-off-by: Luiz Capitulino --- qemu-char.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-char.h | 6 +++++ 2 files changed, 74 insertions(+), 0 deletions(-) diff --git a/qemu-char.c b/qemu-char.c index 6d2dce7..1ca9ccc 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -2279,6 +2279,74 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) return NULL; } +/***********************************************************/ +/* Buffered chardev */ +typedef struct { + size_t outbuf_size; + size_t outbuf_capacity; + uint8_t *outbuf; +} BufferedDriver; + +static int buffered_chr_write(CharDriverState *chr, const uint8_t *buf, int len) +{ + BufferedDriver *d = chr->opaque; + + if (d->outbuf_capacity < (d->outbuf_size + len)) { + /* grow outbuf */ + d->outbuf_capacity += len; + d->outbuf_capacity *= 2; + d->outbuf = qemu_realloc(d->outbuf, d->outbuf_capacity); + } + + memcpy(d->outbuf + d->outbuf_size, buf, len); + d->outbuf_size += len; + + return 0; +} + +void qemu_chr_init_buffered(CharDriverState *chr) +{ + BufferedDriver *d; + + d = qemu_mallocz(sizeof(BufferedDriver)); + d->outbuf_capacity = 4096; + d->outbuf = qemu_mallocz(d->outbuf_capacity); + + memset(chr, 0, sizeof(*chr)); + chr->opaque = d; + chr->chr_write = buffered_chr_write; +} + +QString *qemu_chr_buffered_to_qs(CharDriverState *chr) +{ + char *str; + QString *qs; + BufferedDriver *d = chr->opaque; + + if (d->outbuf_size == 0) { + return NULL; + } + + str = qemu_malloc(d->outbuf_size + 1); + memcpy(str, d->outbuf, d->outbuf_size); + str[d->outbuf_size] = '\0'; + + qs = qstring_from_str(str); + qemu_free(str); + + return qs; +} + +void qemu_chr_close_buffered(CharDriverState *chr) +{ + BufferedDriver *d = chr->opaque; + + qemu_free(d->outbuf); + qemu_free(chr->opaque); + chr->opaque = NULL; + chr->chr_write = NULL; +} + QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) { char host[65], port[33], width[8], height[8]; diff --git a/qemu-char.h b/qemu-char.h index 18ad12b..4467641 100644 --- a/qemu-char.h +++ b/qemu-char.h @@ -6,6 +6,7 @@ #include "qemu-option.h" #include "qemu-config.h" #include "qobject.h" +#include "qstring.h" /* character device */ @@ -100,6 +101,11 @@ CharDriverState *qemu_chr_open_eventfd(int eventfd); extern int term_escape_char; +/* buffered chardev */ +void qemu_chr_init_buffered(CharDriverState *chr); +void qemu_chr_close_buffered(CharDriverState *chr); +QString *qemu_chr_buffered_to_qs(CharDriverState *chr); + /* async I/O support */ int qemu_set_fd_handler2(int fd,