Skip to contentSkip to conversation

v4: virtio-serial fixes, new abi for port discovery

PATCH Superseded Amit Shah 3 participants 23 messages Mar 31, 2010 – Apr 1, 2010 Kbuild

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Amit Shah

author
#0 lore raw

cover v4: virtio-serial fixes, new abi for port discovery

Hello,

These patches rework the way ports are announced to the guests. A control message is used to let the guest know a new port is added. Initial port discovery and port hot-plug work via this way now.

This was done to have the host and guest port numbering in sync to avoid surprises after several hotplug/unplug operations and migrations.

The ability to assign a particular port number to ports is also added so that management software can control the placement of ports.

Differences from v3:
- Removed the QMP events (will ride on top of new infrastructure by Luiz)

- Add flow control: ports can signal to the virtio-serial code to stop sending data till it's ready to accept more.

- A fix for not abusing the virtqueue_push() api: we should signal to the guest the number of bytes written for guest to consume. We were using the number to signify the bytes consumed by hosts.

- Juan's comment about re-using control queue buffers

Overall:
- Users can set the port id they wish to instantiate ports at by using the ,nr= parameter to 'virtserialport' and 'virtconsole' devices

- Migration fixes: refuse migration when:
- number of active ports is different between the src and destination
- max_nr_ports a device can support on the src is more

- If a qemu chardev connection to a port is closed on the dest while it was open on the src, inform the guest about this. (Also do the same for port closed on src but open on dest.)

- Use control messages for relaying new port information instead of config space (changes abi)

- Propagate error message from guest in instantiating a port or a device to the user.

- Handle scatter/gather for control output and data output from the guest

- Fix abuse of virtio api in the virtqueue_push() function

- Add an API for the ports for flow control: ports can signal when they're ready to accept data / stop sending data.

Amit Shah (17):
virtio-serial: save/load: Ensure target has enough ports
virtio-serial: save/load: Ensure nr_ports on src and dest are same.
virtio-serial: save/load: Ensure we have hot-plugged ports
instantiated
virtio-serial: save/load: Send target host connection status if
different
virtio-serial: Use control messages to notify guest of new ports
virtio-serial: whitespace: match surrounding code
virtio-serial: Remove redundant check for 0-sized write request
virtio-serial: Update copyright year to 2010
virtio-serial: Propagate errors in initialising ports / devices in
guest
virtio-serial: Send out guest data to ports only if port is opened
iov: Introduce a new file for helpers around iovs, add iov_from_buf()
iov: Add iov_to_buf and iov_size helpers
virtio-serial: Handle scatter-gather buffers for control messages
virtio-serial: Handle scatter/gather input from the guest
virtio-serial: Apps should consume all data that guest sends out /
Fix virtio api abuse
virtio-serial: Discard data that guest sends us when ports aren't
connected
virtio-serial: Implement flow control for individual ports

 Makefile.objs          |    1 +
 hw/iov.c               |   70 ++++++++++
 hw/iov.h               |   19 +++
 hw/virtio-balloon.c    |   35 +-----
 hw/virtio-console.c    |   11 +-
 hw/virtio-net.c        |   20 +---
 hw/virtio-serial-bus.c |  326 +++++++++++++++++++++++++++++++++++------------
 hw/virtio-serial.h     |   34 ++++--
 8 files changed, 368 insertions(+), 148 deletions(-)
 create mode 100644 hw/iov.c
 create mode 100644 hw/iov.h
author
#1 lore raw

PATCH 1/17 virtio-serial: save/load: Ensure target has enough ports

The target could be started with max_nr_ports for a virtio-serial device lesser than what was available on the source machine. Fail the migration in such a case.

---
 hw/virtio-serial-bus.c |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)
hw/virtio-serial-bus.c +11 -2
index 17c1ec1..9a7f0c1 100644
@@ -374,10 +374,13 @@ static void virtio_serial_save(QEMUFile *f, void *opaque)
 
     /* Items in struct VirtIOSerial */
 
+    qemu_put_be32s(f, &s->bus->max_nr_ports);
+
     /* Do this because we might have hot-unplugged some ports */
     nr_active_ports = 0;
-    QTAILQ_FOREACH(port, &s->ports, next)
+    QTAILQ_FOREACH(port, &s->ports, next) {
         nr_active_ports++;
+    }
 
     qemu_put_be32s(f, &nr_active_ports);
 
