Skip to contentSkip to conversation

virtio-console: Move to qdev, multiple devices, generic ports

PATCH Applied Amit Shah 3 participants 12 messages Jan 19, 2010 – Jan 21, 2010 Kbuild

lore
Share & tools
lore

Amit Shah

author
#0 lore raw

cover virtio-console: Move to qdev, multiple devices, generic ports

Hello all,

In this series I've removed the buffering that happened in the host and ack the amount of data that ports actually consume. This basically removes the older patch 5/8 that introduced buffering and throttling.

Other changes include addition of patch 8: MSI support and ensuring we don't crash in the event we don't have chardevs opened and guest writes out to virtio-consoles.

Obligatory disclaimer:
This series splits up the patches by functionality. Note, however, that patches 2-5 introduce some functionality that's advertised to the guest as having to work all at once or not at all. Also, the savevm version is bumped only once but save/restore state is added in each of the patches. They are split only for easier reviewability.

The older virtio-console.c file is completely removed and a new virtio-serial.c is introduced so that reviewing is easier. I can send a later patch to rename virtio-serial.c back to virtio-console.c.

Amit Shah (8):
virtio: Remove duplicate macro definition for max. virtqueues, bump
up the max
virtio-console: qdev conversion, new virtio-serial-bus
virtio-serial-bus: Maintain guest and host port open/close state
virtio-serial-bus: Add a port 'name' property for port discovery in
guests
virtio-serial-bus: Add ability to hot-unplug ports
virtio-serial: Add a 'virtserialport' device for generic serial port
support
Move virtio-serial to Makefile.objs
virtio-serial: Use MSI vectors for port virtqueues

 Makefile.objs          |    2 +-
 Makefile.target        |    2 +-
 hw/pc.c                |   11 +-
 hw/ppc440_bamboo.c     |    7 -
 hw/qdev.c              |   10 +-
 hw/s390-virtio-bus.c   |   17 +-
 hw/s390-virtio-bus.h   |    2 +
 hw/s390-virtio.c       |    8 -
 hw/virtio-console.c    |  143 -----------
 hw/virtio-console.h    |   19 --
 hw/virtio-pci.c        |   17 +-
 hw/virtio-serial-bus.c |  620 ++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio-serial.c     |  146 ++++++++++++
 hw/virtio-serial.h     |  173 ++++++++++++++
 hw/virtio.c            |    2 -
 hw/virtio.h            |    4 +-
 qemu-options.hx        |    4 +
 sysemu.h               |    6 -
 vl.c                   |    2 +
 19 files changed, 978 insertions(+), 217 deletions(-)
 delete mode 100644 hw/virtio-console.c
 delete mode 100644 hw/virtio-console.h
 create mode 100644 hw/virtio-serial-bus.c
 create mode 100644 hw/virtio-serial.c
 create mode 100644 hw/virtio-serial.h
author
#1 lore raw

PATCH 1/8 virtio: Remove duplicate macro definition for max. virtqueues, bump up the max

VIRTIO_PCI_QUEUE_MAX is redefined in hw/virtio.c. Let's just keep it in hw/virtio.h.

Also, bump up the value of the maximum allowed virtqueues to 64. This is in preparation to allow multiple ports per virtio-console device.

---
 hw/virtio.c |    2 --
 hw/virtio.h |    2 +-
 2 files changed, 1 insertions(+), 3 deletions(-)
hw/virtio.c +0 -2
index fa7184a..7c020a3 100644
@@ -75,8 +75,6 @@ struct VirtQueue
     void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
 };
 
