Skip to contentSkip to conversation

Spice / usb-redir chardev flowcontrol patches

PATCH In review Hans de Goede 2 participants 10 messages Mar 13, 2013 – Mar 14, 2013

lore
Share & tools
lore
#0 lore raw

cover Spice / usb-redir chardev flowcontrol patches

Here is a series adding watch support to the spicevmc chardev backend and flowcontrol support to the usb-redir device. It begins with a few chardev related bugfixes which were found during the development of this series.

Note that this series is based *on top of* Gerd Hoffmann's chardev.4 series to avoid conflicts with that series.

Regards,

Hans

#1 lore raw

PATCH 1/7 virtio-console: Also throttle when less was written then requested

This is necessary so that we get properly woken up to write the rest.

---
 hw/virtio-console.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/virtio-console.c +3 -2
index e2d1c58..1d87c5b 100644
@@ -47,7 +47,7 @@ static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
     ret = qemu_chr_fe_write(vcon->chr, buf, len);
     trace_virtio_console_flush_buf(port->id, len, ret);
 
-    if (ret <= 0) {
+    if (ret < len) {
         VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
 
         /*
@@ -56,7 +56,8 @@ static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
          * we had a finer-grained message, like -EPIPE, we could close
          * this connection.
          */
-        ret = 0;
+        if (ret < 0)
+            ret = 0;
         if (!k->is_console) {
             virtio_serial_throttle_port(port, true);
             qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT, chr_write_unblocked,
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH 2/7 virtio-console: Remove any pending watches on close

In reply to Hans de Goede
---
 hw/virtio-console.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
hw/virtio-console.c +14 -2
index 1d87c5b..ec0f91b 100644
@@ -18,6 +18,7 @@
 typedef struct VirtConsole {
     VirtIOSerialPort port;
     CharDriverState *chr;
+    guint watch;
 } VirtConsole;
 
 /*
@@ -29,6 +30,7 @@ static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
 {
     VirtConsole *vcon = opaque;
 
+    vcon->watch = 0;
     virtio_serial_throttle_port(&vcon->port, false);
     return FALSE;
 }
@@ -60,8 +62,10 @@ static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
             ret = 0;
         if (!k->is_console) {
             virtio_serial_throttle_port(port, true);
-            qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT, chr_write_unblocked,
-                                  vcon);
+            if (!vcon->watch) {
+                vcon->watch = qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT,
+                                                    chr_write_unblocked, vcon);
+            }
         }
     }
     return ret;
@@ -86,6 +90,10 @@ static void guest_close(VirtIOSerialPort *port)
     if (!vcon->chr) {
         return;
     }
+    if (vcon->watch) {
+        g_source_remove(vcon->watch);
+        vcon->watch = 0;
+    }
     qemu_chr_fe_close(vcon->chr);
 }
 
@@ -116,6 +124,10 @@ static void chr_event(void *opaque, int event)
         virtio_serial_open(&vcon->port);
         break;
     case CHR_EVENT_CLOSED:
+        if (vcon->watch) {
+            g_source_remove(vcon->watch);
+            vcon->watch = 0;
+        }
         virtio_serial_close(&vcon->port);
         break;
     }
Signature · 1 line
1.8.1.4
#3 lore raw

Re: [Qemu-devel] [PATCH 2/7] virtio-console: Remove any pending watches on close

Quoted 39 lines · Hans de Goede
---
 hw/virtio-console.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
hw/virtio-console.c +6 -2
index 1d87c5b..ec0f91b 100644
@@ -18,6 +18,7 @@
 typedef struct VirtConsole {
     VirtIOSerialPort port;
     CharDriverState *chr;
+    guint watch;
 } VirtConsole;
 
 /*
@@ -29,6 +30,7 @@ static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
 {
     VirtConsole *vcon = opaque;
 
+    vcon->watch = 0;
     virtio_serial_throttle_port(&vcon->port, false);
     return FALSE;
 }
@@ -60,8 +62,10 @@ static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
             ret = 0;
         if (!k->is_console) {
             virtio_serial_throttle_port(port, true);
-            qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT, chr_write_unblocked,
-                                  vcon);
+            if (!vcon->watch) {
+                vcon->watch = qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT,
+                                                    chr_write_unblocked, vcon);
+            }
         }
     }
     return ret;

hw/virtio-console.c static void guest_close(VirtIOSerialPort *port) · line 98

Quoted 9 lines
hw/virtio-console.c +4 -0
@@ -86,6 +90,10 @@ static void guest_close(VirtIOSerialPort *port)
     if (!vcon->chr) {
         return;
     }
+    if (vcon->watch) {
+        g_source_remove(vcon->watch);
+        vcon->watch = 0;
+    }
     qemu_chr_fe_close(vcon->chr);
 }

Even if a guest closed the connection, we want the data the guest sent before the close to go out.

What's the reason to not do that?

hw/virtio-console.c static void chr_event(void *opaque, int event) · line 133

Quoted 11 lines
hw/virtio-console.c +4 -0
@@ -116,6 +124,10 @@ static void chr_event(void *opaque, int event)
         virtio_serial_open(&vcon->port);
         break;
     case CHR_EVENT_CLOSED:
+        if (vcon->watch) {
+            g_source_remove(vcon->watch);
+            vcon->watch = 0;
+        }
         virtio_serial_close(&vcon->port);
         break;
     }

Agreed with the rest.

Amit

#4 lore raw

Re: [Qemu-devel] [PATCH 2/7] virtio-console: Remove any pending watches on close

Hi,

Quoted 53 lines · Amit Shah
Quoted 39 lines · Hans de Goede
---
  hw/virtio-console.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)
hw/virtio-console.c +6 -2
index 1d87c5b..ec0f91b 100644
@@ -18,6 +18,7 @@
  typedef struct VirtConsole {
      VirtIOSerialPort port;
      CharDriverState *chr;
+    guint watch;
  } VirtConsole;
 
  /*
@@ -29,6 +30,7 @@ static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
  {
      VirtConsole *vcon = opaque;
 
+    vcon->watch = 0;
      virtio_serial_throttle_port(&vcon->port, false);
      return FALSE;
  }
@@ -60,8 +62,10 @@ static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
              ret = 0;
          if (!k->is_console) {
              virtio_serial_throttle_port(port, true);
-            qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT, chr_write_unblocked,
-                                  vcon);
+            if (!vcon->watch) {
+                vcon->watch = qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT,
+                                                    chr_write_unblocked, vcon);
+            }
          }
      }
      return ret;

hw/virtio-console.c static void guest_close(VirtIOSerialPort *port) · line 98

Quoted 9 lines
hw/virtio-console.c +4 -0
@@ -86,6 +90,10 @@ static void guest_close(VirtIOSerialPort *port)
      if (!vcon->chr) {
          return;
      }
+    if (vcon->watch) {
+        g_source_remove(vcon->watch);
+        vcon->watch = 0;
+    }
      qemu_chr_fe_close(vcon->chr);
  }

Even if a guest closed the connection, we want the data the guest sent before the close to go out.

Quoted 1 line

What's the reason to not do that?

There is no reason, this was a bad attempt by me to deal with closing the watch on virtio-port hot-unplug. I've removed this in my local version of the patchset and instead added an exit callback to deal with virtio-port hot-unplug the proper way.

I still have a few other patches for spice-qemu-char.c I'm working on, once those are finished, I'll resend the series with this fixed and rebased on top of Gerd Hoffman's chardev.v5 tree.

Regards,

Hans

#5 lore raw

PATCH 3/7 spice-qemu-char: Fix name parameter issues after qapi-ifying

In reply to Hans de Goede

The strings passed in through the qapi calls are dynamic memory, since we want to have them stick around longer then just the call to qemu_chr_open_spice_* we need to strdup them.

---
 spice-qemu-char.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
spice-qemu-char.c +7 -2
index 0c92ca8..a94f76b 100644
@@ -185,6 +185,11 @@ static void spice_chr_close(struct CharDriverState *chr)
     printf("%s\n", __func__);
     vmc_unregister_interface(s);
     QLIST_REMOVE(s, next);
+
+    g_free((char *)s->sin.subtype);
+#if SPICE_SERVER_VERSION >= 0x000c02
+    g_free((char *)s->sin.portname);
+#endif
     g_free(s);
 }
 
@@ -226,7 +231,7 @@ static CharDriverState *chr_open(const char *subtype)
     s = g_malloc0(sizeof(SpiceCharDriver));
     s->chr = chr;
     s->active = false;
-    s->sin.subtype = subtype;
+    s->sin.subtype = g_strdup(subtype);
     chr->opaque = s;
     chr->chr_write = spice_chr_write;
     chr->chr_close = spice_chr_close;
@@ -284,7 +289,7 @@ CharDriverState *qemu_chr_open_spice_port(const char *name)
 
     chr = chr_open("port");
     s = chr->opaque;
-    s->sin.portname = name;
+    s->sin.portname = g_strdup(name);
 
     return chr;
 }
Signature · 1 line
1.8.1.4
#6 lore raw

PATCH 4/7 spice-qemu-char: Remove dead debugging code

In reply to Hans de Goede

Since commit d62e5f7036a018b2ad09f17ebd481bd28953d783 "chardev: add spice support to qapi"

It is impossible to set the debug parameter, so all the dprintf calls are essentially nops. Since we've not needed the debug parameter in ages this is not a problem, if it later turns out we do need some more debugging options we can add more trace-points.

---
 spice-qemu-char.c | 17 -----------------
 1 file changed, 17 deletions(-)
spice-qemu-char.c +0 -17
index a94f76b..8a9236d 100644
@@ -8,14 +8,6 @@
 
 #include "qemu/osdep.h"
 
-#define dprintf(_scd, _level, _fmt, ...)                                \
-    do {                                                                \
-        static unsigned __dprintf_counter = 0;                          \
-        if (_scd->debug >= _level) {                                    \
-            fprintf(stderr, "scd: %3d: " _fmt, ++__dprintf_counter, ## __VA_ARGS__);\
-        }                                                               \
-    } while (0)
-
 typedef struct SpiceCharDriver {
     CharDriverState*      chr;
     SpiceCharDeviceInstance     sin;
@@ -24,7 +16,6 @@ typedef struct SpiceCharDriver {
     uint8_t               *buffer;
     uint8_t               *datapos;
     ssize_t               bufsize, datalen;
-    uint32_t              debug;
     QLIST_ENTRY(SpiceCharDriver) next;
 } SpiceCharDriver;
 
@@ -49,7 +40,6 @@ static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
         p += last_out;
     }
 
-    dprintf(scd, 3, "%s: %zu/%zd\n", __func__, out, len + out);
     trace_spice_vmc_write(out, len + out);
     return out;
 }
@@ -59,7 +49,6 @@ static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
     SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
     int bytes = MIN(len, scd->datalen);
 
-    dprintf(scd, 2, "%s: %p %d/%d/%zd\n", __func__, scd->datapos, len, bytes, scd->datalen);
     if (bytes > 0) {
         memcpy(buf, scd->datapos, bytes);
         scd->datapos += bytes;
@@ -84,11 +73,9 @@ static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
         chr_event = CHR_EVENT_BREAK;
         break;
     default:
-        dprintf(scd, 2, "%s: unknown %d\n", __func__, event);
         return;
     }
 
-    dprintf(scd, 2, "%s: %d\n", __func__, event);
     trace_spice_vmc_event(chr_event);
     qemu_chr_be_event(scd->chr, chr_event);
 }
@@ -141,7 +128,6 @@ static void vmc_register_interface(SpiceCharDriver *scd)
     if (scd->active) {
         return;
     }
-    dprintf(scd, 1, "%s\n", __func__);
     scd->sin.base.sif = &vmc_interface.base;
     qemu_spice_add_interface(&scd->sin.base);
     scd->active = true;
@@ -153,7 +139,6 @@ static void vmc_unregister_interface(SpiceCharDriver *scd)
     if (!scd->active) {
         return;
     }
-    dprintf(scd, 1, "%s\n", __func__);
     spice_server_remove_interface(&scd->sin.base);
     scd->active = false;
     trace_spice_vmc_unregister_interface(scd);
@@ -164,7 +149,6 @@ static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
 {
     SpiceCharDriver *s = chr->opaque;
 
-    dprintf(s, 2, "%s: %d\n", __func__, len);
     vmc_register_interface(s);
     assert(s->datalen == 0);
     if (s->bufsize < len) {
@@ -182,7 +166,6 @@ static void spice_chr_close(struct CharDriverState *chr)
 {
     SpiceCharDriver *s = chr->opaque;
 
-    printf("%s\n", __func__);
     vmc_unregister_interface(s);
     QLIST_REMOVE(s, next);
 
Signature · 1 line
1.8.1.4
#7 lore raw

PATCH 5/7 spice-qemu-char: Add watch support

In reply to Hans de Goede
---
 spice-qemu-char.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 63 insertions(+), 4 deletions(-)
spice-qemu-char.c +63 -4
index 8a9236d..e9eea0d 100644
@@ -13,12 +13,18 @@ typedef struct SpiceCharDriver {
     SpiceCharDeviceInstance     sin;
     char                  *subtype;
     bool                  active;
+    bool                  blocked;
     uint8_t               *buffer;
     uint8_t               *datapos;
     ssize_t               bufsize, datalen;
     QLIST_ENTRY(SpiceCharDriver) next;
 } SpiceCharDriver;
 
+typedef struct SpiceCharSource {
+    GSource               source;
+    SpiceCharDriver       *scd;
+} SpiceCharSource;
+
 static QLIST_HEAD(, SpiceCharDriver) spice_chars =
     QLIST_HEAD_INITIALIZER(spice_chars);
 
@@ -54,9 +60,10 @@ static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
         scd->datapos += bytes;
         scd->datalen -= bytes;
         assert(scd->datalen >= 0);
-        if (scd->datalen == 0) {
-            scd->datapos = 0;
-        }
+    }
+    if (scd->datalen == 0) {
+        scd->datapos = 0;
+        scd->blocked = false;
     }
     trace_spice_vmc_read(bytes, len);
     return bytes;
@@ -144,10 +151,54 @@ static void vmc_unregister_interface(SpiceCharDriver *scd)
     trace_spice_vmc_unregister_interface(scd);
 }
 
+static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
+{
+    SpiceCharSource *src = (SpiceCharSource *)source;
+
+    *timeout = -1;
+
+    return !src->scd->blocked;
+}
+
+static gboolean spice_char_source_check(GSource *source)
+{
+    SpiceCharSource *src = (SpiceCharSource *)source;
+
+    return !src->scd->blocked;
+}
+
+static gboolean spice_char_source_dispatch(GSource *source,
+    GSourceFunc callback, gpointer user_data)
+{
+    GIOFunc func = (GIOFunc)callback;
+
+    return func(NULL, G_IO_OUT, user_data);
+}
+
+GSourceFuncs SpiceCharSourceFuncs = {
+    .prepare  = spice_char_source_prepare,
+    .check    = spice_char_source_check,
+    .dispatch = spice_char_source_dispatch,
+};
+
+static GSource *spice_chr_add_watch(CharDriverState *chr, GIOCondition cond)
+{
+    SpiceCharDriver *scd = chr->opaque;
+    SpiceCharSource *src;
+
+    assert(cond == G_IO_OUT);
+
+    src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
+                                          sizeof(SpiceCharSource));
+    src->scd = scd;
+
+    return (GSource *)src;
+}
 
 static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
 {
     SpiceCharDriver *s = chr->opaque;
+    int read_bytes;
 
     vmc_register_interface(s);
     assert(s->datalen == 0);
@@ -159,7 +210,14 @@ static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
     s->datapos = s->buffer;
     s->datalen = len;
     spice_server_char_device_wakeup(&s->sin);
-    return len;
+    read_bytes = len - s->datalen;
+    if (read_bytes != len) {
+        /* We'll get passed in the unconsumed data with the next call */
+        s->datalen = 0;
+        s->datapos = NULL;
+        s->blocked = true;
+    }
+    return read_bytes;
 }
 
 static void spice_chr_close(struct CharDriverState *chr)
@@ -217,6 +275,7 @@ static CharDriverState *chr_open(const char *subtype)
     s->sin.subtype = g_strdup(subtype);
     chr->opaque = s;
     chr->chr_write = spice_chr_write;
+    chr->chr_add_watch = spice_chr_add_watch;
     chr->chr_close = spice_chr_close;
     chr->chr_guest_open = spice_chr_guest_open;
     chr->chr_guest_close = spice_chr_guest_close;
Signature · 1 line
1.8.1.4
#8 lore raw

PATCH 6/7 spice-qemu-char.c: Remove intermediate buffer

In reply to Hans de Goede

From: Alon Levy <[email protected]>

virtio-serial's buffer is valid when it calls us, and we don't access it otherwise: vmc_read is only called in response to wakeup, or else we set datalen=0 and throttle. Then vmc_read is called back, we return 0 (not accessing the buffer) and set the timer to unthrottle.

Also make datalen int and not ssize_t (to fit spice_chr_write signature).

HdG: Update to apply to spice-qemu-char with new gio-channel based flowcontrol support.

---
 spice-qemu-char.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
spice-qemu-char.c +3 -9
index e9eea0d..99cb315 100644
@@ -14,9 +14,8 @@ typedef struct SpiceCharDriver {
     char                  *subtype;
     bool                  active;
     bool                  blocked;
-    uint8_t               *buffer;
-    uint8_t               *datapos;
-    ssize_t               bufsize, datalen;
+    const uint8_t         *datapos;
+    int                   datalen;
     QLIST_ENTRY(SpiceCharDriver) next;
 } SpiceCharDriver;
 
@@ -202,12 +201,7 @@ static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
 
     vmc_register_interface(s);
     assert(s->datalen == 0);
-    if (s->bufsize < len) {
-        s->bufsize = len;
-        s->buffer = g_realloc(s->buffer, s->bufsize);
-    }
-    memcpy(s->buffer, buf, len);
-    s->datapos = s->buffer;
+    s->datapos = buf;
     s->datalen = len;
     spice_server_char_device_wakeup(&s->sin);
     read_bytes = len - s->datalen;
Signature · 1 line
1.8.1.4
#9 lore raw

PATCH 7/7 usb-redir: Add flow control support

In reply to Hans de Goede
---
 hw/usb/redirect.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
hw/usb/redirect.c +31 -1
index c519b9b..9ba714d 100644
@@ -104,6 +104,8 @@ struct USBRedirDevice {
     /* Data passed from chardev the fd_read cb to the usbredirparser read cb */
     const uint8_t *read_buf;
     int read_buf_size;
+    /* Active chardev-watch-tag */
+    guint watch;
     /* For async handling of close */
     QEMUBH *chardev_close_bh;
     /* To delay the usb attach in case of quick chardev close + open */
@@ -254,9 +256,21 @@ static int usbredir_read(void *priv, uint8_t *data, int count)
     return count;
 }
 
+static gboolean usbredir_write_unblocked(GIOChannel *chan, GIOCondition cond,
+                                         void *opaque)
+{
+    USBRedirDevice *dev = opaque;
+
+    dev->watch = 0;
+    usbredirparser_do_write(dev->parser);
+
+    return FALSE;
+}
+
 static int usbredir_write(void *priv, uint8_t *data, int count)
 {
     USBRedirDevice *dev = priv;
+    int r;
 
     if (!dev->cs->opened) {
         return 0;
@@ -267,7 +281,16 @@ static int usbredir_write(void *priv, uint8_t *data, int count)
         return 0;
     }
 
-    return qemu_chr_fe_write(dev->cs, data, count);
+    r = qemu_chr_fe_write(dev->cs, data, count);
+    if (r < count) {
+        if (!dev->watch) {
+            dev->watch = qemu_chr_fe_add_watch(dev->cs, G_IO_OUT,
+                                               usbredir_write_unblocked, dev);
+        }
+        if (r < 0)
+            r = 0;
+    }
+    return r;
 }
 
 /*
@@ -1085,6 +1108,10 @@ static void usbredir_chardev_close_bh(void *opaque)
         usbredirparser_destroy(dev->parser);
         dev->parser = NULL;
     }
+    if (dev->watch) {
+        g_source_remove(dev->watch);
+        dev->watch = 0;
+    }
 }
 
 static void usbredir_create_parser(USBRedirDevice *dev)
@@ -1319,6 +1346,9 @@ static void usbredir_handle_destroy(USBDevice *udev)
     if (dev->parser) {
         usbredirparser_destroy(dev->parser);
     }
+    if (dev->watch) {
+        g_source_remove(dev->watch);
+    }
 
     free(dev->filter_rules);
 }
Signature · 1 line
1.8.1.4