@@ -399,7 +402,7 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
 {
     VirtIOSerial *s = opaque;
     VirtIOSerialPort *port;
-    uint32_t nr_active_ports;
+    uint32_t max_nr_ports, nr_active_ports;
     unsigned int i;
 
     if (version_id > 2) {
@@ -420,6 +423,12 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
 
     /* Items in struct VirtIOSerial */
 
+    qemu_get_be32s(f, &max_nr_ports);
+    if (max_nr_ports > s->bus->max_nr_ports) {
+        /* Source could have more ports than us. Fail migration. */
+        return -EINVAL;
+    }
+
     qemu_get_be32s(f, &nr_active_ports);
 
     /* Items in struct VirtIOSerialPort */
Signature · 1 line
1.6.2.5
author
#2 lore raw

PATCH 2/17 virtio-serial: save/load: Ensure nr_ports on src and dest are same.

The number of ports on the source as well as the destination machines should match. If they don't, it means some ports that got hotplugged on the source aren't instantiated on the destination. Or that ports that were hot-unplugged on the source are created on the destination.

---
 hw/virtio-serial-bus.c |   18 ++++++++++++++++--
 1 files changed, 16 insertions(+), 2 deletions(-)
hw/virtio-serial-bus.c +16 -2
index 9a7f0c1..d31e62d 100644
@@ -402,7 +402,7 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
 {
     VirtIOSerial *s = opaque;
     VirtIOSerialPort *port;
-    uint32_t max_nr_ports, nr_active_ports;
+    uint32_t max_nr_ports, nr_active_ports, nr_ports;
     unsigned int i;
 
     if (version_id > 2) {
@@ -419,7 +419,21 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
     /* 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);
+    nr_ports = qemu_get_be32(f);
+
+    if (nr_ports != s->config.nr_ports) {
+        /*
+         * Source hot-plugged/unplugged ports and we don't have all of
+         * them here.
+         *
+         * Note: This condition cannot check for all hotplug/unplug
+         * events: eg, if one port was hot-plugged and one was
+         * unplugged, the nr_ports remains the same but the port id's
+         * would have changed and we won't catch it here. A later
+         * check for !find_port_by_id() will confirm if this happened.
+         */
+        return -EINVAL;
+    }
 
     /* Items in struct VirtIOSerial */
 
Signature · 1 line
1.6.2.5
author
#3 lore raw

PATCH 3/17 virtio-serial: save/load: Ensure we have hot-plugged ports instantiated

If some ports that were hot-plugged on the source are not available on the destination, fail migration instead of trying to deref a NULL pointer.

---
 hw/virtio-serial-bus.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +7 -0
index d31e62d..5316ef6 100644
@@ -451,6 +451,13 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
 
         id = qemu_get_be32(f);
         port = find_port_by_id(s, id);
+        if (!port) {
+            /*
+             * The requested port was hot-plugged on the source but we
+             * don't have it
+             */
+            return -EINVAL;
+        }
 
         port->guest_connected = qemu_get_byte(f);
     }
Signature · 1 line
1.6.2.5
author
#4 lore raw

PATCH 4/17 virtio-serial: save/load: Send target host connection status if different

If the host connection to a port is closed on the destination machine after migration, whereas the connection was open on the source, the guest has to be informed of that.

Similar for a host connection open on the destination.

---
 hw/virtio-serial-bus.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +11 -0
index 5316ef6..484dc94 100644
@@ -395,6 +395,7 @@ static void virtio_serial_save(QEMUFile *f, void *opaque)
          */
         qemu_put_be32s(f, &port->id);
         qemu_put_byte(f, port->guest_connected);
+        qemu_put_byte(f, port->host_connected);
     }
 }
 
@@ -448,6 +449,7 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
     /* Items in struct VirtIOSerialPort */
     for (i = 0; i < nr_active_ports; i++) {
         uint32_t id;
+        bool host_connected;
 
         id = qemu_get_be32(f);
         port = find_port_by_id(s, id);
@@ -460,6 +462,15 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
         }
 
         port->guest_connected = qemu_get_byte(f);
+        host_connected = qemu_get_byte(f);
+        if (host_connected != port->host_connected) {
+            /*
+             * We have to let the guest know of the host connection
+             * status change
+             */
+            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
+                               port->host_connected);
+        }
     }
 
     return 0;
Signature · 1 line
1.6.2.5
author
#5 lore raw

PATCH 5/17 virtio-serial: Use control messages to notify guest of new ports

Allow the port 'id's to be set by a user on the command line. This is needed by management apps that will want a stable port numbering scheme for hot-plug/unplug and migration.

Since the port numbers are shared with the guest (to identify ports in control messages), we just send a control message to the guest indicating addition of new ports (hot-plug) or notifying the guest of the available ports when the guest sends us a DEVICE_READY control message.

---
 hw/virtio-console.c    |    2 +
 hw/virtio-serial-bus.c |  181 +++++++++++++++++++++++++++++++-----------------
 hw/virtio-serial.h     |   17 +++--
 3 files changed, 130 insertions(+), 70 deletions(-)
