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 92159
SES_SetTransport(struct worker *wrk, struct sess *sp, struct req *req,
97
    const struct transport *xp)
98
{
99
100 92159
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
101 92159
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
102 92159
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
103 92159
        CHECK_OBJ_NOTNULL(xp, TRANSPORT_MAGIC);
104 92159
        assert(xp->number > 0);
105
106 92159
        sp->sattr[SA_TRANSPORT] = xp->number;
107 92159
        req->transport = xp;
108 92159
        wrk->task->func = xp->new_session;
109 92159
        wrk->task->priv = req;
110 92159
}
111
112
/*--------------------------------------------------------------------*/
113
114
#define SES_NOATTR_OFFSET 0xffff
115
116
static int
117 931260
ses_get_attr(const struct sess *sp, enum sess_attr a, void **dst)
118
{
119 931260
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
120 931260
        assert(a < SA_LAST);
121 931260
        AN(dst);
122
123 931260
        if (sp->sattr[a] == SES_NOATTR_OFFSET) {
124 83373
                *dst = NULL;
125 83373
                return (-1);
126
        }
127 847887
        *dst = WS_AtOffset(sp->ws, sp->sattr[a], 0);
128 847887
        return (0);
129 931260
}
130
131
static int
132 456936
ses_set_attr(const struct sess *sp, enum sess_attr a, const void *src, int sz)
133
{
134
        void *dst;
135 456936
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
136 456936
        assert(a < SA_LAST);
137 456936
        AN(src);
138 456936
        assert(sz > 0);
139
140 456936
        if (sp->sattr[a] == SES_NOATTR_OFFSET)
141 0
                return (-1);
142 456936
        dst = WS_AtOffset(sp->ws, sp->sattr[a], sz);
143 456936
        AN(dst);
144 456936
        memcpy(dst, src, sz);
145 456936
        return (0);
146 456936
}
147
148
static int
149 420081
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 420081
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
155 420081
        assert(a < SA_LAST);
156 420081
        AN(dst);
157 420081
        sz = *szp;
158 420081
        *szp = 0;
159 420081
        assert(sz >= 0);
160 420081
        if (WS_ReserveSize(sp->ws, sz) == 0)
161 40
                return (0);
162 420041
        o = WS_ReservationOffset(sp->ws);
163 420041
        if (o >= SES_NOATTR_OFFSET) {
164 0
                WS_Release(sp->ws, 0);
165 0
                return (0);
166
        }
167 420041
        *dst = WS_Reservation(sp->ws);
168 420041
        *szp = sz;
169 420041
        sp->sattr[a] = (uint16_t)o;
170 420041
        WS_Release(sp->ws, sz);
171 420041
        return (1);
172 420081
}
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 172659
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 172659
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
207 172659
        AN(src);
208
209 172659
        assert(a <  SA_LAST);
210 172659
        if (strcmp(sess_attr[a].type, "char"))
211 0
                WRONG("wrong sess_attr: not char");
212
213 172659
        l = sz = strlen(src) + 1;
214 172659
        if (! ses_res_attr(sp, a, &q, &sz))
215 0
                return (0);
216 172659
        assert(l == sz);
217 172659
        strcpy(q, src);
218 172659
        return (1);
219 172659
}
220
221
const char *
222 292904
SES_Get_String_Attr(const struct sess *sp, enum sess_attr a)
223
{
224
        void *q;
225
226 292904
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
227
228 292904
        assert(a <  SA_LAST);
229 292904
        if (strcmp(sess_attr[a].type, "char"))
230 0
                WRONG("wrong sess_attr: not char");
231
232 292904
        if (ses_get_attr(sp, a, &q) < 0)
233 0
                return (NULL);
234 292904
        return (q);
235 292904
}
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 319681
HTC_RxInit(struct http_conn *htc, struct ws *ws)
259
{
260
        unsigned rollback;
261
        int l;
262
263 319681
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
264 319681
        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 319681
        rollback = !strcasecmp(ws->id, "req") && htc->body_status == NULL;
271 319681
        l = WS_Pipeline(htc->ws, htc->pipeline_b, htc->pipeline_e, rollback);
272 319681
        xxxassert(l >= 0);
273
274 319681
        htc->rxbuf_b = WS_Reservation(ws);
275 319681
        htc->rxbuf_e = htc->rxbuf_b + l;
276 319681
        htc->pipeline_b = NULL;
277 319681
        htc->pipeline_e = NULL;
278 319681
}
279
280
void
281 245143
HTC_RxPipeline(struct http_conn *htc, char *p)
282
{
283
284 245143
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
285 245143
        assert(p >= htc->rxbuf_b);
286 245143
        assert(p <= htc->rxbuf_e);
287 245143
        if (p == htc->rxbuf_e) {
288 185106
                htc->pipeline_b = NULL;
289 185106
                htc->pipeline_e = NULL;
290 185106
        } else {
291 60037
                htc->pipeline_b = p;
292 60037
                htc->pipeline_e = htc->rxbuf_e;
293
        }
294 245143
}
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
 * t0 is when we start
