| | 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 |
92247 |
SES_SetTransport(struct worker *wrk, struct sess *sp, struct req *req, |
97 |
|
const struct transport *xp) |
98 |
|
{ |
99 |
|
|
100 |
92247 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
101 |
92247 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
102 |
92247 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
103 |
92247 |
CHECK_OBJ_NOTNULL(xp, TRANSPORT_MAGIC); |
104 |
92247 |
assert(xp->number > 0); |
105 |
|
|
106 |
92247 |
sp->sattr[SA_TRANSPORT] = xp->number; |
107 |
92247 |
req->transport = xp; |
108 |
92247 |
wrk->task->func = xp->new_session; |
109 |
92247 |
wrk->task->priv = req; |
110 |
92247 |
} |
111 |
|
|
112 |
|
/*--------------------------------------------------------------------*/ |
113 |
|
|
114 |
|
#define SES_NOATTR_OFFSET 0xffff |
115 |
|
|
116 |
|
static int |
117 |
940599 |
ses_get_attr(const struct sess *sp, enum sess_attr a, void **dst) |
118 |
|
{ |
119 |
940599 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
120 |
940599 |
assert(a < SA_LAST); |
121 |
940599 |
AN(dst); |
122 |
|
|
123 |
940599 |
if (sp->sattr[a] == SES_NOATTR_OFFSET) { |
124 |
83456 |
*dst = NULL; |
125 |
83456 |
return (-1); |
126 |
|
} |
127 |
857143 |
*dst = WS_AtOffset(sp->ws, sp->sattr[a], 0); |
128 |
857143 |
return (0); |
129 |
940599 |
} |
130 |
|
|
131 |
|
static int |
132 |
460882 |
ses_set_attr(const struct sess *sp, enum sess_attr a, const void *src, int sz) |
133 |
|
{ |
134 |
|
void *dst; |
135 |
460882 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
136 |
460882 |
assert(a < SA_LAST); |
137 |
460882 |
AN(src); |
138 |
460882 |
assert(sz > 0); |
139 |
|
|
140 |
460882 |
if (sp->sattr[a] == SES_NOATTR_OFFSET) |
141 |
0 |
return (-1); |
142 |
460882 |
dst = WS_AtOffset(sp->ws, sp->sattr[a], sz); |
143 |
460882 |
AN(dst); |
144 |
460882 |
memcpy(dst, src, sz); |
145 |
460882 |
return (0); |
146 |
460882 |
} |
147 |
|
|
148 |
|
static int |
149 |
420497 |
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 |
420497 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
155 |
420497 |
assert(a < SA_LAST); |
156 |
420497 |
AN(dst); |
157 |
420497 |
sz = *szp; |
158 |
420497 |
*szp = 0; |
159 |
420497 |
assert(sz >= 0); |
160 |
420497 |
if (WS_ReserveSize(sp->ws, sz) == 0) |
161 |
40 |
return (0); |
162 |
420457 |
o = WS_ReservationOffset(sp->ws); |
163 |
420457 |
if (o >= SES_NOATTR_OFFSET) { |
164 |
0 |
WS_Release(sp->ws, 0); |
165 |
0 |
return (0); |
166 |
|
} |
167 |
420457 |
*dst = WS_Reservation(sp->ws); |
168 |
420457 |
*szp = sz; |
169 |
420457 |
sp->sattr[a] = (uint16_t)o; |
170 |
420457 |
WS_Release(sp->ws, sz); |
171 |
420457 |
return (1); |
172 |
420497 |
} |
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 |
172806 |
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 |
172806 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
207 |
172806 |
AN(src); |
208 |
|
|
209 |
172806 |
assert(a < SA_LAST); |
210 |
172806 |
if (strcmp(sess_attr[a].type, "char")) |
211 |
0 |
WRONG("wrong sess_attr: not char"); |
212 |
|
|
213 |
172806 |
l = sz = strlen(src) + 1; |
214 |
172806 |
if (! ses_res_attr(sp, a, &q, &sz)) |
215 |
0 |
return (0); |
216 |
172806 |
assert(l == sz); |
217 |
172806 |
strcpy(q, src); |
218 |
172806 |
return (1); |
219 |
172806 |
} |
220 |
|
|
221 |
|
const char * |
222 |
295473 |
SES_Get_String_Attr(const struct sess *sp, enum sess_attr a) |
223 |
|
{ |
224 |
|
void *q; |
225 |
|
|
226 |
295473 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
227 |
|
|
228 |
295473 |
assert(a < SA_LAST); |
229 |
295473 |
if (strcmp(sess_attr[a].type, "char")) |
230 |
0 |
WRONG("wrong sess_attr: not char"); |
231 |
|
|
232 |
295473 |
if (ses_get_attr(sp, a, &q) < 0) |
233 |
0 |
return (NULL); |
234 |
295473 |
return (q); |
235 |
295473 |
} |
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 |
321080 |
HTC_RxInit(struct http_conn *htc, struct ws *ws) |
259 |
|
{ |
260 |
|
unsigned rollback; |
261 |
|
int l; |
262 |
|
|
263 |
321080 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
264 |
321080 |
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 |
321080 |
rollback = !strcasecmp(ws->id, "req") && htc->body_status == NULL; |
271 |
321080 |
l = WS_Pipeline(htc->ws, htc->pipeline_b, htc->pipeline_e, rollback); |
272 |
321080 |
xxxassert(l >= 0); |
273 |
|
|
274 |
321080 |
htc->rxbuf_b = WS_Reservation(ws); |
275 |
321080 |
htc->rxbuf_e = htc->rxbuf_b + l; |
276 |
321080 |
htc->pipeline_b = NULL; |
277 |
321080 |
htc->pipeline_e = NULL; |
278 |
321080 |
} |
279 |
|
|
280 |
|
void |
281 |
246468 |
HTC_RxPipeline(struct http_conn *htc, char *p) |
282 |
|
{ |
283 |
|
|
284 |
246468 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
285 |
246468 |
assert(p >= htc->rxbuf_b); |
286 |
246468 |
assert(p <= htc->rxbuf_e); |
287 |
246468 |
if (p == htc->rxbuf_e) { |
288 |
186397 |
htc->pipeline_b = NULL; |
289 |
186397 |
htc->pipeline_e = NULL; |
290 |
186397 |
} else { |
291 |
60071 |
htc->pipeline_b = p; |
292 |
60071 |
htc->pipeline_e = htc->rxbuf_e; |
293 |
|
} |
294 |
246468 |
} |
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 |
320973 |
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 |
320973 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
323 |
320973 |
AN(htc->rfd); |
324 |
320973 |
assert(*htc->rfd > 0); |
325 |
320973 |
AN(htc->rxbuf_b); |
326 |
320973 |
AN(WS_Reservation(htc->ws)); |
327 |
|
|
328 |
320973 |
l = pdiff(htc->rxbuf_b, htc->rxbuf_e); |
329 |
320973 |
r = WS_ReservationSize(htc->ws); |
330 |
320973 |
assert(l <= r); |
331 |
|
|
332 |
320973 |
AZ(isnan(tn) && isnan(td)); |
333 |
320973 |
if (t1 != NULL) |
334 |
195098 |
assert(isnan(*t1)); |
335 |
|
|
336 |
320973 |
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 |
320853 |
z = r; |
342 |
320853 |
if (z < maxbytes) |
343 |
4440 |
maxbytes = z; /* Cap maxbytes at available WS */ |
344 |
|
|
345 |
576272 |
while (1) { |
346 |
576272 |
now = VTIM_real(); |
347 |
576272 |
AZ(htc->pipeline_b); |
348 |
576272 |
AZ(htc->pipeline_e); |
349 |
576272 |
l = pdiff(htc->rxbuf_b, htc->rxbuf_e); |
350 |
576272 |
assert(l <= r); |
351 |
|
|
352 |
576272 |
hs = func(htc); |
353 |
576272 |
if (hs == HTC_S_OVERFLOW || hs == HTC_S_JUNK) { |
354 |
160 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
355 |
160 |
return (hs); |
356 |
|
} |
357 |
576114 |
if (hs == HTC_S_COMPLETE) { |
358 |
248155 |
WS_ReleaseP(htc->ws, htc->rxbuf_e); |
359 |
|
/* Got it, run with it */ |
360 |
248155 |
if (t1 != NULL && isnan(*t1)) |
361 |
125866 |
*t1 = now; |
362 |
248155 |
if (t2 != NULL) |
363 |
131771 |
*t2 = now; |
364 |
248155 |
return (HTC_S_COMPLETE); |
365 |
|
} |
366 |
327959 |
if (hs == HTC_S_MORE) { |
367 |
|
/* Working on it */ |
368 |
53388 |
if (t1 != NULL && isnan(*t1)) |
369 |
6105 |
*t1 = now; |
370 |
327959 |
} else if (hs == HTC_S_EMPTY) |
371 |
274571 |
htc->rxbuf_e = htc->rxbuf_b; |
372 |
|
else |
373 |
0 |
WRONG("htc_status_e"); |
374 |
|
|
375 |
327959 |
if (hs == HTC_S_EMPTY && !isnan(ti) && (isnan(tn) || ti < tn)) |
376 |
274367 |
tmo = ti - now; |
377 |
53594 |
else if (isnan(tn)) |
378 |
1427 |
tmo = td; |
379 |
52167 |
else if (isnan(td)) |
380 |
52167 |
tmo = tn - now; |
381 |
0 |
else if (td < tn - now) |
382 |
0 |
tmo = td; |
383 |
|
else |
384 |
0 |
tmo = tn - now; |
385 |
|
|
386 |
327961 |
AZ(isnan(tmo)); |
387 |
327961 |
z = maxbytes - (htc->rxbuf_e - htc->rxbuf_b); |
388 |
327961 |
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 |
327201 |
if (tmo <= 0.0) |
395 |
6080 |
tmo = 1e-3; |
396 |
327201 |
z = VTCP_read(*htc->rfd, htc->rxbuf_e, z, tmo); |
397 |
327201 |
if (z == 0 || z == -1) { |
398 |
60674 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
399 |
60674 |
return (HTC_S_EOF); |
400 |
266527 |
} else if (z > 0) |
401 |
255419 |
htc->rxbuf_e += z; |
402 |
11108 |
else if (z == -2) { |
403 |
11108 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
404 |
11108 |
if (hs == HTC_S_EMPTY) |
405 |
7338 |
return (HTC_S_IDLE); |
406 |
|
else |
407 |
3770 |
return (HTC_S_TIMEOUT); |
408 |
|
} |
409 |
|
} |
410 |
320977 |
} |
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 |
84584 |
SES_New(struct pool *pp) |
422 |
|
{ |
423 |
|
struct sess *sp; |
424 |
|
unsigned sz; |
425 |
|
char *p, *e; |
426 |
|
|
427 |
84584 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
428 |
84584 |
sp = MPL_Get(pp->mpl_sess, &sz); |
429 |
84584 |
AN(sp); |
430 |
84584 |
INIT_OBJ(sp, SESS_MAGIC); |
431 |
84584 |
sp->pool = pp; |
432 |
84584 |
sp->refcnt = 1; |
433 |
84584 |
memset(sp->sattr, 0xff, sizeof sp->sattr); |
434 |
|
|
435 |
84584 |
e = (char*)sp + sz; |
436 |
84584 |
p = (char*)(sp + 1); |
437 |
84584 |
p = (void*)PRNDUP(p); |
438 |
84584 |
assert(p < e); |
439 |
84584 |
WS_Init(sp->ws, "ses", p, e - p); |
440 |
|
|
441 |
84584 |
sp->t_open = NAN; |
442 |
84584 |
sp->t_idle = NAN; |
443 |
84584 |
sp->timeout_idle = NAN; |
444 |
84584 |
sp->timeout_linger = NAN; |
445 |
84584 |
sp->send_timeout = NAN; |
446 |
84584 |
sp->idle_send_timeout = NAN; |
447 |
84584 |
Lck_New(&sp->mtx, lck_sess); |
448 |
84584 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
449 |
84584 |
return (sp); |
450 |
|
} |
451 |
|
|
452 |
|
/*-------------------------------------------------------------------- |
453 |
|
* Handle a session (from waiter) |
454 |
|
*/ |
455 |
|
|
456 |
|
static void v_matchproto_(waiter_handle_f) |
457 |
6881 |
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 |
6881 |
CHECK_OBJ_NOTNULL(wp, WAITED_MAGIC); |
465 |
6881 |
CAST_OBJ_NOTNULL(sp, wp->priv1, SESS_MAGIC); |
466 |
6881 |
CAST_OBJ_NOTNULL(xp, wp->priv2, TRANSPORT_MAGIC); |
467 |
6881 |
assert(WS_Reservation(sp->ws) == wp); |
468 |
6881 |
FINI_OBJ(wp); |
469 |
|
|
470 |
|
/* The WS was reserved in SES_Wait() */ |
471 |
6881 |
WS_Release(sp->ws, 0); |
472 |
|
|
473 |
6881 |
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 |
5601 |
pp = sp->pool; |
482 |
5601 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
483 |
|
/* SES_Wait() guarantees the next will not assert. */ |
484 |
5601 |
assert(sizeof *tp <= WS_ReserveSize(sp->ws, sizeof *tp)); |
485 |
5601 |
tp = WS_Reservation(sp->ws); |
486 |
5601 |
tp->func = xp->unwait; |
487 |
5601 |
tp->priv = sp; |
488 |
5601 |
if (Pool_Task(pp, tp, TASK_QUEUE_REQ)) |
489 |
0 |
SES_Delete(sp, SC_OVERLOAD, now); |
490 |
5601 |
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 |
6881 |
} |
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 |
8 |
WS_MarkOverflow(sp->ws); |
529 |
8 |
SES_Delete(sp, SC_OVERLOAD, NAN); |
530 |
8 |
return; |
531 |
|
} |
532 |
|
|
533 |
7159 |
wp = WS_Reservation(sp->ws); |
534 |
7159 |
INIT_OBJ(wp, WAITED_MAGIC); |
535 |
7159 |
wp->fd = sp->fd; |
536 |
7159 |
wp->priv1 = sp; |
537 |
7159 |
wp->priv2 = xp; |
538 |
7159 |
wp->idle = sp->t_idle; |
539 |
7159 |
wp->func = ses_handle; |
540 |
7159 |
wp->tmo = SESS_TMO(sp, timeout_idle); |
541 |
7159 |
if (Wait_Enter(pp->waiter, wp)) |
542 |
0 |
SES_Delete(sp, SC_PIPE_OVERFLOW, NAN); |
543 |
7159 |
} |
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 |
83765 |
ses_close_acct(stream_close_t reason) |
554 |
|
{ |
555 |
|
|
556 |
83765 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
557 |
83765 |
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 |
83765 |
if (reason->is_err) |
568 |
17424 |
VSC_C_main->sess_closed_err++; |
569 |
83765 |
} |
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 |
83769 |
SES_Close(struct sess *sp, stream_close_t reason) |
579 |
|
{ |
580 |
|
int i; |
581 |
|
|
582 |
83769 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
583 |
83769 |
assert(reason->idx > 0); |
584 |
83769 |
assert(sp->fd > 0); |
585 |
83769 |
i = close(sp->fd); |
586 |
83769 |
assert(i == 0 || errno != EBADF); /* XXX EINVAL seen */ |
587 |
83769 |
sp->fd = -reason->idx; |
588 |
83769 |
ses_close_acct(reason); |
589 |
83769 |
} |
590 |
|
|
591 |
|
/*-------------------------------------------------------------------- |
592 |
|
* Report and dismantle a session. |
593 |
|
*/ |
594 |
|
|
595 |
|
void |
596 |
83765 |
SES_Delete(struct sess *sp, stream_close_t reason, vtim_real now) |
597 |
|
{ |
598 |
|
|
599 |
83765 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
600 |
83765 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
601 |
|
|
602 |
83765 |
if (reason != SC_NULL) |
603 |
63985 |
SES_Close(sp, reason); |
604 |
83765 |
assert(sp->fd < 0); |
605 |
|
|
606 |
83765 |
if (isnan(now)) |
607 |
82485 |
now = VTIM_real(); |
608 |
83765 |
AZ(isnan(sp->t_open)); |
609 |
83765 |
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 |
83765 |
if (reason == SC_NULL) { |
619 |
19784 |
assert(sp->fd < 0 && -sp->fd < SCE_MAX); |
620 |
19784 |
reason = sc_lookup[-sp->fd]; |
621 |
19784 |
} |
622 |
|
|
623 |
83765 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
624 |
83765 |
VSL(SLT_SessClose, sp->vxid, "%s %.3f", reason->name, now - sp->t_open); |
625 |
83765 |
VSL(SLT_End, sp->vxid, "%s", ""); |
626 |
83765 |
if (WS_Overflowed(sp->ws)) |
627 |
160 |
VSC_C_main->ws_session_overflow++; |
628 |
83765 |
SES_Rel(sp); |
629 |
83765 |
} |
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 |
172926 |
SES_Rel(struct sess *sp) |
675 |
|
{ |
676 |
|
int i; |
677 |
|
struct pool *pp; |
678 |
|
|
679 |
172926 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
680 |
172926 |
pp = sp->pool; |
681 |
172926 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
682 |
|
|
683 |
172926 |
Lck_Lock(&sp->mtx); |
684 |
172926 |
assert(sp->refcnt > 0); |
685 |
172926 |
i = --sp->refcnt; |
686 |
172926 |
Lck_Unlock(&sp->mtx); |
687 |
172926 |
if (i) |
688 |
89159 |
return; |
689 |
83767 |
Lck_Delete(&sp->mtx); |
690 |
|
#ifdef ENABLE_WORKSPACE_EMULATOR |
691 |
|
WS_Rollback(sp->ws, 0); |
692 |
|
#endif |
693 |
83767 |
MPL_Free(sp->pool->mpl_sess, sp); |
694 |
172926 |
} |
695 |
|
|
696 |
|
/*-------------------------------------------------------------------- |
697 |
|
* Create and delete pools |
698 |
|
*/ |
699 |
|
|
700 |
|
void |
701 |
72552 |
SES_NewPool(struct pool *pp, unsigned pool_no) |
702 |
|
{ |
703 |
|
char nb[4 /* "sess" */ + 10 /* "%u" */ + 1]; |
704 |
|
|
705 |
72552 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
706 |
72552 |
bprintf(nb, "req%u", pool_no); |
707 |
145104 |
pp->mpl_req = MPL_New(nb, &cache_param->pool_req, |
708 |
72552 |
&cache_param->workspace_client); |
709 |
72552 |
bprintf(nb, "sess%u", pool_no); |
710 |
145104 |
pp->mpl_sess = MPL_New(nb, &cache_param->pool_sess, |
711 |
72552 |
&cache_param->workspace_session); |
712 |
|
|
713 |
72552 |
bprintf(nb, "pool%u", pool_no); |
714 |
72552 |
pp->waiter = Waiter_New(nb); |
715 |
72552 |
} |
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 |
} |