Blame SOURCES/0019-stream-device-Limit-sending-queue-from-guest-to-serv.patch

ad1357
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
ad1357
From: Frediano Ziglio <fziglio@redhat.com>
ad1357
Date: Wed, 19 Apr 2017 16:24:54 +0100
ad1357
Subject: [spice-server] stream-device: Limit sending queue from guest to
ad1357
 server
ad1357
ad1357
Do not allow the guest to fill host memory.
ad1357
Also having a huge queue mainly cause to have a higher video
ad1357
latency.
ad1357
ad1357
Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
ad1357
Acked-by: Jonathon Jongsma <jjongsma@redhat.com>
ad1357
---
ad1357
 server/stream-channel.c | 41 ++++++++++++++++++++++++++++++++++++++++-
ad1357
 server/stream-channel.h | 10 ++++++++++
ad1357
 server/stream-device.c  | 35 ++++++++++++++++++++++++++++++++++-
ad1357
 3 files changed, 84 insertions(+), 2 deletions(-)
ad1357
ad1357
diff --git a/server/stream-channel.c b/server/stream-channel.c
ad1357
index 51b8badf9..ec4bf021d 100644
ad1357
--- a/server/stream-channel.c
ad1357
+++ b/server/stream-channel.c
ad1357
@@ -71,9 +71,15 @@ struct StreamChannel {
ad1357
     /* size of the current video stream */
ad1357
     unsigned width, height;
ad1357
 
ad1357
+    StreamQueueStat queue_stat;
ad1357
+
ad1357
     /* callback to notify when a stream should be started or stopped */
ad1357
     stream_channel_start_proc start_cb;
ad1357
     void *start_opaque;
ad1357
+
ad1357
+    /* callback to notify when queue statistics changes */
ad1357
+    stream_channel_queue_stat_proc queue_cb;
ad1357
+    void *queue_opaque;
ad1357
 };
ad1357
 
