From 8922e44832592354f846181b2aae9cbb4393f816 Mon Sep 17 00:00:00 2001 From: phk Date: Tue, 13 Jun 2006 07:57:32 +0000 Subject: [PATCH] Put more meat on the stevedore (storage backend) interface: Pull the struct definition into _stevedore.h and include this from cache.h and mgt.h, they both need to be able to see it. Add the stevedore pointer as an argument to the stevedore alloc function so multiple stevedores is possible later on. Add the stevedore pointer to the storage object, so freeing it again is possible. Add -s argument processing to select a given stevedore, call it's ->init method and pass the stevedore in the heritage. In the cache process pick stevedore out from heritage, call its open method. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@170 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/_stevedore.h | 21 +++++++++ varnish-cache/bin/varnishd/cache.h | 19 +++----- varnish-cache/bin/varnishd/cache_fetch.c | 4 +- varnish-cache/bin/varnishd/cache_main.c | 6 +-- varnish-cache/bin/varnishd/heritage.h | 13 +++--- varnish-cache/bin/varnishd/mgt.h | 4 ++ varnish-cache/bin/varnishd/storage_malloc.c | 5 ++- varnish-cache/bin/varnishd/varnishd.c | 49 ++++++++++++++++++--- 8 files changed, 92 insertions(+), 29 deletions(-) create mode 100644 varnish-cache/bin/varnishd/_stevedore.h diff --git a/varnish-cache/bin/varnishd/_stevedore.h b/varnish-cache/bin/varnishd/_stevedore.h new file mode 100644 index 00000000..41ba35a7 --- /dev/null +++ b/varnish-cache/bin/varnishd/_stevedore.h @@ -0,0 +1,21 @@ +/* + * $Id: cache.h 164 2006-05-01 12:45:20Z phk $ + */ + +struct stevedore; + +typedef void storage_init_f(struct stevedore *, const char *spec); +typedef void storage_open_f(struct stevedore *); +typedef struct storage *storage_alloc_f(struct stevedore *, size_t size); +typedef void storage_free_f(struct storage *); + +struct stevedore { + const char *name; + storage_init_f *init; /* called by mgt process */ + storage_open_f *open; /* called by cache process */ + storage_alloc_f *alloc; + storage_free_f *free; + + /* private fields */ + void *priv; +}; diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 379caf45..2f5a8fd6 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -42,21 +42,16 @@ struct storage { unsigned char *ptr; unsigned len; void *priv; + struct stevedore *stevedore; }; -typedef void storage_init_f(void); -typedef struct storage *storage_alloc_f(unsigned size); -typedef void storage_free_f(struct storage *); - -struct stevedore { - const char *name; - storage_init_f *init; - storage_alloc_f *alloc; - storage_free_f *free; -}; - -extern struct stevedore sma_stevedore; +#include "_stevedore.h" +/* + * XXX: in the longer term, we want to support multiple stevedores, + * XXX: selected by some kind of heuristics based on size, lifetime + * XXX: etc etc. For now we support only one. + */ extern struct stevedore *stevedore; /* Prototypes etc ----------------------------------------------------*/ diff --git a/varnish-cache/bin/varnishd/cache_fetch.c b/varnish-cache/bin/varnishd/cache_fetch.c index dbcd6c60..75c02e4d 100644 --- a/varnish-cache/bin/varnishd/cache_fetch.c +++ b/varnish-cache/bin/varnishd/cache_fetch.c @@ -33,7 +33,7 @@ fetch_straight(struct worker *w, struct sess *sp, int fd, struct http *hp, char cl = strtoumax(b, NULL, 0); - st = stevedore->alloc(cl); + st = stevedore->alloc(stevedore, cl); TAILQ_INSERT_TAIL(&sp->obj->store, st, list); st->len = cl; sp->obj->len = cl; @@ -104,7 +104,7 @@ fetch_chunked(struct worker *w, struct sess *sp, int fd, struct http *hp) q++; if (u == 0) break; - st = stevedore->alloc(u); + st = stevedore->alloc(stevedore, u); TAILQ_INSERT_TAIL(&sp->obj->store, st, list); st->len = u; sp->obj->len += u; diff --git a/varnish-cache/bin/varnishd/cache_main.c b/varnish-cache/bin/varnishd/cache_main.c index 1c3c232f..50023b07 100644 --- a/varnish-cache/bin/varnishd/cache_main.c +++ b/varnish-cache/bin/varnishd/cache_main.c @@ -125,9 +125,9 @@ child_main(void) if (hash->init != NULL) hash->init(); - stevedore = &sma_stevedore; - if (stevedore->init != NULL) - stevedore->init(); + stevedore = heritage.stevedore; + if (stevedore->open != NULL) + stevedore->open(stevedore); CVCL_Load(heritage.vcl_file, "boot"); cli = cli_setup(eb, heritage.fds[2], heritage.fds[1], 0, cli_proto); diff --git a/varnish-cache/bin/varnishd/heritage.h b/varnish-cache/bin/varnishd/heritage.h index 018cb054..8aad4824 100644 --- a/varnish-cache/bin/varnishd/heritage.h +++ b/varnish-cache/bin/varnishd/heritage.h @@ -18,15 +18,18 @@ struct heritage { * interface IP number). */ #define HERITAGE_NSOCKS 2 /* IPv4 + IPv6 */ - int sock_local[HERITAGE_NSOCKS]; - int sock_remote[HERITAGE_NSOCKS]; + int sock_local[HERITAGE_NSOCKS]; + int sock_remote[HERITAGE_NSOCKS]; /* Share memory log fd and size (incl header) */ - int vsl_fd; - unsigned vsl_size; + int vsl_fd; + unsigned vsl_size; /* Initial VCL file */ - char *vcl_file; + char *vcl_file; + + /* Storage method */ + struct stevedore *stevedore; }; extern struct heritage heritage; diff --git a/varnish-cache/bin/varnishd/mgt.h b/varnish-cache/bin/varnishd/mgt.h index c4b855ea..9cc269e6 100644 --- a/varnish-cache/bin/varnishd/mgt.h +++ b/varnish-cache/bin/varnishd/mgt.h @@ -13,3 +13,7 @@ void mgt_child_request(mgt_ccb_f *, void *, char **argv, const char *fmt, ...); /* tcp.c */ int open_tcp(const char *port); + +#include "_stevedore.h" + +extern struct stevedore sma_stevedore; diff --git a/varnish-cache/bin/varnishd/storage_malloc.c b/varnish-cache/bin/varnishd/storage_malloc.c index d67ed50b..01d73217 100644 --- a/varnish-cache/bin/varnishd/storage_malloc.c +++ b/varnish-cache/bin/varnishd/storage_malloc.c @@ -17,7 +17,7 @@ struct sma { }; static struct storage * -sma_alloc(unsigned size) +sma_alloc(struct stevedore *st __unused, unsigned size) { struct sma *sma; @@ -41,8 +41,9 @@ sma_free(struct storage *s) } struct stevedore sma_stevedore = { - "Malloc", + "malloc", NULL, /* init */ + NULL, /* open */ sma_alloc, sma_free }; diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index aabc1492..7324a6a9 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -277,10 +277,12 @@ static void usage(void) { fprintf(stderr, "usage: varnishd [options]\n"); - fprintf(stderr, " %-20s # %s\n", "-b", "backend_IP_number"); - fprintf(stderr, " %-20s # %s\n", "-d", "debug"); - fprintf(stderr, " %-20s # %s\n", "-f", "VCL_file"); - fprintf(stderr, " %-20s # %s\n", "-p number", "TCP listen port"); + fprintf(stderr, " %-28s # %s\n", "-b", "backend_IP_number"); + fprintf(stderr, " %-28s # %s\n", "-d", "debug"); + fprintf(stderr, " %-28s # %s\n", "-f", "VCL_file"); + fprintf(stderr, " %-28s # %s\n", "-p number", "TCP listen port"); + fprintf(stderr, " %-28s # %s\n", + "-s kind[,storageoptions]", "Backend storage specification"); #if 0 -c clusterid@cluster_controller -m memory_limit @@ -294,6 +296,37 @@ usage(void) /*--------------------------------------------------------------------*/ +static int +cmp_storage(struct stevedore *s, const char *p, const char *q) +{ + if (strlen(s->name) != q - p) + return (1); + if (strncmp(s->name, p, q - p)) + return (1); + return (0); +} + +static void +setup_storage(const char *sflag) +{ + const char *p; + + p = strchr(sflag, ','); + if (p == NULL) + p = strchr(sflag, '\0'); + if (!cmp_storage(&sma_stevedore, sflag, p)) { + heritage.stevedore = &sma_stevedore; + } else { + fprintf(stderr, "Unknown storage method \"%*.*s\"\n", + p - sflag, p - sflag, sflag); + exit (2); + } + if (heritage.stevedore->init != NULL) + heritage.stevedore->init(heritage.stevedore, p); +} + +/*--------------------------------------------------------------------*/ + #include "shmlog.h" static void @@ -324,6 +357,7 @@ init_vsl(const char *fn, unsigned size) AZ(ftruncate(heritage.vsl_fd, sizeof slh + size)); heritage.vsl_size = slh.size + slh.start; } + /*--------------------------------------------------------------------*/ /* for development purposes */ @@ -338,6 +372,7 @@ main(int argc, char *argv[]) unsigned dflag = 1; /* XXX: debug=on for now */ const char *bflag = NULL; const char *fflag = NULL; + const char *sflag = "malloc"; register_printf_render_std((const unsigned char *)"HVQ"); @@ -357,6 +392,9 @@ main(int argc, char *argv[]) case 'p': portnumber = optarg; break; + case 's': + sflag = optarg; + break; default: usage(); } @@ -385,6 +423,8 @@ main(int argc, char *argv[]) if (heritage.vcl_file == NULL) exit (1); + setup_storage(sflag); + /* * XXX: Lacking the suspend/resume facility (due to the socket API * missing an unlisten(2) facility) we may want to push this into @@ -398,6 +438,5 @@ main(int argc, char *argv[]) testme(); - exit(0); } -- 2.39.5