varnish-cache/bin/varnishd/cache/cache_fetch.c
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_varnishd.h"
34
#include "cache_filter.h"
35
#include "cache_objhead.h"
36
#include "storage/storage.h"
37
#include "vcl.h"
38
#include "vtim.h"
39
#include "vcc_interface.h"
40
41
#define FETCH_STEPS \
42
        FETCH_STEP(mkbereq,           MKBEREQ) \
43
        FETCH_STEP(retry,             RETRY) \
44
        FETCH_STEP(startfetch,        STARTFETCH) \
45
        FETCH_STEP(condfetch,         CONDFETCH) \
46
        FETCH_STEP(fetch,             FETCH) \
47
        FETCH_STEP(fetchbody,         FETCHBODY) \
48
        FETCH_STEP(fetchend,          FETCHEND) \
49
        FETCH_STEP(error,             ERROR) \
50
        FETCH_STEP(fail,              FAIL) \
51
        FETCH_STEP(done,              DONE)
52
53
typedef const struct fetch_step *vbf_state_f(struct worker *, struct busyobj *);
54
55
struct fetch_step {
56
        const char      *name;
57
        vbf_state_f     *func;
58 24282
};
59 24282
60 24282
#define FETCH_STEP(l, U) \
61 24282
    static vbf_state_f vbf_stp_##l; \
62
    static const struct fetch_step F_STP_##U[1] = {{ .name = "Fetch Step " #l, .func = vbf_stp_##l, }};
63
FETCH_STEPS
64
#undef FETCH_STEP
65
66
static hdr_t const H_X_Varnish = HDR("X-Varnish");
67
68
/*--------------------------------------------------------------------
69
 * Allocate an object, with fall-back to Transient.
70
 * XXX: This somewhat overlaps the stuff in stevedore.c
71
 * XXX: Should this be merged over there ?
72
 */
73
74
static int
75 29497
vbf_allocobj(struct busyobj *bo, unsigned l)
76
{
77
        struct objcore *oc;
78
        const struct stevedore *stv;
79
        vtim_dur lifetime;
80
81 29497
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
82 29497
        oc = bo->fetch_objcore;
83 29497
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
84
85 29497
        lifetime = oc->ttl + oc->grace + oc->keep;
86
87 29497
        if (bo->uncacheable) {
88 11674
                stv = stv_transient;
89 11674
                bo->wrk->stats->beresp_uncacheable++;
90 11674
        }
91 17823
        else if (lifetime < cache_param->shortlived) {
92 1651
                stv = stv_transient;
93 1651
                bo->wrk->stats->beresp_shortlived++;
94 1651
        }
95
        else
96 16172
                stv = bo->storage;
97
98 29497
        bo->storage = NULL;
99
100 29497
        if (stv == NULL)
101 13
                return (0);
102
103 29484
        if (STV_NewObject(bo->wrk, oc, stv, l))
104 29393
                return (1);
105
106 91
        if (stv == stv_transient)
107 52
                return (0);
108
109
        /*
110
         * Try to salvage the transaction by allocating a shortlived object
111
         * on Transient storage.
112
         */
113
114 39
        oc->ttl = vmin_t(float, oc->ttl, cache_param->shortlived);
115 39
        oc->grace = 0.0;
116 39
        oc->keep = 0.0;
117 39
        return (STV_NewObject(bo->wrk, oc, stv_transient, l));
118 29497
}
119
120
static void
121 26546
vbf_cleanup(struct busyobj *bo)
122
{
123
        struct vfp_ctx *vfc;
124
125 26546
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
126 26546
        vfc = bo->vfc;
127 26546
        CHECK_OBJ_NOTNULL(vfc, VFP_CTX_MAGIC);
128
129 26546
        bo->acct.beresp_bodybytes += VFP_Close(vfc);
130 26546
        bo->vfp_filter_list = NULL;
131
132 26546
        if (bo->director_state != DIR_S_NULL)
133 26439
                VDI_Finish(bo);
134 26546
}
135
136
void
137 117
Bereq_Rollback(VRT_CTX)
138
{
139
        struct busyobj *bo;
140
141 117
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
142 117
        bo = ctx->bo;
143 117
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
144
145 117
        if (bo->htc != NULL) {
146 104
                assert(bo->htc->body_status != BS_TAKEN);
147 104
                if (bo->htc->body_status != BS_NONE)
148 13
                        bo->htc->doclose = SC_RESP_CLOSE;
149 104
        }
150
151 117
        vbf_cleanup(bo);
152 117
        VCL_TaskLeave(ctx, bo->privs);
153 117
        VCL_TaskEnter(bo->privs);
154 117
        HTTP_Clone(bo->bereq, bo->bereq0);
155 117
        bo->vfp_filter_list = NULL;
156 117
        bo->err_reason = NULL;
157 117
        AN(bo->ws_bo);
158 117
        WS_Rollback(bo->ws, bo->ws_bo);
159 117
}
160
161
/*--------------------------------------------------------------------
162
 * Turn the beresp into a obj
163
 */
164
165
static int
166 29493
vbf_beresp2obj(struct busyobj *bo)
167
{
168
        unsigned l, l2;
169
        const char *b;
170
        uint8_t *bp;
171 29493
        struct vsb *vary = NULL;
172 29493
        int varyl = 0;
173
        struct objcore *oc;
174
175 29493
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
176 29493
        oc = bo->fetch_objcore;
177 29493
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
178
179 29493
        l = 0;
180
181
        /* Create Vary instructions */
182 29493
        if (!(oc->flags & OC_F_PRIVATE)) {
183 19149
                varyl = VRY_Create(bo, &vary);
184 19149
                if (varyl > 0) {
185 2795
                        AN(vary);
186 2795
                        assert(varyl == VSB_len(vary));
187 2795
                        l += PRNDUP((intptr_t)varyl);
188 19149
                } else if (varyl < 0) {
189
                        /*
190
                         * Vary parse error
191
                         * Complain about it, and make this a pass.
192
                         */
193 65
                        VSLb(bo->vsl, SLT_Error,
194
                            "Illegal 'Vary' header from backend, "
195
                            "making this a pass.");
196 65
                        bo->uncacheable = 1;
197 65
                        AZ(vary);
198 65
                } else
199
                        /* No vary */
200 16289
                        AZ(vary);
201 19149
        }
202
203 58986
        l2 = http_EstimateWS(bo->beresp,
204 29493
            bo->uncacheable ? HTTPH_A_PASS : HTTPH_A_INS);
205 29493
        l += l2;
206
207 29493
        if (bo->uncacheable)
208 11670
                oc->flags |= OC_F_HFM;
209
210 29493
        if (!vbf_allocobj(bo, l)) {
211 78
                if (vary != NULL)
212 0
                        VSB_destroy(&vary);
213 78
                AZ(vary);
214 78
                return (VFP_Error(bo->vfc, "Could not get storage"));
215
        }
216
217 29415
        if (vary != NULL) {
218 2795
                AN(ObjSetAttr(bo->wrk, oc, OA_VARY, varyl, VSB_data(vary)));
219 2795
                VSB_destroy(&vary);
220 2795
        }
221
222 29415
        AZ(ObjSetXID(bo->wrk, oc, bo->vsl->wid));
223
224
        /* for HTTP_Encode() VSLH call */
225 29415
        bo->beresp->logtag = SLT_ObjMethod;
226
227
        /* Filter into object */
228 29415
        bp = ObjSetAttr(bo->wrk, oc, OA_HEADERS, l2, NULL);
229 29415
        AN(bp);
230 58830
        HTTP_Encode(bo->beresp, bp, l2,
231 29415
            bo->uncacheable ? HTTPH_A_PASS : HTTPH_A_INS);
232
233 29415
        if (http_GetHdr(bo->beresp, H_Last_Modified, &b))
234 559
                AZ(ObjSetDouble(bo->wrk, oc, OA_LASTMODIFIED, VTIM_parse(b)));
235
        else
236 28856
                AZ(ObjSetDouble(bo->wrk, oc, OA_LASTMODIFIED,
237
                    floor(oc->t_origin)));
238
239 29415
        return (0);
240 29493
}
241
242
/*--------------------------------------------------------------------
243
 * Copy req->bereq and release req if no body
244
 */
