From patchwork Wed Sep 9 08:12:03 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Shah X-Patchwork-Id: 46331 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n898D0p4017260 for ; Wed, 9 Sep 2009 08:13:00 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752727AbZIIIMw (ORCPT ); Wed, 9 Sep 2009 04:12:52 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1752662AbZIIIMv (ORCPT ); Wed, 9 Sep 2009 04:12:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51163 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751996AbZIIIMt (ORCPT ); Wed, 9 Sep 2009 04:12:49 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n898CmRn015607; Wed, 9 Sep 2009 04:12:49 -0400 Received: from localhost (vpn-11-236.rdu.redhat.com [10.11.11.236]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n898CkIR029155; Wed, 9 Sep 2009 04:12:47 -0400 From: Amit Shah To: qemu-devel@nongnu.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Cc: Amit Shah Subject: [PATCH 3/5] virtio-console: in-qemu api for open/read/write/close ports Date: Wed, 9 Sep 2009 13:42:03 +0530 Message-Id: <1252483925-26336-5-git-send-email-amit.shah@redhat.com> In-Reply-To: <1252483925-26336-4-git-send-email-amit.shah@redhat.com> References: <1252483925-26336-1-git-send-email-amit.shah@redhat.com> <1252483925-26336-2-git-send-email-amit.shah@redhat.com> <1252483925-26336-3-git-send-email-amit.shah@redhat.com> <1252483925-26336-4-git-send-email-amit.shah@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This is a simple-to-use api for opening a port, registering a callback for reading stuff, writing to a port and closing it. Another api for hot-adding a port can be provided. Signed-off-by: Amit Shah --- hw/virtio-console.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++-- hw/virtio-console.h | 5 ++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/hw/virtio-console.c b/hw/virtio-console.c index 135fdbb..9e78a9e 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,51 @@ 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 = get_port_from_id(id); + struct virtio_console_control cpkt; + + if (port == NULL) { + return NULL; + } + /* Don't allow opening an already-open port */ + if (port->host_connected) { + return NULL; + } + /* Send port open notification to the guest */ + port->host_connected = true; + port->read_callback = read_callback; + 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 virtio_console_set_port_active(uint32_t idx) { int i; diff --git a/hw/virtio-console.h b/hw/virtio-console.h index 56448a9..62d0c4b 100644 --- a/hw/virtio-console.h +++ b/hw/virtio-console.h @@ -68,5 +68,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