varnish-cache/bin/varnishd/cache/cache_req.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * Request management
31
 *
32
 */
33
34
#include "config.h"
35
36 116228
#include <stdio.h>
37 116228
#include <stdlib.h>
38 116228
39 116228
#include "cache_varnishd.h"
40
#include "cache_filter.h"
41
#include "cache_pool.h"
42
#include "cache_transport.h"
43
44
#include "common/heritage.h"
45
#include "vtim.h"
46
47
void
48 207243
Req_AcctLogCharge(struct VSC_main_wrk *ds, struct req *req)
49
{
50
        struct acct_req *a;
51
52 207243
        AN(ds);
53 207243
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
54
55 207243
        a = &req->acct;
56
57 207243
        if (!IS_NO_VXID(req->vsl->wid) && !(req->res_mode & RES_PIPE)) {
58 300326
                VSLb(req->vsl, SLT_ReqAcct, "%ju %ju %ju %ju %ju %ju",
59 150163
                    (uintmax_t)a->req_hdrbytes,
60 150163
                    (uintmax_t)a->req_bodybytes,
61 150163
                    (uintmax_t)(a->req_hdrbytes + a->req_bodybytes),
62 150163
                    (uintmax_t)a->resp_hdrbytes,
63 150163
                    (uintmax_t)a->resp_bodybytes,
64 150163
                    (uintmax_t)(a->resp_hdrbytes + a->resp_bodybytes));
65 150163
        }
66
67 207243
        if (IS_TOPREQ(req)) {
68
#define ACCT(foo) ds->s_##foo += a->foo;
69
#include "tbl/acct_fields_req.h"
70
        }
71 207243
        memset(a, 0, sizeof *a);
72 207243
}
73
74
void
75 42257
Req_LogHit(struct worker *wrk, struct req *req, struct objcore *oc,
76
    intmax_t fetch_progress)
77
{
78
        const char *clen, *sep;
79
80 42257
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
81 42257
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
82 42257
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
83
84 42257
        if (fetch_progress >= 0) {
85 3735
                clen = HTTP_GetHdrPack(wrk, oc, H_Content_Length);
86 3735
                if (clen == NULL)
87 319
                        clen = sep = "";
88
                else
89 3416
                        sep = " ";
90 7470
                VSLb(req->vsl, SLT_Hit, "%ju %.6f %.6f %.6f %jd%s%s",
91 3735
                    VXID(ObjGetXID(wrk, oc)), EXP_Dttl(req, oc),
92 3735
                    oc->grace, oc->keep,
93 3735
                    fetch_progress, sep, clen);
94 3735
        } else {
95 77044
                VSLb(req->vsl, SLT_Hit, "%ju %.6f %.6f %.6f",
96 38522
                    VXID(ObjGetXID(wrk, oc)), EXP_Dttl(req, oc),
97 38522
                    oc->grace, oc->keep);
98
        }
99 42257
}
100
101
const char *
102 146010
Req_LogStart(const struct worker *wrk, struct req *req)
103
{
104
        const char *ci, *cp, *endpname;
105
106 146010
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
107 146010
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
108 146010
        CHECK_OBJ_NOTNULL(req->sp, SESS_MAGIC);
109
110 146010
        ci = SES_Get_String_Attr(req->sp, SA_CLIENT_IP);
111 146010
        cp = SES_Get_String_Attr(req->sp, SA_CLIENT_PORT);
112 146010
        CHECK_OBJ_NOTNULL(req->sp->listen_sock, LISTEN_SOCK_MAGIC);
113 146010
        endpname = req->sp->listen_sock->name;
114 146010
        AN(endpname);
115 146010
        VSLb(req->vsl, SLT_ReqStart, "%s %s %s", ci, cp, endpname);
116
117 146010
        return (ci);
118
}
119
120
/*--------------------------------------------------------------------
121
 * Alloc/Free a request
122
 */