245
246
static const struct fetch_step * v_matchproto_(vbf_state_f)
247 29794
vbf_stp_mkbereq(struct worker *wrk, struct busyobj *bo)
248
{
249
        const char *q;
250
        struct objcore *oc;
251
252 29794
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
253 29794
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
254 29794
        CHECK_OBJ_NOTNULL(bo->req, REQ_MAGIC);
255 29794
        oc = bo->fetch_objcore;
256 29794
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
257
258 29794
        assert(oc->boc->state == BOS_INVALID);
259 29794
        AZ(bo->storage);
260
261 29794
        HTTP_Setup(bo->bereq0, bo->ws, bo->vsl, SLT_BereqMethod);
262 59588
        http_FilterReq(bo->bereq0, bo->req->http,
263 29794
            bo->uncacheable ? HTTPH_R_PASS : HTTPH_R_FETCH);
264
265 29794
        if (bo->uncacheable)
266 10333
                AZ(bo->stale_oc);
267
        else {
268 19461
                http_ForceField(bo->bereq0, HTTP_HDR_METHOD, "GET");
269 19461
                if (cache_param->http_gzip_support)
270 19396
                        http_ForceHeader(bo->bereq0, H_Accept_Encoding, "gzip");
271
        }
272 29794
        http_ForceField(bo->bereq0, HTTP_HDR_PROTO, "HTTP/1.1");
273
274 30275
        if (bo->stale_oc != NULL && !(bo->stale_oc->flags & OC_F_DYING) &&
275 2028
            ObjCheckFlag(bo->wrk, bo->stale_oc, OF_IMSCAND) &&
276 533
            (bo->stale_oc->boc != NULL || ObjGetLen(wrk, bo->stale_oc) != 0)) {
277 507
                AZ(bo->stale_oc->flags & (OC_F_HFM|OC_F_PRIVATE));
278 507
                q = RFC2616_Strong_LM(NULL, wrk, bo->stale_oc);
279 507
                if (q != NULL)
280 338
                        http_PrintfHeader(bo->bereq0,
281 169
                            "If-Modified-Since: %s", q);
282 507
                q = HTTP_GetHdrPack(bo->wrk, bo->stale_oc, H_ETag);
283 507
                if (q != NULL)
284 702
                        http_PrintfHeader(bo->bereq0,
285 351
                            "If-None-Match: %s", q);
286 507
        }
287
288 29794
        http_CopyHome(bo->bereq0);
289 29794
        HTTP_Setup(bo->bereq, bo->ws, bo->vsl, SLT_BereqMethod);
290 29794
        bo->ws_bo = WS_Snapshot(bo->ws);
291 29794
        HTTP_Clone(bo->bereq, bo->bereq0);
292
293 29794
        if (bo->req->req_body_status->avail == 0) {
294 28573
                VBO_SetState(bo->wrk, bo, BOS_REQ_DONE);
295 29794
        } else if (bo->req->req_body_status == BS_CACHED) {
296 299
                AN(bo->req->body_oc);
297 299
                bo->bereq_body = bo->req->body_oc;
298 299
                HSH_Ref(bo->bereq_body);
299 299
                VBO_SetState(bo->wrk, bo, BOS_REQ_DONE);
300 299
        }
301 29794
        return (F_STP_STARTFETCH);
302
}
303
304
/*--------------------------------------------------------------------
305
 * Start a new VSL transaction and try again
306
 * Prepare the busyobj and fetch processors
307
 */
308
309
static const struct fetch_step * v_matchproto_(vbf_state_f)
310 494
vbf_stp_retry(struct worker *wrk, struct busyobj *bo)
311
{
312 494
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
313 494
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
314
315 494
        assert(bo->fetch_objcore->boc->state <= BOS_REQ_DONE);
316
317 494
        if (bo->no_retry != NULL && bo->no_retry != retry_disabled) {
318 52
                VSLb(bo->vsl, SLT_Error,
319 26
                    "Retry not possible, %s", bo->no_retry);
320 26
                return (F_STP_FAIL);
321
        }
322
323 468
        VSLb_ts_busyobj(bo, "Retry", W_TIM_real(wrk));
324
325
        /* VDI_Finish (via vbf_cleanup) must have been called before */
326 468
        assert(bo->director_state == DIR_S_NULL);
327
328
        /* reset other bo attributes - See VBO_GetBusyObj */
329 468
        bo->storage = NULL;
330 468
        bo->do_esi = 0;
331 468
        bo->do_stream = 1;
332 468
        bo->was_304 = 0;
333 468
        bo->err_code = 0;
334 468
        bo->err_reason = NULL;
335 468
        bo->connect_timeout = NAN;
336 468
        bo->first_byte_timeout = NAN;
337 468
        bo->between_bytes_timeout = NAN;
338 468
        if (bo->htc != NULL)
339 0
                bo->htc->doclose = SC_NULL;
340
341
        // XXX: BereqEnd + BereqAcct ?
342 468
        VSL_ChgId(bo->vsl, "bereq", "retry", VXID_Get(wrk, VSL_BACKENDMARKER));
343 468
        VSLb_ts_busyobj(bo, "Start", bo->t_prev);
344 468
        http_VSL_log(bo->bereq);
345
346 468
        return (F_STP_STARTFETCH);
347 494
}
348
349
/*--------------------------------------------------------------------
350
 * 304 setup logic
351
 */
