| | 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 |
96950 |
SES_SetTransport(struct worker *wrk, struct sess *sp, struct req *req, |
| 97 |
|
const struct transport *xp) |
| 98 |
|
{ |
| 99 |
|
|
| 100 |
96950 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
| 101 |
96950 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 102 |
96950 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
| 103 |
96950 |
CHECK_OBJ_NOTNULL(xp, TRANSPORT_MAGIC); |
| 104 |
96950 |
assert(xp->number > 0); |
| 105 |
|
|
| 106 |
96950 |
sp->sattr[SA_TRANSPORT] = xp->number; |
| 107 |
96950 |
req->transport = xp; |
| 108 |
96950 |
wrk->task->func = xp->new_session; |
| 109 |
96950 |
wrk->task->priv = req; |
| 110 |
96950 |
} |
| 111 |
|
|
| 112 |
|
/*--------------------------------------------------------------------*/ |
| 113 |
|
|
| 114 |
|
#define SES_NOATTR_OFFSET 0xffff |
| 115 |
|
|
| 116 |
|
static int |
| 117 |
991060 |
ses_get_attr(const struct sess *sp, enum sess_attr a, void **dst) |
| 118 |
|
{ |
| 119 |
991060 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 120 |
991060 |
assert(a < SA_LAST); |
| 121 |
991060 |
AN(dst); |
| 122 |
|
|
| 123 |
991060 |
if (sp->sattr[a] == SES_NOATTR_OFFSET) { |
| 124 |
87469 |
*dst = NULL; |
| 125 |
87469 |
return (-1); |
| 126 |
|
} |
| 127 |
903591 |
*dst = WS_AtOffset(sp->ws, sp->sattr[a], 0); |
| 128 |
903591 |
return (0); |
| 129 |
991060 |
} |
| 130 |
|
|
| 131 |
|
static int |
| 132 |
482200 |
ses_set_attr(const struct sess *sp, enum sess_attr a, const void *src, int sz) |
| 133 |
|
{ |
| 134 |
|
void *dst; |
| 135 |
482200 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 136 |
482200 |
assert(a < SA_LAST); |
| 137 |
482200 |
AN(src); |
| 138 |
482200 |
assert(sz > 0); |
| 139 |
|
|
| 140 |
482200 |
if (sp->sattr[a] == SES_NOATTR_OFFSET) |
| 141 |
0 |
return (-1); |
| 142 |
482200 |
dst = WS_AtOffset(sp->ws, sp->sattr[a], sz); |
| 143 |
482200 |
AN(dst); |
| 144 |
482200 |
memcpy(dst, src, sz); |
| 145 |
482200 |
return (0); |
| 146 |
482200 |
} |
| 147 |
|
|
| 148 |
|
static int |
| 149 |
440292 |
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 |
440292 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 155 |
440292 |
assert(a < SA_LAST); |
| 156 |
440292 |
AN(dst); |
| 157 |
440292 |
sz = *szp; |
| 158 |
440292 |
*szp = 0; |
| 159 |
440292 |
assert(sz >= 0); |
| 160 |
440292 |
if (WS_ReserveSize(sp->ws, sz) == 0) |
| 161 |
40 |
return (0); |
| 162 |
440252 |
o = WS_ReservationOffset(sp->ws); |
| 163 |
440252 |
if (o >= SES_NOATTR_OFFSET) { |
| 164 |
0 |
WS_Release(sp->ws, 0); |
| 165 |
0 |
return (0); |
| 166 |
|
} |
| 167 |
440252 |
*dst = WS_Reservation(sp->ws); |
| 168 |
440252 |
*szp = sz; |
| 169 |
440252 |
sp->sattr[a] = (uint16_t)o; |
| 170 |
440252 |
WS_Release(sp->ws, sz); |
| 171 |
440252 |
return (1); |
| 172 |
440292 |
} |
| 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 |
180774 |
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 |
180774 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 207 |
180774 |
AN(src); |
| 208 |
|
|
| 209 |
180774 |
assert(a < SA_LAST); |
| 210 |
180774 |
if (strcmp(sess_attr[a].type, "char")) |
| 211 |
0 |
WRONG("wrong sess_attr: not char"); |
| 212 |
|
|
| 213 |
180774 |
l = sz = strlen(src) + 1; |
| 214 |
180774 |
if (! ses_res_attr(sp, a, &q, &sz)) |
| 215 |
0 |
return (0); |
| 216 |
180774 |
assert(l == sz); |
| 217 |
180774 |
strcpy(q, src); |
| 218 |
180774 |
return (1); |
| 219 |
180774 |
} |
| 220 |
|
|
| 221 |
|
const char * |
| 222 |
309372 |
SES_Get_String_Attr(const struct sess *sp, enum sess_attr a) |
| 223 |
|
{ |
| 224 |
|
void *q; |
| 225 |
|
|
| 226 |
309372 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 227 |
|
|
| 228 |
309372 |
assert(a < SA_LAST); |
| 229 |
309372 |
if (strcmp(sess_attr[a].type, "char")) |
| 230 |
0 |
WRONG("wrong sess_attr: not char"); |
| 231 |
|
|
| 232 |
309372 |
if (ses_get_attr(sp, a, &q) < 0) |
| 233 |
0 |
return (NULL); |
| 234 |
309372 |
return (q); |
| 235 |
309372 |
} |
| 236 |
|
|
| 237 |
|
/*--------------------------------------------------------------------*/ |
| 238 |
|
|
| 239 |
|
void |
| 240 |
5118 |
HTC_Status(enum htc_status_e e, const char **name, const char **desc) |
| 241 |
|
{ |
| 242 |
|
|
| 243 |
5118 |
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 |
5118 |
} |
| 254 |
|
|
| 255 |
|
/*--------------------------------------------------------------------*/ |
| 256 |
|
|
| 257 |
|
void |
| 258 |
336085 |
HTC_RxInit(struct http_conn *htc, struct ws *ws) |
| 259 |
|
{ |
| 260 |
|
unsigned rollback; |
| 261 |
|
int l; |
| 262 |
|
|
| 263 |
336085 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
| 264 |
336085 |
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 |
336085 |
rollback = !strcasecmp(ws->id, "req") && htc->body_status == NULL; |
| 271 |
336085 |
l = WS_Pipeline(htc->ws, htc->pipeline_b, htc->pipeline_e, rollback); |
| 272 |
336085 |
xxxassert(l >= 0); |
| 273 |
|
|
| 274 |
336085 |
htc->rxbuf_b = WS_Reservation(ws); |
| 275 |
336085 |
htc->rxbuf_e = htc->rxbuf_b + l; |
| 276 |
336085 |
htc->pipeline_b = NULL; |
| 277 |
336085 |
htc->pipeline_e = NULL; |
| 278 |
336085 |
} |
| 279 |
|
|
| 280 |
|
void |
| 281 |
257982 |
HTC_RxPipeline(struct http_conn *htc, char *p) |
| 282 |
|
{ |
| 283 |
|
|
| 284 |
257982 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
| 285 |
257982 |
assert(p >= htc->rxbuf_b); |
| 286 |
257982 |
assert(p <= htc->rxbuf_e); |
| 287 |
257982 |
if (p == htc->rxbuf_e) { |
| 288 |
196492 |
htc->pipeline_b = NULL; |
| 289 |
196492 |
htc->pipeline_e = NULL; |
| 290 |
196492 |
} else { |
| 291 |
61490 |
htc->pipeline_b = p; |
| 292 |
61490 |
htc->pipeline_e = htc->rxbuf_e; |
| 293 |
|
} |
| 294 |
257982 |
} |
| 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 |
335977 |
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 |
335977 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
| 323 |
335977 |
AN(htc->rfd); |
| 324 |
335977 |
assert(*htc->rfd > 0); |
| 325 |
335977 |
AN(htc->rxbuf_b); |
| 326 |
335977 |
AN(WS_Reservation(htc->ws)); |
| 327 |
|
|
| 328 |
335977 |
l = pdiff(htc->rxbuf_b, htc->rxbuf_e); |
| 329 |
335977 |
r = WS_ReservationSize(htc->ws); |
| 330 |
335977 |
assert(l <= r); |
| 331 |
|
|
| 332 |
335977 |
AZ(isnan(tn) && isnan(td)); |
| 333 |
335977 |
if (t1 != NULL) |
| 334 |
248051 |
assert(isnan(*t1)); |
| 335 |
|
|
| 336 |
335977 |
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 |
335857 |
z = r; |
| 342 |
335857 |
if (z < maxbytes) |
| 343 |
4440 |
maxbytes = z; /* Cap maxbytes at available WS */ |
| 344 |
|
|
| 345 |
603602 |
while (1) { |
| 346 |
603602 |
now = VTIM_real(); |
| 347 |
603602 |
AZ(htc->pipeline_b); |
| 348 |
603602 |
AZ(htc->pipeline_e); |
| 349 |
603602 |
l = pdiff(htc->rxbuf_b, htc->rxbuf_e); |
| 350 |
603602 |
assert(l <= r); |
| 351 |
|
|
| 352 |
603602 |
hs = func(htc); |
| 353 |
603602 |
if (hs == HTC_S_OVERFLOW || hs == HTC_S_JUNK) { |
| 354 |
200 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
| 355 |
200 |
return (hs); |
| 356 |
|
} |
| 357 |
603402 |
if (hs == HTC_S_COMPLETE) { |
| 358 |
259654 |
WS_ReleaseP(htc->ws, htc->rxbuf_e); |
| 359 |
|
/* Got it, run with it */ |
| 360 |
259654 |
if (t1 != NULL && isnan(*t1)) |
| 361 |
142167 |
*t1 = now; |
| 362 |
259654 |
if (t2 != NULL) |
| 363 |
138040 |
*t2 = now; |
| 364 |
259654 |
return (HTC_S_COMPLETE); |
| 365 |
|
} |
| 366 |
343748 |
if (hs == HTC_S_MORE) { |
| 367 |
|
/* Working on it */ |
| 368 |
57243 |
if (t1 != NULL && isnan(*t1)) |
| 369 |
39784 |
*t1 = now; |
| 370 |
343748 |
} else if (hs == HTC_S_EMPTY) |
| 371 |
286505 |
htc->rxbuf_e = htc->rxbuf_b; |
| 372 |
|
else |
| 373 |
0 |
WRONG("htc_status_e"); |
| 374 |
|
|
| 375 |
343748 |
if (hs == HTC_S_EMPTY && !isnan(ti) && (isnan(tn) || ti < tn)) |
| 376 |
286271 |
tmo = ti - now; |
| 377 |
57487 |
else if (isnan(tn)) |
| 378 |
1449 |
tmo = td; |
| 379 |
56038 |
else if (isnan(td)) |
| 380 |
56038 |
tmo = tn - now; |
| 381 |
0 |
else if (td < tn - now) |
| 382 |
0 |
tmo = td; |
| 383 |
|
else |
| 384 |
0 |
tmo = tn - now; |
| 385 |
|
|
| 386 |
343758 |
AZ(isnan(tmo)); |
| 387 |
343758 |
z = maxbytes - (htc->rxbuf_e - htc->rxbuf_b); |
| 388 |
343758 |
if (z <= 0) { |
| 389 |
|
/* maxbytes reached but not HTC_S_COMPLETE. Return |
| 390 |
|
* overflow. */ |
| 391 |
800 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
| 392 |
800 |
return (HTC_S_OVERFLOW); |
| 393 |
|
} |
| 394 |
342958 |
if (tmo <= 0.0) |
| 395 |
6094 |
tmo = 1e-3; |
| 396 |
342958 |
z = VTCP_read(*htc->rfd, htc->rxbuf_e, z, tmo); |
| 397 |
342958 |
if (z == 0 || z == -1) { |
| 398 |
63956 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
| 399 |
63956 |
return (HTC_S_EOF); |
| 400 |
279002 |
} else if (z > 0) |
| 401 |
267745 |
htc->rxbuf_e += z; |
| 402 |
11257 |
else if (z == -2) { |
| 403 |
11257 |
WS_ReleaseP(htc->ws, htc->rxbuf_b); |
| 404 |
11257 |
if (hs == HTC_S_EMPTY) |
| 405 |
7510 |
return (HTC_S_IDLE); |
| 406 |
|
else |
| 407 |
3747 |
return (HTC_S_TIMEOUT); |
| 408 |
|
} |
| 409 |
|
} |
| 410 |
335987 |
} |
| 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 |
88494 |
SES_New(struct pool *pp) |
| 422 |
|
{ |
| 423 |
|
struct sess *sp; |
| 424 |
|
unsigned sz; |
| 425 |
|
char *p, *e; |
| 426 |
|
|
| 427 |
88494 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
| 428 |
88494 |
sp = MPL_Get(pp->mpl_sess, &sz); |
| 429 |
88494 |
AN(sp); |
| 430 |
88494 |
INIT_OBJ(sp, SESS_MAGIC); |
| 431 |
88494 |
sp->pool = pp; |
| 432 |
88494 |
sp->refcnt = 1; |
| 433 |
88494 |
memset(sp->sattr, 0xff, sizeof sp->sattr); |
| 434 |
|
|
| 435 |
88494 |
e = (char*)sp + sz; |
| 436 |
88494 |
p = (char*)(sp + 1); |
| 437 |
88494 |
p = (void*)PRNDUP(p); |
| 438 |
88494 |
assert(p < e); |
| 439 |
88494 |
WS_Init(sp->ws, "ses", p, e - p); |
| 440 |
|
|
| 441 |
88494 |
sp->t_open = NAN; |
| 442 |
88494 |
sp->t_idle = NAN; |
| 443 |
88494 |
sp->timeout_idle = NAN; |
| 444 |
88494 |
sp->timeout_linger = NAN; |
| 445 |
88494 |
sp->send_timeout = NAN; |
| 446 |
88494 |
sp->idle_send_timeout = NAN; |
| 447 |
88494 |
Lck_New(&sp->mtx, lck_sess); |
| 448 |
88494 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 449 |
88494 |
return (sp); |
| 450 |
|
} |
| 451 |
|
|
| 452 |
|
/*-------------------------------------------------------------------- |
| 453 |
|
* Handle a session (from waiter) |
| 454 |
|
*/ |
| 455 |
|
|
| 456 |
|
static void v_matchproto_(waiter_handle_f) |
| 457 |
7057 |
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 |
7057 |
CHECK_OBJ_NOTNULL(wp, WAITED_MAGIC); |
| 465 |
7057 |
CAST_OBJ_NOTNULL(sp, wp->priv1, SESS_MAGIC); |
| 466 |
7057 |
CAST_OBJ_NOTNULL(xp, wp->priv2, TRANSPORT_MAGIC); |
| 467 |
7057 |
assert(WS_Reservation(sp->ws) == wp); |
| 468 |
7057 |
FINI_OBJ(wp); |
| 469 |
|
|
| 470 |
|
/* The WS was reserved in SES_Wait() */ |
| 471 |
7057 |
WS_Release(sp->ws, 0); |
| 472 |
|
|
| 473 |
7057 |
switch (ev) { |
| 474 |
|
case WAITER_TIMEOUT: |
| 475 |
520 |
SES_Delete(sp, SC_RX_CLOSE_IDLE, now); |
| 476 |
520 |
break; |
| 477 |
|
case WAITER_REMCLOSE: |
| 478 |
922 |
SES_Delete(sp, SC_REM_CLOSE, now); |
| 479 |
922 |
break; |
| 480 |
|
case WAITER_ACTION: |
| 481 |
5615 |
pp = sp->pool; |
| 482 |
5615 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
| 483 |
|
/* SES_Wait() guarantees the next will not assert. */ |
| 484 |
5615 |
assert(sizeof *tp <= WS_ReserveSize(sp->ws, sizeof *tp)); |
| 485 |
5615 |
tp = WS_Reservation(sp->ws); |
| 486 |
5615 |
tp->func = xp->unwait; |
| 487 |
5615 |
tp->priv = sp; |
| 488 |
5615 |
if (Pool_Task(pp, tp, TASK_QUEUE_REQ)) |
| 489 |
0 |
SES_Delete(sp, SC_OVERLOAD, now); |
| 490 |
5615 |
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 |
7057 |
} |
| 498 |
|
|
| 499 |
|
/*-------------------------------------------------------------------- |
| 500 |
|
*/ |
| 501 |
|
|
| 502 |
|
void |
| 503 |
7335 |
SES_Wait(struct sess *sp, const struct transport *xp) |
| 504 |
|
{ |
| 505 |
|
struct pool *pp; |
| 506 |
|
struct waited *wp; |
| 507 |
|
unsigned u; |
| 508 |
|
|
| 509 |
7335 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 510 |
7335 |
CHECK_OBJ_NOTNULL(xp, TRANSPORT_MAGIC); |
| 511 |
7335 |
pp = sp->pool; |
| 512 |
7335 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
| 513 |
7335 |
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 |
7335 |
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 |
7335 |
u = WS_ReserveAll(sp->ws); |
| 527 |
7335 |
if (u < sizeof (struct waited) || u < sizeof(struct pool_task)) { |
| 528 |
2 |
WS_MarkOverflow(sp->ws); |
| 529 |
2 |
SES_Delete(sp, SC_OVERLOAD, NAN); |
| 530 |
2 |
return; |
| 531 |
|
} |
| 532 |
|
|
| 533 |
7335 |
wp = WS_Reservation(sp->ws); |
| 534 |
7335 |
INIT_OBJ(wp, WAITED_MAGIC); |
| 535 |
7335 |
wp->fd = sp->fd; |
| 536 |
7335 |
wp->priv1 = sp; |
| 537 |
7335 |
wp->priv2 = xp; |
| 538 |
7335 |
wp->idle = sp->t_idle; |
| 539 |
7335 |
wp->func = ses_handle; |
| 540 |
7335 |
wp->tmo = SESS_TMO(sp, timeout_idle); |
| 541 |
7335 |
if (Wait_Enter(pp->waiter, wp)) |
| 542 |
0 |
SES_Delete(sp, SC_PIPE_OVERFLOW, NAN); |
| 543 |
7335 |
} |
| 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 |
87921 |
ses_close_acct(stream_close_t reason) |
| 554 |
|
{ |
| 555 |
|
|
| 556 |
87921 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
| 557 |
87921 |
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 |
87921 |
if (reason->is_err) |
| 568 |
14150 |
VSC_C_main->sess_closed_err++; |
| 569 |
87921 |
} |
| 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 |
87926 |
SES_Close(struct sess *sp, stream_close_t reason) |
| 579 |
|
{ |
| 580 |
|
int i; |
| 581 |
|
|
| 582 |
87926 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
| 583 |
87926 |
assert(reason->idx > 0); |
| 584 |
87926 |
assert(sp->fd > 0); |
| 585 |
87926 |
i = close(sp->fd); |
| 586 |
87926 |
assert(i == 0 || errno != EBADF); /* XXX EINVAL seen */ |
| 587 |
87926 |
sp->fd = -reason->idx; |
| 588 |
87926 |
ses_close_acct(reason); |
| 589 |
87926 |
} |
| 590 |
|
|
| 591 |
|
/*-------------------------------------------------------------------- |
| 592 |
|
* Report and dismantle a session. |
| 593 |
|
*/ |
| 594 |
|
|
| 595 |
|
void |
| 596 |
87923 |
SES_Delete(struct sess *sp, stream_close_t reason, vtim_real now) |
| 597 |
|
{ |
| 598 |
|
|
| 599 |
87923 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 600 |
87923 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
| 601 |
|
|
| 602 |
87923 |
if (reason != SC_NULL) |
| 603 |
67754 |
SES_Close(sp, reason); |
| 604 |
87923 |
assert(sp->fd < 0); |
| 605 |
|
|
| 606 |
87923 |
if (isnan(now)) |
| 607 |
86482 |
now = VTIM_real(); |
| 608 |
87923 |
AZ(isnan(sp->t_open)); |
| 609 |
87923 |
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 |
87923 |
if (reason == SC_NULL) { |
| 619 |
20172 |
assert(sp->fd < 0 && -sp->fd < SCE_MAX); |
| 620 |
20172 |
reason = sc_lookup[-sp->fd]; |
| 621 |
20172 |
} |
| 622 |
|
|
| 623 |
87923 |
CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC); |
| 624 |
87923 |
VSL(SLT_SessClose, sp->vxid, "%s %.3f", reason->name, now - sp->t_open); |
| 625 |
87923 |
VSL(SLT_End, sp->vxid, "%s", ""); |
| 626 |
87923 |
if (WS_Overflowed(sp->ws)) |
| 627 |
160 |
VSC_C_main->ws_session_overflow++; |
| 628 |
87923 |
SES_Rel(sp); |
| 629 |
87923 |
} |
| 630 |
|
|
| 631 |
|
void |
| 632 |
59159 |
SES_DeleteHS(struct sess *sp, enum htc_status_e hs, vtim_real now) |
| 633 |
|
{ |
| 634 |
|
stream_close_t reason; |
| 635 |
|
|
| 636 |
59159 |
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 |
240 |
reason = SC_RX_OVERFLOW; |
| 648 |
240 |
break; |
| 649 |
|
case HTC_S_EOF: |
| 650 |
58839 |
reason = SC_REM_CLOSE; |
| 651 |
58839 |
break; |
| 652 |
|
default: |
| 653 |
0 |
WRONG("htc_status (bad)"); |
| 654 |
0 |
} |
| 655 |
59159 |
SES_Delete(sp, reason, now); |
| 656 |
59159 |
} |
| 657 |
|
|
| 658 |
|
|
| 659 |
|
/*-------------------------------------------------------------------- |
| 660 |
|
*/ |
| 661 |
|
|
| 662 |
|
void |
| 663 |
92429 |
SES_Ref(struct sess *sp) |
| 664 |
|
{ |
| 665 |
|
|
| 666 |
92429 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 667 |
92429 |
Lck_Lock(&sp->mtx); |
| 668 |
92429 |
assert(sp->refcnt > 0); |
| 669 |
92429 |
sp->refcnt++; |
| 670 |
92429 |
Lck_Unlock(&sp->mtx); |
| 671 |
92429 |
} |
| 672 |
|
|
| 673 |
|
void |
| 674 |
180323 |
SES_Rel(struct sess *sp) |
| 675 |
|
{ |
| 676 |
|
int i; |
| 677 |
|
struct pool *pp; |
| 678 |
|
|
| 679 |
180323 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
| 680 |
180323 |
pp = sp->pool; |
| 681 |
180323 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
| 682 |
|
|
| 683 |
180323 |
Lck_Lock(&sp->mtx); |
| 684 |
180323 |
assert(sp->refcnt > 0); |
| 685 |
180323 |
i = --sp->refcnt; |
| 686 |
180323 |
Lck_Unlock(&sp->mtx); |
| 687 |
180323 |
if (i) |
| 688 |
92396 |
return; |
| 689 |
87927 |
Lck_Delete(&sp->mtx); |
| 690 |
|
#ifdef ENABLE_WORKSPACE_EMULATOR |
| 691 |
|
WS_Rollback(sp->ws, 0); |
| 692 |
|
#endif |
| 693 |
87927 |
MPL_Free(sp->pool->mpl_sess, sp); |
| 694 |
180323 |
} |
| 695 |
|
|
| 696 |
|
/*-------------------------------------------------------------------- |
| 697 |
|
* Create and delete pools |
| 698 |
|
*/ |
| 699 |
|
|
| 700 |
|
void |
| 701 |
75187 |
SES_NewPool(struct pool *pp, unsigned pool_no) |
| 702 |
|
{ |
| 703 |
|
char nb[4 /* "sess" */ + 10 /* "%u" */ + 1]; |
| 704 |
|
|
| 705 |
75187 |
CHECK_OBJ_NOTNULL(pp, POOL_MAGIC); |
| 706 |
75187 |
bprintf(nb, "req%u", pool_no); |
| 707 |
150374 |
pp->mpl_req = MPL_New(nb, &cache_param->pool_req, |
| 708 |
75187 |
&cache_param->workspace_client); |
| 709 |
75187 |
bprintf(nb, "sess%u", pool_no); |
| 710 |
150374 |
pp->mpl_sess = MPL_New(nb, &cache_param->pool_sess, |
| 711 |
75187 |
&cache_param->workspace_session); |
| 712 |
|
|
| 713 |
75187 |
bprintf(nb, "pool%u", pool_no); |
| 714 |
75187 |
pp->waiter = Waiter_New(nb); |
| 715 |
75187 |
} |
| 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 |
} |