hw/virtio-console.c +2 -0
index bd44ec6..17b221d 100644
@@ -99,6 +99,7 @@ static VirtIOSerialPortInfo virtconsole_info = {
     .exit          = virtconsole_exitfn,
     .qdev.props = (Property[]) {
         DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1),
+        DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
         DEFINE_PROP_CHR("chardev", VirtConsole, chr),
         DEFINE_PROP_STRING("name", VirtConsole, port.name),
         DEFINE_PROP_END_OF_LIST(),
@@ -133,6 +134,7 @@ static VirtIOSerialPortInfo virtserialport_info = {
     .init          = virtserialport_initfn,
     .exit          = virtconsole_exitfn,
     .qdev.props = (Property[]) {
+        DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
         DEFINE_PROP_CHR("chardev", VirtConsole, chr),
         DEFINE_PROP_STRING("name", VirtConsole, port.name),
         DEFINE_PROP_END_OF_LIST(),
hw/virtio-serial-bus.c +118 -63

292 lines not on this page · raw message

hw/virtio-serial.h +10 -7
index f297b00..0548689 100644
@@ -27,6 +27,8 @@
 /* Features supported */
 #define VIRTIO_CONSOLE_F_MULTIPORT	1
 
+#define VIRTIO_CONSOLE_BAD_ID           (~(uint32_t)0)
+
 struct virtio_console_config {
     /*
      * These two fields are used by VIRTIO_CONSOLE_F_SIZE which
@@ -36,7 +38,6 @@ struct virtio_console_config {
     uint16_t rows;
 
     uint32_t max_nr_ports;
-    uint32_t nr_ports;
 } __attribute__((packed));
 
 struct virtio_console_control {
@@ -46,12 +47,14 @@ struct virtio_console_control {
 };
 
 /* 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_PORT_REMOVE	5
+#define VIRTIO_CONSOLE_DEVICE_READY	0
+#define VIRTIO_CONSOLE_PORT_ADD		1
+#define VIRTIO_CONSOLE_PORT_REMOVE	2
+#define VIRTIO_CONSOLE_PORT_READY	3
+#define VIRTIO_CONSOLE_CONSOLE_PORT	4
+#define VIRTIO_CONSOLE_RESIZE		5
+#define VIRTIO_CONSOLE_PORT_OPEN	6
+#define VIRTIO_CONSOLE_PORT_NAME	7
 
 /* == In-qemu interface == */
 
Signature · 1 line
1.6.2.5
author
#6 lore raw

PATCH 6/17 virtio-serial: whitespace: match surrounding code

The virtio-serial code doesn't mix declarations and definitions, so separate them out on different lines.

---
 hw/virtio-serial-bus.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
hw/virtio-serial-bus.c +4 -1
index 00e8616..80f0259 100644
@@ -354,7 +354,10 @@ static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
 
 static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
 {
-    VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
+    VirtIOSerial *vser;
+
+    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
+
     if (vser->bus->max_nr_ports > 1) {
         features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
     }
Signature · 1 line
1.6.2.5
author
#15 lore raw

PATCH 15/17 virtio-serial: Apps should consume all data that guest sends out / Fix virtio api abuse

In reply to Amit Shah

We cannot indicate to the guest how much data was consumed by an app for out_bufs. So we just have to assume the apps will consume all the data that are handed over to them.

Fix the virtio api abuse in control_out() and handle_output().

---
 hw/virtio-console.c    |    7 ++-----
 hw/virtio-serial-bus.c |    6 +++---
 hw/virtio-serial.h     |    6 +++---
 3 files changed, 8 insertions(+), 11 deletions(-)
hw/virtio-console.c +2 -5
index bbbb6b8..caea11f 100644
@@ -20,14 +20,11 @@ typedef struct VirtConsole {
 
 
 /* Callback function that's called when the guest sends us data */
-static size_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
+static void flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
 {
     VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
-    ssize_t ret;
 
-    ret = qemu_chr_write(vcon->chr, buf, len);
-
-    return ret < 0 ? 0 : ret;
+    qemu_chr_write(vcon->chr, buf, len);
 }
 
 /* Readiness of the guest to accept data on a port */
hw/virtio-serial-bus.c +3 -3
index 49a6baa..7ac46f5 100644
@@ -336,7 +336,7 @@ static void control_out(VirtIODevice *vdev, VirtQueue *vq)
         copied = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, len);
 
         handle_control_message(vser, buf, copied);
-        virtqueue_push(vq, &elem, copied);
+        virtqueue_push(vq, &elem, 0);
     }
     if (len) {
         qemu_free(buf);
@@ -382,11 +382,11 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
         buf = qemu_malloc(buf_size);
         ret = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, buf_size);
 
-        ret = port->info->have_data(port, buf, ret);
+        port->info->have_data(port, buf, ret);
         qemu_free(buf);
 
     next_buf:
-        virtqueue_push(vq, &elem, ret);
+        virtqueue_push(vq, &elem, 0);
     }
     virtio_notify(vdev, vq);
 }
hw/virtio-serial.h +3 -3
index f023873..62d76a2 100644
@@ -136,10 +136,10 @@ struct VirtIOSerialPortInfo {
 
     /*
      * Guest wrote some data to the port. This data is handed over to
-     * the app via this callback. The app should return the number of
-     * bytes it successfully consumed.
+     * the app via this callback.  The app is supposed to consume all
+     * the data that is presented to it.
      */
-    size_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf, size_t len);
+    void (*have_data)(VirtIOSerialPort *port, const uint8_t *buf, size_t len);
 };
 
 /* Interface to the virtio-serial bus */
Signature · 1 line
1.6.2.5
author
#17 lore raw

PATCH 17/17 virtio-serial: Implement flow control for individual ports

In reply to Amit Shah

Individual ports can now signal to the virtio-serial core to stop sending data if the ports cannot immediately handle new data. When a port later unthrottles, any data queued up in the virtqueue are sent to the port.

---
 hw/virtio-serial-bus.c |   24 ++++++++++++++++++++++++
 hw/virtio-serial.h     |    9 +++++++++
 2 files changed, 33 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +24 -0
index 757de7c..7b57ed8 100644
@@ -187,6 +187,11 @@ int virtio_serial_close(VirtIOSerialPort *port)
     port->host_connected = false;
     send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
 
+    /*
+     * If there's any data the guest sent which the app didn't
+     * consume, discard it.
+     */
+    flush_queued_data(port, true);
     return 0;
 }
 