305
 * *t1 becomes time of first non-idle rx
306
 * *t2 becomes time of complete rx
307
 * ti is when we return IDLE if nothing has arrived
308
 * tn is when we timeout on non-complete (total timeout)
309
 * td is max timeout between reads
310
 */
311
312
enum htc_status_e
313 319551
HTC_RxStuff(struct http_conn *htc, htc_complete_f *func,
314
    vtim_real *t1, vtim_real *t2, vtim_real ti, vtim_real tn, vtim_dur td,
315
    int maxbytes)
316
{
317
        vtim_dur tmo;
318
        vtim_real now;
319
        enum htc_status_e hs;
320
        unsigned l, r;
321
        ssize_t z;
322
323 319551
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
324 319551
        AN(htc->rfd);
325 319551
        assert(*htc->rfd > 0);
326 319551
        AN(htc->rxbuf_b);
327 319551
        AN(WS_Reservation(htc->ws));
328
329 319551
        l = pdiff(htc->rxbuf_b, htc->rxbuf_e);
330 319551
        r = WS_ReservationSize(htc->ws);
331 319551
        assert(l <= r);
332
333 319551
        AZ(isnan(tn) && isnan(td));
334 319551
        if (t1 != NULL)
335 193723
                assert(isnan(*t1));
336
337 319551
        if (l == r) {
338
                /* Can't work with a zero size buffer */
339 120
                WS_ReleaseP(htc->ws, htc->rxbuf_b);
340 120
                return (HTC_S_OVERFLOW);
341
        }
342 319431
        z = r;
343 319431
        if (z < maxbytes)
344 4440
                maxbytes = z;   /* Cap maxbytes at available WS */
345
346 573732
        while (1) {
347 573732
                now = VTIM_real();
348 573732
                AZ(htc->pipeline_b);
349 573732
                AZ(htc->pipeline_e);
350 573732
                l = pdiff(htc->rxbuf_b, htc->rxbuf_e);
351 573732
                assert(l <= r);
352
353 573732
                hs = func(htc);
354 573732
                if (hs == HTC_S_OVERFLOW || hs == HTC_S_JUNK) {
355 160
                        WS_ReleaseP(htc->ws, htc->rxbuf_b);
356 160
                        return (hs);
357
                }
358 573600
                if (hs == HTC_S_COMPLETE) {
359 246832
                        WS_ReleaseP(htc->ws, htc->rxbuf_e);
360
                        /* Got it, run with it */
361 246832
                        if (t1 != NULL && isnan(*t1))
362 124589
                                *t1 = now;
363 246832
                        if (t2 != NULL)
364 130493
                                *t2 = now;
365 246832
                        return (HTC_S_COMPLETE);
366
                }
367 326768
                if (hs == HTC_S_MORE) {
368
                        /* Working on it */
369 53590
                        if (t1 != NULL && isnan(*t1))
370 6104
                                *t1 = now;
371 326768
                } else if (hs == HTC_S_EMPTY)
372 273178
                        htc->rxbuf_e = htc->rxbuf_b;
373
                else
374 0
                        WRONG("htc_status_e");
375
376 326768
                if (hs == HTC_S_EMPTY && !isnan(ti) && (isnan(tn) || ti < tn))
377 272983
                        tmo = ti - now;
378 53791
                else if (isnan(tn))
379 1405
                        tmo = td;
380 52386
                else if (isnan(td))
381 52386
                        tmo = tn - now;
382 0
                else if (td < tn - now)
383 0
                        tmo = td;
384
                else
385 0
                        tmo = tn - now;
386
387 326774
                AZ(isnan(tmo));
388 326774
                z = maxbytes - (htc->rxbuf_e - htc->rxbuf_b);
389 326774
                if (z <= 0) {
390
                        /* maxbytes reached but not HTC_S_COMPLETE. Return
391
                         * overflow. */
392 760
                        WS_ReleaseP(htc->ws, htc->rxbuf_b);
393 760
                        return (HTC_S_OVERFLOW);
394
                }
395 326014
                if (tmo <= 0.0)
396 6080
                        tmo = 1e-3;
397 326014
                z = VTCP_read(*htc->rfd, htc->rxbuf_e, z, tmo);
398 326014
                if (z == 0 || z == -1) {
399 60597
                        WS_ReleaseP(htc->ws, htc->rxbuf_b);
400 60597
                        return (HTC_S_EOF);
401 265417
                } else if (z > 0)
402 254301
                        htc->rxbuf_e += z;
403 11116
                else if (z == -2) {
404 11116
                        WS_ReleaseP(htc->ws, htc->rxbuf_b);
405 11116
                        if (hs == HTC_S_EMPTY)
406 7337
                                return (HTC_S_IDLE);
407
                        else
408 3779
                                return (HTC_S_TIMEOUT);
409
                }
410
        }
411 319585
}
412
413
/*--------------------------------------------------------------------
414
 * Get a new session, preferably by recycling an already ready one
415
 *
416
 * Layout is:
417
 *      struct sess
418
 *      workspace
419
 */