123
124
struct req *
125 117097
Req_New(struct sess *sp)
126
{
127
        struct pool *pp;
128
        struct req *req;
129
        uint16_t nhttp;
130
        unsigned sz, hl;
131
        char *p, *e;
132
133 117097
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
134 117097
        pp = sp->pool;
135 117097
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
136
137 117097
        req = MPL_Get(pp->mpl_req, &sz);
138 117097
        AN(req);
139 117097
        req->magic = REQ_MAGIC;
140 117097
        req->sp = sp;
141
142 117097
        e = (char*)req + sz;
143 117097
        p = (char*)(req + 1);
144 117097
        p = (void*)PRNDUP(p);
145 117097
        assert(p < e);
146
147 117097
        nhttp = (uint16_t)cache_param->http_max_hdr;
148 117097
        hl = HTTP_estimate(nhttp);
149
150 117097
        req->http = HTTP_create(p, nhttp, hl);
151 117097
        p += hl;
152 117097
        p = (void*)PRNDUP(p);
153 117097
        assert(p < e);
154
155 117097
        req->http0 = HTTP_create(p, nhttp, hl);
156 117097
        p += hl;
157 117097
        p = (void*)PRNDUP(p);
158 117097
        assert(p < e);
159
160 117097
        req->resp = HTTP_create(p, nhttp, hl);
161 117097
        p += hl;
162 117097
        p = (void*)PRNDUP(p);
163 117097
        assert(p < e);
164
165 117097
        sz = cache_param->vsl_buffer;
166 117097
        VSL_Setup(req->vsl, p, sz);
167 117097
        p += sz;
168 117097
        p = (void*)PRNDUP(p);
169
170 117097
        req->vfc = (void*)p;
171 117097
        INIT_OBJ(req->vfc, VFP_CTX_MAGIC);
172 117097
        p = (void*)PRNDUP(p + sizeof(*req->vfc));
173
174 117097
        req->vdc = (void*)p;
175 117097
        memset(req->vdc, 0, sizeof *req->vdc);
176 117097
        p = (void*)PRNDUP(p + sizeof(*req->vdc));
177
178 117097
        req->htc = (void*)p;
179 117097
        INIT_OBJ(req->htc, HTTP_CONN_MAGIC);
180 117097
        req->htc->doclose = SC_NULL;
181 117097
        p = (void*)PRNDUP(p + sizeof(*req->htc));
182
183 117097
        req->top = (void*)p;
184 117097
        INIT_OBJ(req->top, REQTOP_MAGIC);
185 117097
        req->top->topreq = req;
186 117097
        p = (void*)PRNDUP(p + sizeof(*req->top));
187
188 117097
        assert(p < e);
189
190 117097
        WS_Init(req->ws, "req", p, e - p);
191
192 117097
        req->t_first = NAN;
193 117097
        req->t_prev = NAN;
194 117097
        req->t_req = NAN;
195 117097
        req->req_step = R_STP_TRANSPORT;
196 117097
        req->doclose = SC_NULL;
197
198 117097
        return (req);
199
}
200
201
void
202 116228
Req_Release(struct req *req)
203
{
204
        struct sess *sp;
205
        struct pool *pp;
206
207 116228
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
208
209
        /* Make sure the request counters have all been zeroed */
210
#define ACCT(foo) \
211
        AZ(req->acct.foo);
212
#include "tbl/acct_fields_req.h"
213
214 116228
        AZ(req->vcl);
215 116228
        if (!IS_NO_VXID(req->vsl->wid))
216 80
                VSL_End(req->vsl);
217
#ifdef ENABLE_WORKSPACE_EMULATOR
218
        WS_Rollback(req->ws, 0);
219
#endif
220 116228
        TAKE_OBJ_NOTNULL(sp, &req->sp, SESS_MAGIC);
221
        pp = sp->pool;
222 116228
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
223 116228
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
224
        MPL_AssertSane(req);
225
        VSL_Flush(req->vsl, 0);
226
        MPL_Free(pp->mpl_req, req);
227
}
228
229
/*----------------------------------------------------------------------
230
 * TODO:
231
 * - check for code duplication with cnt_recv_prep
232
 * - re-check if complete
233
 * - XXX should PRIV_TOP use vcl0?
234
 * - XXX PRIV_TOP does not get rolled back, should it for !IS_TOPREQ ?
235
 */
