[master] da539902c via backends in VCL

Nils Goroll nils.goroll at uplex.de
Mon Feb 20 15:38:06 UTC 2023


commit da539902c2c8842305a944b56dff105e9ba4a6c8
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Fri Mar 22 17:32:36 2019 +0100

    via backends in VCL
    
    Due to the intialization order, only native vcl backends can be used
    from vcl as via backends. As directors are defined in vcl_init which
    gets to run only after the native backends have been initialized,
    directors can not be refered to from a backend.

diff --git a/bin/varnishtest/tests/c00042.vtc b/bin/varnishtest/tests/c00042.vtc
new file mode 100644
index 000000000..9bd29ccfd
--- /dev/null
+++ b/bin/varnishtest/tests/c00042.vtc
@@ -0,0 +1,63 @@
+varnishtest "Test vcl defined via backends"
+
+server s1 {
+	rxreq
+	txresp
+	rxreq
+	txresp
+} -start
+
+# the use case for via-proxy is to have a(n ha)proxy make a(n ssl)
+# connection on our behalf. For the purpose of testing, we use another
+# varnish in place - but we are behaving realistically in that we do
+# not use any prior information for the actual backend connection -
+# just the information from the proxy protocol
+
+varnish v2 -proto PROXY -vcl {
+	import debug;
+	import std;
+
+	backend dummy { .host = "${bad_backend}"; }
+
+	sub vcl_init {
+		new s1 = debug.dyn("0.0.0.0", "0");
+	}
+
+	sub vcl_recv {
+		s1.refresh(server.ip, std.port(server.ip));
+		set req.backend_hint = s1.backend();
+		return (pass);
+	}
+} -start
+
+varnish v1 -vcl {
+	import debug;
+	import vtc;
+
+	backend dummy { .host = "${bad_backend}"; }
+
+	backend v2 { .host = "${v2_addr}"; .port = "${v2_port}"; }
+	backend s1 { .via = v2; .host = "${s1_addr}"; .port = "${s1_port}"; }
+
+	sub vcl_recv {
+		set req.backend_hint = s1;
+	}
+} -start
+
+client c1 {
+	txreq -url /1
+	rxresp
+	expect resp.status == 200
+	txreq -url /2
+	rxresp
+	expect resp.status == 200
+} -run
+
+varnish v1 -errvcl "Cannot set both .via and .path" {
+	backend v2 { .host = "${v2_addr}"; .port = "${v2_port}"; }
+
+	backend s1 {
+		.via = v2;
+		.path = "/path/to/uds";
+	}
+}
diff --git a/lib/libvcc/vcc_backend.c b/lib/libvcc/vcc_backend.c
index ad694f11d..a062e1cb5 100644
--- a/lib/libvcc/vcc_backend.c
+++ b/lib/libvcc/vcc_backend.c
@@ -378,6 +378,7 @@ vcc_ParseHostDef(struct vcc *tl, const struct token *t_be, const char *vgcname)
 	struct fld_spec *fs;
 	struct inifin *ifp;
 	struct vsb *vsb1;
+	struct symbol *via = NULL;
 	char *p;
 	unsigned u;
 	double t;
@@ -418,6 +419,7 @@ vcc_ParseHostDef(struct vcc *tl, const struct token *t_be, const char *vgcname)
 	    "?max_connections",
 	    "?proxy_header",
 	    "?preamble",
+	    "?via",
 	    NULL);
 
 	tl->fb = VSB_new_auto();
@@ -538,6 +540,13 @@ vcc_ParseHostDef(struct vcc *tl, const struct token *t_be, const char *vgcname)
 			t_preamble = tl->t;
 			vcc_NextToken(tl);
 			SkipToken(tl, ';');
+		} else if (vcc_IdIs(t_field, "via")) {
+			via = VCC_SymbolGet(tl, SYM_MAIN, SYM_BACKEND,
+			    SYMTAB_EXISTING, XREF_REF);
+			ERRCHK(tl);
+			AN(via);
+			AN(via->rname);
+			SkipToken(tl, ';');
 		} else {
 			ErrInternal(tl);
 			VSB_destroy(&tl->fb);
@@ -559,6 +568,12 @@ vcc_ParseHostDef(struct vcc *tl, const struct token *t_be, const char *vgcname)
 		return;
 	}
 
+	if (via != NULL && t_path != NULL) {
+		VSB_printf(tl->sb, "Cannot set both .via and .path.\n");
+		vcc_ErrWhere(tl, t_be);
+		return;
+	}
+
 	vsb1 = VSB_new_auto();
 	AN(vsb1);
 	VSB_printf(vsb1,
@@ -606,8 +621,8 @@ vcc_ParseHostDef(struct vcc *tl, const struct token *t_be, const char *vgcname)
 	ifp = New_IniFin(tl);
 	VSB_printf(ifp->ini,
 	    "\t%s =\n\t    VRT_new_backend_clustered(ctx, vsc_cluster,\n"
-	    "\t\t&vgc_dir_priv_%s, NULL);\n",
-	    vgcname, vgcname);
+	    "\t\t&vgc_dir_priv_%s, %s);\n",
+	    vgcname, vgcname, via ? via->rname : "NULL");
 	VSB_printf(ifp->ini,
 	    "\tif (%s)\n\t\tVRT_StaticDirector(%s);", vgcname, vgcname);
 	VSB_printf(ifp->fin, "\t\tVRT_delete_backend(ctx, &%s);", vgcname);


More information about the varnish-commit mailing list