[syslog-ng] [PATCH (3.4)] afsocket: Implement tcp_keep_alive_time()

Gergely Nagy algernon at balabit.hu
Thu Aug 16 17:31:17 CEST 2012


With the new tcp_keep_alive_time() option one can control on a
per-source basis the time after which a TCP connection will start
sending TCP keepalive probes (assuming so-keepalive(yes)).

While it is accepted for all stream sources, it only makes sense for
TCP-based streams.

By default, it's value is 0, which means using the kernel default (2
hours).

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 modules/afsocket/afsocket-grammar.ym |    2 ++
 modules/afsocket/afsocket-parser.c   |    1 +
 modules/afsocket/afsocket.c          |   11 +++++++++++
 modules/afsocket/afsocket.h          |    2 ++
 4 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/modules/afsocket/afsocket-grammar.ym b/modules/afsocket/afsocket-grammar.ym
index 7970ca9..48b0526 100644
--- a/modules/afsocket/afsocket-grammar.ym
+++ b/modules/afsocket/afsocket-grammar.ym
@@ -83,6 +83,7 @@ TLSContext *last_tls_context;
 %token KW_SO_SNDBUF
 %token KW_SO_RCVBUF
 %token KW_SO_KEEPALIVE
+%token KW_TCP_KEEP_ALIVE_TIME
 %token KW_SPOOF_SOURCE
 
 %token KW_KEEP_ALIVE
@@ -252,6 +253,7 @@ source_afinet_tcp_option
 
 source_afsocket_stream_params
 	: KW_KEEP_ALIVE '(' yesno ')'		{ afsocket_sd_set_keep_alive(last_driver, $3); }
+        | KW_TCP_KEEP_ALIVE_TIME '(' LL_NUMBER ')' { afsocket_sd_set_keep_alive_time(last_driver, $3); }
 	| KW_MAX_CONNECTIONS '(' LL_NUMBER ')'	{ afsocket_sd_set_max_connections(last_driver, $3); }
 	;
 
diff --git a/modules/afsocket/afsocket-parser.c b/modules/afsocket/afsocket-parser.c
index 44671ca..bd8f5d6 100644
--- a/modules/afsocket/afsocket-parser.c
+++ b/modules/afsocket/afsocket-parser.c
@@ -64,6 +64,7 @@ static CfgLexerKeyword afsocket_keywords[] = {
   { "so_sndbuf",          KW_SO_SNDBUF },
   { "so_keepalive",       KW_SO_KEEPALIVE },
   { "tcp_keep_alive",     KW_SO_KEEPALIVE, 0, KWS_OBSOLETE, "so_keepalive" },
+  { "tcp_keep_alive_time",KW_TCP_KEEP_ALIVE_TIME },
   { "spoof_source",       KW_SPOOF_SOURCE },
   { "transport",          KW_TRANSPORT },
   { "max_connections",    KW_MAX_CONNECTIONS },
diff --git a/modules/afsocket/afsocket.c b/modules/afsocket/afsocket.c
index a7cc7bc..4a71b11 100644
--- a/modules/afsocket/afsocket.c
+++ b/modules/afsocket/afsocket.c
@@ -43,6 +43,7 @@
 #include <arpa/inet.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <netinet/tcp.h>
 
 #if ENABLE_TCP_WRAPPER
 #include <tcpd.h>
@@ -79,6 +80,8 @@ afsocket_setup_socket(gint fd, SocketOptions *sock_options, AFSocketDirection di
         setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &sock_options->broadcast, sizeof(sock_options->broadcast));
     }
   setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &sock_options->keepalive, sizeof(sock_options->keepalive));
+  if (sock_options->keepalive_time > 0)
+    setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &sock_options->keepalive_time, sizeof(sock_options->keepalive_time));
   return TRUE;
 }
 
@@ -386,6 +389,14 @@ afsocket_sd_set_keep_alive(LogDriver *s, gint enable)
 }
 
 void
+afsocket_sd_set_keep_alive_time(LogDriver *s, gint timeout)
+{
+  AFSocketSourceDriver *self = (AFSocketSourceDriver *) s;
+
+  self->keepalive_timeout = timeout;
+}
+
+void
 afsocket_sd_set_max_connections(LogDriver *s, gint max_connections)
 {
   AFSocketSourceDriver *self = (AFSocketSourceDriver *) s;
diff --git a/modules/afsocket/afsocket.h b/modules/afsocket/afsocket.h
index 392e249..fd11d28 100644
--- a/modules/afsocket/afsocket.h
+++ b/modules/afsocket/afsocket.h
@@ -58,6 +58,7 @@ typedef struct _SocketOptions
   gint rcvbuf;
   gint broadcast;
   gint keepalive;
+  gint keepalive_time;
 } SocketOptions;
 
 gboolean afsocket_setup_socket(gint fd, SocketOptions *sock_options, AFSocketDirection dir);
@@ -78,6 +79,7 @@ struct _AFSocketSourceDriver
   gint max_connections;
   gint num_connections;
   gint listen_backlog;
+  gint keepalive_timeout;
   GList *connections;
   SocketOptions *sock_options_ptr;
 
-- 
1.7.9




More information about the syslog-ng mailing list