Skip to contentSkip to conversation

qapi: Add InetSocketAddress member keep-alive

PATCH In review Vladimir Sementsov-Ogievskiy 4 participants 6 messages Jul 25, 2019 – Jul 29, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 qapi: Add InetSocketAddress member keep-alive

It's needed to provide keepalive for nbd client to track server availability.

---

Notes:
v4; [by Markus's comments]

- use "passive socket" term
- move check for not enabled keep_alive to inet_listen_saddr()

v3: [by Markus's comments]

- Fix commit subject
- Add comment to qapi and restrict server-side connections
- Fix s/"keep-alive="/",keep-alive"/

 qapi/sockets.json   |  6 +++++-
 util/qemu-sockets.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
qapi/sockets.json +5 -1
index fc81d8d5e8..32375f3a36 100644
@@ -53,6 +53,9 @@
 #
 # @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)
+#
 # Since: 1.3
 ##
 { 'struct': 'InetSocketAddress',
@@ -61,7 +64,8 @@
     '*numeric':  'bool',
     '*to': 'uint16',
     '*ipv4': 'bool',
-    '*ipv6': 'bool' } }
+    '*ipv6': 'bool',
+    '*keep-alive': 'bool' } }
 
 ##
 # @UnixSocketAddress:
util/qemu-sockets.c +28 -0
index a5092dbd12..35d2b7f773 100644
@@ -219,6 +219,12 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
     bool socket_created = false;
     Error *err = NULL;
 
+    if (saddr->keep_alive) {
+        error_setg(errp, "keep-alive options is not supported for passive "
+                   "sockets");
+        return -1;
+    }
+
     memset(&ai,0, sizeof(ai));
     ai.ai_flags = AI_PASSIVE;
     if (saddr->has_numeric && saddr->numeric) {
@@ -458,6 +464,19 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
     }
 
     freeaddrinfo(res);
+
+    if (saddr->keep_alive) {
+        int val = 1;
+        int ret = qemu_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;
+        }
+    }
+
     return sock;
 }
 
@@ -653,6 +672,15 @@ int inet_parse(InetSocketAddress *addr, const char *str, Error **errp)
         }
         addr->has_ipv6 = true;
     }
+    begin = strstr(optstr, ",keep-alive");
+    if (begin) {
+        if (inet_parse_flag("keep-alive", begin + strlen(",keep-alive"),
+                            &addr->keep_alive, errp) < 0)
+        {
+            return -1;
+        }
+        addr->has_keep_alive = true;
+    }
     return 0;
 }
 
Signature · 1 line
2.18.0
#2 lore raw
Quoted 8 lines · Markus Armbruster
Quoted 4 lines · Vladimir Sementsov-Ogievskiy

It's needed to provide keepalive for nbd client to track server availability.

It looks like this could go in any number of ways (Markus since it touches qapi, Dan since it touches sockets, or me since it was written for use with NBD); but unless anyone complains, I'm happy to queue this through my NBD tree for 4.2.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#3 lore raw
Quoted 6 lines · Eric Blake
Quoted 5 lines · Markus Armbruster
Quoted 3 lines · Vladimir Sementsov-Ogievskiy

It's needed to provide keepalive for nbd client to track server availability.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

It looks like this could go in any number of ways (Markus since it touches qapi, Dan since it touches sockets, or me since it was written for use with NBD); but unless anyone complains, I'm happy to queue this through my NBD tree for 4.2.

I'm fine with you queuing it, since I have nothing pending myself to bundle it up 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 :|
#4 lore raw
In reply to Eric Blake
Quoted 6 lines · Eric Blake
Quoted 5 lines · Markus Armbruster
Quoted 3 lines · Vladimir Sementsov-Ogievskiy

It's needed to provide keepalive for nbd client to track server availability.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

It looks like this could go in any number of ways (Markus since it touches qapi, Dan since it touches sockets, or me since it was written for use with NBD); but unless anyone complains, I'm happy to queue this through my NBD tree for 4.2.

Go right ahead.

#5 lore raw
In reply to Vladimir Sementsov-Ogievskiy

util/qemu-sockets.c static int inet_listen_saddr(InetSocketAddress *saddr, · line 223

Quoted 13 lines · Vladimir Sementsov-Ogievskiy

It's needed to provide keepalive for nbd client to track server availability.

---

+++ b/util/qemu-sockets.c

util/qemu-sockets.c +2 -0
@@ -219,6 +219,12 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
     bool socket_created = false;
     Error *err = NULL;
 
+    if (saddr->keep_alive) {
+        error_setg(errp, "keep-alive options is not supported for passive "

option

(will fix as part of staging)

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