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