[master] 8a3dfdf The arguments we pass to the VCL and VRT code is getting out of hand and I've already forgot about them one important place already.

Poul-Henning Kamp phk at varnish-cache.org
Tue Apr 30 11:04:01 CEST 2013


commit 8a3dfdf3291190193616f5047ddda5a01dfad063
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Apr 30 09:02:04 2013 +0000

    The arguments we pass to the VCL and VRT code is getting out of hand
    and I've already forgot about them one important place already.
    
    Introduce a "VRT_context", which is a container holding all the
    bits we want to pass to VCL and VRT.
    
    Implement it first for VRT_count(), which is, BTW, how I realized I
    had forgotten to pass all the new arguments to user defined
    functions.

diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index 43ecc7c..7dd3c8b 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -39,6 +39,7 @@
 #include "cache.h"
 
 #include "vcl.h"
+#include "vrt.h"
 #include "vcli.h"
 #include "vcli_priv.h"
 
@@ -172,8 +173,13 @@ VCL_Load(const char *fn, const char *name, struct cli *cli)
 {
 	struct vcls *vcl;
 	struct VCL_conf const *cnf;
+	struct vrt_ctx ctx;
 
 	ASSERT_CLI();
+
+	memset(&ctx, 0, sizeof ctx);
+	ctx.magic = VRT_CTX_MAGIC;
+
 	vcl = vcl_find(name);
 	if (vcl != NULL) {
 		VCLI_Out(cli, "Config '%s' already loaded", name);
@@ -214,7 +220,7 @@ VCL_Load(const char *fn, const char *name, struct cli *cli)
 	REPLACE(vcl->name, name);
 	VCLI_Out(cli, "Loaded \"%s\" as \"%s\"", fn , name);
 	VTAILQ_INSERT_TAIL(&vcl_head, vcl, list);
-	(void)vcl->conf->init_func(NULL, NULL, NULL, NULL);
+	(void)vcl->conf->init_func(&ctx, NULL, NULL, NULL, NULL);
 	Lck_Lock(&vcl_mtx);
 	if (vcl_active == NULL)
 		vcl_active = vcl;
@@ -232,13 +238,16 @@ VCL_Load(const char *fn, const char *name, struct cli *cli)
 static void
 VCL_Nuke(struct vcls *vcl)
 {
+	struct vrt_ctx ctx;
 
+	memset(&ctx, 0, sizeof ctx);
+	ctx.magic = VRT_CTX_MAGIC;
 	ASSERT_CLI();
 	assert(vcl != vcl_active);
 	assert(vcl->conf->discard);
 	assert(vcl->conf->busy == 0);
 	VTAILQ_REMOVE(&vcl_head, vcl, list);
-	(void)vcl->conf->fini_func(NULL, NULL, NULL, NULL);
+	(void)vcl->conf->fini_func(&ctx, NULL, NULL, NULL, NULL);
 	vcl->conf->fini_vcl(NULL);
 	free(vcl->name);
 	(void)dlclose(vcl->dlh);
@@ -368,18 +377,26 @@ vcl_call_method(struct worker *wrk, struct req *req, struct busyobj *bo,
 {
 	char *aws;
 	struct vsl_log *vsl;
+	struct vrt_ctx ctx;
 
 	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
+	memset(&ctx, 0, sizeof ctx);
+	ctx.magic = VRT_CTX_MAGIC;
 	if (req != NULL) {
 		AZ(bo);
 		CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
 		CHECK_OBJ_NOTNULL(req->sp, SESS_MAGIC);
 		vsl = req->vsl;
+		ctx.vsl = vsl;
+		ctx.vcl = req->vcl;
 	} else {
 		AZ(req);
 		CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
 		vsl = bo->vsl;
+		ctx.vsl = vsl;
+		ctx.vcl = bo->vcl;
 	}
+	ctx.ws = ws;
 	if (method == VCL_MET_BACKEND_FETCH ||
 	    method == VCL_MET_PASS ||
 	    method == VCL_MET_MISS ||
@@ -394,7 +411,7 @@ vcl_call_method(struct worker *wrk, struct req *req, struct busyobj *bo,
 	wrk->handling = 0;
 	wrk->cur_method = method;
 	VSLb(vsl, SLT_VCL_call, "%s", VCL_Method_Name(method));
-	(void)func(wrk, req, bo, ws);
+	(void)func(&ctx, wrk, req, bo, ws);
 	VSLb(vsl, SLT_VCL_return, "%s", VCL_Return_Name(wrk->handling));
 	wrk->cur_method = 0;
 	WS_Reset(wrk->aws, aws);
diff --git a/bin/varnishd/cache/cache_vrt.c b/bin/varnishd/cache/cache_vrt.c
index aa16783..4f87829 100644
--- a/bin/varnishd/cache/cache_vrt.c
+++ b/bin/varnishd/cache/cache_vrt.c
@@ -86,14 +86,13 @@ VRT_error(struct req *req, unsigned code, const char *reason)
 /*--------------------------------------------------------------------*/
 
 void
-VRT_count(struct req *req, unsigned u)
+VRT_count(const struct vrt_ctx *ctx, unsigned u)
 {
 
-	if (req == NULL)
-		return;
-	CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
-	VSLb(req->vsl, SLT_VCL_trace, "%u %u.%u", u,
-	    req->vcl->ref[u].line, req->vcl->ref[u].pos);
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	if (ctx->vsl != NULL)
+		VSLb(ctx->vsl, SLT_VCL_trace, "%u %u.%u", u,
+		    ctx->vcl->ref[u].line, ctx->vcl->ref[u].pos);
 }
 
 /*--------------------------------------------------------------------*/
diff --git a/include/vrt.h b/include/vrt.h
index 9dfc649..acf736b 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -32,7 +32,10 @@
  */
 
 struct req;
+struct busyobj;
 struct worker;
+struct vsl_log;
+struct http;
 struct ws;
 struct vsb;
 struct cli;
@@ -58,6 +61,29 @@ typedef const char *			VCL_STRING;
 typedef double				VCL_TIME;
 typedef void				VCL_VOID;
 
+/***********************************************************************
+ * This is the composite argument we pass to compiled VCL and VRT
+ * functions.
+ */
+
+struct vrt_ctx {
+	unsigned			magic;
+#define VRT_CTX_MAGIC			0x6bb8f0db
+
+	struct vsl_log			*vsl;
+	struct VCL_conf			*vcl;
+	struct ws			*ws;
+
+	struct req			*req;
+	struct http			*http_req;
+	struct http			*http_obj;
+	struct http			*http_resp;
+
+	struct busyobj			*bo;
+	struct http			*http_bereq;
+	struct http			*http_beresp;
+};
+
 /***********************************************************************/
 
 enum gethdr_e { HDR_REQ, HDR_RESP, HDR_OBJ, HDR_BEREQ, HDR_BERESP };
@@ -172,7 +198,7 @@ const char *VRT_regsub(struct req *, int all, const char *,
 void VRT_ban_string(const char *);
 void VRT_purge(const struct worker *, struct req *, double ttl, double grace);
 
-void VRT_count(struct req *, unsigned);
+void VRT_count(const struct vrt_ctx *, unsigned);
 int VRT_rewrite(const char *, const char *);
 void VRT_error(struct req *, unsigned, const char *);
 int VRT_switch_config(const char *);
diff --git a/lib/libvcl/generate.py b/lib/libvcl/generate.py
index a4b3b7a..1a1c2bd 100755
--- a/lib/libvcl/generate.py
+++ b/lib/libvcl/generate.py
@@ -765,6 +765,7 @@ file_header(fo)
 
 fo.write("""
 struct sess;
+struct vrt_ctx;
 struct req;
 struct busyobj;
 struct ws;
@@ -773,7 +774,7 @@ struct worker;
 
 typedef int vcl_init_f(struct cli *);
 typedef void vcl_fini_f(struct cli *);
-typedef int vcl_func_f(struct worker *, struct req *, struct busyobj *,
+typedef int vcl_func_f(struct vrt_ctx *ctx, struct worker *, struct req *, struct busyobj *,
     struct ws *);
 """)
 
diff --git a/lib/libvcl/vcc_action.c b/lib/libvcl/vcc_action.c
index 6a08e45..217aee4 100644
--- a/lib/libvcl/vcc_action.c
+++ b/lib/libvcl/vcc_action.c
@@ -46,7 +46,7 @@ parse_call(struct vcc *tl)
 	ExpectErr(tl, ID);
 	vcc_AddCall(tl, tl->t);
 	vcc_AddRef(tl, tl->t, SYM_SUB);
-	Fb(tl, 1, "if (VGC_function_%.*s(req))\n", PF(tl->t));
+	Fb(tl, 1, "if (VGC_function_%.*s(ctx, req))\n", PF(tl->t));
 	Fb(tl, 1, "\treturn (1);\n");
 	vcc_NextToken(tl);
 	return;
diff --git a/lib/libvcl/vcc_compile.c b/lib/libvcl/vcc_compile.c
index 30a71cb..d6dd786 100644
--- a/lib/libvcl/vcc_compile.c
+++ b/lib/libvcl/vcc_compile.c
@@ -693,7 +693,7 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
 	for (i = 0; i < VCL_MET_MAX; i++) {
 		Fc(tl, 1, "\nstatic int __match_proto__(vcl_func_f)\n");
 		Fc(tl, 1,
-		    "VGC_function_%s(struct worker *wrk,"
+		    "VGC_function_%s(struct vrt_ctx *ctx, struct worker *wrk,"
 		    " struct req *req, struct busyobj *bo, struct ws *ws)\n",
 		    method_tab[i].name);
 		AZ(VSB_finish(tl->fm[i]));
diff --git a/lib/libvcl/vcc_parse.c b/lib/libvcl/vcc_parse.c
index af97241..75e976f 100644
--- a/lib/libvcl/vcc_parse.c
+++ b/lib/libvcl/vcc_parse.c
@@ -47,7 +47,7 @@ static void vcc_Compound(struct vcc *tl);
 } while (0)
 
 #define C(tl, sep)	do {					\
-	Fb(tl, 1, "VRT_count(req, %u)%s\n", ++tl->cnt, sep);	\
+	Fb(tl, 1, "VRT_count(ctx, %u)%s\n", ++tl->cnt, sep);	\
 	tl->t->cnt = tl->cnt;					\
 } while (0)
 
@@ -237,10 +237,10 @@ vcc_Function(struct vcc *tl)
 		}
 		tl->curproc = vcc_AddProc(tl, tl->t);
 		Fh(tl, 0, "static int VGC_function_%.*s "
-		    "(struct req *);\n", PF(tl->t));
+		    "(struct vrt_ctx *ctx, struct req *);\n", PF(tl->t));
 		Fc(tl, 1, "\nstatic int __match_proto__(vcl_func_t)\n");
-		Fc(tl, 1, "VGC_function_%.*s(struct req *req)\n",
-		    PF(tl->t));
+		Fc(tl, 1, "VGC_function_%.*s(struct vrt_ctx *ctx, "
+		    "struct req *req)\n", PF(tl->t));
 	}
 	vcc_NextToken(tl);
 	tl->indent += INDENT;



More information about the varnish-commit mailing list