qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Luiz Capitulino <lcapitulino@redhat.com>
To: qemu-devel@nongnu.org
Cc: aliguori@us.ibm.com, armbru@redhat.com
Subject: [Qemu-devel] [PATCH 1/3] qemu-char: Introduce Buffered driver
Date: Fri, 29 Oct 2010 12:28:32 -0200	[thread overview]
Message-ID: <1288362514-31407-2-git-send-email-lcapitulino@redhat.com> (raw)
In-Reply-To: <1288362514-31407-1-git-send-email-lcapitulino@redhat.com>

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 <lcapitulino@redhat.com>
---
 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,
-- 
1.7.3.2.145.g7ebee

  reply	other threads:[~2010-10-29 14:28 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-29 14:28 [Qemu-devel] [PATCH v1 0/3]: QMP: Human Monitor passthrough Luiz Capitulino
2010-10-29 14:28 ` Luiz Capitulino [this message]
2010-10-29 14:28 ` [Qemu-devel] [PATCH 2/3] QMP: Introduce Human Monitor passthrough command Luiz Capitulino
2010-11-10 13:20   ` Markus Armbruster
2010-11-10 13:36     ` Luiz Capitulino
2010-11-11 15:47       ` Markus Armbruster
2010-11-11 15:55         ` Daniel P. Berrange
2010-11-11 16:30           ` Anthony Liguori
2010-11-11 16:39             ` Daniel P. Berrange
2010-11-11 16:47               ` Luiz Capitulino
2010-11-11 16:58               ` Anthony Liguori
2010-11-11 16:55         ` Luiz Capitulino
2010-11-11 16:59           ` Anthony Liguori
2010-11-11 17:45           ` Markus Armbruster
2010-10-29 14:28 ` [Qemu-devel] [PATCH 3/3] QMP/qmp-shell: Introduce HMP mode Luiz Capitulino
2010-11-10 13:22   ` Markus Armbruster
2010-11-11 16:22 ` [Qemu-devel] [PATCH v1 0/3]: QMP: Human Monitor passthrough Avi Kivity
2010-11-11 16:24   ` Luiz Capitulino

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1288362514-31407-2-git-send-email-lcapitulino@redhat.com \
    --to=lcapitulino@redhat.com \
    --cc=aliguori@us.ibm.com \
    --cc=armbru@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).