Skip to contentSkip to conversation

qapi: InitSocketAddress: add keepalive option

PATCH In review Vladimir Sementsov-Ogievskiy 2 participants 3 messages Jun 10, 2019 – Jun 14, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 qapi: InitSocketAddress: add keepalive option

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

---

v2: [by Markus's comments]
- improve commit message
- s/keepalive/keep-alive
- update inet_parse()

 qapi/sockets.json   |  5 ++++-
 util/qemu-sockets.c | 22 ++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)
qapi/sockets.json +4 -1
index fc81d8d5e8..13a2627e1d 100644
@@ -53,6 +53,8 @@
 #
 # @ipv6: whether to accept IPv6 addresses, default try both IPv4 and IPv6
 #
+# @keep-alive: enable keep-alive when connecting to this socket (Since 4.1)
+#
 # Since: 1.3
 ##
 { 'struct': 'InetSocketAddress',
@@ -61,7 +63,8 @@
     '*numeric':  'bool',
     '*to': 'uint16',
     '*ipv4': 'bool',
-    '*ipv6': 'bool' } }
+    '*ipv6': 'bool',
+    '*keep-alive': 'bool' } }
 
 ##
 # @UnixSocketAddress:
util/qemu-sockets.c +22 -0
index 8850a280a8..9c842c4a93 100644
@@ -457,6 +457,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 +665,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

You misspelled InetSocketAddress and keep-alive in the subject. Suggest

qapi: Add InetSocketAddress member keep-alive

Quoted 21 lines · Vladimir Sementsov-Ogievskiy

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

---

v2: [by Markus's comments]
- improve commit message
- s/keepalive/keep-alive
- update inet_parse()

 qapi/sockets.json   |  5 ++++-
 util/qemu-sockets.c | 22 ++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)
qapi/sockets.json +0 -0
index fc81d8d5e8..13a2627e1d 100644

qapi/sockets.json line 60

Quoted 8 lines
qapi/sockets.json +2 -0
@@ -53,6 +53,8 @@
 #
 # @ipv6: whether to accept IPv6 addresses, default try both IPv4 and IPv6
 #
+# @keep-alive: enable keep-alive when connecting to this socket (Since 4.1)
+#
 # Since: 1.3
 ##
 { 'struct': 'InetSocketAddress',

Please document that @keep-alive is silently ignored for passive sockets.

Even better would be rejecting it then, of course.

Quoted 35 lines
qapi/sockets.json +2 -1
@@ -61,7 +63,8 @@
     '*numeric':  'bool',
     '*to': 'uint16',
     '*ipv4': 'bool',
-    '*ipv6': 'bool' } }
+    '*ipv6': 'bool',
+    '*keep-alive': 'bool' } }
 
 ##
 # @UnixSocketAddress:
util/qemu-sockets.c +13 -0
index 8850a280a8..9c842c4a93 100644
@@ -457,6 +457,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;
 }
 

util/qemu-sockets.c int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) · line 670

Quoted 6 lines
util/qemu-sockets.c +3 -0
@@ -652,6 +665,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="),

Shouldn't you use strlen(",keep-alive")?

Quoted 8 lines
util/qemu-sockets.c +6 -0
+                            &addr->keep_alive, errp) < 0)
+        {
+            return -1;
+        }
+        addr->has_keep_alive = true;
+    }
     return 0;
 }
#2 lore raw
Quoted 3 lines · Markus Armbruster

You misspelled InetSocketAddress and keep-alive in the subject. Suggest

qapi: Add InetSocketAddress member keep-alive

Ohh, I feel stupid

Quoted 36 lines
Quoted 21 lines · Vladimir Sementsov-Ogievskiy

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

---

v2: [by Markus's comments]
- improve commit message
- s/keepalive/keep-alive
- update inet_parse()

  qapi/sockets.json   |  5 ++++-
  util/qemu-sockets.c | 22 ++++++++++++++++++++++
  2 files changed, 26 insertions(+), 1 deletion(-)
qapi/sockets.json +0 -0
index fc81d8d5e8..13a2627e1d 100644

qapi/sockets.json line 60

Quoted 8 lines
qapi/sockets.json +2 -0
@@ -53,6 +53,8 @@
  #
  # @ipv6: whether to accept IPv6 addresses, default try both IPv4 and IPv6
  #
+# @keep-alive: enable keep-alive when connecting to this socket (Since 4.1)
+#
  # Since: 1.3
  ##
  { 'struct': 'InetSocketAddress',

Please document that @keep-alive is silently ignored for passive sockets.

Quoted 1 line

Even better would be rejecting it then, of course.

Ok

Quoted 35 lines
Quoted 35 lines
qapi/sockets.json +2 -1
@@ -61,7 +63,8 @@
      '*numeric':  'bool',
      '*to': 'uint16',
      '*ipv4': 'bool',
-    '*ipv6': 'bool' } }
+    '*ipv6': 'bool',
+    '*keep-alive': 'bool' } }
 
  ##
  # @UnixSocketAddress:
util/qemu-sockets.c +13 -0
index 8850a280a8..9c842c4a93 100644
@@ -457,6 +457,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;
  }
 
Quoted 9 lines

util/qemu-sockets.c int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) · line 670

Quoted 7 lines
util/qemu-sockets.c +3 -0
@@ -652,6 +665,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="),

Shouldn't you use strlen(",keep-alive")?

Hmm yes.

Quoted 9 lines

util/qemu-sockets.c int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) · line 678

Quoted 9 lines
util/qemu-sockets.c +6 -0
+                            &addr->keep_alive, errp) < 0)
+        {
+            return -1;
+        }
+        addr->has_keep_alive = true;
+    }
      return 0;
  }

Thanks for reviewing, I'll resend soon.

Signature · 2 lines
Best regards,
Vladimir