Skip to contentSkip to conversation

util/qemu-sockets: Introduce inet socket options controlling TCP keep-alive

PATCH Superseded Juraj Marcin 2 participants 13 messages Apr 8, 2025 – Apr 30, 2025 Arm/Amlogic Meson SoC support

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover util/qemu-sockets: Introduce inet socket options controlling TCP keep-alive

This series extends the work introduced by commit aec21d3175 ("qapi: Add InetSocketAddress member keep-alive"). [1]

First, the series fixes an issue in qio_dns_resolver_lookup_sync_inet(), where the InetSocketAddress structure is only partially copied. Next, it refactors setting client socket options into a separate function and the success and failure paths in inet_listen_saddr() in preparation for keep-alive support on server sockets and the addition of new TCP keep-alive options.

Then, the series adds support for keep-alive on server sockets and adds three new InetSocketAddress options for control of TCP keep-alive settings. By default, the value of all new settings is 0, which means no custom socket option value is set.

This is useful, for example, for live migration. In case there is no traffic from the destination to the source machine during postcopy, the destination cannot detect a failed connection due to a lack of non-acknowledged packets and stays in the postcopy-active state until paused by the management of the QEMU instance.

[1]: https://lore.kernel.org/all/[email protected]/

---
V3:
- moved the InetSocketAddress struct copy fix and the common function setting socket options into a separate commit
- refactored inet_listen_saddr()

V2:
- moved socket options setting into a common function for both server and client sockets (suggested by Vladimir)

Juraj Marcin (5):
io: Fix partial struct copy in qio_dns_resolver_lookup_sync_inet() util/qemu-sockets: Refactor setting client sockopts into a separate
function
util/qemu-sockets: Refactor success and failure paths in
inet_listen_saddr()
util/qemu-sockets: Add support for keep-alive flag to passive sockets utils/qemu-sockets: Introduce inet socket options controlling TCP
keep-alive

 io/dns-resolver.c   |  21 ++----
 meson.build         |   6 ++
 qapi/sockets.json   |  19 ++++-
 util/qemu-sockets.c | 177 ++++++++++++++++++++++++++++++++++----------
 4 files changed, 165 insertions(+), 58 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v3 1/5 io: Fix partial struct copy in qio_dns_resolver_lookup_sync_inet()

From: Juraj Marcin <[email protected]>

Commit aec21d3175 (qapi: Add InetSocketAddress member keep-alive) introduces the keep-alive flag, but this flag is not copied together with other options in qio_dns_resolver_lookup_sync_inet().

This patch fixes this issue and also prevents future ones by copying the entire structure first and only then overriding a few attributes that need to be different.

Fixes aec21d31756c (qapi: Add InetSocketAddress member keep-alive)
---
 io/dns-resolver.c | 21 +++++----------------
 1 file changed, 5 insertions(+), 16 deletions(-)
io/dns-resolver.c +5 -16
index 53b0e8407a..3712438f82 100644
@@ -111,22 +111,11 @@ static int qio_dns_resolver_lookup_sync_inet(QIODNSResolver *resolver,
                     uaddr, INET6_ADDRSTRLEN, uport, 32,
                     NI_NUMERICHOST | NI_NUMERICSERV);
 
-        newaddr->u.inet = (InetSocketAddress){
-            .host = g_strdup(uaddr),
-            .port = g_strdup(uport),
-            .has_numeric = true,
-            .numeric = true,
-            .has_to = iaddr->has_to,
-            .to = iaddr->to,
-            .has_ipv4 = iaddr->has_ipv4,
-            .ipv4 = iaddr->ipv4,
-            .has_ipv6 = iaddr->has_ipv6,
-            .ipv6 = iaddr->ipv6,
-#ifdef HAVE_IPPROTO_MPTCP
-            .has_mptcp = iaddr->has_mptcp,
-            .mptcp = iaddr->mptcp,
-#endif
-        };
+        newaddr->u.inet = *iaddr;
+        newaddr->u.inet.host = g_strdup(uaddr),
+        newaddr->u.inet.port = g_strdup(uport),
+        newaddr->u.inet.has_numeric = true,
+        newaddr->u.inet.numeric = true,
 
         (*addrs)[i] = newaddr;
     }
Signature · 1 line
2.48.1
Reviewed-by on v3 1/5
#2 lore raw

