Code Coverage: vinyl-cache/bin/vinyld/cache/cache_deliver_proc.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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
#include "config.h"
32
33
#include "cache_int.h"
34
#include "cache_filter.h"
35
#include "cache_objhead.h"
36
37
void
38
VDP_Panic(struct vsb *vsb, const struct vdp_ctx *vdc)
39 6
{
40 6
        struct vdp_entry *vde;
41
42 6
        if (PAN_dump_struct(vsb, vdc, VDP_CTX_MAGIC, "vdc"))
43 4
                return;
44 2
        VSB_printf(vsb, "nxt = %p,\n", vdc->nxt);
45 2
        VSB_printf(vsb, "retval = %d,\n", vdc->retval);
46
47 2
        if (!VTAILQ_EMPTY(&vdc->vdp)) {
48 1
                VSB_cat(vsb, "filters = {\n");
49 1
                VSB_indent(vsb, 2);
50 1
                VTAILQ_FOREACH(vde, &vdc->vdp, list)
51 4
                        VSB_printf(vsb, "%s = %p { priv = %p }\n",
52 4
                            vde->vdp->name, vde, vde->priv);
53 1
                VSB_indent(vsb, -2);
54 1
                VSB_cat(vsb, "},\n");
55 1
        }
56
57 2
        VSB_indent(vsb, -2);
58 2
        VSB_cat(vsb, "},\n");
59 2
}
60
61
/*
62
 * Ensure that transports have called VDP_Close()
63
 * to avoid leaks in VDPs
64
 */
65
void
66
VDP_Fini(const struct vdp_ctx *vdc)
67 9103
{
68 9103
        assert(VTAILQ_EMPTY(&vdc->vdp));
69 9103
}
70
71
void
72
VDP_Init(struct vdp_ctx *vdc, struct worker *wrk, struct vsl_log *vsl,
73
    const struct req *req, const struct busyobj *bo, intmax_t *clen)
74 12382
{
75 12382
        AN(vdc);
76 12382
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
77 12382
        AN(vsl);
78
79 12382
        AN(clen);
80
81 12382
        assert((req ? 1 : 0) ^ (bo ? 1 : 0));
82
83 12382
        AN(clen);
84 12382
        assert(*clen >= -1);
85
86 12382
        INIT_OBJ(vdc, VDP_CTX_MAGIC);
87 12382
        VTAILQ_INIT(&vdc->vdp);
88 12382
        vdc->wrk = wrk;
89 12382
        vdc->vsl = vsl;
90 12382
        vdc->clen = clen;
91
92 12382
        if (req != NULL) {
93 9530
                CHECK_OBJ(req, REQ_MAGIC);
94 9530
                vdc->oc = req->objcore;
95 9530
                vdc->hp = req->resp;
96 9530
        }
97 2852
        else {
98 2852
                CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
99 2852
                vdc->oc = bo->bereq_body;
100 2852
                vdc->hp = bo->bereq;
101 2852
        }
102 12382
}
103
104
/* VDP_bytes
105
 *
106
 * Pushes len bytes at ptr down the delivery processor list.
107
 *
108
 * This function picks and calls the next delivery processor from the
109
 * list. The return value is the return value of the delivery
110
 * processor. Upon seeing a non-zero return value, that lowest value
111
 * observed is latched in ->retval and all subsequent calls to
112
 * VDP_bytes will return that value directly without calling the next
113
 * processor.
114
 *
115
 * VDP_END marks the end of successful processing, it is issued by
116
 * VDP_DeliverObj() and may also be sent downstream by processors ending the
117
 * stream (for return value != 0)
118
 *
119
 * VDP_END must at most be received once per processor, so any VDP sending it
120
 * downstream must itself not forward it a second time.
121
 *
122
 * Valid return values (of VDP_bytes and any VDP function):
123
 * r < 0:  Error, breaks out early on an error condition
124
 * r == 0: Continue
125
 * r > 0:  Stop, breaks out early without error condition
126
 */
127
128
int
129
VDP_bytes(struct vdp_ctx *vdc, enum vdp_action act,
130
    const void *ptr, ssize_t len)
