varnish-cache/lib/libvcc/vcc_backend.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 */
31
32
#include "config.h"
33
34
#include <math.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <sys/stat.h>
38
39
#include "vcc_compile.h"
40
#include "vus.h"
41
42
/*--------------------------------------------------------------------
43
 * Struct sockaddr is not really designed to be a compile time
44
 * initialized data structure, so we encode it as a byte-string
45
 * and put it in an official sockaddr when we load the VCL.
46
 */
47
48
static void
49 5356
Emit_Sockaddr(struct vcc *tl, struct vsb *vsb1, const struct token *t_host,
50
    const struct token *t_port)
51
{
52
        const char *ipv4, *ipv4a, *ipv6, *ipv6a, *pa;
53
        char buf[BUFSIZ];
54
55 5356
        AN(t_host->dec);
56
57 5356
        if (t_port != NULL)
58 4068
                bprintf(buf, "%s %s", t_host->dec, t_port->dec);
59
        else
60 1288
                bprintf(buf, "%s", t_host->dec);
61 10712
        Resolve_Sockaddr(tl, buf, "80",
62 5356
            &ipv4, &ipv4a, &ipv6, &ipv6a, &pa, 2, t_host, "Backend host");
63 5356
        ERRCHK(tl);
64 5348
        if (ipv4 != NULL) {
65 10648
                VSB_printf(vsb1,
66
                    "\t.ipv4 = (const struct suckaddr *)%s,\n",
67 5324
                    ipv4);
68 5324
        }
69 5348
        if (ipv6 != NULL) {
70 56
                VSB_printf(vsb1,
71
                    "\t.ipv6 = (const struct suckaddr *)%s,\n",
72 28
                    ipv6);
73 28
        }
74 5348
        VSB_cat(vsb1, "\t.uds_path = (void *) 0,\n");
75 5356
}
76
77
/*
78
 * For UDS, we do not create a VSA. We run the VUS_resolver() checks and, if
79
 * it's a path, can be accessed, and is a socket. If so, just emit the path
80
 * field and set the IP suckaddrs to NULL.
81
 */
82
83
static int
84 128
uds_resolved(void *priv, const struct sockaddr_un *uds)
85
{
86 128
        (void) priv;
87 128
        (void) uds;
88 128
        return (42);
89
}
90
91
static void
92 124
emit_path(struct vsb *vsb1, char *path)
93
{
94 124
        VSB_printf(vsb1, "\t.uds_path = \"%s\",\n", path);
95 124
        VSB_cat(vsb1, "\t.ipv4 = (void *) 0,\n");
96 124
        VSB_cat(vsb1, "\t.ipv6 = (void *) 0,\n");
97 124
}
98
99
static void
100 136
Emit_UDS_Path(struct vcc *tl, struct vsb *vsb1,
101
    const struct token *t_path, const char *errid)
102
{
103
        struct stat st;
104
        const char *vus_err;
105
106 136
        AN(t_path);
107 136
        AN(t_path->dec);
108
109 136
        if (! VUS_is(t_path->dec)) {
110 8
                VSB_printf(tl->sb,
111
                           "%s: Must be a valid path or abstract socket:\n",
112 4
                           errid);
113 4
                vcc_ErrWhere(tl, t_path);
114 4
                return;
115
        }
116 132
        if (VUS_resolver(t_path->dec, uds_resolved, NULL, &vus_err) != 42) {
117 4
                VSB_printf(tl->sb, "%s: %s\n", errid, vus_err);
118 4
                vcc_ErrWhere(tl, t_path);
119 4
                return;
120
        }
121 128
        if (*t_path->dec == '@') {
122 0
                emit_path(vsb1, t_path->dec);
123 0
                return;
124
        }
125 128
        assert(*t_path->dec == '/');
126 128
        errno = 0;
127 128
        if (stat(t_path->dec, &st) != 0) {
128 4
                int err = errno;
129 8
                VSB_printf(tl->sb, "%s: Cannot stat: %s\n", errid,
130 4
                           strerror(errno));
131 4
                vcc_ErrWhere(tl, t_path);
132 4
                if (err == ENOENT || err == EACCES)
133 4
                        vcc_Warn(tl);
134
                else
135 0
                        return;
136 128
        } else if (!S_ISSOCK(st.st_mode)) {
137 4
                VSB_printf(tl->sb, "%s: Not a socket:\n", errid);
138 4
                vcc_ErrWhere(tl, t_path);
139 4
                return;
140
        }
141 124
        emit_path(vsb1, t_path->dec);
142 136
}
143
144
/*--------------------------------------------------------------------
145
 * Disallow mutually exclusive field definitions
146
 */