-#define VIRTIO_PCI_QUEUE_MAX        16
-
 /* virt queue functions */
 static void virtqueue_init(VirtQueue *vq)
 {
hw/virtio.h +1 -1
index 3994cc9..7b2b327 100644
@@ -90,7 +90,7 @@ typedef struct {
     unsigned (*get_features)(void * opaque);
 } VirtIOBindings;
 
-#define VIRTIO_PCI_QUEUE_MAX 16
+#define VIRTIO_PCI_QUEUE_MAX 64
 
 #define VIRTIO_NO_VECTOR 0xffff
 
Signature · 1 line
1.6.2.5
author
#2 lore raw

PATCH 2/8 virtio-console: qdev conversion, new virtio-serial-bus

This commit converts the virtio-console device to create a new virtio-serial bus that can host console and generic serial ports. The file hosting this code is now called virtio-serial-bus.c.

The virtio console is now a very simple qdev device that sits on the virtio-serial-bus and communicates between the bus and qemu's chardevs.

This commit also includes a few changes to the virtio backing code for pci and s390 to spawn the virtio-serial bus.

As a result of the qdev conversion, we get rid of a lot of legacy code. The old-style way of instantiating a virtio console using

-virtioconsole ...

is maintained, but the new, preferred way is to use

-device virtio-serial -device virtconsole,chardev=...

With this commit, multiple devices as well as multiple ports with a single device can be supported.

For multiple ports support, each port gets an IO vq pair. Since the guest needs to know in advance how many vqs a particular device will need, we have to set this number as a property of the virtio-serial device and also as a config option.

In addition, we also spawn a pair of control IO vqs. This is an internal channel meant for guest-host communication for things like port open/close, sending port properties over to the guest, etc.

This commit is a part of a series of other commits to get the full implementation of multiport support. Future commits will add other support as well as ride on the savevm version that we bump up here.

---
 Makefile.target        |    2 +-
 hw/pc.c                |   11 +-
 hw/ppc440_bamboo.c     |    7 -
 hw/qdev.c              |   10 +-
 hw/s390-virtio-bus.c   |   17 +-
 hw/s390-virtio-bus.h   |    2 +
 hw/s390-virtio.c       |    8 -
 hw/virtio-console.c    |  143 --------------
 hw/virtio-console.h    |   19 --
 hw/virtio-pci.c        |   13 +-
 hw/virtio-serial-bus.c |  507 ++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio-serial.c     |  111 +++++++++++
 hw/virtio-serial.h     |  158 +++++++++++++++
 hw/virtio.h            |    2 +-
 qemu-options.hx        |    4 +
 sysemu.h               |    6 -
 vl.c                   |    2 +
 17 files changed, 809 insertions(+), 213 deletions(-)
 delete mode 100644 hw/virtio-console.c
 delete mode 100644 hw/virtio-console.h
 create mode 100644 hw/virtio-serial-bus.c
 create mode 100644 hw/virtio-serial.c
 create mode 100644 hw/virtio-serial.h
Makefile.target +1 -1
index e661478..60df16d 100644
@@ -172,7 +172,7 @@ ifdef CONFIG_SOFTMMU
 obj-y = vl.o async.o monitor.o pci.o pci_host.o pcie_host.o machine.o gdbstub.o
 # virtio has to be here due to weird dependency between PCI and virtio-net.
 # need to fix this properly
-obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-console.o virtio-pci.o
+obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-serial.o virtio-serial-bus.o virtio-pci.o
 obj-$(CONFIG_KVM) += kvm.o kvm-all.o
 obj-$(CONFIG_ISA_MMIO) += isa_mmio.o
 LIBS+=-lz
hw/pc.c +1 -10
index a93c5f2..3aadfa9 100644
@@ -1018,15 +1018,6 @@ static void pc_init1(ram_addr_t ram_size,
             pci_create_simple(pci_bus, -1, "lsi53c895a");
         }
     }