ad1357
 struct StreamChannelClass {
ad1357
@@ -98,6 +104,7 @@ typedef struct StreamCreateItem {
ad1357
 
ad1357
 typedef struct StreamDataItem {
ad1357
     RedPipeItem base;
ad1357
+    StreamChannel *channel;
ad1357
     // NOTE: this must be the last field in the structure
ad1357
     SpiceMsgDisplayStreamData data;
ad1357
 } StreamDataItem;
ad1357
@@ -454,6 +461,27 @@ stream_channel_change_format(StreamChannel *channel, const StreamMsgFormat *fmt)
ad1357
     red_channel_pipes_add(red_channel, &item->base);
ad1357
 }
ad1357
 
ad1357
+static inline void
ad1357
+stream_channel_update_queue_stat(StreamChannel *channel,
ad1357
+                                 int32_t num_diff, int32_t size_diff)
ad1357
+{
ad1357
+    channel->queue_stat.num_items += num_diff;
ad1357
+    channel->queue_stat.size += size_diff;
ad1357
+    if (channel->queue_cb) {
ad1357
+        channel->queue_cb(channel->queue_opaque, &channel->queue_stat, channel);
ad1357
+    }
ad1357
+}
ad1357
+
ad1357
+static void
ad1357
+data_item_free(RedPipeItem *base)
ad1357
+{
ad1357
+    StreamDataItem *pipe_item = SPICE_UPCAST(StreamDataItem, base);
ad1357
+
ad1357
+    stream_channel_update_queue_stat(pipe_item->channel, -1, -pipe_item->data.data_size);
ad1357
+
ad1357
+    g_free(pipe_item);
ad1357
+}
ad1357
+
ad1357
 void
ad1357
 stream_channel_send_data(StreamChannel *channel, const void *data, size_t size, uint32_t mm_time)
ad1357
 {
ad1357
@@ -467,10 +495,13 @@ stream_channel_send_data(StreamChannel *channel, const void *data, size_t size,
ad1357
     RedChannel *red_channel = RED_CHANNEL(channel);
ad1357
 
ad1357
     StreamDataItem *item = g_malloc(sizeof(*item) + size);
ad1357
-    red_pipe_item_init(&item->base, RED_PIPE_ITEM_TYPE_STREAM_DATA);
ad1357
+    red_pipe_item_init_full(&item->base, RED_PIPE_ITEM_TYPE_STREAM_DATA,
ad1357
+                            data_item_free);
ad1357
     item->data.base.id = channel->stream_id;
ad1357
     item->data.base.multi_media_time = mm_time;
ad1357
     item->data.data_size = size;
ad1357
+    item->channel = channel;
ad1357
+    stream_channel_update_queue_stat(channel, 1, size);
ad1357
     // TODO try to optimize avoiding the copy
ad1357
     memcpy(item->data.data, data, size);
ad1357
     red_channel_pipes_add(red_channel, &item->base);
ad1357
@@ -485,6 +516,14 @@ stream_channel_register_start_cb(StreamChannel *channel,
ad1357
 }
ad1357
 
ad1357
 void
ad1357
+stream_channel_register_queue_stat_cb(StreamChannel *channel,
ad1357
+                                      stream_channel_queue_stat_proc cb, void *opaque)
ad1357
+{
ad1357
+    channel->queue_cb = cb;
ad1357
+    channel->queue_opaque = opaque;
ad1357
+}
ad1357
+
ad1357
+void
ad1357
 stream_channel_reset(StreamChannel *channel)
ad1357
 {
ad1357
     struct {
ad1357
diff --git a/server/stream-channel.h b/server/stream-channel.h
ad1357
index bd075a951..f961d7157 100644
ad1357
--- a/server/stream-channel.h
ad1357
+++ b/server/stream-channel.h
ad1357
@@ -67,6 +67,16 @@ typedef void (*stream_channel_start_proc)(void *opaque, struct StreamMsgStartSto
ad1357
 void stream_channel_register_start_cb(StreamChannel *channel,
ad1357
                                       stream_channel_start_proc cb, void *opaque);
ad1357
 
ad1357
+typedef struct StreamQueueStat {
ad1357
+    uint32_t num_items;
ad1357
+    uint32_t size;
ad1357
+} StreamQueueStat;
ad1357
+
ad1357
+typedef void (*stream_channel_queue_stat_proc)(void *opaque, const StreamQueueStat *stats,
ad1357
+                                               StreamChannel *channel);
ad1357
+void stream_channel_register_queue_stat_cb(StreamChannel *channel,
ad1357
+                                           stream_channel_queue_stat_proc cb, void *opaque);
ad1357
+
ad1357
 G_END_DECLS
ad1357
 
ad1357
 #endif /* STREAM_CHANNEL_H_ */
ad1357
diff --git a/server/stream-device.c b/server/stream-device.c
ad1357
index ae108788b..f87538d49 100644
ad1357
--- a/server/stream-device.c
ad1357
+++ b/server/stream-device.c
ad1357
@@ -44,6 +44,7 @@ struct StreamDevice {
ad1357
     uint8_t hdr_pos;
ad1357
     bool has_error;
ad1357
     bool opened;
ad1357
+    bool flow_stopped;
ad1357
     StreamChannel *stream_channel;
ad1357
 };
ad1357
 
ad1357
@@ -72,7 +73,7 @@ stream_device_read_msg_from_dev(RedCharDevice *self, SpiceCharDeviceInstance *si
ad1357
     int n;
ad1357
     bool handled = false;
ad1357
 
ad1357
-    if (dev->has_error || !dev->stream_channel) {
ad1357
+    if (dev->has_error || dev->flow_stopped || !dev->stream_channel) {
ad1357
         return NULL;
ad1357
     }
ad1357
 
ad1357
@@ -181,6 +182,9 @@ handle_msg_data(StreamDevice *dev, SpiceCharDeviceInstance *sin)
ad1357
         if (n <= 0) {
ad1357
             break;
ad1357
         }
ad1357
+        // TODO collect all message ??
ad1357
+        // up: we send a single frame together
ad1357
+        // down: guest can cause a crash
ad1357
         stream_channel_send_data(dev->stream_channel, buf, n, reds_get_mm_time());
ad1357
         dev->hdr.size -= n;
ad1357
     }
ad1357
@@ -233,6 +237,33 @@ stream_device_stream_start(void *opaque, StreamMsgStartStop *start,
ad1357
     red_char_device_write_buffer_add(char_dev, buf);
ad1357
 }
ad1357
 
ad1357
+static void
ad1357
+stream_device_stream_queue_stat(void *opaque, const StreamQueueStat *stats G_GNUC_UNUSED,
ad1357
+                                StreamChannel *stream_channel G_GNUC_UNUSED)
ad1357
+{
ad1357
+    StreamDevice *dev = (StreamDevice *) opaque;
ad1357
+
ad1357
+    if (!dev->opened) {
ad1357
+        return;
ad1357
+    }
ad1357
+
ad1357
+    // very easy control flow... if any data stop
ad1357
+    // this seems a very small queue but as we use tcp
ad1357
+    // there's already that queue
ad1357
+    if (stats->num_items) {
ad1357
+        dev->flow_stopped = true;
ad1357
+        return;
ad1357
+    }
ad1357
+
ad1357
+    if (dev->flow_stopped) {
ad1357
+        dev->flow_stopped = false;
ad1357
+        // TODO resume flow...
ad1357
+        // avoid recursion if we need to call get data from data handling from
ad1357
+        // data handling
ad1357
+        red_char_device_wakeup(&dev->parent);
ad1357
+    }
ad1357
+}
ad1357
+
ad1357
 RedCharDevice *
ad1357
 stream_device_connect(RedsState *reds, SpiceCharDeviceInstance *sin)
ad1357
 {
ad1357
@@ -277,6 +308,7 @@ allocate_channels(StreamDevice *dev)
ad1357
     dev->stream_channel = stream_channel;
ad1357
 
ad1357
     stream_channel_register_start_cb(stream_channel, stream_device_stream_start, dev);
ad1357
+    stream_channel_register_queue_stat_cb(stream_channel, stream_device_stream_queue_stat, dev);
ad1357
 }
ad1357
 
ad1357
 static void
ad1357
@@ -303,6 +335,7 @@ stream_device_port_event(RedCharDevice *char_dev, uint8_t event)
ad1357
     }
ad1357
     dev->hdr_pos = 0;
ad1357
     dev->has_error = false;
ad1357
+    dev->flow_stopped = false;
ad1357
     red_char_device_reset(char_dev);
ad1357
     reset_channels(dev);
ad1357
 }