147
148
static void
149 5632
vcc_Redef(struct vcc *tl, const char *redef, const struct token **t_did,
150
    const struct token *t_field)
151
{
152 5632
        if (*t_did != NULL) {
153 12
                VSB_printf(tl->sb, "%s redefinition at:\n", redef);
154 12
                vcc_ErrWhere(tl, t_field);
155 12
                VSB_cat(tl->sb, "Previous definition:\n");
156 12
                vcc_ErrWhere(tl, *t_did);
157 12
                return;
158
        }
159 5620
        *t_did = t_field;
160 5632
}
161
162
/*--------------------------------------------------------------------
163
 * Parse a backend probe specification
164
 */
165
166
static void
167 188
vcc_ParseProbeSpec(struct vcc *tl, const struct symbol *sym, char **namep)
168
{
169
        struct fld_spec *fs;
170
        const struct token *t_field;
171 188
        const struct token *t_did = NULL, *t_window = NULL, *t_threshold = NULL;
172 188
        struct token *t_initial = NULL;
173
        unsigned window, threshold, initial, status, exp_close;
174
        char buf[32];
175
        const char *name;
176
        double t;
177
178 188
        fs = vcc_FldSpec(tl,
179
            "?url",
180
            "?request",
181
            "?expected_response",
182
            "?timeout",
183
            "?interval",
184
            "?window",
185
            "?threshold",
186
            "?initial",
187
            "?expect_close",
188
            NULL);
189
190 188
        SkipToken(tl, '{');
191
192 188
        if (sym != NULL) {
193 84
                name = sym->rname;
194 84
        } else {
195 104
                bprintf(buf, "vgc_probe__%d", tl->nprobe++);
196 104
                name = buf;
197
        }
198 188
        Fh(tl, 0, "static const struct vrt_backend_probe %s[] = {{\n", name);
199 188
        Fh(tl, 0, "\t.magic = VRT_BACKEND_PROBE_MAGIC,\n");
200 188
        if (namep != NULL)
201 188
                *namep = TlDup(tl, name);
202
203 188
        window = 0;
204 188
        threshold = 0;
205 188
        initial = 0;
206 188
        status = 0;
207 188
        exp_close = 1;
208 628
        while (tl->t->tok != '}') {
209
210 464
                vcc_IsField(tl, &t_field, fs);
211 464
                ERRCHK(tl);
212 464
                if (vcc_IdIs(t_field, "url")) {
213 52
                        vcc_Redef(tl, "Probe request", &t_did, t_field);
214 52
                        ERRCHK(tl);
215 48
                        ExpectErr(tl, CSTR);
216 48
                        Fh(tl, 0, "\t.url = ");
217 48
                        EncToken(tl->fh, tl->t);
218 48
                        Fh(tl, 0, ",\n");
219 48
                        vcc_NextToken(tl);
220 460
                } else if (vcc_IdIs(t_field, "request")) {
221 20
                        vcc_Redef(tl, "Probe request", &t_did, t_field);
222 20
                        ERRCHK(tl);
223 16
                        ExpectErr(tl, CSTR);
224 16
                        Fh(tl, 0, "\t.request =\n");
225 64
                        while (tl->t->tok == CSTR) {
226 48
                                Fh(tl, 0, "\t\t");
227 48
                                EncToken(tl->fh, tl->t);
228 48
                                Fh(tl, 0, " \"\\r\\n\"\n");
229 48
                                vcc_NextToken(tl);
230
                        }
231 16
                        Fh(tl, 0, "\t\t\"\\r\\n\",\n");
232 408
                } else if (vcc_IdIs(t_field, "timeout")) {
233 40
                        Fh(tl, 0, "\t.timeout = ");
234 40
                        vcc_Duration(tl, &t);
235 40
                        ERRCHK(tl);
236 36
                        Fh(tl, 0, "%g,\n", t);
237 388
                } else if (vcc_IdIs(t_field, "interval")) {
238 104
                        Fh(tl, 0, "\t.interval = ");
239 104
                        vcc_Duration(tl, &t);
240 104
                        ERRCHK(tl);
241 104
                        Fh(tl, 0, "%g,\n", t);
242 352
                } else if (vcc_IdIs(t_field, "window")) {
243 80
                        t_window = tl->t;
244 80
                        window = vcc_UintVal(tl);
245 80
                        ERRCHK(tl);
246 248
                } else if (vcc_IdIs(t_field, "initial")) {
247 56
                        t_initial = tl->t;
248 56
                        initial = vcc_UintVal(tl);
249 56
                        ERRCHK(tl);
250 168
                } else if (vcc_IdIs(t_field, "expected_response")) {
251 8
                        status = vcc_UintVal(tl);
252 8
                        if (status < 100 || status > 999) {
253 4
                                VSB_cat(tl->sb,
254
                                    "Must specify .expected_response with "
255
                                    "exactly three digits "
256
                                    "(100 <= x <= 999)\n");
257 4
                                vcc_ErrWhere(tl, tl->t);
258 4
                                return;
259
                        }
260 4
                        ERRCHK(tl);
261 108
                } else if (vcc_IdIs(t_field, "threshold")) {
262 88
                        t_threshold = tl->t;
263 88
                        threshold = vcc_UintVal(tl);
264 88
                        ERRCHK(tl);
265 104
                } else if (vcc_IdIs(t_field, "expect_close")) {
266 16
                        exp_close = vcc_BoolVal(tl);
267 16
                        ERRCHK(tl);
268 8
                } else {
269 0
                        vcc_ErrToken(tl, t_field);
270 0
                        vcc_ErrWhere(tl, t_field);
271 0
                        ErrInternal(tl);
272 0
                        return;
273
                }
274
275 440
                SkipToken(tl, ';');
276
        }
277 164
        free(fs);
278
279 164
        if (t_threshold != NULL || t_window != NULL) {
280 92
                if (t_threshold == NULL && t_window != NULL) {
281 4
                        VSB_cat(tl->sb,
282
                            "Must specify .threshold with .window\n");
283 4
                        vcc_ErrWhere(tl, t_window);
284 4
                        return;
285 88
                } else if (t_threshold != NULL && t_window == NULL) {
286 12
                        if (threshold > 64) {
287 4
                                VSB_cat(tl->sb,
288
                                    "Threshold must be 64 or less.\n");
289 4
                                vcc_ErrWhere(tl, t_threshold);
290 4
                                return;
291
                        }
292 8
                        window = threshold + 1;
293 84
                } else if (window > 64) {
294 4
                        AN(t_window);
295 4
                        VSB_cat(tl->sb, "Window must be 64 or less.\n");
296 4
                        vcc_ErrWhere(tl, t_window);
297 4
                        return;
298
                }
299 80
                if (threshold > window ) {
300 4
                        VSB_cat(tl->sb,
301
                            "Threshold cannot be greater than window.\n");
302 4
                        AN(t_threshold);
303 4
                        vcc_ErrWhere(tl, t_threshold);
304 4
                        AN(t_window);
305 4
                        vcc_ErrWhere(tl, t_window);
306 4
                }
307 80
                Fh(tl, 0, "\t.window = %u,\n", window);
308 80
                Fh(tl, 0, "\t.threshold = %u,\n", threshold);
309 80
        }
310 152
        if (t_initial != NULL)
311 56
                Fh(tl, 0, "\t.initial = %u,\n", initial);
312
        else
313 96
                Fh(tl, 0, "\t.initial = ~0U,\n");
314 152
        if (status > 0)
315 4
                Fh(tl, 0, "\t.exp_status = %u,\n", status);
316 152
        Fh(tl, 0, "\t.exp_close = %u,\n", exp_close);
317 152
        Fh(tl, 0, "}};\n");
318 152
        SkipToken(tl, '}');
319 188
}
320
321
/*--------------------------------------------------------------------
322
 * Parse and emit a probe definition
323
 */
