Skip to contentSkip to conversation

NBD socket backlog

PATCH In review Eric Blake 4 participants 10 messages Feb 9, 2021 – Feb 10, 2021

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

v3 cover NBD socket backlog

in v2:
- also adjust backlog of QMP nbd-server-start [Dan]
- tweak qemu-nbd backlog to -e when not persistent [Nir]
- allow qemu-nbd -e0 for symmetry with QMP [new patch 2]

Eric Blake (2):
qemu-nbd: Use SOMAXCONN for socket listen() backlog
qemu-nbd: Permit --shared=0 for unlimited clients

 docs/tools/qemu-nbd.rst |  4 ++--
 blockdev-nbd.c          |  7 ++++++-
 qemu-nbd.c              | 15 +++++++++++----
 3 files changed, 19 insertions(+), 7 deletions(-)
Signature · 1 line
2.30.0
author
#1 lore raw

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

Our default of a backlog of 1 connection is rather puny; it gets in the way when we are explicitly allowing multiple clients (such as qemu-nbd -e N [--shared], or nbd-server-start with its default "max-connections":0 for unlimited), but is even a problem when we stick to qemu-nbd's default of only 1 active client but use -t [--persistent] where a second client can start using the server once the first finishes. While the effects are less noticeable on TCP sockets (since the client can poll() to learn when the server is ready again), it is definitely observable on Unix sockets, where on Unix, a client will fail with EAGAIN and no recourse but to sleep an arbitrary amount of time before retrying if the server backlog is already full.

Since QMP nbd-server-start is always persistent, it now always requests a backlog of SOMAXCONN; meanwhile, qemu-nbd will request SOMAXCONN if persistent, otherwise its backlog should be based on the expected number of clients.

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.

---
 blockdev-nbd.c |  7 ++++++-
 qemu-nbd.c     | 10 +++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)
blockdev-nbd.c +6 -1
index d8443d235b73..b264620b98d8 100644
@@ -134,7 +134,12 @@ void nbd_server_start(SocketAddress *addr, const char *tls_creds,
     qio_net_listener_set_name(nbd_server->listener,
                               "nbd-listener");
 
-    if (qio_net_listener_open_sync(nbd_server->listener, addr, 1, errp) < 0) {
+    /*
+     * Because this server is persistent, a backlog of SOMAXCONN is
+     * better than trying to size it to max_connections.
+     */
+    if (qio_net_listener_open_sync(nbd_server->listener, addr, SOMAXCONN,
+                                   errp) < 0) {
         goto error;
     }
 
qemu-nbd.c +9 -1
index 608c63e82a25..1a340ea4858d 100644
@@ -964,8 +964,16 @@ int main(int argc, char **argv)
 
     server = qio_net_listener_new();
     if (socket_activation == 0) {
+        int backlog;
+
+        if (persistent) {
+            backlog = SOMAXCONN;
+        } else {
+            backlog = MIN(shared, SOMAXCONN);
+        }
         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, backlog,
+                                       &local_err) < 0) {
             object_unref(OBJECT(server));
             error_report_err(local_err);
             exit(EXIT_FAILURE);
Signature · 1 line
2.30.0
Tested-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] qemu-nbd: Use SOMAXCONN for socket listen() backlog

Quoted 59 lines · Eric Blake

Our default of a backlog of 1 connection is rather puny; it gets in the way when we are explicitly allowing multiple clients (such as qemu-nbd -e N [--shared], or nbd-server-start with its default "max-connections":0 for unlimited), but is even a problem when we stick to qemu-nbd's default of only 1 active client but use -t [--persistent] where a second client can start using the server once the first finishes. While the effects are less noticeable on TCP sockets (since the client can poll() to learn when the server is ready again), it is definitely observable on Unix sockets, where on Unix, a client will fail with EAGAIN and no recourse but to sleep an arbitrary amount of time before retrying if the server backlog is already full.

Since QMP nbd-server-start is always persistent, it now always requests a backlog of SOMAXCONN; meanwhile, qemu-nbd will request SOMAXCONN if persistent, otherwise its backlog should be based on the expected number of clients.

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.

---
 blockdev-nbd.c |  7 ++++++-
 qemu-nbd.c     | 10 +++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)
