Skip to contentSkip to conversation

qemu-nbd: Use SOMAXCONN for socket listen() backlog

PATCH Superseded Eric Blake 4 participants 6 messages Feb 5, 2021 – Feb 8, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH v2 1/1 qemu-nbd: Use SOMAXCONN for socket listen() backlog

Our default of a backlog of 1 connection is rather puny, particularly for scenarios where we expect multiple listeners to connect (such as qemu-nbd -e X). This is especially important for Unix sockets, as a definite benefit to clients: at least on Linux, a client trying to connect to a Unix socket with a backlog gets an EAGAIN failure with no way to poll() for when the backlog is no longer present short of sleeping an arbitrary amount of time before retrying.

See https://bugzilla.redhat.com/1925045 for a demonstration of where our low backlog prevents libnbd from connecting as many parallel clients as it wants.

---

v2: target the correct API used by qemu-nbd, rather than an unrelated legacy wrapper [Dan]

 qemu-nbd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
qemu-nbd.c +2 -1
index 608c63e82a25..cd20ee73be19 100644
@@ -965,7 +965,8 @@ int main(int argc, char **argv)
     server = qio_net_listener_new();
     if (socket_activation == 0) {
         saddr = nbd_build_socket_address(sockpath, bindto, port);
-        if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
+        if (qio_net_listener_open_sync(server, saddr, SOMAXCONN,
+                                       &local_err) < 0) {
             object_unref(OBJECT(server));
             error_report_err(local_err);
             exit(EXIT_FAILURE);
Signature · 1 line
2.30.0
#1 lore raw
Quoted 28 lines · Eric Blake

Our default of a backlog of 1 connection is rather puny, particularly for scenarios where we expect multiple listeners to connect (such as qemu-nbd -e X). This is especially important for Unix sockets, as a definite benefit to clients: at least on Linux, a client trying to connect to a Unix socket with a backlog gets an EAGAIN failure with no way to poll() for when the backlog is no longer present short of sleeping an arbitrary amount of time before retrying.

See https://bugzilla.redhat.com/1925045 for a demonstration of where our low backlog prevents libnbd from connecting as many parallel clients as it wants.

---

v2: target the correct API used by qemu-nbd, rather than an unrelated legacy wrapper [Dan]

 qemu-nbd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
qemu-nbd.c +0 -0
index 608c63e82a25..cd20ee73be19 100644

qemu-nbd.c int main(int argc, char **argv) · line 968

Quoted 5 lines
qemu-nbd.c +1 -1
@@ -965,7 +965,8 @@ int main(int argc, char **argv)
     server = qio_net_listener_new();
     if (socket_activation == 0) {
         saddr = nbd_build_socket_address(sockpath, bindto, port);
-        if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
+        if (qio_net_listener_open_sync(server, saddr, SOMAXCONN,

Shouldn't we use value based on --shared=N?

Using maximum value makes sense for generic server expecting to handle many connections from different clients. qemu-nbd is typically used by one client, and we
need to make it possible to connect a known number of connections quickly.

Quoted 8 lines
qemu-nbd.c +1 -1
+                                       &local_err) < 0) {
             object_unref(OBJECT(server));
             error_report_err(local_err);
             exit(EXIT_FAILURE);
--

2.30.0

author
#2 lore raw
Quoted 24 lines · Nir Soffer
Quoted 23 lines · Eric Blake

Our default of a backlog of 1 connection is rather puny, particularly for scenarios where we expect multiple listeners to connect (such as qemu-nbd -e X). This is especially important for Unix sockets, as a definite benefit to clients: at least on Linux, a client trying to connect to a Unix socket with a backlog gets an EAGAIN failure with no way to poll() for when the backlog is no longer present short of sleeping an arbitrary amount of time before retrying.

See https://bugzilla.redhat.com/1925045 for a demonstration of where our low backlog prevents libnbd from connecting as many parallel clients as it wants.

---

v2: target the correct API used by qemu-nbd, rather than an unrelated legacy wrapper [Dan]

 qemu-nbd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

qemu-nbd.c int main(int argc, char **argv) · line 968

Quoted 10 lines
qemu-nbd.c +1 -1
index 608c63e82a25..cd20ee73be19 100644
@@ -965,7 +965,8 @@ int main(int argc, char **argv)
     server = qio_net_listener_new();
     if (socket_activation == 0) {
         saddr = nbd_build_socket_address(sockpath, bindto, port);
-        if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
+        if (qio_net_listener_open_sync(server, saddr, SOMAXCONN,

Shouldn't we use value based on --shared=N?

That's a possibility. I don't know how many resources we tie up if we allow more clients to try to connect() than what we will ultimately accept when --shared is small.

But there's also the issue that for qemu-nbd, we default to 1 client unless you pass -e/--shared on the command line, whereas starting an NBD server in qemu via QMP (including via qemu-storage-daemon) defaults max-connections to 0 meaning unlimited.

Quoted 5 lines

Using maximum value makes sense for generic server expecting to handle many connections from different clients. qemu-nbd is typically used by one client, and we
need to make it possible to connect a known number of connections quickly.

At any rate, when max-connections is specified to something smaller than SOMAXCONN, listen()ing to a smaller number of connections seems reasonable, so I'll play with that for v3.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
Tested-by on v2 1/1
#3 lore raw
In reply to Eric Blake
Quoted 27 lines · Eric Blake

Our default of a backlog of 1 connection is rather puny, particularly for scenarios where we expect multiple listeners to connect (such as qemu-nbd -e X). This is especially important for Unix sockets, as a definite benefit to clients: at least on Linux, a client trying to connect to a Unix socket with a backlog gets an EAGAIN failure with no way to poll() for when the backlog is no longer present short of sleeping an arbitrary amount of time before retrying.

See https://bugzilla.redhat.com/1925045 for a demonstration of where our low backlog prevents libnbd from connecting as many parallel clients as it wants.

---

v2: target the correct API used by qemu-nbd, rather than an unrelated legacy wrapper [Dan]

 qemu-nbd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
qemu-nbd.c +0 -0
index 608c63e82a25..cd20ee73be19 100644

qemu-nbd.c int main(int argc, char **argv) · line 972

Quoted 9 lines
qemu-nbd.c +2 -1
@@ -965,7 +965,8 @@ int main(int argc, char **argv)
     server = qio_net_listener_new();
     if (socket_activation == 0) {
         saddr = nbd_build_socket_address(sockpath, bindto, port);
-        if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
+        if (qio_net_listener_open_sync(server, saddr, SOMAXCONN,
+                                       &local_err) < 0) {
             object_unref(OBJECT(server));
             error_report_err(local_err);
             exit(EXIT_FAILURE);

This one works:

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top
#4 lore raw
In reply to Eric Blake
Quoted 27 lines · Eric Blake

Our default of a backlog of 1 connection is rather puny, particularly for scenarios where we expect multiple listeners to connect (such as qemu-nbd -e X). This is especially important for Unix sockets, as a definite benefit to clients: at least on Linux, a client trying to connect to a Unix socket with a backlog gets an EAGAIN failure with no way to poll() for when the backlog is no longer present short of sleeping an arbitrary amount of time before retrying.

See https://bugzilla.redhat.com/1925045 for a demonstration of where our low backlog prevents libnbd from connecting as many parallel clients as it wants.

---

v2: target the correct API used by qemu-nbd, rather than an unrelated legacy wrapper [Dan]

 qemu-nbd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
qemu-nbd.c +0 -0
index 608c63e82a25..cd20ee73be19 100644

qemu-nbd.c int main(int argc, char **argv) · line 969

Quoted 6 lines
qemu-nbd.c +2 -1
@@ -965,7 +965,8 @@ int main(int argc, char **argv)
     server = qio_net_listener_new();
     if (socket_activation == 0) {
         saddr = nbd_build_socket_address(sockpath, bindto, port);
-        if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
+        if (qio_net_listener_open_sync(server, saddr, SOMAXCONN,
+                                       &local_err) < 0) {

This addresses qemu-nbd, but surely we want to be consistent with the QMP nbd-server-start impl too, in blockdev-nbd.c

qemu-nbd.c int main(int argc, char **argv) · line 972

Quoted 3 lines
qemu-nbd.c +0 -0
             object_unref(OBJECT(server));
             error_report_err(local_err);
             exit(EXIT_FAILURE);

Regards,
Daniel

Signature · 3 lines
|: 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 :|
author
#5 lore raw
Quoted 23 lines · Daniel P. Berrangé
Quoted 22 lines · Eric Blake

Our default of a backlog of 1 connection is rather puny, particularly for scenarios where we expect multiple listeners to connect (such as qemu-nbd -e X). This is especially important for Unix sockets, as a definite benefit to clients: at least on Linux, a client trying to connect to a Unix socket with a backlog gets an EAGAIN failure with no way to poll() for when the backlog is no longer present short of sleeping an arbitrary amount of time before retrying.

See https://bugzilla.redhat.com/1925045 for a demonstration of where our low backlog prevents libnbd from connecting as many parallel clients as it wants.

---

v2: target the correct API used by qemu-nbd, rather than an unrelated legacy wrapper [Dan]

 qemu-nbd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 15 lines

qemu-nbd.c int main(int argc, char **argv) · line 969

Quoted 11 lines
qemu-nbd.c +2 -1
index 608c63e82a25..cd20ee73be19 100644
@@ -965,7 +965,8 @@ int main(int argc, char **argv)
     server = qio_net_listener_new();
     if (socket_activation == 0) {
         saddr = nbd_build_socket_address(sockpath, bindto, port);
-        if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
+        if (qio_net_listener_open_sync(server, saddr, SOMAXCONN,
+                                       &local_err) < 0) {

This addresses qemu-nbd, but surely we want to be consistent with the QMP nbd-server-start impl too, in blockdev-nbd.c

Good point, I'll send v3.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org