324
325
void
326 88
vcc_ParseProbe(struct vcc *tl)
327
{
328
        struct symbol *sym;
329
        char *p;
330
331 88
        vcc_NextToken(tl);                      /* ID: probe */
332
333 88
        vcc_ExpectVid(tl, "backend probe");     /* ID: name */
334 88
        ERRCHK(tl);
335
336 88
        sym = VCC_HandleSymbol(tl, PROBE);
337 88
        ERRCHK(tl);
338 84
        AN(sym);
339 84
        vcc_ParseProbeSpec(tl, sym, &p);
340
341 84
        if (sym->type == DEFAULT)
342 40
                tl->default_probe = p;
343
        else
344 44
                free(p);
345 88
}
346
347
/*--------------------------------------------------------------------
348
 * Parse and emit a backend host definition
349
 *
350
 * The struct vrt_backend is emitted to Fh().
351
 */
352
353
static void
354 6524
vcc_ParseHostDef(struct vcc *tl, struct symbol *sym,
355
    const struct token *t_be, const char *vgcname)
356
{
357
        const struct token *t_field;
358
        const struct token *t_val;
359
        const struct token *t_probe;
360 6524
        const struct token *t_host = NULL;
361 6524
        const struct token *t_port = NULL;
362 6524
        const struct token *t_path = NULL;
363 6524
        const struct token *t_hosthdr = NULL;
364 6524
        const struct token *t_authority = NULL;
365 6524
        const struct token *t_did = NULL;
366 6524
        const struct token *t_preamble = NULL;
367
        struct symbol *pb;
368
        struct fld_spec *fs;
369
        struct inifin *ifp;
370
        struct vsb *vsb1;
371 6524
        struct symbol *via = NULL;
372 6524
        vtim_dur connect_timeout = NAN;
373 6524
        vtim_dur first_byte_timeout = NAN;
374 6524
        vtim_dur between_bytes_timeout = NAN;
375 6524
        vtim_dur backend_wait_timeout = NAN;
376
        char *p;
377
        unsigned u;
378
379 6940
        if (tl->t->tok == ID &&
380 948
            (vcc_IdIs(tl->t, "none") || vcc_IdIs(tl->t, "None"))) {
381 948
                vcc_NextToken(tl);
382 948
                SkipToken(tl, ';');
383 948
                ifp = New_IniFin(tl);
384 948
                VSB_printf(ifp->ini, "\t(void)%s;", vgcname);
385 948
                VSB_printf(ifp->fin, "\t\t(void)%s;", vgcname);
386 948
                return;
387
        }
388
389 5576
        SkipToken(tl, '{');
390
391
        /* Check for old syntax */
392 5576
        if (tl->t->tok == ID && vcc_IdIs(tl->t, "set")) {
393 4
                VSB_cat(tl->sb,
394
                    "NB: Backend Syntax has changed:\n"
395
                    "Remove \"set\" and \"backend\" in front"
396
                    " of backend fields.\n" );
397 4
                vcc_ErrToken(tl, tl->t);
398 4
                VSB_cat(tl->sb, " at ");
399 4
                vcc_ErrWhere(tl, tl->t);
400 4
                return;
401
        }
402
403 5572
        fs = vcc_FldSpec(tl,
404
            "?host",
405
            "?port",
406
            "?path",
407
            "?host_header",
408
            "?connect_timeout",
409
            "?first_byte_timeout",
410
            "?between_bytes_timeout",
411
            "?probe",
412
            "?max_connections",
413
            "?proxy_header",
414
            "?preamble",
415
            "?via",
416
            "?authority",
417
            "?wait_timeout",
418
            "?wait_limit",
419
            NULL);
420
421 5572
        tl->fb = VSB_new_auto();
422 5572
        AN(tl->fb);
423
424 11144
        Fb(tl, 0, "\nstatic const struct vrt_backend vgc_dir_priv_%s = {\n",
425 5572
            vgcname);
426
427 5572
        Fb(tl, 0, "\t.magic = VRT_BACKEND_MAGIC,\n");
428 5572
        Fb(tl, 0, "\t.endpoint = &vgc_dir_ep_%s,\n", vgcname);
429 5572
        Fb(tl, 0, "\t.vcl_name = \"%.*s", PF(t_be));
430 5572
        Fb(tl, 0, "\",\n");
431
432 15508
        while (tl->t->tok != '}') {
433
434 10008
                vcc_IsField(tl, &t_field, fs);
435 10008
                ERRCHK(tl);
436 10000
                if (vcc_IdIs(t_field, "host")) {
437 5416
                        vcc_Redef(tl, "Address", &t_did, t_field);
438 5416
                        ERRCHK(tl);
439 5416
                        ExpectErr(tl, CSTR);
440 5416
                        assert(tl->t->dec != NULL);
441 5416
                        t_host = tl->t;
442 5416
                        vcc_NextToken(tl);
443 5416
                        SkipToken(tl, ';');
444 10000
                } else if (vcc_IdIs(t_field, "port")) {
445 4076
                        ExpectErr(tl, CSTR);
446 4076
                        assert(tl->t->dec != NULL);
447 4076
                        t_port = tl->t;
448 4076
                        vcc_NextToken(tl);
449 4076
                        SkipToken(tl, ';');
450 4584
                } else if (vcc_IdIs(t_field, "path")) {
451 148
                        if (tl->syntax < VCL_41) {
452 4
                                VSB_cat(tl->sb,
453
                                    "Unix socket backends only supported"
454
                                    " in VCL4.1 and higher.\n");
455 4
                                vcc_ErrToken(tl, tl->t);
456 4
                                VSB_cat(tl->sb, " at ");
457 4
                                vcc_ErrWhere(tl, tl->t);
458 4
                                VSB_destroy(&tl->fb);
459 4
                                return;
460
                        }
461 144
                        vcc_Redef(tl, "Address", &t_did, t_field);
462 144
                        ERRCHK(tl);
463 140
                        ExpectErr(tl, CSTR);
464 140
                        assert(tl->t->dec != NULL);
465 140
                        t_path = tl->t;
466 140
                        vcc_NextToken(tl);
467 140
                        SkipToken(tl, ';');
468 500
                } else if (vcc_IdIs(t_field, "host_header")) {
469 16
                        ExpectErr(tl, CSTR);
470 16
                        assert(tl->t->dec != NULL);
471 16
                        t_hosthdr = tl->t;
472 16
                        vcc_NextToken(tl);
473 16
                        SkipToken(tl, ';');
474 360
                } else if (vcc_IdIs(t_field, "connect_timeout")) {
475 12
                        Fb(tl, 0, "\t.connect_timeout = ");
476 12
                        vcc_Duration(tl, &connect_timeout);
477 12
                        ERRCHK(tl);
478 12
                        Fb(tl, 0, "%g,\n", connect_timeout);
479 12
                        SkipToken(tl, ';');
480 344
                } else if (vcc_IdIs(t_field, "first_byte_timeout")) {
481 12
                        Fb(tl, 0, "\t.first_byte_timeout = ");
482 12
                        vcc_Duration(tl, &first_byte_timeout);
483 12
                        ERRCHK(tl);
484 12
                        Fb(tl, 0, "%g,\n", first_byte_timeout);
485 12
                        SkipToken(tl, ';');
486 332
                } else if (vcc_IdIs(t_field, "between_bytes_timeout")) {
487 4
                        Fb(tl, 0, "\t.between_bytes_timeout = ");
488 4
                        vcc_Duration(tl, &between_bytes_timeout);
489 4
                        ERRCHK(tl);
490 4
                        Fb(tl, 0, "%g,\n", between_bytes_timeout);
491 4
                        SkipToken(tl, ';');
492 320
                } else if (vcc_IdIs(t_field, "max_connections")) {
493 44
                        u = vcc_UintVal(tl);
494 44
                        ERRCHK(tl);
495 44
                        SkipToken(tl, ';');
496 44
                        Fb(tl, 0, "\t.max_connections = %u,\n", u);
497 316
                } else if (vcc_IdIs(t_field, "proxy_header")) {
498 24
                        t_val = tl->t;
499 24
                        u = vcc_UintVal(tl);
500 24
                        ERRCHK(tl);
501 24
                        if (u != 1 && u != 2) {
502 0
                                VSB_cat(tl->sb,
503
                                    ".proxy_header must be 1 or 2\n");
504 0
                                vcc_ErrWhere(tl, t_val);
505 0
                                VSB_destroy(&tl->fb);
506 0
                                return;
507
                        }
508 24
                        SkipToken(tl, ';');
509 24
                        Fb(tl, 0, "\t.proxy_header = %u,\n", u);
510 272
                } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == '{') {
511 104
                        vcc_ParseProbeSpec(tl, NULL, &p);
512 104
                        Fb(tl, 0, "\t.probe = %s,\n", p);
513 104
                        free(p);
514 104
                        ERRCHK(tl);
515 208
                } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == ID) {
516 44
                        t_probe = tl->t;
517 44
                        pb = VCC_SymbolGet(tl, SYM_MAIN, SYM_PROBE,
518
                            SYMTAB_EXISTING, XREF_REF);
519 44
                        ERRCHK(tl);
520 40
                        AN(pb);
521 40
                        if (pb->type == DEFAULT) {
522 4
                                if (tl->default_probe == NULL) {
523 4
                                        VSB_cat(tl->sb,
524
                                            "No default probe defined\n");
525 4
                                        vcc_ErrToken(tl, t_probe);
526 4
                                        VSB_cat(tl->sb, " at\n");
527 4
                                        vcc_ErrWhere(tl, t_probe);
528 4
                                }
529 4
                                pb = PROBE->default_sym;
530 4
                        }
531 40
                        ERRCHK(tl);
532 36
                        Fb(tl, 0, "\t.probe = %s,\n", pb->rname);
533 36
                        SkipToken(tl, ';');
534 136
                } else if (vcc_IdIs(t_field, "probe")) {
535 4
                        VSB_cat(tl->sb, "Expected '{' or name of probe, got ");
536 4
                        vcc_ErrToken(tl, tl->t);
537 4
                        VSB_cat(tl->sb, " at\n");
538 4
                        vcc_ErrWhere(tl, tl->t);
539 4
                        VSB_destroy(&tl->fb);
540 4
                        return;
541 96
                } else if (vcc_IdIs(t_field, "preamble")) {
542 4
                        ExpectErr(tl, CBLOB);
543 4
                        t_preamble = tl->t;
544 4
                        vcc_NextToken(tl);
545 4
                        SkipToken(tl, ';');
546 96
                } else if (vcc_IdIs(t_field, "via")) {
547 40
                        via = VCC_SymbolGet(tl, SYM_MAIN, SYM_BACKEND,
548
                            SYMTAB_EXISTING, XREF_REF);
549 40
                        ERRCHK(tl);
550 40
                        AN(via);
551 40
                        AN(via->rname);
552
553 40
                        if (via->extra != NULL) {
554 4
                                AZ(strcmp(via->extra, "via"));
555 4
                                VSB_cat(tl->sb,
556
                                        "Cannot stack .via backends at\n");
557 4
                                vcc_ErrWhere(tl, tl->t);
558 4
                                VSB_destroy(&tl->fb);
559 4
                                return;
560
                        }
561
562 36
                        AN(sym);
563 36
                        AZ(sym->extra);
564 36
                        sym->extra = "via";
565 36
                        SkipToken(tl, ';');
566 88
                } else if (vcc_IdIs(t_field, "authority")) {
567 12
                        ExpectErr(tl, CSTR);
568 12
                        assert(tl->t->dec != NULL);
569 12
                        t_authority = tl->t;
570 12
                        vcc_NextToken(tl);
571 12
                        SkipToken(tl, ';');
572 52
                } else if (vcc_IdIs(t_field, "wait_timeout")) {
573 20
                        Fb(tl, 0, "\t.backend_wait_timeout = ");
574 20
                        vcc_Duration(tl, &backend_wait_timeout);
575 20
                        ERRCHK(tl);
576 20
                        Fb(tl, 0, "%g,\n", backend_wait_timeout);
577 20
                        SkipToken(tl, ';');
578 40
                } else if (vcc_IdIs(t_field, "wait_limit")) {
579 20
                        u = vcc_UintVal(tl);
580 20
                        ERRCHK(tl);
581 20
                        SkipToken(tl, ';');
582 20
                        Fb(tl, 0, "\t.backend_wait_limit = %u,\n", u);
583 20
                } else {
584 0
                        ErrInternal(tl);
585 0
                        VSB_destroy(&tl->fb);
586 0
                        return;
587
                }
