io: cork TLS writes to avoid small TLS records
PATCH 1/1 io: cork TLS writes to avoid small TLS records
The migration code caches vmstate/ram writes into an iovec and flushes this periodically. The total amount of data to sent may be 100's of KB, but split across many iovec, each of which is potentially quite small.
The QIOChannelTLS receives the iovec, but since 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 lots of small TLS records on the wire. This has shown writes alternate between about 4k and 30 bytes in some tests.
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 stil result in lots of small TLS records hitting the wire which is not good for throughput. The migration code flushes in batches because it wants large writes for high throughput.
To achieve this we must tell GNUTLS to encrypt data but not immediately sent TLS records by "corking" its output. Once the complete iovec has been written to GNUTLS, it can be uncorked allowing it to hit the wire.
There is added complexity with uncorking on non-blocking channels as not all encrypted data can be sent at once. qio_channel_write() will return what was sent, but some data might remain pending inside GNUTLS buffers. A later call to qio_channel_write() will provide the same plain text data buffers that have already been cached. Thus the code must attempt to uncork GNUTLS again to clear pending data and then deduct the equivalent amount of plain text.
--- crypto/tlssession.c | 33 ++++++++++++++ include/crypto/tlssession.h | 25 +++++++++++ include/io/channel-tls.h | 1 + io/channel-tls.c | 87 ++++++++++++++++++++++++++++++++++--- 4 files changed, 140 insertions(+), 6 deletions(-)
crypto/tlssession.c +33 -0
@@ -513,6 +513,39 @@ qcrypto_tls_session_read(QCryptoTLSSession *session, } +void qcrypto_tls_session_write_cork (QCryptoTLSSession *sess) +{ +gnutls_record_cork (sess->handle); +} + + +ssize_t qcrypto_tls_session_write_uncork (QCryptoTLSSession *sess, +Error **errp) +{ +int ret; + ret =gnutls_record_uncork (sess->handle,0 ); +if (ret ==GNUTLS_E_AGAIN || + ret ==GNUTLS_E_INTERRUPTED ) { + ret =gnutls_record_check_corked (sess->handle); +if (ret <0 ) { +error_setg (errp, +"Cannot query pending TLS output: %s" , +gnutls_strerror (ret)); +return -1 ; + } + +return ret; + }else if (ret <0 ) { +error_setg (errp, +"Cannot uncork TLS output: %s" , +gnutls_strerror (ret)); +return -1 ; + } + +return 0 ; +} + +size_t qcrypto_tls_session_check_pending (QCryptoTLSSession *session) {
include/crypto/tlssession.h +25 -0
@@ -208,6 +208,31 @@ typedef ssize_t (*QCryptoTLSSessionReadFunc)(void *buf,void *opaque, Error **errp); +/** +* qcrypto_tls_session_write_cork: +* @sess: the TLS session object +* +* Causes future qcrypto_tls_session_write() calls to encrypt +* and queue data, without sending on the wire. +*/ +void qcrypto_tls_session_write_cork (QCryptoTLSSession *sess); + +/** +* qcrypto_tls_session_write_uncork: +* @sess: the TLS session object +* @errp: pointer to a NULL-initialized error object +* +* Attempt to send previously queued data. If the underlying +* stream is non-blocking, then only a subset of data (if any) +* may be written. To process outstanding data, this method +* must be called again until it returns 0. +* +* Returns: the number of bytes remaining to be sent, +* or -1 on error. +*/ +ssize_t qcrypto_tls_session_write_uncork (QCryptoTLSSession *sess, +Error **errp); +/** * qcrypto_tls_session_set_callbacks: * @sess: the TLS session object
include/io/channel-tls.h +1 -0
@@ -50,6 +50,7 @@ struct QIOChannelTLS {QIOChannelShutdown shutdown;guint hs_ioc_tag;guint bye_ioc_tag; +size_t corked; };/**
io/channel-tls.c +81 -6
111 lines not on this page · raw message
Signature · 1 line
2.55.0
Quoted 42 lines · Daniel P. Berrangé
The migration code caches vmstate/ram writes into an iovec and flushes this periodically. The total amount of data to sent may be 100's of KB, but split across many iovec, each of which is potentially quite small.
The QIOChannelTLS receives the iovec, but since 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 lots of small TLS records on the wire. This has shown writes alternate between about 4k and 30 bytes in some tests.
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 stil result in lots of small TLS records hitting the wire which is not good for throughput. The migration code flushes in batches because it wants large writes for high throughput.
To achieve this we must tell GNUTLS to encrypt data but not immediately sent TLS records by "corking" its output. Once the complete iovec has been written to GNUTLS, it can be uncorked allowing it to hit the wire.
There is added complexity with uncorking on non-blocking channels as not all encrypted data can be sent at once.
qio_channel_write()will return what was sent, but some data might remain pending inside GNUTLS buffers. A later call toqio_channel_write()will provide the same plain text data buffers that have already been cached. Thus the code must attempt to uncork GNUTLS again to clear pending data and then deduct the equivalent amount of plain text.
Quoted 1 line
Signed-off-by Daniel P. Berrangé <[email protected]>
I wanted to test across two hosts on this patch, not for the issue because I can't reproduce, but for perf, due to the expected reduce on write syscalls. But then one of my hosts died.. so I only did a run locally with PSK, running high dirty loads and sample the bandwidth without multifd.
vanilla:
2203.89 2061.09 2243.31 2241.19 2183.49
2186.59 (±74.59)
patched:
3185.10 3223.91 2877.11 2987.71 2632.30
2981.23 (±241.40)
That's 36% improvement.
I still think we can do more things on top when we will care about perf (ktls might be the next to think about), but it looks a good start besides fixing the extremely (kind of a bug) slowness.
Thanks,
Signature · 1 line
Peter Xu
Quoted 1 line · Peter Xu
Reviewed-by Peter Xu <[email protected]>
Ah, I forgot to say this one still breaks !CONFIG_TLS builds, so as long as that part will be fixed the R-b stands.
Thanks,
Signature · 1 line
Peter Xu
Hi Daniel,
Quoted 42 lines · Daniel P. Berrangé
The migration code caches vmstate/ram writes into an iovec and flushes this periodically. The total amount of data to sent may be 100's of KB, but split across many iovec, each of which is potentially quite small.
The QIOChannelTLS receives the iovec, but since 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 lots of small TLS records on the wire. This has shown writes alternate between about 4k and 30 bytes in some tests.
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 stil result in lots of small TLS records hitting the wire which is not good for throughput. The migration code flushes in batches because it wants large writes for high throughput.
To achieve this we must tell GNUTLS to encrypt data but not immediately sent TLS records by "corking" its output. Once the complete iovec has been written to GNUTLS, it can be uncorked allowing it to hit the wire.
There is added complexity with uncorking on non-blocking channels as not all encrypted data can be sent at once.
qio_channel_write()will return what was sent, but some data might remain pending inside GNUTLS buffers. A later call toqio_channel_write()will provide the same plain text data buffers that have already been cached. Thus the code must attempt to uncork GNUTLS again to clear pending data and then deduct the equivalent amount of plain text.
Quoted 7 lines
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/tlssession.c | 33 ++++++++++++++ include/crypto/tlssession.h | 25 +++++++++++ include/io/channel-tls.h | 1 + io/channel-tls.c | 87 ++++++++++++++++++++++++++++++++++--- 4 files changed, 140 insertions(+), 6 deletions(-)
I have tested the patch and after applying I could no longer reproduce the "go slow" issue.
Thanks!