blockdev-nbd.c +6 -1
index d8443d235b73..b264620b98d8 100644
@@ -134,7 +134,12 @@ void nbd_server_start(SocketAddress *addr, const char *tls_creds,
     qio_net_listener_set_name(nbd_server->listener,
                               "nbd-listener");
 
-    if (qio_net_listener_open_sync(nbd_server->listener, addr, 1, errp) < 0) {
+    /*
+     * Because this server is persistent, a backlog of SOMAXCONN is
+     * better than trying to size it to max_connections.
+     */
+    if (qio_net_listener_open_sync(nbd_server->listener, addr, SOMAXCONN,
+                                   errp) < 0) {
         goto error;
     }
 
qemu-nbd.c +4 -0
index 608c63e82a25..1a340ea4858d 100644
@@ -964,8 +964,16 @@ int main(int argc, char **argv)
 
     server = qio_net_listener_new();
     if (socket_activation == 0) {
+        int backlog;
+
+        if (persistent) {
+            backlog = SOMAXCONN;

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

Quoted 10 lines
qemu-nbd.c +5 -1
+        } else {
+            backlog = MIN(shared, SOMAXCONN);
+        }
         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, backlog,
+                                       &local_err) < 0) {
             object_unref(OBJECT(server));
             error_report_err(local_err);
             exit(EXIT_FAILURE);

Works fine here, so:

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
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW
author
#3 lore raw

Re: [PATCH v3 1/2] qemu-nbd: Use SOMAXCONN for socket listen() backlog

Quoted 10 lines · Richard W.M. Jones
Quoted 9 lines · Eric Blake

Our default of a backlog of 1 connection is rather puny; it gets in the way when we are explicitly allowing multiple clients (such as qemu-nbd -e N [--shared], or nbd-server-start with its default "max-connections":0 for unlimited), but is even a problem when we stick to qemu-nbd's default of only 1 active client but use -t [--persistent] where a second client can start using the server once the first finishes. While the effects are less noticeable on TCP sockets (since the client can poll() to learn when the server is ready again), it is definitely observable on Unix sockets, where on Unix, a

s/where on Unix/where on Linux/

Quoted 22 lines
Quoted 12 lines

client will fail with EAGAIN and no recourse but to sleep an arbitrary amount of time before retrying if the server backlog is already full.

Since QMP nbd-server-start is always persistent, it now always requests a backlog of SOMAXCONN; meanwhile, qemu-nbd will request SOMAXCONN if persistent, otherwise its backlog should be based on the expected number of clients.

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.

Quoted 8 lines
---
 blockdev-nbd.c |  7 ++++++-
 qemu-nbd.c     | 10 +++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)
Quoted 4 lines

Works fine here, so:

Thanks for testing.

Quoted 2 lines

Rich.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v3 1/2
#4 lore raw

Re: [PATCH v3 1/2] qemu-nbd: Use SOMAXCONN for socket listen() backlog

In reply to Eric Blake
Quoted 21 lines · Eric Blake

Our default of a backlog of 1 connection is rather puny; it gets in the way when we are explicitly allowing multiple clients (such as qemu-nbd -e N [--shared], or nbd-server-start with its default "max-connections":0 for unlimited), but is even a problem when we stick to qemu-nbd's default of only 1 active client but use -t [--persistent] where a second client can start using the server once the first finishes. While the effects are less noticeable on TCP sockets (since the client can poll() to learn when the server is ready again), it is definitely observable on Unix sockets, where on Unix, a client will fail with EAGAIN and no recourse but to sleep an arbitrary amount of time before retrying if the server backlog is already full.

Since QMP nbd-server-start is always persistent, it now always requests a backlog of SOMAXCONN; meanwhile, qemu-nbd will request SOMAXCONN if persistent, otherwise its backlog should be based on the expected number of clients.

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.

Quoted 7 lines
---
 blockdev-nbd.c |  7 ++++++-
 qemu-nbd.c     | 10 +++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)

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 :|
#5 lore raw

Re: [PATCH v3 1/2] qemu-nbd: Use SOMAXCONN for socket listen() backlog

In reply to Eric Blake
Quoted 13 lines · Eric Blake

Our default of a backlog of 1 connection is rather puny; it gets in the way when we are explicitly allowing multiple clients (such as qemu-nbd -e N [--shared], or nbd-server-start with its default "max-connections":0 for unlimited), but is even a problem when we stick to qemu-nbd's default of only 1 active client but use -t [--persistent] where a second client can start using the server once the first finishes. While the effects are less noticeable on TCP sockets (since the client can poll() to learn when the server is ready again), it is definitely observable on Unix sockets, where on Unix, a client will fail with EAGAIN and no recourse but to sleep an arbitrary amount of time before retrying if the server backlog is already full.

Quoted 2 lines

Since QMP nbd-server-start is always persistent, it now always requests a backlog of SOMAXCONN;

This makes sense since we don't limit the number of connections.

Quoted 3 lines

