aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2014-11-01 14:10:23 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2014-11-01 14:10:23 +0200
commitdacdfd20c9e59e088bfb23c2852b566acc6ad06f (patch)
tree9c8acf2e253afd9593d5f6fccd560f7173485d48 /src
parent4d452a770122daf33fdbe0a549e2804b6f2db086 (diff)
downloadvmod-tbf-dacdfd20c9e59e088bfb23c2852b566acc6ad06f.tar.gz
vmod-tbf-dacdfd20c9e59e088bfb23c2852b566acc6ad06f.tar.bz2
Support for VCL 4.0
* NEWS: Update. * configure.ac: Version 1.0.90 Detect Varnish version, set up the C defines and automake conditionals to indicate it. * src/.gitignore: Update. * src/Makefile.am: Don't distribute vcc_if.c and vcc_if.h, build them for the current Varnish version. * src/tbf.h: New file. * src/getla.c: Include tbf.h, rewrite to compile with both 3.0 and 4.0 * src/sleep.c: Likewise. * src/tbf.c: Likewise. * src/time.c: Likewise. * src/vmod_tbf.vcc: Rewrite using v4.0 format. * tests/.gitignore: New file. * tests/Makefile.am: Build test00.vtc and test02.vtc * tests/test00.vtc: Rename to tests/test00.vti, add conditional compilation directives. * tests/test02.vtc: Rename to tests/test02.vti, add conditional compilation directives.
Diffstat (limited to 'src')
-rw-r--r--src/.gitignore1
-rw-r--r--src/Makefile.am31
-rw-r--r--src/getla.c11
-rw-r--r--src/sleep.c8
-rw-r--r--src/tbf.c30
-rw-r--r--src/tbf.h38
-rw-r--r--src/time.c23
-rw-r--r--src/vmod_tbf.vcc47
8 files changed, 128 insertions, 61 deletions
diff --git a/src/.gitignore b/src/.gitignore
index 7f6e438..ce7aef0 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -1,2 +1,3 @@
vcc_if.c
vcc_if.h
+*.rst
diff --git a/src/Makefile.am b/src/Makefile.am
index 727d33b..f4e3bec 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -16,6 +16,7 @@
AM_CPPFLAGS=\
-I$(VARNISHSRC)/include\
+ -I$(VARNISHSRC)/bin/varnishd\
-I$(VARNISHSRC)\
-DLOCALSTATEDIR=\"$(localstatedir)\"
@@ -28,19 +29,37 @@ libvmod_tbf_la_LDFLAGS=-module -export-dynamic -avoid-version
libvmod_tbf_la_LIBADD=
libvmod_tbf_la_SOURCES = \
+ tbf.c\
+ tbf.h\
getla.c\
sleep.c\
- time.c\
- tbf.c\
+ time.c
+nodist_libvmod_tbf_la_SOURCES = \
vcc_if.c vcc_if.h
-BUILT_SOURCES = vcc_if.c vcc_if.h
+tbf.lo: vcc_if.h
+
+CLEANFILES = vcc_if.c vcc_if.h *.rst
+
+if VARNISH4
+ vmodtool = $(VARNISHSRC)/lib/libvcc/vmodtool.py
+ vmodtoolargs = --strict
+ vccfile = $(top_srcdir)/src/vmod_tbf.vcc
+else
+ vmodtool = $(VARNISHSRC)/lib/libvmod_std/vmod.py
+ vmodtoolargs =
+ vccfile = vmod_tbf.vcc3
+
+vmod_tbf.vcc3: $(top_srcdir)/src/vmod_tbf.vcc
+ CLEANFILES += vmod_tbf.vcc3
+endif
-vcc_if.c vcc_if.h: $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod_tbf.vcc
- @PYTHON@ $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod_tbf.vcc
+.vcc.vcc3:
+ sed -n '/^\$$/{s///;s/\(Module *[^ ][^ ]*\).*/\1/;p}' $< > $@
+vcc_if.c vcc_if.h: $(vmodtool) $(vccfile)
+ @PYTHON@ $(vmodtool) $(vmodtoolargs) $(vccfile)
EXTRA_DIST = \
vmod_tbf.vcc
-CLEANFILES = $(builddir)/vcc_if.c $(builddir)/vcc_if.h
diff --git a/src/getla.c b/src/getla.c
index c735956..51791c4 100644
--- a/src/getla.c
+++ b/src/getla.c
@@ -15,15 +15,10 @@
along with vmod-tbf. If not, see <http://www.gnu.org/licenses/>.
*/
#define _BSD_SOURCE
-#include <config.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <syslog.h>
+#include "tbf.h"
#if defined(HAVE_SYSINFO) && defined(HAVE_SYS_SYSINFO_H)
# include <sys/sysinfo.h>
#endif
-#include "vrt.h"
-#include "vcc_if.h"
static double
sys_getla(int what)
@@ -50,8 +45,8 @@ sys_getla(int what)
#endif
}
-double
-vmod_getla(struct sess *sp, int what)
+VCL_REAL
+vmod_getla(MOD_CTX ctx, VCL_INT what)
{
switch (what) {
case 1:
diff --git a/src/sleep.c b/src/sleep.c
index 70c4337..9242475 100644
--- a/src/sleep.c
+++ b/src/sleep.c
@@ -14,14 +14,12 @@
You should have received a copy of the GNU General Public License
along with vmod-tbf. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <config.h>
+#include "tbf.h"
#include <errno.h>
#include <time.h>
-#include "vrt.h"
-#include "vcc_if.h"
-void
-vmod_sleep(struct sess *sp, double t)
+VCL_VOID
+vmod_sleep(MOD_CTX ctx, VCL_REAL t)
{
struct timespec ts, ret;
diff --git a/src/tbf.c b/src/tbf.c
index 7050fc7..56a4ad1 100644
--- a/src/tbf.c
+++ b/src/tbf.c
@@ -14,17 +14,12 @@
You should have received a copy of the GNU General Public License
along with vmod-tbf. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <config.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdbool.h>
+#include "tbf.h"
#include <syslog.h>
#include <inttypes.h>
#include <sys/stat.h>
+#include <sys/time.h>
#include <db.h>
-#include "vrt.h"
-#include "vcc_if.h"
-#include "bin/varnishd/cache.h"
static int debug_level;
@@ -351,7 +346,7 @@ tbf_init(struct vmod_priv *priv, const struct VCL_conf *vclconf)
}
void
-vmod_open(struct sess *sp, const char *dir, const char *params)
+vmod_open(MOD_CTX ctx, const char *dir, const char *params)
{
if (db) {
syslog(LOG_DAEMON|LOG_ERR, "tbf.open called twice");
@@ -362,7 +357,7 @@ vmod_open(struct sess *sp, const char *dir, const char *params)
}
void
-vmod_close(struct sess *sp)
+vmod_close(MOD_CTX ctx)
{
pthread_mutex_lock(&mutex);
if (db) {
@@ -377,7 +372,7 @@ vmod_close(struct sess *sp)
}
void
-vmod_sync(struct sess *sp)
+vmod_sync(MOD_CTX ctx)
{
if (db) {
debug(1, ("synchronizing database"));
@@ -410,7 +405,7 @@ struct tbf_bucket {
};
int
-tbf_proc(struct sess *sp, DB *db, const char *key, int cost,
+tbf_proc(MOD_CTX ctx, DB *db, const char *key, int cost,
unsigned long interval, int burst_size)
{
DBT keydat, content;
@@ -499,8 +494,9 @@ tbf_proc(struct sess *sp, DB *db, const char *key, int cost,
return res;
}
-unsigned
-vmod_rate(struct sess *sp, const char *key, int cost, double t, int burst_size)
+VCL_BOOL
+vmod_rate(MOD_CTX ctx, VCL_STRING key, VCL_INT cost, VCL_REAL t,
+ VCL_INT burst_size)
{
unsigned long interval = t * USEC_PER_SEC;
int rc;
@@ -527,7 +523,7 @@ vmod_rate(struct sess *sp, const char *key, int cost, double t, int burst_size)
kp = keylock_find_safe(key);
debug(2, ("found key %s, ref %u", key, kp->refcnt));
AZ(pthread_mutex_lock(&kp->mutex));
- rc = tbf_proc(sp, db, key, cost, interval, burst_size);
+ rc = tbf_proc(ctx, db, key, cost, interval, burst_size);
if (--kp->refcnt == 0)
keylock_remove_safe(kp);
AZ(pthread_mutex_unlock(&kp->mutex));
@@ -539,8 +535,8 @@ vmod_rate(struct sess *sp, const char *key, int cost, double t, int burst_size)
#define ISWS(c) ((c)==' '||(c)=='\t')
-unsigned
-vmod_check(struct sess *sp, const char *key, const char *spec)
+VCL_BOOL
+vmod_check(MOD_CTX ctx, VCL_STRING key, VCL_STRING spec)
{
double t, v, n;
char *p;
@@ -601,5 +597,5 @@ vmod_check(struct sess *sp, const char *key, const char *spec)
syslog(LOG_DAEMON|LOG_WARNING, "garbage after rate spec: %s",
spec);
- return vmod_rate(sp, key, 1, n/v, v/n+1);
+ return vmod_rate(ctx, key, 1, n/v, v/n+1);
}
diff --git a/src/tbf.h b/src/tbf.h
new file mode 100644
index 0000000..3c7a118
--- /dev/null
+++ b/src/tbf.h
@@ -0,0 +1,38 @@
+/* This file is part of vmod-tbf
+ Copyright (C) 2013-2014 Sergey Poznyakoff
+
+ Vmod-tbf is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ Vmod-tbf is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with vmod-tbf. If not, see <http://www.gnu.org/licenses/>.
+*/
+#include <config.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <syslog.h>
+#include "vrt.h"
+#include "vcc_if.h"
+#include "pthread.h"
+#if VARNISHVERSION == 3
+# include "bin/varnishd/cache.h"
+# define VCL_VOID void
+# define VCL_INT int
+# define VCL_REAL double
+# define VCL_BOOL unsigned
+# define VCL_STRING const char *
+# define MOD_CTX struct sess *
+# define WSPTR(s) ((s)->wrk->ws)
+#else
+# include "bin/varnishd/cache/cache.h"
+# define MOD_CTX const struct vrt_ctx *
+# define WSPTR(s) ((s)->ws)
+#endif
diff --git a/src/time.c b/src/time.c
index 01919de..9a5068b 100644
--- a/src/time.c
+++ b/src/time.c
@@ -14,36 +14,31 @@
You should have received a copy of the GNU General Public License
along with vmod-tbf. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <config.h>
-#include <stdlib.h>
-#include <stdio.h>
+#include "tbf.h"
#include <time.h>
-#include "vrt.h"
-#include "vcc_if.h"
-#include "bin/varnishd/cache.h"
-int
-vmod_systime(struct sess *sp)
+VCL_INT
+vmod_systime(MOD_CTX ctx)
{
return time(NULL);
}
-const char *
-vmod_strftime(struct sess *sp, const char *format, int timestamp)
+VCL_STRING
+vmod_strftime(MOD_CTX ctx, VCL_STRING format, VCL_INT timestamp)
{
time_t ts = (time_t) timestamp;
size_t u, n;
char *p;
- u = WS_Reserve(sp->wrk->ws, 0);
- p = sp->wrk->ws->f;
+ u = WS_Reserve(WSPTR(ctx), 0);
+ p = WSPTR(ctx)->f;
n = strftime(p, u, format, gmtime(&ts));
if (n == 0) {
- WS_Release(sp->wrk->ws, 0);
+ WS_Release(WSPTR(ctx), 0);
return NULL;
}
- WS_Release(sp->wrk->ws, n + 1);
+ WS_Release(WSPTR(ctx), n + 1);
return p;
}
diff --git a/src/vmod_tbf.vcc b/src/vmod_tbf.vcc
index 3634807..b27093b 100644
--- a/src/vmod_tbf.vcc
+++ b/src/vmod_tbf.vcc
@@ -1,11 +1,36 @@
-Module tbf
-Init tbf_init
-Function VOID open(STRING, STRING)
-Function VOID close()
-Function VOID sync()
-Function BOOL rate(STRING, INT, DURATION, INT)
-Function BOOL check(STRING, STRING)
-Function REAL getla(INT)
-Function INT systime()
-Function STRING strftime(STRING, INT)
-Function VOID sleep(DURATION)
+# This file is part of vmod-tbf
+# Copyright (C) 2013 Sergey Poznyakoff
+#
+# Vmod-tbf is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# Vmod-tbf is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with vmod-tbf. If not, see <http://www.gnu.org/licenses/>.
+
+$Module tbf 3 Token Bucket Filtering for Varnish
+
+COLOPHON
+========
+This document provides a short description of the **vmod-tbf** module.
+For a detailed documentation, please see vmod-tbf(3) manual page.
+
+DESCRIPTION
+===========
+
+$Init tbf_init
+$Function VOID open(STRING, STRING)
+$Function VOID close()
+$Function VOID sync()
+$Function BOOL rate(STRING, INT, DURATION, INT)
+$Function BOOL check(STRING, STRING)
+$Function REAL getla(INT)
+$Function INT systime()
+$Function STRING strftime(STRING, INT)
+$Function VOID sleep(DURATION)

Return to:

Send suggestions and report system problems to the System administrator.