588
589
        }
590
591 5500
        vcc_FieldsOk(tl, fs);
592 5500
        free(fs);
593 5500
        ERRCHK(tl);
594
595 5500
        if (isnan(connect_timeout))
596 5488
                Fb(tl, 0, "\t.connect_timeout = -1.0,\n");
597 5500
        if (isnan(first_byte_timeout))
598 5488
                Fb(tl, 0, "\t.first_byte_timeout = -1.0,\n");
599 5500
        if (isnan(between_bytes_timeout))
600 5496
                Fb(tl, 0, "\t.between_bytes_timeout = -1.0,\n");
601 5500
        if (isnan(backend_wait_timeout))
602 5480
                Fb(tl, 0, "\t.backend_wait_timeout = -1.0,\n");
603
604 5500
        ExpectErr(tl, '}');
605
606 5500
        if (t_host == NULL && t_path == NULL) {
607 4
                VSB_cat(tl->sb, "Expected .host or .path.\n");
608 4
                vcc_ErrWhere(tl, t_be);
609 4
                VSB_destroy(&tl->fb);
610 4
                return;
611
        }
612
613 5496
        if (via != NULL && t_path != NULL) {
614 4
                VSB_cat(tl->sb, "Cannot set both .via and .path.\n");
615 4
                vcc_ErrWhere(tl, t_be);
616 4
                return;
617
        }