Re: [PATCH v3 1/5] io: Fix partial struct copy in qio_dns_resolver_lookup_sync_inet()

Quoted 10 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

Commit aec21d3175 (qapi: Add InetSocketAddress member keep-alive) introduces the keep-alive flag, but this flag is not copied together with other options in qio_dns_resolver_lookup_sync_inet().

This patch fixes this issue and also prevents future ones by copying the entire structure first and only then overriding a few attributes that need to be different.

Quoted 5 lines
Fixes aec21d31756c (qapi: Add InetSocketAddress member keep-alive)
---
 io/dns-resolver.c | 21 +++++----------------
 1 file changed, 5 insertions(+), 16 deletions(-)

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

PATCH v3 2/5 util/qemu-sockets: Refactor setting client sockopts into a separate function

In reply to Juraj Marcin

From: Juraj Marcin <[email protected]>

This is done in preparation for enabling the SO_KEEPALIVE support for server sockets and adding settings for more TCP keep-alive socket options.

---
 util/qemu-sockets.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
util/qemu-sockets.c +19 -10
index 77477c1cd5..d15f6aa4b0 100644
@@ -205,6 +205,22 @@ static int try_bind(int socket, InetSocketAddress *saddr, struct addrinfo *e)
 #endif
 }
 
+static int inet_set_sockopts(int sock, InetSocketAddress *saddr, Error **errp)
+{
+    if (saddr->keep_alive) {
+        int keep_alive = 1;
+        int ret = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
+                             &keep_alive, sizeof(keep_alive));
+
+        if (ret < 0) {
+            error_setg_errno(errp, errno,
+                             "Unable to set keep-alive option on socket");
+            return -1;
+        }
+    }
+    return 0;
+}
+
 static int inet_listen_saddr(InetSocketAddress *saddr,
                              int port_offset,
                              int num,
@@ -475,16 +491,9 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
         return sock;
     }
 
-    if (saddr->keep_alive) {
-        int val = 1;
-        int ret = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
-                             &val, sizeof(val));
-
-        if (ret < 0) {
-            error_setg_errno(errp, errno, "Unable to set KEEPALIVE");
-            close(sock);
-            return -1;
-        }
+    if (inet_set_sockopts(sock, saddr, errp)) {
+        close(sock);
+        return -1;
     }
 
     return sock;
Signature · 1 line
2.48.1
#4 lore raw

Re: [PATCH v3 2/5] util/qemu-sockets: Refactor setting client sockopts into a separate function

Quoted 43 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

This is done in preparation for enabling the SO_KEEPALIVE support for server sockets and adding settings for more TCP keep-alive socket options.

---
 util/qemu-sockets.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
util/qemu-sockets.c +16 -1
index 77477c1cd5..d15f6aa4b0 100644
@@ -205,6 +205,22 @@ static int try_bind(int socket, InetSocketAddress *saddr, struct addrinfo *e)
 #endif
 }
 
+static int inet_set_sockopts(int sock, InetSocketAddress *saddr, Error **errp)
+{
+    if (saddr->keep_alive) {
+        int keep_alive = 1;
+        int ret = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
+                             &keep_alive, sizeof(keep_alive));
+
+        if (ret < 0) {
+            error_setg_errno(errp, errno,
+                             "Unable to set keep-alive option on socket");
+            return -1;
+        }
+    }
+    return 0;
+}
+
 static int inet_listen_saddr(InetSocketAddress *saddr,
                              int port_offset,
                              int num,
@@ -475,16 +491,9 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
         return sock;
     }
 
-    if (saddr->keep_alive) {

util/qemu-sockets.c int inet_connect_saddr(InetSocketAddress *saddr, Error **errp) · line 494

Quoted 10 lines
util/qemu-sockets.c +1 -9
-        int val = 1;
-        int ret = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
-                             &val, sizeof(val));
-
-        if (ret < 0) {
-            error_setg_errno(errp, errno, "Unable to set KEEPALIVE");
-            close(sock);
-            return -1;
-        }
+    if (inet_set_sockopts(sock, saddr, errp)) {

Since this returns -1 on error, by convention we check "< 0", reserving positive return values for future non-error scenarios.

Quoted 8 lines
util/qemu-sockets.c +2 -0
+        close(sock);
+        return -1;
     }
 
     return sock;
Signature · 1 line
2.48.1

With 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

