[master] edf9ecc Add "tcp_pools" which is where struct vbc is headed now.

Poul-Henning Kamp phk at FreeBSD.org
Tue Jan 13 12:13:24 CET 2015


commit edf9ecc6af705ff60b5bbbce2c9b94e847991cd8
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Jan 13 11:12:40 2015 +0000

    Add "tcp_pools" which is where struct vbc is headed now.

diff --git a/bin/varnishd/Makefile.am b/bin/varnishd/Makefile.am
index 3d614c8..c0f6c1f 100644
--- a/bin/varnishd/Makefile.am
+++ b/bin/varnishd/Makefile.am
@@ -13,6 +13,7 @@ varnishd_SOURCES = \
 	cache/cache_backend.c \
 	cache/cache_backend_cfg.c \
 	cache/cache_backend_poll.c \
+	cache/cache_backend_tcp.c \
 	cache/cache_ban.c \
 	cache/cache_busyobj.c \
 	cache/cache_cli.c \
diff --git a/bin/varnishd/cache/cache_backend.c b/bin/varnishd/cache/cache_backend.c
index 706ec0b..a5c2b22 100644
--- a/bin/varnishd/cache/cache_backend.c
+++ b/bin/varnishd/cache/cache_backend.c
@@ -90,7 +90,7 @@ VBE_ReleaseConn(struct vbc *vc)
 static void
 bes_conn_try(struct busyobj *bo, struct vbc *vc, const struct vbe_dir *vs)
 {
-	int s, msec;
+	int s;
 	double tmod;
 	struct backend *bp = vs->backend;
 	char abuf1[VTCP_ADDRBUFSIZE];
@@ -111,20 +111,7 @@ bes_conn_try(struct busyobj *bo, struct vbc *vc, const struct vbe_dir *vs)
 	/* release lock during stuff that can take a long time */
 
 	FIND_TMO(connect_timeout, tmod, bo, vs->vrt);
-	msec = (int)floor(tmod * 1000.0);
-
-	if (cache_param->prefer_ipv6 && bp->ipv6 != NULL) {
-		s = VTCP_connect(bp->ipv6, msec);
-		vc->addr = bp->ipv6;
-	}
-	if (s == -1 && bp->ipv4 != NULL) {
-		s = VTCP_connect(bp->ipv4, msec);
-		vc->addr = bp->ipv4;
-	}
-	if (s == -1 && !cache_param->prefer_ipv6 && bp->ipv6 != NULL) {
-		s = VTCP_connect(bp->ipv6, msec);
-		vc->addr = bp->ipv6;
-	}
+	s = VBT_Open(bp->tcp_pool, tmod, &vc->addr);
 
 	vc->fd = s;
 	if (s < 0) {
diff --git a/bin/varnishd/cache/cache_backend.h b/bin/varnishd/cache/cache_backend.h
index dc7c85d..b7c27e0 100644
--- a/bin/varnishd/cache/cache_backend.h
+++ b/bin/varnishd/cache/cache_backend.h
@@ -40,6 +40,7 @@
 struct vbp_target;
 struct vbc;
 struct vrt_backend_probe;
+struct tcp_pool;
 
 /*--------------------------------------------------------------------
  * An instance of a backend from a VCL program.
@@ -78,6 +79,8 @@ struct backend {
 	double			health_changed;
 
 	struct VSC_C_vbe	*vsc;
+
+	struct tcp_pool		*tcp_pool;
 };
 
 /* -------------------------------------------------------------------*/
@@ -91,7 +94,7 @@ struct vbc {
 	struct vbe_dir		*vdis;
 	int			fd;
 
-	struct suckaddr		*addr;
+	const struct suckaddr	*addr;
 
 	uint8_t			recycled;
 };
@@ -120,3 +123,8 @@ void VBP_Summary(struct cli *cli, const struct vbp_target *vt);
 /* cache_backend_poll.c */
 void VBP_Init(void);
 
+struct tcp_pool *VBT_Ref(const char *name, const struct suckaddr *ip4,
+    const struct suckaddr *ip6);
+void VBT_Rel(struct tcp_pool **tpp);
+int VBT_Open(struct tcp_pool *tp, double tmo, const struct suckaddr **sa);
+
diff --git a/bin/varnishd/cache/cache_backend_cfg.c b/bin/varnishd/cache/cache_backend_cfg.c
index 432ff05..29c9be9 100644
--- a/bin/varnishd/cache/cache_backend_cfg.c
+++ b/bin/varnishd/cache/cache_backend_cfg.c
@@ -66,6 +66,7 @@ VBE_Nuke(struct backend *b)
 	free(b->ipv6_addr);
 	free(b->port);
 	VSM_Free(b->vsc);
+	VBT_Rel(&b->tcp_pool);
 	FREE_OBJ(b);
 	VSC_C_main->n_backend--;
 }
@@ -213,6 +214,9 @@ VBE_AddBackend(struct cli *cli, const struct vrt_backend *vb)
 	REPLACE(b->ipv6_addr, vb->ipv6_addr);
 	REPLACE(b->port, vb->port);
 
+	b->tcp_pool = VBT_Ref(vb->vcl_name,
+	    vb->ipv4_suckaddr, vb->ipv6_suckaddr);
+
 	/*
 	 * Copy over the sockaddrs
 	 */
diff --git a/bin/varnishd/cache/cache_backend_poll.c b/bin/varnishd/cache/cache_backend_poll.c
index 1519e0e..10fd4c3 100644
--- a/bin/varnishd/cache/cache_backend_poll.c
+++ b/bin/varnishd/cache/cache_backend_poll.c
@@ -41,14 +41,16 @@
 #include <poll.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/socket.h>
 
 #include "cache.h"
 
 #include "cache_backend.h"
 #include "vcli_priv.h"
 #include "vrt.h"
-#include "vtcp.h"
 #include "vtim.h"
+#include "vtcp.h"
+#include "vsa.h"
 
 /* Default averaging rate, we want something pretty responsive */
 #define AVG_RATE			4
@@ -113,6 +115,7 @@ vbp_poke(struct vbp_target *vt)
 	struct backend *bp;
 	char buf[8192], *p;
 	struct pollfd pfda[1], *pfd = pfda;
+	const struct suckaddr *sa;
 
 	bp = vt->backend;
 	CHECK_OBJ_NOTNULL(bp, BACKEND_MAGIC);
@@ -121,32 +124,22 @@ vbp_poke(struct vbp_target *vt)
 	t_end = t_start + vt->probe.timeout;
 	tmo = (int)round((t_end - t_now) * 1e3);
 
-	s = -1;
-	if (cache_param->prefer_ipv6 && bp->ipv6 != NULL) {
-		s = VTCP_connect(bp->ipv6, tmo);
-		t_now = VTIM_real();
-		tmo = (int)round((t_end - t_now) * 1e3);
-		if (s >= 0)
-			vt->good_ipv6 |= 1;
-	}
-	if (tmo > 0 && s < 0 && bp->ipv4 != NULL) {
-		s = VTCP_connect(bp->ipv4, tmo);
-		t_now = VTIM_real();
-		tmo = (int)round((t_end - t_now) * 1e3);
-		if (s >= 0)
-			vt->good_ipv4 |= 1;
-	}
-	if (tmo > 0 && s < 0 && bp->ipv6 != NULL) {
-		s = VTCP_connect(bp->ipv6, tmo);
-		t_now = VTIM_real();
-		tmo = (int)round((t_end - t_now) * 1e3);
-		if (s >= 0)
-			vt->good_ipv6 |= 1;
-	}
+	s = VBT_Open(bp->tcp_pool, t_end - t_now, &sa);
 	if (s < 0) {
 		/* Got no connection: failed */
 		return;
 	}
+
+	i = VSA_Get_Proto(sa);
+	if (i == AF_INET)
+		vt->good_ipv4 |= 1;
+	else if(i == AF_INET6)
+		vt->good_ipv6 |= 1;
+	else
+		WRONG("Wrong probe protocol family");
+
+	t_now = VTIM_real();
+	tmo = (int)round((t_end - t_now) * 1e3);
 	if (tmo <= 0) {
 		/* Spent too long time getting it */
 		VTCP_close(&s);
diff --git a/bin/varnishd/cache/cache_backend_tcp.c b/bin/varnishd/cache/cache_backend_tcp.c
new file mode 100644
index 0000000..175ab48
--- /dev/null
+++ b/bin/varnishd/cache/cache_backend_tcp.c
@@ -0,0 +1,158 @@
+/*-
+ * Copyright (c) 2015 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.
+ *
+ * TCP connection pools for backends
+ *
+ */
+
+#include "config.h"
+
+#include <math.h>
+#include <poll.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <stdio.h>
+
+#include "cache.h"
+
+#include "cache_backend.h"
+#include "vrt.h"
+#include "vtcp.h"
+#include "vsa.h"
+
+struct tcp_pool {
+	unsigned		magic;
+#define TCP_POOL_MAGIC		0x28b0e42a
+
+	char			*name;
+	struct suckaddr		*ip4;
+	struct suckaddr		*ip6;
+
+	VTAILQ_ENTRY(tcp_pool)	list;
+	int			refcnt;
+
+
+};
+
+static VTAILQ_HEAD(, tcp_pool)	pools = VTAILQ_HEAD_INITIALIZER(pools);
+
+/*--------------------------------------------------------------------
+ */
+
+struct tcp_pool *
+VBT_Ref(const char *name, const struct suckaddr *ip4,
+    const struct suckaddr *ip6)
+{
+	struct tcp_pool *tp;
+
+	ASSERT_CLI();
+	VTAILQ_FOREACH(tp, &pools, list) {
+		assert(tp->refcnt > 0);
+		if (strcmp(tp->name, name))
+			continue;
+		if (ip4 == NULL) {
+			if (tp->ip4 != NULL)
+				continue;
+		} else {
+			if (tp->ip4 == NULL)
+				continue;
+			if (VSA_Compare(ip4, tp->ip4))
+				continue;
+		}
+		if (ip6 == NULL) {
+			if (tp->ip6 != NULL)
+				continue;
+		} else {
+			if (tp->ip6 == NULL)
+				continue;
+			if (VSA_Compare(ip6, tp->ip6))
+				continue;
+		}
+		tp->refcnt++;
+		return (tp);
+	}
+
+	ALLOC_OBJ(tp, TCP_POOL_MAGIC);
+	AN(tp);
+	REPLACE(tp->name, name);
+	if (ip4 != NULL)
+		tp->ip4 = VSA_Clone(ip4);
+	if (ip6 != NULL)
+		tp->ip6 = VSA_Clone(ip6);
+	tp->refcnt = 1;
+	VTAILQ_INSERT_HEAD(&pools, tp, list);
+	return (tp);
+}
+
+/*--------------------------------------------------------------------
+ */
+
+void
+VBT_Rel(struct tcp_pool **tpp)
+{
+	struct tcp_pool *tp;
+
+	AN(tpp);
+	tp = *tpp;
+	*tpp = NULL;
+	CHECK_OBJ_NOTNULL(tp, TCP_POOL_MAGIC);
+	assert(tp->refcnt > 0);
+	if (--tp->refcnt > 0)
+		return;
+	VTAILQ_REMOVE(&pools, tp, list);
+	free(tp->name);
+	free(tp->ip4);
+	free(tp->ip6);
+	FREE_OBJ(tp);
+}
+
+/*--------------------------------------------------------------------
+ */
+
+int
+VBT_Open(struct tcp_pool *tp, double tmo, const struct suckaddr **sa)
+{
+	int s;
+	int msec;
+
+	CHECK_OBJ_NOTNULL(tp, TCP_POOL_MAGIC);
+
+	msec = (int)floor(tmo * 1000.0);
+	if (cache_param->prefer_ipv6) {
+		*sa = tp->ip6;
+		s = VTCP_connect(tp->ip6, msec);
+		if (s >= 0)
+			return(s);
+	}
+	*sa = tp->ip4;
+	s = VTCP_connect(tp->ip4, msec);
+	if (s < 0 && !cache_param->prefer_ipv6) {
+		*sa = tp->ip6;
+		s = VTCP_connect(tp->ip6, msec);
+	}
+	return(s);
+}



More information about the varnish-commit mailing list