render / rpms / qemu

Forked from rpms/qemu 9 months ago
Clone

Blame qemu-virtio-serial-bus-Maintain-guest-and-host-port-open.patch

amitshah 99a9ef
From 4945aee74f494cc8a17ce1634b5200eb9ee227d2 Mon Sep 17 00:00:00 2001
amitshah 99a9ef
From: Amit Shah <amit.shah@redhat.com>
amitshah 99a9ef
Date: Wed, 20 Jan 2010 00:36:53 +0530
amitshah 99a9ef
Subject: [PATCH 3/9] virtio-serial-bus: Maintain guest and host port open/close state
amitshah 99a9ef
amitshah 99a9ef
Via control channel messages, the guest can tell us whether a port got
amitshah 99a9ef
opened or closed. Similarly, we can also indicate to the guest of host
amitshah 99a9ef
port open/close events.
amitshah 99a9ef
amitshah 99a9ef
Signed-off-by: Amit Shah <amit.shah@redhat.com>
amitshah 99a9ef
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
amitshah 99a9ef
---
amitshah 99a9ef
 hw/virtio-serial-bus.c |   94 ++++++++++++++++++++++++++++++++++++++++++++++++
amitshah 99a9ef
 hw/virtio-serial.h     |    6 +++
amitshah 99a9ef
 2 files changed, 100 insertions(+), 0 deletions(-)
amitshah 99a9ef
amitshah 99a9ef
diff --git a/hw/virtio-serial-bus.c b/hw/virtio-serial-bus.c
amitshah 99a9ef
index 5132c9c..9af21df 100644
amitshah 99a9ef
--- a/hw/virtio-serial-bus.c
amitshah 99a9ef
+++ b/hw/virtio-serial-bus.c
amitshah 99a9ef
@@ -66,6 +66,11 @@ static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
amitshah 99a9ef
     return NULL;
amitshah 99a9ef
 }
amitshah 99a9ef
 
amitshah 99a9ef
+static bool use_multiport(VirtIOSerial *vser)
amitshah 99a9ef
+{
amitshah 99a9ef
+    return vser->vdev.features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
amitshah 99a9ef
+}
amitshah 99a9ef
+
amitshah 99a9ef
 static size_t write_to_port(VirtIOSerialPort *port,
amitshah 99a9ef
                             const uint8_t *buf, size_t size)
amitshah 99a9ef
 {
amitshah 99a9ef
@@ -139,11 +144,22 @@ static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
amitshah 99a9ef
 /* Functions for use inside qemu to open and read from/write to ports */
amitshah 99a9ef
 int virtio_serial_open(VirtIOSerialPort *port)
amitshah 99a9ef
 {
amitshah 99a9ef
+    /* Don't allow opening an already-open port */
amitshah 99a9ef
+    if (port->host_connected) {
amitshah 99a9ef
+        return 0;
amitshah 99a9ef
+    }
amitshah 99a9ef
+    /* Send port open notification to the guest */
amitshah 99a9ef
+    port->host_connected = true;
amitshah 99a9ef
+    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
amitshah 99a9ef
+
amitshah 99a9ef
     return 0;
amitshah 99a9ef
 }
amitshah 99a9ef
 
amitshah 99a9ef
 int virtio_serial_close(VirtIOSerialPort *port)
amitshah 99a9ef
 {
amitshah 99a9ef
+    port->host_connected = false;
amitshah 99a9ef
+    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
amitshah 99a9ef
+
amitshah 99a9ef
     return 0;
amitshah 99a9ef
 }
amitshah 99a9ef
 
amitshah 99a9ef
@@ -151,6 +167,9 @@ int virtio_serial_close(VirtIOSerialPort *port)
amitshah 99a9ef
 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
amitshah 99a9ef
                             size_t size)
amitshah 99a9ef
 {
amitshah 99a9ef
+    if (!port || !port->host_connected || !port->guest_connected) {
amitshah 99a9ef
+        return 0;
amitshah 99a9ef
+    }
amitshah 99a9ef
     return write_to_port(port, buf, size);
amitshah 99a9ef
 }
amitshah 99a9ef
 
amitshah 99a9ef
@@ -167,6 +186,9 @@ size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
amitshah 99a9ef
         virtio_queue_empty(vq)) {
amitshah 99a9ef
         return 0;
amitshah 99a9ef
     }
amitshah 99a9ef
+    if (use_multiport(port->vser) && !port->guest_connected) {
amitshah 99a9ef
+        return 0;
amitshah 99a9ef
+    }
amitshah 99a9ef
 
