From patchwork Fri Oct 29 14:28:32 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 69599 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 25F57B70DC for ; Sat, 30 Oct 2010 01:41:38 +1100 (EST) Received: from localhost ([127.0.0.1]:37461 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PBq85-0004AO-Va for incoming@patchwork.ozlabs.org; Fri, 29 Oct 2010 10:40:30 -0400 Received: from [140.186.70.92] (port=50269 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PBpwh-00052n-J7 for qemu-devel@nongnu.org; Fri, 29 Oct 2010 10:28:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PBpwf-0006sW-IN for qemu-devel@nongnu.org; Fri, 29 Oct 2010 10:28:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44881) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PBpwf-0006sA-Br for qemu-devel@nongnu.org; Fri, 29 Oct 2010 10:28:41 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o9TESeMD003122 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 29 Oct 2010 10:28:40 -0400 Received: from localhost (ovpn-113-85.phx2.redhat.com [10.3.113.85]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id o9TESdxa013208; Fri, 29 Oct 2010 10:28:40 -0400 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Fri, 29 Oct 2010 12:28:32 -0200 Message-Id: <1288362514-31407-2-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1288362514-31407-1-git-send-email-lcapitulino@redhat.com> References: <1288362514-31407-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 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 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 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 | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-char.h | 6 +++++ 2 files changed, 69 insertions(+), 0 deletions(-) diff --git a/qemu-char.c b/qemu-char.c index 6d2dce7..a4bd09c 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -2279,6 +2279,69 @@ 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)) { + /* 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_buffered(CharDriverState *chr) +{ + BufferedDriver *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 = buffered_chr_write; +} + +/* assumes the stored data is a string */ +QString *qemu_chr_buffered_to_qs(CharDriverState *chr) +{ + BufferedDriver *d = chr->opaque; + + if (d->outbuf_size == 0) { + return NULL; + } + + return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1); +} + +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,