| | varnish-cache/bin/varnishd/cache/cache_session.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 |
|
* Session management |
31 |
|
* |
32 |
|
* The overall goal here is to hold as little state as possible for an |
33 |
|
* idle session. This leads to various nasty-ish overloads of struct |
34 |
|
* sess fields, for instance ->fd being negative ->reason. |
35 |
|
* |
36 |
|
*/ |
37 |
|
//lint --e{766} |
38 |
|
|
39 |
|
#include "config.h" |
40 |
|
|
41 |
|
#include "cache_varnishd.h" |
42 |
|
|
43 |
|
#include <stdio.h> |
44 |
|
#include <stdlib.h> |
45 |
|
|
46 |
|
#include "cache_pool.h" |
47 |
|
#include "cache_transport.h" |
48 |
|
|
49 |
|
#include "vsa.h" |
50 |
|
#include "vtcp.h" |
51 |
|
#include "vtim.h" |
52 |
|
#include "waiter/waiter.h" |
53 |
|
|
54 |
|
static const struct { |
55 |
|
const char *type; |
56 |
|
} sess_attr[SA_LAST] = { |
57 |
|
#define SESS_ATTR(UC, lc, typ, len) [SA_##UC] = { #typ }, |
58 |
|
#include "tbl/sess_attr.h" |
59 |
|
}; |
60 |
|
|
61 |
|
enum sess_close { |
62 |
|
SCE_NULL = 0, |
63 |
|
#define SESS_CLOSE(nm, stat, err, desc) SCE_##nm, |
64 |
|
#include "tbl/sess_close.h" |
65 |
|
SCE_MAX, |
66 |
|
}; |
67 |
|
|
68 |
|
const struct stream_close SC_NULL[1] = {{ |
69 |
|
.magic = STREAM_CLOSE_MAGIC, |
70 |
|
.idx = SCE_NULL, |
71 |
|
.is_err = 0, |
72 |
|
.name = "null", |
73 |
|
.desc = "Not Closing", |
74 |
|
}}; |
75 |
|
|
76 |
|
#define SESS_CLOSE(nm, stat, err, text) \ |
77 |
|
const struct stream_close SC_##nm[1] = {{ \ |
78 |
|
.magic = STREAM_CLOSE_MAGIC, \ |
79 |
|
.idx = SCE_##nm, \ |
80 |
|
.is_err = err, \ |
81 |
|
.name = #nm, \ |
82 |
|
.desc = text, \ |
83 |
|
}}; |
84 |
|
#include "tbl/sess_close.h" |
85 |
|
|
86 |
|
static const stream_close_t sc_lookup[SCE_MAX] = { |
87 |
|
[SCE_NULL] = SC_NULL, |
88 |
|
#define SESS_CLOSE(nm, stat, err, desc) \ |
89 |
|
[SCE_##nm] = SC_##nm, |
90 |
|
#include "tbl/sess_close.h" |
91 |
|
}; |
92 |
|
|
93 |
|
/*--------------------------------------------------------------------*/ |
94 |
|
|
95 |
|
void |
96 |
92251 |
SES_SetTransport(struct worker *wrk, struct sess *sp, struct req *req, |
97 |
|
const struct transport *xp) |
98 |
|
{ |
99 |
|
|
100 |
92251 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
101 |
92251 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
102 |
92251 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
103 |
92251 |
CHECK_OBJ_NOTNULL(xp, TRANSPORT_MAGIC); |
104 |
92251 |
assert(xp->number > 0); |
105 |
|
|
106 |
92251 |
sp->sattr[SA_TRANSPORT] = xp->number; |
107 |
92251 |
req->transport = xp; |
108 |
92251 |
wrk->task->func = xp->new_session; |
109 |
92251 |
wrk->task->priv = req; |
110 |
92251 |
} |
111 |
|
|
112 |
|
/*--------------------------------------------------------------------*/ |
113 |
|
|
114 |
|
#define SES_NOATTR_OFFSET 0xffff |
115 |
|
|
116 |
|
static int |
117 |
940608 |
ses_get_attr(const struct sess *sp, enum sess_attr a, void **dst) |
118 |
|
{ |
119 |
940608 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
120 |
940608 |
assert(a < SA_LAST); |
121 |
940608 |
AN(dst); |
122 |
|
|
123 |
940608 |
if (sp->sattr[a] == SES_NOATTR_OFFSET) { |
124 |
83456 |
*dst = NULL; |
125 |
83456 |
return (-1); |
126 |
|
} |
127 |
857152 |
*dst = WS_AtOffset(sp->ws, sp->sattr[a], 0); |
128 |
857152 |
return (0); |
129 |
940608 |
} |
130 |
|
|
131 |
|
static int |
132 |
460883 |
ses_set_attr(const struct sess *sp, enum sess_attr a, const void *src, int sz) |
133 |
|
{ |
134 |
|
void *dst; |
135 |
460883 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
136 |
460883 |
assert(a < SA_LAST); |
137 |
460883 |
AN(src); |
138 |
460883 |
assert(sz > 0); |
139 |
|
|
140 |
460883 |
if (sp->sattr[a] == SES_NOATTR_OFFSET) |
141 |
0 |
return (-1); |
142 |
460883 |
dst = WS_AtOffset(sp->ws, sp->sattr[a], sz); |
143 |
460883 |
AN(dst); |
144 |
460883 |
memcpy(dst, src, sz); |
145 |
460883 |
return (0); |
146 |
460883 |
} |
147 |
|
|
148 |
|
static int |
149 |
420511 |
ses_res_attr(struct sess *sp, enum sess_attr a, void **dst, ssize_t *szp) |
150 |
|
{ |
151 |
|
unsigned o; |
152 |
|
ssize_t sz; |
153 |
|
|
154 |
420511 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
155 |
420511 |
assert(a < SA_LAST); |
156 |
420511 |
AN(dst); |
157 |
420511 |
sz = *szp; |
158 |
420511 |
*szp = 0; |
159 |
420511 |
assert(sz >= 0); |
160 |
420511 |
if (WS_ReserveSize(sp->ws, sz) == 0) |
161 |
40 |
return (0); |
162 |
420471 |
o = WS_ReservationOffset(sp->ws); |
163 |
420471 |
if (o >= SES_NOATTR_OFFSET) { |
164 |
0 |
WS_Release(sp->ws, 0); |
165 |
0 |
return (0); |
166 |
|
} |
167 |
420471 |
*dst = WS_Reservation(sp->ws); |
168 |
420471 |
*szp = sz; |
169 |
420471 |
sp->sattr[a] = (uint16_t)o; |
170 |
420471 |
WS_Release(sp->ws, sz); |
171 |
420471 |
return (1); |
172 |
420511 |
} |
173 |
|
|
174 |
|
#define SESS_ATTR(UP, low, typ, len) \ |
175 |
|
int \ |
176 |
|
SES_Set_##low(const struct sess *sp, const typ *src) \ |
177 |
|
{ \ |
178 |
|
assert(len > 0); \ |
179 |
|
return (ses_set_attr(sp, SA_##UP, src, len)); \ |
180 |
|
} \ |
181 |
|
\ |
182 |
|
int \ |
183 |
|
SES_Get_##low(const struct sess *sp, typ **dst) \ |
184 |
|
{ \ |
185 |
|
assert(len > 0); \ |
186 |
|
return (ses_get_attr(sp, SA_##UP, (void**)dst)); \ |
187 |
|
} \ |
188 |
|
\ |
189 |
|
int \ |
190 |
|
SES_Reserve_##low(struct sess *sp, typ **dst, ssize_t *sz) \ |
191 |
|
{ \ |
192 |
|
assert(len > 0); \ |
193 |
|
AN(sz); \ |
194 |
|
*sz = len; \ |
195 |
|
return (ses_res_attr(sp, SA_##UP, (void**)dst, sz)); \ |
196 |
|
} |
197 |
|
|
198 |
|
#include "tbl/sess_attr.h" |
199 |
|
|
200 |
|
int |
201 |
172805 |
SES_Set_String_Attr(struct sess *sp, enum sess_attr a, const char *src) |
202 |
|
{ |
203 |
|
void *q; |
204 |
|
ssize_t l, sz; |
205 |
|
|
206 |
172805 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
207 |
172805 |
AN(src); |
208 |
|
|
209 |
172805 |
assert(a < SA_LAST); |
210 |
172805 |
if (strcmp(sess_attr[a].type, "char")) |
211 |
0 |
WRONG("wrong sess_attr: not char"); |
212 |
|
|
213 |
172805 |
l = sz = strlen(src) + 1; |
214 |
172805 |
if (! ses_res_attr(sp, a, &q, &sz)) |
215 |
0 |
return (0); |
216 |
172805 |
assert(l == sz); |
217 |
172805 |
strcpy(q, src); |
218 |
172805 |
return (1); |
219 |
172805 |
} |
220 |
|
|
221 |
|
const char * |
222 |
295475 |
SES_Get_String_Attr(const struct sess *sp, enum sess_attr a) |
223 |
|
{ |
224 |
|
void *q; |
225 |
|
|
226 |
295475 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
227 |
|
|
228 |
295475 |
assert(a < SA_LAST); |
229 |
295475 |
if (strcmp(sess_attr[a].type, "char")) |
230 |
0 |
WRONG("wrong sess_attr: not char"); |
231 |
|
|
232 |
295475 |
if (ses_get_attr(sp, a, &q) < 0) |
233 |
0 |
return (NULL); |
234 |
295475 |
return (q); |
235 |
295475 |
} |
236 |
|
|
237 |
|
/*--------------------------------------------------------------------*/ |
238 |
|
|
239 |
|
void |
240 |
4720 |
HTC_Status(enum htc_status_e e, const char **name, const char **desc) |
241 |
|
{ |
242 |
|
|
243 |
4720 |
switch (e) { |
244 |
|
#define HTC_STATUS(e, n, s, l) \ |
245 |
|
case HTC_S_ ## e: \ |
246 |
|
*name = s; \ |
247 |
|
*desc = l; \ |
248 |
|
return; |
249 |
|
#include "tbl/htc.h" |
250 |
|
default: |
251 |
0 |
WRONG("HTC_Status"); |
252 |
|
} |
253 |
4720 |
} |
254 |
|
|
255 |
|
/*--------------------------------------------------------------------*/ |
256 |
|
|
257 |
|
void |
258 |
321079 |
HTC_RxInit(struct http_conn *htc, struct ws *ws) |
259 |
|
{ |
260 |
|
unsigned rollback; |
261 |
|
int l; |
262 |
|
|
263 |
321079 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
264 |
321079 |
htc->ws = ws; |
265 |
|
|
266 |
|
/* NB: HTTP/1 keep-alive triggers a rollback, so does the first |
267 |
|
* request of a session or an h2 request where the rollback is a |
268 |
|
* no-op in terms of workspace usage. |
269 |
|
*/ |
270 |
321079 |
rollback = !strcasecmp(ws->id, "req") && htc->body_status == NULL; |
271 |
321079 |
l = WS_Pipeline(htc->ws, htc->pipeline_b, htc->pipeline_e, rollback); |
272 |
321079 |
xxxassert(l >= 0); |
273 |
|
|
274 |
321079 |
htc->rxbuf_b = WS_Reservation(ws); |
275 |
321079 |
htc->rxbuf_e = htc->rxbuf_b + l; |
276 |
321079 |
htc->pipeline_b = NULL; |
277 |
321079 |
htc->pipeline_e = NULL; |
278 |
321079 |
} |
279 |
|
|
280 |
|
void |
281 |
246471 |
HTC_RxPipeline(struct http_conn *htc, char *p) |
282 |
|
{ |
283 |
|
|
284 |
246471 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
285 |
246471 |
assert(p >= htc->rxbuf_b); |
286 |
246471 |
assert(p <= htc->rxbuf_e); |
287 |
246471 |
if (p == htc->rxbuf_e) { |
288 |
186403 |
htc->pipeline_b = NULL; |
289 |
186403 |
htc->pipeline_e = NULL; |
290 |
186403 |
} else { |
291 |
60068 |
htc->pipeline_b = p; |
292 |
60068 |
htc->pipeline_e = htc->rxbuf_e; |
293 |
|
} |
294 |
246471 |
} |
295 |
|
|
296 |
|
/*---------------------------------------------------------------------- |
297 |
|
* Receive a request/packet/whatever, with timeouts |
298 |
|
* |
299 |
|
* maxbytes is the maximum number of bytes the caller expects to need to |
300 |
|
* reach a complete work unit. Note that due to pipelining the actual |
301 |
|
* number of bytes passed to func in htc->rxbuf_b through htc->rxbuf_e may |
302 |
|
* be larger. |
303 |
|
* |
304 |
|
* *t1 becomes time of first non-idle rx |
305 |
|
* *t2 becomes time of complete rx |
306 |
|
* ti is when we return IDLE if nothing has arrived |
307 |
|
* tn is when we timeout on non-complete (total timeout) |
308 |
|
* td is max timeout between reads |
309 |
|
*/ |
310 |
|
|
311 |
|
enum htc_status_e |
312 |
320976 |
HTC_RxStuff(struct http_conn *htc, htc_complete_f *func, |
313 |
|
vtim_real *t1, vtim_real *t2, vtim_real ti, vtim_real tn, vtim_dur td, |
314 |
|
int maxbytes) |
315 |
|
{ |
316 |
|
vtim_dur tmo; |
317 |
|
vtim_real now; |
318 |
|
enum htc_status_e hs; |
319 |
|
unsigned l, r; |
320 |
|
ssize_t z; |
321 |
|
|
322 |
320976 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
323 |
320976 |
AN(htc->rfd); |
324 |
320976 |
assert(*htc->rfd > 0); |
325 |
320976 |
AN(htc->rxbuf_b); |
326 |
320976 |
AN(WS_Reservation(htc->ws)); |
327 |
|
|
328 |
320976 |
l = pdiff(htc->rxbuf_b, htc->rxbuf_e); |
329 |
320976 |
r = WS_ReservationSize(htc->ws); |
330 |
320976 |
assert(l <= r); |
331 |
|
|
332 |
320976 |
AZ(isnan(tn) && isnan(td)); |
333 |
320976 |
if (t1 != NULL) |
334 |
195101 |
assert(isnan(*t1)); |
335 |
|
|
336 |
320976 |
if (l == r) { |
337 |
|
/* Can't work with a zero size buffer */ |
338 |
120 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
339 |
120 |
return (HTC_S_OVERFLOW); |
340 |
|
} |
341 |
320856 |
z = r; |
342 |
320856 |
if (z < maxbytes) |
343 |
4440 |
maxbytes = z; /* Cap maxbytes at available WS */ |
344 |
|
|
345 |
575901 |
while (1) { |
346 |
575901 |
now = VTIM_real(); |
347 |
575901 |
AZ(htc->pipeline_b); |
348 |
575901 |
AZ(htc->pipeline_e); |
349 |
575901 |
l = pdiff(htc->rxbuf_b, htc->rxbuf_e); |
350 |
575901 |
assert(l <= r); |
351 |
|
|
352 |
575901 |
hs = func(htc); |
353 |
575901 |
if (hs == HTC_S_OVERFLOW || hs == HTC_S_JUNK) { |
354 |
160 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
355 |
160 |
return (hs); |
356 |
|
} |
357 |
575741 |
if (hs == HTC_S_COMPLETE) { |
358 |
248160 |
WS_ReleaseP(htc->ws, htc->rxbuf_e); |
359 |
|
/* Got it, run with it */ |
360 |
248160 |
if (t1 != NULL && isnan(*t1)) |
361 |
125865 |
*t1 = now; |
362 |
248160 |
if (t2 != NULL) |
363 |
131774 |
*t2 = now; |
364 |
248160 |
return (HTC_S_COMPLETE); |
365 |
|
} |
366 |
327581 |
if (hs == HTC_S_MORE) { |
367 |
|
/* Working on it */ |
368 |
53011 |
if (t1 != NULL && isnan(*t1)) |
369 |
6108 |
*t1 = now; |
370 |
327581 |
} else if (hs == HTC_S_EMPTY) |
371 |
274570 |
htc->rxbuf_e = htc->rxbuf_b; |
372 |
|
else |
373 |
0 |
WRONG("htc_status_e"); |
374 |
|
|
375 |
327581 |
if (hs == HTC_S_EMPTY && !isnan(ti) && (isnan(tn) || ti < tn)) |
376 |
274366 |
tmo = ti - now; |
377 |
53217 |
else if (isnan(tn)) |
378 |
1426 |
tmo = td; |
379 |
51791 |
else if (isnan(td)) |
380 |
51791 |
tmo = tn - now; |
381 |
0 |
else if (td < tn - now) |
382 |
0 |
tmo = td; |
383 |
|
else |
384 |
0 |
tmo = tn - now; |
385 |
|
|
386 |
327583 |
AZ(isnan(tmo)); |
387 |
327583 |
z = maxbytes - (htc->rxbuf_e - htc->rxbuf_b); |
388 |
327583 |
if (z <= 0) { |
389 |
|
/* maxbytes reached but not HTC_S_COMPLETE. Return |
390 |
|
* overflow. */ |
391 |
760 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
392 |
760 |
return (HTC_S_OVERFLOW); |
393 |
|
} |
394 |
326823 |
if (tmo <= 0.0) |
395 |
6081 |
tmo = 1e-3; |
396 |
326823 |
z = VTCP_read(*htc->rfd, htc->rxbuf_e, z, tmo); |
397 |
326823 |
if (z == 0 || z == -1) { |
398 |
60673 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
399 |
60673 |
return (HTC_S_EOF); |
400 |
266150 |
} else if (z > 0) |
401 |
255045 |
htc->rxbuf_e += z; |
402 |
11105 |
else if (z == -2) { |
403 |
11105 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
404 |
11105 |
if (hs == HTC_S_EMPTY) |
405 |
7338 |
return (HTC_S_IDLE); |
406 |
|
else |
407 |
3767 |
return (HTC_S_TIMEOUT); |
408 |
|
} |
409 |
|
} |
410 |
320978 |
} |
411 |
|
|
412 |
|
/*-------------------------------------------------------------------- |
413 |
|
* Get a new session, preferably by recycling an already ready one |
414 |
|
* |
415 |
|
* Layout is: |
416 |
|
* struct sess |
417 |
|
* workspace |
418 |
|
*/ |
419 |
|
|
420 |
|
struct sess * |
421 |
84586 |
SES_New(struct pool *pp) |
422 |
|
{ |
423 |
|
struct sess *sp; |
424 |
|
unsigned sz; |
425 |
|
char *p, *e; |
426 |
|
|
427 |
84586 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
428 |
84586 |
sp = MPL_Get(pp->mpl_sess, &sz); |
429 |
84586 |
AN(sp); |
430 |
84586 |
INIT_OBJ(sp, SESS_MAGIC); |
431 |
84586 |
sp->pool = pp; |
432 |
84586 |
sp->refcnt = 1; |
433 |
84586 |
memset(sp->sattr, 0xff, sizeof sp->sattr); |
434 |
|
|
435 |
84586 |
e = (char*)sp + sz; |
436 |
84586 |
p = (char*)(sp + 1); |
437 |
84586 |
p = (void*)PRNDUP(p); |
438 |
84586 |
assert(p < e); |
439 |
84586 |
WS_Init(sp->ws, "ses", p, e - p); |
440 |
|
|
441 |
84586 |
sp->t_open = NAN; |
442 |
84586 |
sp->t_idle = NAN; |
443 |
84586 |
sp->timeout_idle = NAN; |
444 |
84586 |
sp->timeout_linger = NAN; |
445 |
84586 |
sp->send_timeout = NAN; |
446 |
84586 |
sp->idle_send_timeout = NAN; |
447 |
84586 |
Lck_New(&sp->mtx, lck_sess); |
448 |
84586 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
449 |
84586 |
return (sp); |
450 |
|
} |
451 |
|
|
452 |
|
/*-------------------------------------------------------------------- |
453 |
|
* Handle a session (from waiter) |
454 |
|
*/ |
455 |
|
|
456 |
|
static void v_matchproto_(waiter_handle_f) |
457 |
6882 |
ses_handle(struct waited *wp, enum wait_event ev, vtim_real now) |
458 |
|
{ |
459 |
|
struct sess *sp; |
460 |
|
struct pool *pp; |
461 |
|
struct pool_task *tp; |
462 |
|
const struct transport *xp; |
463 |
|
|
464 |
6882 |
CHECK_OBJ_NOTNULL(wp, WAITED_MAGIC); |
465 |
6882 |
CAST_OBJ_NOTNULL(sp, wp->priv1, SESS_MAGIC); |
466 |
6882 |
CAST_OBJ_NOTNULL(xp, wp->priv2, TRANSPORT_MAGIC); |
467 |
6882 |
assert(WS_Reservation(sp->ws) == wp); |
468 |
6882 |
FINI_OBJ(wp); |
469 |
|
|
470 |
|
/* The WS was reserved in SES_Wait() */ |
471 |
6882 |
WS_Release(sp->ws, 0); |
472 |
|
|
473 |
6882 |
switch (ev) { |
474 |
|
case WAITER_TIMEOUT: |
475 |
480 |
SES_Delete(sp, SC_RX_CLOSE_IDLE, now); |
476 |
480 |
break; |
477 |
|
case WAITER_REMCLOSE: |
478 |
800 |
SES_Delete(sp, SC_REM_CLOSE, now); |
479 |
800 |
break; |
480 |
|
case WAITER_ACTION: |
481 |
5602 |
pp = sp->pool; |
482 |
5602 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
483 |
|
/* SES_Wait() guarantees the next will not assert. */ |
484 |
5602 |
assert(sizeof *tp <= WS_ReserveSize(sp->ws, sizeof *tp)); |
485 |
5602 |
tp = WS_Reservation(sp->ws); |
486 |
5602 |
tp->func = xp->unwait; |
487 |
5602 |
tp->priv = sp; |
488 |
5602 |
if (Pool_Task(pp, tp, TASK_QUEUE_REQ)) |
489 |
0 |
SES_Delete(sp, SC_OVERLOAD, now); |
490 |
5602 |
break; |
491 |
|
case WAITER_CLOSE: |
492 |
0 |
WRONG("Should not see WAITER_CLOSE on client side"); |
493 |
0 |
break; |
494 |
|
default: |
495 |
0 |
WRONG("Wrong event in ses_handle"); |
496 |
0 |
} |
497 |
6882 |
} |
498 |
|
|
499 |
|
/*-------------------------------------------------------------------- |
500 |
|
*/ |
501 |
|
|
502 |
|
void |
503 |
7163 |
SES_Wait(struct sess *sp, const struct transport *xp) |
504 |
|
{ |
505 |
|
struct pool *pp; |
506 |
|
struct waited *wp; |
507 |
|
unsigned u; |
508 |
|
|
509 |
7163 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
510 |
7163 |
CHECK_OBJ_NOTNULL(xp, TRANSPORT_MAGIC); |
511 |
7163 |
pp = sp->pool; |
512 |
7163 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
513 |
7163 |
assert(sp->fd > 0); |
514 |
|
/* |
515 |
|
* XXX: waiter_epoll prevents us from zeroing the struct because |
516 |
|
* XXX: it keeps state across calls. |
517 |
|
*/ |
518 |
7163 |
VTCP_nonblocking(sp->fd); |
519 |
|
|
520 |
|
/* |
521 |
|
* Put struct waited on the workspace. Make sure that the |
522 |
|
* workspace can hold enough space for both struct waited |
523 |
|
* and pool_task, as pool_task will be needed when coming |
524 |
|
* off the waiter again. |
525 |
|
*/ |
526 |
7163 |
u = WS_ReserveAll(sp->ws); |
527 |
7163 |
if (u < sizeof (struct waited) || u < sizeof(struct pool_task)) { |
528 |
6 |
WS_MarkOverflow(sp->ws); |
529 |
6 |
SES_Delete(sp, SC_OVERLOAD, NAN); |
530 |
6 |
return; |
531 |
|
} |
532 |
|
|
533 |
7161 |
wp = WS_Reservation(sp->ws); |
534 |
7161 |
INIT_OBJ(wp, WAITED_MAGIC); |
535 |
7161 |
wp->fd = sp->fd; |
536 |
7161 |
wp->priv1 = sp; |
537 |
7161 |
wp->priv2 = xp; |
538 |
7161 |
wp->idle = sp->t_idle; |
539 |
7161 |
wp->func = ses_handle; |
540 |
7161 |
wp->tmo = SESS_TMO(sp, timeout_idle); |
541 |
7161 |
if (Wait_Enter(pp->waiter, wp)) |
542 |
0 |
SES_Delete(sp, SC_PIPE_OVERFLOW, NAN); |
543 |
7161 |
} |
544 |
|
|
545 |
|
/*-------------------------------------------------------------------- |
546 |
|
* Update sc_ counters by reason |
547 |
|
* |
548 |
|
* assuming that the approximation of non-atomic global counters is sufficient. |
549 |
|
* if not: update to per-wrk |
550 |
|
*/ |
551 |
|
|
552 |
|
static void |
553 |
83775 |
ses_close_acct(stream_close_t reason) |
554 |
|
{ |
555 |
|
|
556 |
83775 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
557 |
83775 |
switch (reason->idx) { |
558 |
|
#define SESS_CLOSE(reason, stat, err, desc) \ |
559 |
|
case SCE_ ## reason: \ |
560 |
|
VSC_C_main->sc_ ## stat++; \ |
561 |
|
break; |
562 |
|
#include "tbl/sess_close.h" |
563 |
|
|
564 |
|
default: |
565 |
0 |
WRONG("Wrong event in ses_close_acct"); |
566 |
|
} |
567 |
83775 |
if (reason->is_err) |
568 |
17432 |
VSC_C_main->sess_closed_err++; |
569 |
83775 |
} |
570 |
|
|
571 |
|
/*-------------------------------------------------------------------- |
572 |
|
* Close a session's connection. |
573 |
|
* XXX: Technically speaking we should catch a t_end timestamp here |
574 |
|
* XXX: for SES_Delete() to use. |
575 |
|
*/ |
576 |
|
|
577 |
|
void |
578 |
83779 |
SES_Close(struct sess *sp, stream_close_t reason) |
579 |
|
{ |
580 |
|
int i; |
581 |
|
|
582 |
83779 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
583 |
83779 |
assert(reason->idx > 0); |
584 |
83779 |
assert(sp->fd > 0); |
585 |
83779 |
i = close(sp->fd); |
586 |
83779 |
assert(i == 0 || errno != EBADF); /* XXX EINVAL seen */ |
587 |
83779 |
sp->fd = -reason->idx; |
588 |
83779 |
ses_close_acct(reason); |
589 |
83779 |
} |
590 |
|
|
591 |
|
/*-------------------------------------------------------------------- |
592 |
|
* Report and dismantle a session. |
593 |
|
*/ |
594 |
|
|
595 |
|
void |
596 |
83776 |
SES_Delete(struct sess *sp, stream_close_t reason, vtim_real now) |
597 |
|
{ |
598 |
|
|
599 |
83776 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
600 |
83776 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
601 |
|
|
602 |
83776 |
if (reason != SC_NULL) |
603 |
63993 |
SES_Close(sp, reason); |
604 |
83776 |
assert(sp->fd < 0); |
605 |
|
|
606 |
83776 |
if (isnan(now)) |
607 |
82495 |
now = VTIM_real(); |
608 |
83776 |
AZ(isnan(sp->t_open)); |
609 |
83776 |
if (now < sp->t_open) { |
610 |
0 |
VSL(SLT_Debug, sp->vxid, |
611 |
|
"Clock step (now=%f < t_open=%f)", |
612 |
0 |
now, sp->t_open); |
613 |
0 |
if (now + cache_param->clock_step < sp->t_open) |
614 |
0 |
WRONG("Clock step detected"); |
615 |
0 |
now = sp->t_open; /* Do not log negatives */ |
616 |
0 |
} |
617 |
|
|
618 |
83776 |
if (reason == SC_NULL) { |
619 |
19786 |
assert(sp->fd < 0 && -sp->fd < SCE_MAX); |
620 |
19786 |
reason = sc_lookup[-sp->fd]; |
621 |
19786 |
} |
622 |
|
|
623 |
83776 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
624 |
83776 |
VSL(SLT_SessClose, sp->vxid, "%s %.3f", reason->name, now - sp->t_open); |
625 |
83776 |
VSL(SLT_End, sp->vxid, "%s", ""); |
626 |
83776 |
if (WS_Overflowed(sp->ws)) |
627 |
160 |
VSC_C_main->ws_session_overflow++; |
628 |
83776 |
SES_Rel(sp); |
629 |
83776 |
} |
630 |
|
|
631 |
|
void |
632 |
56320 |
SES_DeleteHS(struct sess *sp, enum htc_status_e hs, vtim_real now) |
633 |
|
{ |
634 |
|
stream_close_t reason; |
635 |
|
|
636 |
56320 |
switch (hs) { |
637 |
|
case HTC_S_JUNK: |
638 |
40 |
reason = SC_RX_JUNK; |
639 |
40 |
break; |
640 |
|
case HTC_S_CLOSE: |
641 |
0 |
reason = SC_REM_CLOSE; |
642 |
0 |
break; |
643 |
|
case HTC_S_TIMEOUT: |
644 |
40 |
reason = SC_RX_TIMEOUT; |
645 |
40 |
break; |
646 |
|
case HTC_S_OVERFLOW: |
647 |
160 |
reason = SC_RX_OVERFLOW; |
648 |
160 |
break; |
649 |
|
case HTC_S_EOF: |
650 |
56080 |
reason = SC_REM_CLOSE; |
651 |
56080 |
break; |
652 |
|
default: |
653 |
0 |
WRONG("htc_status (bad)"); |
654 |
0 |
} |
655 |
56320 |
SES_Delete(sp, reason, now); |
656 |
56320 |
} |
657 |
|
|
658 |
|
|
659 |
|
/*-------------------------------------------------------------------- |
660 |
|
*/ |
661 |
|
|
662 |
|
void |
663 |
89194 |
SES_Ref(struct sess *sp) |
664 |
|
{ |
665 |
|
|
666 |
89194 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
667 |
89194 |
Lck_Lock(&sp->mtx); |
668 |
89194 |
assert(sp->refcnt > 0); |
669 |
89194 |
sp->refcnt++; |
670 |
89194 |
Lck_Unlock(&sp->mtx); |
671 |
89194 |
} |
672 |
|
|
673 |
|
void |
674 |
172936 |
SES_Rel(struct sess *sp) |
675 |
|
{ |
676 |
|
int i; |
677 |
|
struct pool *pp; |
678 |
|
|
679 |
172936 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
680 |
172936 |
pp = sp->pool; |
681 |
172936 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
682 |
|
|
683 |
172936 |
Lck_Lock(&sp->mtx); |
684 |
172936 |
assert(sp->refcnt > 0); |
685 |
172936 |
i = --sp->refcnt; |
686 |
172936 |
Lck_Unlock(&sp->mtx); |
687 |
172936 |
if (i) |
688 |
89159 |
return; |
689 |
83777 |
Lck_Delete(&sp->mtx); |
690 |
|
#ifdef ENABLE_WORKSPACE_EMULATOR |
691 |
|
WS_Rollback(sp->ws, 0); |
692 |
|
#endif |
693 |
83777 |
MPL_Free(sp->pool->mpl_sess, sp); |
694 |
172936 |
} |
695 |
|
|
696 |
|
/*-------------------------------------------------------------------- |
697 |
|
* Create and delete pools |
698 |
|
*/ |
699 |
|
|
700 |
|
void |
701 |
72551 |
SES_NewPool(struct pool *pp, unsigned pool_no) |
702 |
|
{ |
703 |
|
char nb[4 /* "sess" */ + 10 /* "%u" */ + 1]; |
704 |
|
|
705 |
72551 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
706 |
72551 |
bprintf(nb, "req%u", pool_no); |
707 |
145102 |
pp->mpl_req = MPL_New(nb, &cache_param->pool_req, |
708 |
72551 |
&cache_param->workspace_client); |
709 |
72551 |
bprintf(nb, "sess%u", pool_no); |
710 |
145102 |
pp->mpl_sess = MPL_New(nb, &cache_param->pool_sess, |
711 |
72551 |
&cache_param->workspace_session); |
712 |
|
|
713 |
72551 |
bprintf(nb, "pool%u", pool_no); |
714 |
72551 |
pp->waiter = Waiter_New(nb); |
715 |
72551 |
} |
716 |
|
|
717 |
|
void |
718 |
80 |
SES_DestroyPool(struct pool *pp) |
719 |
|
{ |
720 |
80 |
MPL_Destroy(&pp->mpl_req); |
721 |
80 |
MPL_Destroy(&pp->mpl_sess); |
722 |
80 |
Waiter_Destroy(&pp->waiter); |
723 |
80 |
} |