varnish-cache/bin/varnishd/cache/cache_busyobj.c
1
/*-
2
 * Copyright (c) 2013-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * Handle backend connections and backend request structures.
30
 *
31
 */
32
33
#include "config.h"
34
35
#include <stdlib.h>
36
37
#include "cache_varnishd.h"
38
#include "cache_filter.h"
39
#include "cache_objhead.h"
40
41
static struct mempool           *vbopool;
42
43
/*--------------------------------------------------------------------
44
 */
45
46
void
47 614
VBO_Init(void)
48
{
49
50 614
        vbopool = MPL_New("busyobj", &cache_param->vbo_pool,
51 614
            &cache_param->workspace_backend);
52 614
        AN(vbopool);
53 614
}
54
55
/*--------------------------------------------------------------------
56
 * BusyObj handling
57
 */
58
59
static struct busyobj *
60 1342
vbo_New(void)
61
{
62
        struct busyobj *bo;
63
        unsigned sz;
64
65 1342
        bo = MPL_Get(vbopool, &sz);
66 1342
        XXXAN(bo);
67 1342
        bo->magic = BUSYOBJ_MAGIC;
68 1342
        bo->end = (char *)bo + sz;
69 1342
        return (bo);
70
}
71
72
static void
73 1341
vbo_Free(struct busyobj **bop)
74
{
75
        struct busyobj *bo;
76
77 1341
        TAKE_OBJ_NOTNULL(bo, bop, BUSYOBJ_MAGIC);
78 1341
        AZ(bo->htc);
79 1341
        MPL_Free(vbopool, bo);
80 1341
}
81
82
struct busyobj *
83 1342
VBO_GetBusyObj(struct worker *wrk, const struct req *req)
84
{
85
        struct busyobj *bo;
86
        uint16_t nhttp;
87
        unsigned sz;
88
        char *p;
89
90 1342
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
91
92 1342
        bo = vbo_New();
93 1342
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
94
95 1342
        p = (void*)(bo + 1);
96 1342
        p = (void*)PRNDUP(p);
97 1342
        assert(p < bo->end);
98
99 1342
        nhttp = (uint16_t)cache_param->http_max_hdr;
100 1342
        sz = HTTP_estimate(nhttp);
101
102 1342
        bo->bereq0 = HTTP_create(p, nhttp, sz);
103 1342
        p += sz;
104 1342
        p = (void*)PRNDUP(p);
105 1342
        assert(p < bo->end);
106
107 1342
        bo->bereq = HTTP_create(p, nhttp, sz);
108 1342
        p += sz;
109 1342
        p = (void*)PRNDUP(p);
110 1342
        assert(p < bo->end);
111
112 1342
        bo->beresp = HTTP_create(p, nhttp, sz);
113 1342
        p += sz;
114 1342
        p = (void*)PRNDUP(p);
115 1342
        assert(p < bo->end);
116
117 1342
        sz = cache_param->vsl_buffer;
118 1342
        VSL_Setup(bo->vsl, p, sz);
119 1342
        bo->vsl->wid = VXID_Get(wrk, VSL_BACKENDMARKER);
120 1342
        p += sz;
121 1342
        p = (void*)PRNDUP(p);
122 1342
        assert(p < bo->end);
123
124 1342
        bo->vfc = (void*)p;
125 1342
        p += sizeof (*bo->vfc);
126 1342
        p = (void*)PRNDUP(p);
127 1342
        INIT_OBJ(bo->vfc, VFP_CTX_MAGIC);
128
129 1342
        WS_Init(bo->ws, "bo", p, bo->end - p);
130
131 1342
        bo->do_stream = 1;
132
133 1342
        bo->director_req = req->director_hint;
134 1342
        bo->vcl = req->vcl;
135 1342
        VCL_Ref(bo->vcl);
136
137 1342
        bo->t_first = bo->t_prev = NAN;
138
139 1342
        memcpy(bo->digest, req->digest, sizeof bo->digest);
140
141 1342
        VRTPRIV_init(bo->privs);
142
143 1342
        return (bo);
144
}
145
146
void
147 1341
VBO_ReleaseBusyObj(struct worker *wrk, struct busyobj **pbo)
148
{
149
        struct busyobj *bo;
150
151 1341
        CHECK_OBJ_ORNULL(wrk, WORKER_MAGIC);
152 1341
        TAKE_OBJ_NOTNULL(bo, pbo, BUSYOBJ_MAGIC);
153 1341
        CHECK_OBJ_ORNULL(bo->fetch_objcore, OBJCORE_MAGIC);
154
155 1341
        AZ(bo->htc);
156 1341
        AZ(bo->stale_oc);
157
158 1341
        VRTPRIV_dynamic_kill(bo->privs, (uintptr_t)bo);
159 1341
        assert(VTAILQ_EMPTY(&bo->privs->privs));
160
161 8046
        VSLb(bo->vsl, SLT_BereqAcct, "%ju %ju %ju %ju %ju %ju",
162 1341
            (uintmax_t)bo->acct.bereq_hdrbytes,
163 1341
            (uintmax_t)bo->acct.bereq_bodybytes,
164 1341
            (uintmax_t)(bo->acct.bereq_hdrbytes + bo->acct.bereq_bodybytes),
165 1341
            (uintmax_t)bo->acct.beresp_hdrbytes,
166 1341
            (uintmax_t)bo->acct.beresp_bodybytes,
167 1341
            (uintmax_t)(bo->acct.beresp_hdrbytes + bo->acct.beresp_bodybytes));
168
169 1341
        VSL_End(bo->vsl);
170
171 1341
        AZ(bo->htc);
172
173 1341
        if (bo->fetch_objcore != NULL) {
174 1329
                AN(wrk);
175 1329
                (void)HSH_DerefObjCore(wrk, &bo->fetch_objcore,
176
                    HSH_RUSH_POLICY);
177
        }
178
179 1341
        VCL_Rel(&bo->vcl);
180
181 1341
        memset(&bo->retries, 0,
182
            sizeof *bo - offsetof(struct busyobj, retries));
183
184 1341
        vbo_Free(&bo);
185 1341
}