PATCH v3 3/5 util/qemu-sockets: Refactor success and failure paths in inet_listen_saddr()

In reply to Juraj Marcin

From: Juraj Marcin <[email protected]>

To get a listening socket, we need to first create a socket, try binding it to a certain port, and lastly starting listening to it. Each of these operations can fail due to various reasons, one of them being that the requested address/port is already in use. In such case, the function tries the same process with a new port number.

This patch refactors the port number loop, so the success path is no longer buried inside the 'if' statements in the middle of the loop. Now, the success path is not nested and ends at the end of the iteration after successful socket creation, binding, and listening. In case any of the operations fails, it either continues to the next iteration (and the next port) or jumps out of the loop to handle the error and exits the function.

---
 util/qemu-sockets.c | 51 ++++++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 24 deletions(-)
util/qemu-sockets.c +27 -24
index d15f6aa4b0..a86964786a 100644
@@ -303,11 +303,20 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
         port_min = inet_getport(e);
         port_max = saddr->has_to ? saddr->to + port_offset : port_min;
         for (p = port_min; p <= port_max; p++) {
+            if (slisten >= 0) {
+                /*
+                 * We have a socket we tried with the previous port. It cannot
+                 * be rebound, we need to close it and create a new one.
+                 */
+                close(slisten);
+                slisten = -1;
+            }
             inet_setport(e, p);
 
             slisten = create_fast_reuse_socket(e);
             if (slisten < 0) {
-                /* First time we expect we might fail to create the socket
+                /*
+                 * First time we expect we might fail to create the socket
                  * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded.
                  * Later iterations should always succeed if first iteration
                  * worked though, so treat that as fatal.
@@ -317,40 +326,38 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
                 } else {
                     error_setg_errno(errp, errno,
                                      "Failed to recreate failed listening socket");
-                    goto listen_failed;
+                    goto fail;
                 }
             }
             socket_created = true;
 
             rc = try_bind(slisten, saddr, e);
             if (rc < 0) {
-                if (errno != EADDRINUSE) {
-                    error_setg_errno(errp, errno, "Failed to bind socket");
-                    goto listen_failed;
+                if (errno == EADDRINUSE) {
+                    /* This port is already used, try the next one */
+                    continue;
                 }
-            } else {
-                if (!listen(slisten, num)) {
-                    goto listen_ok;
-                }
-                if (errno != EADDRINUSE) {
-                    error_setg_errno(errp, errno, "Failed to listen on socket");
-                    goto listen_failed;
+                error_setg_errno(errp, errno, "Failed to bind socket");
+                goto fail;
+            }
+            if (listen(slisten, num)) {
+                if (errno == EADDRINUSE) {
+                    /* This port is already used, try the next one */
+                    continue;
                 }
+                error_setg_errno(errp, errno, "Failed to listen on socket");
+                goto fail;
             }
-            /* Someone else managed to bind to the same port and beat us
-             * to listen on it! Socket semantics does not allow us to
-             * recover from this situation, so we need to recreate the
-             * socket to allow bind attempts for subsequent ports:
-             */
-            close(slisten);
-            slisten = -1;
+            /* We have a listening socket */
+            freeaddrinfo(res);
+            return slisten;
         }
     }
     error_setg_errno(errp, errno,
                      socket_created ?
                      "Failed to find an available port" :
                      "Failed to create a socket");
-listen_failed:
+fail:
     saved_errno = errno;
     if (slisten >= 0) {
         close(slisten);
@@ -358,10 +365,6 @@ listen_failed:
     freeaddrinfo(res);
     errno = saved_errno;
     return -1;
-
-listen_ok:
-    freeaddrinfo(res);
-    return slisten;
 }
 
 #ifdef _WIN32
Signature · 1 line
2.48.1
Reviewed-by on v3 3/5
#6 lore raw

Re: [PATCH v3 3/5] util/qemu-sockets: Refactor success and failure paths in inet_listen_saddr()

Quoted 16 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

To get a listening socket, we need to first create a socket, try binding it to a certain port, and lastly starting listening to it. Each of these operations can fail due to various reasons, one of them being that the requested address/port is already in use. In such case, the function tries the same process with a new port number.

This patch refactors the port number loop, so the success path is no longer buried inside the 'if' statements in the middle of the loop. Now, the success path is not nested and ends at the end of the iteration after successful socket creation, binding, and listening. In case any of the operations fails, it either continues to the next iteration (and the next port) or jumps out of the loop to handle the error and exits the function.