131 92556
{
132 92556
        int retval;
133 92556
        struct vdp_entry *vdpe;
134
135 92556
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
136 92556
        if (vdc->retval)
137 25
                return (vdc->retval);
138 92531
        vdpe = vdc->nxt;
139 92531
        CHECK_OBJ_NOTNULL(vdpe, VDP_ENTRY_MAGIC);
140
141
        /* at most one VDP_END call */
142 92531
        assert(vdpe->end == VDP_NULL);
143
144 92531
        if (act == VDP_NULL)
145 28982
                assert(len > 0);
146 63549
        else if (act == VDP_END)
147 8291
                vdpe->end = VDP_END;
148 55258
        else
149 55258
                assert(act == VDP_FLUSH);
150
151
        /* Call the present layer, while pointing to the next layer down */
152 92531
        vdc->nxt = VTAILQ_NEXT(vdpe, list);
153 92531
        vdpe->calls++;
154 92531
        vdc->bytes_done = len;
155 92531
        retval = vdpe->vdp->bytes(vdc, act, &vdpe->priv, ptr, len);
156 92531
        vdpe->bytes_in += vdc->bytes_done;
157 92531
        if (retval && (vdc->retval == 0 || retval < vdc->retval))
158 63
                vdc->retval = retval; /* Latch error value */
159 92531
        vdc->nxt = vdpe;
160 92531
        return (vdc->retval);
161 92531
}
162
163
int
164
VDP_Push(VRT_CTX, struct vdp_ctx *vdc, struct ws *ws, const struct vdp *vdp,
165
    void *priv)
166 11303
{
167 11303
        struct vdp_entry *vdpe;
168
169 11303
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
170 11303
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
171 11303
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
172 11303
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
173 11303
        AN(vdc->clen);
174 11303
        assert(*vdc->clen >= -1);
175 11303
        AN(ws);
176 11303
        AN(vdp);
177 11303
        AN(vdp->name);
178
179 11303
        if (vdc->retval)
180 0
                return (vdc->retval);
181
182 11303
        if (DO_DEBUG(DBG_PROCESSORS))
183 49
                VSLb(vdc->vsl, SLT_Debug, "VDP_push(%s)", vdp->name);
184
185 11303
        vdpe = WS_Alloc(ws, sizeof *vdpe);
186 11303
        if (vdpe == NULL) {
187 128
                AZ(vdc->retval);
188 128
                vdc->retval = -1;
189 128
                return (vdc->retval);
190 128
        }
191 11175
        INIT_OBJ(vdpe, VDP_ENTRY_MAGIC);
192 11175
        vdpe->vdp = vdp;
193 11175
        vdpe->priv = priv;
194 11175
        VTAILQ_INSERT_TAIL(&vdc->vdp, vdpe, list);
195 11175
        vdc->nxt = VTAILQ_FIRST(&vdc->vdp);
196
197 11175
        AZ(vdc->retval);
198 11175
        if (vdpe->vdp->init != NULL)
199 6942
                vdc->retval = vdpe->vdp->init(ctx, vdc, &vdpe->priv);
200 11175
        vdc->oc = NULL;
201
202 11175
        if (vdc->retval) {
203 64
                VTAILQ_REMOVE(&vdc->vdp, vdpe, list);
204 64
                vdc->nxt = VTAILQ_FIRST(&vdc->vdp);
205 64
                vdc->retval = vdc->retval > 0 ? 0 : vdc->retval;
206 64
        } else
207 11111
                AN(vdp->bytes);
208 11175
        return (vdc->retval);
209 11175
}
210
211
uint64_t
212
VDP_Close(struct vdp_ctx *vdc, struct objcore *oc, struct boc *boc)
213 11407
{
214 11407
        struct vdp_entry *vdpe;
215 11407
        uint64_t rv = 0;
216
217 11407
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
218 11407
        CHECK_OBJ_NOTNULL(vdc->wrk, WORKER_MAGIC);
219 11407
        CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
220 11407
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
221
222 22498
        while ((vdpe = VTAILQ_FIRST(&vdc->vdp)) != NULL) {
223 11091
                CHECK_OBJ(vdpe, VDP_ENTRY_MAGIC);
224 11091
                rv = vdpe->bytes_in;
225 11091
                VSLb(vdc->vsl, SLT_VdpAcct, "%s %ju %ju", vdpe->vdp->name,
226 11091
                    (uintmax_t)vdpe->calls, (uintmax_t)rv);
227 11091
                if (vdpe->vdp->fini != NULL)
228 5668
                        AZ(vdpe->vdp->fini(vdc, &vdpe->priv));
229 11091
                AZ(vdpe->priv);
230 11091
                VTAILQ_REMOVE(&vdc->vdp, vdpe, list);
231 11091
                vdc->nxt = VTAILQ_FIRST(&vdc->vdp);
232
#ifdef VDP_PEDANTIC_ARMED
233
                // enable when we are confident to get VDP_END right
234
                if (vdc->nxt == NULL && vdc->retval >= 0)
235
                        assert(vdpe->end == VDP_END);
236
#endif
237 11091
        }
238 11407
        if (oc != NULL)
239 8554
                HSH_Cancel(vdc->wrk, oc, boc);
240 11407
        return (rv);
241 11407
}
242
243
/*--------------------------------------------------------------------*/
244
245
/*
246
 * Push a VDPIO vdp. This can only be used with only vdpio-enabled VDPs or
247
 * after a successful upgrade
248
 */
