r5371 - in trunk/varnish-cache: bin/varnishd bin/varnishtest/tests lib/libvcl

phk at varnish-cache.org phk at varnish-cache.org
Thu Sep 30 13:10:07 CEST 2010


Author: phk
Date: 2010-09-30 13:10:07 +0200 (Thu, 30 Sep 2010)
New Revision: 5371

Modified:
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_backend.c
   trunk/varnish-cache/bin/varnishd/cache_vrt.c
   trunk/varnish-cache/bin/varnishtest/tests/v00028.vtc
   trunk/varnish-cache/lib/libvcl/generate.py
Log:
Add three new VCL variables, readble from vcl_fetch{} only:

beresp.backend.name (STRING)
	VCL name of backend fetched from.
	This name may have the form "somedirector[29]" if backends
	are declared directly in directors.

beresp.backend.ip (IP)
	IP number of remote end of connection.

beresp.backend.port (INT)
	TCP port number of remote end of connection.

Fixes:	#481


Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2010-09-30 10:05:55 UTC (rev 5370)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2010-09-30 11:10:07 UTC (rev 5371)
@@ -462,6 +462,9 @@
 	struct vdi_simple	*vdis;
 	int			fd;
 
+	struct sockaddr		*addr;
+	socklen_t		addrlen;
+
 	uint8_t			recycled;
 
 	/* Timeouts */

Modified: trunk/varnish-cache/bin/varnishd/cache_backend.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_backend.c	2010-09-30 10:05:55 UTC (rev 5370)
+++ trunk/varnish-cache/bin/varnishd/cache_backend.c	2010-09-30 11:10:07 UTC (rev 5371)
@@ -94,6 +94,8 @@
 	assert(vc->backend == NULL);
 	assert(vc->fd < 0);
 
