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