Quoted 4 lines
---
 util/qemu-sockets.c | 51 ++++++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 24 deletions(-)

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

PATCH v3 4/5 util/qemu-sockets: Add support for keep-alive flag to passive sockets

In reply to Juraj Marcin

From: Juraj Marcin <[email protected]>

Commit aec21d3175 (qapi: Add InetSocketAddress member keep-alive) introduces the keep-alive flag, which enables the SO_KEEPALIVE socket option, but only on client-side sockets. However, this option is also useful for server-side sockets, so they can check if a client is still reachable or drop the connection otherwise.

This patch enables the SO_KEEPALIVE socket option on passive server-side sockets if the keep-alive flag is enabled. This socket option is then inherited by active server-side sockets communicating with connected clients.

---
 qapi/sockets.json   | 4 ++--
 util/qemu-sockets.c | 9 +++------
 2 files changed, 5 insertions(+), 8 deletions(-)
qapi/sockets.json +2 -2
index 6a95023315..62797cd027 100644
@@ -56,8 +56,8 @@
 # @ipv6: whether to accept IPv6 addresses, default try both IPv4 and
 #     IPv6
 #
-# @keep-alive: enable keep-alive when connecting to this socket.  Not
-#     supported for passive sockets.  (Since 4.2)
+# @keep-alive: enable keep-alive when connecting to/listening on this socket.
+#     (Since 4.2, not supported for listening sockets until 10.1)
 #
 # @mptcp: enable multi-path TCP.  (Since 6.1)
 #
util/qemu-sockets.c +3 -6
index a86964786a..fed17a1ffb 100644
@@ -236,12 +236,6 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
     int saved_errno = 0;
     bool socket_created = false;
 
-    if (saddr->keep_alive) {
-        error_setg(errp, "keep-alive option is not supported for passive "
-                   "sockets");
-        return -1;
-    }
-
     memset(&ai,0, sizeof(ai));
     ai.ai_flags = AI_PASSIVE;
     if (saddr->has_numeric && saddr->numeric) {
@@ -349,6 +343,9 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
                 goto fail;
             }
             /* We have a listening socket */
+            if (inet_set_sockopts(slisten, saddr, errp)) {
+                goto fail;
+            }
             freeaddrinfo(res);
             return slisten;
         }
Signature · 1 line
2.48.1
Reviewed-by on v3 4/5
#8 lore raw

Re: [PATCH v3 4/5] util/qemu-sockets: Add support for keep-alive flag to passive sockets

Quoted 13 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

Commit aec21d3175 (qapi: Add InetSocketAddress member keep-alive) introduces the keep-alive flag, which enables the SO_KEEPALIVE socket option, but only on client-side sockets. However, this option is also useful for server-side sockets, so they can check if a client is still reachable or drop the connection otherwise.

This patch enables the SO_KEEPALIVE socket option on passive server-side sockets if the keep-alive flag is enabled. This socket option is then inherited by active server-side sockets communicating with connected clients.

Quoted 5 lines
---
 qapi/sockets.json   | 4 ++--
 util/qemu-sockets.c | 9 +++------
 2 files changed, 5 insertions(+), 8 deletions(-)

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

PATCH v3 5/5 utils/qemu-sockets: Introduce inet socket options controlling TCP keep-alive

In reply to Juraj Marcin

From: Juraj Marcin <[email protected]>

With the default TCP stack configuration, it could be even 2 hours before the connection times out due to the other side not being reachable. However, in some cases, the application needs to be aware of a connection issue much sooner.

This is the case, for example, for postcopy live migration. If there is no traffic from the migration destination guest (server-side) to the migration source guest (client-side), the destination keeps waiting for pages indefinitely and does not switch to the postcopy-paused state. This can happen, for example, if the destination QEMU instance is started with the '-S' command line option and the machine is not started yet, or if the machine is idle and produces no new page faults for not-yet-migrated pages.

This patch introduces new inet socket parameters that control count, idle period, and interval of TCP keep-alive packets before the connection is considered broken. These parameters are available on systems where the respective TCP socket options are defined (TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL).

The default value for all is 0, which means the system configuration is used.

---
 meson.build         |  6 ++++
 qapi/sockets.json   | 15 ++++++++
 util/qemu-sockets.c | 88 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+)
