From patchwork Thu Jan 14 13:17:38 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Shah X-Patchwork-Id: 42889 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 9A7C4B7CBB for ; Fri, 15 Jan 2010 01:07:26 +1100 (EST) Received: from localhost ([127.0.0.1]:38810 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NVQM7-0002SJ-7e for incoming@patchwork.ozlabs.org; Thu, 14 Jan 2010 09:07:23 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NVQ4R-0002kd-4Q for qemu-devel@nongnu.org; Thu, 14 Jan 2010 08:49:07 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NVQ4M-0002jM-TK for qemu-devel@nongnu.org; Thu, 14 Jan 2010 08:49:06 -0500 Received: from [199.232.76.173] (port=59849 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NVQ4M-0002jA-EA for qemu-devel@nongnu.org; Thu, 14 Jan 2010 08:49:02 -0500 Received: from mx20.gnu.org ([199.232.41.8]:15641) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NVPyB-00087X-9M for qemu-devel@nongnu.org; Thu, 14 Jan 2010 08:42:40 -0500 Received: from mx1.redhat.com ([209.132.183.28]) by mx20.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NVPbR-0002bM-HA for qemu-devel@nongnu.org; Thu, 14 Jan 2010 08:19:09 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o0EDJ0lj021659 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 14 Jan 2010 08:19:00 -0500 Received: from localhost (vpn-246-68.phx2.redhat.com [10.3.246.68]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o0EDIwhC021709; Thu, 14 Jan 2010 08:18:59 -0500 From: Amit Shah To: qemu-devel@nongnu.org Date: Thu, 14 Jan 2010 18:47:38 +0530 Message-Id: <1263475063-15238-4-git-send-email-amit.shah@redhat.com> In-Reply-To: <1263475063-15238-3-git-send-email-amit.shah@redhat.com> References: <1263475063-15238-1-git-send-email-amit.shah@redhat.com> <1263475063-15238-2-git-send-email-amit.shah@redhat.com> <1263475063-15238-3-git-send-email-amit.shah@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by mx20.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Amit Shah Subject: [Qemu-devel] [PATCH 3/8] virtio-serial-bus: Maintain guest and host port open/close state 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 Via control channel messages, the guest can tell us whether a port got opened or closed. Similarly, we can also indicate to the guest of host port open/close events. Signed-off-by: Amit Shah --- hw/virtio-serial-bus.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-serial.c | 6 +++ hw/virtio-serial.h | 6 +++ 3 files changed, 103 insertions(+), 0 deletions(-) diff --git a/hw/virtio-serial-bus.c b/hw/virtio-serial-bus.c index fcb86a6..276de6a 100644 --- a/hw/virtio-serial-bus.c +++ b/hw/virtio-serial-bus.c @@ -160,11 +160,22 @@ static size_t send_control_event(VirtIOSerialPort *port, uint16_t event, /* Functions for use inside qemu to open and read from/write to ports */ int virtio_serial_open(VirtIOSerialPort *port) { + /* Don't allow opening an already-open port */ + if (port->host_connected) { + return 0; + } + /* Send port open notification to the guest */ + port->host_connected = true; + send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1); + return 0; } int virtio_serial_close(VirtIOSerialPort *port) { + port->host_connected = false; + send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0); + return 0; } @@ -172,6 +183,9 @@ int virtio_serial_close(VirtIOSerialPort *port) ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf, size_t size) { + if (!port || !port->host_connected || !port->guest_connected) { + return 0; + } return write_to_port(port, buf, size); } @@ -192,6 +206,9 @@ size_t virtio_serial_guest_ready(VirtIOSerialPort *port) virtio_queue_empty(vq)) { return 0; } + if (use_multiport(port->vser) && !port->guest_connected) { + return 0; + } size = 4096; if (virtqueue_avail_bytes(vq, size, 0)) { @@ -230,6 +247,11 @@ static void handle_control_message(VirtIOSerial *vser, void *buf) if (port->is_console) { send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1); } + + if (port->host_connected) { + send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1); + } + /* * When the guest has asked us for this information it means * the guest is all setup and has its virtqueues @@ -240,6 +262,19 @@ static void handle_control_message(VirtIOSerial *vser, void *buf) port->info->guest_ready(port); } break; + + case VIRTIO_CONSOLE_PORT_OPEN: + port->guest_connected = cpkt.value; + if (cpkt.value && port->info->guest_open) { + /* Send the guest opened notification if an app is interested */ + port->info->guest_open(port); + } + + if (!cpkt.value && port->info->guest_close) { + /* Send the guest closed notification if an app is interested */ + port->info->guest_close(port); + } + break; } } @@ -334,6 +369,8 @@ static void set_config(VirtIODevice *vdev, const uint8_t *config_data) static void virtio_serial_save(QEMUFile *f, void *opaque) { VirtIOSerial *s = opaque; + VirtIOSerialPort *port; + uint32_t nr_active_ports; /* The virtio device */ virtio_save(&s->vdev, f); @@ -342,15 +379,42 @@ static void virtio_serial_save(QEMUFile *f, void *opaque) qemu_put_be16s(f, &s->config.cols); qemu_put_be16s(f, &s->config.rows); qemu_put_be32s(f, &s->config.nr_ports); + + /* Items in struct VirtIOSerial */ + + /* Do this because we might have hot-unplugged some ports */ + nr_active_ports = 0; + QTAILQ_FOREACH(port, &s->ports, next) + nr_active_ports++; + + qemu_put_be32s(f, &nr_active_ports); + + /* + * Items in struct VirtIOSerialPort. + */ + QTAILQ_FOREACH(port, &s->ports, next) { + /* + * We put the port number because we may not have an active + * port at id 0 that's reserved for a console port, or in case + * of ports that might have gotten unplugged + */ + qemu_put_be32s(f, &port->id); + qemu_put_byte(f, port->guest_connected); + + } } static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id) { VirtIOSerial *s = opaque; + VirtIOSerialPort *port; + uint32_t nr_active_ports; + unsigned int i; if (version_id > 2) { return -EINVAL; } + /* The virtio device */ virtio_load(&s->vdev, f); @@ -363,6 +427,21 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id) qemu_get_be16s(f, &s->config.rows); s->config.nr_ports = qemu_get_be32(f); + /* Items in struct VirtIOSerial */ + + qemu_get_be32s(f, &nr_active_ports); + + /* Items in struct VirtIOSerialPort */ + for (i = 0; i < nr_active_ports; i++) { + uint32_t id; + + id = qemu_get_be32(f); + port = find_port_by_id(s, id); + + port->guest_connected = qemu_get_byte(f); + + } + return 0; } @@ -392,6 +471,10 @@ static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) monitor_printf(mon, "%*s dev-prop-int: id: %u\n", indent, "", port->id); + monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n", + indent, "", port->guest_connected); + monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n", + indent, "", port->host_connected); } static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base) @@ -425,6 +508,14 @@ static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base) port->id = plugging_port0 ? 0 : port->vser->config.nr_ports++; + if (!use_multiport(port->vser)) { + /* + * Allow writes to guest in this case; we have no way of + * knowing if a guest port is connected. + */ + port->guest_connected = true; + } + QTAILQ_INSERT_TAIL(&port->vser->ports, port, next); port->ivq = port->vser->ivqs[port->id]; port->ovq = port->vser->ovqs[port->id]; diff --git a/hw/virtio-serial.c b/hw/virtio-serial.c index 9d003f8..d9a6f32 100644 --- a/hw/virtio-serial.c +++ b/hw/virtio-serial.c @@ -68,6 +68,12 @@ static int virtconsole_initfn(VirtIOSerialDevice *dev) port->is_console = true; + /* + * For console ports, just assume the guest is ready to accept our + * data. + */ + port->guest_connected = true; + if (vcon->chr) { qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, vcon); diff --git a/hw/virtio-serial.h b/hw/virtio-serial.h index 9aac856..1576eef 100644 --- a/hw/virtio-serial.h +++ b/hw/virtio-serial.h @@ -53,6 +53,7 @@ struct virtio_console_header { #define VIRTIO_CONSOLE_PORT_READY 0 #define VIRTIO_CONSOLE_CONSOLE_PORT 1 #define VIRTIO_CONSOLE_RESIZE 2 +#define VIRTIO_CONSOLE_PORT_OPEN 3 /* == In-qemu interface == */ @@ -96,6 +97,11 @@ struct VirtIOSerialPort { /* Identify if this is a port that binds with hvc in the guest */ uint8_t is_console; + + /* Is the corresponding guest device open? */ + bool guest_connected; + /* Is this device open for IO on the host? */ + bool host_connected; }; struct VirtIOSerialPortInfo {