qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Amit Shah <amit.shah@redhat.com>
To: qemu-devel@nongnu.org
Cc: Amit Shah <amit.shah@redhat.com>
Subject: [Qemu-devel] [PATCH 3/8] virtio-serial-bus: Maintain guest and host port open/close state
Date: Wed, 20 Jan 2010 00:36:53 +0530	[thread overview]
Message-ID: <1263928018-32531-4-git-send-email-amit.shah@redhat.com> (raw)
In-Reply-To: <1263928018-32531-3-git-send-email-amit.shah@redhat.com>

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 <amit.shah@redhat.com>
---
 hw/virtio-serial-bus.c |   94 ++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio-serial.h     |    6 +++
 2 files changed, 100 insertions(+), 0 deletions(-)

diff --git a/hw/virtio-serial-bus.c b/hw/virtio-serial-bus.c
index e8bbd7d..5bf2990 100644
--- a/hw/virtio-serial-bus.c
+++ b/hw/virtio-serial-bus.c
@@ -66,6 +66,11 @@ static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
     return NULL;
 }
 
+static bool use_multiport(VirtIOSerial *vser)
+{
+    return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
+}
+
 static size_t write_to_port(VirtIOSerialPort *port,
                             const uint8_t *buf, size_t size)
 {
@@ -139,11 +144,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;
 }
 
@@ -151,6 +167,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);
 }
 
@@ -167,6 +186,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;
+    }
 
     if (virtqueue_avail_bytes(vq, 4096, 0)) {
         return 4096;
@@ -203,6 +225,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
@@ -213,6 +240,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;
     }
 }
 
@@ -302,6 +342,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);
@@ -310,15 +352,41 @@ 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);
 
@@ -331,6 +399,20 @@ 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;
 }
 
@@ -360,6 +442,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)
@@ -393,6 +479,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.h b/hw/virtio-serial.h
index fe8e357..d9c7acb 100644
--- a/hw/virtio-serial.h
+++ b/hw/virtio-serial.h
@@ -49,6 +49,7 @@ struct virtio_console_control {
 #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 == */
 
@@ -92,6 +93,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 {
-- 
1.6.2.5

  reply	other threads:[~2010-01-19 19:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-19 19:06 [Qemu-devel] [PATCH 0/8] virtio-console: Move to qdev, multiple devices, generic ports Amit Shah
2010-01-19 19:06 ` [Qemu-devel] [PATCH 1/8] virtio: Remove duplicate macro definition for max. virtqueues, bump up the max Amit Shah
2010-01-19 19:06   ` [Qemu-devel] [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus Amit Shah
2010-01-19 19:06     ` Amit Shah [this message]
2010-01-19 19:06       ` [Qemu-devel] [PATCH 4/8] virtio-serial-bus: Add a port 'name' property for port discovery in guests Amit Shah
2010-01-19 19:06         ` [Qemu-devel] [PATCH 5/8] virtio-serial-bus: Add ability to hot-unplug ports Amit Shah
2010-01-19 19:06           ` [Qemu-devel] [PATCH 6/8] virtio-serial: Add a 'virtserialport' device for generic serial port support Amit Shah
2010-01-19 19:06             ` [Qemu-devel] [PATCH 7/8] Move virtio-serial to Makefile.objs Amit Shah
2010-01-19 19:06               ` [Qemu-devel] [PATCH 8/8] virtio-serial: Use MSI vectors for port virtqueues Amit Shah
2010-01-20 14:56   ` [Qemu-devel] [PATCH 1/8] virtio: Remove duplicate macro definition for max. virtqueues, bump up the max Anthony Liguori
2010-01-21  9:53     ` Markus Armbruster
2010-01-21  9:58       ` Amit Shah
  -- strict thread matches above, loose matches on Subject: below --
2010-01-14 13:17 [Qemu-devel] [PATCH 0/8] virtio-console: Move to qdev, multiple devices, generic ports Amit Shah
2010-01-14 13:17 ` [Qemu-devel] [PATCH 1/8] virtio: Remove duplicate macro definition for max. virtqueues, bump up the max Amit Shah
2010-01-14 13:17   ` [Qemu-devel] [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus Amit Shah
2010-01-14 13:17     ` [Qemu-devel] [PATCH 3/8] virtio-serial-bus: Maintain guest and host port open/close state Amit Shah
2010-01-07  7:31 [Qemu-devel] [PATCH 0/8] virtio-console: Move to qdev, multiple devices, generic ports Amit Shah
2010-01-07  7:31 ` [Qemu-devel] [PATCH 1/8] virtio: Remove duplicate macro definition for max. virtqueues, bump up the max Amit Shah
2010-01-07  7:31   ` [Qemu-devel] [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus Amit Shah
2010-01-07  7:31     ` [Qemu-devel] [PATCH 3/8] virtio-serial-bus: Maintain guest and host port open/close state Amit Shah
2010-01-04 17:34 [Qemu-devel] [PATCH 0/8] virtio-console: Move to qdev, multiple devices, generic ports Amit Shah
2010-01-04 17:34 ` [Qemu-devel] [PATCH 1/8] virtio: Remove duplicate macro definition for max. virtqueues, bump up the max Amit Shah
2010-01-04 17:34   ` [Qemu-devel] [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus Amit Shah
2010-01-04 17:34     ` [Qemu-devel] [PATCH 3/8] virtio-serial-bus: Maintain guest and host port open/close state Amit Shah

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=1263928018-32531-4-git-send-email-amit.shah@redhat.com \
    --to=amit.shah@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).