varnish-cache/bin/varnishd/cache/cache_fetch_proc.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2015 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <stdlib.h>
33
34
#include "cache_varnishd.h"
35
#include "cache_filter.h"
36
#include "vcli_serve.h"
37
38
static unsigned fetchfrag;
39
40
/*--------------------------------------------------------------------
41
 * We want to issue the first error we encounter on fetching and
42
 * supress the rest.  This function does that.
43
 *
44
 * Other code is allowed to look at busyobj->fetch_failed to bail out
45
 *
46
 * For convenience, always return VFP_ERROR
47
 */
48
49
enum vfp_status
50 73
VFP_Error(struct vfp_ctx *vc, const char *fmt, ...)
51
{
52
        va_list ap;
53
54 73
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
55 73
        if (!vc->failed) {
56 39
                va_start(ap, fmt);
57 39
                VSLbv(vc->wrk->vsl, SLT_FetchError, fmt, ap);
58 39
                va_end(ap);
59 39
                vc->failed = 1;
60
        }
61 73
        return (VFP_ERROR);
62
}
63
64
/*--------------------------------------------------------------------
65
 * Fetch Storage to put object into.
66
 *
67
 */
68
69
enum vfp_status
70 55827
VFP_GetStorage(struct vfp_ctx *vc, ssize_t *sz, uint8_t **ptr)
71
{
72
        ssize_t l;
73
74 55827
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
75 55827
        AN(sz);
76 55827
        assert(*sz >= 0);
77 55827
        AN(ptr);
78
79 55827
        l = fetchfrag;
80 55827
        if (l == 0)
81 55724
                l = *sz;
82 55827
        if (l == 0)
83 53796
                l = cache_param->fetch_chunksize;
84 55827
        *sz = l;
85 55827
        if (!ObjGetSpace(vc->wrk, vc->oc, sz, ptr)) {
86 5
                *sz = 0;
87 5
                *ptr = NULL;
88 5
                return (VFP_Error(vc, "Could not get storage"));
89
        }
90 55822
        assert(*sz > 0);
91 55822
        AN(*ptr);
92 55822
        return (VFP_OK);
93
}
94
95
void
96 54688
VFP_Extend(const struct vfp_ctx *vc, ssize_t sz)
97
{
98 54688
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
99
100 54688
        ObjExtend(vc->wrk, vc->oc, sz);
101 54689
}
102
103
/**********************************************************************
104
 */
105
106
void
107 1399
VFP_Setup(struct vfp_ctx *vc, struct worker *wrk)
108
{
109
110 1399
        INIT_OBJ(vc, VFP_CTX_MAGIC);
111 1399
        VTAILQ_INIT(&vc->vfp);
112 1399
        vc->wrk = wrk;
113 1399
}
114
115
/**********************************************************************
116
 */
117
118
void
119 1284
VFP_Close(struct vfp_ctx *vc)
120
{
121
        struct vfp_entry *vfe;
122
123 2302
        VTAILQ_FOREACH(vfe, &vc->vfp, list) {
124 1017
                if (vfe->vfp->fini != NULL)
125 226
                        vfe->vfp->fini(vc, vfe);
126 1017
                VSLb(vc->wrk->vsl, SLT_VfpAcct, "%s %ju %ju", vfe->vfp->name,
127
                    (uintmax_t)vfe->calls, (uintmax_t)vfe->bytes_out);
128
        }
129 1285
}
130
131
int
132 1272
VFP_Open(struct vfp_ctx *vc)
133
{
134
        struct vfp_entry *vfe;
135
136 1272
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
137 1272
        CHECK_OBJ_NOTNULL(vc->resp, HTTP_MAGIC);
138 1272
        CHECK_OBJ_NOTNULL(vc->wrk, WORKER_MAGIC);
139 1272
        AN(vc->wrk->vsl);
140
141 2289
        VTAILQ_FOREACH_REVERSE(vfe, &vc->vfp, vfp_entry_s, list) {
142 1017
                if (vfe->vfp->init == NULL)
143 791
                        continue;
144 226
                if (DO_DEBUG(DBG_PROCESSORS))
145 0
                        VSLb(vc->wrk->vsl, SLT_Debug, "VFP_Open(%s)",
146 0
                             vfe->vfp->name);
147 226
                vfe->closed = vfe->vfp->init(vc, vfe);
148 226
                if (vfe->closed != VFP_OK && vfe->closed != VFP_NULL) {
149 0
                        (void)VFP_Error(vc, "Fetch filter %s failed to open",
150 0
                            vfe->vfp->name);
151 0
                        VFP_Close(vc);
152 0
                        return (-1);
153
                }
154
        }
155
156 1272
        return (0);
157
}
158
159
/**********************************************************************
160
 * Suck data up from lower levels.
161
 * Once a layer return non VFP_OK, clean it up and produce the same
162
 * return value for any subsequent calls.
163
 */
