Commit 6fe390f8 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

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


git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@5371 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent fdc9c8de
......@@ -462,6 +462,9 @@ struct vbc {
struct vdi_simple *vdis;
int fd;
struct sockaddr *addr;
socklen_t addrlen;
uint8_t recycled;
/* Timeouts */
......
......@@ -94,6 +94,8 @@ VBE_ReleaseConn(struct vbc *vc)
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 @@ vbe_TryConnect(const struct sess *sp, int pf, const struct sockaddr *sa,
/*--------------------------------------------------------------------*/
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 @@ bes_conn_try(const struct sess *sp, const struct vdi_simple *vs)
/* 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 @@ vbe_GetVbe(struct sess *sp, struct vdi_simple *vs)
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 @@ vbe_GetVbe(struct sess *sp, struct vdi_simple *vs)
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++;
......
......@@ -552,6 +552,40 @@ VRT_r_bereq_between_bytes_timeout(struct sess *sp)
/*--------------------------------------------------------------------*/
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)
{
......
......@@ -27,6 +27,11 @@ varnish v1 -vcl+backend {
}
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;
}
......
......@@ -298,6 +298,24 @@ sp_variables = (
( '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',),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment