[master] 5763353 Move the probing of system default tcp-keepalive parameters to a separate source file and open (and close) a socket specifically for that purpose, so we can do the right thing with parameter defaults.

Poul-Henning Kamp phk at FreeBSD.org
Tue Jan 7 20:01:39 CET 2014


commit 57633531456febf60663562b938f3ff7a40f59d3
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Jan 7 19:00:37 2014 +0000

    Move the probing of system default tcp-keepalive parameters
    to a separate source file and open (and close) a socket specifically
    for that purpose, so we can do the right thing with parameter
    defaults.

diff --git a/bin/varnishd/Makefile.am b/bin/varnishd/Makefile.am
index 6980404..9d281f1 100644
--- a/bin/varnishd/Makefile.am
+++ b/bin/varnishd/Makefile.am
@@ -64,6 +64,7 @@ varnishd_SOURCES = \
 	mgt/mgt_param.c \
 	mgt/mgt_param_tbl.c \
 	mgt/mgt_param_bits.c \
+	mgt/mgt_param_tcp.c \
 	mgt/mgt_param_tweak.c \
 	mgt/mgt_pool.c \
 	mgt/mgt_sandbox.c \
diff --git a/bin/varnishd/cache/cache_acceptor.c b/bin/varnishd/cache/cache_acceptor.c
index 6a601a8..e56750f 100644
--- a/bin/varnishd/cache/cache_acceptor.c
+++ b/bin/varnishd/cache/cache_acceptor.c
@@ -368,26 +368,11 @@ VCA_SetupSess(struct worker *wrk, struct sess *sp)
 
 /*--------------------------------------------------------------------*/
 