164
165
enum vfp_status
166 108862
VFP_Suck(struct vfp_ctx *vc, void *p, ssize_t *lp)
167
{
168
        enum vfp_status vp;
169
        struct vfp_entry *vfe;
170
171 108862
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
172 108862
        AN(p);
173 108862
        AN(lp);
174 108862
        vfe = vc->vfp_nxt;
175 108862
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
176 108862
        vc->vfp_nxt = VTAILQ_NEXT(vfe, list);
177
178 108862
        if (vfe->closed == VFP_NULL) {
179
                /* Layer asked to be bypassed when opened */
180 0
                vp = VFP_Suck(vc, p, lp);
181 108862
        } else if (vfe->closed == VFP_OK) {
182 108817
                vp = vfe->vfp->pull(vc, vfe, p, lp);
183 108817
                if (vp != VFP_OK && vp != VFP_END && vp != VFP_ERROR)
184 0
                        vp = VFP_Error(vc, "Fetch filter %s returned %d",
185 0
                            vfe->vfp->name, vp);
186
                else
187 108817
                        vfe->bytes_out += *lp;
188 108817
                vfe->closed = vp;
189 108817
                vfe->calls++;
190
        } else {
191
                /* Already closed filter */
192 45
                *lp = 0;
193 45
                vp = vfe->closed;
194
        }
195 108862
        vc->vfp_nxt = vfe;
196 108862
        return (vp);
197
}
198
199
/*--------------------------------------------------------------------
200
 */
201
struct vfp_entry *
202 1035
VFP_Push(struct vfp_ctx *vc, const struct vfp *vfp)
203
{
204
        struct vfp_entry *vfe;
205
206 1035
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
207 1035
        CHECK_OBJ_NOTNULL(vc->resp, HTTP_MAGIC);
208
209 1035
        vfe = WS_Alloc(vc->resp->ws, sizeof *vfe);
210 1035
        if (vfe == NULL) {
211 1
                (void)VFP_Error(vc, "Workspace overflow");
212 1
                return (NULL);
213
        }
214
215 1034
        INIT_OBJ(vfe, VFP_ENTRY_MAGIC);
216 1034
        vfe->vfp = vfp;
217 1034
        vfe->closed = VFP_OK;
218 1034
        VTAILQ_INSERT_HEAD(&vc->vfp, vfe, list);
219 1034
        vc->vfp_nxt = vfe;
220 1034
        return (vfe);
221
}
222
223
/*--------------------------------------------------------------------
224
 * Debugging aids
225
 */
226
227
static void v_matchproto_(cli_func_t)
228 6
debug_fragfetch(struct cli *cli, const char * const *av, void *priv)
229
{
230
        (void)priv;
231
        (void)cli;
232 6
        fetchfrag = strtoul(av[2], NULL, 0);
233 6
}
234
235
static struct cli_proto debug_cmds[] = {
236
        { CLICMD_DEBUG_FRAGFETCH,               "d", debug_fragfetch },
237
        { NULL }
238
};
239
240
/*--------------------------------------------------------------------
241
 *
242
 */
243
244
void
245 614
VFP_Init(void)
246
{
247
248 614
        CLI_AddFuncs(debug_cmds);
249 614
}