varnish-cache/lib/libvcc/vcc_storage.c
1
/*-
2
 * Copyright (c) 2010-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * All stuff related to the storage.* part of the namespace.
29
 *
30
 * "All" is actually only a wildcard function, which instantiates variables
31
 * on demand under the storage.* tree of the namespace.
32
 *
33
 * About the syntax:
34
 * -----------------
35
 *
36
 * One of our long term goals is to have dynamic storage configuration, such
37
 * as the ability to add or remove a stevedore on the fly, without restarting
38
 * the worker process.
39
 *
40
 * Even though this goal is far out in the future, it influences the syntax
41
 * design of storage selection from VCL.
42
 *
43
 * In difference from backends, where we know the possible set of backends at
44
 * compile time, we will not in the future know the identity of the stevedores
45
 * available at compile time, so we have to rely on VRT name resolution.
46
 *
47
 * This indicates a namespace on the form storage.<stevedore>.<property>
48
 *
49
 * For each property, we must define a default value if the named stevedore
50
 * does not exists, such that for instance stevedore.forgetit.freespace
51
 * returns zero etc.
52
 *
53
 */
54
55
#include "config.h"
56
57
#include <stdarg.h>
58
#include <stdlib.h>
59
#include <string.h>
60
61
#include "vcc_compile.h"
62
#include "libvcc.h"             // VCC_Stevedore() proto
63
64
/*--------------------------------------------------------------------
65
 *
66
 */
67
68
static struct stvars {
69
        const char      *name;
70
        vcc_type_t      fmt;
71
} stvars[] = {
72
#define VRTSTVVAR(nm, vtype, ctype, dval)       { #nm, vtype },
73
#include "tbl/vrt_stv_var.h"
74
#undef VRTSTVVAR
75
        { NULL,                 BOOL }
76
};
77
78
void
79 1820
vcc_stevedore(struct vcc *vcc, const char *stv_name)
80
{
81
        struct symbol *sym;
82
        struct stvars *sv;
83
        char buf[1024];
84
85 1820
        CHECK_OBJ_NOTNULL(vcc, VCC_MAGIC);
86 1820
        bprintf(buf, "storage.%s", stv_name);
87 1820
        sym = VCC_Symbol(vcc, NULL, buf, NULL, SYM_VAR, 1);
88 1820
        AN(sym);
89 1820
        sym->fmt = STEVEDORE;
90 1820
        sym->eval = vcc_Eval_Var;
91 1820
        bprintf(buf, "VRT_stevedore(\"%s\")", stv_name);
92 1820
        sym->rname = TlDup(vcc, buf);
93 1820
        sym->r_methods = ~0;
94
95 7280
        for (sv = stvars; sv->name != NULL; sv++) {
96 5460
                bprintf(buf, "storage.%s.%s", stv_name, sv->name);
97 5460
                sym = VCC_Symbol(vcc, NULL, buf, NULL, SYM_VAR, 1);
98 5460
                AN(sym);
99 5460
                sym->fmt = sv->fmt;
100 5460
                sym->eval = vcc_Eval_Var;
101 5460
                bprintf(buf, "VRT_Stv_%s(\"%s\")", sv->name, stv_name);
102 5460
                sym->rname = TlDup(vcc, buf);
103 5460
                sym->r_methods = ~0;
104
        }
105 1820
}