amitshah 99a9ef
     if (virtqueue_avail_bytes(vq, 4096, 0)) {
amitshah 99a9ef
         return 4096;
amitshah 99a9ef
@@ -203,6 +225,11 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)
amitshah 99a9ef
         if (port->is_console) {
amitshah 99a9ef
             send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
amitshah 99a9ef
         }
amitshah 99a9ef
+
amitshah 99a9ef
+        if (port->host_connected) {
amitshah 99a9ef
+            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
amitshah 99a9ef
+        }
amitshah 99a9ef
+
amitshah 99a9ef
         /*
amitshah 99a9ef
          * When the guest has asked us for this information it means
amitshah 99a9ef
          * the guest is all setup and has its virtqueues
amitshah 99a9ef
@@ -213,6 +240,19 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)
amitshah 99a9ef
             port->info->guest_ready(port);
amitshah 99a9ef
         }
amitshah 99a9ef
         break;
amitshah 99a9ef
+
amitshah 99a9ef
+    case VIRTIO_CONSOLE_PORT_OPEN:
amitshah 99a9ef
+        port->guest_connected = cpkt.value;
amitshah 99a9ef
+        if (cpkt.value && port->info->guest_open) {
amitshah 99a9ef
+            /* Send the guest opened notification if an app is interested */
amitshah 99a9ef
+            port->info->guest_open(port);
amitshah 99a9ef
+        }
amitshah 99a9ef
+
amitshah 99a9ef
+        if (!cpkt.value && port->info->guest_close) {
amitshah 99a9ef
+            /* Send the guest closed notification if an app is interested */
amitshah 99a9ef
+            port->info->guest_close(port);
amitshah 99a9ef
+        }
amitshah 99a9ef
+        break;
amitshah 99a9ef
     }
amitshah 99a9ef
 }
amitshah 99a9ef
 
amitshah 99a9ef
@@ -300,6 +340,8 @@ static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
amitshah 99a9ef
 static void virtio_serial_save(QEMUFile *f, void *opaque)
amitshah 99a9ef
 {
amitshah 99a9ef
     VirtIOSerial *s = opaque;
amitshah 99a9ef
+    VirtIOSerialPort *port;
amitshah 99a9ef
+    uint32_t nr_active_ports;
amitshah 99a9ef
 
amitshah 99a9ef
     /* The virtio device */
amitshah 99a9ef
     virtio_save(&s->vdev, f);
amitshah 99a9ef
@@ -308,15 +350,41 @@ static void virtio_serial_save(QEMUFile *f, void *opaque)
amitshah 99a9ef
     qemu_put_be16s(f, &s->config.cols);
amitshah 99a9ef
     qemu_put_be16s(f, &s->config.rows);
amitshah 99a9ef
     qemu_put_be32s(f, &s->config.nr_ports);
amitshah 99a9ef
+
amitshah 99a9ef
+    /* Items in struct VirtIOSerial */
amitshah 99a9ef
+
amitshah 99a9ef
+    /* Do this because we might have hot-unplugged some ports */
amitshah 99a9ef
+    nr_active_ports = 0;
amitshah 99a9ef
+    QTAILQ_FOREACH(port, &s->ports, next)
amitshah 99a9ef
+        nr_active_ports++;
amitshah 99a9ef
+
amitshah 99a9ef
+    qemu_put_be32s(f, &nr_active_ports);
amitshah 99a9ef
+
amitshah 99a9ef
+    /*
amitshah 99a9ef
+     * Items in struct VirtIOSerialPort.
amitshah 99a9ef
+     */
amitshah 99a9ef
+    QTAILQ_FOREACH(port, &s->ports, next) {
amitshah 99a9ef
+        /*
amitshah 99a9ef
+         * We put the port number because we may not have an active
amitshah 99a9ef
+         * port at id 0 that's reserved for a console port, or in case
amitshah 99a9ef
+         * of ports that might have gotten unplugged
amitshah 99a9ef
+         */
amitshah 99a9ef
+        qemu_put_be32s(f, &port->id);
amitshah 99a9ef
+        qemu_put_byte(f, port->guest_connected);
amitshah 99a9ef
+    }
amitshah 99a9ef
 }
amitshah 99a9ef
 
amitshah 99a9ef
 static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