-
-    /* Add virtio console devices */
-    if (pci_enabled) {
-        for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
-            if (virtcon_hds[i]) {
-                pci_create_simple(pci_bus, -1, "virtio-console-pci");
-            }
-        }
-    }
 }
 
 static void pc_init_pci(ram_addr_t ram_size,
@@ -1102,7 +1093,7 @@ static QEMUMachine pc_machine_v0_10 = {
             .property = "class",
             .value    = stringify(PCI_CLASS_STORAGE_OTHER),
         },{
-            .driver   = "virtio-console-pci",
+            .driver   = "virtio-serial-pci",
             .property = "class",
             .value    = stringify(PCI_CLASS_DISPLAY_OTHER),
         },{
hw/ppc440_bamboo.c +0 -7
index a488240..1ab9872 100644
@@ -108,13 +108,6 @@ static void bamboo_init(ram_addr_t ram_size,
     env = ppc440ep_init(&ram_size, &pcibus, pci_irq_nrs, 1, cpu_model);
 
     if (pcibus) {
-        /* Add virtio console devices */
-        for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
-            if (virtcon_hds[i]) {
-                pci_create_simple(pcibus, -1, "virtio-console-pci");
-            }
-        }
-
         /* Register network interfaces. */
         for (i = 0; i < nb_nics; i++) {
             /* There are no PCI NICs on the Bamboo board, but there are
hw/qdev.c +3 -7
index b6bd4ae..c643576 100644
@@ -321,13 +321,9 @@ void qdev_machine_creation_done(void)
 CharDriverState *qdev_init_chardev(DeviceState *dev)
 {
     static int next_serial;
-    static int next_virtconsole;
-    /* FIXME: This is a nasty hack that needs to go away.  */
-    if (strncmp(dev->info->name, "virtio", 6) == 0) {
-        return virtcon_hds[next_virtconsole++];
-    } else {
-        return serial_hds[next_serial++];
-    }
+
+    /* FIXME: This function needs to go away: use chardev properties!  */
+    return serial_hds[next_serial++];
 }
 
 BusState *qdev_get_parent_bus(DeviceState *dev)
hw/s390-virtio-bus.c +10 -7
index 980e7eb..6b6dafc 100644
@@ -26,7 +26,7 @@
 #include "loader.h"
 #include "elf.h"
 #include "hw/virtio.h"
-#include "hw/virtio-console.h"
+#include "hw/virtio-serial.h"
 #include "hw/sysbus.h"
 #include "kvm.h"
 
@@ -131,7 +131,7 @@ static int s390_virtio_blk_init(VirtIOS390Device *dev)
     return s390_virtio_device_init(dev, vdev);
 }
 
-static int s390_virtio_console_init(VirtIOS390Device *dev)
+static int s390_virtio_serial_init(VirtIOS390Device *dev)
 {
     VirtIOS390Bus *bus;
     VirtIODevice *vdev;
@@ -139,7 +139,7 @@ static int s390_virtio_console_init(VirtIOS390Device *dev)
 
     bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
 
-    vdev = virtio_console_init((DeviceState *)dev);
+    vdev = virtio_serial_init((DeviceState *)dev, dev->max_virtserial_ports);
     if (!vdev) {
         return -1;
     }
@@ -342,11 +342,14 @@ static VirtIOS390DeviceInfo s390_virtio_blk = {
     },
 };
 
-static VirtIOS390DeviceInfo s390_virtio_console = {
-    .init = s390_virtio_console_init,
-    .qdev.name = "virtio-console-s390",
+static VirtIOS390DeviceInfo s390_virtio_serial = {
+    .init = s390_virtio_serial_init,
+    .qdev.name = "virtio-serial-s390",
+    .qdev.alias = "virtio-serial",
     .qdev.size = sizeof(VirtIOS390Device),
     .qdev.props = (Property[]) {
+        DEFINE_PROP_UINT32("max_ports", VirtIOS390Device, max_virtserial_ports,
+                           31),
         DEFINE_PROP_END_OF_LIST(),
     },
 };
@@ -370,7 +373,7 @@ static void s390_virtio_bus_register_withprop(VirtIOS390DeviceInfo *info)
 
 static void s390_virtio_register(void)
 {
-    s390_virtio_bus_register_withprop(&s390_virtio_console);
+    s390_virtio_bus_register_withprop(&s390_virtio_serial);
     s390_virtio_bus_register_withprop(&s390_virtio_blk);
     s390_virtio_bus_register_withprop(&s390_virtio_net);
 }
hw/s390-virtio-bus.h +2 -0
index 8ae2065..8e4763a 100644
@@ -41,6 +41,8 @@ typedef struct VirtIOS390Device {
     DriveInfo *dinfo;
     NICConf nic;
     uint32_t host_features;
+    /* Max. number of ports we can have for a the virtio-serial device */
+    uint32_t max_virtserial_ports;
 } VirtIOS390Device;
 
 typedef struct VirtIOS390Bus {
hw/s390-virtio.c +0 -8
index 0fa6ba6..3582728 100644
@@ -26,7 +26,6 @@
 #include "loader.h"
 #include "elf.h"
 #include "hw/virtio.h"
-#include "hw/virtio-console.h"
 #include "hw/sysbus.h"
 #include "kvm.h"
 
@@ -207,13 +206,6 @@ static void s390_init(ram_addr_t ram_size,
                                strlen(kernel_cmdline), 1);
     }
 
-    /* Create VirtIO console */
-    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
-        if (virtcon_hds[i]) {
-            qdev_init_nofail(qdev_create((BusState *)s390_bus, "virtio-console-s390"));
-        }
-    }
-
     /* Create VirtIO network adapters */
     for(i = 0; i < nb_nics; i++) {
         NICInfo *nd = &nd_table[i];
hw/virtio-console.c deleted +0 -143

143 lines not on this page · raw message

hw/virtio-console.h deleted +0 -19
deleted file mode 100644
index 84d0717..0000000
@@ -1,19 +0,0 @@
-/*
- * Virtio Console Support
- *
- * Copyright IBM, Corp. 2008
- *
- * Authors:
- *  Christian Ehrhardt <[email protected]>
- *
- * This work is licensed under the terms of the GNU GPL, version 2.  See
- * the COPYING file in the top-level directory.
- *
- */
-#ifndef _QEMU_VIRTIO_CONSOLE_H
-#define _QEMU_VIRTIO_CONSOLE_H
-
-/* The ID for virtio console */
-#define VIRTIO_ID_CONSOLE 3
-
-#endif
hw/virtio-pci.c +9 -4
index 573c98a..e7fabfb 100644
@@ -95,6 +95,8 @@ typedef struct {
     DriveInfo *dinfo;
     NICConf nic;
     uint32_t host_features;
+    /* Max. number of ports we can have for a the virtio-serial device */
+    uint32_t max_virtserial_ports;
 } VirtIOPCIProxy;
 
 /* virtio device */
@@ -483,7 +485,7 @@ static int virtio_blk_exit_pci(PCIDevice *pci_dev)
     return virtio_exit_pci(pci_dev);
 }
 
-static int virtio_console_init_pci(PCIDevice *pci_dev)
+static int virtio_serial_init_pci(PCIDevice *pci_dev)
 {
     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
     VirtIODevice *vdev;
@@ -493,7 +495,7 @@ static int virtio_console_init_pci(PCIDevice *pci_dev)
         proxy->class_code != PCI_CLASS_OTHERS)          /* qemu-kvm  */
         proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
 
-    vdev = virtio_console_init(&pci_dev->qdev);
+    vdev = virtio_serial_init(&pci_dev->qdev, proxy->max_virtserial_ports);
     if (!vdev) {
         return -1;
     }
@@ -573,13 +575,16 @@ static PCIDeviceInfo virtio_info[] = {
         },
         .qdev.reset = virtio_pci_reset,
     },{
-        .qdev.name = "virtio-console-pci",
+        .qdev.name = "virtio-serial-pci",
+        .qdev.alias = "virtio-serial",
         .qdev.size = sizeof(VirtIOPCIProxy),
-        .init      = virtio_console_init_pci,
+        .init      = virtio_serial_init_pci,
         .exit      = virtio_exit_pci,
         .qdev.props = (Property[]) {
             DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
             DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
+            DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, max_virtserial_ports,
+                               31),
             DEFINE_PROP_END_OF_LIST(),
         },
         .qdev.reset = virtio_pci_reset,
hw/virtio-serial-bus.c new file +507 -0

507 lines not on this page · raw message

hw/virtio-serial.c new file +111 -0

111 lines not on this page · raw message

hw/virtio-serial.h new file +158 -0

158 lines not on this page · raw message

hw/virtio.h +1 -1
index 7b2b327..62e882b 100644
@@ -171,7 +171,7 @@ void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
 /* Base devices.  */
 VirtIODevice *virtio_blk_init(DeviceState *dev, DriveInfo *dinfo);
 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf);
