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