varnish-cache/bin/varnishd/cache/cache_req.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2011 Varnish Software AS
4
 * All rights reserved.
5
 *
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
 * Request management
30
 *
31
 */
32
33
#include "config.h"
34
35
#include "cache_varnishd.h"
36
#include "cache_filter.h"
37
38
#include <stdio.h>
39
#include <stdlib.h>
40
41
#include "cache_pool.h"
42
#include "cache_transport.h"
43
44
#include "vtim.h"
45
46
void
47 2840
Req_AcctLogCharge(struct VSC_main *ds, struct req *req)
48
{
49
        struct acct_req *a;
50
51 2840
        AN(ds);
52 2840
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
53
54 2840
        a = &req->acct;
55
56 2840
        if (req->vsl->wid && !(req->res_mode & RES_PIPE)) {
57 4000
                VSLb(req->vsl, SLT_ReqAcct, "%ju %ju %ju %ju %ju %ju",
58
                    (uintmax_t)a->req_hdrbytes,
59
                    (uintmax_t)a->req_bodybytes,
60 2000
                    (uintmax_t)(a->req_hdrbytes + a->req_bodybytes),
61
                    (uintmax_t)a->resp_hdrbytes,
62
                    (uintmax_t)a->resp_bodybytes,
63 2000
                    (uintmax_t)(a->resp_hdrbytes + a->resp_bodybytes));
64
        }
65
66
#define ACCT(foo)                       \
67
        ds->s_##foo += a->foo;          \
68
        a->foo = 0;
69
#include "tbl/acct_fields_req.h"
70 2840
}
71
72
/*--------------------------------------------------------------------
73
 * Alloc/Free a request
74
 */
75
76
struct req *
77 1549
Req_New(const struct worker *wrk, struct sess *sp)
78
{
79
        struct pool *pp;
80
        struct req *req;
81
        uint16_t nhttp;
82
        unsigned sz, hl;
83
        char *p, *e;
84
85 1549
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
86 1549
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
87 1549
        pp = sp->pool;
88 1549
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
89
90 1549
        req = MPL_Get(pp->mpl_req, &sz);
91 1549
        AN(req);
92 1549
        req->magic = REQ_MAGIC;
93 1549
        req->sp = sp;
94 1549
        req->top = req; // esi overrides
95
96 1549
        INIT_OBJ(req->htc, HTTP_CONN_MAGIC);
97
98 1549
        e = (char*)req + sz;
99 1549
        p = (char*)(req + 1);
100 1549
        p = (void*)PRNDUP(p);
101 1549
        assert(p < e);
102
103 1549
        nhttp = (uint16_t)cache_param->http_max_hdr;
104 1549
        hl = HTTP_estimate(nhttp);
105
106 1550
        req->http = HTTP_create(p, nhttp, hl);
107 1550
        p += hl;
108 1550
        p = (void*)PRNDUP(p);
109 1550
        assert(p < e);
110
111 1550
        req->http0 = HTTP_create(p, nhttp, hl);
112 1550
        p += hl;
113 1550
        p = (void*)PRNDUP(p);
114 1550
        assert(p < e);
115
116 1550
        req->resp = HTTP_create(p, nhttp, hl);
117 1550
        p += hl;
118 1550
        p = (void*)PRNDUP(p);
119 1550
        assert(p < e);
120
121 1550
        sz = cache_param->vsl_buffer;
122 1550
        VSL_Setup(req->vsl, p, sz);
123 1550
        p += sz;
124 1550
        p = (void*)PRNDUP(p);
125
126 1550
        req->vfc = (void*)p;
127 1550
        INIT_OBJ(req->vfc, VFP_CTX_MAGIC);
128 1550
        p = (void*)PRNDUP(p + sizeof(*req->vfc));
129
130 1550
        req->vdc = (void*)p;
131 1550
        INIT_OBJ(req->vdc, VDP_CTX_MAGIC);
132 1550
        VTAILQ_INIT(&req->vdc->vdp);
133 1550
        p = (void*)PRNDUP(p + sizeof(*req->vdc));
134
135 1550
        assert(p < e);
136
137 1550
        WS_Init(req->ws, "req", p, e - p);
138
139 1550
        req->req_bodybytes = 0;
140
141 1550
        req->t_first = NAN;
142 1550
        req->t_prev = NAN;
143 1550
        req->t_req = NAN;
144
145 1550
        VRTPRIV_init(req->privs);
146
147 1550
        return (req);
148
}
149
150
void
151 1544
Req_Release(struct req *req)
152
{
153
        struct sess *sp;
154
        struct pool *pp;
155
156 1544
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
157
158
        /* Make sure the request counters have all been zeroed */
159
#define ACCT(foo) \
160
        AZ(req->acct.foo);
161
#include "tbl/acct_fields_req.h"
162
163 1544
        AZ(req->vcl);
164 1544
        if (req->vsl->wid)
165 246
                VSL_End(req->vsl);
166 1544
        sp = req->sp;
167 1544
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
168 1544
        pp = sp->pool;
169 1544
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
170 1544
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
171 1544
        MPL_AssertSane(req);
172 1544
        VSL_Flush(req->vsl, 0);
173 1544
        req->sp = NULL;
174 1544
        MPL_Free(pp->mpl_req, req);
175 1544
}
176
177
/*----------------------------------------------------------------------
178
 * TODO: remove code duplication with cnt_recv_prep
179
 */
180
181
void
182 1833
Req_Cleanup(struct sess *sp, struct worker *wrk, struct req *req)
183
{
184
185 1833
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
186 1833
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
187 1833
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
188 1833
        assert(sp == req->sp);
189
190 1833
        req->director_hint = NULL;
191 1833
        req->restarts = 0;
192
193 1833
        AZ(req->esi_level);
194 1833
        assert(req->top == req);
195
196 1833
        if (req->vcl != NULL) {
197 1708
                if (wrk->vcl != NULL)
198 3
                        VCL_Rel(&wrk->vcl);
199 1708
                wrk->vcl = req->vcl;
200 1708
                req->vcl = NULL;
201
        }
202
203 1833
        VRTPRIV_dynamic_kill(req->privs, (uintptr_t)req);
204 1833
        VRTPRIV_dynamic_kill(req->privs, (uintptr_t)&req->top);
205 1833
        assert(VTAILQ_EMPTY(&req->privs->privs));
206
207
        /* Charge and log byte counters */
208 1833
        if (req->vsl->wid) {
209 1764
                Req_AcctLogCharge(wrk->stats, req);
210 1764
                VSL_End(req->vsl);
211
        }
212 1833
        req->req_bodybytes = 0;
213
214 1833
        if (!isnan(req->t_prev) && req->t_prev > 0.)
215 1708
                sp->t_idle = req->t_prev;
216
        else
217 125
                sp->t_idle = W_TIM_real(wrk);
218
219 1833
        req->t_first = NAN;
220 1833
        req->t_prev = NAN;
221 1833
        req->t_req = NAN;
222 1833
        req->req_body_status = REQ_BODY_INIT;
223
224 1833
        req->hash_always_miss = 0;
225 1833
        req->hash_ignore_busy = 0;
226 1833
        req->is_hit = 0;
227
228 1833
        WS_Reset(req->ws, 0);
229 1833
}
230
231
/*----------------------------------------------------------------------
232
 */
233
234
void v_matchproto_(vtr_req_fail_f)
235 13
Req_Fail(struct req *req, enum sess_close reason)
236
{
237 13
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
238
239 13
        AN(req->transport->req_fail);
240 13
        req->transport->req_fail(req, reason);
241 13
}