[master] 32aaa009c Reverse the TCP(|UDS) and Connection pool layers.

Poul-Henning Kamp phk at FreeBSD.org
Thu Jan 7 13:00:09 UTC 2021


commit 32aaa009c63c07ba89744c47daf5e755560cfbdc
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Thu Jan 7 12:40:00 2021 +0000

    Reverse the TCP(|UDS) and Connection pool layers.
    
    Now the TCP|UDS simply returns a conn_pool, which then becomes
    the primary handle.

diff --git a/bin/varnishd/cache/cache_backend.c b/bin/varnishd/cache/cache_backend.c
index 3f6d4afa6..4dc9324b8 100644
--- a/bin/varnishd/cache/cache_backend.c
+++ b/bin/varnishd/cache/cache_backend.c
@@ -155,7 +155,7 @@ vbe_dir_getfd(VRT_CTX, struct worker *wrk, struct backend *bp,
 	bo->htc->doclose = SC_NULL;
 
 	FIND_TMO(connect_timeout, tmod, bo, bp);
-	pfd = VTP_Get(bp->tcp_pool, tmod, wrk, force_fresh, &err);
+	pfd = VCP_Get(bp->conn_pool, tmod, wrk, force_fresh, &err);
 	if (pfd == NULL) {
 		VBE_Connect_Error(bp->vsc, err);
 		VSLb(bo->vsl, SLT_FetchError,
@@ -188,7 +188,7 @@ vbe_dir_getfd(VRT_CTX, struct worker *wrk, struct backend *bp,
 		// account as if connect failed - good idea?
 		VSC_C_main->backend_fail++;
 		bo->htc = NULL;
-		VTP_Close(&pfd);
+		VCP_Close(&pfd);
 		AZ(pfd);
 		Lck_Lock(&bp->mtx);
 		bp->n_conn--;
@@ -237,7 +237,7 @@ vbe_dir_finish(VRT_CTX, VCL_BACKEND d)
 	if (bo->htc->doclose != SC_NULL || bp->proxy_header != 0) {
 		VSLb(bo->vsl, SLT_BackendClose, "%d %s close", *PFD_Fd(pfd),
 		    VRT_BACKEND_string(bp->director));
-		VTP_Close(&pfd);
+		VCP_Close(&pfd);
 		AZ(pfd);
 		Lck_Lock(&bp->mtx);
 	} else {
@@ -246,7 +246,7 @@ vbe_dir_finish(VRT_CTX, VCL_BACKEND d)
 		    VRT_BACKEND_string(bp->director));
 		Lck_Lock(&bp->mtx);
 		VSC_C_main->backend_recycle++;
-		VTP_Recycle(bo->wrk, &pfd);
+		VCP_Recycle(bo->wrk, &pfd);
 	}
 	assert(bp->n_conn > 0);
 	bp->n_conn--;
@@ -295,7 +295,7 @@ vbe_dir_gethdrs(VRT_CTX, VCL_BACKEND d)
 				&bo->acct.bereq_bodybytes);
 
 		if (PFD_State(pfd) != PFD_STATE_USED) {
-			if (VTP_Wait(wrk, pfd, VTIM_real() +
+			if (VCP_Wait(wrk, pfd, VTIM_real() +
 			    bo->htc->first_byte_timeout) != 0) {
 				bo->htc->doclose = SC_RX_TIMEOUT;
 				VSLb(bo->vsl, SLT_FetchError,
@@ -341,7 +341,7 @@ vbe_dir_getip(VRT_CTX, VCL_BACKEND d)
 	CHECK_OBJ_NOTNULL(ctx->bo->htc, HTTP_CONN_MAGIC);
 	pfd = ctx->bo->htc->priv;
 
-	return (VTP_GetIp(pfd));
+	return (VCP_GetIp(pfd));
 }
 
 /*--------------------------------------------------------------------*/
@@ -430,7 +430,7 @@ vbe_free(struct backend *be)
 	else
 		VTAILQ_REMOVE(&backends, be, list);
 	VSC_C_main->n_backend--;
-	VTP_Rel(&be->tcp_pool);
+	VCP_Rel(&be->conn_pool);
 	Lck_Unlock(&backends_mtx);
 
 #define DA(x)	do { if (be->x != NULL) free(be->x); } while (0)
@@ -462,7 +462,7 @@ vbe_panic(const struct director *d, struct vsb *vsb)
 	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
 	CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC);
 
-	VTP_Panic(vsb, bp->tcp_pool);
+	VCP_Panic(vsb, bp->conn_pool);
 	VSB_printf(vsb, "hosthdr = %s,\n", bp->hosthdr);
 	VSB_printf(vsb, "n_conn = %u,\n", bp->n_conn);
 }
@@ -611,15 +611,15 @@ VRT_new_backend_clustered(VRT_CTX, struct vsmw_cluster *vc,
 	if (! vcl->temp->is_warm)
 		VRT_VSC_Hide(be->vsc_seg);
 
-	be->tcp_pool = VTP_Ref(vep, vbe_proto_ident);
-	AN(be->tcp_pool);
+	be->conn_pool = VTP_Ref(vep, vbe_proto_ident);
+	AN(be->conn_pool);
 
 	vbp = vrt->probe;
 	if (vbp == NULL)
 		vbp = VCL_DefaultProbe(vcl);
 
 	if (vbp != NULL) {
-		VBP_Insert(be, vbp, be->tcp_pool);
+		VBP_Insert(be, vbp, be->conn_pool);
 		m = vbe_methods;
 	} else {
 		be->sick = 0;
diff --git a/bin/varnishd/cache/cache_backend.h b/bin/varnishd/cache/cache_backend.h
index 9262c5290..393b5df09 100644
--- a/bin/varnishd/cache/cache_backend.h
+++ b/bin/varnishd/cache/cache_backend.h
@@ -42,7 +42,7 @@
 struct vbp_target;
 struct vrt_ctx;
 struct vrt_backend_probe;
-struct tcp_pool;
+struct conn_pool;
 
 /*--------------------------------------------------------------------
  * An instance of a backend from a VCL program.
@@ -67,7 +67,7 @@ struct backend {
 	struct vsc_seg		*vsc_seg;
 	struct VSC_vbe		*vsc;
 
-	struct tcp_pool		*tcp_pool;
+	struct conn_pool	*conn_pool;
 
 	VCL_BACKEND		director;
 
@@ -84,7 +84,7 @@ void VBE_SetHappy(const struct backend *, uint64_t);
 /* cache_backend_probe.c */
 void VBP_Update_Backend(struct vbp_target *vt);
 void VBP_Insert(struct backend *b, struct vrt_backend_probe const *p,
-    struct tcp_pool *);
+    struct conn_pool *);
 void VBP_Remove(struct backend *b);
 void VBP_Control(const struct backend *b, int stop);
 void VBP_Status(struct vsb *, const struct backend *, int details, int json);
diff --git a/bin/varnishd/cache/cache_backend_probe.c b/bin/varnishd/cache/cache_backend_probe.c
index 8fb53d756..ee9e0dbf2 100644
--- a/bin/varnishd/cache/cache_backend_probe.c
+++ b/bin/varnishd/cache/cache_backend_probe.c
@@ -64,7 +64,7 @@ struct vbp_target {
 	VRT_BACKEND_PROBE_FIELDS()
 
 	struct backend			*backend;
-	struct tcp_pool			*tcp_pool;
+	struct conn_pool		*conn_pool;
 
 	char				*req;
 	int				req_len;
@@ -103,7 +103,7 @@ vbp_delete(struct vbp_target *vt)
 #define DN(x)	/**/
 	VRT_BACKEND_PROBE_HANDLE();
 #undef DN
-	VTP_Rel(&vt->tcp_pool);
+	VCP_Rel(&vt->conn_pool);
 	free(vt->req);
 	FREE_OBJ(vt);
 }
@@ -285,7 +285,7 @@ vbp_poke(struct vbp_target *vt)
 	t_start = t_now = VTIM_real();
 	t_end = t_start + vt->timeout;
 
-	s = VTP_Open(vt->tcp_pool, t_end - t_now, &sa, &err);
+	s = VCP_Open(vt->conn_pool, t_end - t_now, &sa, &err);
 	if (s < 0) {
 		bprintf(vt->resp_buf, "Open error %d (%s)", err, vstrerror(err));
 		Lck_Lock(&vbp_mtx);
@@ -673,7 +673,7 @@ VBP_Control(const struct backend *be, int enable)
 
 void
 VBP_Insert(struct backend *b, const struct vrt_backend_probe *vp,
-    struct tcp_pool *tp)
+    struct conn_pool *tp)
 {
 	struct vbp_target *vt;
 
@@ -685,8 +685,8 @@ VBP_Insert(struct backend *b, const struct vrt_backend_probe *vp,
 	ALLOC_OBJ(vt, VBP_TARGET_MAGIC);
 	XXXAN(vt);
 
-	vt->tcp_pool = tp;
-	VTP_AddRef(vt->tcp_pool);
+	vt->conn_pool = tp;
+	VCP_AddRef(vt->conn_pool);
 	vt->backend = b;
 	b->probe = vt;
 
diff --git a/bin/varnishd/cache/cache_main.c b/bin/varnishd/cache/cache_main.c
index a62cb83f6..e9d2da664 100644
--- a/bin/varnishd/cache/cache_main.c
+++ b/bin/varnishd/cache/cache_main.c
@@ -377,7 +377,7 @@ child_main(int sigmagic, size_t altstksz)
 	HTTP_Init();
 
 	VBO_Init();
-	VTP_Init();
+	VCP_Init();
 	VBP_Init();
 	VDI_Init();
 	VBE_InitCfg();
diff --git a/bin/varnishd/cache/cache_tcp_pool.c b/bin/varnishd/cache/cache_tcp_pool.c
index a649e7e25..31423253f 100644
--- a/bin/varnishd/cache/cache_tcp_pool.c
+++ b/bin/varnishd/cache/cache_tcp_pool.c
@@ -27,7 +27,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * TCP connection pools.
+ * (TCP|UDS) connection pools.
  *
  */
 
@@ -216,7 +216,7 @@ VCP_Ref(const uint8_t *ident)
 /*--------------------------------------------------------------------
  */
 
-static void *
+static struct conn_pool *
 VCP_New(struct conn_pool *cp, uint8_t ident[VSHA256_DIGEST_LENGTH],
     void *priv, const struct cp_methods *cm)
 {
@@ -240,14 +240,13 @@ VCP_New(struct conn_pool *cp, uint8_t ident[VSHA256_DIGEST_LENGTH],
 	VTAILQ_INSERT_HEAD(&conn_pools, cp, list);
 	Lck_Unlock(&conn_pools_mtx);
 
-	return (priv);
+	return (cp);
 }
 
-
 /*--------------------------------------------------------------------
  */
 
-static void
+void
 VCP_AddRef(struct conn_pool *cp)
 {
 	CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC);
@@ -262,12 +261,13 @@ VCP_AddRef(struct conn_pool *cp)
  * Release Conn pool, destroy if last reference.
  */
 
-static void
-VCP_Rel(struct conn_pool *cp)
+void
+VCP_Rel(struct conn_pool **cpp)
 {
+	struct conn_pool *cp;
 	struct pfd *pfd, *pfd2;
 
-	CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC);
+	TAKE_OBJ_NOTNULL(cp, cpp, CONN_POOL_MAGIC);
 
 	Lck_Lock(&conn_pools_mtx);
 	assert(cp->refcnt > 0);
@@ -305,7 +305,7 @@ VCP_Rel(struct conn_pool *cp)
  * Recycle a connection.
  */
 
-static void
+void
 VCP_Recycle(const struct worker *wrk, struct pfd **pfdp)
 {
 	struct pfd *pfd;
@@ -366,7 +366,7 @@ VCP_Recycle(const struct worker *wrk, struct pfd **pfdp)
  * Open a new connection from pool.
  */
 
-static int
+int
 VCP_Open(struct conn_pool *cp, vtim_dur tmo, VCL_IP *ap, int *err)
 {
 	int r;
@@ -440,7 +440,7 @@ VCP_Open(struct conn_pool *cp, vtim_dur tmo, VCL_IP *ap, int *err)
  * Close a connection.
  */
 
-static void
+void
 VCP_Close(struct pfd **pfdp)
 {
 	struct pfd *pfd;
@@ -474,7 +474,7 @@ VCP_Close(struct pfd **pfdp)
  * Get a connection, possibly recycled
  */
 
-static struct pfd *
+struct pfd *
 VCP_Get(struct conn_pool *cp, vtim_dur tmo, struct worker *wrk,
     unsigned force_fresh, int *err)
 {
@@ -526,7 +526,7 @@ VCP_Get(struct conn_pool *cp, vtim_dur tmo, struct worker *wrk,
 /*--------------------------------------------------------------------
  */
 
-static int
+int
 VCP_Wait(struct worker *wrk, struct pfd *pfd, vtim_real tmo)
 {
 	struct conn_pool *cp;
@@ -558,6 +558,40 @@ VCP_Wait(struct worker *wrk, struct pfd *pfd, vtim_real tmo)
 /*--------------------------------------------------------------------
  */
 
+VCL_IP
+VCP_GetIp(struct pfd *pfd)
+{
+
+	CHECK_OBJ_NOTNULL(pfd, PFD_MAGIC);
+	return (pfd->addr);
+}
+
+/*--------------------------------------------------------------------*/
+
+void
+VCP_Panic(struct vsb *vsb, struct conn_pool *cp)
+{
+
+	if (PAN_dump_struct(vsb, cp, CONN_POOL_MAGIC, "conn_pool"))
+		return;
+	VSB_printf(vsb, "ident = ");
+	VSB_quote(vsb, cp->ident, VSHA256_DIGEST_LENGTH, VSB_QUOTE_HEX);
+	VSB_printf(vsb, ",\n");
+	cp->methods->panic(vsb, cp->priv);
+	VSB_indent(vsb, -2);
+	VSB_cat(vsb, "},\n");
+}
+
+/*--------------------------------------------------------------------*/
+
+void
+VCP_Init(void)
+{
+	Lck_New(&conn_pools_mtx, lck_tcp_pool);
+}
+
+/**********************************************************************/
+
 struct tcp_pool {
 	unsigned				magic;
 #define TCP_POOL_MAGIC				0x28b0e42a
@@ -643,25 +677,6 @@ vtp_panic(struct vsb *vsb, const void *priv)
 
 /*--------------------------------------------------------------------*/
 
-void
-VTP_Panic(struct vsb *vsb, struct tcp_pool *tp)
-{
-	struct conn_pool *cp;
-
-	cp = tp->cp;
-
-	if (PAN_dump_struct(vsb, cp, CONN_POOL_MAGIC, "conn_pool"))
-		return;
-	VSB_printf(vsb, "ident = ");
-	VSB_quote(vsb, cp->ident, VSHA256_DIGEST_LENGTH, VSB_QUOTE_HEX);
-	VSB_printf(vsb, ",\n");
-	cp->methods->panic(vsb, cp->priv);
-	VSB_indent(vsb, -2);
-	VSB_cat(vsb, "},\n");
-}
-
-/*--------------------------------------------------------------------*/
-
 static void v_matchproto_(cp_close_f)
 vtp_close(struct pfd *pfd)
 {
@@ -740,7 +755,7 @@ static const struct cp_methods vus_methods = {
  * it doesn't exist already.
  */
 
-struct tcp_pool *
+struct conn_pool *
 VTP_Ref(const struct vrt_endpoint *vep, const char *ident)
 {
 	struct tcp_pool *tp;
@@ -775,7 +790,7 @@ VTP_Ref(const struct vrt_endpoint *vep, const char *ident)
 
 	cp = VCP_Ref(digest);
 	if (cp != NULL)
-		return (cp->priv);
+		return (cp);
 
 	/*
 	 * this is racy - we could end up with additional pools on the same id
@@ -797,100 +812,3 @@ VTP_Ref(const struct vrt_endpoint *vep, const char *ident)
 	}
 	return (VCP_New(tp->cp, digest, tp, methods));
 }
-
-/*--------------------------------------------------------------------
- * Add a reference to a tcp_pool
- */
-
-void
-VTP_AddRef(struct tcp_pool *tp)
-{
-	CHECK_OBJ_NOTNULL(tp, TCP_POOL_MAGIC);
-	VCP_AddRef(tp->cp);
-}
-
-/*--------------------------------------------------------------------
- * Release TCP pool, destroy if last reference.
- */
-
-void
-VTP_Rel(struct tcp_pool **tpp)
-{
-	struct tcp_pool *tp;
-
-	TAKE_OBJ_NOTNULL(tp, tpp, TCP_POOL_MAGIC);
-	VCP_Rel(tp->cp);
-}
-
-/*--------------------------------------------------------------------
- * Open a new connection from pool.
- */
-
-int
-VTP_Open(struct tcp_pool *tp, vtim_dur tmo, VCL_IP *ap, int *err)
-{
-	return (VCP_Open(tp->cp, tmo, ap, err));
-}
-
-/*--------------------------------------------------------------------
- * Recycle a connection.
- */
-
-void
-VTP_Recycle(const struct worker *wrk, struct pfd **pfdp)
-{
-
-	VCP_Recycle(wrk, pfdp);
-}
-
-/*--------------------------------------------------------------------
- * Close a connection.
- */
-
-void
-VTP_Close(struct pfd **pfdp)
-{
-
-	VCP_Close(pfdp);
-}
-
-/*--------------------------------------------------------------------
- * Get a connection
- */
-
-struct pfd *
-VTP_Get(struct tcp_pool *tp, vtim_dur tmo, struct worker *wrk,
-	unsigned force_fresh, int *err)
-{
-
-	return (VCP_Get(tp->cp, tmo, wrk, force_fresh, err));
-}
-
-/*--------------------------------------------------------------------
- */
-
-int
-VTP_Wait(struct worker *wrk, struct pfd *pfd, vtim_real tmo)
-{
-	return (VCP_Wait(wrk, pfd, tmo));
-}
-
-/*--------------------------------------------------------------------
- */
-
-VCL_IP
-VTP_GetIp(struct pfd *pfd)
-{
-
-	CHECK_OBJ_NOTNULL(pfd, PFD_MAGIC);
-	return (pfd->addr);
-}
-
-
-/*--------------------------------------------------------------------*/
-
-void
-VTP_Init(void)
-{
-	Lck_New(&conn_pools_mtx, lck_tcp_pool);
-}
diff --git a/bin/varnishd/cache/cache_tcp_pool.h b/bin/varnishd/cache/cache_tcp_pool.h
index 42dddb941..c2fccb909 100644
--- a/bin/varnishd/cache/cache_tcp_pool.h
+++ b/bin/varnishd/cache/cache_tcp_pool.h
@@ -28,11 +28,11 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * Outgoing TCP connection pools
+ * Outgoing TCP|UDS connection pools
  *
  */
 
-struct tcp_pool;
+struct conn_pool;
 struct pfd;
 #define PFD_STATE_AVAIL		(1<<0)
 #define PFD_STATE_USED		(1<<1)
@@ -48,13 +48,12 @@ void PFD_LocalName(const struct pfd *, char *, unsigned, char *, unsigned);
 void PFD_RemoteName(const struct pfd *, char *, unsigned, char *, unsigned);
 
 /*---------------------------------------------------------------------
-
  * Prototypes
  */
 
 struct VSC_vbe;
 
-struct tcp_pool *VTP_Ref(const struct vrt_endpoint *, const char *ident);
+struct conn_pool *VTP_Ref(const struct vrt_endpoint *, const char *ident);
 	/*
 	 * Get a reference to a TCP pool. Either one or both of ipv4 or
 	 * ipv6 arg must be non-NULL, or uds must be non-NULL. If recycling
@@ -62,47 +61,47 @@ struct tcp_pool *VTP_Ref(const struct vrt_endpoint *, const char *ident);
 	 * other pools with same {ipv4, ipv6, uds}.
 	 */
 
-void VTP_AddRef(struct tcp_pool *);
+void VCP_AddRef(struct conn_pool *);
 	/*
 	 * Get another reference to an already referenced TCP pool.
 	 */
 
-void VTP_Rel(struct tcp_pool **);
+void VCP_Rel(struct conn_pool **);
 	/*
 	 * Release reference to a TCP pool.  When last reference is released
 	 * the pool is destroyed and all cached connections closed.
 	 */
 
-int VTP_Open(struct tcp_pool *, vtim_dur tmo, VCL_IP *, int*);
+int VCP_Open(struct conn_pool *, vtim_dur tmo, VCL_IP *, int*);
 	/*
 	 * Open a new connection and return the address used.
 	 * errno will be returned in the last argument.
 	 */
 
-void VTP_Close(struct pfd **);
+void VCP_Close(struct pfd **);
 	/*
 	 * Close a connection.
 	 */
 
-void VTP_Recycle(const struct worker *, struct pfd **);
+void VCP_Recycle(const struct worker *, struct pfd **);
 	/*
 	 * Recycle an open connection.
 	 */
 
-struct pfd *VTP_Get(struct tcp_pool *, vtim_dur tmo, struct worker *,
+struct pfd *VCP_Get(struct conn_pool *, vtim_dur tmo, struct worker *,
     unsigned force_fresh, int *err);
 	/*
 	 * Get a (possibly) recycled connection.
 	 * errno will be stored in err
 	 */
 
-int VTP_Wait(struct worker *, struct pfd *, vtim_real tmo);
+int VCP_Wait(struct worker *, struct pfd *, vtim_real tmo);
 	/*
 	 * If the connection was recycled (state != VTP_STATE_USED) call this
 	 * function before attempting to receive on the connection.
 	 */
 
-void VTP_Panic(struct vsb *, struct tcp_pool *);
+void VCP_Panic(struct vsb *, struct conn_pool *);
 
-VCL_IP VTP_GetIp(struct pfd *);
+VCL_IP VCP_GetIp(struct pfd *);
 
diff --git a/bin/varnishd/cache/cache_varnishd.h b/bin/varnishd/cache/cache_varnishd.h
index d74f6ed5b..e53d28a23 100644
--- a/bin/varnishd/cache/cache_varnishd.h
+++ b/bin/varnishd/cache/cache_varnishd.h
@@ -145,8 +145,8 @@ void VCA_Shutdown(void);
 void VBE_InitCfg(void);
 void VBE_Poll(void);
 
-/* cache_backend_tcp.c */
-void VTP_Init(void);
+/* cache_tcp_pool.c */
+void VCP_Init(void);
 
 /* cache_backend_poll.c */
 void VBP_Init(void);


More information about the varnish-commit mailing list