420
421
struct sess *
422 84501
SES_New(struct pool *pp)
423
{
424
        struct sess *sp;
425
        unsigned sz;
426
        char *p, *e;
427
428 84501
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
429 84501
        sp = MPL_Get(pp->mpl_sess, &sz);
430 84501
        AN(sp);
431 84501
        INIT_OBJ(sp, SESS_MAGIC);
432 84501
        sp->pool = pp;
433 84501
        sp->refcnt = 1;
434 84501
        memset(sp->sattr, 0xff, sizeof sp->sattr);
435
436 84501
        e = (char*)sp + sz;
437 84501
        p = (char*)(sp + 1);
438 84501
        p = (void*)PRNDUP(p);
439 84501
        assert(p < e);
440 84501
        WS_Init(sp->ws, "ses", p, e - p);
441
442 84501
        sp->t_open = NAN;
443 84501
        sp->t_idle = NAN;
444 84501
        sp->timeout_idle = NAN;
445 84501
        sp->timeout_linger = NAN;
446 84501
        sp->send_timeout = NAN;
447 84501
        sp->idle_send_timeout = NAN;
448 84501
        Lck_New(&sp->mtx, lck_sess);
449 84501
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
450 84501
        return (sp);
451
}
452
453
/*--------------------------------------------------------------------
454
 * Handle a session (from waiter)
455
 */
456
457
static void v_matchproto_(waiter_handle_f)
458 6880
ses_handle(struct waited *wp, enum wait_event ev, vtim_real now)
459
{
460
        struct sess *sp;
461
        struct pool *pp;
462
        struct pool_task *tp;
463
        const struct transport *xp;
464
465 6880
        CHECK_OBJ_NOTNULL(wp, WAITED_MAGIC);
466 6880
        CAST_OBJ_NOTNULL(sp, wp->priv1, SESS_MAGIC);
467 6880
        CAST_OBJ_NOTNULL(xp, wp->priv2, TRANSPORT_MAGIC);
468 6880
        assert(WS_Reservation(sp->ws) == wp);
469 6880
        FINI_OBJ(wp);
470
471
        /* The WS was reserved in SES_Wait() */
472 6880
        WS_Release(sp->ws, 0);
473
474 6880
        switch (ev) {
475
        case WAITER_TIMEOUT:
476 480
                SES_Delete(sp, SC_RX_CLOSE_IDLE, now);
477 480
                break;
478
        case WAITER_REMCLOSE:
479 800
                SES_Delete(sp, SC_REM_CLOSE, now);
480 800
                break;
481
        case WAITER_ACTION:
482 5600
                pp = sp->pool;
483 5600
                CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
484
                /* SES_Wait() guarantees the next will not assert. */
485 5600
                assert(sizeof *tp <= WS_ReserveSize(sp->ws, sizeof *tp));
486 5600
                tp = WS_Reservation(sp->ws);
487 5600
                tp->func = xp->unwait;
488 5600
                tp->priv = sp;
489 5600
                if (Pool_Task(pp, tp, TASK_QUEUE_REQ))
490 0
                        SES_Delete(sp, SC_OVERLOAD, now);
491 5600
                break;
492
        case WAITER_CLOSE:
493 0
                WRONG("Should not see WAITER_CLOSE on client side");
494 0
                break;
495
        default:
496 0
                WRONG("Wrong event in ses_handle");
497 0
        }
498 6880
}
499
500
/*--------------------------------------------------------------------
501
 */
