[master] 180e785 Always pull the local address of the socket out right away and log it. Parallel use of sockets would force us to add locking and that's simply not worth it.

Poul-Henning Kamp phk at varnish-cache.org
Fri Nov 22 10:51:23 CET 2013


commit 180e785d8aaaac7abb1f47645ae4691b6ac8357a
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri Nov 22 09:50:16 2013 +0000

    Always pull the local address of the socket out right away and
    log it.  Parallel use of sockets would force us to add locking
    and that's simply not worth it.
    
    Also:
    
    Fixes #1376

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 5ed30c4..77a109a 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -766,8 +766,8 @@ struct sess {
 
 	/* Session related fields ------------------------------------*/
 
-	struct suckaddr		*their_addr;
-	struct suckaddr		*our_addr;
+	struct suckaddr		*remote_addr;
+	struct suckaddr		*local_addr;
 
 	/* formatted ascii client address */
 	char			addr[ADDR_BUFSIZE];
@@ -1114,7 +1114,6 @@ struct req *SES_GetReq(struct worker *, struct sess *);
 void SES_Handle(struct sess *sp, double now);
 void SES_ReleaseReq(struct req *);
 pool_func_t SES_pool_accept_task;
-void SES_Get_Our_Addr(struct sess *sp);
 
 
 /* cache_shmlog.c */
diff --git a/bin/varnishd/cache/cache_acceptor.c b/bin/varnishd/cache/cache_acceptor.c
index 17fe100..ba203c9 100644
--- a/bin/varnishd/cache/cache_acceptor.c
+++ b/bin/varnishd/cache/cache_acceptor.c
@@ -352,8 +352,7 @@ VCA_SetupSess(struct worker *wrk, struct sess *sp)
 	wa->acceptsock = -1;
 	retval = wa->acceptlsock->name;
 	assert(wa->acceptaddrlen <= vsa_suckaddr_len);
-	sp->their_addr = VSA_Build(sp->their_addr,
-	    &wa->acceptaddr, wa->acceptaddrlen);
+	AN(VSA_Build(sp->remote_addr, &wa->acceptaddr, wa->acceptaddrlen));
 	vca_pace_good();
 	wrk->stats.sess_conn++;
 	WS_Release(wrk->aws, 0);
diff --git a/bin/varnishd/cache/cache_session.c b/bin/varnishd/cache/cache_session.c
index cdfcc9d..ad0f537 100644
--- a/bin/varnishd/cache/cache_session.c
+++ b/bin/varnishd/cache/cache_session.c
@@ -91,6 +91,11 @@ SES_Charge(struct worker *wrk, struct req *req)
 
 /*--------------------------------------------------------------------
  * Get a new session, preferably by recycling an already ready one
+ *
+ * Layout is:
+ *	struct sess
+ *	struct vsa (local_addr)
+ *	struct vsa (remote_addr)
  */
 
 static struct sess *
@@ -107,15 +112,19 @@ ses_new(struct sesspool *pp)
 
 	s = (char *)sp;
 	s += sizeof *sp;
+
+	memset(s, 0, vsa_suckaddr_len);
+	sp->local_addr = (void*)s;
 	s += vsa_suckaddr_len;
+
 	memset(s, 0, vsa_suckaddr_len);
-	sp->their_addr = (void*)s;
+	sp->remote_addr = (void*)s;
 	s += vsa_suckaddr_len;
+
 	assert((char *)sp + sz == s);
 
 	sp->t_open = NAN;
 	sp->t_idle = NAN;
-	sp->our_addr = NULL;
 	Lck_New(&sp->mtx, lck_sess);
 	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 	return (sp);
@@ -165,29 +174,6 @@ ses_sess_pool_task(struct worker *wrk, void *arg)
 }
 
 /*--------------------------------------------------------------------
- * Get the local socket address
- */
-
-void
-SES_Get_Our_Addr(struct sess *sp)
-{
-	char *s;
-	struct sockaddr_storage ss;
-	socklen_t sl;
-
-	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
-	if (sp->our_addr != NULL)
-		return;
-
-	sl = sizeof ss;
-	AZ(getsockname(sp->fd, (void*)&ss, &sl));
-	s = (char *)sp;
-	s += sizeof *sp;
-	sp->our_addr = VSA_Build(s, &ss, sl);
-	assert(VSA_Sane(sp->our_addr));
-}
-
-/*--------------------------------------------------------------------
  * VSL log the endpoints of the TCP connection.
  *
  * We use VSL() to get the sessions vxid and to make sure tha this
@@ -200,22 +186,23 @@ SES_Get_Our_Addr(struct sess *sp)
 static void
 ses_vsl_socket(struct sess *sp, const char *lsockname)
 {
+	struct sockaddr_storage ss;
+	socklen_t sl;
 	char laddr[ADDR_BUFSIZE];
 	char lport[PORT_BUFSIZE];
 
 	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 	AN(lsockname);
 
-	VTCP_name(sp->their_addr,
+	AN(sp->local_addr);
+	sl = sizeof ss;
+	AZ(getsockname(sp->fd, (void*)&ss, &sl));
+	AN(VSA_Build(sp->local_addr, &ss, sl));
+	assert(VSA_Sane(sp->local_addr));
+
+	VTCP_name(sp->remote_addr,
 	    sp->addr, sizeof sp->addr, sp->port, sizeof sp->port);
-	if (cache_param->log_local_addr) {
-		SES_Get_Our_Addr(sp);
-		VTCP_name(sp->our_addr,
-		    laddr, sizeof laddr, lport, sizeof lport);
-	} else {
-		strcpy(laddr, "-");
-		strcpy(lport, "-");
-	}
+	VTCP_name(sp->local_addr, laddr, sizeof laddr, lport, sizeof lport);
 	VSL(SLT_Begin, sp->vxid, "sess");
 	VSL(SLT_SessOpen, sp->vxid, "%s %s %s %s %s %.6f %d",
 	    sp->addr, sp->port, lsockname, laddr, lport, sp->t_open, sp->fd);
diff --git a/bin/varnishd/cache/cache_vrt_var.c b/bin/varnishd/cache/cache_vrt_var.c
index 0ef4a05..2555f24 100644
--- a/bin/varnishd/cache/cache_vrt_var.c
+++ b/bin/varnishd/cache/cache_vrt_var.c
@@ -540,7 +540,7 @@ VRT_r_client_ip(const struct vrt_ctx *ctx)
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
 	CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
-	return (ctx->req->sp->their_addr);
+	return (ctx->req->sp->remote_addr);
 }
 
 VCL_IP
@@ -549,8 +549,8 @@ VRT_r_server_ip(const struct vrt_ctx *ctx)
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
 	CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
-	SES_Get_Our_Addr(ctx->req->sp);
-	return (ctx->req->sp->our_addr);
+	AN(ctx->req->sp->local_addr);
+	return (ctx->req->sp->local_addr);
 }
 
 const char*
@@ -584,8 +584,8 @@ VRT_r_server_port(const struct vrt_ctx *ctx)
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
 	CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
-	SES_Get_Our_Addr(ctx->req->sp);
-	return (VSA_Port(ctx->req->sp->our_addr));
+	AN(ctx->req->sp->local_addr);
+	return (VSA_Port(ctx->req->sp->local_addr));
 }
 
 /*--------------------------------------------------------------------*/
