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