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 7214
Req_AcctLogCharge(struct VSC_main_wrk *ds, struct req *req)
48
{
49
        struct acct_req *a;
50
51 7214
        AN(ds);
52 7214
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
53
54 7214
        a = &req->acct;
55
56 7214
        if (req->vsl->wid && !(req->res_mode & RES_PIPE)) {
57 5086
                VSLb(req->vsl, SLT_ReqAcct, "%ju %ju %ju %ju %ju %ju",
58 5086
                    (uintmax_t)a->req_hdrbytes,
59 5086
                    (uintmax_t)a->req_bodybytes,
60 5086
                    (uintmax_t)(a->req_hdrbytes + a->req_bodybytes),
61 5086
                    (uintmax_t)a->resp_hdrbytes,
62 5086
                    (uintmax_t)a->resp_bodybytes,
63 5086
                    (uintmax_t)(a->resp_hdrbytes + a->resp_bodybytes));
64
        }
65
66
        /* Charge to main byte counters (except for ESI subrequests) */
67
#define ACCT(foo)                       \
68
        if (req->esi_level == 0)        \
69
                ds->s_##foo += a->foo;  \
70
        a->foo = 0;
71
#include "tbl/acct_fields_req.h"
72 7216
}
73
74
/*--------------------------------------------------------------------
75
 * Alloc/Free a request
76
 */
77
78
struct req *
79 3834
Req_New(const struct worker *wrk, struct sess *sp)
80
{
81
        struct pool *pp;
82
        struct req *req;
83
        uint16_t nhttp;
84
        unsigned sz, hl;
85
        char *p, *e;
86
87 3834
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
88 3834
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
89 3834
        pp = sp->pool;
90 3834
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
91
92 3834
        req = MPL_Get(pp->mpl_req, &sz);
93 3834
        AN(req);
94 3834
        req->magic = REQ_MAGIC;
95 3834
        req->sp = sp;
96 3834
        req->top = req; // esi overrides
97
98 3834
        e = (char*)req + sz;
99 3834
        p = (char*)(req + 1);
100 3834
        p = (void*)PRNDUP(p);
101 3834
        assert(p < e);
102
103 3834
        nhttp = (uint16_t)cache_param->http_max_hdr;
104 3834
        hl = HTTP_estimate(nhttp);
105
106 3834
        req->http = HTTP_create(p, nhttp, hl);
107 3834
        p += hl;
108 3834
        p = (void*)PRNDUP(p);
109 3834
        assert(p < e);
110
111 3834
        req->http0 = HTTP_create(p, nhttp, hl);
112 3834
        p += hl;
113 3834
        p = (void*)PRNDUP(p);
114 3834
        assert(p < e);
115
116 3834
        req->resp = HTTP_create(p, nhttp, hl);
117 3834
        p += hl;
118 3834
        p = (void*)PRNDUP(p);
119 3834
        assert(p < e);
120
121 3834
        sz = cache_param->vsl_buffer;
122 3834
        VSL_Setup(req->vsl, p, sz);
123 3834
        p += sz;
124 3834
        p = (void*)PRNDUP(p);
125
126 3834
        req->vfc = (void*)p;
127 3834
        INIT_OBJ(req->vfc, VFP_CTX_MAGIC);
128 3834
        p = (void*)PRNDUP(p + sizeof(*req->vfc));
129
130 3834
        req->htc = (void*)p;
131 3834
        p = (void*)PRNDUP(p + sizeof(*req->htc));
132
133 3834
        req->vdc = (void*)p;
134 3834
        INIT_OBJ(req->vdc, VDP_CTX_MAGIC);
135 3834
        VTAILQ_INIT(&req->vdc->vdp);
136 3834
        p = (void*)PRNDUP(p + sizeof(*req->vdc));
137
138 3834
        req->htc = (void*)p;
139 3834
        INIT_OBJ(req->htc, HTTP_CONN_MAGIC);
140 3834
        p = (void*)PRNDUP(p + sizeof(*req->htc));
141
142 3834
        assert(p < e);
143
144 3834
        WS_Init(req->ws, "req", p, e - p);
145
146 3834
        req->req_bodybytes = 0;
147
148 3834
        req->t_first = NAN;
149 3834
        req->t_prev = NAN;
150 3834
        req->t_req = NAN;
151
152 3834
        return (req);
153
}
154
155
void
156 3808
Req_Release(struct req *req)
157
{
158
        struct sess *sp;
159
        struct pool *pp;
160
161 3808
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
162
163
        /* Make sure the request counters have all been zeroed */
164
#define ACCT(foo) \
165
        AZ(req->acct.foo);
166
#include "tbl/acct_fields_req.h"
167
168 3808
        AZ(req->vcl);
169 3808
        if (req->vsl->wid)
170 496
                VSL_End(req->vsl);
171 3808
        sp = req->sp;
172 3808
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
173 3808
        pp = sp->pool;
174 3808
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
175 3808
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
176 3808
        MPL_AssertSane(req);
177 3808
        VSL_Flush(req->vsl, 0);
178 3808
        req->sp = NULL;
179 3808
        MPL_Free(pp->mpl_req, req);
180 3808
}
181
182
/*----------------------------------------------------------------------
183
 * TODO:
184
 * - check for code duplication with cnt_recv_prep
185
 * - re-check if complete
186
 */