meson.build +6 -0
index 41f68d3806..680f47cf42 100644
@@ -2734,6 +2734,12 @@ if linux_io_uring.found()
   config_host_data.set('HAVE_IO_URING_PREP_WRITEV2',
                        cc.has_header_symbol('liburing.h', 'io_uring_prep_writev2'))
 endif
+config_host_data.set('HAVE_TCP_KEEPCNT',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPCNT'))
+config_host_data.set('HAVE_TCP_KEEPIDLE',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPIDLE'))
+config_host_data.set('HAVE_TCP_KEEPINTVL',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPINTVL'))
 
 # has_member
 config_host_data.set('HAVE_SIGEV_NOTIFY_THREAD_ID',
qapi/sockets.json +15 -0
index 62797cd027..bb9d298635 100644
@@ -59,6 +59,18 @@
 # @keep-alive: enable keep-alive when connecting to/listening on this socket.
 #     (Since 4.2, not supported for listening sockets until 10.1)
 #
+# @keep-alive-count: number of keep-alive packets sent before the connection is
+#     closed.  Only supported for TCP sockets on systems where TCP_KEEPCNT
+#     socket option is defined.  (Since 10.1)
+#
+# @keep-alive-idle: time in seconds the connection needs to be idle before
+#     sending a keepalive packet.  Only supported for TCP sockets on systems
+#     where TCP_KEEPIDLE socket option is defined.  (Since 10.1)
+#
+# @keep-alive-interval: time in secods between keep-alive packets.  Only
+#     supported for TCP sockets on systems where TCP_KEEPINTVL is defined.
+#     (Since 10.1)
+#
 # @mptcp: enable multi-path TCP.  (Since 6.1)
 #
 # Since: 1.3
@@ -71,6 +83,9 @@
     '*ipv4': 'bool',
     '*ipv6': 'bool',
     '*keep-alive': 'bool',
+    '*keep-alive-count': { 'type': 'uint32', 'if': 'HAVE_TCP_KEEPCNT' },
+    '*keep-alive-idle': { 'type': 'uint32', 'if': 'HAVE_TCP_KEEPIDLE' },
+    '*keep-alive-interval': { 'type': 'uint32', 'if': 'HAVE_TCP_KEEPINTVL' },
     '*mptcp': { 'type': 'bool', 'if': 'HAVE_IPPROTO_MPTCP' } } }
 
 ##
util/qemu-sockets.c +88 -0

106 lines not on this page · raw message

Signature · 1 line
2.48.1
#10 lore raw

Re: [PATCH v3 5/5] utils/qemu-sockets: Introduce inet socket options controlling TCP keep-alive

Quoted 37 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

With the default TCP stack configuration, it could be even 2 hours before the connection times out due to the other side not being reachable. However, in some cases, the application needs to be aware of a connection issue much sooner.

This is the case, for example, for postcopy live migration. If there is no traffic from the migration destination guest (server-side) to the migration source guest (client-side), the destination keeps waiting for pages indefinitely and does not switch to the postcopy-paused state. This can happen, for example, if the destination QEMU instance is started with the '-S' command line option and the machine is not started yet, or if the machine is idle and produces no new page faults for not-yet-migrated pages.

This patch introduces new inet socket parameters that control count, idle period, and interval of TCP keep-alive packets before the connection is considered broken. These parameters are available on systems where the respective TCP socket options are defined (TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL).

The default value for all is 0, which means the system configuration is used.

---
 meson.build         |  6 ++++
 qapi/sockets.json   | 15 ++++++++
 util/qemu-sockets.c | 88 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+)
meson.build +0 -0
index 41f68d3806..680f47cf42 100644

meson.build if linux_io_uring.found() · line 2742

Quoted 9 lines
meson.build +6 -0
@@ -2734,6 +2734,12 @@ if linux_io_uring.found()
   config_host_data.set('HAVE_IO_URING_PREP_WRITEV2',
                        cc.has_header_symbol('liburing.h', 'io_uring_prep_writev2'))
 endif
+config_host_data.set('HAVE_TCP_KEEPCNT',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPCN'T))
+config_host_data.set('HAVE_TCP_KEEPIDLE',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPIDLE'))
+config_host_data.set('HAVE_TCP_KEEPINTVL',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPINTVL'))

What platforms are you aware of that do NOT have these settings available ? I'm wondering if we can just assume they always exist.

Quoted 10 lines
meson.build +0 -0
 # has_member
 config_host_data.set('HAVE_SIGEV_NOTIFY_THREAD_ID',
qapi/sockets.json +0 -0
index 62797cd027..bb9d298635 100644
@@ -59,6 +59,18 @@
 # @keep-alive: enable keep-alive when connecting to/listening on this socket.
 #     (Since 4.2, not supported for listening sockets until 10.1)

qapi/sockets.json line 70

Quoted 10 lines
qapi/sockets.json +9 -0
 #
+# @keep-alive-count: number of keep-alive packets sent before the connection is
+#     closed.  Only supported for TCP sockets on systems where TCP_KEEPCNT
+#     socket option is defined.  (Since 10.1)
+#
+# @keep-alive-idle: time in seconds the connection needs to be idle before
+#     sending a keepalive packet.  Only supported for TCP sockets on systems
+#     where TCP_KEEPIDLE socket option is defined.  (Since 10.1)
+#
+# @keep-alive-interval: time in secods between keep-alive packets.  Only

Trivial typo s/secods/seconds/

Quoted 7 lines
qapi/sockets.json +3 -0
+#     supported for TCP sockets on systems where TCP_KEEPINTVL is defined.
+#     (Since 10.1)
+#
 # @mptcp: enable multi-path TCP.  (Since 6.1)
 #
 # Since: 1.3

qapi/sockets.json line 91

Quoted 9 lines
qapi/sockets.json +3 -0
@@ -71,6 +83,9 @@
     '*ipv4': 'bool',
     '*ipv6': 'bool',
     '*keep-alive': 'bool',
+    '*keep-alive-count': { 'type': 'uint32', 'if': 'HAVE_TCP_KEEPCNT' },
+    '*keep-alive-idle': { 'type': 'uint32', 'if': 'HAVE_TCP_KEEPIDLE' },
+    '*keep-alive-interval': { 'type': 'uint32', 'if': 'HAVE_TCP_KEEPINTVL' },
     '*mptcp': { 'type': 'bool', 'if': 'HAVE_IPPROTO_MPTCP' } } }
 
 ##

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

Re: [PATCH v3 5/5] utils/qemu-sockets: Introduce inet socket options controlling TCP keep-alive

Quoted 43 lines · Daniel P. Berrangé
Quoted 42 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

With the default TCP stack configuration, it could be even 2 hours before the connection times out due to the other side not being reachable. However, in some cases, the application needs to be aware of a connection issue much sooner.

This is the case, for example, for postcopy live migration. If there is no traffic from the migration destination guest (server-side) to the migration source guest (client-side), the destination keeps waiting for pages indefinitely and does not switch to the postcopy-paused state. This can happen, for example, if the destination QEMU instance is started with the '-S' command line option and the machine is not started yet, or if the machine is idle and produces no new page faults for not-yet-migrated pages.

This patch introduces new inet socket parameters that control count, idle period, and interval of TCP keep-alive packets before the connection is considered broken. These parameters are available on systems where the respective TCP socket options are defined (TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL).

The default value for all is 0, which means the system configuration is used.

---
 meson.build         |  6 ++++
 qapi/sockets.json   | 15 ++++++++
 util/qemu-sockets.c | 88 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+)
meson.build +2 -0
index 41f68d3806..680f47cf42 100644
@@ -2734,6 +2734,12 @@ if linux_io_uring.found()
   config_host_data.set('HAVE_IO_URING_PREP_WRITEV2',
                        cc.has_header_symbol('liburing.h', 'io_uring_prep_writev2'))
 endif
+config_host_data.set('HAVE_TCP_KEEPCNT',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPCN'T))
Quoted 8 lines

meson.build if linux_io_uring.found() · line 2742

Quoted 4 lines
meson.build +4 -0
+config_host_data.set('HAVE_TCP_KEEPIDLE',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPIDLE'))
+config_host_data.set('HAVE_TCP_KEEPINTVL',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPINTVL'))

What platforms are you aware of that do NOT have these settings available ? I'm wondering if we can just assume they always exist.

macOS appears to have these, except that 'TCP_KEEPIDLE' is under a differnt name 'TCP_KEEPALIVE':

https://github.com/apple/darwin-xnu/blob/xnu-4570.1.46/bsd/man/man4/tcp.4#L172

Likewise I see them available in mingw for Wndows builds, with both names

$ grep -r TCP_KEEP /usr/i686-w64-mingw32/sys-root/mingw/include/
/usr/i686-w64-mingw32/sys-root/mingw/include/ws2ipdef.h:#define TCP_KEEPALIVE 3
/usr/i686-w64-mingw32/sys-root/mingw/include/ws2ipdef.h:#define TCP_KEEPCNT 16
/usr/i686-w64-mingw32/sys-root/mingw/include/ws2ipdef.h:#define TCP_KEEPIDLE TCP_KEEPALIVE
/usr/i686-w64-mingw32/sys-root/mingw/include/ws2ipdef.h:#define TCP_KEEPINTVL 17

but your patch wouldn't enable it because it checks netinet/tcp.h

AFAICT, the only platform that matters to QEMU that seems to miss this is OpenBSD

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

Re: [PATCH v3 5/5] utils/qemu-sockets: Introduce inet socket options controlling TCP keep-alive

Hi Daniel

Quoted 67 lines · Daniel P. Berrangé
Quoted 43 lines · Daniel P. Berrangé
Quoted 42 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

With the default TCP stack configuration, it could be even 2 hours before the connection times out due to the other side not being reachable. However, in some cases, the application needs to be aware of a connection issue much sooner.

This is the case, for example, for postcopy live migration. If there is no traffic from the migration destination guest (server-side) to the migration source guest (client-side), the destination keeps waiting for pages indefinitely and does not switch to the postcopy-paused state. This can happen, for example, if the destination QEMU instance is started with the '-S' command line option and the machine is not started yet, or if the machine is idle and produces no new page faults for not-yet-migrated pages.

This patch introduces new inet socket parameters that control count, idle period, and interval of TCP keep-alive packets before the connection is considered broken. These parameters are available on systems where the respective TCP socket options are defined (TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL).

The default value for all is 0, which means the system configuration is used.

---
 meson.build         |  6 ++++
 qapi/sockets.json   | 15 ++++++++
 util/qemu-sockets.c | 88 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+)
meson.build +2 -0
index 41f68d3806..680f47cf42 100644
@@ -2734,6 +2734,12 @@ if linux_io_uring.found()
   config_host_data.set('HAVE_IO_URING_PREP_WRITEV2',
                        cc.has_header_symbol('liburing.h', 'io_uring_prep_writev2'))
 endif
+config_host_data.set('HAVE_TCP_KEEPCNT',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPCN'T))
Quoted 8 lines

meson.build if linux_io_uring.found() · line 2742

Quoted 4 lines
meson.build +4 -0
+config_host_data.set('HAVE_TCP_KEEPIDLE',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPIDLE'))
+config_host_data.set('HAVE_TCP_KEEPINTVL',
+                     cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPINTVL'))

What platforms are you aware of that do NOT have these settings available ? I'm wondering if we can just assume they always exist.

macOS appears to have these, except that 'TCP_KEEPIDLE' is under a differnt name 'TCP_KEEPALIVE':

https://github.com/apple/darwin-xnu/blob/xnu-4570.1.46/bsd/man/man4/tcp.4#L172

Likewise I see them available in mingw for Wndows builds, with both names

$ grep -r TCP_KEEP /usr/i686-w64-mingw32/sys-root/mingw/include/
/usr/i686-w64-mingw32/sys-root/mingw/include/ws2ipdef.h:#define TCP_KEEPALIVE 3
/usr/i686-w64-mingw32/sys-root/mingw/include/ws2ipdef.h:#define TCP_KEEPCNT 16
/usr/i686-w64-mingw32/sys-root/mingw/include/ws2ipdef.h:#define TCP_KEEPIDLE TCP_KEEPALIVE
/usr/i686-w64-mingw32/sys-root/mingw/include/ws2ipdef.h:#define TCP_KEEPINTVL 17
Quoted 5 lines

but your patch wouldn't enable it because it checks netinet/tcp.h

AFAICT, the only platform that matters to QEMU that seems to miss this is OpenBSD

Yes, I couldn't find them in the OpenBSD man-pages. Other BSD variants support all of them. I will mention it explicitly in the QAPI docs, that OpenBSD is not supported.

I will also fix it for Windows and Darwin, and submit an updated series.

Thank you!

Best regards,

Juraj Marcin

Quoted 8 lines

With 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 :|