352
353
static void
354 403
vbf_304_logic(struct busyobj *bo)
355
{
356
357 403
        AZ(bo->stale_oc->flags & (OC_F_HFM|OC_F_PRIVATE));
358 403
        if (ObjCheckFlag(bo->wrk, bo->stale_oc, OF_CHGCE)) {
359
                /*
360
                 * If a VFP changed C-E in the stored
361
                 * object, then don't overwrite C-E from
362
                 * the IMS fetch, and we must weaken any
363
                 * new ETag we get.
364
                 */
365 26
                RFC2616_Weaken_Etag(bo->beresp);
366 26
        }
367 403
        http_Unset(bo->beresp, H_Content_Encoding);
368 403
        http_Unset(bo->beresp, H_Content_Length);
369 403
        HTTP_Merge(bo->wrk, bo->stale_oc, bo->beresp);
370 403
}
371
372
/*--------------------------------------------------------------------
373
 * Setup bereq from bereq0, run vcl_backend_fetch
374
 */
375
376
static const struct fetch_step * v_matchproto_(vbf_state_f)
377 30262
vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
378
{
379
        int i;
380
        const char *q;
381
        vtim_real now;
382 30262
        unsigned handling, skip_vbr = 0;
383
        struct objcore *oc;
384
385 30262
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
386 30262
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
387 30262
        oc = bo->fetch_objcore;
388 30262
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
389
390
        // this complements the stale_oc handling in vbf_stp_mkbereq():
391
        // Conditions might have changed since we made the bereq (retry)
392 30262
        if (! bo->uncacheable && bo->stale_oc != NULL &&
393 2067
            bo->stale_oc->flags & OC_F_DYING) {
394 26
                http_Unset(bo->bereq, H_If_Modified_Since);
395 26
                http_Unset(bo->bereq, H_If_None_Match);
396 26
        }
397
398 30262
        AZ(bo->storage);
399 30262
        bo->storage = bo->uncacheable ? stv_transient : STV_next();
400
401 30262
        if (bo->retries > 0)
402 468
                http_Unset(bo->bereq, H_X_Varnish);
403
404 30262
        http_PrintfHeader(bo->bereq, "X-Varnish: %ju", VXID(bo->vsl->wid));
405
406 30262
        if (bo->bereq_body == NULL && bo->req == NULL)
407 29042
                http_Unset(bo->bereq, H_Content_Length);
408
409 30262
        VCL_backend_fetch_method(bo->vcl, wrk, NULL, bo, NULL);
410
411 30262
        if (wrk->vpi->handling == VCL_RET_ABANDON ||
412 30223
            wrk->vpi->handling == VCL_RET_FAIL)
413 90
                return (F_STP_FAIL);
414
415 30172
        assert (wrk->vpi->handling == VCL_RET_FETCH ||
416
            wrk->vpi->handling == VCL_RET_ERROR);
417
418 30172
        HTTP_Setup(bo->beresp, bo->ws, bo->vsl, SLT_BerespMethod);
419
420 30172
        assert(oc->boc->state <= BOS_REQ_DONE);
421
422 30172
        AZ(bo->htc);
423
424 30172
        VFP_Setup(bo->vfc, wrk);
425 30172
        bo->vfc->oc = oc;
426 30172
        bo->vfc->resp = bo->beresp;
427 30172
        bo->vfc->req = bo->bereq;
428
429 30172
        if (wrk->vpi->handling == VCL_RET_ERROR)
430 130
                return (F_STP_ERROR);
431
432 30042
        VSLb_ts_busyobj(bo, "Fetch", W_TIM_real(wrk));
433 30042
        i = VDI_GetHdr(bo);
434 30042
        if (bo->htc != NULL)
435 26530
                CHECK_OBJ_NOTNULL(bo->htc->doclose, STREAM_CLOSE_MAGIC);
436
437 30042
        bo->t_resp = now = W_TIM_real(wrk);
438 30042
        VSLb_ts_busyobj(bo, "Beresp", now);
439
440 30042
        if (i) {
441 3497
                assert(bo->director_state == DIR_S_NULL);
442 3497
                return (F_STP_ERROR);
443
        }
444
445 26545
        if (bo->htc != NULL && bo->htc->body_status == BS_ERROR) {
446 78
                bo->htc->doclose = SC_RX_BODY;
447 78
                vbf_cleanup(bo);
448 78
                VSLb(bo->vsl, SLT_Error, "Body cannot be fetched");
449 78
                assert(bo->director_state == DIR_S_NULL);
450 78
                return (F_STP_ERROR);
451
        }
452
453 26467
        if (!http_GetHdr(bo->beresp, H_Date, NULL)) {
454
                /*
455
                 * RFC 2616 14.18 Date: The Date general-header field
456
                 * represents the date and time at which the message was
457
                 * originated, having the same semantics as orig-date in
458
                 * RFC 822. ... A received message that does not have a
459
                 * Date header field MUST be assigned one by the recipient
460
                 * if the message will be cached by that recipient or
461
                 * gatewayed via a protocol which requires a Date.
462
                 *
463
                 * If we didn't get a Date header, we assign one here.
464
                 */
465 1079
                http_TimeHeader(bo->beresp, "Date: ", now);
466 1079
        }
467
468
        /*
469
         * These two headers can be spread over multiple actual headers
470
         * and we rely on their content outside of VCL, so collect them
471
         * into one line here.
472
         */
473 26465
        http_CollectHdr(bo->beresp, H_Cache_Control);
474 26465
        http_CollectHdr(bo->beresp, H_Vary);
475
476
        /* What does RFC2616 think about TTL ? */
477 52930
        RFC2616_Ttl(bo, now,
478 26465
            &oc->t_origin,
479 26465
            &oc->ttl,
480 26465
            &oc->grace,
481 26465
            &oc->keep);
482
483 26465
        AZ(bo->do_esi);
484 26465
        AZ(bo->was_304);
485
486 26465
        if (http_IsStatus(bo->beresp, 304) && !bo->uncacheable) {
487 520
                if (bo->stale_oc == NULL){
488 0
                        VSLb(bo->vsl, SLT_Error,
489
                            "304 response but not conditional fetch");
490 0
                        bo->htc->doclose = SC_RX_BAD;
491 0
                        vbf_cleanup(bo);
492 0
                        return (F_STP_ERROR);
493
                }
494 520
                bo->was_304 = 1;
495 520
                VCL_backend_refresh_method(bo->vcl, wrk, NULL, bo, NULL);
496 520
                switch (wrk->vpi->handling) {
497
                case VCL_RET_MERGE:
498 403
                        vbf_304_logic(bo);
499 403
                        break;
500
                case VCL_RET_BERESP:
501 26
                        http_SetStatus(bo->beresp, 200, NULL);
502 26
                        http_Unset(bo->beresp, H_Content_Length);
503 26
                        http_Unset(bo->beresp, H_Content_Encoding);
504 52
                        q = HTTP_GetHdrPack(wrk, bo->stale_oc,
505 26
                            H_Content_Length);
506 26
                        if (q != NULL) {
507 26
                                http_ForceHeader(bo->beresp,
508 13
                                    H_Content_Length, q);
509 13
                        }
510 52
                        q = HTTP_GetHdrPack(wrk, bo->stale_oc,
511 26
                            H_Content_Encoding);
512 26
                        if (q != NULL) {
513 0
                                http_ForceHeader(bo->beresp,
514 0
                                    H_Content_Encoding, q);
515 0
                        }
516 52
                        q = HTTP_GetHdrPack(wrk, bo->stale_oc,
517 26
                            H_Last_Modified);
518 26
                        if (q != NULL) {
519 0
                                http_ForceHeader(bo->beresp,
520 0
                                    H_Last_Modified, q);
521 0
                        }
522 26
                        q = HTTP_GetHdrPack(wrk, bo->stale_oc, H_ETag);
523 26
                        if (q != NULL)
524 26
                                http_ForceHeader(bo->beresp, H_ETag, q);
525 26
                        break;
526
                case VCL_RET_OBJ_STALE:
527 26
                        if (HTTP_Decode(bo->beresp, ObjGetAttr(bo->wrk,
528 13
                                bo->stale_oc, OA_HEADERS, NULL))) {
529 0
                                bo->htc->doclose = SC_RX_OVERFLOW;
530 0
                                vbf_cleanup(bo);
531 0
                                return (F_STP_ERROR);
532
                        }
533 13
                        break;
534
                case VCL_RET_RETRY:
535
                case VCL_RET_ERROR:
536
                case VCL_RET_ABANDON:
537
                case VCL_RET_FAIL:
538 78
                        skip_vbr = 1;
539 78
                        break;
540
                default:
541 0
                        WRONG("Illegal return from vcl_backend_refresh{}");
542 0
                }
543 520
        }
544
545 26465
        if (bo->htc != NULL && bo->htc->doclose == SC_NULL &&
546 25268
            http_GetHdrField(bo->bereq, H_Connection, "close", NULL))
547 169
                bo->htc->doclose = SC_REQ_CLOSE;
548 26465
        if (!skip_vbr)
549 26375
                VCL_backend_response_method(bo->vcl, wrk, NULL, bo, NULL);
550
551 26441
        if (bo->htc != NULL && bo->htc->doclose == SC_NULL &&
552 24981
            http_GetHdrField(bo->beresp, H_Connection, "close", NULL))
553 13
                bo->htc->doclose = SC_RESP_CLOSE;
554
555 26441
        if (VRG_CheckBo(bo) < 0) {
556 91
                if (bo->director_state != DIR_S_NULL)
557 78
                        VDI_Finish(bo);
558 91
                return (F_STP_ERROR);
559
        }
560
561 52633
        if (wrk->vpi->handling == VCL_RET_ABANDON ||
562 26309
            wrk->vpi->handling == VCL_RET_FAIL ||
563 26283
            wrk->vpi->handling == VCL_RET_ERROR) {
564
                /* do not count deliberately ending the backend connection as
565
                 * fetch failure
566
                 */
567 195
                handling = wrk->vpi->handling;
568 195
                if (bo->htc)
569 195
                        bo->htc->doclose = SC_RESP_CLOSE;
570 195
                vbf_cleanup(bo);
571 195
                wrk->vpi->handling = handling;
572
573 195
                if (wrk->vpi->handling == VCL_RET_ERROR)
574 130
                        return (F_STP_ERROR);
575
                else
576 65
                        return (F_STP_FAIL);
577
        }
578
579 26155
        if (wrk->vpi->handling == VCL_RET_RETRY) {
580 377
                if (bo->htc && bo->htc->body_status != BS_NONE)
581 91
                        bo->htc->doclose = SC_RESP_CLOSE;
582 377
                vbf_cleanup(bo);
583
584 377
                if (bo->retries++ < bo->max_retries)
585 351
                        return (F_STP_RETRY);
586
587 26
                VSLb(bo->vsl, SLT_VCL_Error,
588
                    "Too many retries, delivering 503");
589 26
                assert(bo->director_state == DIR_S_NULL);
590 26
                return (F_STP_ERROR);
591
        }
592
593 25778
        VSLb_ts_busyobj(bo, "Process", W_TIM_real(wrk));
594 25778
        assert(oc->boc->state <= BOS_REQ_DONE);
595 25778
        if (oc->boc->state != BOS_REQ_DONE)
596 533
                VBO_SetState(wrk, bo, BOS_REQ_DONE);
597
598 25778
        if (bo->do_esi)
599 4030
                bo->do_stream = 0;
600 25778
        if (wrk->vpi->handling == VCL_RET_PASS) {
601 286
                oc->flags |= OC_F_HFP;
602 286
                bo->uncacheable = 1;
603 286
                wrk->vpi->handling = VCL_RET_DELIVER;
604 286
        }
605 25778
        if (!bo->uncacheable || !bo->do_stream)
606 17797
                oc->boc->transit_buffer = 0;
607 25778
        if (bo->uncacheable)
608 9775
                oc->flags |= OC_F_HFM;
609
610 25778
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
611
612 25778
        return (bo->was_304 ? F_STP_CONDFETCH : F_STP_FETCH);
613 30236
}
614
615
/*--------------------------------------------------------------------
616
 */