diff --git a/bin/varnishd/common/params.h b/bin/varnishd/common/params.h
index 28c4500..7a69119 100644
--- a/bin/varnishd/common/params.h
+++ b/bin/varnishd/common/params.h
@@ -165,9 +165,6 @@ struct params {
 	/* CLI buffer size */
 	unsigned		cli_buffer;
 
-	/* Log local socket address to shm */
-	unsigned		log_local_addr;
-
 	/* Prefer IPv6 connections to backend*/
 	unsigned		prefer_ipv6;
 
diff --git a/bin/varnishd/mgt/mgt_param_tbl.c b/bin/varnishd/mgt/mgt_param_tbl.c
index 169f521..4bb6592 100644
--- a/bin/varnishd/mgt/mgt_param_tbl.c
+++ b/bin/varnishd/mgt/mgt_param_tbl.c
@@ -436,14 +436,6 @@ struct parspec mgt_parspec[] = {
 		"more sessions take a detour around the waiter.",
 		EXPERIMENTAL,
 		"0.050", "seconds" },
-	{ "log_local_address", tweak_bool, &mgt_param.log_local_addr,
-		NULL, NULL,
-		"Log the local address on the TCP connection in the "
-		"SessionOpen VSL record.\n"
-		"Disabling this saves a getsockname(2) system call "
-		"per TCP connection.",
-		0,
-		"on", "bool" },
 	{ "waiter", tweak_waiter, NULL,
 		NULL, NULL,
 		"Select the waiter kernel interface.",
diff --git a/bin/varnishtest/tests/c00003.vtc b/bin/varnishtest/tests/c00003.vtc
index 0327873..9e811b6 100644
--- a/bin/varnishtest/tests/c00003.vtc
+++ b/bin/varnishtest/tests/c00003.vtc
@@ -8,7 +8,6 @@ server s1 {
 # This requires non-local binds to be disabled.  If you see this fail
 # and are on Linux, ensure /proc/net/ipv4/ip_nonlocal_bind is set to 0.
 varnish v1 -cliok "param.set listen_address ${bad_ip}:0"
-varnish v1 -cliok "param.set log_local_address off"
 varnish v1 -vcl+backend {} -clierr 300 start
 
 varnish v1 -cliok "param.set listen_address 127.0.0.1:0,${bad_ip}:9082"



More information about the varnish-commit mailing list