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 2055
VBO_Init(void)
48
{
49
50 2055
        vbopool = MPL_New("busyobj", &cache_param->vbo_pool,
51 2055
            &cache_param->workspace_backend);
52 2055
        AN(vbopool);
53 2055
}
54
55
/*--------------------------------------------------------------------
56
 * BusyObj handling
57
 */
58
59
static struct busyobj *
60 4944
vbo_New(void)
61
{
62
        struct busyobj *bo;
63
        unsigned sz;
64
65 4944
        bo = MPL_Get(vbopool, &sz);
66 4944
        XXXAN(bo);
67 4944
        bo->magic = BUSYOBJ_MAGIC;
68 4944
        bo->end = (char *)bo + sz;
69 4944
        return (bo);
70
}
71
72
static void
73 4941
vbo_Free(struct busyobj **bop)
74
{
75
        struct busyobj *bo;
76
77 4941
        TAKE_OBJ_NOTNULL(bo, bop, BUSYOBJ_MAGIC);
78 4941
        AZ(bo->htc);
79 4941
        MPL_Free(vbopool, bo);
80 4941
}
81
82
struct busyobj *
83 4944
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 4944
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
91
92 4944
        bo = vbo_New();
93 4944
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
94
95 4944
        p = (void*)(bo + 1);
96 4944
        p = (void*)PRNDUP(p);
97 4944
        assert(p < bo->end);
98
99 4944
        nhttp = (uint16_t)cache_param->http_max_hdr;
100 4944
        sz = HTTP_estimate(nhttp);
101
102 4944
        bo->bereq0 = HTTP_create(p, nhttp, sz);
103 4944
        p += sz;
104 4944
        p = (void*)PRNDUP(p);
105 4944
        assert(p < bo->end);
106
107 4944
        bo->bereq = HTTP_create(p, nhttp, sz);
108 4944
        p += sz;
109 4944
        p = (void*)PRNDUP(p);
110 4944
        assert(p < bo->end);
111
112 4944
        bo->beresp = HTTP_create(p, nhttp, sz);
113 4944
        p += sz;
114 4944
        p = (void*)PRNDUP(p);
115 4944
        assert(p < bo->end);
116
117 4944
        sz = cache_param->vsl_buffer;
118 4944
        VSL_Setup(bo->vsl, p, sz);
119 4944
        bo->vsl->wid = VXID_Get(wrk, VSL_BACKENDMARKER);
120 4944
        p += sz;
121 4944
        p = (void*)PRNDUP(p);
122 4944
        assert(p < bo->end);
123
124 4944
        bo->vfc = (void*)p;
125 4944
        p += sizeof (*bo->vfc);
126 4944
        p = (void*)PRNDUP(p);
127 4944
        INIT_OBJ(bo->vfc, VFP_CTX_MAGIC);
128
129 4944
        WS_Init(bo->ws, "bo", p, bo->end - p);
130
131 4944
        bo->do_stream = 1;
132
133 4944
        bo->director_req = req->director_hint;
134 4944
        bo->vcl = req->vcl;
135 4944
        VCL_Ref(bo->vcl);
136
137 4944
        bo->t_first = bo->t_prev = NAN;
138
139 4944
        memcpy(bo->digest, req->digest, sizeof bo->digest);
140
141 4944
        VRTPRIV_init(bo->privs);
142
143 4944
        return (bo);
144
}
145
146
void
147 4941
VBO_ReleaseBusyObj(struct worker *wrk, struct busyobj **pbo)
148
{
149
        struct busyobj *bo;
150
151 4941
        CHECK_OBJ_ORNULL(wrk, WORKER_MAGIC);
152 4941
        TAKE_OBJ_NOTNULL(bo, pbo, BUSYOBJ_MAGIC);
153 4941
        CHECK_OBJ_ORNULL(bo->fetch_objcore, OBJCORE_MAGIC);
154
155 4941
        AZ(bo->htc);
156 4941
        AZ(bo->stale_oc);
157
158 4941
        VRTPRIV_dynamic_kill(bo->privs, (uintptr_t)bo);
159 4941
        assert(VTAILQ_EMPTY(&bo->privs->privs));
160
161 4941
        VSLb(bo->vsl, SLT_BereqAcct, "%ju %ju %ju %ju %ju %ju",
162 4941
            (uintmax_t)bo->acct.bereq_hdrbytes,
163 4941
            (uintmax_t)bo->acct.bereq_bodybytes,
164 4941
            (uintmax_t)(bo->acct.bereq_hdrbytes + bo->acct.bereq_bodybytes),
165 4941
            (uintmax_t)bo->acct.beresp_hdrbytes,
166 4941
            (uintmax_t)bo->acct.beresp_bodybytes,
167 4941
            (uintmax_t)(bo->acct.beresp_hdrbytes + bo->acct.beresp_bodybytes));
168
169 4941
        VSL_End(bo->vsl);
170
171 4941
        AZ(bo->htc);
172
173 4941
        if (bo->fetch_objcore != NULL) {
174 4899
                AN(wrk);
175 4899
                (void)HSH_DerefObjCore(wrk, &bo->fetch_objcore,
176
                    HSH_RUSH_POLICY);
177
        }
178
179 4941
        VCL_Rel(&bo->vcl);
180
181 4941
        memset(&bo->retries, 0,
182
            sizeof *bo - offsetof(struct busyobj, retries));
183
184 4941
        vbo_Free(&bo);
185 4941
}