QEMU-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Daniel P. Berrangé" <berrange@redhat.com>,
	"Peter Xu" <peterx@redhat.com>,
	"Juraj Marcin" <jmarcin@redhat.com>,
	"Fabiano Rosas" <farosas@suse.de>
Subject: [PATCH] io: bounce-buffer TLS writes to avoid nagle go-slow
Date: Mon,  7 Sep 2026 15:51:12 +0100	[thread overview]
Message-ID: <20260907145112.852497-1-berrange@redhat.com> (raw)

The migration code caches vmstate/ram writes into an iovec and
flushes this every 128kb.

The QIOChannelTLS receives the iovec, but size GNUTLS cannot
accept iovec data, it iterates calling send for each element.

As a result of the migration data pattern, this results in
GNUTLS putting writes on the wire that alternate between about
4k and 30 bytes.

This is triggering the nagle algorithm on migration-test for
many of the TLS test cases, resulting in a "go slow" for I/O
that eventually hits the migration timeout configured by the
test.

Not every contributor reports seeing the "go slow" but for
those who do see it, it hits >= 95% of the time for at least
one of the migration TLS test cases run by 'make check'.

While we could disable the nagle algorithm (and multifd
channels already do this), that would just lead to lots of
small TCP packets hitting the wire which is not good for
throughput. The migration code flushes in batches of 128kb
because it wants large writes for high throughput.

The only way to achieve this in the TLS code is to bounce
buffer the writes in order to flatten the iovec. We cannot
fully flatten, however, since GNUTLS puts a cap on the max
TLS record size it is willing to send, which is negotiated
with the server, typically 16 kb out of the box.

This patch thus queries the max TLS record size and then
flattens the iovec into buffers of this size. If the
iovec only contains a single element, bounce buffering
is skipped to avoid the redundant copy.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 crypto/tlssession.c         | 10 ++++++++++
 include/crypto/tlssession.h |  2 ++
 include/io/channel-tls.h    |  2 ++
 io/channel-tls.c            | 35 ++++++++++++++++++++++++++++++-----
 io/trace-events             |  1 +
 5 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/crypto/tlssession.c b/crypto/tlssession.c
index 314e3e96ba..cf4daf8544 100644
--- a/crypto/tlssession.c
+++ b/crypto/tlssession.c
@@ -653,6 +653,11 @@ qcrypto_tls_session_get_peer_name(QCryptoTLSSession *session)
     return NULL;
 }
 
+size_t qcrypto_tls_session_get_send_buffer(QCryptoTLSSession *session)
+{
+    return gnutls_record_get_max_size(session->handle);
+}
+
 
 #else /* ! CONFIG_GNUTLS */
 
@@ -757,4 +762,9 @@ qcrypto_tls_session_get_peer_name(QCryptoTLSSession *sess)
     return NULL;
 }
 
+size_t qcrypto_tls_session_get_send_buffer(QCryptoTLSSession *sess)
+{
+    return 1;
+}
+
 #endif