236
237
void
238 7358
Req_Rollback(VRT_CTX)
239
{
240
        struct req *req;
241
242 7358
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
243 7358
        req = ctx->req;
244 7358
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
245
246 7358
        if (IS_TOPREQ(req))
247 7239
                VCL_TaskLeave(ctx, req->top->privs);
248 7358
        VCL_TaskLeave(ctx, req->privs);
249 7358
        VCL_TaskEnter(req->privs);
250 7358
        if (IS_TOPREQ(req))
251 7237
                VCL_TaskEnter(req->top->privs);
252 7358
        HTTP_Clone(req->http, req->http0);
253 7358
        req->vdp_filter_list = NULL;
254 7358
        req->vfp_filter_list = NULL;
255 7358
        req->vcf = NULL;
256 7358
        if (WS_Overflowed(req->ws))
257 400
                req->wrk->stats->ws_client_overflow++;
258 7358
        AN(req->ws_req);
259 7358
        WS_Rollback(req->ws, req->ws_req);
260 7358
}
261
262
/*----------------------------------------------------------------------
263
 * TODO: remove code duplication with cnt_recv_prep
264
 */
265
266
void
267 156550
Req_Cleanup(struct sess *sp, struct worker *wrk, struct req *req)
268
{
269
270 156550
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
271 156550
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
272 156550
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
273 156550
        assert(sp == req->sp);
274 156550
        if (IS_TOPREQ(req))
275 141910
                AZ(req->top->vcl0);
276
277 156550
        AZ(req->director_hint);
278 156550
        req->restarts = 0;
279
280 156550
        if (req->vcl != NULL)
281 127653
                VCL_Recache(wrk, &req->vcl);
282
283
        /* Charge and log byte counters */
284 156550
        if (!IS_NO_VXID(req->vsl->wid)) {
285 151079
                Req_AcctLogCharge(wrk->stats, req);
286 151079
                if (!IS_SAME_VXID(req->vsl->wid, sp->vxid))
287 145693
                        VSL_End(req->vsl);
288
                else
289 5386
                        req->vsl->wid = NO_VXID; /* ending an h2 stream 0 */
290 151079
        }
291
292 156550
        if (!isnan(req->t_prev) && req->t_prev > 0. && req->t_prev > sp->t_idle)
293 144210
                sp->t_idle = req->t_prev;
294
        else
295 12340
                sp->t_idle = W_TIM_real(wrk);
296
297 156550
        req->t_first = NAN;
298 156550
        req->t_prev = NAN;
299 156550
        req->t_req = NAN;
300 156550
        req->req_body_status = NULL;
301
302 156550
        req->hash_always_miss = 0;
303 156550
        req->hash_ignore_busy = 0;
304 156550
        req->hash_ignore_vary = 0;
305 156550
        req->esi_level = 0;
306 156550
        req->is_hit = 0;
307 156550
        req->req_step = R_STP_TRANSPORT;
308 156550
        req->vcf = NULL;
309 156550
        req->doclose = SC_NULL;
310 156550
        req->htc->doclose = SC_NULL;
311 156550
        req->htc->body_status = NULL;
312
313 156550
        if (WS_Overflowed(req->ws))
314 6680
                wrk->stats->ws_client_overflow++;
315
316 156550
        wrk->seen_methods = 0;
317
318 156550
        VDP_Fini(req->vdc);
319 156550
}
320
321
/*----------------------------------------------------------------------
322
 */
323
324
void v_matchproto_(vtr_req_fail_f)
325 1160
Req_Fail(struct req *req, stream_close_t reason)
326
{
327 1160
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
328
329 1160
        AN(req->transport->req_fail);
330 1160
        req->transport->req_fail(req, reason);
331 1160
}