618
619 5492
        if (via != NULL)
620 32
                AZ(via->extra);
621
622 5492
        vsb1 = VSB_new_auto();
623 5492
        AN(vsb1);
624 10984
        VSB_printf(vsb1,
625
            "\nstatic const struct vrt_endpoint vgc_dir_ep_%s = {\n",
626 5492
            vgcname);
627 5492
        VSB_cat(vsb1, "\t.magic = VRT_ENDPOINT_MAGIC,\n");
628
629 5492
        assert(t_host != NULL || t_path != NULL);
630 5492
        if (t_host != NULL)
631
                /* Check that the hostname makes sense */
632 5356
                Emit_Sockaddr(tl, vsb1, t_host, t_port);
633
        else
634
                /* Check that the path can be a legal UDS */
635 136
                Emit_UDS_Path(tl, vsb1, t_path, "Backend path");
636 5492
        ERRCHK(tl);
637
638 5472
        if (t_preamble != NULL)
639 4
                VSB_printf(vsb1, "\t.preamble = %s,\n", t_preamble->dec);
640
641 5472
        VSB_cat(vsb1, "};\n");
642 5472
        AZ(VSB_finish(vsb1));
643 5472
        Fh(tl, 0, "%s", VSB_data(vsb1));
644 5472
        VSB_destroy(&vsb1);
