From patchwork Wed Nov 10 18:59:04 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 70677 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 E38F3B7104 for ; Thu, 11 Nov 2010 06:00:32 +1100 (EST) Received: from localhost ([127.0.0.1]:53608 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PGFuI-00060O-7Q for incoming@patchwork.ozlabs.org; Wed, 10 Nov 2010 14:00:30 -0500 Received: from [140.186.70.92] (port=35522 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PGFtF-0005ye-51 for qemu-devel@nongnu.org; Wed, 10 Nov 2010 13:59:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PGFtD-0003XZ-KI for qemu-devel@nongnu.org; Wed, 10 Nov 2010 13:59:25 -0500 Received: from mx1.redhat.com ([209.132.183.28]:5827) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PGFtD-0003X4-Cd for qemu-devel@nongnu.org; Wed, 10 Nov 2010 13:59:23 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id oAAIxMUJ007000 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 10 Nov 2010 13:59:22 -0500 Received: from localhost (ovpn-113-58.phx2.redhat.com [10.3.113.58]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id oAAIxLp5031003; Wed, 10 Nov 2010 13:59:21 -0500 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Wed, 10 Nov 2010 16:59:04 -0200 Message-Id: <1289415546-19105-2-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1289415546-19105-1-git-send-email-lcapitulino@redhat.com> References: <1289415546-19105-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 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/3] qemu-char: Introduce Memory 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 chardev operations. That's, all writes to this driver are stored in an internal buffer and it 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 is going to be used by the monitor's "HMP passthrough via QMP" feature, which needs to run monitor handlers without a backing device. Signed-off-by: Luiz Capitulino --- qemu-char.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-char.h | 6 +++++ 2 files changed, 72 insertions(+), 0 deletions(-) diff --git a/qemu-char.c b/qemu-char.c index 88997f9..896df14 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -2275,6 +2275,72 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) return NULL; } +/***********************************************************/ +/* Memory chardev */ +typedef struct { + size_t outbuf_size; + size_t outbuf_capacity; + uint8_t *outbuf; +} MemoryDriver; + +static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len) +{ + MemoryDriver *d = chr->opaque; + + /* TODO: the QString implementation has the same code, we should + * introduce a generic way to do this in cutils.c */ + if (d->outbuf_capacity < d->outbuf_size + len) { + /* grown 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 len; +} + +#define DEFAULT_BUF_SIZE 4096 + +void qemu_chr_init_mem(CharDriverState *chr) +{ + MemoryDriver *d; + + d = qemu_malloc(sizeof(*d)); + d->outbuf_size = 0; + d->outbuf_capacity = DEFAULT_BUF_SIZE; + d->outbuf = qemu_mallocz(d->outbuf_capacity); + + memset(chr, 0, sizeof(*chr)); + chr->opaque = d; + chr->chr_write = mem_chr_write; +} + +/* assumes the stored data is a string */ +QString *qemu_chr_mem_to_qs(CharDriverState *chr) +{ + MemoryDriver *d = chr->opaque; + + if (d->outbuf_size == 0) { + return NULL; + } + + return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1); +} + +/* NOTE: this driver can not be closed with qemu_chr_close()! */ +void qemu_chr_close_mem(CharDriverState *chr) +{ + MemoryDriver *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..c4e55b4 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; +/* memory chardev */ +void qemu_chr_init_mem(CharDriverState *chr); +void qemu_chr_close_mem(CharDriverState *chr); +QString *qemu_chr_mem_to_qs(CharDriverState *chr); + /* async I/O support */ int qemu_set_fd_handler2(int fd,