r5654 - in trunk/varnish-cache: bin/varnishd include lib/libvcl

phk at varnish-cache.org phk at varnish-cache.org
Sun Dec 19 22:38:29 CET 2010


Author: phk
Date: 2010-12-19 22:38:28 +0100 (Sun, 19 Dec 2010)
New Revision: 5654

Added:
   trunk/varnish-cache/include/vrt_stv_var.h
Modified:
   trunk/varnish-cache/bin/varnishd/stevedore.c
   trunk/varnish-cache/bin/varnishd/stevedore.h
   trunk/varnish-cache/include/Makefile.am
   trunk/varnish-cache/include/vrt.h
   trunk/varnish-cache/lib/libvcl/generate.py
   trunk/varnish-cache/lib/libvcl/vcc_storage.c
Log:
Add support for stevedore variable access from VCL.

Be a total CPP-show-off-douche and make it very easy to add further
variables in the future, provided you can find the magic table.

Presently define three variables:


	REAL storage.$stevedore.free_space
	REAL storage.$stevedore.used_space
	BOOL storage.$stevedore.happy

Still debating if I should add a "SPACE" type to VCL, so that you
can write:
	if (storage.foobar.free_space < 1 GB) {...}

until I decide, it is a REAL, which is less than useful, because
we have no relational operators besides identity for REAL.



Modified: trunk/varnish-cache/bin/varnishd/stevedore.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/stevedore.c	2010-12-17 20:00:21 UTC (rev 5653)
+++ trunk/varnish-cache/bin/varnishd/stevedore.c	2010-12-19 21:38:28 UTC (rev 5654)
@@ -537,11 +537,30 @@
 }
 
 int
-VRT_Stv(struct sess *sp, const char *nm)
+VRT_Stv(const char *nm)
 {
-	(void)sp;
 
 	if (stv_find(nm) != NULL)
 		return (1);
 	return (0);
 }
