From: phk Date: Tue, 22 Jan 2008 09:55:36 +0000 (+0000) Subject: Make it possible to refer to backend hosts (== simple backends) by name. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f7b8fc2310c5781008be6634dd4d524c4c949a7f;p=varnish Make it possible to refer to backend hosts (== simple backends) by name. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2365 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/include/vrt.h b/varnish-cache/include/vrt.h index 94ba4571..25abbc1d 100644 --- a/varnish-cache/include/vrt.h +++ b/varnish-cache/include/vrt.h @@ -43,6 +43,7 @@ struct sockaddr; struct vrt_backend_host { const char *portname; const char *hostname; + const char *ident; }; struct vrt_simple_backend { diff --git a/varnish-cache/lib/libvcl/vcc_backend.c b/varnish-cache/lib/libvcl/vcc_backend.c index 0a8f7afe..fd41543d 100644 --- a/varnish-cache/lib/libvcl/vcc_backend.c +++ b/varnish-cache/lib/libvcl/vcc_backend.c @@ -69,19 +69,19 @@ CheckHostPort(const char *host, const char *port) */ static void -vcc_EmitBeIdent(struct tokenlist *tl, struct token *first, struct token *last) +vcc_EmitBeIdent(struct vsb *v, const struct token *first, const struct token *last) { - Fc(tl, 0, "\t.ident ="); + vsb_printf(v, "\t.ident ="); while (first != last) { if (first->dec != NULL) - Fc(tl, 0, "\n\t \"\\\"\" %.*s \"\\\" \"", + vsb_printf(v, "\n\t \"\\\"\" %.*s \"\\\" \"", PF(first)); else - Fc(tl, 0, "\n\t \"%.*s \"", PF(first)); + vsb_printf(v, "\n\t \"%.*s \"", PF(first)); first = VTAILQ_NEXT(first, list); } - Fc(tl, 0, ",\n"); + vsb_printf(v, ",\n"); } /*-------------------------------------------------------------------- @@ -111,12 +111,27 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh) struct token *t_host = NULL, *t_fhost = NULL; struct token *t_port = NULL, *t_fport = NULL; const char *ep; + struct host *h; t_first = tl->t; *nbh = tl->nbackend_host++; if (tl->t->tok == ID) { - ErrInternal(tl); /* Reference by name */ + VTAILQ_FOREACH(h, &tl->hosts, list) { + if (vcc_Teq(h->name, tl->t)) + break; + } + if (h == NULL) { + vsb_printf(tl->sb, "Reference to unknown backend "); + vcc_ErrToken(tl, tl->t); + vsb_printf(tl->sb, " at\n"); + vcc_ErrWhere(tl, tl->t); + return; + } + vcc_NextToken(tl); + ExpectErr(tl, ';'); + vcc_NextToken(tl); + *nbh = h->hnum; return; } ExpectErr(tl, '{'); @@ -151,6 +166,7 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh) ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); if (t_host != NULL) { + assert(t_fhost != NULL); vsb_printf(tl->sb, "Multiple .host fields in backend: "); vcc_ErrToken(tl, t_field); @@ -167,6 +183,7 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh) ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); if (t_port != NULL) { + assert(t_fport != NULL); vsb_printf(tl->sb, "Multiple .port fields in backend: "); vcc_ErrToken(tl, t_field); @@ -223,6 +240,7 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh) } ExpectErr(tl, '}'); + vcc_EmitBeIdent(tl->fh, t_first, tl->t); Fh(tl, 0, "};\n"); vcc_NextToken(tl); } @@ -230,36 +248,40 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh) void vcc_ParseSimpleBackend(struct tokenlist *tl) { - struct token *t_be = NULL; struct token *t_first; + struct host *h; int nbh; + h = TlAlloc(tl, sizeof *h); t_first = tl->t; /* T_BACKEND */ vcc_NextToken(tl); ExpectErr(tl, ID); /* name */ - t_be = tl->t; + h->name = tl->t; vcc_NextToken(tl); vcc_ParseBackendHost(tl, &nbh); ERRCHK(tl); + h->hnum = nbh; + VTAILQ_INSERT_TAIL(&tl->hosts, h, list); + /* In the compiled vcl we use these macros to refer to backends */ Fh(tl, 1, "\n#define VGC_backend_%.*s (VCL_conf.backend[%d])\n", - PF(t_be), tl->nbackend); + PF(h->name), tl->nbackend); - vcc_AddDef(tl, t_be, R_BACKEND); + vcc_AddDef(tl, h->name, R_BACKEND); Fi(tl, 0, "\tVRT_init_simple_backend(&VGC_backend_%.*s , &sbe_%.*s);\n", - PF(t_be), PF(t_be)); - Ff(tl, 0, "\tVRT_fini_backend(VGC_backend_%.*s);\n", PF(t_be)); + PF(h->name), PF(h->name)); + Ff(tl, 0, "\tVRT_fini_backend(VGC_backend_%.*s);\n", PF(h->name)); Fc(tl, 0, "\nstatic const struct vrt_simple_backend sbe_%.*s = {\n", - PF(t_be)); - Fc(tl, 0, "\t.name = \"%.*s\",\n", PF(t_be)); + PF(h->name)); + Fc(tl, 0, "\t.name = \"%.*s\",\n", PF(h->name)); Fc(tl, 0, "\t.host = &bh_%d,\n", nbh); - vcc_EmitBeIdent(tl, t_first, tl->t); + vcc_EmitBeIdent(tl->fc, t_first, tl->t); Fc(tl, 0, "};\n"); tl->nbackend++; diff --git a/varnish-cache/lib/libvcl/vcc_compile.c b/varnish-cache/lib/libvcl/vcc_compile.c index 9f6932d2..a188e9f8 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.c +++ b/varnish-cache/lib/libvcl/vcc_compile.c @@ -478,6 +478,7 @@ vcc_NewTokenList(void) tl = calloc(sizeof *tl, 1); assert(tl != NULL); + VTAILQ_INIT(&tl->hosts); VTAILQ_INIT(&tl->membits); VTAILQ_INIT(&tl->tokens); VTAILQ_INIT(&tl->refs); diff --git a/varnish-cache/lib/libvcl/vcc_compile.h b/varnish-cache/lib/libvcl/vcc_compile.h index 42c019b0..65cd678f 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.h +++ b/varnish-cache/lib/libvcl/vcc_compile.h @@ -59,12 +59,19 @@ struct token { char *dec; }; +struct host { + VTAILQ_ENTRY(host) list; + unsigned hnum; + struct token *name; +}; + VTAILQ_HEAD(tokenhead, token); struct tokenlist { struct tokenhead tokens; VTAILQ_HEAD(, source) sources; VTAILQ_HEAD(, membit) membits; + VTAILQ_HEAD(, host) hosts; unsigned nsources; struct source *src; struct token *t; diff --git a/varnish-cache/lib/libvcl/vcc_fixed_token.c b/varnish-cache/lib/libvcl/vcc_fixed_token.c index 0aaba94f..27a8eb9d 100644 --- a/varnish-cache/lib/libvcl/vcc_fixed_token.c +++ b/varnish-cache/lib/libvcl/vcc_fixed_token.c @@ -423,6 +423,7 @@ vcl_output_lang_h(struct vsb *sb) vsb_cat(sb, "struct vrt_backend_host {\n"); vsb_cat(sb, " const char *portname;\n"); vsb_cat(sb, " const char *hostname;\n"); + vsb_cat(sb, " const char *ident;\n"); vsb_cat(sb, "};\n"); vsb_cat(sb, "\n"); vsb_cat(sb, "struct vrt_simple_backend {\n");