@@ -226,6 +231,21 @@ size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
     return 0;
 }
 
+void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
+{
+    if (!port) {
+        return;
+    }
+
+    if (throttle) {
+        port->throttled = true;
+        return;
+    }
+
+    port->throttled = false;
+    flush_queued_data(port, false);
+}
+
 /* Guest wants to notify us of some event */
 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
 {
@@ -382,6 +402,10 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
         discard = true;
     }
 
+    if (!discard && port->throttled) {
+        return;
+    }
+
     flush_queued_data(port, discard);
 }
 
hw/virtio-serial.h +9 -0
index 62d76a2..a93b545 100644
@@ -110,6 +110,8 @@ struct VirtIOSerialPort {
     bool guest_connected;
     /* Is this device open for IO on the host? */
     bool host_connected;
+    /* Do apps not want to receive data? */
+    bool throttled;
 };
 
 struct VirtIOSerialPortInfo {
@@ -173,4 +175,11 @@ ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
  */
 size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
 
+/*
+ * Flow control: Ports can signal to the virtio-serial core to stop
+ * sending data or re-start sending data, depending on the 'throttle'
+ * value here.
+ */
+void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle);
+
 #endif
Signature · 1 line
1.6.2.5
author
#16 lore raw

PATCH 16/17 virtio-serial: Discard data that guest sends us when ports aren't connected

In reply to Amit Shah

Before the earlier patch, we relied on incorrect virtio api usage to signal to the guest that a particular buffer wasn't consumed by the host.

After fixing that, we now just discard the data the guest sends us while a host port is disconnected or doesn't have a handler registered for consuming data.

This commit really doesn't change anything from the current behaviour, just makes the code slightly better by spinning off data handling to ports in another function.

---
 hw/virtio-serial-bus.c |   68 ++++++++++++++++++++++--------------------------
 1 files changed, 31 insertions(+), 37 deletions(-)
hw/virtio-serial-bus.c +31 -37
index 7ac46f5..757de7c 100644
@@ -111,6 +111,29 @@ static size_t write_to_port(VirtIOSerialPort *port,
     return offset;
 }
 
+static void flush_queued_data(VirtIOSerialPort *port, bool discard)
+{
+    VirtQueue *vq;
+    VirtQueueElement elem;
+
+    vq = port->ovq;
+    while (virtqueue_pop(vq, &elem)) {
+        uint8_t *buf;
+        size_t ret, buf_size;
+
+        if (!discard) {
+            buf_size = iov_size(elem.out_sg, elem.out_num);
+            buf = qemu_malloc(buf_size);
+            ret = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, buf_size);
+
+            port->info->have_data(port, buf, ret);
+            qemu_free(buf);
+        }
+        virtqueue_push(vq, &elem, 0);
+    }
+    virtio_notify(&port->vser->vdev, vq);
+}
+
 static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
 {
     VirtQueueElement elem;
@@ -348,47 +371,18 @@ static void control_out(VirtIODevice *vdev, VirtQueue *vq)
 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtIOSerial *vser;
-    VirtQueueElement elem;
+    VirtIOSerialPort *port;
+    bool discard;
 
     vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
+    port = find_port_by_vq(vser, vq);
 
-    while (virtqueue_pop(vq, &elem)) {
-        VirtIOSerialPort *port;
-        uint8_t *buf;
-        size_t ret, buf_size;
-
-        port = find_port_by_vq(vser, vq);
-        if (!port) {
-            ret = 0;
-            goto next_buf;
-        }
-
-	if (!port->host_connected) {
-            ret = 0;
-            goto next_buf;
-        }
-
-        /*
-         * A port may not have any handler registered for consuming the
-         * data that the guest sends or it may not have a chardev associated
-         * with it. Just ignore the data in that case.
-         */
-        if (!port->info->have_data) {
-            ret = 0;
-            goto next_buf;
-        }
-
-        buf_size = iov_size(elem.out_sg, elem.out_num);
-        buf = qemu_malloc(buf_size);
-        ret = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, buf_size);
-
-        port->info->have_data(port, buf, ret);
-        qemu_free(buf);
-
-    next_buf:
-        virtqueue_push(vq, &elem, 0);
+    discard = false;
+    if (!port || !port->host_connected || !port->info->have_data) {
+        discard = true;
     }
-    virtio_notify(vdev, vq);
+
+    flush_queued_data(port, discard);
 }
 
 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
Signature · 1 line
1.6.2.5
author
#12 lore raw

PATCH 12/17 iov: Add iov_to_buf and iov_size helpers

In reply to Amit Shah

iov_to_buf() puts the buffer contents in the iov in a linearized buffer.

iov_size() gets the length of the contents in the iov.

The iov_to_buf() function is the memcpy_to_iovec() function that was used in virtio-ballon.c.

---
 hw/iov.c            |   37 +++++++++++++++++++++++++++++++++++++
 hw/iov.h            |    3 +++
 hw/virtio-balloon.c |   35 ++++-------------------------------
 3 files changed, 44 insertions(+), 31 deletions(-)
hw/iov.c +37 -0
index 07bd499..588cd04 100644
@@ -31,3 +31,40 @@ size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
     }
     return offset;
 }