617
618
static const struct fetch_step * v_matchproto_(vbf_state_f)
619 14476
vbf_stp_fetchbody(struct worker *wrk, struct busyobj *bo)
620
{
621
        ssize_t l;
622
        uint8_t *ptr;
623 14476
        enum vfp_status vfps = VFP_ERROR;
624
        ssize_t est;
625
        struct vfp_ctx *vfc;
626
        struct objcore *oc;
627
628 14476
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
629 14476
        vfc = bo->vfc;
630 14476
        CHECK_OBJ_NOTNULL(vfc, VFP_CTX_MAGIC);
631 14476
        oc = bo->fetch_objcore;
632 14476
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
633
634 14476
        AN(vfc->vfp_nxt);
635
636 14476
        est = bo->htc->content_length;
637 14476
        if (est < 0)
638 1937
                est = 0;
639
640 14476
        do {
641 728081
                if (oc->flags & OC_F_CANCEL) {
642
                        /*
643
                         * A pass object and delivery was terminated
644
                         * We don't fail the fetch, in order for HitMiss
645
                         * objects to be created.
646
                         */
647 36
                        AN(oc->flags & OC_F_HFM);
648 36
                        VSLb(wrk->vsl, SLT_Debug,
649
                            "Fetch: Pass delivery abandoned");
650 36
                        bo->htc->doclose = SC_RX_BODY;
651 36
                        break;
652
                }
653 728045
                AZ(vfc->failed);
654 728045
                l = est;
655 728045
                assert(l >= 0);
656 728045
                if (VFP_GetStorage(vfc, &l, &ptr) != VFP_OK) {
657 65
                        bo->htc->doclose = SC_RX_BODY;
658 65
                        break;
659
                }
660
661 727980
                AZ(vfc->failed);
662 727980
                vfps = VFP_Suck(vfc, ptr, &l);
663 727980
                if (l >= 0 && vfps != VFP_ERROR) {
664 727557
                        VFP_Extend(vfc, l, vfps);
665 727557
                        if (est >= l)
666 37148
                                est -= l;
667
                        else
668 690409
                                est = 0;
669 727557
                }
670 727980
        } while (vfps == VFP_OK);
671
672 14468
        if (vfc->failed) {
673 481
                (void)VFP_Error(vfc, "Fetch pipeline failed to process");
674 481
                bo->htc->doclose = SC_RX_BODY;
675 481
                vbf_cleanup(bo);
676 481
                if (!bo->do_stream) {
677 273
                        assert(oc->boc->state < BOS_STREAM);
678
                        // XXX: doclose = ?
679 273
                        return (F_STP_ERROR);
680
                } else {
681 208
                        wrk->stats->fetch_failed++;
682 208
                        return (F_STP_FAIL);
683
                }
684
        }
685
686 13987
        return (F_STP_FETCHEND);
687 14468
}
688
689
static const struct fetch_step * v_matchproto_(vbf_state_f)
690 25347
vbf_stp_fetch(struct worker *wrk, struct busyobj *bo)
691
{
692
        struct vrt_ctx ctx[1];
693
        struct objcore *oc;
694
695 25347
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
696 25347
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
697 25347
        oc = bo->fetch_objcore;
698 25347
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
699
700 25347
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
701
702 25347
        if (bo->htc == NULL) {
703 26
                (void)VFP_Error(bo->vfc, "No backend connection (rollback?)");
704 26
                vbf_cleanup(bo);
705 26
                return (F_STP_ERROR);
706
        }
707
708
        /* No body -> done */
709 25321
        if (bo->htc->body_status == BS_NONE || bo->htc->content_length == 0) {
710 9841
                http_Unset(bo->beresp, H_Content_Encoding);
711 9841
                bo->do_gzip = bo->do_gunzip = 0;
712 9841
                bo->do_stream = 0;
713 9841
                bo->vfp_filter_list = "";
714 25323
        } else if (bo->vfp_filter_list == NULL) {
715 15404
                bo->vfp_filter_list = VBF_Get_Filter_List(bo);
716 15404
        }
717
718 25323
        if (bo->vfp_filter_list == NULL ||
719 25322
            VCL_StackVFP(bo->vfc, bo->vcl, bo->vfp_filter_list)) {
720 313
                (bo)->htc->doclose = SC_OVERLOAD;
721 313
                vbf_cleanup(bo);
722 313
                return (F_STP_ERROR);
723
        }
724
725 25010
        if (oc->flags & OC_F_PRIVATE)
726 8252
                AN(bo->uncacheable);
727
728 25010
        oc->boc->fetched_so_far = 0;
729
730 25010
        INIT_OBJ(ctx, VRT_CTX_MAGIC);
731 25010
        VCL_Bo2Ctx(ctx, bo);
732
733 25010
        if (VFP_Open(ctx, bo->vfc)) {
734 689
                (void)VFP_Error(bo->vfc, "Fetch pipeline failed to open");
735 689
                bo->htc->doclose = SC_RX_BODY;
736 689
                vbf_cleanup(bo);
737 689
                return (F_STP_ERROR);
738
        }
739
740 24321
        if (vbf_beresp2obj(bo)) {
741 39
                bo->htc->doclose = SC_RX_BODY;
742 39
                vbf_cleanup(bo);
743 39
                return (F_STP_ERROR);
744
        }
745
746
#define OBJ_FLAG(U, l, v)                                               \
747
        if (bo->vfc->obj_flags & OF_##U)                                \
748
                ObjSetFlag(bo->wrk, oc, OF_##U, 1);
749
#include "tbl/obj_attr.h"
750
751 24282
        if (!(oc->flags & OC_F_HFM) &&
752 15470
            http_IsStatus(bo->beresp, 200) && (
753 15222
              RFC2616_Strong_LM(bo->beresp, NULL, NULL) != NULL ||
754 14912
              http_GetHdr(bo->beresp, H_ETag, NULL)))
755 779
                ObjSetFlag(bo->wrk, oc, OF_IMSCAND, 1);
756
757 24282
        assert(oc->boc->refcount >= 1);
758
759 24282
        assert(oc->boc->state == BOS_REQ_DONE);
760
761 24282
        if (bo->do_stream)
762 10812
                VBO_SetState(wrk, bo, BOS_STREAM);
763
764
        VSLb(bo->vsl, SLT_Fetch_Body, "%u %s %s",
765
            bo->htc->body_status->nbr, bo->htc->body_status->name,
766
            bo->do_stream ? "stream" : "-");
767
768 24282
        if (bo->htc->body_status != BS_NONE) {
769 14467
                assert(bo->htc->body_status != BS_ERROR);
770 14467
                return (F_STP_FETCHBODY);
771
        }
772 9815
        AZ(bo->vfc->failed);
773
        return (F_STP_FETCHEND);
774
}
775
776
static const struct fetch_step * v_matchproto_(vbf_state_f)
777 24179
vbf_stp_fetchend(struct worker *wrk, struct busyobj *bo)
778
{
779
780
        struct objcore *oc;
781
782 24179
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
783 24179
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
784 24179
        oc = bo->fetch_objcore;
785 24179
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
786
787 24179
        AZ(bo->vfc->failed);
788
789
        /* Recycle the backend connection before setting BOS_FINISHED to
790
           give predictable backend reuse behavior for varnishtest */
791 24179
        vbf_cleanup(bo);
792
793 24179
        AZ(ObjSetU64(wrk, oc, OA_LEN, oc->boc->fetched_so_far));
794
795 24179
        if (bo->do_stream)
796 10971
                assert(oc->boc->state == BOS_STREAM);
797
        else
798 13208
                assert(oc->boc->state == BOS_REQ_DONE);
799
800 24179
        VBO_SetState(wrk, bo, BOS_FINISHED);
801 24179
        VSLb_ts_busyobj(bo, "BerespBody", W_TIM_real(wrk));
802 24179
        if (bo->stale_oc != NULL) {
803 2054
                VSL(SLT_ExpKill, NO_VXID, "VBF_Superseded x=%ju n=%ju",
804 1027
                    VXID(ObjGetXID(wrk, bo->stale_oc)),
805 1027
                    VXID(ObjGetXID(wrk, bo->fetch_objcore)));
806 1027
                HSH_Replace(bo->stale_oc, bo->fetch_objcore);
807 1027
        }
808 24179
        return (F_STP_DONE);
809
}
810
811
/*--------------------------------------------------------------------
812
 */
813
814
struct vbf_objiter_priv {
815
        unsigned                magic;
816
#define VBF_OBITER_PRIV_MAGIC   0x3c272a17
817
        struct busyobj          *bo;
818
        // not yet allocated
819
        ssize_t         l;
820
        // current allocation
821
        uint8_t         *p;
822
        ssize_t         pl;
823
};
824
825
static int v_matchproto_(objiterate_f)
826 429
vbf_objiterate(void *priv, unsigned flush, const void *ptr, ssize_t len)
827
{
828
        struct vbf_objiter_priv *vop;
829
        ssize_t l;
830 429
        const uint8_t *ps = ptr;
831
832 429
        CAST_OBJ_NOTNULL(vop, priv, VBF_OBITER_PRIV_MAGIC);
833 429
        CHECK_OBJ_NOTNULL(vop->bo, BUSYOBJ_MAGIC);
834
835 429
        flush &= OBJ_ITER_END;
836
837 845
        while (len > 0) {
838 429
                if (vop->pl == 0) {
839 416
                        vop->p = NULL;
840 416
                        AN(vop->l);
841 416
                        vop->pl = vop->l;
842 832
                        if (VFP_GetStorage(vop->bo->vfc, &vop->pl, &vop->p)
843 416
                            != VFP_OK)
844 13
                                return (1);
845 403
                        if (vop->pl < vop->l)
846 26
                                vop->l -= vop->pl;
847
                        else
848 377
                                vop->l = 0;
849 403
                }
850 416
                AN(vop->pl);
851 416
                AN(vop->p);
852
853 416
                l = vmin(vop->pl, len);
854 416
                memcpy(vop->p, ps, l);
855 793
                VFP_Extend(vop->bo->vfc, l,
856 416
                           flush && l == len ? VFP_END : VFP_OK);
857 416
                ps += l;
858 416
                vop->p += l;
859 416
                len -= l;
860 416
                vop->pl -= l;
861
        }
862 416
        if (flush && vop->bo->vfc->failed == 0)
863 377
                AZ(vop->l);
864 416
        return (0);
865 429
}
866
867
static const struct fetch_step * v_matchproto_(vbf_state_f)
868 429
vbf_stp_condfetch(struct worker *wrk, struct busyobj *bo)
869
{
870
        struct boc *stale_boc;
871
        enum boc_state_e stale_state;
872
        struct objcore *oc, *stale_oc;
873
        struct vbf_objiter_priv vop[1];
874
875 429
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
876 429
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
877 429
        oc = bo->fetch_objcore;
878 429
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
879 429
        stale_oc = bo->stale_oc;
880 429
        CHECK_OBJ_NOTNULL(stale_oc, OBJCORE_MAGIC);
881
882 429
        stale_boc = HSH_RefBoc(stale_oc);
883 429
        CHECK_OBJ_ORNULL(stale_boc, BOC_MAGIC);
884 429
        if (stale_boc) {
885
                /* Wait for the stale object to become fully fetched, so
886
                 * that we can catch fetch errors, before we unbusy the
887
                 * new object. This serves two purposes. First it helps
888
                 * with request coalescing, and stops long chains of
889
                 * IMS-updated short-TTL objects all streaming from a
890
                 * single slow body fetch. Second it makes sure that all
891
                 * the object attributes are complete when we copy them
892
                 * (this would be an issue for ie OA_GZIPBITS). */
893 52
                VSLb(bo->vsl, SLT_Notice,
894
                    "vsl: Conditional fetch wait for streaming object");
895
                /* XXX: We should have a VCL controlled timeout here */
896 52
                stale_state = ObjWaitState(stale_oc, BOS_FINISHED);
897 52
                HSH_DerefBoc(bo->wrk, stale_oc);
898 52
                stale_boc = NULL;
899 52
                if (stale_state != BOS_FINISHED) {
900 26
                        assert(stale_state == BOS_FAILED);
901 26
                        AN(stale_oc->flags & OC_F_FAILED);
902 26
                }
903 52
        }
904
905 429
        AZ(stale_boc);
906 429
        if (stale_oc->flags & OC_F_FAILED) {
907 26
                (void)VFP_Error(bo->vfc, "Template object failed");
908 26
                vbf_cleanup(bo);
909 26
                wrk->stats->fetch_failed++;
910 26
                return (F_STP_FAIL);
911
        }
912
913 403
        if (vbf_beresp2obj(bo)) {
914 13
                vbf_cleanup(bo);
915 13
                wrk->stats->fetch_failed++;
916 13
                return (F_STP_FAIL);
917
        }
918
919 390
        if (ObjHasAttr(bo->wrk, stale_oc, OA_ESIDATA))
920 13
                AZ(ObjCopyAttr(bo->wrk, oc, stale_oc, OA_ESIDATA));
921
922 390
        AZ(ObjCopyAttr(bo->wrk, oc, stale_oc, OA_FLAGS));
923 390
        if (oc->flags & OC_F_HFM)
924 26
                ObjSetFlag(bo->wrk, oc, OF_IMSCAND, 0);
925 390
        AZ(ObjCopyAttr(bo->wrk, oc, stale_oc, OA_GZIPBITS));
926
927 390
        if (bo->do_stream)
928 377
                VBO_SetState(wrk, bo, BOS_STREAM);
929
930 390
        INIT_OBJ(vop, VBF_OBITER_PRIV_MAGIC);
931 390
        vop->bo = bo;
932 390
        vop->l = ObjGetLen(bo->wrk, stale_oc);
933 390
        if (ObjIterate(wrk, stale_oc, vop, vbf_objiterate, 0))
934 13
                (void)VFP_Error(bo->vfc, "Template object failed");
935
936 390
        if (bo->vfc->failed) {
937 13
                vbf_cleanup(bo);
938 13
                wrk->stats->fetch_failed++;
939 13
                return (F_STP_FAIL);
940
        }
941 377
        return (F_STP_FETCHEND);
942 429
}
943
944
/*--------------------------------------------------------------------
945
 * Create synth object
946
 *
947
 * replaces a stale object unless
948
 * - abandoning the bereq or
949
 * - leaving vcl_backend_error with return (deliver)
950
 *
951
 * We do want the stale replacement to avoid an object pileup with short ttl and
952
 * long grace/keep, yet there could exist cases where a cache object is
953
 * deliberately created to momentarily override a stale object.
954
 *
955
 * If this case exists, we should add a vcl veto (e.g. beresp.replace_stale with
956
 * default true)
957
 */
958
959
static const struct fetch_step * v_matchproto_(vbf_state_f)
960 5291
vbf_stp_error(struct worker *wrk, struct busyobj *bo)
961
{
962
        ssize_t l, ll, o;
963
        vtim_real now;
964
        uint8_t *ptr;
965
        struct vsb *synth_body;
966
        struct objcore *stale, *oc;
967
968 5291
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
969 5291
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
970 5291
        oc = bo->fetch_objcore;
971 5291
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
972 5291
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
973 5291
        assert(oc->boc->state < BOS_STREAM);
974 5291
        assert(bo->director_state == DIR_S_NULL);
975
976 5291
        if (wrk->vpi->handling != VCL_RET_ERROR)
977 5031
                wrk->stats->fetch_failed++;
978
979 5291
        now = W_TIM_real(wrk);
980 5291
        VSLb_ts_busyobj(bo, "Error", now);
981
982 5291
        if (oc->stobj->stevedore != NULL) {
983
                // replacing an already fetched object with a "synth" one
984 273
                assert(oc->boc->state < BOS_STREAM);
985 273
                oc->boc->fetched_so_far = 0;
986 273
                ObjFreeObj(bo->wrk, oc);
987 273
        }
988
989 5291
        if (bo->storage == NULL)
990 312
                bo->storage = STV_next();
991
992
        // XXX: reset all beresp flags ?
993
994 5291
        HTTP_Setup(bo->beresp, bo->ws, bo->vsl, SLT_BerespMethod);
995 5291
        if (bo->err_code > 0)
996 858
                http_PutResponse(bo->beresp, "HTTP/1.1", bo->err_code,
997 429
                    bo->err_reason);
998
        else
999 4862
                http_PutResponse(bo->beresp, "HTTP/1.1", 503,
1000
                    "Backend fetch failed");
1001
1002 5291
        http_TimeHeader(bo->beresp, "Date: ", now);
1003 5291
        http_SetHeader(bo->beresp, "Server: Varnish");
1004
1005 5291
        stale = bo->stale_oc;
1006 5291
        oc->t_origin = now;
1007 5291
        oc->ttl = 0;
1008 5291
        oc->grace = 0;
1009 5291
        oc->keep = 0;
1010
1011 5291
        synth_body = VSB_new_auto();
1012 5291
        AN(synth_body);
1013
1014 5291
        VCL_backend_error_method(bo->vcl, wrk, NULL, bo, synth_body);
1015
1016 5291
        AZ(VSB_finish(synth_body));
1017
1018 5291
        if (wrk->vpi->handling == VCL_RET_ABANDON || wrk->vpi->handling == VCL_RET_FAIL) {
1019 364
                VSB_destroy(&synth_body);
1020 364
                return (F_STP_FAIL);
1021
        }
1022
1023 4927
        if (wrk->vpi->handling == VCL_RET_RETRY) {
1024 156
                VSB_destroy(&synth_body);
1025 156
                if (bo->retries++ < bo->max_retries)
1026 143
                        return (F_STP_RETRY);
1027 13
                VSLb(bo->vsl, SLT_VCL_Error, "Too many retries, failing");
1028 13
                return (F_STP_FAIL);
1029
        }
1030
1031 4771
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
1032
1033 4771
        assert(bo->vfc->wrk == bo->wrk);
1034 4771
        assert(bo->vfc->oc == oc);
1035 4771
        assert(bo->vfc->resp == bo->beresp);
1036 4771
        assert(bo->vfc->req == bo->bereq);
1037
1038 4771
        if (vbf_beresp2obj(bo)) {
1039 26
                VSB_destroy(&synth_body);
1040 26
                return (F_STP_FAIL);
1041
        }
1042
1043 4745
        oc->boc->transit_buffer = 0;
1044
1045 4745
        ll = VSB_len(synth_body);
1046 4745
        o = 0;
1047 8853
        while (ll > 0) {
1048 4121
                l = ll;
1049 4121
                if (VFP_GetStorage(bo->vfc, &l, &ptr) != VFP_OK) {
1050 13
                        VSB_destroy(&synth_body);
1051 13
                        return (F_STP_FAIL);
1052
                }
1053 4108
                l = vmin(l, ll);
1054 4108
                memcpy(ptr, VSB_data(synth_body) + o, l);
1055 4108
                VFP_Extend(bo->vfc, l, l == ll ? VFP_END : VFP_OK);
1056 4108
                ll -= l;
1057 4108
                o += l;
1058
        }
1059 4732
        assert(o == VSB_len(synth_body));
1060 4732
        AZ(ObjSetU64(wrk, oc, OA_LEN, o));
1061 4732
        VSB_destroy(&synth_body);
1062 4732
        if (stale != NULL && oc->ttl > 0)
1063 286
                HSH_Kill(stale);
1064 4732
        VBO_SetState(wrk, bo, BOS_FINISHED);
1065 4732
        return (F_STP_DONE);
1066 5291
}
1067
1068
/*--------------------------------------------------------------------
1069
 */
1070
1071
static const struct fetch_step * v_matchproto_(vbf_state_f)
1072 884
vbf_stp_fail(struct worker *wrk, struct busyobj *bo)
1073
{
1074
        struct objcore *oc;
1075
1076 884
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1077 884
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1078 884
        oc = bo->fetch_objcore;
1079 884
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1080
1081 884
        assert(oc->boc->state < BOS_FINISHED);
1082 884
        VBO_SetState(wrk, bo, BOS_FAILED);
1083 884
        HSH_Kill(oc);
1084 884
        return (F_STP_DONE);
1085
}
1086
1087
/*--------------------------------------------------------------------
1088
 */
1089
1090
static const struct fetch_step * v_matchproto_(vbf_state_f)
1091 0
vbf_stp_done(struct worker *wrk, struct busyobj *bo)
1092
{
1093
1094 0
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1095 0
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1096 0
        WRONG("Just plain wrong");
1097 0
        NEEDLESS(return (F_STP_DONE));
1098
}
1099
1100
static void v_matchproto_(task_func_t)
1101 29796
vbf_fetch_thread(struct worker *wrk, void *priv)
1102
{
1103
        struct vrt_ctx ctx[1];
1104
        struct busyobj *bo;
1105
        struct objcore *oc;
1106
        const struct fetch_step *stp;
1107
1108 29796
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1109 29796
        CAST_OBJ_NOTNULL(bo, priv, BUSYOBJ_MAGIC);
1110 29796
        CHECK_OBJ_NOTNULL(bo->req, REQ_MAGIC);
1111 29796
        oc = bo->fetch_objcore;
1112 29796
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1113
1114 29796
        THR_SetBusyobj(bo);
1115 29796
        stp = F_STP_MKBEREQ;
1116 29796
        assert(isnan(bo->t_first));
1117 29796
        assert(isnan(bo->t_prev));
1118 29796
        VSLb_ts_busyobj(bo, "Start", W_TIM_real(wrk));
1119
1120 29796
        bo->wrk = wrk;
1121 29796
        wrk->vsl = bo->vsl;
1122
1123
#if 0
1124
        if (bo->stale_oc != NULL) {
1125
                CHECK_OBJ_NOTNULL(bo->stale_oc, OBJCORE_MAGIC);
1126
                /* We don't want the oc/stevedore ops in fetching thread */
1127
                if (!ObjCheckFlag(wrk, bo->stale_oc, OF_IMSCAND))
1128
                        (void)HSH_DerefObjCore(wrk, &bo->stale_oc, 0);
1129
        }
1130
#endif
1131
1132 29796
        VCL_TaskEnter(bo->privs);
1133 160925
        while (stp != F_STP_DONE) {
1134 131129
                CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1135 131129
                assert(oc->boc->refcount >= 1);
1136 131129
                if (oc->boc->state < BOS_REQ_DONE)
1137 31030
                        AN(bo->req);
1138
                else
1139 100099
                        AZ(bo->req);
1140 131129
                AN(stp);
1141 131129
                AN(stp->name);
1142 131129
                AN(stp->func);
1143 131129
                stp = stp->func(wrk, bo);
1144
        }
1145
1146 29796
        assert(bo->director_state == DIR_S_NULL);
1147
1148 29796
        INIT_OBJ(ctx, VRT_CTX_MAGIC);
1149 29796
        VCL_Bo2Ctx(ctx, bo);
1150 29796
        VCL_TaskLeave(ctx, bo->privs);
1151 29796
        http_Teardown(bo->bereq);
1152 29796
        http_Teardown(bo->beresp);
1153
        // cannot make assumptions about the number of references here #3434
1154 29796
        if (bo->bereq_body != NULL)
1155 234
                (void)HSH_DerefObjCore(bo->wrk, &bo->bereq_body);
1156
1157 29796
        if (oc->boc->state == BOS_FINISHED) {
1158 28912
                AZ(oc->flags & OC_F_FAILED);
1159 57824
                VSLb(bo->vsl, SLT_Length, "%ju",
1160 28912
                    (uintmax_t)ObjGetLen(bo->wrk, oc));
1161 28912
        }
1162
        // AZ(oc->boc); // XXX
1163
1164 29796
        if (bo->stale_oc != NULL)
1165 2041
                (void)HSH_DerefObjCore(wrk, &bo->stale_oc);
1166
1167 29796
        wrk->vsl = NULL;
1168 29796
        HSH_DerefBoc(wrk, oc);
1169 29796
        SES_Rel(bo->sp);
1170 29796
        VBO_ReleaseBusyObj(wrk, &bo);
1171 29796
        THR_SetBusyobj(NULL);
1172 29796
}
1173
1174
/*--------------------------------------------------------------------
1175
 */
1176
1177
void
1178 29809
VBF_Fetch(struct worker *wrk, struct req *req, struct objcore *oc,
1179
    struct objcore *oldoc, enum vbf_fetch_mode_e mode)
1180
{
1181
        enum boc_state_e state;
1182
        struct boc *boc;
1183
        struct busyobj *bo;
1184
        enum task_prio prio;
1185
        const char *how;
1186
1187 29809
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1188 29809
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1189 29809
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1190 29809
        CHECK_OBJ_ORNULL(oldoc, OBJCORE_MAGIC);
1191
1192 29809
        bo = VBO_GetBusyObj(wrk, req);
1193 29809
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1194 29809
        AN(bo->vcl);
1195
1196 29809
        boc = HSH_RefBoc(oc);
1197 29809
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
1198 29809
        assert(boc->state < BOS_STREAM);
1199 29809
        boc->transit_buffer = cache_param->transit_buffer;
1200
1201 29809
        switch (mode) {
1202
        case VBF_PASS:
1203 10335
                prio = TASK_QUEUE_BO;
1204 10335
                how = "pass";
1205 10335
                bo->uncacheable = 1;
1206 10335
                break;
1207
        case VBF_NORMAL:
1208 18343
                prio = TASK_QUEUE_BO;
1209 18343
                how = "fetch";
1210 18343
                break;
1211
        case VBF_BACKGROUND:
1212 1131
                prio = TASK_QUEUE_BG;
1213 1131
                how = "bgfetch";
1214 1131
                bo->is_bgfetch = 1;
1215 1131
                break;
1216
        default:
1217 0
                WRONG("Wrong fetch mode");
1218 0
        }
1219
1220
#define REQ_BEREQ_FLAG(l, r, w, d) bo->l = req->l;
1221
#include "tbl/req_bereq_flags.h"
1222
1223
        VSLb(bo->vsl, SLT_Begin, "bereq %ju %s", VXID(req->vsl->wid), how);
1224
        VSLbs(bo->vsl, SLT_VCL_use, TOSTRAND(VCL_Name(bo->vcl)));
1225
        VSLb(req->vsl, SLT_Link, "bereq %ju %s", VXID(bo->vsl->wid), how);
1226
1227
        THR_SetBusyobj(bo);
1228
1229
        bo->sp = req->sp;
1230
        SES_Ref(bo->sp);
1231
1232
        oc->boc->vary = req->vary_b;
1233
        req->vary_b = NULL;
1234
1235
        HSH_Ref(oc);
1236 29809
        AZ(bo->fetch_objcore);
1237
        bo->fetch_objcore = oc;
1238
1239 29809
        AZ(bo->stale_oc);
1240 29809
        if (oldoc != NULL) {
1241 2054
                assert(oldoc->refcnt > 0);
1242 2054
                HSH_Ref(oldoc);
1243 2054
                bo->stale_oc = oldoc;
1244 2054
        }
1245
1246 29809
        AZ(bo->req);
1247
        bo->req = req;
1248
1249
        bo->fetch_task->priv = bo;
1250
        bo->fetch_task->func = vbf_fetch_thread;
1251
1252 29809
        if (Pool_Task(wrk->pool, bo->fetch_task, prio)) {
1253 31
                wrk->stats->bgfetch_no_thread++;
1254 31
                VSLb(bo->vsl, SLT_FetchError,
1255
                    "No thread available for bgfetch");
1256 31
                (void)vbf_stp_fail(req->wrk, bo);
1257 31
                if (bo->stale_oc != NULL)
1258 13
                        (void)HSH_DerefObjCore(wrk, &bo->stale_oc);
1259 31
                HSH_DerefBoc(wrk, oc);
1260 31
                SES_Rel(bo->sp);
1261 31
                THR_SetBusyobj(NULL);
1262 31
                VBO_ReleaseBusyObj(wrk, &bo);
1263 31
        } else {
1264 29778
                THR_SetBusyobj(NULL);
1265 29778
                bo = NULL; /* ref transferred to fetch thread */
1266 29778
                if (mode == VBF_BACKGROUND) {
1267 1118
                        (void)ObjWaitState(oc, BOS_REQ_DONE);
1268 1118
                        (void)VRB_Ignore(req);
1269 1118
                } else {
1270 28660
                        state = ObjWaitState(oc, BOS_STREAM);
1271 28660
                        AZ(oc->flags & OC_F_BUSY);
1272 28660
                        if (state == BOS_FAILED)
1273 351
                                AN(oc->flags & OC_F_FAILED);
1274
                }
1275
        }
1276 29809
        AZ(bo);
1277
        VSLb_ts_req(req, "Fetch", W_TIM_real(wrk));
1278 29809
        assert(oc->boc == boc);
1279
        HSH_DerefBoc(wrk, oc);
1280 29809
        if (mode == VBF_BACKGROUND)
1281 1131
                (void)HSH_DerefObjCore(wrk, &oc);
1282
}