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