249
int
250
VDPIO_Push(VRT_CTX, struct vdp_ctx *vdc, struct ws *ws, const struct vdp *vdp,
251
    void *priv)
252 1104
{
253 1104
        struct vdp_entry *vdpe;
254 1104
        int r;
255
256 1104
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
257 1104
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
258 1104
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
259 1104
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
260 1104
        AN(vdc->clen);
261 1104
        assert(*vdc->clen >= -1);
262 1104
        AN(ws);
263 1104
        AN(vdp);
264 1104
        AN(vdp->name);
265
266 1104
        if (vdc->retval < 0)
267 0
                return (vdc->retval);
268
269 1104
        AN(vdp->io_init);
270
271
        // the first VDP (which leases from storage) only gets the minimum
272
        // capacity requirement of 1
273 1104
        if (vdc->retval == 0) {
274 976
                assert(VTAILQ_EMPTY(&vdc->vdp));
275 976
                vdc->retval = 1;
276 976
        }
277
278 1104
        if (DO_DEBUG(DBG_PROCESSORS))
279 128
                VSLb(vdc->vsl, SLT_Debug, "VDPIO_push(%s)", vdp->name);
280
281 1104
        vdpe = WS_Alloc(ws, sizeof *vdpe);
282 1104
        if (vdpe == NULL) {
283 0
                vdc->retval = -ENOMEM;
284 0
                return (vdc->retval);
285 0
        }
286 1104
        INIT_OBJ(vdpe, VDP_ENTRY_MAGIC);
287 1104
        vdpe->vdp = vdp;
288 1104
        vdpe->priv = priv;
289 1104
        VTAILQ_INSERT_TAIL(&vdc->vdp, vdpe, list);
290 1104
        vdc->nxt = VTAILQ_FIRST(&vdc->vdp);
291
292 1104
        assert(vdc->retval > 0);
293 1104
        if (vdpe->vdp->io_init != NULL) {
294 1104
                r = vdpe->vdp->io_init(ctx, vdc, &vdpe->priv, vdc->retval);
295 1104
                if (r <= 0) {
296 0
                        VTAILQ_REMOVE(&vdc->vdp, vdpe, list);
297 0
                        vdc->nxt = VTAILQ_FIRST(&vdc->vdp);
298 0
                }
299 1104
                else
300 1104
                        AN(vdp->io_lease);
301 1104
                if (r != 0)
302 1104
                        vdc->retval = r;
303 1104
        }
304 1104
        vdc->oc = NULL;
305 1104
        return (vdc->retval);
306 1104
}
307
308
/*
309
 * upgrade an already initialized VDP filter chain to VDPIO, if possible
310
 * returns:
311
 * > 0 cap
312
 * -ENOTSUP io_upgrade missing for at least one filter
313
 * vdc->retval if < 0
314
 */
315
int
316
VDPIO_Upgrade(VRT_CTX, struct vdp_ctx *vdc)
317 40
{
318 40
        struct vdp_entry *vdpe;
319 40
        int cap, r;
320
321 40
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
322 40
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
323
324 40
        VTAILQ_FOREACH(vdpe, &vdc->vdp, list)
325 32
                if (vdpe->vdp->io_upgrade == NULL)
326 8
                        return (-ENOTSUP);
327
328 32
        if (vdc->retval < 0)
329 0
                return (vdc->retval);
330
331
        // minimum capacity requirement for the first filter (after storage)
332 32
        r = cap = 1;
333 32
        VTAILQ_FOREACH(vdpe, &vdc->vdp, list) {
334 16
                r = vdpe->vdp->io_upgrade(ctx, vdc, &vdpe->priv, cap);
335 16
                if (DO_DEBUG(DBG_PROCESSORS)) {
336 16
                        VSLb(vdc->vsl, SLT_Debug, "VDPIO_Upgrade "
337 16
                            "%d = %s(cap = %d)",
338 16
                            r, vdpe->vdp->name, cap);
339 16
                }
340 16
                if (r < 0)
341 0
                        return ((vdc->retval = r));
342
                // XXX remove if filter does not want to be pushed?
343 16
                assert(r != 0);
344 16
                cap = r;
345 16
        }
346 32
        return ((vdc->retval = r));
347 32
}
348
349
uint64_t
350
VDPIO_Close1(struct vdp_ctx *vdc, struct vdp_entry *vdpe)
351 1120
{
352 1120
        uint64_t rv;
353
354 1120
        CHECK_OBJ_NOTNULL(vdpe, VDP_ENTRY_MAGIC);
355 1120
        rv = vdpe->bytes_in;
356 1120
        VSLb(vdc->vsl, SLT_VdpAcct, "%s %ju %ju", vdpe->vdp->name,
357 1120
            (uintmax_t)vdpe->calls, (uintmax_t)rv);
358 1120
        if (vdpe->vdp->io_fini != NULL)
359 880
                vdpe->vdp->io_fini(vdc, &vdpe->priv);
360 1120
        AZ(vdpe->priv);
361 1120
        VTAILQ_REMOVE(&vdc->vdp, vdpe, list);
362 1120
        vdc->nxt = VTAILQ_FIRST(&vdc->vdp);
363 1120
        return (rv);
364 1120
}
365
366
uint64_t
367
VDPIO_Close(struct vdp_ctx *vdc, struct objcore *oc, struct boc *boc)
368 1008
{
369 1008
        struct vdp_entry *vdpe;
370 1008
        uint64_t rv = 0;
371
372 1008
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
373 1008
        CHECK_OBJ_NOTNULL(vdc->wrk, WORKER_MAGIC);
374 1008
        CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
375 1008
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
376
377 2096
        while ((vdpe = VTAILQ_FIRST(&vdc->vdp)) != NULL)
378 1088
                rv = VDPIO_Close1(vdc, vdpe);
379
380 1008
        if (oc != NULL)
381 32
                HSH_Cancel(vdc->wrk, oc, boc);
382 1008
        return (rv);
383 1008
}
384
385
/*
386
 * ============================================================
387
 * VDPIO helpers: VAI management
388
 *
389
 * Transports should not need to talk to the VAI Object interface directly,
390
 * because its state is kept in the vdp_ctx
391
 *
392
 * So we wrap init, return and fini
393
 */