-VirtIODevice *virtio_console_init(DeviceState *dev);
+VirtIODevice *virtio_serial_init(DeviceState *dev, uint32_t max_nr_ports);
 VirtIODevice *virtio_balloon_init(DeviceState *dev);
 
 void virtio_net_exit(VirtIODevice *vdev);
qemu-options.hx +4 -0
index e2edd71..0c429e5 100644
@@ -1885,6 +1885,10 @@ DEF("virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon, \
 STEXI
 @item -virtioconsole @var{c}
 Set virtio console.
+
+This option is maintained for backward compatibility.
+
+Please use @code{-device virtconsole} for the new way of invocation.
 ETEXI
 
 DEF("show-cursor", 0, QEMU_OPTION_show_cursor, \
sysemu.h +0 -6
index 9d80bb2..9c3b281 100644
@@ -231,12 +231,6 @@ extern CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 
 extern CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 
-/* virtio consoles */
-
-#define MAX_VIRTIO_CONSOLES 1
-
-extern CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
-
 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
 
 #ifdef HAS_AUDIO
vl.c +2 -0
index 06cb40d..89663a4 100644
@@ -173,6 +173,8 @@ int main(int argc, char **argv)
 
 #define DEFAULT_RAM_SIZE 128
 
+#define MAX_VIRTIO_CONSOLES 1
+
 static const char *data_dir;
 const char *bios_name = NULL;
 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
Signature · 1 line
1.6.2.5
author
#3 lore raw

PATCH 3/8 virtio-serial-bus: Maintain guest and host port open/close state

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.

---
 hw/virtio-serial-bus.c |   94 ++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio-serial.h     |    6 +++
 2 files changed, 100 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +94 -0
index e8bbd7d..5bf2990 100644
@@ -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];
hw/virtio-serial.h +6 -0
index fe8e357..d9c7acb 100644
@@ -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 {
Signature · 1 line
1.6.2.5
author
#4 lore raw

PATCH 4/8 virtio-serial-bus: Add a port 'name' property for port discovery in guests

The port 'id' or number is internal state between the guest kernel and our bus implementation. This is invocation-dependent and isn't part of the guest-host ABI.

To correcly enumerate and map ports between the host and the guest, the 'name' property is used.

Example:

-device virtserialport,name=org.qemu.port.0

This invocation will get us a char device in the guest at:

/dev/virtio-ports/org.qemu.port.0

which can be a symlink to

    /dev/vport0p3

This 'name' property is exposed by the guest kernel in a sysfs attribute:

/sys/kernel/virtio-ports/vport0p3/name

A simple udev script can pick up this name and create the symlink mentioned above.

---
 hw/virtio-serial-bus.c |   17 +++++++++++++++++
 hw/virtio-serial.c     |    1 +
 hw/virtio-serial.h     |    8 ++++++++
 3 files changed, 26 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +17 -0
index 5bf2990..7e100d0 100644
@@ -204,6 +204,8 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)
 {
     struct VirtIOSerialPort *port;
     struct virtio_console_control cpkt, *gcpkt;
+    uint8_t *buffer;
+    size_t buffer_len;
 
     gcpkt = buf;
     port = find_port_by_id(vser, ldl_p(&gcpkt->id));
@@ -226,6 +228,21 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)
             send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
         }
 
