varnish-cache/bin/varnishd/cache/cache_busyobj.c
0
/*-
1
 * Copyright (c) 2013-2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
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
 * Handle backend connections and backend request structures.
31
 *
32
 */
33
34
#include "config.h"
35
36
#include <stdlib.h>
37
38
#include "cache_varnishd.h"
39
#include "cache_filter.h"
40
#include "cache_objhead.h"
41
42
static struct mempool           *vbopool;
43
44
/*--------------------------------------------------------------------
45
 */
46
47
void
48 2786
VBO_Init(void)
49
{
50
51 5572
        vbopool = MPL_New("busyobj", &cache_param->pool_vbo,
52 2786
            &cache_param->workspace_backend);
53 2786
        AN(vbopool);
54 2786
}
55
56
/*--------------------------------------------------------------------
57
 * BusyObj handling
58
 */
59
60
struct busyobj *
61 6798
VBO_GetBusyObj(const struct worker *wrk, const struct req *req)
62
{
63
        struct busyobj *bo;
64
        uint16_t nhttp;
65
        unsigned sz;
66
        char *p;
67
68 6798
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
69
70 6798
        bo = MPL_Get(vbopool, &sz);
71 6798
        XXXAN(bo);
72 6798
        bo->magic = BUSYOBJ_MAGIC;
73 6798
        bo->end = (char *)bo + sz;
74 6798
        bo->max_retries = cache_param->max_retries;
75
76 6798
        p = (void*)(bo + 1);
77 6798
        p = (void*)PRNDUP(p);
78 6798
        assert(p < bo->end);
79
80 6798
        nhttp = (uint16_t)cache_param->http_max_hdr;
81 6798
        sz = HTTP_estimate(nhttp);
82
83 6798
        bo->bereq0 = HTTP_create(p, nhttp, sz);
84 6798
        p += sz;
85 6798
        p = (void*)PRNDUP(p);
86 6798
        assert(p < bo->end);
87
88 6798
        bo->bereq = HTTP_create(p, nhttp, sz);
89 6798
        p += sz;
90 6798
        p = (void*)PRNDUP(p);
91 6798
        assert(p < bo->end);
92
93 6798
        bo->beresp = HTTP_create(p, nhttp, sz);
94 6798
        p += sz;
95 6798
        p = (void*)PRNDUP(p);
96 6798
        assert(p < bo->end);
97
98 6798
        sz = cache_param->vsl_buffer;
99 6798
        VSL_Setup(bo->vsl, p, sz);
100 6798
        bo->vsl->wid = VXID_Get(wrk, VSL_BACKENDMARKER);
101 6798
        p += sz;
102 6798
        p = (void*)PRNDUP(p);
103 6798
        assert(p < bo->end);
104
105 6798
        bo->vfc = (void*)p;
106 6798
        p += sizeof (*bo->vfc);
107 6798
        p = (void*)PRNDUP(p);
108 6798
        INIT_OBJ(bo->vfc, VFP_CTX_MAGIC);
109
110 6798
        WS_Init(bo->ws, "bo", p, bo->end - p);
111
112 6798
        bo->do_stream = 1;
113
114 6798
        if (req->client_identity != NULL) {
115 3
                bo->client_identity = WS_Copy(bo->ws, req->client_identity, -1);
116 3
                XXXAN(bo->client_identity);
117 3
        }
118
119 6798
        VRT_Assign_Backend(&bo->director_req, req->director_hint);
120 6798
        bo->vcl = req->vcl;
121 6798
        VCL_Ref(bo->vcl);
122
123 6798
        bo->t_first = bo->t_prev = NAN;
124 6798
        bo->connect_timeout = NAN;
125 6798
        bo->first_byte_timeout = NAN;
126 6798
        bo->between_bytes_timeout = NAN;
127
128 6798
        memcpy(bo->digest, req->digest, sizeof bo->digest);
129
130 6798
        return (bo);
131
}
132
133
void
134 6795
VBO_ReleaseBusyObj(struct worker *wrk, struct busyobj **pbo)
135
{
136
        struct busyobj *bo;
137
138 6795
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
139 6795
        TAKE_OBJ_NOTNULL(bo, pbo, BUSYOBJ_MAGIC);
140 6795
        CHECK_OBJ_ORNULL(bo->fetch_objcore, OBJCORE_MAGIC);
141
142 6795
        AZ(bo->htc);
143 6795
        AZ(bo->stale_oc);
144
145 13590
        VSLb(bo->vsl, SLT_BereqAcct, "%ju %ju %ju %ju %ju %ju",
146 6795
            (uintmax_t)bo->acct.bereq_hdrbytes,
147 6795
            (uintmax_t)bo->acct.bereq_bodybytes,
148 6795
            (uintmax_t)(bo->acct.bereq_hdrbytes + bo->acct.bereq_bodybytes),
149 6795
            (uintmax_t)bo->acct.beresp_hdrbytes,
150 6795
            (uintmax_t)bo->acct.beresp_bodybytes,
151 6795
            (uintmax_t)(bo->acct.beresp_hdrbytes + bo->acct.beresp_bodybytes));
152
153 6795
        VSL_End(bo->vsl);
154
155 6795
        if (WS_Overflowed(bo->ws))
156 510
                wrk->stats->ws_backend_overflow++;
157
158 6795
        if (bo->fetch_objcore != NULL) {
159 6702
                (void)HSH_DerefObjCore(wrk, &bo->fetch_objcore,
160
                    HSH_RUSH_POLICY);
161 6702
        }
162
163 6795
        VRT_Assign_Backend(&bo->director_req, NULL);
164 6795
        VRT_Assign_Backend(&bo->director_resp, NULL);
165 6795
        VCL_Rel(&bo->vcl);
166
#ifdef ENABLE_WORKSPACE_EMULATOR
167
        WS_Rollback(bo->ws, 0);
168
#endif
169
170 6795
        MPL_Free(vbopool, bo);
171 6795
}