Patchwork [3/5] virtio-console: Add a in-qemu api for open/read/write/close ports

login
register
mail settings
Submitter Amit Shah
Date Sept. 11, 2009, 3:52 p.m.
Message ID <1252684353-25067-4-git-send-email-amit.shah@redhat.com>
Download mbox | patch
Permalink /patch/33473/
State Superseded
Headers show

Comments

Amit Shah - Sept. 11, 2009, 3:52 p.m.
This is a simple-to-use api for opening a port, registering
a callback for read, writing to a port and closing it.

Signed-off-by: Amit Shah <amit.shah@redhat.com>
---
 hw/virtio-console.c |   65 ++++++++++++++++++++++++++++++++++++++++++++++++--
 hw/virtio-console.h |    5 ++++
 2 files changed, 67 insertions(+), 3 deletions(-)

Patch

diff --git a/hw/virtio-console.c b/hw/virtio-console.c
index 9fa35ec..dce7191 100644
--- a/hw/virtio-console.c
+++ b/hw/virtio-console.c
@@ -52,6 +52,11 @@  struct VirtIOConsolePort {
 
     TAILQ_HEAD(, VirtIOConsolePortBuffer) unflushed_buffer_head;
 
+    /* Callback that's invoked when we have a buffer that can be consumed
+     * by an in-qemu user of this port
+     */
+    size_t (*read_callback)(const uint8_t *buf, const size_t len);
+
     bool guest_connected;
     bool host_connected;
 };
@@ -150,10 +155,15 @@  static bool has_complete_data(VirtIOConsolePort *port)
 
 static size_t flush_buf(VirtIOConsolePort *port, const uint8_t *buf, size_t len)
 {
-    if (!port->hd) {
-        return 0;
+    int ret;
+
+    ret = 0;
+    if (port->read_callback) {
+        ret = port->read_callback(buf, len);
+    } else if (port->hd) {
+        ret = qemu_chr_write(port->hd, buf, len);
     }
-    return qemu_chr_write(port->hd, buf, len);
+    return ret;
 }
 
 static void flush_queue(VirtIOConsolePort *port)
@@ -431,6 +441,55 @@  static void vcon_event(void *opaque, int event)
     send_control_event(port, &cpkt);
 }
 
+/* Functions for use inside qemu to open and read from/write to ports */
+VirtIOConsolePort *virtio_console_open(uint32_t id,
+                                       size_t(*read_callback)(const uint8_t*buf,
+                                                              const size_t len))
+{
+    VirtIOConsolePort *port;
+    struct virtio_console_control cpkt;
+
+    port = get_port_from_id(id);
+    if (!port) {
+        return NULL;
+    }
+    /* Don't allow opening an already-open port */
+    if (port->host_connected) {
+        return NULL;
+    }
+    port->read_callback = read_callback;
+
+    /* Send port open notification to the guest */
+    port->host_connected = true;
+    cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
+    cpkt.value = 1;
+    send_control_event(port, &cpkt);
+
+    return port;
+}
+
+void virtio_console_close(VirtIOConsolePort *port)
+{
+    struct virtio_console_control cpkt;
+
+    if (!port)
+        return;
+
+    port->read_callback = NULL;
+
+    cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
+    cpkt.value = 0;
+    send_control_event(port, &cpkt);
+}
+
+size_t virtio_console_write(VirtIOConsolePort *port, uint8_t *buf, size_t size)
+{
+    if (!port || !port->host_connected) {
+        return 0;
+    }
+    return write_to_port(port, buf, size, false);
+}
+
 static void set_port_active(uint32_t *map, uint32_t idx)
 {
     int i;
diff --git a/hw/virtio-console.h b/hw/virtio-console.h
index 9bf6aaf..b06297e 100644
--- a/hw/virtio-console.h
+++ b/hw/virtio-console.h
@@ -70,5 +70,10 @@  struct virtio_console_header {
 typedef struct VirtIOConsolePort VirtIOConsolePort;
 void virtio_console_monitor_command(Monitor *mon,
                                     const char *command, const char *param);
+VirtIOConsolePort *virtio_console_open(uint32_t id,
+                                       size_t(*read_callback)(const uint8_t*buf,
+                                                              const size_t len));
+void virtio_console_close(VirtIOConsolePort *port);
+size_t virtio_console_write(VirtIOConsolePort *port, uint8_t *buf, size_t size);
 
 #endif