+
+#define VRTSTVVAR_PROTO
+#include "vrt_stv_var.h"
+
+#define VRTSTVVAR(nm, vtype, ctype, dval)	\
+ctype						\
+VRT_Stv_##nm(const char *nm)			\
+{						\
+	const struct stevedore *stv;		\
+						\
+	stv = stv_find(nm);			\
+	if (stv == NULL)			\
+		return (dval);			\
+	if (stv->var_##nm == NULL)		\
+		return (dval);			\
+	return (stv->var_##nm(stv));		\
+}
+
+#include "vrt_stv_var.h"
+#undef VRTSTVVAR

Modified: trunk/varnish-cache/bin/varnishd/stevedore.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/stevedore.h	2010-12-17 20:00:21 UTC (rev 5653)
+++ trunk/varnish-cache/bin/varnishd/stevedore.h	2010-12-19 21:38:28 UTC (rev 5654)
@@ -45,6 +45,9 @@
     unsigned ltot, const struct stv_objsecrets *);
 typedef void storage_close_f(const struct stevedore *);
 
+/* Prototypes for VCL variable responders */
+typedef double storage_var_double(const struct stevedore *);
+typedef int storage_var_int(const struct stevedore *);
 
 struct stevedore {
 	unsigned		magic;
@@ -61,6 +64,10 @@
 
 	struct lru		*lru;
 
+#define VRTSTVVAR(nm, vtype, ctype, dval) storage_var_##ctype *var_##nm;
+#include "vrt_stv_var.h"
+#undef VRTSTVVAR
+
 	/* private fields */
 	void			*priv;
 
@@ -98,4 +105,3 @@
 #ifdef HAVE_LIBUMEM
 extern const struct stevedore smu_stevedore;
 #endif
-

Modified: trunk/varnish-cache/include/Makefile.am
===================================================================
--- trunk/varnish-cache/include/Makefile.am	2010-12-17 20:00:21 UTC (rev 5653)
+++ trunk/varnish-cache/include/Makefile.am	2010-12-19 21:38:28 UTC (rev 5654)
@@ -49,6 +49,7 @@
 	vre.h \
 	vrt.h \
 	vrt_obj.h \
+	vrt_stv_var.h \
 	vss.h \
 	vtypes.h
 

Modified: trunk/varnish-cache/include/vrt.h
===================================================================
--- trunk/varnish-cache/include/vrt.h	2010-12-17 20:00:21 UTC (rev 5653)
+++ trunk/varnish-cache/include/vrt.h	2010-12-19 21:38:28 UTC (rev 5654)
@@ -207,7 +207,7 @@
 }
 
 /* Stevedore related functions */
-int VRT_Stv(struct sess *sp, const char *nm);
+int VRT_Stv(const char *nm);
 
 /* Convert things to string */
 

Added: trunk/varnish-cache/include/vrt_stv_var.h
===================================================================
--- trunk/varnish-cache/include/vrt_stv_var.h	                        (rev 0)
+++ trunk/varnish-cache/include/vrt_stv_var.h	2010-12-19 21:38:28 UTC (rev 5654)
@@ -0,0 +1,42 @@
+/*-
+ * Copyright (c) 2010 Varnish-Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id$
+ */
+
+#ifdef VRTSTVVAR_PROTO
+#define VRTSTVVAR(nm, vtype, ctype, dval) ctype VRT_Stv_ ## nm(const char *nm);
+#endif
+
+VRTSTVVAR(free_space,	REAL, double,	0.)
+VRTSTVVAR(used_space,	REAL, double,	0.)
+VRTSTVVAR(happy,	BOOL, int,	0)
+
+#ifdef VRTSTVVAR_PROTO
+#undef VRTSTVVAR_PROTO
+#undef VRTSTVVAR
+#endif

Modified: trunk/varnish-cache/lib/libvcl/generate.py
===================================================================
--- trunk/varnish-cache/lib/libvcl/generate.py	2010-12-17 20:00:21 UTC (rev 5653)
+++ trunk/varnish-cache/lib/libvcl/generate.py	2010-12-19 21:38:28 UTC (rev 5654)
@@ -41,6 +41,9 @@
 # XXX: does it actually do that ?
 
 import sys
+import subprocess
+
+
 srcroot = "../.."
 buildroot = "../.."
 if len(sys.argv) == 3:
@@ -504,10 +507,14 @@
 #######################################################################
 # Read a C-source file and spit out code that outputs it with vsb_cat()
 
-def emit_file(fo, fn):
-	fi = open(fn)
-	fc = fi.read()
-	fi.close()
+def emit_file(fo, fn, cpp = False):
+	if cpp:
+		fc = subprocess.check_output(["cpp", "-DVRTSTVVAR_PROTO", fn])
+		fc = fc.decode("ascii")
+	else:
+		fi = open(fn)
+		fc = fi.read()
+		fi.close()
 
 	w = 66		# Width of lines, after white space prefix
 	maxlen = 10240	# Max length of string literal
@@ -809,6 +816,7 @@
 emit_file(fo, buildroot + "/include/vcl.h")
 emit_file(fo, srcroot + "/include/vrt.h")
 emit_file(fo, buildroot + "/include/vrt_obj.h")
+emit_file(fo, buildroot + "/include/vrt_stv_var.h", True)
 
 fo.write("""
 }

Modified: trunk/varnish-cache/lib/libvcl/vcc_storage.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_storage.c	2010-12-17 20:00:21 UTC (rev 5653)
+++ trunk/varnish-cache/lib/libvcl/vcc_storage.c	2010-12-19 21:38:28 UTC (rev 5654)
@@ -89,6 +89,16 @@
 	return (v);
 }
 
+static struct stvars {
+	const char	*name;
+	enum var_type	fmt;
+} stvars[] = {
+#define VRTSTVVAR(nm, vtype, ctype, dval)	{ #nm, vtype },
+#include "vrt_stv_var.h"
+#undef VRTSTVVAR
+	{ NULL,			0 }
+};
+
 struct symbol *
 vcc_Stv_Wildcard(struct vcc *tl, const struct token *t,
     const struct symbol *wcsym)
@@ -96,6 +106,7 @@
 	const char *p, *q;
 	struct var *v = NULL;
 	struct symbol *sym;
+	struct stvars *sv;
 	char stv[1024];
 	char buf[1024];
 
@@ -110,13 +121,21 @@
 
 	if (q == t->e) {
 		v = vcc_Stv_mkvar(tl, t, BOOL);
-		bprintf(buf, "VRT_Stv(sp, \"%s\")", stv);
+		bprintf(buf, "VRT_Stv(\"%s\")", stv);
 		v->rname = TlDup(tl, buf);
-#if 0
 	} else {
+		assert(*q  == '.');
 		q++;
-fprintf(stderr, "Q: %s <%.*s>\n", stv, (int)(t->e - q), q);
-#endif
+		for(sv = stvars; sv->name != NULL; sv++) {
+			if (strncmp(q, sv->name, t->e - q))
+				continue;
+			if (sv->name[t->e - q] != '\0')
+				continue;
+			v = vcc_Stv_mkvar(tl, t, sv->fmt);
+			bprintf(buf, "VRT_Stv_%s(\"%s\")", sv->name, stv);
+			v->rname = TlDup(tl, buf);
+			break;
+		}
 	}
 
 	if (v == NULL)




More information about the varnish-commit mailing list