+        if (port->name) {
+            stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
+            stw_p(&cpkt.value, 1);
+
+            buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
+            buffer = qemu_malloc(buffer_len);
+
+            memcpy(buffer, &cpkt, sizeof(cpkt));
+            memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
+            buffer[buffer_len - 1] = 0;
+
+            send_control_msg(port, buffer, buffer_len);
+            qemu_free(buffer);
+        }
+
         if (port->host_connected) {
             send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
         }
hw/virtio-serial.c +1 -0
index 1dc031e..9c2c93c 100644
@@ -100,6 +100,7 @@ static VirtIOSerialPortInfo virtconsole_info = {
     .qdev.props = (Property[]) {
         DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1),
         DEFINE_PROP_CHR("chardev", VirtConsole, chr),
+        DEFINE_PROP_STRING("name", VirtConsole, port.name),
         DEFINE_PROP_END_OF_LIST(),
     },
 };
hw/virtio-serial.h +8 -0
index d9c7acb..28ea7da 100644
@@ -50,6 +50,7 @@ struct virtio_console_control {
 #define VIRTIO_CONSOLE_CONSOLE_PORT	1
 #define VIRTIO_CONSOLE_RESIZE		2
 #define VIRTIO_CONSOLE_PORT_OPEN	3
+#define VIRTIO_CONSOLE_PORT_NAME	4
 
 /* == In-qemu interface == */
 
@@ -84,6 +85,13 @@ struct VirtIOSerialPort {
     VirtQueue *ivq, *ovq;
 
     /*
+     * This name is sent to the guest and exported via sysfs.
+     * The guest could create symlinks based on this information.
+     * The name is in the reverse fqdn format, like org.qemu.console.0
+     */
+    char *name;
+
+    /*
      * This id helps identify ports between the guest and the host.
      * The guest sends a "header" with this id with each data packet
      * that it sends and the host can then find out which associated
Signature · 1 line
1.6.2.5
author
#5 lore raw

PATCH 5/8 virtio-serial-bus: Add ability to hot-unplug ports

---
 hw/virtio-serial-bus.c |    2 ++
 hw/virtio-serial.h     |    1 +
 2 files changed, 3 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +2 -0
index 7e100d0..403268f 100644
@@ -520,6 +520,8 @@ static int virtser_port_qdev_exit(DeviceState *qdev)
     VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
     VirtIOSerial *vser = port->vser;
 
+    send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
+
     /*
      * Don't decrement nr_ports here; thus we keep a linearly
      * increasing port id. Not utilising an id again saves us a couple
hw/virtio-serial.h +1 -0
index 28ea7da..f297b00 100644
@@ -51,6 +51,7 @@ struct virtio_console_control {
 #define VIRTIO_CONSOLE_RESIZE		2
 #define VIRTIO_CONSOLE_PORT_OPEN	3
 #define VIRTIO_CONSOLE_PORT_NAME	4
+#define VIRTIO_CONSOLE_PORT_REMOVE	5
 
 /* == In-qemu interface == */
 
Signature · 1 line
1.6.2.5
author
#6 lore raw

PATCH 6/8 virtio-serial: Add a 'virtserialport' device for generic serial port support

This commit adds a simple chardev-based serial port. Any data the guest sends is forwarded to the chardev and vice-versa.

Sample uses for such a device can be obtaining info from the guest like the file systems used, apps installed, etc. for offline usage and logged-in users, clipboard copy-paste, etc. for online usage.

---
 hw/virtio-serial.c |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
hw/virtio-serial.c +34 -0
index 9c2c93c..bd44ec6 100644
@@ -110,3 +110,37 @@ static void virtconsole_register(void)
     virtio_serial_port_qdev_register(&virtconsole_info);
 }
 device_init(virtconsole_register)
+
+/* Generic Virtio Serial Ports */
+static int virtserialport_initfn(VirtIOSerialDevice *dev)
+{
+    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
+    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
+
+    port->info = dev->info;
+
+    if (vcon->chr) {
+        qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
+                              vcon);
+        port->info->have_data = flush_buf;
+    }
+    return 0;
+}
+
+static VirtIOSerialPortInfo virtserialport_info = {
+    .qdev.name     = "virtserialport",
+    .qdev.size     = sizeof(VirtConsole),
+    .init          = virtserialport_initfn,
+    .exit          = virtconsole_exitfn,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_CHR("chardev", VirtConsole, chr),
+        DEFINE_PROP_STRING("name", VirtConsole, port.name),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static void virtserialport_register(void)
+{
+    virtio_serial_port_qdev_register(&virtserialport_info);
+}
+device_init(virtserialport_register)
Signature · 1 line
1.6.2.5
author
#7 lore raw

PATCH 7/8 Move virtio-serial to Makefile.objs

There's nothing target-dependent in the virtio-serial code so allow it to be compiled just once for all the targets.

---
 Makefile.objs   |    2 +-
 Makefile.target |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Makefile.objs +1 -1
index 5802d39..77ff7f6 100644
@@ -127,7 +127,7 @@ user-obj-y += cutils.o cache-utils.o
 
 hw-obj-y =
 hw-obj-y += loader.o
-hw-obj-y += virtio.o
+hw-obj-y += virtio.o virtio-serial.o
 hw-obj-y += fw_cfg.o
 hw-obj-y += watchdog.o
 hw-obj-$(CONFIG_ECC) += ecc.o
Makefile.target +1 -1
index 60df16d..0bf2253 100644
@@ -172,7 +172,7 @@ ifdef CONFIG_SOFTMMU
 obj-y = vl.o async.o monitor.o pci.o pci_host.o pcie_host.o machine.o gdbstub.o
 # virtio has to be here due to weird dependency between PCI and virtio-net.
 # need to fix this properly
-obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-serial.o virtio-serial-bus.o virtio-pci.o
+obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-pci.o virtio-serial-bus.o
 obj-$(CONFIG_KVM) += kvm.o kvm-all.o
 obj-$(CONFIG_ISA_MMIO) += isa_mmio.o
 LIBS+=-lz
Signature · 1 line
1.6.2.5
author
#8 lore raw

PATCH 8/8 virtio-serial: Use MSI vectors for port virtqueues

This commit enables the use of MSI interrupts for virtqueue notifications for ports. We use nr_ports + 1 (for control channel) msi entries for the ports, as only the in_vq operations need an interrupt on the guest.

---
 hw/virtio-pci.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
hw/virtio-pci.c +4 -0
index e7fabfb..709d13e 100644
@@ -499,10 +499,13 @@ static int virtio_serial_init_pci(PCIDevice *pci_dev)
     if (!vdev) {
         return -1;
     }
+    vdev->nvectors = proxy->nvectors ? proxy->nvectors
+                                     : proxy->max_virtserial_ports + 1;
     virtio_init_pci(proxy, vdev,
                     PCI_VENDOR_ID_REDHAT_QUMRANET,
                     PCI_DEVICE_ID_VIRTIO_CONSOLE,
                     proxy->class_code, 0x00);
+    proxy->nvectors = vdev->nvectors;
     return 0;
 }
 
