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 1128
VFP_Error(struct vfp_ctx *vc, const char *fmt, ...)
51
{
52
        va_list ap;
53
54 1128
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
55 1128
        if (!vc->failed) {
56 696
                va_start(ap, fmt);
57 696
                VSLbv(vc->wrk->vsl, SLT_FetchError, fmt, ap);
58 696
                va_end(ap);
59 696
                vc->failed = 1;
60
        }
61 1128
        return (VFP_ERROR);
62
}
63
64
/*--------------------------------------------------------------------
65
 * Fetch Storage to put object into.
66
 *
67
 */
68
69
enum vfp_status
70 673238
VFP_GetStorage(struct vfp_ctx *vc, ssize_t *sz, uint8_t **ptr)
71
{
72
        ssize_t l;
73
74 673238
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
75 673238
        AN(sz);
76 673238
        assert(*sz >= 0);
77 673238
        AN(ptr);
78
79 673238
        l = fetchfrag;
80 673238
        if (l == 0)
81 672002
                l = *sz;
82 673238
        if (l == 0)
83 647523
                l = cache_param->fetch_chunksize;
84 673238
        *sz = l;
85 673238
        if (!ObjGetSpace(vc->wrk, vc->oc, sz, ptr)) {
86 60
                *sz = 0;
87 60
                *ptr = NULL;
88 60
                return (VFP_Error(vc, "Could not get storage"));
89
        }
90 673178
        assert(*sz > 0);
91 673178
        AN(*ptr);
92 673178
        return (VFP_OK);
93
}
94
95
void
96 658872
VFP_Extend(const struct vfp_ctx *vc, ssize_t sz)
97
{
98 658872
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
99
100 658872
        ObjExtend(vc->wrk, vc->oc, sz);
101 658871
}
102
103
/**********************************************************************
104
 */
105
106
void
107 20508
VFP_Setup(struct vfp_ctx *vc, struct worker *wrk)
108
{
109
110 20508
        INIT_OBJ(vc, VFP_CTX_MAGIC);
111 20508
        VTAILQ_INIT(&vc->vfp);
112 20508
        vc->wrk = wrk;
113 20508
}
114
115
/**********************************************************************
116
 */
117
118
void
119 18552
VFP_Close(struct vfp_ctx *vc)
120
{
121
        struct vfp_entry *vfe;
122
123 33072
        VTAILQ_FOREACH(vfe, &vc->vfp, list) {
124 14520
                if (vfe->vfp->fini != NULL)
125 3060
                        vfe->vfp->fini(vc, vfe);
126 14520
                VSLb(vc->wrk->vsl, SLT_VfpAcct, "%s %ju %ju", vfe->vfp->name,
127 14520
                    (uintmax_t)vfe->calls, (uintmax_t)vfe->bytes_out);
128
        }
129 18552
}
130
131
int
132 18371
VFP_Open(struct vfp_ctx *vc)
133
{
134
        struct vfp_entry *vfe;
135
136 18371
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
137 18371
        CHECK_OBJ_NOTNULL(vc->resp, HTTP_MAGIC);
138 18371
        CHECK_OBJ_NOTNULL(vc->wrk, WORKER_MAGIC);
139 18371
        AN(vc->wrk->vsl);
140
141 32879
        VTAILQ_FOREACH_REVERSE(vfe, &vc->vfp, vfp_entry_s, list) {
142 14519
                if (vfe->vfp->init == NULL)
143 11580
                        continue;
144 2939
                if (DO_DEBUG(DBG_PROCESSORS))
145 0
                        VSLb(vc->wrk->vsl, SLT_Debug, "VFP_Open(%s)",
146 0
                             vfe->vfp->name);
147 2940
                vfe->closed = vfe->vfp->init(vc, vfe);
148 2940
                if (vfe->closed != VFP_OK && vfe->closed != VFP_NULL) {
149 12
                        (void)VFP_Error(vc, "Fetch filter %s failed to open",
150 12
                            vfe->vfp->name);
151 12
                        VFP_Close(vc);
152 12
                        return (-1);
153
                }
154
        }
155
156 18360
        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 1309436
VFP_Suck(struct vfp_ctx *vc, void *p, ssize_t *lp)
167
{
168
        enum vfp_status vp;
169
        struct vfp_entry *vfe;
170
171 1309436
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
172 1309436
        AN(p);
173 1309436
        AN(lp);
174 1309436
        vfe = vc->vfp_nxt;
175 1309436
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
176 1309436
        vc->vfp_nxt = VTAILQ_NEXT(vfe, list);
177
178 1309436
        if (vfe->closed == VFP_NULL) {
179
                /* Layer asked to be bypassed when opened */
180 48
                vp = VFP_Suck(vc, p, lp);
181 1309388
        } else if (vfe->closed == VFP_OK) {
182 1308776
                vp = vfe->vfp->pull(vc, vfe, p, lp);
183 1308776
                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 1308776
                        vfe->bytes_out += *lp;
188 1308776
                vfe->closed = vp;
189 1308776
                vfe->calls++;
190
        } else {
191
                /* Already closed filter */
192 612
                *lp = 0;
193 612
                vp = vfe->closed;
194
        }
195 1309436
        vc->vfp_nxt = vfe;
196 1309436
        return (vp);
197
}
198
199
/*--------------------------------------------------------------------
200
 */
201
202
struct vfp_entry *
203 14976
VFP_Push(struct vfp_ctx *vc, const struct vfp *vfp)
204
{
205
        struct vfp_entry *vfe;
206
207 14976
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
208 14976
        CHECK_OBJ_NOTNULL(vc->resp, HTTP_MAGIC);
209
210 14976
        vfe = WS_Alloc(vc->resp->ws, sizeof *vfe);
211 14975
        if (vfe == NULL) {
212 216
                (void)VFP_Error(vc, "Workspace overflow");
213 216
                return (NULL);
214
        }
215
216 14759
        INIT_OBJ(vfe, VFP_ENTRY_MAGIC);
217 14759
        vfe->vfp = vfp;
218 14759
        vfe->closed = VFP_OK;
219 14759
        VTAILQ_INSERT_HEAD(&vc->vfp, vfe, list);
220 14759
        vc->vfp_nxt = vfe;
221 14759
        return (vfe);
222
}
223
224
/*--------------------------------------------------------------------
225
 * Debugging aids
226
 */
227
228
static void v_matchproto_(cli_func_t)
229 72
debug_fragfetch(struct cli *cli, const char * const *av, void *priv)
230
{
231
        (void)priv;
232
        (void)cli;
233 72
        fetchfrag = strtoul(av[2], NULL, 0);
234 72
}
235
236
static struct cli_proto debug_cmds[] = {
237
        { CLICMD_DEBUG_FRAGFETCH,               "d", debug_fragfetch },
238
        { NULL }
239
};
240
241
/*--------------------------------------------------------------------
242
 *
243
 */
244
245
void
246 8220
VFP_Init(void)
247
{
248
249 8220
        CLI_AddFuncs(debug_cmds);
250 8220
}