645
646
        /* Emit the hosthdr field, fall back to .host if not specified */
647
        /* If .path is specified, set "0.0.0.0". */
648 5472
        Fb(tl, 0, "\t.hosthdr = ");
649 5472
        if (t_hosthdr != NULL)
650 16
                EncToken(tl->fb, t_hosthdr);
651 5456
        else if (t_host != NULL)
652 5340
                EncToken(tl->fb, t_host);
653
        else
654 116
                Fb(tl, 0, "\"0.0.0.0\"");
655 5472
        Fb(tl, 0, ",\n");
656
657
        /*
658
         * Emit the authority field, falling back to hosthdr, then host.
659
         *
660
         * When authority is "", sending the TLV is disabled.
661
         *
662
         * Falling back to host may result in an IP address in authority,
663
         * which is an illegal SNI HostName (RFC 4366 ch. 3.1). But we
664
         * document the potential error, rather than try to find out
665
         * whether or not Emit_Sockaddr() had to look up a name.
666
         */
667 5472
        if (via != NULL) {
668 32
                AN(t_host);
669 32
                Fb(tl, 0, "\t.authority = ");
670 32
                if (t_authority != NULL)
671 12
                        EncToken(tl->fb, t_authority);
672 20
                else if (t_hosthdr != NULL)
673 4
                        EncToken(tl->fb, t_hosthdr);
674
                else
675 16
                        EncToken(tl->fb, t_host);
676 32
                Fb(tl, 0, ",\n");
677 32
        }