@@ -581,6 +584,7 @@ static PCIDeviceInfo virtio_info[] = {
         .init      = virtio_serial_init_pci,
         .exit      = virtio_exit_pci,
         .qdev.props = (Property[]) {
+            DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 0),
             DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
             DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
             DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, max_virtserial_ports,
Signature · 1 line
1.6.2.5
#9 lore raw

Re: [Qemu-devel] [PATCH 1/8] virtio: Remove duplicate macro definition for max. virtqueues, bump up the max

In reply to Amit Shah
Quoted 8 lines · Amit Shah

VIRTIO_PCI_QUEUE_MAX is redefined in hw/virtio.c. Let's just keep it in hw/virtio.h.

Also, bump up the value of the maximum allowed virtqueues to 64. This is in preparation to allow multiple ports per virtio-console device.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 32 lines
---
  hw/virtio.c |    2 --
  hw/virtio.h |    2 +-
  2 files changed, 1 insertions(+), 3 deletions(-)
hw/virtio.c +0 -2
index fa7184a..7c020a3 100644
@@ -75,8 +75,6 @@ struct VirtQueue
      void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
  };
 
-#define VIRTIO_PCI_QUEUE_MAX        16
-
  /* virt queue functions */
  static void virtqueue_init(VirtQueue *vq)
  {
hw/virtio.h +1 -1
index 3994cc9..7b2b327 100644
@@ -90,7 +90,7 @@ typedef struct {
      unsigned (*get_features)(void * opaque);
  } VirtIOBindings;
 
-#define VIRTIO_PCI_QUEUE_MAX 16
+#define VIRTIO_PCI_QUEUE_MAX 64
 
  #define VIRTIO_NO_VECTOR 0xffff
 
#10 lore raw

Re: [Qemu-devel] [PATCH 1/8] virtio: Remove duplicate macro definition for max. virtqueues, bump up the max

Amit, what about renaming hw/virtio-serial.c to a less misleading name now?

author
#11 lore raw

Re: [Qemu-devel] [PATCH 1/8] virtio: Remove duplicate macro definition for max. virtqueues, bump up the max

Quoted 2 lines · Markus Armbruster

Amit, what about renaming hw/virtio-serial.c to a less misleading name now?

I'm going to push that upstream; Anthony in the past indicated he was ok with it.

Amit