[PATCH v4 1/3] io: Add helper for setting socket send buffer size

Nir Soffer posted 3 patches 7 months ago
Maintainers: "Daniel P. Berrangé" <berrange@redhat.com>, Eric Blake <eblake@redhat.com>, Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
[PATCH v4 1/3] io: Add helper for setting socket send buffer size
Posted by Nir Soffer 7 months ago
Testings reading and writing from qemu-nbd using a unix domain socket
shows that the platform default send buffer size is too low, leading to
poor performance and hight cpu usage.

Add a helper for setting socket send buffer size to be used in NBD code.
It can also be used in other context.

We don't need a helper for receive buffer size since it is not used with
unix domain sockets. This is documented for Linux, and not documented
for macOS.

Failing to set the socket buffer size is not a fatal error, but the
caller may want to warn about the failure.

Signed-off-by: Nir Soffer <nirsof@gmail.com>
---
 include/io/channel-socket.h | 13 +++++++++++++
 io/channel-socket.c         | 11 +++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/io/channel-socket.h b/include/io/channel-socket.h
index ab15577d38..a88cf8b3a9 100644
--- a/include/io/channel-socket.h
+++ b/include/io/channel-socket.h
@@ -261,5 +261,18 @@ QIOChannelSocket *
 qio_channel_socket_accept(QIOChannelSocket *ioc,
                           Error **errp);
 
+/**
+ * qio_channel_socket_set_send_buffer:
+ * @ioc: the socket channel object
+ * @size: buffer size
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Set the underlying socket send buffer size.
+ *
+ * Retruns: 0 on success, or -1 on error.
+ */
+int qio_channel_socket_set_send_buffer(QIOChannelSocket *ioc,
+                                       size_t size,
+                                       Error **errp);
 
 #endif /* QIO_CHANNEL_SOCKET_H */
diff --git a/io/channel-socket.c b/io/channel-socket.c
index 608bcf066e..a776cd45f5 100644
--- a/io/channel-socket.c
+++ b/io/channel-socket.c
@@ -78,6 +78,17 @@ qio_channel_socket_new(void)
     return sioc;
 }
 
+int qio_channel_socket_set_send_buffer(QIOChannelSocket *ioc,
+                                       size_t size,
+                                       Error **errp)
+{
+    if (setsockopt(ioc->fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0) {
+        error_setg_errno(errp, errno, "Unable to set socket send buffer size");
+        return -1;
+    }
+
+    return 0;
+}
 
 static int
 qio_channel_socket_set_fd(QIOChannelSocket *sioc,
-- 
2.39.5 (Apple Git-154)
Re: [PATCH v4 1/3] io: Add helper for setting socket send buffer size
Posted by Richard W.M. Jones 7 months ago
On Sat, May 17, 2025 at 11:11:52PM +0300, Nir Soffer wrote:
> Testings reading and writing from qemu-nbd using a unix domain socket

Nit pick that this should be "Testing", not "Testings".  (Or
"Experiments" if you really wanted a plural noun.)

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v
Re: [PATCH v4 1/3] io: Add helper for setting socket send buffer size
Posted by Eric Blake 7 months ago
On Mon, May 19, 2025 at 12:20:48PM +0100, Richard W.M. Jones wrote:
> On Sat, May 17, 2025 at 11:11:52PM +0300, Nir Soffer wrote:
> > Testings reading and writing from qemu-nbd using a unix domain socket
> 
> Nit pick that this should be "Testing", not "Testings".  (Or
> "Experiments" if you really wanted a plural noun.)

I'll tweak that while staging the patch for a pull request; no need
for another revision.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
Re: [PATCH v4 1/3] io: Add helper for setting socket send buffer size
Posted by Daniel P. Berrangé 7 months ago
On Sat, May 17, 2025 at 11:11:52PM +0300, Nir Soffer wrote:
> Testings reading and writing from qemu-nbd using a unix domain socket
> shows that the platform default send buffer size is too low, leading to
> poor performance and hight cpu usage.
> 
> Add a helper for setting socket send buffer size to be used in NBD code.
> It can also be used in other context.
> 
> We don't need a helper for receive buffer size since it is not used with
> unix domain sockets. This is documented for Linux, and not documented
> for macOS.
> 
> Failing to set the socket buffer size is not a fatal error, but the
> caller may want to warn about the failure.
> 
> Signed-off-by: Nir Soffer <nirsof@gmail.com>
> ---
>  include/io/channel-socket.h | 13 +++++++++++++
>  io/channel-socket.c         | 11 +++++++++++
>  2 files changed, 24 insertions(+)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|