From patchwork Tue Sep 22 16:23:47 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Shah X-Patchwork-Id: 34091 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 08692B7B65 for ; Wed, 23 Sep 2009 02:33:05 +1000 (EST) Received: from localhost ([127.0.0.1]:34348 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mq8IV-0003RG-29 for incoming@patchwork.ozlabs.org; Tue, 22 Sep 2009 12:32:59 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mq8AV-0000hY-Iz for qemu-devel@nongnu.org; Tue, 22 Sep 2009 12:24:43 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mq8AQ-0000fM-OZ for qemu-devel@nongnu.org; Tue, 22 Sep 2009 12:24:43 -0400 Received: from [199.232.76.173] (port=40675 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mq8AQ-0000f8-Gf for qemu-devel@nongnu.org; Tue, 22 Sep 2009 12:24:38 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42755) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Mq8AP-0002he-Ow for qemu-devel@nongnu.org; Tue, 22 Sep 2009 12:24:38 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n8MGObcF011018 for ; Tue, 22 Sep 2009 12:24:37 -0400 Received: from localhost (vpn-12-12.rdu.redhat.com [10.11.12.12]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n8MGOWM5028126; Tue, 22 Sep 2009 12:24:34 -0400 From: Amit Shah To: qemu-devel@nongnu.org Date: Tue, 22 Sep 2009 21:53:47 +0530 Message-Id: <1253636627-12746-5-git-send-email-amit.shah@redhat.com> In-Reply-To: <1253636627-12746-4-git-send-email-amit.shah@redhat.com> References: <1253636627-12746-1-git-send-email-amit.shah@redhat.com> <1253636627-12746-2-git-send-email-amit.shah@redhat.com> <1253636627-12746-3-git-send-email-amit.shah@redhat.com> <1253636627-12746-4-git-send-email-amit.shah@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Amit Shah Subject: [Qemu-devel] [PATCH 4/4] virtio-console: Add a in-qemu api for open/read/write/close ports 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 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 --- hw/virtio-console.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++-- hw/virtio-console.h | 5 ++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/hw/virtio-console.c b/hw/virtio-console.c index d750c8e..c41f13c 100644 --- a/hw/virtio-console.c +++ b/hw/virtio-console.c @@ -58,6 +58,11 @@ struct VirtIOConsolePort { QTAILQ_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; }; @@ -128,10 +133,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) @@ -443,6 +453,57 @@ 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(virtio_console, 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_active_in_map(uint32_t *map, uint32_t idx) { int i; diff --git a/hw/virtio-console.h b/hw/virtio-console.h index 526713e..5f0f549 100644 --- a/hw/virtio-console.h +++ b/hw/virtio-console.h @@ -88,5 +88,10 @@ VirtConBus *virtcon_bus_new(DeviceState *dev); 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