394
395
// return true if error
396
int
397
VDPIO_Init(struct vdp_ctx *vdc, struct objcore *oc, struct ws *ws,
398
    vai_notify_cb *notify_cb, void *notify_priv, struct vscaret *scaret)
399 1008
{
400 1008
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
401 1008
        VSCARET_CHECK_NOTNULL(scaret);
402 1008
        AN(scaret->capacity);
403 1008
        AZ(scaret->used);
404
405 1008
        AZ(vdc->vai_hdl);
406 1008
        vdc->vai_hdl = ObjVAIinit(vdc->wrk, oc, ws, notify_cb, notify_priv);
407 1008
        if (vdc->vai_hdl == NULL)
408 0
                return (1);
409 1008
        vdc->scaret = scaret;
410 1008
        return (0);
411 1008
}
412
413
// return leases stashed in scaret
414
void
415
VDPIO_Return(const struct vdp_ctx *vdc)
416 1136
{
417 1136
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
418
419 1136
        ObjVAIreturn(vdc->wrk, vdc->vai_hdl, vdc->scaret);
420 1136
}
421
422
// Notify after a VDP's io_lease has return -EAGAIN
423
// usually not called from the io thread, so it has a separate wrk
424
void
425
VDPIO_Notify(struct worker *wrk, const struct vdp_ctx *vdc)
426 128
{
427 128
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
428
429 128
        ObjVAInotify(wrk, vdc->vai_hdl);
430 128
}
431
432
void
433
VDPIO_Fini(struct vdp_ctx *vdc)
434 1008
{
435 1008
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
436
437 1008
        VDPIO_Return(vdc);
438 1008
        ObjVAIfini(vdc->wrk, &vdc->vai_hdl);
439 1008
}
440
441
/*--------------------------------------------------------------------*/
442
int v_matchproto_(objiterate_f)
443
VDP_ObjIterate(void *priv, unsigned flush, const void *ptr, ssize_t len)
444 18568
{
445 18568
        enum vdp_action act;
446
447 18568
        if (flush == 0)
448 9120
                act = VDP_NULL;
449 9448
        else if ((flush & OBJ_ITER_END) != 0)
450 7211
                act = VDP_END;
451 2237
        else
452 2237
                act = VDP_FLUSH;
453
454 18568
        return (VDP_bytes(priv, act, ptr, len));
455 18568
}
456
457
458
int
459
VDP_DeliverObj(struct vdp_ctx *vdc, struct objcore *oc)
460 7011
{
461 7011
        int r, final;
462
463 7011
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
464 7011
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
465 7011
        CHECK_OBJ_NOTNULL(vdc->wrk, WORKER_MAGIC);
466 7011
        AN(vdc->vsl);
467 7011
        AZ(vdc->oc);
468 7011
        vdc->hp = NULL;
469 7011
        vdc->clen = NULL;
470 7011
        final = oc->flags & OC_F_TRANSIENT ? 1 : 0;
471 7011
        r = ObjIterate(vdc->wrk, oc, vdc, VDP_ObjIterate, final);
472 7011
        if (r < 0)
473 49
                return (r);
474 6962
        return (0);
475 7011
}