502
503
void
504 7161
SES_Wait(struct sess *sp, const struct transport *xp)
505
{
506
        struct pool *pp;
507
        struct waited *wp;
508
        unsigned u;
509
510 7161
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
511 7161
        CHECK_OBJ_NOTNULL(xp, TRANSPORT_MAGIC);
512 7161
        pp = sp->pool;
513 7161
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
514 7161
        assert(sp->fd > 0);
515
        /*
516
         * XXX: waiter_epoll prevents us from zeroing the struct because
517
         * XXX: it keeps state across calls.
518
         */
519 7161
        VTCP_nonblocking(sp->fd);
520
521
        /*
522
         * Put struct waited on the workspace. Make sure that the
523
         * workspace can hold enough space for both struct waited
524
         * and pool_task, as pool_task will be needed when coming
525
         * off the waiter again.
526
         */
527 7161
        u = WS_ReserveAll(sp->ws);
528 7161
        if (u < sizeof (struct waited) || u < sizeof(struct pool_task)) {
529 0
                WS_MarkOverflow(sp->ws);
530 0
                SES_Delete(sp, SC_OVERLOAD, NAN);
531 0
                return;
532
        }
533
534 7161
        wp = WS_Reservation(sp->ws);
535 7161
        INIT_OBJ(wp, WAITED_MAGIC);
536 7161
        wp->fd = sp->fd;
537 7161
        wp->priv1 = sp;
538 7161
        wp->priv2 = xp;
539 7161
        wp->idle = sp->t_idle;
540 7161
        wp->func = ses_handle;
541 7161
        wp->tmo = SESS_TMO(sp, timeout_idle);
542 7161
        if (Wait_Enter(pp->waiter, wp))
543 0
                SES_Delete(sp, SC_PIPE_OVERFLOW, NAN);
544 7161
}
545
546
/*--------------------------------------------------------------------
547
 * Update sc_ counters by reason
548
 *
549
 * assuming that the approximation of non-atomic global counters is sufficient.
550
 * if not: update to per-wrk
551
 */
552
553
static void
554 83682
ses_close_acct(stream_close_t reason)
555
{
556
557 83682
        CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC);
558 83682
        switch (reason->idx) {
559
#define SESS_CLOSE(reason, stat, err, desc)             \
560
        case SCE_ ## reason:                            \
561
                VSC_C_main->sc_ ## stat++;              \
562
                break;
563
#include "tbl/sess_close.h"
564
565
        default:
566 0
                WRONG("Wrong event in ses_close_acct");
567
        }
568 83682
        if (reason->is_err)
569 17425
                VSC_C_main->sess_closed_err++;
570 83682
}
571
572
/*--------------------------------------------------------------------
573
 * Close a session's connection.
574
 * XXX: Technically speaking we should catch a t_end timestamp here
575
 * XXX: for SES_Delete() to use.
576
 */
577
578
void
579 83680
SES_Close(struct sess *sp, stream_close_t reason)
580
{
581
        int i;
582
583 83680
        CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC);
584 83680
        assert(reason->idx > 0);
585 83680
        assert(sp->fd > 0);
586 83680
        i = close(sp->fd);
587 83680
        assert(i == 0 || errno != EBADF); /* XXX EINVAL seen */
588 83680
        sp->fd = -reason->idx;
589 83680
        ses_close_acct(reason);
590 83680
}
591
592
/*--------------------------------------------------------------------
593
 * Report and dismantle a session.
594
 */
595
596
void
597 83681
SES_Delete(struct sess *sp, stream_close_t reason, vtim_real now)
598
{
599
600 83681
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
601 83681
        CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC);
602
603 83681
        if (reason != SC_NULL)
604 63899
                SES_Close(sp, reason);
605 83681
        assert(sp->fd < 0);
606
607 83681
        if (isnan(now))
608 82402
                now = VTIM_real();
609 83681
        AZ(isnan(sp->t_open));