+	vc->addr = NULL;
+	vc->addrlen = 0;
 	vc->recycled = 0;
 	if (params->cache_vbcs) {
 		Lck_Lock(&VBE_mtx);
@@ -166,8 +168,8 @@
 
 /*--------------------------------------------------------------------*/
 
-static int
-bes_conn_try(const struct sess *sp, const struct vdi_simple *vs)
+static void
+bes_conn_try(const struct sess *sp, struct vbc *vc, const struct vdi_simple *vs)
 {
 	int s;
 	struct backend *bp = vs->backend;
@@ -184,20 +186,31 @@
 
 	/* release lock during stuff that can take a long time */
 
-	if (params->prefer_ipv6 && bp->ipv6 != NULL)
+	if (params->prefer_ipv6 && bp->ipv6 != NULL) {
 		s = vbe_TryConnect(sp, PF_INET6, bp->ipv6, bp->ipv6len, vs);
-	if (s == -1 && bp->ipv4 != NULL)
+		vc->addr = bp->ipv6;
+		vc->addrlen = bp->ipv6len;
+	}
+	if (s == -1 && bp->ipv4 != NULL) {
 		s = vbe_TryConnect(sp, PF_INET, bp->ipv4, bp->ipv4len, vs);
-	if (s == -1 && !params->prefer_ipv6 && bp->ipv6 != NULL)
+		vc->addr = bp->ipv4;
+		vc->addrlen = bp->ipv4len;
+	}
+	if (s == -1 && !params->prefer_ipv6 && bp->ipv6 != NULL) {
 		s = vbe_TryConnect(sp, PF_INET6, bp->ipv6, bp->ipv6len, vs);
+		vc->addr = bp->ipv6;
+		vc->addrlen = bp->ipv6len;
+	}
 
+	vc->fd = s;
 	if (s < 0) {
 		Lck_Lock(&bp->mtx);
 		bp->n_conn--;
 		bp->refcount--;		/* Only keep ref on success */
 		Lck_Unlock(&bp->mtx);
+		vc->addr = NULL;
+		vc->addrlen = 0;
 	}
-	return (s);
 }
 
 /*--------------------------------------------------------------------
@@ -354,6 +367,7 @@
 			bp->refcount++;
 			assert(vc->backend == bp);
 			assert(vc->fd >= 0);
+			AN(vc->addr);
 			VTAILQ_REMOVE(&bp->connlist, vc, list);
 		}
 		Lck_Unlock(&bp->mtx);
@@ -395,7 +409,7 @@
 	vc = vbe_NewConn();
 	assert(vc->fd == -1);
 	AZ(vc->backend);
-	vc->fd = bes_conn_try(sp, vs);
+	bes_conn_try(sp, vc, vs);
 	if (vc->fd < 0) {
 		VBE_ReleaseConn(vc);
 		VSC_main->backend_fail++;

Modified: trunk/varnish-cache/bin/varnishd/cache_vrt.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_vrt.c	2010-09-30 10:05:55 UTC (rev 5370)
+++ trunk/varnish-cache/bin/varnishd/cache_vrt.c	2010-09-30 11:10:07 UTC (rev 5371)
@@ -552,6 +552,40 @@
 
 /*--------------------------------------------------------------------*/
 
+const char *
+VRT_r_beresp_backend_name(const struct sess *sp)
+{
+
+	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
+	CHECK_OBJ_NOTNULL(sp->vbc, VBC_MAGIC);
+	return(sp->vbc->backend->vcl_name);
+}
+
+struct sockaddr *
+VRT_r_beresp_backend_ip(const struct sess *sp)
+{
+
+	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
+	CHECK_OBJ_NOTNULL(sp->vbc, VBC_MAGIC);
+	return(sp->vbc->addr);
+}
+
+int
+VRT_r_beresp_backend_port(const struct sess *sp)
+{
+	char abuf[TCP_ADDRBUFSIZE];
+	char pbuf[TCP_PORTBUFSIZE];
+
+	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
+	CHECK_OBJ_NOTNULL(sp->vbc, VBC_MAGIC);
+	TCP_name(sp->vbc->addr, sp->vbc->addrlen,
+	    abuf, sizeof abuf, pbuf, sizeof pbuf);
+
+	return (atoi(pbuf));
+}
+
+/*--------------------------------------------------------------------*/
+
 void
 VRT_handling(struct sess *sp, unsigned hand)
 {

Modified: trunk/varnish-cache/bin/varnishtest/tests/v00028.vtc
===================================================================
--- trunk/varnish-cache/bin/varnishtest/tests/v00028.vtc	2010-09-30 10:05:55 UTC (rev 5370)
+++ trunk/varnish-cache/bin/varnishtest/tests/v00028.vtc	2010-09-30 11:10:07 UTC (rev 5371)
@@ -27,6 +27,11 @@
 		}
 		return (pass);
 	}
+	sub vcl_fetch {
+		set beresp.http.be_ip = beresp.backend.ip;
+		set beresp.http.be_port = beresp.backend.port;
+		set beresp.http.be_name = beresp.backend.name;
+	}
 	sub vcl_deliver {
 		set resp.http.id = client.identity;
 	}

Modified: trunk/varnish-cache/lib/libvcl/generate.py
===================================================================
--- trunk/varnish-cache/lib/libvcl/generate.py	2010-09-30 10:05:55 UTC (rev 5370)
+++ trunk/varnish-cache/lib/libvcl/generate.py	2010-09-30 11:10:07 UTC (rev 5371)
@@ -298,6 +298,24 @@
 		( 'fetch',),
 		'const struct sess *'
 	),
+	('beresp.backend.name',
+		'STRING',
+		( 'fetch',),
+		( ),
+		'const struct sess *'
+	),
+	('beresp.backend.ip',
+		'IP',
+		( 'fetch',),
+		( ),
+		'const struct sess *'
+	),
+	('beresp.backend.port',
+		'INT',
+		( 'fetch',),
+		( ),
+		'const struct sess *'
+	),
 	('obj.proto',
 		'STRING',
 		( 'hit', 'error',),




More information about the varnish-commit mailing list