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