610 83681
        if (now < sp->t_open) {
611 0
                VSL(SLT_Debug, sp->vxid,
612
                    "Clock step (now=%f < t_open=%f)",
613 0
                    now, sp->t_open);
614 0
                if (now + cache_param->clock_step < sp->t_open)
615 0
                        WRONG("Clock step detected");
616 0
                now = sp->t_open; /* Do not log negatives */
617 0
        }
618
619 83681
        if (reason == SC_NULL) {
620 19781
                assert(sp->fd < 0 && -sp->fd < SCE_MAX);
621 19781
                reason = sc_lookup[-sp->fd];
622 19781
        }
623
624 83681
        CHECK_OBJ_NOTNULL(reason, STREAM_CLOSE_MAGIC);
625 83681
        VSL(SLT_SessClose, sp->vxid, "%s %.3f", reason->name, now - sp->t_open);
626 83681
        VSL(SLT_End, sp->vxid, "%s", "");
627 83681
        if (WS_Overflowed(sp->ws))
628 160
                VSC_C_main->ws_session_overflow++;
629 83681
        SES_Rel(sp);
630 83681
}
631
632
void
633 56239
SES_DeleteHS(struct sess *sp, enum htc_status_e hs, vtim_real now)
634
{
635
        stream_close_t reason;
636
637 56239
        switch (hs) {
638
        case HTC_S_JUNK:
639 40
                reason = SC_RX_JUNK;
640 40
                break;
641
        case HTC_S_CLOSE:
642 0
                reason = SC_REM_CLOSE;
643 0
                break;
644
        case HTC_S_TIMEOUT:
645 40
                reason = SC_RX_TIMEOUT;
646 40
                break;
647
        case HTC_S_OVERFLOW:
648 160
                reason = SC_RX_OVERFLOW;
649 160
                break;
650
        case HTC_S_EOF:
651 55999
                reason = SC_REM_CLOSE;
652 55999
                break;
653
        default:
654 0
                WRONG("htc_status (bad)");
655 0
        }
656 56239
        SES_Delete(sp, reason, now);
657 56239
}
658
659
660
/*--------------------------------------------------------------------
661
 */
662
663
void
664 89153
SES_Ref(struct sess *sp)
665
{
666
667 89153
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
668 89153
        Lck_Lock(&sp->mtx);
669 89153
        assert(sp->refcnt > 0);
670 89153
        sp->refcnt++;
671 89153
        Lck_Unlock(&sp->mtx);
672 89153
}
673
674
void
675 172795
SES_Rel(struct sess *sp)
676
{
677
        int i;
678
        struct pool *pp;
679
680 172795
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
681 172795
        pp = sp->pool;
682 172795
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
683
684 172795
        Lck_Lock(&sp->mtx);
685 172795
        assert(sp->refcnt > 0);
686 172795
        i = --sp->refcnt;
687 172795
        Lck_Unlock(&sp->mtx);
688 172795
        if (i)
689 89116
                return;
690 83679
        Lck_Delete(&sp->mtx);
691
#ifdef ENABLE_WORKSPACE_EMULATOR
692
        WS_Rollback(sp->ws, 0);
693
#endif
694 83679
        MPL_Free(sp->pool->mpl_sess, sp);
695 172795
}
696
697
/*--------------------------------------------------------------------
698
 * Create and delete pools
699
 */
700
701
void
702 72457
SES_NewPool(struct pool *pp, unsigned pool_no)
703
{
704
        char nb[4 /* "sess" */ + 10 /* "%u" */ + 1];
705
706 72457
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
707 72457
        bprintf(nb, "req%u", pool_no);
708 144914
        pp->mpl_req = MPL_New(nb, &cache_param->pool_req,
709 72457
            &cache_param->workspace_client);
710 72457
        bprintf(nb, "sess%u", pool_no);
711 144914
        pp->mpl_sess = MPL_New(nb, &cache_param->pool_sess,
712 72457
            &cache_param->workspace_session);
713
714 72457
        bprintf(nb, "pool%u", pool_no);
715 72457
        pp->waiter = Waiter_New(nb);
716 72457
}
717
718
void
719 80
SES_DestroyPool(struct pool *pp)
720
{
721 80
        MPL_Destroy(&pp->mpl_req);
722 80
        MPL_Destroy(&pp->mpl_sess);
723 80
        Waiter_Destroy(&pp->waiter);
724 80
}