varnish-cache/vmod/vmod_h2.c
0
/*-
1
 * Copyright 2023 UPLEX - Nils Goroll Systemoptimierung
2
 * All rights reserved.
3
 *
4
 * Author: Nils Goroll <nils.goroll@uplex.de>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are met:
10
 * 1. Redistributions of source code must retain the above copyright notice,
11
 *    this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
20
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 *
27
 */
28
29
#include "config.h"
30
31
#include "cache/cache_varnishd.h"
32
33
#include "vcc_h2_if.h"
34
35
#include "cache/cache_transport.h"
36
#include "http2/cache_http2.h"
37
38
static struct h2_sess *
39 640
h2get(VRT_CTX)
40
{
41
        struct h2_sess *h2;
42
        uintptr_t *up;
43
44 640
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
45 640
        CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC); // $Restrict client
46 640
        if (ctx->req->transport != &HTTP2_transport)
47 320
                return (NULL);
48 320
        AZ(SES_Get_proto_priv(ctx->req->sp, &up));
49 320
        CAST_OBJ_NOTNULL(h2, (void *)*up, H2_SESS_MAGIC);
50 320
        return (h2);
51 640
}
52
VCL_BOOL
53 80
vmod_is(VRT_CTX)
54
{
55 80
        struct h2_sess *h2 = h2get(ctx);
56
57 80
        return (h2 != NULL);
58
}
59
60
#define GETSET(type, name, argname) \
61
type                                                    \
62
vmod_ ## name(VRT_CTX, struct VARGS(name) *args)        \
63
{                                                       \
64
        struct h2_sess *h2 = h2get(ctx);                \
65
        type r;                                         \
66
                                                        \
67
        (void)args;                                     \
68
                                                        \
69
        if (h2 == NULL)                                 \
70
                return (-1);                            \
71
                                                        \
72
        if (! args->valid_ ## argname)                  \
73
                return (h2->name);                      \
74
        if (h2->name == args->argname)                  \
75
                return (h2->name);                      \
76
                                                        \
77
        Lck_Lock(&h2->sess->mtx);                       \
78
        r = h2->name;                                   \
79
        if (h2->name != args->argname) {                \
80
                h2->name = args->argname;               \
81
                h2->rst_budget = h2->rapid_reset_limit; \
82
                h2->last_rst = ctx->now;                \
83
        }                                               \
84
        Lck_Unlock(&h2->sess->mtx);                     \
85
        return (r);                                     \
86
}
87
88 160
GETSET(VCL_DURATION, rapid_reset, threshold)
89 160
GETSET(VCL_INT, rapid_reset_limit, number)
90 160
GETSET(VCL_DURATION, rapid_reset_period, duration)
91
92
VCL_REAL
93 80
vmod_rapid_reset_budget(VRT_CTX)
94
{
95 80
        struct h2_sess *h2 = h2get(ctx);
96
97 80
        if (h2 == NULL)
98 40
                return (-1);
99
100 40
        return (h2->rst_budget);
101 80
}