Skip to contentSkip to conversation

qapi: Add InetSocketAddress member keep-alive

PATCH Superseded Vladimir Sementsov-Ogievskiy 3 participants 4 messages Jun 14, 2019 – Jul 24, 2019

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

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

---

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..c44af481a1 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 server-side connections. (Since 4.1)
+#
 # 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 8850a280a8..813063761b 100644
@@ -438,6 +438,12 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
     struct addrinfo *res, *e;
     int sock = -1;
 
+    if (saddr->keep_alive) {
+        error_setg(errp, "keep-alive options is not supported for server-side "
+                   "connection");
+        return -1;
+    }
+
     res = inet_parse_connect_saddr(saddr, errp);
     if (!res) {
         return -1;
@@ -457,6 +463,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;
 }
 
@@ -652,6 +671,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
#1 lore raw

I apologize for dragging my feet on this review.

Quoted 22 lines · Vladimir Sementsov-Ogievskiy

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

---

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 +0 -0
index fc81d8d5e8..c44af481a1 100644

qapi/sockets.json line 58

Quoted 6 lines
qapi/sockets.json +3 -0
@@ -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 server-side connections. (Since 4.1)
+#

Is "server-side connection" is an established term?

For what it's worth, "passive socket" is, see listen(2).

Quoted 20 lines
qapi/sockets.json +2 -1
 # 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 +0 -0
index 8850a280a8..813063761b 100644
@@ -438,6 +438,12 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
     struct addrinfo *res, *e;
     int sock = -1;

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

Quoted 10 lines
util/qemu-sockets.c +6 -0
 
+    if (saddr->keep_alive) {
+        error_setg(errp, "keep-alive options is not supported for server-side "
+                   "connection");
+        return -1;
+    }
+
     res = inet_parse_connect_saddr(saddr, errp);
     if (!res) {
         return -1;

I'm afraid you added this to the wrong function; ...

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

Quoted 6 lines
util/qemu-sockets.c +2 -0
@@ -457,6 +463,19 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
     }
 
     freeaddrinfo(res);
+
+    if (saddr->keep_alive) {

... it renders this code unreachable.

I guess the "not supported for passive sockets" check should go into inet_listen_saddr() instead.

Quoted 29 lines
util/qemu-sockets.c +20 -0
+        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;
 }
 
@@ -652,6 +671,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;
 }
#2 lore raw
Quoted 11 lines · Markus Armbruster

I apologize for dragging my feet on this review.

Quoted 6 lines · Vladimir Sementsov-Ogievskiy

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

---

Quoted 7 lines

qapi/sockets.json line 57

Quoted 7 lines

+++ b/qapi/sockets.json

qapi/sockets.json +2 -0
@@ -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 server-side connections. (Since 4.1)

It looks like this missed 4.1. Are you planning on sending v4, to address

Quoted 42 lines

qapi/sockets.json line 58

Quoted 1 line
qapi/sockets.json +1 -0
+#

Is "server-side connection" is an established term?

For what it's worth, "passive socket" is, see listen(2).

Quoted 20 lines
qapi/sockets.json +2 -1
 # 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 +0 -0
index 8850a280a8..813063761b 100644
@@ -438,6 +438,12 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
     struct addrinfo *res, *e;
     int sock = -1;

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

Quoted 10 lines
util/qemu-sockets.c +6 -0
 
+    if (saddr->keep_alive) {
+        error_setg(errp, "keep-alive options is not supported for server-side "
+                   "connection");
+        return -1;
+    }
+
     res = inet_parse_connect_saddr(saddr, errp);
     if (!res) {
         return -1;

I'm afraid you added this to the wrong function; ...

Quoted 3 lines
util/qemu-sockets.c +0 -0
@@ -457,6 +463,19 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
     }
 
Quoted 8 lines

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

Quoted 3 lines
util/qemu-sockets.c +2 -0
     freeaddrinfo(res);
+
+    if (saddr->keep_alive) {

... it renders this code unreachable.

I guess the "not supported for passive sockets" check should go into inet_listen_saddr() instead.

this comment?

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

I apologize for dragging my feet on this review.

Quoted 6 lines · Vladimir Sementsov-Ogievskiy

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

---

Quoted 7 lines

qapi/sockets.json line 57

Quoted 7 lines

+++ b/qapi/sockets.json

qapi/sockets.json +2 -0
@@ -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 server-side connections. (Since 4.1)

It looks like this missed 4.1. Are you planning on sending v4, to address

Quoted 48 lines

qapi/sockets.json line 58

Quoted 1 line
qapi/sockets.json +1 -0
+#

Is "server-side connection" is an established term?

For what it's worth, "passive socket" is, see listen(2).

Quoted 20 lines
qapi/sockets.json +2 -1
  # 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 +0 -0
index 8850a280a8..813063761b 100644
@@ -438,6 +438,12 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
      struct addrinfo *res, *e;
      int sock = -1;

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

Quoted 10 lines
util/qemu-sockets.c +6 -0
 
+    if (saddr->keep_alive) {
+        error_setg(errp, "keep-alive options is not supported for server-side "
+                   "connection");
+        return -1;
+    }
+
      res = inet_parse_connect_saddr(saddr, errp);
      if (!res) {
          return -1;

I'm afraid you added this to the wrong function; ...

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

Quoted 6 lines
util/qemu-sockets.c +2 -0
@@ -457,6 +463,19 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
      }
 
      freeaddrinfo(res);
+
+    if (saddr->keep_alive) {

... it renders this code unreachable.

Quoted 5 lines
Quoted 2 lines

I guess the "not supported for passive sockets" check should go into inet_listen_saddr() instead.

this comment?

Sorry for big delay, will resend for 4.2 soon.

Signature · 2 lines
Best regards,
Vladimir