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