virtio-console: Move to qdev, multiple devices, generic ports
cover virtio-console: Move to qdev, multiple devices, generic ports
Hello,
This series addresses some comments from last time:
- locking is removed
- send/receive struct elements in endian-safe way
- appropriate device is created based on machine being used (virtio-serial-pci vs virtio-serial-s390).
This series splits up the patches by functionality. Note, however, that patches 2-6 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 support for buffering guest output, throttling
guests
virtio-serial-bus: Add ability to hot-unplug ports
virtio-serial: Add 'virtserialport' device for generic serial port
support
Move virtio-serial and virtio-serial-bus to Makefile.hw
Makefile.hw | 2 +- Makefile.target | 2 +- hw/pc.c | 11 +- hw/ppc440_bamboo.c | 7 - hw/qdev.c | 8 +- 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 | 946 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-serial.c | 151 ++++++++ hw/virtio-serial.h | 227 ++++++++++++ hw/virtio.c | 2 - hw/virtio.h | 4 +- qemu-options.hx | 4 + sysemu.h | 6 - vl.c | 17 +- 19 files changed, 1371 insertions(+), 218 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
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
@@ -75,8 +75,6 @@ struct VirtQueuevoid (*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
@@ -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
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 | 8 +- 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 | 554 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-serial.c | 107 ++++++++++ hw/virtio-serial.h | 173 +++++++++++++++ hw/virtio.h | 2 +- qemu-options.hx | 4 + sysemu.h | 6 - vl.c | 17 ++- 17 files changed, 879 insertions(+), 214 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
@@ -156,7 +156,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
@@ -1242,15 +1242,6 @@ static void pc_init1(ram_addr_t ram_size, } } -/* 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" ); - } - } - } -rom_load_fw (fw_cfg); }
@@ -1308,7 +1299,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
@@ -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 +2 -6
@@ -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++]; - } +return serial_hds[next_serial++]; }BusState *qdev_get_parent_bus (DeviceState *dev)
hw/s390-virtio-bus.c +10 -7
@@ -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"
@@ -130,7 +130,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;
@@ -138,7 +138,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 ; }
@@ -336,11 +336,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 (), }, };
@@ -364,7 +367,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
@@ -40,6 +40,8 @@ typedef struct VirtIOS390Device {VirtIODevice *vdev;DriveInfo *dinfo;NICConf nic; +/* 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
@@ -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
@@ -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
@@ -92,6 +92,8 @@ typedef struct {uint32_t nvectors;DriveInfo *dinfo;NICConf nic; +/* Max. number of ports we can have for a the virtio-serial device */ +uint32_t max_virtserial_ports; } VirtIOPCIProxy;/* virtio device */
@@ -481,7 +483,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;
@@ -491,7 +493,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 ; }
@@ -569,12 +571,15 @@ 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_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 +554 -0
554 lines not on this page · raw message
hw/virtio-serial.c new file +107 -0
107 lines not on this page · raw message
hw/virtio-serial.h new file +173 -0
173 lines not on this page · raw message
hw/virtio.h +1 -1
@@ -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
@@ -1873,6 +1873,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
@@ -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 +15 -2
@@ -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
@@ -286,8 +288,9 @@ static struct { { .driver = "isa-parallel", .flag = &default_parallel }, { .driver = "isa-fdc", .flag = &default_floppy }, { .driver = "ide-drive", .flag = &default_cdrom }, - { .driver = "virtio-console-pci", .flag = &default_virtcon }, - { .driver = "virtio-console-s390", .flag = &default_virtcon }, + { .driver = "virtio-serial-pci", .flag = &default_virtcon }, + { .driver = "virtio-serial-s390", .flag = &default_virtcon }, + { .driver = "virtio-serial", .flag = &default_virtcon }, { .driver = "VGA", .flag = &default_vga }, { .driver = "cirrus-vga", .flag = &default_vga }, { .driver = "vmware-svga", .flag = &default_vga },
@@ -4816,6 +4819,7 @@ static int virtcon_parse(const char *devname) {static int index =0 ;char label[32 ]; +QemuOpts *bus_opts, *dev_opts;if (strcmp (devname,"none" ) ==0 )return 0 ;
@@ -4823,6 +4827,13 @@ static int virtcon_parse(const char *devname)fprintf (stderr,"qemu: too many virtio consoles\n" );exit (1 ); } + + bus_opts =qemu_opts_create (&qemu_device_opts,NULL ,0 ); +qemu_opt_set (bus_opts,"driver" ,"virtio-serial" ); + + dev_opts =qemu_opts_create (&qemu_device_opts,NULL ,0 ); +qemu_opt_set (dev_opts,"driver" ,"virtconsole" ); +snprintf (label,sizeof (label),"virtcon%d" , index); virtcon_hds[index] =qemu_chr_open (label, devname,NULL );if (!virtcon_hds[index]) {
@@ -4830,6 +4841,8 @@ static int virtcon_parse(const char *devname) devname, strerror(errno));return -1 ; } +qemu_opt_set (dev_opts,"chardev" , label); + index++;return 0 ; }
Signature · 1 line
1.6.2.5
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 | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-serial.c | 6 ++++ hw/virtio-serial.h | 6 ++++ 3 files changed, 85 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +73 -0
@@ -162,11 +162,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 ; }
@@ -174,6 +185,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); }
@@ -194,6 +208,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 )) {
@@ -232,6 +249,9 @@ 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
@@ -242,6 +262,17 @@ 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 ; } }
@@ -342,6 +373,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);
@@ -352,11 +385,35 @@ static void virtio_serial_save(QEMUFile *f, void *opaque)/* Items in struct VirtIOSerial */ qemu_put_be32s (f, &s->guest_features); + +/* 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 ;
@@ -375,6 +432,18 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)/* Items in struct VirtIOSerial */ qemu_get_be32s (f, &s->guest_features); +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 ; }
@@ -406,6 +475,10 @@ static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) indent, "", port->id);monitor_printf (mon,"%*s dev-prop-int: is_console: %d\n" , indent,"" , port->is_console); +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)
hw/virtio-serial.c +6 -0
@@ -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);
hw/virtio-serial.h +6 -0
@@ -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 {
Signature · 1 line
1.6.2.5
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 | 16 ++++++++++++++++ hw/virtio-serial.c | 1 + hw/virtio-serial.h | 8 ++++++++ 3 files changed, 25 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +16 -0
@@ -228,6 +228,8 @@ static void handle_control_message(VirtIOSerial *vser, void *buf) {struct VirtIOSerialPort *port;struct virtio_console_control *cpkt; +uint8_t *buffer; +size_t buffer_len; cpkt = buf; port =find_port_by_id (vser,le32_to_cpu (cpkt->id));
@@ -249,6 +251,20 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)if (port->is_console) {send_control_event (port,VIRTIO_CONSOLE_CONSOLE_PORT ,1 ); } +if (port->name) { + cpkt->event =cpu_to_le16 (VIRTIO_CONSOLE_PORT_NAME ); + cpkt->value =cpu_to_le16 (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
@@ -102,6 +102,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
@@ -54,6 +54,7 @@ struct virtio_console_header {#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 == */
@@ -88,6 +89,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
PATCH 5/8 virtio-serial-bus: Add support for buffering guest output, throttling guests
Guests send us one buffer at a time. Current guests send buffers sized 4K bytes. If guest userspace applications sent out > 4K bytes in one write() syscall, the write request actually sends out multiple buffers, each of 4K in size.
This usually isn't a problem but for some apps, like VNC, the entire data has to be sent in one go to make copy/paste work fine. So if an app on the guest sends out guest clipboard contents, it has to be sent to the vnc server in one go as the guest app sent it.
For this to be done, we need the guest to send us START and END markers for each write request so that we can find out complete buffers and send them off to ports.
This needs us to buffer all the data that comes in from the guests, hold it off till we see all the data corresponding to one write request, merge it all in one buffer and then send it to the port the data was destined for.
Also, we add support for caching of these buffers till a port indicates it's ready to receive data.
We keep caching data the guest sends us till a port accepts it. However, this could lead to an OOM condition where a rogue process on the guest could continue pumping in data while the host continues to cache it.
We introduce a per-port byte-limit property to alleviate this condition. When this limit is reached, we send a control message to the guest indicating it to not send us any more data till further indication. When the number of bytes cached go lesser than the limit specified, we open tell the guest to restart sending data.
--- hw/virtio-serial-bus.c | 305 +++++++++++++++++++++++++++++++++++++++++++++++- hw/virtio-serial.c | 11 +- hw/virtio-serial.h | 39 ++++++ 3 files changed, 348 insertions(+), 7 deletions(-)
hw/virtio-serial-bus.c +303 -2
@@ -46,6 +46,20 @@ struct VirtIOSerial {uint32_t guest_features; }; +/* This struct holds individual buffers received for each port */ +typedef struct VirtIOSerialPortBuffer { +QTAILQ_ENTRY (VirtIOSerialPortBuffer) next; + +uint8_t *buf; + +size_t len;/* length of the buffer */ +size_t offset;/* offset from which to consume data in the buffer */ + +uint32_t flags;/* Sent by guest (start of data stream, end of stream) */ + +bool previous_failure;/* Did sending out this buffer fail previously? */ +}VirtIOSerialPortBuffer ; +static VirtIOSerialPort *find_port_by_id (VirtIOSerial *vser,uint32_t id) {VirtIOSerialPort *port;
@@ -159,6 +173,197 @@ static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,return send_control_msg (port, &cpkt,sizeof (cpkt)); } +static void init_buf (VirtIOSerialPortBuffer *buf,uint8_t *buffer,size_t len) +{ + buf->buf = buffer; + buf->len = len; + buf->offset =0 ; + buf->flags =0 ; + buf->previous_failure = false; +} + +static VirtIOSerialPortBuffer *alloc_buf (size_t len) +{ +VirtIOSerialPortBuffer *buf; + + buf =qemu_malloc (sizeof (*buf)); + buf->buf =qemu_malloc (len); + +init_buf (buf, buf->buf, len); + +return buf; +} + +static void free_buf (VirtIOSerialPortBuffer *buf) +{ +qemu_free (buf->buf); +qemu_free (buf); +} + +static size_t get_complete_data_size (VirtIOSerialPort *port) +{ +VirtIOSerialPortBuffer *buf; +size_t size; +bool is_complete, start_seen; + + size =0 ; + is_complete = false; + start_seen = false; + QTAILQ_FOREACH(buf, &port->unflushed_buffers, next) { + size += buf->len - buf->offset; + +if (buf->flags &VIRTIO_CONSOLE_HDR_END_DATA ) { + is_complete = true; +break ; + } +if (buf ==QTAILQ_FIRST (&port->unflushed_buffers) + && !(buf->flags &VIRTIO_CONSOLE_HDR_START_DATA )) { + +/* There's some data that arrived without a START flag. Flush it. */ + is_complete = true; +break ; + } + +if (buf->flags &VIRTIO_CONSOLE_HDR_START_DATA ) { +if (start_seen) { +/* +* There's some data that arrived without an END +* flag. Flush it. +*/ + size -= buf->len + buf->offset; + is_complete = true; +break ; + } + start_seen = true; + } + } +return is_complete ? size :0 ; +} + +/* +* The guest could have sent the data corresponding to one write +* request split up in multiple buffers. The first buffer has the +* VIRTIO_CONSOLE_HDR_START_DATA flag set and the last buffer has the +* VIRTIO_CONSOLE_HDR_END_DATA flag set. Using this information, merge +* the parts into one buf here to process it for output. +*/ +static VirtIOSerialPortBuffer *get_complete_buf (VirtIOSerialPort *port) +{ +VirtIOSerialPortBuffer *buf, *buf2; +uint8_t *outbuf; +size_t out_offset, out_size; + + out_size =get_complete_data_size (port); +if (!out_size) +return NULL ; + + buf =QTAILQ_FIRST (&port->unflushed_buffers); +if (buf->len - buf->offset == out_size) { +QTAILQ_REMOVE (&port->unflushed_buffers, buf, next); +return buf; + } + out_offset =0 ; + outbuf =qemu_malloc (out_size); + + QTAILQ_FOREACH_SAFE(buf, &port->unflushed_buffers, next, buf2) { +size_t copy_size; + + copy_size = buf->len - buf->offset; +memcpy (outbuf + out_offset, buf->buf + buf->offset, copy_size); + out_offset += copy_size; + +QTAILQ_REMOVE (&port->unflushed_buffers, buf, next); +qemu_free (buf->buf); + +if (out_offset == out_size) { +break ; + } +qemu_free (buf); + } +init_buf (buf, outbuf, out_size); + buf->flags =VIRTIO_CONSOLE_HDR_START_DATA |VIRTIO_CONSOLE_HDR_END_DATA ; + +return buf; +} + +/* Call with the unflushed_buffers_lock held */ +static void flush_queue (VirtIOSerialPort *port) +{ +VirtIOSerialPortBuffer *buf; +size_t out_size; +ssize_t ret; + +/* +* If a device is interested in buffering packets till it's +* opened, cache the data the guest sends us till a connection is +* established. +*/ +if (!port->host_connected && port->cache_buffers) { +return ; + } +while ((buf =get_complete_buf (port))) { + out_size = buf->len - buf->offset; +if (!port->host_connected) { +/* +* Caching is disabled and host is not connected, so +* discard the buffer. Do this only after merging the +* buffer as a port can get connected in the middle of +* dropping buffers and the port will end up getting the +* incomplete output. +*/ + port->nr_bytes -= buf->len + buf->offset; +free_buf (buf); +continue ; + } + + ret = port->info->have_data (port, buf->buf + buf->offset, out_size); +if (ret < out_size) { +QTAILQ_INSERT_HEAD (&port->unflushed_buffers, buf, next); + } +if (ret <=0 ) { +/* We're not progressing at all */ +if (buf->previous_failure) { +break ; + } + buf->previous_failure = true; + }else { + buf->offset += ret; + port->nr_bytes -= ret; + buf->previous_failure = false; + } +if (!(buf->len - buf->offset)) { +free_buf (buf); + } + } + +if (port->host_throttled && port->nr_bytes < port->byte_limit) { + port->host_throttled = false; +send_control_event (port,VIRTIO_CONSOLE_THROTTLE_PORT ,0 ); + } +} + +static void flush_all_ports (VirtIOSerial *vser) +{ +struct VirtIOSerialPort *port; + + QTAILQ_FOREACH(port, &vser->ports, next) { +if (port->has_activity) { + port->has_activity = false; +flush_queue (port); + } + } +} + +static void remove_port_buffers (VirtIOSerialPort *port) +{ +struct VirtIOSerialPortBuffer *buf, *buf2; + + QTAILQ_FOREACH_SAFE(buf, &port->unflushed_buffers, next, buf2) { +QTAILQ_REMOVE (&port->unflushed_buffers, buf, next); +free_buf (buf); + } +} +/* Functions for use inside qemu to open and read from/write to ports */ int virtio_serial_open (VirtIOSerialPort *port) {
@@ -170,6 +375,10 @@ int virtio_serial_open(VirtIOSerialPort *port) port->host_connected = true;send_control_event (port,VIRTIO_CONSOLE_PORT_OPEN ,1 ); +/* Flush any buffers that were cached while the port was closed */ +if (port->cache_buffers && port->info->have_data) { +flush_queue (port); + }return 0 ; }
@@ -178,6 +387,9 @@ int virtio_serial_close(VirtIOSerialPort *port) port->host_connected = false;send_control_event (port,VIRTIO_CONSOLE_PORT_OPEN ,0 ); +if (!port->cache_buffers) { +remove_port_buffers (port); + }return 0 ; }
@@ -265,6 +477,13 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)send_control_msg (port, buffer, buffer_len);qemu_free (buffer); } +/* +* We also want to signal to the guest whether or not the port +* is set to caching the buffers when disconnected. +*/ +if (port->cache_buffers) { +send_control_event (port,VIRTIO_CONSOLE_CACHE_BUFFERS ,1 ); + }if (port->host_connected) {send_control_event (port,VIRTIO_CONSOLE_PORT_OPEN ,1 ); }
@@ -312,6 +531,10 @@ static void control_out(VirtIODevice *vdev, VirtQueue *vq)/* * Guest wrote something to some port. +* +* Flush the data in the entire chunk that we received rather than +* splitting it into multiple buffers. VNC clients don't consume split +* buffers */ static void handle_output (VirtIODevice *vdev,VirtQueue *vq) {
@@ -322,6 +545,7 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq)while (virtqueue_pop (vq, &elem)) {VirtIOSerialPort *port; +VirtIOSerialPortBuffer *buf;struct virtio_console_header header;int header_len;
@@ -330,6 +554,9 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq)if (elem.out_sg[0 ].iov_len < header_len) { goto next_buf; } +if (header_len) { +memcpy (&header, elem.out_sg[0 ].iov_base, header_len); + } port =find_port_by_vq (vser, vq);if (!port) { goto next_buf;
@@ -344,13 +571,38 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq) }/* The guest always sends only one sg */ - port->info->have_data (port, elem.out_sg[0 ].iov_base + header_len, - elem.out_sg[0 ].iov_len - header_len); + buf =alloc_buf (elem.out_sg[0 ].iov_len - header_len); +memcpy (buf->buf, elem.out_sg[0 ].iov_base + header_len, buf->len); + +if (header_len) { +/* +* Only the first buffer in a stream will have this +* set. This will help us identify the first buffer and +* the remaining buffers in the stream based on length +*/ + buf->flags =le32_to_cpu (header.flags) + & (VIRTIO_CONSOLE_HDR_START_DATA |VIRTIO_CONSOLE_HDR_END_DATA ); + }else { +/* We always want to flush all the buffers in this case */ + buf->flags =VIRTIO_CONSOLE_HDR_START_DATA + |VIRTIO_CONSOLE_HDR_END_DATA ; + } +QTAILQ_INSERT_TAIL (&port->unflushed_buffers, buf, next); + port->nr_bytes += buf->len; + port->has_activity = true; + +if (!port->host_throttled && port->byte_limit && + port->nr_bytes >= port->byte_limit) { + + port->host_throttled = true; +send_control_event (port,VIRTIO_CONSOLE_THROTTLE_PORT ,1 ); + } next_buf:virtqueue_push (vq, &elem, elem.out_sg[0 ].iov_len); }virtio_notify (vdev, vq); +flush_all_ports (vser); }static void handle_input (VirtIODevice *vdev,VirtQueue *vq)
@@ -391,6 +643,7 @@ static void virtio_serial_save(QEMUFile *f, void *opaque)VirtIOSerial *s = opaque;VirtIOSerialPort *port;uint32_t nr_active_ports; +unsigned int nr_bufs;/* The virtio device */ virtio_save (&s->vdev, f);
@@ -413,14 +666,34 @@ static void virtio_serial_save(QEMUFile *f, void *opaque)* Items in struct VirtIOSerialPort. */ QTAILQ_FOREACH(port, &s->ports, next) { +VirtIOSerialPortBuffer *buf; +/* * 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_be64s (f, &port->byte_limit); +qemu_put_be64s (f, &port->nr_bytes);qemu_put_byte (f, port->guest_connected); +qemu_put_byte (f, port->host_throttled); +/* All the pending buffers from active ports */ + nr_bufs =0 ; + QTAILQ_FOREACH(buf, &port->unflushed_buffers, next) { + nr_bufs++; + } +qemu_put_be32s (f, &nr_bufs); +if (!nr_bufs) { +continue ; + } + QTAILQ_FOREACH(buf, &port->unflushed_buffers, next) { +qemu_put_be64s (f, &buf->len); +qemu_put_be64s (f, &buf->offset); +qemu_put_be32s (f, &buf->flags); +qemu_put_buffer (f, buf->buf, buf->len); + } } }
@@ -452,13 +725,34 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)/* Items in struct VirtIOSerialPort */ for (i =0 ; i < nr_active_ports; i++) { +VirtIOSerialPortBuffer *buf;uint32_t id; +unsigned int nr_bufs; id =qemu_get_be32 (f); port =find_port_by_id (s, id); + port->byte_limit =qemu_get_be64 (f); + port->nr_bytes =qemu_get_be64 (f); port->guest_connected =qemu_get_byte (f); + port->host_throttled =qemu_get_byte (f); + +/* All the pending buffers from active ports */ +qemu_get_be32s (f, &nr_bufs); +if (!nr_bufs) { +continue ; + } +for (; nr_bufs; nr_bufs--) { +size_t len; +qemu_get_be64s (f, &len); + buf =alloc_buf (len); + +qemu_get_be64s (f, &buf->offset); +qemu_get_be32s (f, &buf->flags); +qemu_get_buffer (f, buf->buf, buf->len); +QTAILQ_INSERT_TAIL (&port->unflushed_buffers, buf, next); + } }return 0 ; }
@@ -495,6 +789,10 @@ static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) indent, "", port->guest_connected);monitor_printf (mon,"%*s dev-prop-int: host_connected: %d\n" , indent,"" , port->host_connected); +monitor_printf (mon,"%*s dev-prop-int: host_throttled: %d\n" , + indent,"" , port->host_throttled); +monitor_printf (mon,"%*s dev-prop-int: nr_bytes: %zu\n" , + indent,"" , port->nr_bytes); }static int virtser_port_qdev_init (DeviceState *qdev,DeviceInfo *base)
@@ -525,6 +823,7 @@ static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)if (ret) {return ret; } +QTAILQ_INIT (&port->unflushed_buffers); port->id = plugging_port0 ?0 : port->vser->config.nr_ports++;
@@ -567,6 +866,8 @@ static int virtser_port_qdev_exit(DeviceState *qdev)if (port->info->exit) port->info->exit (dev); +remove_port_buffers (port); +return 0 ; }
hw/virtio-serial.c +6 -5
@@ -66,13 +66,14 @@ static int virtconsole_initfn(VirtIOSerialDevice *dev) port->info = dev->info; - port->is_console = true; -/* -* For console ports, just assume the guest is ready to accept our -* data. +* We're not interested in data the guest sends while nothing is +* connected on the host side. Just ignore it instead of saving it +* for later consumption. */ - port->guest_connected = true; + port->cache_buffers =0 ; + + port->is_console = true;if (vcon->chr) { qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
hw/virtio-serial.h +39 -0
@@ -49,12 +49,18 @@ struct virtio_console_header {uint32_t flags;/* Some message between host and guest */ }; +/* Messages between host and guest */ +#define VIRTIO_CONSOLE_HDR_START_DATA (1 << 0) +#define VIRTIO_CONSOLE_HDR_END_DATA (1 << 1) +/* Some events for the internal messages (control packets) */ #define VIRTIO_CONSOLE_PORT_READY 0#define VIRTIO_CONSOLE_CONSOLE_PORT 1#define VIRTIO_CONSOLE_RESIZE 2#define VIRTIO_CONSOLE_PORT_OPEN 3#define VIRTIO_CONSOLE_PORT_NAME 4 +#define VIRTIO_CONSOLE_THROTTLE_PORT 5 +#define VIRTIO_CONSOLE_CACHE_BUFFERS 6/* == In-qemu interface == */
@@ -96,6 +102,13 @@ struct VirtIOSerialPort {char *name;/* +* This list holds buffers pushed by the guest in case the guest +* sent incomplete messages or the host connection was down and +* the device requested to cache the data. +*/ + QTAILQ_HEAD(, VirtIOSerialPortBuffer) unflushed_buffers; + +/* * 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
@@ -103,6 +116,27 @@ struct VirtIOSerialPort {*/ uint32_t id; +/* +* Each port can specify the limit on number of bytes that can be +* outstanding in the unread buffers. This is to prevent any OOM +* situtation if a rogue process on the guest keeps injecting +* data. +*/ +size_t byte_limit; + +/* +* The number of bytes we have queued up in our unread queue +*/ +size_t nr_bytes; + +/* +* This boolean, when set, means "queue data that gets sent to +* this port when the host is not connected". The queued data, if +* any, is then sent out to the port when the host connection is +* opened. +*/ +uint8_t cache_buffers; +/* Identify if this is a port that binds with hvc in the guest */ uint8_t is_console;
@@ -110,6 +144,11 @@ struct VirtIOSerialPort {bool guest_connected;/* Is this device open for IO on the host? */ bool host_connected; +/* Have we sent a throttle message to the guest? */ +bool host_throttled; + +/* Did this port get data in the recent handle_output call? */ +bool has_activity; };struct VirtIOSerialPortInfo {
Signature · 1 line
1.6.2.5
PATCH 6/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
@@ -843,6 +843,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
@@ -61,6 +61,7 @@ struct virtio_console_header {#define VIRTIO_CONSOLE_PORT_NAME 4#define VIRTIO_CONSOLE_THROTTLE_PORT 5#define VIRTIO_CONSOLE_CACHE_BUFFERS 6 +#define VIRTIO_CONSOLE_PORT_REMOVE 7/* == In-qemu interface == */
Signature · 1 line
1.6.2.5
PATCH 7/8 virtio-serial: Add 'virtserialport' device for generic serial port support
This patch adds generic serial ports over the virtio serial bus. These ports have a few more options that are not relevant for virtio console ports: the ability to cache buffers that are received for a port while it's disconnected, setting of limits to the bytes that are cached so as to prevent OOM conditions, etc.
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 | 36 ++++++++++++++++++++++++++++++++++++ 1 files changed, 36 insertions(+), 0 deletions(-)
hw/virtio-serial.c +36 -0
@@ -113,3 +113,39 @@ 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); + } +return 0 ; +} + +static VirtIOSerialPortInfo virtserialport_info = { + .qdev.name ="virtserialport" , + .qdev.size =sizeof (VirtConsole), + .init = virtserialport_initfn, + .exit = virtconsole_exitfn, + .have_data = flush_buf, + .qdev.props = (Property []) { +DEFINE_PROP_CHR ("chardev" , VirtConsole, chr), +DEFINE_PROP_STRING ("name" , VirtConsole, port.name), +DEFINE_PROP_UINT8 ("cache_buffers" , VirtConsole, port.cache_buffers,1 ), +DEFINE_PROP_UINT64 ("byte_limit" , VirtConsole, port.byte_limit,0 ), +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
PATCH 8/8 Move virtio-serial and virtio-serial-bus to Makefile.hw
There's nothing target-dependent in the virtio-serial code so allow it to be compiled just once for all the targets.
--- Makefile.hw | 2 +- Makefile.target | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Makefile.hw +1 -1
@@ -13,7 +13,7 @@ QEMU_CFLAGS+=-I.. -I$(SRC_PATH)/fpu obj-y = obj-y += loader.o -obj-y += virtio.o +obj-y += virtio.o virtio-serial.o virtio-serial-bus.o obj-y += fw_cfg.o obj-y += watchdog.o obj-$(CONFIG_ECC) += ecc.o
Makefile.target +1 -1
@@ -156,7 +156,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 obj-$(CONFIG_KVM) += kvm.o kvm-all.o obj-$(CONFIG_ISA_MMIO) += isa_mmio.o LIBS+=-lz
Signature · 1 line
1.6.2.5
Re: [Qemu-devel] [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus
In reply to Amit ShahQuoted 95 lines · Amit Shah
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.
Signed-off-by Amit Shah<[email protected]>--- Makefile.target | 2 +- hw/pc.c | 11 +- hw/ppc440_bamboo.c | 7 - hw/qdev.c | 8 +- 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 | 554 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-serial.c | 107 ++++++++++ hw/virtio-serial.h | 173 +++++++++++++++ hw/virtio.h | 2 +- qemu-options.hx | 4 + sysemu.h | 6 - vl.c | 17 ++- 17 files changed, 879 insertions(+), 214 deletions(-)delete mode 100644
hw/virtio-console.c
delete mode 100644hw/virtio-console.h
create mode 100644hw/virtio-serial-bus.c
create mode 100644hw/virtio-serial.c
create mode 100644hw/virtio-serial.hMakefile.target +1
-1@@ -156,7 +156,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+=-lzhw/pc.c +0
-9@@ -1242,15 +1242,6 @@ static void pc_init1(ram_addr_t ram_size, } } -/* 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" ); - } - } - } -rom_load_fw (fw_cfg); }
hw/pc.c static QEMUMachine pc_machine_v0_10 = { · line 1303
Quoted 6 lines
hw/pc.c +1
-1@@ -1308,7 +1299,7 @@ static QEMUMachine pc_machine_v0_10 = { .property = "class", .value = stringify(PCI_CLASS_STORAGE_OTHER), },{ - .driver = "virtio-console-pci", + .driver = "virtio-serial-pci",
I don't think we can eliminate the virtio-console-pci device name. If someone used -writeconfig and -virtconsole in 0.12, this change would break their written config files.
hw/pc.c void qdev_machine_creation_done(void) · line 326
Quoted 8 lines
hw/pc.c +1
-1@@ -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. */
Minor nit, this comment is no longer needed.
Quoted 198 lines
hw/pc.c +1
-5-if (strncmp (dev->info->name,"virtio" ,6 ) ==0 ) { -return virtcon_hds[next_virtconsole++]; - }else { -return serial_hds[next_serial++]; - } +return serial_hds[next_serial++]; }BusState *qdev_get_parent_bus(DeviceState *dev)
hw/virtio-pci.c +9
-4@@ -92,6 +92,8 @@ typedef struct {uint32_t nvectors;DriveInfo *dinfo;NICConf nic; +/* Max. number of ports we can have for a the virtio-serial device */ +uint32_t max_virtserial_ports; } VirtIOPCIProxy;/* virtio device */ @@ -481,7 +483,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;@@ -491,7 +493,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 ; }@@ -569,12 +571,15 @@ 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_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 +133
-0@@ -0,0 +1,554 @@ +/* +* A bus for connecting virtio serial and console ports +* +* Copyright (C) 2009 Red Hat, Inc. +* +* Author(s): +* Amit Shah<[email protected]> +* +* Some earlier parts are: +* Copyright IBM, Corp. 2008 +* authored by +* 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. +*/ + +#include "monitor.h" +#include "qemu-queue.h" +#include "sysbus.h" +#include "virtio-serial.h" + +/* The virtio-serial bus on top of which the ports will ride as devices */ +struct VirtIOSerialBus { +BusState qbus; + +/* This is the parent device that provides the bus for ports. */ +VirtIOSerial *vser; + +/* The maximum number of ports that can ride on top of this bus */ +uint32_t max_nr_ports; +}; + +struct VirtIOSerial { +VirtIODevice vdev; + +VirtQueue *c_ivq, *c_ovq; +/* Arrays of ivqs and ovqs: one per port */ +VirtQueue **ivqs, **ovqs; + +VirtIOSerialBus *bus; + + QTAILQ_HEAD(, VirtIOSerialPort) ports; +struct virtio_console_config config; + +uint32_t guest_features; +}; + +static VirtIOSerialPort *find_port_by_id (VirtIOSerial *vser,uint32_t id) +{ +VirtIOSerialPort *port; + + QTAILQ_FOREACH(port,&vser->ports, next) { +if (port->id == id) +return port; + } +return NULL ; +} + +static VirtIOSerialPort *find_port_by_vq (VirtIOSerial *vser,VirtQueue *vq) +{ +VirtIOSerialPort *port; + + QTAILQ_FOREACH(port,&vser->ports, next) { +if (port->ivq == vq || port->ovq == vq) +return port; + } +return NULL ; +} + +static bool use_multiport (VirtIOSerial *vser) +{ +return vser->guest_features& (1 <<VIRTIO_CONSOLE_F_MULTIPORT ); +} + +static size_t write_to_port (VirtIOSerialPort *port, +const uint8_t *buf,size_t size) +{ +VirtQueueElement elem; +struct virtio_console_header header; +VirtQueue *vq; +size_t offset =0 ; +size_t len =0 ; +int header_len; + + vq = port->ivq; +if (!virtio_queue_ready (vq)) { +return 0 ; + } +if (!size) { +return 0 ; + } + header.flags =0 ; + header_len =use_multiport (port->vser) ?sizeof (header) :0 ; + +while (offset< size) { +int i; + +if (!virtqueue_pop (vq,&elem)) { +break ; + } +if (elem.in_sg[0 ].iov_len< header_len) { +/* We can't even store our port number in this buffer. Bug? */ +qemu_error ("virtio-serial: size %zd less than expected\n" , + elem.in_sg[0 ].iov_len); +exit (1 ); + } +if (header_len) { +memcpy (elem.in_sg[0 ].iov_base,&header, header_len); + } + +for (i =0 ; offset< size&& i< elem.in_num; i++) { +/* Copy the header only in the first sg. */ + len =MIN (elem.in_sg[i].iov_len - header_len, size - offset); + +memcpy (elem.in_sg[i].iov_base + header_len, buf + offset, len); + offset += len; + header_len =0 ; + } + header_len =use_multiport (port->vser) ?sizeof (header) :0 ; +virtqueue_push (vq,&elem, len + header_len); + } + +virtio_notify (&port->vser->vdev, vq); +return offset; +} + +static size_t send_control_msg (VirtIOSerialPort *port,void *buf,size_t len) +{ +VirtQueueElement elem; +VirtQueue *vq; +struct virtio_console_control *cpkt; +
hw/virtio-serial-bus.c line 143
Quoted 10 lines
hw/virtio-serial-bus.c new file +10
-0+ vq = port->vser->c_ivq; +if (!virtio_queue_ready (vq)) { +return 0 ; + } +if (!virtqueue_pop (vq,&elem)) { +return 0 ; + } + + cpkt = (struct virtio_console_control *)buf; + cpkt->id =cpu_to_le32 (port->id);
This is not the right way to deal with endianness. The guest should write these fields in whatever their native endianness is. From within qemu, we should access this fields with ldl_p/stl_p. For x86-on-x86, the effect is a nop. For TCG with le-on-be, it becomes a byte swap.+static void control_in(VirtIODevice *vdev, VirtQueue *vq)
Quoted 11 lines
hw/virtio-serial-bus.c new file +10
-0+static int virtio_serial_load (QEMUFile *f,void *opaque,int version_id) +{ +VirtIOSerial *s = opaque; + +if (version_id>2 ) { +return -EINVAL ; + } +/* The virtio device */ +virtio_load (&s->vdev, f); +
hw/virtio-serial-bus.c
Quoted 10 lines
hw/virtio-serial-bus.c new file +10
-0+if (version_id<2 ) { +return 0 ; + } +/* The config space */ +qemu_get_be16s (f,&s->config.cols); +qemu_get_be16s (f,&s->config.rows); + s->config.nr_ports =qemu_get_be32 (f); + +/* Items in struct VirtIOSerial */ +qemu_get_be32s (f,&s->guest_features);
Why save a copy of this when you can access it through the virtio device?
hw/virtio-serial-bus.c line 409
Quoted 11 lines
hw/virtio-serial-bus.c new file +10
-0+static void virtser_bus_dev_print (Monitor *mon,DeviceState *qdev,int indent) +{ +VirtIOSerialDevice *dev =DO_UPCAST (VirtIOSerialDevice, qdev, qdev); +VirtIOSerialPort *port =DO_UPCAST (VirtIOSerialPort, dev,&dev->qdev); + +monitor_printf (mon,"%*s dev-prop-int: id: %u\n" , + indent,"" , port->id); +monitor_printf (mon,"%*s dev-prop-int: is_console: %d\n" , + indent,"" , port->is_console); +}
This will break the build since it's not referenced anywhere.
Regards,
Anthony Liguori
Re: [Qemu-devel] [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus
Hey Anthony
[skipping the part Gerd already answered]
Quoted 15 lines · Anthony Liguori
Quoted 15 lines
hw/virtio-serial-bus.c new file +15
-0+static size_t send_control_msg (VirtIOSerialPort *port,void *buf,size_t len) +{ +VirtQueueElement elem; +VirtQueue *vq; +struct virtio_console_control *cpkt; + + vq = port->vser->c_ivq; +if (!virtio_queue_ready (vq)) { +return 0 ; + } +if (!virtqueue_pop (vq,&elem)) { +return 0 ; + } + + cpkt = (struct virtio_console_control *)buf;
Quoted 8 lines
Quoted 2 lines
+
cpkt->id=cpu_to_le32(port->id);This is not the right way to deal with endianness. The guest should write these fields in whatever their native endianness is. From within qemu, we should access this fields with ldl_p/stl_p. For x86-on-x86, the effect is a nop. For TCG with le-on-be, it becomes a byte swap.
Yes, as indicated in a separate thread, I've done that in my dev tree. I'm just waiting for comments here before sending a new version.
Quoted 15 lines
Quoted 15 lines
hw/virtio-serial-bus.c new file +15
-0+static int virtio_serial_load (QEMUFile *f,void *opaque,int version_id) +{ +VirtIOSerial *s = opaque; + +if (version_id>2 ) { +return -EINVAL ; + } +/* The virtio device */ +virtio_load (&s->vdev, f); + +if (version_id<2 ) { +return 0 ; + } +/* The config space */ +qemu_get_be16s (f,&s->config.cols);
Quoted 8 lines
hw/virtio-serial-bus.c
Quoted 6 lines
hw/virtio-serial-bus.c new file +5
-0+qemu_get_be16s (f,&s->config.rows); + s->config.nr_ports =qemu_get_be32 (f); + +/* Items in struct VirtIOSerial */ +qemu_get_be32s (f,&s->guest_features);Why save a copy of this when you can access it through the virtio device?
Hm, not needed here I guess.
Quoted 5 lines
Quoted 5 lines
hw/virtio-serial-bus.c new file +5
-0+static void virtser_bus_dev_print (Monitor *mon,DeviceState *qdev,int indent) +{ +VirtIOSerialDevice *dev =DO_UPCAST (VirtIOSerialDevice, qdev, qdev); +VirtIOSerialPort *port =DO_UPCAST (VirtIOSerialPort, dev,&dev->qdev); +
Quoted 8 lines
hw/virtio-serial-bus.c line 409
Quoted 6 lines
hw/virtio-serial-bus.c new file +5
-0+monitor_printf (mon,"%*s dev-prop-int: id: %u\n" , + indent,"" , port->id); +monitor_printf (mon,"%*s dev-prop-int: is_console: %d\n" , + indent,"" , port->is_console); +}This will break the build since it's not referenced anywhere.
Again, as mentioned in the other thread, it gets used here:
static struct BusInfo virtser_bus_info = {
.name = "virtio-serial-bus",
.size = sizeof(VirtIOSerialBus),
.print_dev = virtser_bus_dev_print,
};
(I've compile- and run- tested these patches.)
Amit
Re: [Qemu-devel] [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus
Quoted 16 lines · Amit Shah
Quoted 5 lines
Quoted 5 lines
hw/virtio-serial-bus.c new file +5
-0+static void virtser_bus_dev_print (Monitor *mon,DeviceState *qdev,int indent) +{ +VirtIOSerialDevice *dev =DO_UPCAST (VirtIOSerialDevice, qdev, qdev); +VirtIOSerialPort *port =DO_UPCAST (VirtIOSerialPort, dev,&dev->qdev); +Quoted 9 lines
hw/virtio-serial-bus.c line 409
Quoted 7 lines
hw/virtio-serial-bus.c new file +5
-0+monitor_printf (mon,"%*s dev-prop-int: id: %u\n" , + indent,"" , port->id); +monitor_printf (mon,"%*s dev-prop-int: is_console: %d\n" , + indent,"" , port->is_console); +}This will break the build since it's not referenced anywhere.
Again, as mentioned in the other thread, it gets used here:
Quoted 8 lines
static struct BusInfo
virtser_bus_info= {
.name = "virtio-serial-bus",
.size = sizeof(VirtIOSerialBus),
.print_dev =virtser_bus_dev_print,
};(I've compile- and run- tested these patches.)
Okay.
Quoted 2 lines
Amit
Re: [Qemu-devel] [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus
In reply to Anthony LiguoriHi,
Quoted 6 lines
Quoted 2 lines
- .driver = "virtio-console-pci",
+ .driver = "virtio-serial-pci",I don't think we can eliminate the virtio-console-pci device name. If someone used -writeconfig and -virtconsole in 0.12, this change would break their written config files.
It wouldn't. virtio-console-pci was never fully converted to qdev because of the multichannel work in flight. Thus you can't create a functional device using '-device virtio-console-pci,<properties>' because the driver lacks a chardev property.
Quoted 8 lines
Quoted 6 lines
CharDriverState *qdev_init_chardev(DeviceState *dev) {
static intnext_serial;
- static intnext_virtconsole;
+
/* FIXME: This is a nasty hack that needs to go away. */Minor nit, this comment is no longer needed.
The whole function is a nasty hack which needs to go away. The RightWay[tm] is to use a chardev property instead of this function.
So the comment is still correct. Maybe some rewording is helpful though to make this more clear as the comment got a big bunch of review comments already ;)
cheers,
Gerd
Re: [Qemu-devel] [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus
Quoted 10 lines · Gerd Hoffmann
Hi,
Quoted 6 lines
Quoted 2 lines
- .driver = "virtio-console-pci",
+ .driver = "virtio-serial-pci",I don't think we can eliminate the virtio-console-pci device name. If someone used -writeconfig and -virtconsole in 0.12, this change would break their written config files.
It wouldn't. virtio-console-pci was never fully converted to qdev because of the multichannel work in flight. Thus you can't create a functional device using '-device virtio-console-pci,<properties>' because the driver lacks a chardev property.
Ah, okay.
Quoted 5 lines
Quoted 1 line
Minor nit, this comment is no longer needed.
The whole function is a nasty hack which needs to go away. The RightWay[tm] is to use a chardev property instead of this function.
So the comment is still correct. Maybe some rewording is helpful though to make this more clear as the comment got a big bunch of review comments already ;)
Okay, then a small rewording with that point would be helpful. It wasn't clear to me that the function should become a property.
Quoted 2 lines
cheers,
Gerd
[Qemu-devel] Re: [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus
In reply to Amit ShahHi,
hw/virtio-serial-bus.c line 221
Quoted 6 lines
hw/virtio-serial-bus.c new file +5
-0+/* Guest wants to notify us of some event */ +static void handle_control_message (VirtIOSerial *vser,void *buf) +{ + cpkt->event =le16_to_cpu (cpkt->event); + cpkt->value =le16_to_cpu (cpkt->value);
Does this modify guest memory?
Quoted 1 line
+
DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1),
virtser_bus_dev_print() prints this. Properties are printed by "info qtree" anyway, so you can drop that from virtser_bus_dev_print ...
cheers,
Gerd
[Qemu-devel] Re: [PATCH 2/8] virtio-console: qdev conversion, new virtio-serial-bus
Quoted 10 lines · Gerd Hoffmann
Hi,
hw/virtio-serial-bus.c line 221
Quoted 6 lines
hw/virtio-serial-bus.c new file +5
-0+/* Guest wants to notify us of some event */ +static void handle_control_message (VirtIOSerial *vser,void *buf) +{ + cpkt->event =le16_to_cpu (cpkt->event); + cpkt->value =le16_to_cpu (cpkt->value);Does this modify guest memory?
Hm, I don't want to. I'll use a shadow struct.
Quoted 5 lines
Quoted 2 lines
+
DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1),
virtser_bus_dev_print()prints this. Properties are printed by "info qtree" anyway, so you can drop that fromvirtser_bus_dev_print...
I had to make is_console a property for other reasons; I'll remove it from here.
Amit