In the case that a client disables jsonrpc probes the client would fail
to detect if the connection to the server has dropped. To workaround
such case TCP keepalive is enabled.

Signed-off-by: Michael Santana <[email protected]>
---
 lib/socket-util.c | 13 +++++++++++++
 lib/socket-util.h |  1 +
 lib/stream-fd.c   |  1 +
 lib/stream-ssl.c  |  2 ++
 lib/stream-tcp.c  |  1 +
 5 files changed, 18 insertions(+)

diff --git a/lib/socket-util.c b/lib/socket-util.c
index 4f1ffecf5..dbf35f9bb 100644
--- a/lib/socket-util.c
+++ b/lib/socket-util.c
@@ -114,6 +114,19 @@ setsockopt_tcp_nodelay(int fd)
     }
 }
 
+void
+setsockopt_tcp_keepalive(int fd)
+{
+    int on = 1;
+    int retval;
+
+    retval = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof on);
+    if (retval) {
+        retval = sock_errno();
+        VLOG_ERR("setsockopt(SO_KEEPALIVE): %s", sock_strerror(retval));
+    }
+}
+
 /* Sets the DSCP value of socket 'fd' to 'dscp', which must be 63 or less.
  * 'family' must indicate the socket's address family (AF_INET or AF_INET6, to
  * do anything useful). */
diff --git a/lib/socket-util.h b/lib/socket-util.h
index 9ccb7d4cc..f6cf00a32 100644
--- a/lib/socket-util.h
+++ b/lib/socket-util.h
@@ -33,6 +33,7 @@ struct ds;
 int set_nonblocking(int fd);
 void xset_nonblocking(int fd);
 void setsockopt_tcp_nodelay(int fd);
+void setsockopt_tcp_keepalive(int fd);
 int set_dscp(int fd, int family, uint8_t dscp);
 
 bool addr_is_ipv6(const char *host_name);
diff --git a/lib/stream-fd.c b/lib/stream-fd.c
index 46ee7ae27..df609579c 100644
--- a/lib/stream-fd.c
+++ b/lib/stream-fd.c
@@ -93,6 +93,7 @@ fd_connect(struct stream *stream)
     int retval = check_connection_completion(s->fd);
     if (retval == 0 && s->fd_type == AF_INET) {
         setsockopt_tcp_nodelay(s->fd);
+        setsockopt_tcp_keepalive(s->fd);
     }
     return retval;
 }
diff --git a/lib/stream-ssl.c b/lib/stream-ssl.c
index 0ea3f2c08..0a47208da 100644
--- a/lib/stream-ssl.c
+++ b/lib/stream-ssl.c
@@ -267,6 +267,7 @@ new_ssl_stream(char *name, char *server_name, int fd, enum 
session_type type,
      */
     if (state == STATE_SSL_CONNECTING) {
         setsockopt_tcp_nodelay(fd);
+        setsockopt_tcp_keepalive(fd);
     }
 
     /* Create and configure OpenSSL stream. */
@@ -521,6 +522,7 @@ ssl_connect(struct stream *stream)
         }
         sslv->state = STATE_SSL_CONNECTING;
         setsockopt_tcp_nodelay(sslv->fd);
+        setsockopt_tcp_keepalive(sslv->fd);
         /* Fall through. */
 
     case STATE_SSL_CONNECTING:
diff --git a/lib/stream-tcp.c b/lib/stream-tcp.c
index e8dc2bfaa..48443b9be 100644
--- a/lib/stream-tcp.c
+++ b/lib/stream-tcp.c
@@ -43,6 +43,7 @@ new_tcp_stream(char *name, int fd, int connect_status, struct 
stream **streamp)
 {
     if (connect_status == 0) {
         setsockopt_tcp_nodelay(fd);
+        setsockopt_tcp_keepalive(fd);
     }
 
     return new_fd_stream(name, fd, connect_status, AF_INET, streamp);
-- 
2.31.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to