+
+size_t iov_to_buf(const struct iovec *iov, const unsigned int iovcnt,
+                  void *buf, size_t offset, size_t size)
+{
+    uint8_t *ptr;
+    size_t iov_off, buf_off;
+    unsigned int i;
+
+    ptr = buf;
+    iov_off = 0;
+    buf_off = 0;
+    for (i = 0; i < iovcnt && size; i++) {
+        if (offset < (iov_off + iov[i].iov_len)) {
+            size_t len = MIN((iov_off + iov[i].iov_len) - offset , size);
+
+            memcpy(ptr + buf_off, iov[i].iov_base + (offset - iov_off), len);
+
+            buf_off += len;
+            offset += len;
+            size -= len;
+        }
+        iov_off += iov[i].iov_len;
+    }
+    return buf_off;
+}
+
+size_t iov_size(const struct iovec *iov, const unsigned int iovcnt)
+{
+    size_t len;
+    unsigned int i;
+
+    len = 0;
+    for (i = 0; i < iovcnt; i++) {
+        len += iov[i].iov_len;
+    }
+    return len;
+}
hw/iov.h +3 -0
index 5e3e541..60a8547 100644
@@ -14,3 +14,6 @@
 
 size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
                     const void *buf, size_t size);
+size_t iov_to_buf(const struct iovec *iov, const unsigned int iovcnt,
+                  void *buf, size_t offset, size_t size);
+size_t iov_size(const struct iovec *iov, const unsigned int iovcnt);
hw/virtio-balloon.c +4 -31
index f55f7ec..152af80 100644
@@ -11,6 +11,7 @@
  *
  */
 
+#include "iov.h"
 #include "qemu-common.h"
 #include "virtio.h"
 #include "pc.h"
@@ -92,33 +93,6 @@ static QObject *get_stats_qobject(VirtIOBalloon *dev)
     return QOBJECT(dict);
 }
 
-/* FIXME: once we do a virtio refactoring, this will get subsumed into common
- * code */
-static size_t memcpy_from_iovector(void *data, size_t offset, size_t size,
-                                   struct iovec *iov, int iovlen)
-{
-    int i;
-    uint8_t *ptr = data;
-    size_t iov_off = 0;
-    size_t data_off = 0;
-
-    for (i = 0; i < iovlen && size; i++) {
-        if (offset < (iov_off + iov[i].iov_len)) {
-            size_t len = MIN((iov_off + iov[i].iov_len) - offset , size);
-
-            memcpy(ptr + data_off, iov[i].iov_base + (offset - iov_off), len);
-
-            data_off += len;
-            offset += len;
-            size -= len;
-        }
-
-        iov_off += iov[i].iov_len;
-    }
-
-    return data_off;
-}
-
 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtIOBalloon *s = to_virtio_balloon(vdev);
@@ -128,8 +102,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
         size_t offset = 0;
         uint32_t pfn;
 
