qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: "Marc-André Lureau" <marcandre.lureau@gmail.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>, QEMU <qemu-devel@nongnu.org>,
	Markus Armbruster <armbru@redhat.com>
Subject: Re: [PATCH v5 4/9] ui/vdagent: core infrastructure
Date: Tue, 11 May 2021 11:04:19 +0200	[thread overview]
Message-ID: <20210511090419.nl6akk43mt4g5rua@sirius.home.kraxel.org> (raw)
In-Reply-To: <CAJ+F1CKJyE3P7DX6tewSBHUBrqsc_h5CNZi95VpT9bGu=eVgFQ@mail.gmail.com>

> > +/* ------------------------------------------------------------------ */
> > +/* send messages                                                      */
> > +
> > +static void vdagent_send_buf(VDAgentChardev *vd, void *ptr, uint32_t
> > msgsize)
> > +{
> > +    uint8_t *msgbuf = ptr;
> > +    uint32_t len, pos = 0;
> > +
> > +    while (pos < msgsize) {
> > +        len = qemu_chr_be_can_write(CHARDEV(vd));
> > +        if (len > msgsize - pos) {
> > +            len = msgsize - pos;
> > +        }
> > +        qemu_chr_be_write(CHARDEV(vd), msgbuf + pos, len);
> > +        pos += len;
> > +    }
> >
> 
> This looks like it could easily busy loop. Have you thought about fixing
> this?

Incremental fix [ to be squashed ]

take care,
  Gerd

diff --git a/ui/vdagent.c b/ui/vdagent.c
index 64213aa25a06..efa98725fb22 100644
--- a/ui/vdagent.c
+++ b/ui/vdagent.c
@@ -3,7 +3,9 @@
 #include "include/qemu-common.h"
 #include "chardev/char.h"
 #include "hw/qdev-core.h"
+#include "qemu/buffer.h"
 #include "qemu/option.h"
+#include "qemu/units.h"
 #include "ui/clipboard.h"
 #include "ui/console.h"
 #include "ui/input.h"
@@ -16,6 +18,7 @@
 
 #define VDAGENT_MOUSE_DEFAULT true
 #define VDAGENT_CLIPBOARD_DEFAULT false
+#define VDAGENT_BUFFER_LIMIT (1 * MiB)
 
 struct VDAgentChardev {
     Chardev parent;
@@ -32,6 +35,7 @@ struct VDAgentChardev {
     uint32_t msgsize;
     uint8_t *xbuf;
     uint32_t xoff, xsize;
+    Buffer outbuf;
 
     /* mouse */
     DeviceState mouse_dev;
@@ -124,18 +128,20 @@ static const char *type_name[] = {
 /* ------------------------------------------------------------------ */
 /* send messages                                                      */
 
-static void vdagent_send_buf(VDAgentChardev *vd, void *ptr, uint32_t msgsize)
+static void vdagent_send_buf(VDAgentChardev *vd)
 {
-    uint8_t *msgbuf = ptr;
-    uint32_t len, pos = 0;
+    uint32_t len;
 
-    while (pos < msgsize) {
+    while (!buffer_empty(&vd->outbuf)) {
         len = qemu_chr_be_can_write(CHARDEV(vd));
-        if (len > msgsize - pos) {
-            len = msgsize - pos;
+        if (len == 0) {
+            return;
         }
-        qemu_chr_be_write(CHARDEV(vd), msgbuf + pos, len);
-        pos += len;
+        if (len > vd->outbuf.offset) {
+            len = vd->outbuf.offset;
+        }
+        qemu_chr_be_write(CHARDEV(vd), vd->outbuf.buffer, len);
+        buffer_advance(&vd->outbuf, len);
     }
 }
 
@@ -150,16 +156,22 @@ static void vdagent_send_msg(VDAgentChardev *vd, VDAgentMessage *msg)
 
     msg->protocol = VD_AGENT_PROTOCOL;
 
+    if (vd->outbuf.offset + msgsize > VDAGENT_BUFFER_LIMIT) {
+        return;
+    }
+
     while (msgoff < msgsize) {
         chunk.port = VDP_CLIENT_PORT;
         chunk.size = msgsize - msgoff;
         if (chunk.size > 1024) {
             chunk.size = 1024;
         }
-        vdagent_send_buf(vd, &chunk, sizeof(chunk));
-        vdagent_send_buf(vd, msgbuf + msgoff, chunk.size);
+        buffer_reserve(&vd->outbuf, sizeof(chunk) + chunk.size);
+        buffer_append(&vd->outbuf, &chunk, sizeof(chunk));
+        buffer_append(&vd->outbuf, msgbuf + msgoff, chunk.size);
         msgoff += chunk.size;
     }
+    vdagent_send_buf(vd);
 }
 
 static void vdagent_send_caps(VDAgentChardev *vd)
@@ -550,6 +562,7 @@ static void vdagent_chr_open(Chardev *chr,
                                                    &vdagent_mouse_handler);
     }
 
+    buffer_init(&vd->outbuf, "vdagent-outbuf");
     *be_opened = true;
 }
 
@@ -702,6 +715,13 @@ static int vdagent_chr_write(Chardev *chr, const uint8_t *buf, int len)
     return ret;
 }
 
+static void vdagent_chr_accept_input(Chardev *chr)
+{
+    VDAgentChardev *vd = QEMU_VDAGENT_CHARDEV(chr);
+
+    vdagent_send_buf(vd);
+}
+
 static void vdagent_chr_set_fe_open(struct Chardev *chr, int fe_open)
 {
     VDAgentChardev *vd = QEMU_VDAGENT_CHARDEV(chr);
@@ -748,6 +768,7 @@ static void vdagent_chr_class_init(ObjectClass *oc, void *data)
     cc->open             = vdagent_chr_open;
     cc->chr_write        = vdagent_chr_write;
     cc->chr_set_fe_open  = vdagent_chr_set_fe_open;
+    cc->chr_accept_input = vdagent_chr_accept_input;
 }
 
 static const TypeInfo vdagent_chr_type_info = {
-- 
2.31.1



  reply	other threads:[~2021-05-11  9:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-05  6:08 [PATCH v5 0/9] ui: add vdagent implementation and clipboard support Gerd Hoffmann
2021-05-05  6:08 ` [PATCH v5 1/9] build: add separate spice-protocol config option Gerd Hoffmann
2021-05-05  6:08 ` [PATCH v5 2/9] ui: add clipboard infrastructure Gerd Hoffmann
2021-05-05  6:08 ` [PATCH v5 3/9] ui: add clipboard documentation Gerd Hoffmann
2021-05-05  6:08 ` [PATCH v5 4/9] ui/vdagent: core infrastructure Gerd Hoffmann
2021-05-05  9:54   ` Marc-André Lureau
2021-05-11  9:04     ` Gerd Hoffmann [this message]
2021-05-11  9:27       ` Marc-André Lureau
2021-05-05  6:08 ` [PATCH v5 5/9] ui/vdagent: add mouse support Gerd Hoffmann
2021-05-05  6:08 ` [PATCH v5 6/9] ui/vdagent: add clipboard support Gerd Hoffmann
2021-05-05  6:08 ` [PATCH v5 7/9] ui/vnc: " Gerd Hoffmann
2021-05-05  6:09 ` [PATCH v5 8/9] ui/gtk: move struct GtkDisplayState to ui/gtk.h Gerd Hoffmann
2021-05-05  6:09 ` [PATCH v5 9/9] ui/gtk: add clipboard support Gerd Hoffmann
2021-05-05  6:21 ` [PATCH v5 0/9] ui: add vdagent implementation and " no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210511090419.nl6akk43mt4g5rua@sirius.home.kraxel.org \
    --to=kraxel@redhat.com \
    --cc=armbru@redhat.com \
    --cc=marcandre.lureau@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).