187
188
void
189 216
Req_Rollback(struct req *req)
190
{
191 216
        VCL_TaskLeave(req->vcl, req->privs);
192 216
        VCL_TaskEnter(req->vcl, req->privs);
193 216
        HTTP_Copy(req->http, req->http0);
194 216
        if (WS_Overflowed(req->ws))
195 24
                req->wrk->stats->ws_client_overflow++;
196 216
        WS_Reset(req->ws, req->ws_req);
197 216
}
198
199
/*----------------------------------------------------------------------
200
 * TODO: remove code duplication with cnt_recv_prep
201
 */
202
203
void
204 4760
Req_Cleanup(struct sess *sp, struct worker *wrk, struct req *req)
205
{
206
207 4760
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
208 4760
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
209 4760
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
210 4760
        assert(sp == req->sp);
211
212 4760
        req->director_hint = NULL;
213 4760
        req->restarts = 0;
214
215 4760
        AZ(req->esi_level);
216 4760
        AZ(req->privs->magic);
217 4760
        assert(req->top == req);
218
219 4760
        if (req->vcl != NULL) {
220 4472
                if (wrk->vcl != NULL)
221 22
                        VCL_Rel(&wrk->vcl);
222 4472
                wrk->vcl = req->vcl;
223 4472
                req->vcl = NULL;
224
        }
225
226
        /* Charge and log byte counters */
227 4760
        if (req->vsl->wid) {
228 4614
                Req_AcctLogCharge(wrk->stats, req);
229 4614
                VSL_End(req->vsl);
230
        }
231 4760
        req->req_bodybytes = 0;
232
233 4760
        if (!isnan(req->t_prev) && req->t_prev > 0. && req->t_prev > sp->t_idle)
234 4447
                sp->t_idle = req->t_prev;
235
        else
236 313
                sp->t_idle = W_TIM_real(wrk);
237
238 4760
        req->t_first = NAN;
239 4760
        req->t_prev = NAN;
240 4760
        req->t_req = NAN;
241 4760
        req->req_body_status = REQ_BODY_INIT;
242
243 4760
        req->hash_always_miss = 0;
244 4760
        req->hash_ignore_busy = 0;
245 4760
        req->is_hit = 0;
246
247 4760
        if (WS_Overflowed(req->ws))
248 50
                wrk->stats->ws_client_overflow++;
249
250 4760
        WS_Reset(req->ws, 0);
251 4760
}
252
253
/*----------------------------------------------------------------------
254
 */
255
256
void v_matchproto_(vtr_req_fail_f)
257 28
Req_Fail(struct req *req, enum sess_close reason)
258
{
259 28
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
260
261 28
        AN(req->transport->req_fail);
262 28
        req->transport->req_fail(req, reason);
263 28
}