-        while (memcpy_from_iovector(&pfn, offset, 4,
-                                    elem.out_sg, elem.out_num) == 4) {
+        while (iov_to_buf(elem.out_sg, elem.out_num, &pfn, offset, 4) == 4) {
             ram_addr_t pa;
             ram_addr_t addr;
 
@@ -181,8 +154,8 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
      */
     reset_stats(s);
 
-    while (memcpy_from_iovector(&stat, offset, sizeof(stat), elem->out_sg,
-                                elem->out_num) == sizeof(stat)) {
+    while (iov_to_buf(elem->out_sg, elem->out_num, &stat, offset, sizeof(stat))
+           == sizeof(stat)) {
         uint16_t tag = tswap16(stat.tag);
         uint64_t val = tswap64(stat.val);
 
Signature · 1 line
1.6.2.5
author
#11 lore raw

PATCH 11/17 iov: Introduce a new file for helpers around iovs, add iov_from_buf()

In reply to Amit Shah

The virtio-net code uses iov_fill() which fills an iov from a linear buffer. The virtio-serial-bus code does something similar in an open-coded function.

Create a new iov.c file that has iov_from_buf().

Convert virtio-net and virtio-serial-bus over to use this functionality. virtio-net used ints to hold sizes, the new function is going to use size_t types.

Later commits will add the opposite functionality -- going from an iov to a linear buffer.

---
 Makefile.objs          |    1 +
 hw/iov.c               |   33 +++++++++++++++++++++++++++++++++
 hw/iov.h               |   16 ++++++++++++++++
 hw/virtio-net.c        |   20 +++-----------------
 hw/virtio-serial-bus.c |   15 +++++++--------
 5 files changed, 60 insertions(+), 25 deletions(-)
 create mode 100644 hw/iov.c
 create mode 100644 hw/iov.h
Makefile.objs +1 -0
index 233fbba..2ab09d7 100644
@@ -129,6 +129,7 @@ user-obj-y += cutils.o cache-utils.o
 
 hw-obj-y =
 hw-obj-y += vl.o loader.o
+hw-obj-y += iov.o
 hw-obj-y += virtio.o virtio-console.o
 hw-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o
 hw-obj-y += fw_cfg.o pci.o pci_host.o pcie_host.o
hw/iov.c new file +33 -0
new file mode 100644
index 0000000..07bd499
@@ -0,0 +1,33 @@
+/*
+ * Helpers for getting linearized buffers from iov / filling buffers into iovs
+ *
+ * Copyright IBM, Corp. 2007, 2008
+ * Copyright (C) 2010 Red Hat, Inc.
+ *
+ * Author(s):
+ *  Anthony Liguori <[email protected]>
+ *  Amit Shah <[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 "iov.h"
+
+size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
+                    const void *buf, size_t size)
+{
+    size_t offset;
+    unsigned int i;
+
+    offset = 0;
+    for (i = 0; offset < size && i < iovcnt; i++) {
+        size_t len;
+
+        len = MIN(iov[i].iov_len, size - offset);
+
+        memcpy(iov[i].iov_base, buf + offset, len);
+        offset += len;
+    }
+    return offset;
+}
hw/iov.h new file +16 -0
new file mode 100644
index 0000000..5e3e541
@@ -0,0 +1,16 @@
+/*
+ * Helpers for getting linearized buffers from iov / filling buffers into iovs
+ *
+ * Copyright (C) 2010 Red Hat, Inc.
+ *
+ * Author(s):
+ *  Amit Shah <[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 "qemu-common.h"
+
+size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
+                    const void *buf, size_t size);
hw/virtio-net.c +3 -17
index be33c68..273e7f9 100644
@@ -11,6 +11,7 @@
  *
  */
 
+#include "iov.h"
 #include "virtio.h"
 #include "net.h"
 #include "net/checksum.h"
@@ -423,21 +424,6 @@ static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
     }
 }
 
-static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
-{
-    int offset, i;
-
-    offset = i = 0;
-    while (offset < count && i < iovcnt) {
-        int len = MIN(iov[i].iov_len, count - offset);
-        memcpy(iov[i].iov_base, buf + offset, len);
-        offset += len;
-        i++;
-    }
-
-    return offset;
-}
-
 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
                           const void *buf, size_t size, size_t hdr_len)
 {
@@ -573,8 +559,8 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
         }
 
         /* copy in packet.  ugh */
-        len = iov_fill(sg, elem.in_num,
-                       buf + offset, size - offset);
+        len = iov_from_buf(sg, elem.in_num,
+                           buf + offset, size - offset);
         total += len;
 
         /* signal other side */
hw/virtio-serial-bus.c +7 -8
index 236e300..ee45146 100644
@@ -15,6 +15,7 @@
  * the COPYING file in the top-level directory.
  */
 
+#include "iov.h"
 #include "monitor.h"
 #include "qemu-queue.h"
 #include "sysbus.h"
@@ -84,27 +85,25 @@ static size_t write_to_port(VirtIOSerialPort *port,
 {
     VirtQueueElement elem;
     VirtQueue *vq;
-    size_t offset = 0;
-    size_t len = 0;
+    size_t offset;
 
     vq = port->ivq;
     if (!virtio_queue_ready(vq)) {
         return 0;
     }
 
+    offset = 0;
     while (offset < size) {
-        int i;
+        size_t len;
 
         if (!virtqueue_pop(vq, &elem)) {
             break;
         }
 
-        for (i = 0; offset < size && i < elem.in_num; i++) {
-            len = MIN(elem.in_sg[i].iov_len, size - offset);
+        len = iov_from_buf(elem.in_sg, elem.in_num,
+                           buf + offset, size - offset);
+        offset += len;
 
-            memcpy(elem.in_sg[i].iov_base, buf + offset, len);
-            offset += len;
-        }
         virtqueue_push(vq, &elem, len);
     }
 
Signature · 1 line
1.6.2.5
author
#14 lore raw

PATCH 14/17 virtio-serial: Handle scatter/gather input from the guest

In reply to Amit Shah

Current guests don't send more than one iov but it can change later. Ensure we handle that case.

---
 hw/virtio-serial-bus.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
hw/virtio-serial-bus.c +8 -4
index 88fb723..49a6baa 100644
@@ -354,7 +354,8 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
 
     while (virtqueue_pop(vq, &elem)) {
         VirtIOSerialPort *port;
-        size_t ret;
+        uint8_t *buf;
+        size_t ret, buf_size;
 
         port = find_port_by_vq(vser, vq);
         if (!port) {
@@ -377,9 +378,12 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
             goto next_buf;
         }
 
-        /* The guest always sends only one sg */
-        ret = port->info->have_data(port, elem.out_sg[0].iov_base,
-                                    elem.out_sg[0].iov_len);
+        buf_size = iov_size(elem.out_sg, elem.out_num);
+        buf = qemu_malloc(buf_size);
+        ret = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, buf_size);
+
+        ret = port->info->have_data(port, buf, ret);
+        qemu_free(buf);
 
     next_buf:
         virtqueue_push(vq, &elem, ret);
Signature · 1 line
1.6.2.5
author
#8 lore raw

PATCH 8/17 virtio-serial: Update copyright year to 2010

In reply to Amit Shah
---
 hw/virtio-console.c    |    2 +-
 hw/virtio-serial-bus.c |    2 +-
 hw/virtio-serial.h     |    2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
hw/virtio-console.c +1 -1
index 17b221d..bbbb6b8 100644
@@ -1,7 +1,7 @@
 /*
  * Virtio Console and Generic Serial Port Devices
  *
- * Copyright Red Hat, Inc. 2009
+ * Copyright Red Hat, Inc. 2009, 2010
  *
  * Authors:
  *  Amit Shah <[email protected]>
hw/virtio-serial-bus.c +1 -1
index 4435c62..a19e751 100644
@@ -1,7 +1,7 @@
 /*
  * A bus for connecting virtio serial and console ports
  *
- * Copyright (C) 2009 Red Hat, Inc.
+ * Copyright (C) 2009, 2010 Red Hat, Inc.
  *
  * Author(s):
  *  Amit Shah <[email protected]>
hw/virtio-serial.h +1 -1
index 0548689..f023873 100644
@@ -2,7 +2,7 @@
  * Virtio Serial / Console Support
  *
  * Copyright IBM, Corp. 2008
- * Copyright Red Hat, Inc. 2009
+ * Copyright Red Hat, Inc. 2009, 2010
  *
  * Authors:
  *  Christian Ehrhardt <[email protected]>
Signature · 1 line
1.6.2.5
author
#10 lore raw

PATCH 10/17 virtio-serial: Send out guest data to ports only if port is opened

In reply to Amit Shah

Data should be written only when ports are open.

---
 hw/virtio-serial-bus.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +5 -0
index 33083af..236e300 100644
@@ -335,6 +335,11 @@ static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
             goto next_buf;
         }
 
+	if (!port->host_connected) {
+            ret = 0;
+            goto next_buf;
+        }
+
         /*
          * A port may not have any handler registered for consuming the
          * data that the guest sends or it may not have a chardev associated
Signature · 1 line
1.6.2.5
author
#13 lore raw

PATCH 13/17 virtio-serial: Handle scatter-gather buffers for control messages

In reply to Amit Shah

Current control messages are small enough to not be split into multiple buffers but we could run into such a situation in the future or a malicious guest could cause such a situation.

So handle the entire iov request for control messages.

Also ensure the size of the control request is >= what we expect otherwise we risk accessing memory that we don't own.

---
 hw/virtio-serial-bus.c |   34 +++++++++++++++++++++++++++++++---
 1 files changed, 31 insertions(+), 3 deletions(-)
hw/virtio-serial-bus.c +31 -3
index ee45146..88fb723 100644
@@ -204,7 +204,7 @@ size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
 }
 
 /* Guest wants to notify us of some event */
-static void handle_control_message(VirtIOSerial *vser, void *buf)
+static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
 {
     struct VirtIOSerialPort *port;
     struct virtio_console_control cpkt, *gcpkt;
@@ -213,6 +213,11 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)
 
     gcpkt = buf;
 
+    if (len < sizeof(cpkt)) {
+        /* The guest sent an invalid control packet */
+        return;
+    }
+
     cpkt.event = lduw_p(&gcpkt->event);
     cpkt.value = lduw_p(&gcpkt->value);
 
@@ -306,12 +311,35 @@ static void control_out(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtQueueElement elem;
     VirtIOSerial *vser;
+    uint8_t *buf;
+    size_t len;
 
     vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
 
+    len = 0;
+    buf = NULL;
     while (virtqueue_pop(vq, &elem)) {
-        handle_control_message(vser, elem.out_sg[0].iov_base);
-        virtqueue_push(vq, &elem, elem.out_sg[0].iov_len);
+        size_t cur_len, copied;
+
+        cur_len = iov_size(elem.out_sg, elem.out_num);
+        /*
+         * Allocate a new buf only if we didn't have one previously or
+         * if the size of the buf differs
+         */
+        if (cur_len > len) {
+            if (len) {
+                qemu_free(buf);
+            }
+            buf = qemu_malloc(cur_len);
+            len = cur_len;
+        }
+        copied = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, len);
+
+        handle_control_message(vser, buf, copied);
+        virtqueue_push(vq, &elem, copied);
+    }
+    if (len) {
+        qemu_free(buf);
     }
     virtio_notify(vdev, vq);
 }
Signature · 1 line
1.6.2.5
author
#7 lore raw

PATCH 7/17 virtio-serial: Remove redundant check for 0-sized write request

In reply to Amit Shah

The check for a 0-sized write request to a guest port is not necessary; the while loop below won't be executed in this case and all will be fine.

---
 hw/virtio-serial-bus.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
hw/virtio-serial-bus.c +0 -3
index 80f0259..4435c62 100644
@@ -91,9 +91,6 @@ static size_t write_to_port(VirtIOSerialPort *port,
     if (!virtio_queue_ready(vq)) {
         return 0;
     }
-    if (!size) {
-        return 0;
-    }
 
     while (offset < size) {
         int i;
Signature · 1 line
1.6.2.5
author
#9 lore raw

PATCH 9/17 virtio-serial: Propagate errors in initialising ports / devices in guest

In reply to Amit Shah

If adding of ports or devices in the guest fails we can send out a QMP event so that management software can deal with it.

---
 hw/virtio-serial-bus.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
hw/virtio-serial-bus.c +10 -0
index a19e751..33083af 100644
@@ -223,6 +223,11 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)
 
     switch(cpkt.event) {
     case VIRTIO_CONSOLE_DEVICE_READY:
+        if (!cpkt.value) {
+            error_report("virtio-serial-bus: Guest failure in adding device %s\n",
+                vser->bus->qbus.name);
+            break;
+        }
         /*
          * The device is up, we can now tell the device about all the
          * ports we have here.
@@ -233,6 +238,11 @@ static void handle_control_message(VirtIOSerial *vser, void *buf)
         break;
 
     case VIRTIO_CONSOLE_PORT_READY:
+        if (!cpkt.value) {
+            error_report("virtio-serial-bus: Guest failure in adding port %u for device %s\n",
+                         port->id, vser->bus->qbus.name);
+            break;
+        }
         /*
          * Now that we know the guest asked for the port name, we're
          * sure the guest has initialised whatever state is necessary
Signature · 1 line
1.6.2.5
#18 lore raw

[Qemu-devel] Re: [PATCH 16/17] virtio-serial: Discard data that guest sends us when ports aren't connected

In reply to Amit Shah

Amit Shah <[email protected]> wrote:

Hi

hw/virtio-serial-bus.c static size_t write_to_port(VirtIOSerialPort *port, · line 127

Quoted 3 lines
hw/virtio-serial-bus.c +3 -0
+            buf_size = iov_size(elem.out_sg, elem.out_num);
+            buf = qemu_malloc(buf_size);
+            ret = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, buf_size);

This is independent of the this series, but I think this idiom is going to be very common, perhaps having a helper that does it makes sense?

Later, Juan.

Reviewed-by on the v1 series
#21 lore raw
In reply to Amit Shah
Quoted 44 lines · Amit Shah

Hello,

These patches rework the way ports are announced to the guests. A control message is used to let the guest know a new port is added. Initial port discovery and port hot-plug work via this way now.

This was done to have the host and guest port numbering in sync to avoid surprises after several hotplug/unplug operations and migrations.

The ability to assign a particular port number to ports is also added so that management software can control the placement of ports.

Differences from v3:
- Removed the QMP events (will ride on top of new infrastructure by Luiz)

- Add flow control: ports can signal to the virtio-serial code to stop sending data till it's ready to accept more.

- A fix for not abusing the virtqueue_push() api: we should signal to the guest the number of bytes written for guest to consume. We were using the number to signify the bytes consumed by hosts.

- Juan's comment about re-using control queue buffers

Overall:
- Users can set the port id they wish to instantiate ports at by using the ,nr= parameter to 'virtserialport' and 'virtconsole' devices

- Migration fixes: refuse migration when:
- number of active ports is different between the src and destination
- max_nr_ports a device can support on the src is more

- If a qemu chardev connection to a port is closed on the dest while it was open on the src, inform the guest about this. (Also do the same for port closed on src but open on dest.)

- Use control messages for relaying new port information instead of config space (changes abi)

- Propagate error message from guest in instantiating a port or a device to the user.

Quoted 7 lines

- Handle scatter/gather for control output and data output from the guest

- Fix abuse of virtio api in the virtqueue_push() function

- Add an API for the ports for flow control: ports can signal when they're ready to accept data / stop sending data.

Later, Juan.

#19 lore raw

Re: [Qemu-devel] [PATCH 15/17] virtio-serial: Apps should consume all data that guest sends out / Fix virtio api abuse

In reply to Amit Shah

hw/virtio-console.c typedef struct VirtConsole { · line 28

Quoted 12 lines

/* Callback function that's called when the guest sends us data */

hw/virtio-console.c +2 -5
-static size_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
+static void flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
  {
      VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
-    ssize_t ret;
 
-    ret = qemu_chr_write(vcon->chr, buf, len);
-
-    return ret<  0 ? 0 : ret;
+    qemu_chr_write(vcon->chr, buf, len);
  }

Ok, so we loose data in case qemu_chr_write wasn't able to write out all data? Nice opportunity to show flow control in action here ;)

On failed+partial writes just put the remaining data into a buffer and throttle the port until all buffered data has been successfully written to the chardev.

Incremental patch is fine to avoid the chicken-egg issue (throttling is added by patch #17).

cheers,
Gerd

author
#20 lore raw

Re: [Qemu-devel] [PATCH 15/17] virtio-serial: Apps should consume all data that guest sends out / Fix virtio api abuse

Quoted 16 lines · Gerd Hoffmann

hw/virtio-console.c typedef struct VirtConsole { · line 28

Quoted 12 lines

/* Callback function that's called when the guest sends us data */

hw/virtio-console.c +2 -5
-static size_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
+static void flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
  {
      VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
-    ssize_t ret;
 
-    ret = qemu_chr_write(vcon->chr, buf, len);
-
-    return ret<  0 ? 0 : ret;
+    qemu_chr_write(vcon->chr, buf, len);
  }

Ok, so we loose data in case qemu_chr_write wasn't able to write out all data? Nice opportunity to show flow control in action here ;)

Quoted 6 lines

On failed+partial writes just put the remaining data into a buffer and throttle the port until all buffered data has been successfully written to the chardev.

Incremental patch is fine to avoid the chicken-egg issue (throttling is added by patch #17).

Yes, that's what I intend to do in a later patch (series).

Amit

Acked-by on the v1 series
#22 lore raw
In reply to Amit Shah
Quoted 6 lines · Amit Shah

Hello,

These patches rework the way ports are announced to the guests. A control message is used to let the guest know a new port is added. Initial port discovery and port hot-plug work via this way now.

Quoted 6 lines

This was done to have the host and guest port numbering in sync to avoid surprises after several hotplug/unplug operations and migrations.

The ability to assign a particular port number to ports is also added so that management software can control the placement of ports.

cheers,
Gerd