meanwhile, qemu-nbd will request
SOMAXCONN if persistent, otherwise its backlog should be based on the expected number of clients.

If --persistent is used without --shared, we allow only one concurrent connection, so not clear why we need maximum backlog.

I think that separating --persistent and --shared would be easier to understand and use. The backlog will always be based on shared value.

Quoted 17 lines

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.

---
 blockdev-nbd.c |  7 ++++++-
 qemu-nbd.c     | 10 +++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)
blockdev-nbd.c +0 -0
index d8443d235b73..b264620b98d8 100644

blockdev-nbd.c void nbd_server_start(SocketAddress *addr, const char *tls_creds, · line 139

Quoted 7 lines
blockdev-nbd.c +3 -1
@@ -134,7 +134,12 @@ void nbd_server_start(SocketAddress *addr, const char *tls_creds,
     qio_net_listener_set_name(nbd_server->listener,
                               "nbd-listener");
 
-    if (qio_net_listener_open_sync(nbd_server->listener, addr, 1, errp) < 0) {
+    /*
+     * Because this server is persistent, a backlog of SOMAXCONN is
+     * better than trying to size it to max_connections.

The comment is not clear. Previously we used hard code value (1) but we do support more than one connection. Maybe it is better to explain that we don't know how many connections are needed?

Quoted 11 lines
blockdev-nbd.c +3 -0
+     */
+    if (qio_net_listener_open_sync(nbd_server->listener, addr, SOMAXCONN,
+                                   errp) < 0) {
         goto error;
     }
 
qemu-nbd.c +0 -0
index 608c63e82a25..1a340ea4858d 100644

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

Quoted 7 lines
qemu-nbd.c +4 -0
@@ -964,8 +964,16 @@ int main(int argc, char **argv)
 
     server = qio_net_listener_new();
     if (socket_activation == 0) {
+        int backlog;
+
+        if (persistent) {
+            backlog = SOMAXCONN;

This increases the backlog, but since default shared is still 1, we will not accept more than 1 connection, so not clear why SOMAXCONN is better.

Quoted 13 lines
qemu-nbd.c +5 -2
+        } else {
+            backlog = MIN(shared, SOMAXCONN);
+        }
         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, backlog,
+                                       &local_err) < 0) {
             object_unref(OBJECT(server));
             error_report_err(local_err);
             exit(EXIT_FAILURE);
--

2.30.0

author
#6 lore raw

Re: [PATCH v3 1/2] qemu-nbd: Use SOMAXCONN for socket listen() backlog

Quoted 17 lines · Nir Soffer
Quoted 13 lines · Eric Blake

Our default of a backlog of 1 connection is rather puny; it gets in the way when we are explicitly allowing multiple clients (such as qemu-nbd -e N [--shared], or nbd-server-start with its default "max-connections":0 for unlimited), but is even a problem when we stick to qemu-nbd's default of only 1 active client but use -t [--persistent] where a second client can start using the server once the first finishes. While the effects are less noticeable on TCP sockets (since the client can poll() to learn when the server is ready again), it is definitely observable on Unix sockets, where on Unix, a client will fail with EAGAIN and no recourse but to sleep an arbitrary amount of time before retrying if the server backlog is already full.

Quoted 2 lines

Since QMP nbd-server-start is always persistent, it now always requests a backlog of SOMAXCONN;

Quoted 8 lines

This makes sense since we don't limit the number of connections.

Quoted 3 lines

meanwhile, qemu-nbd will request
SOMAXCONN if persistent, otherwise its backlog should be based on the expected number of clients.

If --persistent is used without --shared, we allow only one concurrent connection, so not clear why we need maximum backlog.

We only allow one active connection, but other clients can queue up to also take advantage of the server once the first client disconnects. A larger backlog allows those additional clients to reach the point where they can poll() for activity, rather than getting EAGAIN failures.

Quoted 8 lines

I think that separating --persistent and --shared would be easier to understand and use. The backlog will always be based on shared value.

Quoted 3 lines

+++ b/qemu-nbd.c

(quoted diff) +0 -0
@@ -964,8 +964,16 @@ int main(int argc, char **argv)
 
Quoted 10 lines

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

Quoted 6 lines
qemu-nbd.c +4 -0
     server = qio_net_listener_new();
     if (socket_activation == 0) {
+        int backlog;
+
+        if (persistent) {
+            backlog = SOMAXCONN;

This increases the backlog, but since default shared is still 1, we will not accept more than 1 connection, so not clear why SOMAXCONN is better.

While we aren't servicing the next client yet, we are at least allowing them to make it further in their connection by supporting a backlog.

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

PATCH v3 2/2 qemu-nbd: Permit --shared=0 for unlimited clients

In reply to Eric Blake

This gives us better feature parity with QMP nbd-server-start, where max-connections defaults to 0 for unlimited.

---
 docs/tools/qemu-nbd.rst | 4 ++--
 qemu-nbd.c              | 7 +++----
 2 files changed, 5 insertions(+), 6 deletions(-)
docs/tools/qemu-nbd.rst +2 -2
index fe41336dc550..ee862fa0bc02 100644
@@ -136,8 +136,8 @@ driver options if ``--image-opts`` is specified.
 .. option:: -e, --shared=NUM
 
   Allow up to *NUM* clients to share the device (default
-  ``1``). Safe for readers, but for now, consistency is not
-  guaranteed between multiple writers.
+  ``1``), 0 for unlimited. Safe for readers, but for now,
+  consistency is not guaranteed between multiple writers.
 
 .. option:: -t, --persistent
 
qemu-nbd.c +3 -3
index 1a340ea4858d..5416509ece18 100644
@@ -328,7 +328,7 @@ static void *nbd_client_thread(void *arg)
 
 static int nbd_can_accept(void)
 {
-    return state == RUNNING && nb_fds < shared;
+    return state == RUNNING && (shared == 0 || nb_fds < shared);
 }
 
 static void nbd_update_server_watch(void);
@@ -706,8 +706,8 @@ int main(int argc, char **argv)
             device = optarg;
             break;
         case 'e':
             if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
-                shared < 1) {
+                shared < 0) {
                 error_report("Invalid shared device number '%s'", optarg);
                 exit(EXIT_FAILURE);
             }
@@ -966,7 +965,7 @@ int main(int argc, char **argv)
     if (socket_activation == 0) {
         int backlog;
 
-        if (persistent) {
+        if (persistent || shared == 0) {
             backlog = SOMAXCONN;
         } else {
             backlog = MIN(shared, SOMAXCONN);
Signature · 1 line
2.30.0
Reviewed-by on v3 2/2
#8 lore raw

Re: [PATCH v3 2/2] qemu-nbd: Permit --shared=0 for unlimited clients

Quoted 8 lines · Eric Blake

This gives us better feature parity with QMP nbd-server-start, where max-connections defaults to 0 for unlimited.

---
 docs/tools/qemu-nbd.rst | 4 ++--
 qemu-nbd.c              | 7 +++----
 2 files changed, 5 insertions(+), 6 deletions(-)

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 :|
Reviewed-by on v3 2/2
#9 lore raw

Re: [PATCH v3 2/2] qemu-nbd: Permit --shared=0 for unlimited clients

In reply to Eric Blake
Quoted 3 lines · Eric Blake

This gives us better feature parity with QMP nbd-server-start, where max-connections defaults to 0 for unlimited.

Sound useful

Quoted 54 lines
---
 docs/tools/qemu-nbd.rst | 4 ++--
 qemu-nbd.c              | 7 +++----
 2 files changed, 5 insertions(+), 6 deletions(-)
docs/tools/qemu-nbd.rst +2 -2
index fe41336dc550..ee862fa0bc02 100644
@@ -136,8 +136,8 @@ driver options if ``--image-opts`` is specified.
 .. option:: -e, --shared=NUM
 
   Allow up to *NUM* clients to share the device (default
-  ``1``). Safe for readers, but for now, consistency is not
-  guaranteed between multiple writers.
+  ``1``), 0 for unlimited. Safe for readers, but for now,
+  consistency is not guaranteed between multiple writers.
 
 .. option:: -t, --persistent
 
qemu-nbd.c +3 -4
index 1a340ea4858d..5416509ece18 100644
@@ -328,7 +328,7 @@ static void *nbd_client_thread(void *arg)
 
 static int nbd_can_accept(void)
 {
-    return state == RUNNING && nb_fds < shared;
+    return state == RUNNING && (shared == 0 || nb_fds < shared);
 }
 
 static void nbd_update_server_watch(void);
@@ -706,8 +706,8 @@ int main(int argc, char **argv)
             device = optarg;
             break;
         case 'e':
             if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
-                shared < 1) {
+                shared < 0) {
                 error_report("Invalid shared device number '%s'", optarg);
                 exit(EXIT_FAILURE);
             }
@@ -966,7 +965,7 @@ int main(int argc, char **argv)
     if (socket_activation == 0) {
         int backlog;
 
-        if (persistent) {
+        if (persistent || shared == 0) {
             backlog = SOMAXCONN;
         } else {
             backlog = MIN(shared, SOMAXCONN);
--
Quoted 2 lines

2.30.0