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 2225
vbf_allocobj(struct busyobj *bo, unsigned l)
76
{
77
        struct objcore *oc;
78
        const struct stevedore *stv;
79
        vtim_dur lifetime;
80
81 2225
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
82 2225
        oc = bo->fetch_objcore;
83 2225
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
84
85 2225
        lifetime = oc->ttl + oc->grace + oc->keep;
86
87 2225
        if (bo->uncacheable) {
88 890
                stv = stv_transient;
89 890
                bo->wrk->stats->beresp_uncacheable++;
90 890
        }
91 1335
        else if (lifetime < cache_param->shortlived) {
92 121
                stv = stv_transient;
93 121
                bo->wrk->stats->beresp_shortlived++;
94 121
        }
95
        else
96 1214
                stv = bo->storage;
97
98 2225
        bo->storage = NULL;
99
100 2225
        if (stv == NULL)
101 1
                return (0);
102
103 2224
        if (STV_NewObject(bo->wrk, oc, stv, l))
104 2217
                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 2225
}
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 2225
vbf_beresp2obj(struct busyobj *bo)
167
{
168
        unsigned l, l2;
169
        const char *b;
170
        uint8_t *bp;
171 2225
        struct vsb *vary = NULL;
172 2225
        int varyl = 0;
173
        struct objcore *oc;
174
175 2225
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
176 2225
        oc = bo->fetch_objcore;
177 2225
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
178
179 2225
        l = 0;
180
181
        /* Create Vary instructions */
182 2225
        if (!(oc->flags & OC_F_PRIVATE)) {
183 1440
                varyl = VRY_Create(bo, &vary);
184 1440
                if (varyl > 0) {
185 211
                        AN(vary);
186 211
                        assert(varyl == VSB_len(vary));
187 211
                        l += PRNDUP((intptr_t)varyl);
188 1440
                } 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 1224
                        AZ(vary);
201 1440
        }
202
203 4450
        l2 = http_EstimateWS(bo->beresp,
204 2225
            bo->uncacheable ? HTTPH_A_PASS : HTTPH_A_INS);
205 2225
        l += l2;
206
207 2225
        if (bo->uncacheable)
208 890
                oc->flags |= OC_F_HFM;
209
210 2225
        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 2219
        if (vary != NULL) {
218 211
                AN(ObjSetAttr(bo->wrk, oc, OA_VARY, varyl, VSB_data(vary)));
219 211
                VSB_destroy(&vary);
220 211
        }
221
222 2219
        AZ(ObjSetXID(bo->wrk, oc, bo->vsl->wid));
223
224
        /* for HTTP_Encode() VSLH call */
225 2219
        bo->beresp->logtag = SLT_ObjMethod;
226
227
        /* Filter into object */
228 2219
        bp = ObjSetAttr(bo->wrk, oc, OA_HEADERS, l2, NULL);
229 2219
        AN(bp);
230 4438
        HTTP_Encode(bo->beresp, bp, l2,
231 2219
            bo->uncacheable ? HTTPH_A_PASS : HTTPH_A_INS);
232
233 2219
        if (http_GetHdr(bo->beresp, H_Last_Modified, &b))
234 43
                AZ(ObjSetDouble(bo->wrk, oc, OA_LASTMODIFIED, VTIM_parse(b)));
235
        else
236 2176
                AZ(ObjSetDouble(bo->wrk, oc, OA_LASTMODIFIED,
237
                    floor(oc->t_origin)));
238
239 2219
        return (0);
240 2225
}
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 2248
vbf_stp_mkbereq(struct worker *wrk, struct busyobj *bo)
248
{
249
        const char *q;
250
        struct objcore *oc;
251
252 2248
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
253 2248
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
254 2248
        CHECK_OBJ_NOTNULL(bo->req, REQ_MAGIC);
255 2248
        oc = bo->fetch_objcore;
256 2248
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
257
258 2248
        assert(oc->boc->state == BOS_INVALID);
259 2248
        AZ(bo->storage);
260
261 2248
        HTTP_Setup(bo->bereq0, bo->ws, bo->vsl, SLT_BereqMethod);
262 4496
        http_FilterReq(bo->bereq0, bo->req->http,
263 2248
            bo->uncacheable ? HTTPH_R_PASS : HTTPH_R_FETCH);
264
265 2248
        if (bo->uncacheable)
266 784
                AZ(bo->stale_oc);
267
        else {
268 1464
                http_ForceField(bo->bereq0, HTTP_HDR_METHOD, "GET");
269 1464
                if (cache_param->http_gzip_support)
270 1459
                        http_ForceHeader(bo->bereq0, H_Accept_Encoding, "gzip");
271
        }
272 2248
        http_ForceField(bo->bereq0, HTTP_HDR_PROTO, "HTTP/1.1");
273
274 2277
        if (bo->stale_oc != NULL &&
275 145
            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 2248
        http_CopyHome(bo->bereq0);
289 2248
        HTTP_Setup(bo->bereq, bo->ws, bo->vsl, SLT_BereqMethod);
290 2248
        bo->ws_bo = WS_Snapshot(bo->ws);
291 2248
        HTTP_Clone(bo->bereq, bo->bereq0);
292
293 2248
        if (bo->req->req_body_status->avail == 0) {
294 2158
                bo->req = NULL;
295 2158
                ObjSetState(bo->wrk, oc, BOS_REQ_DONE);
296 2248
        } 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 2248
        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 2282
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 2282
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
401 2282
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
402 2282
        oc = bo->fetch_objcore;
403 2282
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
404
405 2282
        AZ(bo->storage);
406 2282
        bo->storage = bo->uncacheable ? stv_transient : STV_next();
407
408 2282
        if (bo->retries > 0)
409 34
                http_Unset(bo->bereq, H_X_Varnish);
410
411 2282
        http_PrintfHeader(bo->bereq, "X-Varnish: %ju", VXID(bo->vsl->wid));
412
413 2282
        if (bo->bereq_body == NULL && bo->req == NULL)
414 2192
                http_Unset(bo->bereq, H_Content_Length);
415
416 2282
        VCL_backend_fetch_method(bo->vcl, wrk, NULL, bo, NULL);
417
418 2282
        if (wrk->vpi->handling == VCL_RET_ABANDON ||
419 2279
            wrk->vpi->handling == VCL_RET_FAIL)
420 7
                return (F_STP_FAIL);
421
422 2275
        assert (wrk->vpi->handling == VCL_RET_FETCH ||
423
            wrk->vpi->handling == VCL_RET_ERROR);
424
425 2275
        HTTP_Setup(bo->beresp, bo->ws, bo->vsl, SLT_BerespMethod);
426
427 2275
        assert(oc->boc->state <= BOS_REQ_DONE);
428
429 2275
        AZ(bo->htc);
430
431 2275
        VFP_Setup(bo->vfc, wrk);
432 2275
        bo->vfc->oc = oc;
433 2275
        bo->vfc->resp = bo->beresp;
434 2275
        bo->vfc->req = bo->bereq;
435
436 2275
        if (wrk->vpi->handling == VCL_RET_ERROR)
437 10
                return (F_STP_ERROR);
438
439 2265
        VSLb_ts_busyobj(bo, "Fetch", W_TIM_real(wrk));
440 2265
        i = VDI_GetHdr(bo);
441 2265
        if (bo->htc != NULL)
442 2001
                CHECK_OBJ_NOTNULL(bo->htc->doclose, STREAM_CLOSE_MAGIC);
443
444 2265
        bo->t_resp = now = W_TIM_real(wrk);
445 2265
        VSLb_ts_busyobj(bo, "Beresp", now);
446
447 2265
        if (i) {
448 263
                assert(bo->director_state == DIR_S_NULL);
449 263
                return (F_STP_ERROR);
450
        }
451
452 2002
        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 1996
        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 1994
        http_CollectHdr(bo->beresp, H_Cache_Control);
481 1994
        http_CollectHdr(bo->beresp, H_Vary);
482
483
        /* What does RFC2616 think about TTL ? */
484 3988
        RFC2616_Ttl(bo, now,
485 1994
            &oc->t_origin,
486 1994
            &oc->ttl,
487 1994
            &oc->grace,
488 1994
            &oc->keep);
489
490 1994
        AZ(bo->do_esi);
491 1994
        AZ(bo->was_304);
492
493 1994
        if (http_IsStatus(bo->beresp, 304) && vbf_304_logic(bo) < 0)
494 1
                return (F_STP_ERROR);
495
496 1993
        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 1993
        VCL_backend_response_method(bo->vcl, wrk, NULL, bo, NULL);
501
502 1993
        if (bo->htc != NULL && bo->htc->doclose == SC_NULL &&
503 1881
            http_GetHdrField(bo->beresp, H_Connection, "close", NULL))
504 1
                bo->htc->doclose = SC_RESP_CLOSE;
505
506 1993
        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 3967
        if (wrk->vpi->handling == VCL_RET_ABANDON ||
513 1983
            wrk->vpi->handling == VCL_RET_FAIL ||
514 1981
            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 1977
        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 1948
        VSLb_ts_busyobj(bo, "Process", W_TIM_real(wrk));
545 1948
        assert(oc->boc->state <= BOS_REQ_DONE);
546 1948
        if (oc->boc->state != BOS_REQ_DONE) {
547 40
                bo->req = NULL;
548 40
                ObjSetState(wrk, oc, BOS_REQ_DONE);
549 40
        }
550
551 1948
        if (bo->do_esi)
552 310
                bo->do_stream = 0;
553 1948
        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 1948
        if (!bo->uncacheable || !bo->do_stream)
559 1339
                oc->boc->transit_buffer = 0;
560 1948
        if (bo->uncacheable)
561 747
                oc->flags |= OC_F_HFM;
562
563 1948
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
564
565 1948
        return (bo->was_304 ? F_STP_CONDFETCH : F_STP_FETCH);
566 2280
}
567
568
/*--------------------------------------------------------------------
569
 */
570
571
static const struct fetch_step * v_matchproto_(vbf_state_f)
572 1091
vbf_stp_fetchbody(struct worker *wrk, struct busyobj *bo)
573
{
574
        ssize_t l;
575
        uint8_t *ptr;
576 1091
        enum vfp_status vfps = VFP_ERROR;
577
        ssize_t est;
578
        struct vfp_ctx *vfc;
579
        struct objcore *oc;
580
581 1091
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
582 1091
        vfc = bo->vfc;
583 1091
        CHECK_OBJ_NOTNULL(vfc, VFP_CTX_MAGIC);
584 1091
        oc = bo->fetch_objcore;
585 1091
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
586
587 1091
        AN(vfc->vfp_nxt);
588
589 1091
        est = bo->htc->content_length;
590 1091
        if (est < 0)
591 147
                est = 0;
592
593 1091
        do {
594 56001
                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 55999
                AZ(vfc->failed);
607 55999
                l = est;
608 55999
                assert(l >= 0);
609 55999
                if (VFP_GetStorage(vfc, &l, &ptr) != VFP_OK) {
610 5
                        bo->htc->doclose = SC_RX_BODY;
611 5
                        break;
612
                }
613
614 55994
                AZ(vfc->failed);
615 55994
                vfps = VFP_Suck(vfc, ptr, &l);
616 55994
                if (l >= 0 && vfps != VFP_ERROR) {
617 55962
                        VFP_Extend(vfc, l, vfps);
618 55962
                        if (est >= l)
619 2887
                                est -= l;
620
                        else
621 53075
                                est = 0;
622 55962
                }
623 55994
        } while (vfps == VFP_OK);
624
625 1091
        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 1054
        return (F_STP_FETCHEND);
640 1091
}
641
642
static const struct fetch_step * v_matchproto_(vbf_state_f)
643 1921
vbf_stp_fetch(struct worker *wrk, struct busyobj *bo)
644
{
645
        struct vrt_ctx ctx[1];
646
        struct objcore *oc;
647
648 1921
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
649 1921
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
650 1921
        oc = bo->fetch_objcore;
651 1921
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
652
653 1921
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
654
655 1921
        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 1919
        if (bo->htc->body_status == BS_NONE || bo->htc->content_length == 0) {
663 750
                http_Unset(bo->beresp, H_Content_Encoding);
664 750
                bo->do_gzip = bo->do_gunzip = 0;
665 750
                bo->do_stream = 0;
666 750
                bo->vfp_filter_list = "";
667 1919
        } else if (bo->vfp_filter_list == NULL) {
668 1163
                bo->vfp_filter_list = VBF_Get_Filter_List(bo);
669 1163
        }
670
671 1919
        if (bo->vfp_filter_list == NULL ||
672 1919
            VCL_StackVFP(bo->vfc, bo->vcl, bo->vfp_filter_list)) {
673 25
                (bo)->htc->doclose = SC_OVERLOAD;
674 25
                vbf_cleanup(bo);
675 25
                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 1166
            http_IsStatus(bo->beresp, 200) && (
706 1149
              RFC2616_Strong_LM(bo->beresp, NULL, NULL) != NULL ||
707 1125
              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 810
                ObjSetState(wrk, oc, BOS_PREP_STREAM);
716 810
                HSH_Unbusy(wrk, oc);
717 810
                ObjSetState(wrk, oc, BOS_STREAM);
718 810
        }
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 1091
                assert(bo->htc->body_status != BS_ERROR);
726 1091
                return (F_STP_FETCHBODY);
727
        }
728 747
        AZ(bo->vfc->failed);
729
        return (F_STP_FETCHEND);
730
}
731
732
static const struct fetch_step * v_matchproto_(vbf_state_f)
733 1826
vbf_stp_fetchend(struct worker *wrk, struct busyobj *bo)
734
{
735
736
        struct objcore *oc;
737
738 1826
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
739 1826
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
740 1826
        oc = bo->fetch_objcore;
741 1826
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
742
743 1826
        AZ(bo->vfc->failed);
744
745
        /* Recycle the backend connection before setting BOS_FINISHED to
746
           give predictable backend reuse behavior for varnishtest */
747 1826
        vbf_cleanup(bo);
748
749 1826
        AZ(ObjSetU64(wrk, oc, OA_LEN, oc->boc->fetched_so_far));
750
751 1826
        if (bo->do_stream)
752 817
                assert(oc->boc->state == BOS_STREAM);
753
        else {
754 1009
                assert(oc->boc->state == BOS_REQ_DONE);
755 1009
                ObjSetState(wrk, oc, BOS_PREP_STREAM);
756 1009
                HSH_Unbusy(wrk, oc);
757
        }
758
759 1826
        ObjSetState(wrk, oc, BOS_FINISHED);
760 1826
        VSLb_ts_busyobj(bo, "BerespBody", W_TIM_real(wrk));
761 1826
        if (bo->stale_oc != NULL) {
762 146
                VSL(SLT_ExpKill, NO_VXID, "VBF_Superseded x=%ju n=%ju",
763 73
                    VXID(ObjGetXID(wrk, bo->stale_oc)),
764 73
                    VXID(ObjGetXID(wrk, bo->fetch_objcore)));
765 73
                HSH_Replace(bo->stale_oc, bo->fetch_objcore);
766 73
        }
767 1826
        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 25
vbf_objiterate(void *priv, unsigned flush, const void *ptr, ssize_t len)
786
{
787
        struct vbf_objiter_priv *vop;
788
        ssize_t l;
789 25
        const uint8_t *ps = ptr;
790
791 25
        CAST_OBJ_NOTNULL(vop, priv, VBF_OBITER_PRIV_MAGIC);
792 25
        CHECK_OBJ_NOTNULL(vop->bo, BUSYOBJ_MAGIC);
793
794 25
        flush &= OBJ_ITER_END;
795
796 50
        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 25
        if (flush)
822 23
                AZ(vop->l);
823 25
        return (0);
824 25
}
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 21
                oc->boc->fetched_so_far = 0;
947 21
                ObjFreeObj(bo->wrk, oc);
948 21
        }
949
950 396
        if (bo->storage == NULL)
951 24
                bo->storage = STV_next();
952
953
        // XXX: reset all beresp flags ?
954
955 396
        HTTP_Setup(bo->beresp, bo->ws, bo->vsl, SLT_BerespMethod);
956 396
        if (bo->err_code > 0)
957 24
                http_PutResponse(bo->beresp, "HTTP/1.1", bo->err_code,
958 12
                    bo->err_reason);
959
        else
960 384
                http_PutResponse(bo->beresp, "HTTP/1.1", 503,
961
                    "Backend fetch failed");
962
963 396
        http_TimeHeader(bo->beresp, "Date: ", now);
964 396
        http_SetHeader(bo->beresp, "Server: Varnish");
965
966 396
        stale = bo->stale_oc;
967 396
        oc->t_origin = now;
968 396
        if (!VTAILQ_EMPTY(&oc->objhead->waitinglist)) {
969
                /*
970
                 * If there is a waitinglist, it means that there is no
971
                 * grace-able object, so cache the error return for a
972
                 * short time, so the waiting list can drain, rather than
973
                 * each objcore on the waiting list sequentially attempt
974
                 * to fetch from the backend.
975
                 */
976 3
                oc->ttl = 1;
977 3
                oc->grace = 5;
978 3
                oc->keep = 5;
979 3
                stale = NULL;
980 3
        } else {
981 393
                oc->ttl = 0;
982 393
                oc->grace = 0;
983 393
                oc->keep = 0;
984
        }
985
986 396
        synth_body = VSB_new_auto();
987 396
        AN(synth_body);
988
989 396
        VCL_backend_error_method(bo->vcl, wrk, NULL, bo, synth_body);
990
991 396
        AZ(VSB_finish(synth_body));
992
993 396
        if (wrk->vpi->handling == VCL_RET_ABANDON || wrk->vpi->handling == VCL_RET_FAIL) {
994 28
                VSB_destroy(&synth_body);
995 28
                return (F_STP_FAIL);
996
        }
997
998 368
        if (wrk->vpi->handling == VCL_RET_RETRY) {
999 10
                VSB_destroy(&synth_body);
1000 10
                if (bo->retries++ < bo->max_retries)
1001 9
                        return (F_STP_RETRY);
1002 1
                VSLb(bo->vsl, SLT_VCL_Error, "Too many retries, failing");
1003 1
                return (F_STP_FAIL);
1004
        }
1005
1006 358
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
1007
1008 358
        assert(bo->vfc->wrk == bo->wrk);
1009 358
        assert(bo->vfc->oc == oc);
1010 358
        assert(bo->vfc->resp == bo->beresp);
1011 358
        assert(bo->vfc->req == bo->bereq);
1012
1013 358
        if (vbf_beresp2obj(bo)) {
1014 2
                VSB_destroy(&synth_body);
1015 2
                return (F_STP_FAIL);
1016
        }
1017
1018 356
        oc->boc->transit_buffer = 0;
1019
1020 356
        ll = VSB_len(synth_body);
1021 356
        o = 0;
1022 663
        while (ll > 0) {
1023 308
                l = ll;
1024 308
                if (VFP_GetStorage(bo->vfc, &l, &ptr) != VFP_OK) {
1025 1
                        VSB_destroy(&synth_body);
1026 1
                        return (F_STP_FAIL);
1027
                }
1028 307
                l = vmin(l, ll);
1029 307
                memcpy(ptr, VSB_data(synth_body) + o, l);
1030 307
                VFP_Extend(bo->vfc, l, l == ll ? VFP_END : VFP_OK);
1031 307
                ll -= l;
1032 307
                o += l;
1033
        }
1034 355
        assert(o == VSB_len(synth_body));
1035 355
        AZ(ObjSetU64(wrk, oc, OA_LEN, o));
1036 355
        VSB_destroy(&synth_body);
1037 355
        ObjSetState(wrk, oc, BOS_PREP_STREAM);
1038 355
        HSH_Unbusy(wrk, oc);
1039 355
        if (stale != NULL && oc->ttl > 0)
1040 22
                HSH_Kill(stale);
1041 355
        ObjSetState(wrk, oc, BOS_FINISHED);
1042 355
        return (F_STP_DONE);
1043 396
}
1044
1045
/*--------------------------------------------------------------------
1046
 */
1047
1048
static const struct fetch_step * v_matchproto_(vbf_state_f)
1049 67
vbf_stp_fail(struct worker *wrk, struct busyobj *bo)
1050
{
1051
        struct objcore *oc;
1052
1053 67
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1054 67
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1055 67
        oc = bo->fetch_objcore;
1056 67
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1057
1058 67
        assert(oc->boc->state < BOS_FINISHED);
1059 67
        HSH_Fail(oc);
1060 67
        if (!(oc->flags & OC_F_BUSY))
1061 16
                HSH_Kill(oc);
1062 67
        ObjSetState(wrk, oc, BOS_FAILED);
1063 67
        return (F_STP_DONE);
1064
}
1065
1066
/*--------------------------------------------------------------------
1067
 */
1068
1069
static const struct fetch_step * v_matchproto_(vbf_state_f)
1070 0
vbf_stp_done(struct worker *wrk, struct busyobj *bo)
1071
{
1072
1073 0
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1074 0
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1075 0
        WRONG("Just plain wrong");
1076 0
        NEEDLESS(return (F_STP_DONE));
1077
}
1078
1079
static void v_matchproto_(task_func_t)
1080 2248
vbf_fetch_thread(struct worker *wrk, void *priv)
1081
{
1082
        struct vrt_ctx ctx[1];
1083
        struct busyobj *bo;
1084
        struct objcore *oc;
1085
        const struct fetch_step *stp;
1086
1087 2248
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1088 2248
        CAST_OBJ_NOTNULL(bo, priv, BUSYOBJ_MAGIC);
1089 2248
        CHECK_OBJ_NOTNULL(bo->req, REQ_MAGIC);
1090 2248
        oc = bo->fetch_objcore;
1091 2248
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1092
1093 2248
        THR_SetBusyobj(bo);
1094 2248
        stp = F_STP_MKBEREQ;
1095 2248
        assert(isnan(bo->t_first));
1096 2248
        assert(isnan(bo->t_prev));
1097 2248
        VSLb_ts_busyobj(bo, "Start", W_TIM_real(wrk));
1098
1099 2248
        bo->wrk = wrk;
1100 2248
        wrk->vsl = bo->vsl;
1101
1102
#if 0
1103
        if (bo->stale_oc != NULL) {
1104
                CHECK_OBJ_NOTNULL(bo->stale_oc, OBJCORE_MAGIC);
1105
                /* We don't want the oc/stevedore ops in fetching thread */
1106
                if (!ObjCheckFlag(wrk, bo->stale_oc, OF_IMSCAND))
1107
                        (void)HSH_DerefObjCore(wrk, &bo->stale_oc, 0);
1108
        }
1109
#endif
1110
1111 2248
        VCL_TaskEnter(bo->privs);
1112 12141
        while (stp != F_STP_DONE) {
1113 9893
                CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1114 9893
                assert(oc->boc->refcount >= 1);
1115 9893
                if (oc->boc->state < BOS_REQ_DONE)
1116 2337
                        AN(bo->req);
1117
                else
1118 7556
                        AZ(bo->req);
1119 9893
                AN(stp);
1120 9893
                AN(stp->name);
1121 9893
                AN(stp->func);
1122 9893
                stp = stp->func(wrk, bo);
1123
        }
1124
1125 2248
        assert(bo->director_state == DIR_S_NULL);
1126
1127 2248
        INIT_OBJ(ctx, VRT_CTX_MAGIC);
1128 2248
        VCL_Bo2Ctx(ctx, bo);
1129 2248
        VCL_TaskLeave(ctx, bo->privs);
1130 2248
        http_Teardown(bo->bereq);
1131 2248
        http_Teardown(bo->beresp);
1132
        // cannot make assumptions about the number of references here #3434
1133 2248
        if (bo->bereq_body != NULL)
1134 18
                (void) HSH_DerefObjCore(bo->wrk, &bo->bereq_body, 0);
1135
1136 2248
        if (oc->boc->state == BOS_FINISHED) {
1137 2181
                AZ(oc->flags & OC_F_FAILED);
1138 4362
                VSLb(bo->vsl, SLT_Length, "%ju",
1139 2181
                    (uintmax_t)ObjGetLen(bo->wrk, oc));
1140 2181
        }
1141
        // AZ(oc->boc); // XXX
1142
1143 2248
        if (bo->stale_oc != NULL)
1144 145
                (void)HSH_DerefObjCore(wrk, &bo->stale_oc, 0);
1145
1146 2248
        wrk->vsl = NULL;
1147 2248
        HSH_DerefBoc(wrk, oc);
1148 2248
        SES_Rel(bo->sp);
1149 2248
        VBO_ReleaseBusyObj(wrk, &bo);
1150 2248
        THR_SetBusyobj(NULL);
1151 2248
}
1152
1153
/*--------------------------------------------------------------------
1154
 */
1155
1156
void
1157 2249
VBF_Fetch(struct worker *wrk, struct req *req, struct objcore *oc,
1158
    struct objcore *oldoc, enum vbf_fetch_mode_e mode)
1159
{
1160
        struct boc *boc;
1161
        struct busyobj *bo;
1162
        enum task_prio prio;
1163
        const char *how;
1164
1165 2249
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1166 2249
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1167 2249
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1168 2249
        AN(oc->flags & OC_F_BUSY);
1169 2249
        CHECK_OBJ_ORNULL(oldoc, OBJCORE_MAGIC);
1170
1171 2249
        bo = VBO_GetBusyObj(wrk, req);
1172 2249
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1173 2249
        AN(bo->vcl);
1174
1175 2249
        boc = HSH_RefBoc(oc);
1176 2249
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
1177
1178 2249
        switch (mode) {
1179
        case VBF_PASS:
1180 784
                prio = TASK_QUEUE_BO;
1181 784
                how = "pass";
1182 784
                bo->uncacheable = 1;
1183 784
                break;
1184
        case VBF_NORMAL:
1185 1380
                prio = TASK_QUEUE_BO;
1186 1380
                how = "fetch";
1187 1380
                break;
1188
        case VBF_BACKGROUND:
1189 85
                prio = TASK_QUEUE_BG;
1190 85
                how = "bgfetch";
1191 85
                bo->is_bgfetch = 1;
1192 85
                break;
1193
        default:
1194 0
                WRONG("Wrong fetch mode");
1195 0
        }
1196
1197
#define REQ_BEREQ_FLAG(l, r, w, d) bo->l = req->l;
1198
#include "tbl/req_bereq_flags.h"
1199
1200
        VSLb(bo->vsl, SLT_Begin, "bereq %ju %s", VXID(req->vsl->wid), how);
1201
        VSLbs(bo->vsl, SLT_VCL_use, TOSTRAND(VCL_Name(bo->vcl)));
1202
        VSLb(req->vsl, SLT_Link, "bereq %ju %s", VXID(bo->vsl->wid), how);
1203
1204
        THR_SetBusyobj(bo);
1205
1206
        bo->sp = req->sp;
1207
        SES_Ref(bo->sp);
1208
1209
        oc->boc->vary = req->vary_b;
1210
        req->vary_b = NULL;
1211
1212
        HSH_Ref(oc);
1213 2249
        AZ(bo->fetch_objcore);
1214
        bo->fetch_objcore = oc;
1215
1216 2249
        AZ(bo->stale_oc);
1217 2249
        if (oldoc != NULL) {
1218 146
                assert(oldoc->refcnt > 0);
1219 146
                HSH_Ref(oldoc);
1220 146
                bo->stale_oc = oldoc;
1221 146
        }
1222
1223 2249
        AZ(bo->req);
1224
        bo->req = req;
1225
1226
        bo->fetch_task->priv = bo;
1227
        bo->fetch_task->func = vbf_fetch_thread;
1228
1229 2249
        if (Pool_Task(wrk->pool, bo->fetch_task, prio)) {
1230 2
                wrk->stats->bgfetch_no_thread++;
1231 2
                VSLb(bo->vsl, SLT_FetchError,
1232
                    "No thread available for bgfetch");
1233 2
                (void)vbf_stp_fail(req->wrk, bo);
1234 2
                if (bo->stale_oc != NULL)
1235 1
                        (void)HSH_DerefObjCore(wrk, &bo->stale_oc, 0);
1236 2
                HSH_DerefBoc(wrk, oc);
1237 2
                SES_Rel(bo->sp);
1238 2
                THR_SetBusyobj(NULL);
1239 2
                VBO_ReleaseBusyObj(wrk, &bo);
1240 2
        } else {
1241 2247
                THR_SetBusyobj(NULL);
1242 2247
                bo = NULL; /* ref transferred to fetch thread */
1243 2247
                if (mode == VBF_BACKGROUND) {
1244 84
                        ObjWaitState(oc, BOS_REQ_DONE);
1245 84
                        (void)VRB_Ignore(req);
1246 84
                } else {
1247 2163
                        ObjWaitState(oc, BOS_STREAM);
1248 2163
                        if (oc->boc->state == BOS_FAILED) {
1249 27
                                AN((oc->flags & OC_F_FAILED));
1250 27
                        } else {
1251 2136
                                AZ(oc->flags & OC_F_BUSY);
1252
                        }
1253
                }
1254
        }
1255 2249
        AZ(bo);
1256
        VSLb_ts_req(req, "Fetch", W_TIM_real(wrk));
1257 2249
        assert(oc->boc == boc);
1258
        HSH_DerefBoc(wrk, oc);
1259 2249
        if (mode == VBF_BACKGROUND)
1260 85
                (void)HSH_DerefObjCore(wrk, &oc, HSH_RUSH_POLICY);
1261
}