678
679
        /* Close the struct */
680 5472
        Fb(tl, 0, "};\n");
681
682 5472
        vcc_NextToken(tl);
683
684 5472
        AZ(VSB_finish(tl->fb));
685 5472
        Fh(tl, 0, "%s", VSB_data(tl->fb));
686 5472
        VSB_destroy(&tl->fb);
687
688 5472
        ifp = New_IniFin(tl);
689 10944
        VSB_printf(ifp->ini,
690
            "\t%s =\n\t    VRT_new_backend_clustered(ctx, vsc_cluster,\n"
691
            "\t\t&vgc_dir_priv_%s, %s);\n",
692 5472
            vgcname, vgcname, via ? via->rname : "NULL");
693 10944
        VSB_printf(ifp->ini,
694 5472
            "\tif (%s)\n\t\tVRT_StaticDirector(%s);", vgcname, vgcname);
695 5472
        VSB_printf(ifp->fin, "\t\tVRT_delete_backend(ctx, &%s);", vgcname);
696 6524
}
697
698
/*--------------------------------------------------------------------
699
 * Parse directors and backends
700
 */
701
702
void
703 6544
vcc_ParseBackend(struct vcc *tl)
704
{
705
        struct token *t_first, *t_be;
706
        struct symbol *sym;
707
        const char *dn;
708
709 6544
        tl->ndirector++;
710 6544
        t_first = tl->t;
711 6544
        SkipToken(tl, ID);              /* ID: backend */
712
713 6544
        vcc_ExpectVid(tl, "backend");   /* ID: name */
714 6544
        ERRCHK(tl);
715
716 6540
        t_be = tl->t;
717
718 6540
        sym = VCC_HandleSymbol(tl, BACKEND);
719 6540
        ERRCHK(tl);
720 6528
        AN(sym);
721
722 6528
        if (sym->type == DEFAULT) {
723 280
                if (tl->first_director != NULL) {
724 16
                        tl->first_director->noref = 0;
725 16
                        tl->first_director = NULL;
726 16
                        tl->default_director = NULL;
727 16
                }
728 280
                if (tl->default_director != NULL) {
729 4
                        VSB_cat(tl->sb,
730
                            "Only one default director possible.\n");
731 4
                        vcc_ErrWhere(tl, t_first);
732 4
                        return;
733
                }
734 276
                dn = "vgc_backend_default";
735 276
                tl->default_director = dn;
736 276
        } else {
737 6248
                dn = sym->rname;
738 6248
                if (tl->default_director == NULL) {
739 5196
                        tl->first_director = sym;
740 5196
                        tl->default_director = dn;
741 5196
                        sym->noref = 1;
742 5196
                }
743
        }
744
745 6524
        Fh(tl, 0, "\nstatic VCL_BACKEND %s;\n", dn);
746 6524
        vcc_ParseHostDef(tl, sym, t_be, dn);
747 6524
        if (tl->err) {
748 208
                VSB_printf(tl->sb,
749 104
                    "\nIn %.*s specification starting at:\n", PF(t_first));
750 104
                vcc_ErrWhere(tl, t_first);
751 104
                return;
752
        }
753 6544
}
754
755
void
756 12028
vcc_Backend_Init(struct vcc *tl)
757
{
758
        struct inifin *ifp;
759
760 12028
        Fh(tl, 0, "\nstatic struct vsmw_cluster *vsc_cluster;\n");
761 12028
        ifp = New_IniFin(tl);
762 12028
        VSB_cat(ifp->ini, "\tvsc_cluster = VRT_VSM_Cluster_New(ctx,\n"
763
            "\t    ndirector * VRT_backend_vsm_need(ctx));\n");
764 12028
        VSB_cat(ifp->ini, "\tif (vsc_cluster == 0)\n\t\treturn(1);");
765 12028
        VSB_cat(ifp->fin, "\t\tVRT_VSM_Cluster_Destroy(ctx, &vsc_cluster);");
766 12028
}