-#ifdef HAVE_TCP_KEEP
-static void
-vca_tcp_keep_probe(int sock, int nam, volatile unsigned *dst)
-{
-	int i, x;
-	socklen_t l;
-
-	l = sizeof x;
-	i = getsockopt(sock, IPPROTO_TCP, nam, &x, &l);
-	if (i == 0 && x < *dst)
-		*dst = x;
-}
-#endif
-
 static void *
 vca_acct(void *arg)
 {
 	struct listen_sock *ls;
 	double t0, now;
-	unsigned u;
 	int i;
 
 	THR_SetName("cache-acceptor");
@@ -399,18 +384,6 @@ vca_acct(void *arg)
 		if (ls->sock < 0)
 			continue;
 		AZ(listen(ls->sock, cache_param->listen_depth));
-#ifdef HAVE_TCP_KEEP
-		u = (unsigned)round(cache_param->tcp_keepalive_time);
-		vca_tcp_keep_probe(ls->sock, TCP_KEEPIDLE, &u);
-		cache_param->tcp_keepalive_time = u;
-
-		vca_tcp_keep_probe(ls->sock,
-		    TCP_KEEPCNT, &cache_param->tcp_keepalive_probes);
-
-		u = (unsigned)round(cache_param->tcp_keepalive_intvl);
-		vca_tcp_keep_probe(ls->sock, TCP_KEEPINTVL, &u);
-		cache_param->tcp_keepalive_intvl = u;
-#endif
 		vca_tcp_opt_set(ls->sock, 1);
 		if (cache_param->accept_filter) {
 			i = VTCP_filter_http(ls->sock);
diff --git a/bin/varnishd/cache/cache_req_fsm.c b/bin/varnishd/cache/cache_req_fsm.c
index c9defc8..fdf99db 100644
--- a/bin/varnishd/cache/cache_req_fsm.c
+++ b/bin/varnishd/cache/cache_req_fsm.c
@@ -648,7 +648,6 @@ cnt_restart(struct worker *wrk, struct req *req)
 		owid = req->vsl->wid & VSL_IDENTMASK;
 		req->vsl->wid = wid | VSL_CLIENTMARKER;
 		VSLb(req->vsl, SLT_Begin, "req %u restart", owid);
-		
 		req->err_code = 0;
 		req->req_step = R_STP_RECV;
 	}
diff --git a/bin/varnishd/mgt/mgt.h b/bin/varnishd/mgt/mgt.h
index 68efbeb..ecdc6d8 100644
--- a/bin/varnishd/mgt/mgt.h
+++ b/bin/varnishd/mgt/mgt.h
@@ -33,6 +33,7 @@
 #include "common/common.h"
 
 struct cli;
+struct parspec;
 
 extern struct vev_base	*mgt_evb;
 extern unsigned		d_flag;
@@ -79,8 +80,12 @@ void MCF_SetMaximum(const char *param, const char *def);
 void MCF_ParamSet(struct cli *, const char *param, const char *val);
 void MCF_ParamProtect(struct cli *, const char *arg);
 void MCF_DumpRstParam(void);
+void MCF_AddParams(struct parspec *ps);
 extern struct params mgt_param;
 
+/* mgt_param_tcp.c */
+void MCF_TcpParams(void);
+
 /* mgt_sandbox.c */
 enum sandbox_e {
 	SANDBOX_VCC = 1,
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index cd007b1..7da3d88 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -348,6 +348,8 @@ init_params(struct cli *cli)
 
 	MCF_CollectParams();
 
+	MCF_TcpParams();
+
 	/* If we have nobody/nogroup, use them as defaults */
 	if (getpwnam("nobody") != NULL)
 		MCF_SetDefault("user", "nobody");
diff --git a/bin/varnishd/mgt/mgt_param.c b/bin/varnishd/mgt/mgt_param.c
index 8294332..621129d 100644
--- a/bin/varnishd/mgt/mgt_param.c
+++ b/bin/varnishd/mgt/mgt_param.c
@@ -358,7 +358,7 @@ mcf_parspec_cmp(const void *a, const void *b)
 	return (strcmp((*pa)->name, (*pb)->name));
 }
 
-static void
+void
 MCF_AddParams(struct parspec *ps)
 {
 	struct parspec *pp;
diff --git a/bin/varnishd/mgt/mgt_param_tbl.c b/bin/varnishd/mgt/mgt_param_tbl.c
index ecc36f7..de8d1d8 100644
--- a/bin/varnishd/mgt/mgt_param_tbl.c
+++ b/bin/varnishd/mgt/mgt_param_tbl.c
@@ -215,31 +215,6 @@ struct parspec mgt_parspec[] = {
 		"See setsockopt(2) under SO_SNDTIMEO for more information.",
 		DELAYED_EFFECT,
 		"60", "seconds" },
-#ifdef HAVE_TCP_KEEP
-	{ "tcp_keepalive_time", tweak_timeout, &mgt_param.tcp_keepalive_time,
-		"1", "7200",
-		"The number of seconds a connection needs to be idle before "
-		"TCP begins sending out keep-alive probes. Note that this "
-		"setting will only take effect when it is less than the "
-		"system default.",
-		EXPERIMENTAL,
-		"600", "seconds" },
-	{ "tcp_keepalive_probes", tweak_uint, &mgt_param.tcp_keepalive_probes,
-		"1", "100",
-		"The maximum number of TCP keep-alive probes to send before "
-		"giving up and killing the connection if no response is "
-		"obtained from the other end. Note that this setting will "
-		"only take effect when it is less than the system default.",
-		EXPERIMENTAL,
-		"5", "probes" },
-	{ "tcp_keepalive_intvl", tweak_timeout, &mgt_param.tcp_keepalive_intvl,
-		"1", "100",
-		"The number of seconds between TCP keep-alive probes. Note "
-		"that this setting will only take effect when it is less than"
-		"the system default.",
-		EXPERIMENTAL,
-		"5", "seconds" },
-#endif
 	{ "auto_restart", tweak_bool, &mgt_param.auto_restart,
 		NULL, NULL,
 		"Restart child process automatically if it dies.",
diff --git a/bin/varnishd/mgt/mgt_param_tcp.c b/bin/varnishd/mgt/mgt_param_tcp.c
new file mode 100644
index 0000000..7e5c7f9
--- /dev/null
+++ b/bin/varnishd/mgt/mgt_param_tcp.c
@@ -0,0 +1,125 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2013 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Parameters related to TCP keepalives are not universally available
+ * as socket options, and probing for system-wide defaults more appropriate
+ * than our own involves slightly too much grunt-work to be neglible
+ * so we sequestrate that code here.
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <unistd.h>
+
+#include "mgt/mgt.h"
+#include "common/params.h"
+
+#include "vss.h"
+
+#include "mgt/mgt_param.h"
+
+static struct parspec mgt_parspec_tcp_keep[] = {
+	{ "tcp_keepalive_time", tweak_timeout, &mgt_param.tcp_keepalive_time,
+		"1", "7200",
+		"The number of seconds a connection needs to be idle before "
+		"TCP begins sending out keep-alive probes.",
+		EXPERIMENTAL,
+		"", "seconds" },
+	{ "tcp_keepalive_probes", tweak_uint, &mgt_param.tcp_keepalive_probes,
+		"1", "100",
+		"The maximum number of TCP keep-alive probes to send before "
+		"giving up and killing the connection if no response is "
+		"obtained from the other end.",
+		EXPERIMENTAL,
+		"", "probes" },
+	{ "tcp_keepalive_intvl", tweak_timeout, &mgt_param.tcp_keepalive_intvl,
+		"1", "100",
+		"The number of seconds between TCP keep-alive probes.",
+		EXPERIMENTAL,
+		"", "seconds" },
+	{ NULL, NULL, NULL }
+};
+
+#ifdef HAVE_TCP_KEEP
+
+static void
+tcp_probe(int sock, int nam, const char *param, unsigned def)
+{
+	int i;
+	socklen_t l;
+	unsigned u;
+	char buf[10];
+	const char *p;
+
+	l = sizeof u;
+	i = getsockopt(sock, IPPROTO_TCP, nam, &u, &l);
+	if (i < 0 || u == 0)
+		u = def;
+	bprintf(buf, "%u", u);
+	p = strdup(buf);
+	AN(p);
+	MCF_SetDefault(param, p);
+}
+
+static void
+tcp_keep_probes(void)
+{
+	int i, s;
+	struct vss_addr **ta;
+
+	/* Probe a dummy socket for default values */
+
+	i = VSS_resolve(":0", NULL, &ta);
+	if (i == 0)
+		return;		// XXX: log
+	assert (i > 0);
+	s = VSS_listen(ta[0], 10);
+	if (s < 0)
+		return;		// XXX: log
+
+	tcp_probe(s, TCP_KEEPIDLE, "tcp_keepalive_time",	600);
+	tcp_probe(s, TCP_KEEPCNT, "tcp_keepalive_probes",	5);
+	tcp_probe(s, TCP_KEEPINTVL, "tcp_keepalive_intvl",	5);
+	AZ(close(s));
+}
+#endif
+
+void
+MCF_TcpParams(void)
+{
+#ifdef HAVE_TCP_KEEP
+	MCF_AddParams(mgt_parspec_tcp_keep);
+	tcp_keep_probes();
+#endif
+}



More information about the varnish-commit mailing list