diff --git a/include/crypto/tlssession.h b/include/crypto/tlssession.h
index 28e419681e..b29648a0a9 100644
--- a/include/crypto/tlssession.h
+++ b/include/crypto/tlssession.h
@@ -369,4 +369,6 @@ int qcrypto_tls_session_get_key_size(QCryptoTLSSession *sess,
  */
 char *qcrypto_tls_session_get_peer_name(QCryptoTLSSession *sess);
 
+size_t qcrypto_tls_session_get_send_buffer(QCryptoTLSSession *sess);
+
 #endif /* QCRYPTO_TLSSESSION_H */
diff --git a/include/io/channel-tls.h b/include/io/channel-tls.h
index 7e9023570d..1c22a3fd07 100644
--- a/include/io/channel-tls.h
+++ b/include/io/channel-tls.h
@@ -50,6 +50,8 @@ struct QIOChannelTLS {
     QIOChannelShutdown shutdown;
     guint hs_ioc_tag;
     guint bye_ioc_tag;
+    char *send_buffer;
+    size_t send_buffer_len;
 };
 
 /**
diff --git a/io/channel-tls.c b/io/channel-tls.c
index 31ec4d236d..ba2699786e 100644
--- a/io/channel-tls.c
+++ b/io/channel-tls.c
@@ -24,6 +24,7 @@
 #include "io/channel-tls.h"
 #include "trace.h"
 #include "qemu/atomic.h"
+#include "qemu/iov.h"
 
 
 static ssize_t qio_channel_tls_write_handler(const void *buf,
@@ -201,6 +202,12 @@ static gboolean qio_channel_tls_handshake_task(QIOChannelTLS *ioc,
         } else {
             trace_qio_channel_tls_credentials_allow(ioc);
         }
+
+        ioc->send_buffer_len = qcrypto_tls_session_get_send_buffer(
+            ioc->session);
+        ioc->send_buffer = g_new0(char, ioc->send_buffer_len);
+        trace_qio_channel_tls_send_buffer_len(ioc, ioc->send_buffer_len);
+
         qio_task_complete(task);
         return TRUE;
     } else {
@@ -376,6 +383,7 @@ static void qio_channel_tls_finalize(Object *obj)
         g_clear_handle_id(&ioc->bye_ioc_tag, g_source_remove);
     }
 
+    g_free(ioc->send_buffer);
     object_unref(OBJECT(ioc->master));
     qcrypto_tls_session_free(ioc->session);
 }
@@ -446,13 +454,30 @@ static ssize_t qio_channel_tls_writev(QIOChannel *ioc,
                                       Error **errp)
 {
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
-    size_t i;
     ssize_t done = 0;
+    size_t tot = iov_size(iov, niov);
 
-    for (i = 0 ; i < niov ; i++) {
+    /* Skip bounce buffer in simple case */
+    if (niov == 1) {
         ssize_t ret = qcrypto_tls_session_write(tioc->session,
-                                                iov[i].iov_base,
-                                                iov[i].iov_len,
+                                                iov[0].iov_base,
+                                                iov[0].iov_len,
+                                                errp);
+        if (ret == QCRYPTO_TLS_SESSION_ERR_BLOCK) {
+            return QIO_CHANNEL_ERR_BLOCK;
+        } else if (ret < 0) {
+            return -1;
+        }
+        return ret;
+    }
+
+    while (done < tot) {
+        size_t got = iov_to_buf(iov, niov, done,
+                                tioc->send_buffer,
+                                tioc->send_buffer_len);
+        ssize_t ret = qcrypto_tls_session_write(tioc->session,
+                                                tioc->send_buffer,
+                                                got,
                                                 errp);
         if (ret == QCRYPTO_TLS_SESSION_ERR_BLOCK) {
             if (done) {
@@ -464,7 +489,7 @@ static ssize_t qio_channel_tls_writev(QIOChannel *ioc,
             return -1;
         }
         done += ret;
-        if (ret < iov[i].iov_len) {
+        if (ret < got) {
             break;
         }
     }
diff --git a/io/trace-events b/io/trace-events
index ec91453335..88ebd5b478 100644
--- a/io/trace-events
+++ b/io/trace-events
@@ -44,6 +44,7 @@ qio_channel_tls_handshake_pending(void *ioc, int status) "TLS handshake pending
 qio_channel_tls_handshake_fail(void *ioc) "TLS handshake fail ioc=%p"
 qio_channel_tls_handshake_complete(void *ioc) "TLS handshake complete ioc=%p"
 qio_channel_tls_handshake_cancel(void *ioc) "TLS handshake cancel ioc=%p"
+qio_channel_tls_send_buffer_len(void *ioc, int len) "TLS send buffer len ioc=%p len=%d"
 qio_channel_tls_bye_start(void *ioc) "TLS termination start ioc=%p"
 qio_channel_tls_bye_pending(void *ioc, int status) "TLS termination pending ioc=%p status=%d"
 qio_channel_tls_bye_fail(void *ioc) "TLS termination fail ioc=%p"
-- 
2.55.0



             reply	other threads:[~2026-09-07 14:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-07 14:51 Daniel P. Berrangé [this message]
2026-09-08 18:12 ` [PATCH] io: bounce-buffer TLS writes to avoid nagle go-slow Peter Xu
2026-09-08 19:24   ` Daniel P. Berrangé
2026-09-09 21:09     ` Peter Xu

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=20260907145112.852497-1-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=farosas@suse.de \
    --cc=jmarcin@redhat.com \
    --cc=peterx@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