amitshah 99a9ef
 {
amitshah 99a9ef
     VirtIOSerial *s = opaque;
amitshah 99a9ef
+    VirtIOSerialPort *port;
amitshah 99a9ef
+    uint32_t nr_active_ports;
amitshah 99a9ef
+    unsigned int i;
amitshah 99a9ef
 
amitshah 99a9ef
     if (version_id > 2) {
amitshah 99a9ef
         return -EINVAL;
amitshah 99a9ef
     }
amitshah 99a9ef
+
amitshah 99a9ef
     /* The virtio device */
amitshah 99a9ef
     virtio_load(&s->vdev, f);
amitshah 99a9ef
 
amitshah 99a9ef
@@ -329,6 +397,20 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
amitshah 99a9ef
     qemu_get_be16s(f, &s->config.rows);
amitshah 99a9ef
     s->config.nr_ports = qemu_get_be32(f);
amitshah 99a9ef
 
amitshah 99a9ef
+    /* Items in struct VirtIOSerial */
amitshah 99a9ef
+
amitshah 99a9ef
+    qemu_get_be32s(f, &nr_active_ports);
amitshah 99a9ef
+
amitshah 99a9ef
+    /* Items in struct VirtIOSerialPort */
amitshah 99a9ef
+    for (i = 0; i < nr_active_ports; i++) {
amitshah 99a9ef
+        uint32_t id;
amitshah 99a9ef
+
amitshah 99a9ef
+        id = qemu_get_be32(f);
amitshah 99a9ef
+        port = find_port_by_id(s, id);
amitshah 99a9ef
+
amitshah 99a9ef
+        port->guest_connected = qemu_get_byte(f);
amitshah 99a9ef
+    }
amitshah 99a9ef
+
amitshah 99a9ef
     return 0;
amitshah 99a9ef
 }
amitshah 99a9ef
 
amitshah 99a9ef
@@ -357,6 +439,10 @@ static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
amitshah 99a9ef
 
amitshah 99a9ef
     monitor_printf(mon, "%*s dev-prop-int: id: %u\n",
amitshah 99a9ef
                    indent, "", port->id);
amitshah 99a9ef
+    monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n",
amitshah 99a9ef
+                   indent, "", port->guest_connected);
amitshah 99a9ef
+    monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n",
amitshah 99a9ef
+                   indent, "", port->host_connected);
amitshah 99a9ef
 }
amitshah 99a9ef
 
amitshah 99a9ef
 static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
amitshah 99a9ef
@@ -390,6 +476,14 @@ static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
amitshah 99a9ef
 
amitshah 99a9ef
     port->id = plugging_port0 ? 0 : port->vser->config.nr_ports++;
amitshah 99a9ef
 
amitshah 99a9ef
+    if (!use_multiport(port->vser)) {
amitshah 99a9ef
+        /*
amitshah 99a9ef
+         * Allow writes to guest in this case; we have no way of
amitshah 99a9ef
+         * knowing if a guest port is connected.
amitshah 99a9ef
+         */
amitshah 99a9ef
+        port->guest_connected = true;
amitshah 99a9ef
+    }
amitshah 99a9ef
+
amitshah 99a9ef
     QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
amitshah 99a9ef
     port->ivq = port->vser->ivqs[port->id];
amitshah 99a9ef
     port->ovq = port->vser->ovqs[port->id];
amitshah 99a9ef
diff --git a/hw/virtio-serial.h b/hw/virtio-serial.h
amitshah 99a9ef
index fe8e357..d9c7acb 100644
amitshah 99a9ef
--- a/hw/virtio-serial.h
amitshah 99a9ef
+++ b/hw/virtio-serial.h
amitshah 99a9ef
@@ -49,6 +49,7 @@ struct virtio_console_control {
amitshah 99a9ef
 #define VIRTIO_CONSOLE_PORT_READY	0
amitshah 99a9ef
 #define VIRTIO_CONSOLE_CONSOLE_PORT	1
amitshah 99a9ef
 #define VIRTIO_CONSOLE_RESIZE		2
amitshah 99a9ef
+#define VIRTIO_CONSOLE_PORT_OPEN	3
amitshah 99a9ef
 
amitshah 99a9ef
 /* == In-qemu interface == */
amitshah 99a9ef
 
amitshah 99a9ef
@@ -92,6 +93,11 @@ struct VirtIOSerialPort {
amitshah 99a9ef
 
amitshah 99a9ef
     /* Identify if this is a port that binds with hvc in the guest */
amitshah 99a9ef
     uint8_t is_console;
amitshah 99a9ef
+
amitshah 99a9ef
+    /* Is the corresponding guest device open? */
amitshah 99a9ef
+    bool guest_connected;
amitshah 99a9ef
+    /* Is this device open for IO on the host? */
amitshah 99a9ef
+    bool host_connected;
amitshah 99a9ef
 };
amitshah 99a9ef
 
amitshah 99a9ef
 struct VirtIOSerialPortInfo {
amitshah 99a9ef
-- 
amitshah 99a9ef
1.6.2.5
amitshah 99a9ef