Skip to content
This repository was archived by the owner on Feb 16, 2026. It is now read-only.

Commit ffb421f

Browse files
committed
Add vmod_h2 to control rapid_reset parameters per session
1 parent 870ef22 commit ffb421f

6 files changed

Lines changed: 329 additions & 1 deletion

File tree

include/tbl/params.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,9 @@ PARAM_SIMPLE(
12581258
)
12591259

12601260
#define H2_RR_INFO \
1261-
"Changes to this parameter affect the default for new HTTP2 sessions"
1261+
"Changes to this parameter affect the default for new HTTP2 " \
1262+
"sessions. vmod_h2(3) can be used to adjust it from VCL."
1263+
12621264
PARAM_SIMPLE(
12631265
/* name */ h2_rapid_reset,
12641266
/* typ */ timeout,

vmod/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ vmod_vcc_files =
2828
vmod_debug_vcc =
2929

3030
include $(srcdir)/automake_boilerplate_blob.am
31+
include $(srcdir)/automake_boilerplate_h2.am
3132
include $(srcdir)/automake_boilerplate_cookie.am
3233
include $(srcdir)/automake_boilerplate_debug.am
3334
include $(srcdir)/automake_boilerplate_directors.am

vmod/automake_boilerplate_h2.am

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Generated by vmodtool.py --boilerplate.
2+
3+
vmod_h2_vcc ?= $(srcdir)/vmod_h2.vcc
4+
5+
vmod_vcc_files += $(vmod_h2_vcc)
6+
7+
vmod_LTLIBRARIES += libvmod_h2.la
8+
9+
libvmod_h2_la_SOURCES = \
10+
vmod_h2.c
11+
12+
libvmod_h2_la_CFLAGS =
13+
14+
vmodtoolargs_h2 ?= --strict --boilerplate -o vcc_h2_if
15+
vmod_h2_symbols_regex ?= Vmod_h2_Data
16+
17+
libvmod_h2_la_LDFLAGS = \
18+
-export-symbols-regex $(vmod_h2_symbols_regex) \
19+
$(AM_LDFLAGS) \
20+
$(VMOD_LDFLAGS)
21+
22+
nodist_libvmod_h2_la_SOURCES = vcc_h2_if.c vcc_h2_if.h
23+
24+
EXTRA_libvmod_h2_la_DEPENDENCIES = $(nodist_libvmod_h2_la_SOURCES)
25+
26+
EXTRA_DIST += automake_boilerplate_h2.am
27+
28+
$(libvmod_h2_la_OBJECTS): vcc_h2_if.h
29+
30+
vcc_h2_if.h vmod_h2.rst vmod_h2.man.rst: vcc_h2_if.c
31+
32+
# A doc-change will not update mtime on the .h and .c files, so a
33+
# touch(1) is necessary to signal that vmodtool was in fact run.
34+
vcc_h2_if.c: $(VMODTOOL) $(srcdir)/vmod_h2.vcc
35+
@PYTHON@ $(VMODTOOL) $(vmodtoolargs_h2) $(srcdir)/vmod_h2.vcc
36+
touch vcc_h2_if.c
37+
38+
clean-local: clean-vmod-h2
39+
40+
clean-vmod-h2:
41+
rm -f $(nodist_libvmod_h2_la_SOURCES)
42+
rm -f vmod_h2.rst vmod_h2.man.rst

vmod/tests/h2_b00000.vtc

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
varnishtest "VMOD h2 basics"
2+
3+
varnish v1 -arg "-p feature=+http2" -vcl {
4+
import h2;
5+
6+
backend proforma none;
7+
8+
sub vcl_recv {
9+
return(synth(200));
10+
}
11+
12+
sub vcl_synth {
13+
set resp.http.http2-is = h2.is();
14+
set resp.body = "";
15+
return (deliver);
16+
}
17+
} -start
18+
19+
client c1 {
20+
txreq
21+
rxresp
22+
expect resp.status == 200
23+
expect resp.http.http2-is == false
24+
} -start
25+
26+
client c2 {
27+
stream 7 {
28+
txreq
29+
rxresp
30+
expect resp.status == 200
31+
expect resp.http.http2-is == true
32+
} -run
33+
} -start
34+
35+
client c1 -wait
36+
client c2 -wait
37+
38+
# coverage
39+
varnish v1 -vcl {
40+
import h2;
41+
42+
backend proforma none;
43+
44+
sub vcl_recv {
45+
return(synth(200));
46+
}
47+
48+
sub vcl_synth {
49+
set resp.http.rapid-reset-o = h2.rapid_reset(10ms);
50+
set resp.http.rapid-reset-n = h2.rapid_reset();
51+
set resp.http.rapid-reset-limit-o = h2.rapid_reset_limit(100);
52+
set resp.http.rapid-reset-limit-n = h2.rapid_reset_limit();
53+
set resp.http.rapid-reset-period-o = h2.rapid_reset_period(10s);
54+
set resp.http.rapid-reset-period-n = h2.rapid_reset_period();
55+
set resp.http.rapid-reset-budget = h2.rapid_reset_budget();
56+
set resp.body = "";
57+
return (deliver);
58+
}
59+
}
60+
61+
client c1 {
62+
txreq
63+
rxresp
64+
expect resp.status == 200
65+
expect resp.http.rapid-reset-o == -1.000
66+
expect resp.http.rapid-reset-n == -1.000
67+
expect resp.http.rapid-reset-limit-o == -1
68+
expect resp.http.rapid-reset-limit-n == -1
69+
expect resp.http.rapid-reset-period-o == -1.000
70+
expect resp.http.rapid-reset-period-n == -1.000
71+
expect resp.http.rapid-reset-budget == -1.000
72+
} -start
73+
74+
client c2 {
75+
stream 7 {
76+
txreq
77+
rxresp
78+
expect resp.status == 200
79+
expect resp.http.rapid-reset-o == 1.000
80+
expect resp.http.rapid-reset-n == 0.010
81+
expect resp.http.rapid-reset-limit-o == 0
82+
expect resp.http.rapid-reset-limit-n == 100
83+
expect resp.http.rapid-reset-period-o == 60.000
84+
expect resp.http.rapid-reset-period-n == 10.000
85+
expect resp.http.rapid-reset-budget == 100.000
86+
} -run
87+
} -start
88+
89+
client c1 -wait
90+
client c2 -wait

vmod/vmod_h2.c

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

vmod/vmod_h2.vcc

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#-
2+
# Copyright 2023 UPLEX - Nils Goroll Systemoptimierung
3+
# All rights reserved.
4+
#
5+
# Author: Nils Goroll <nils.goroll@uplex.de>
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+
$ABI strict
31+
$Module h2 3 "Module to control the built-in HTTP2 transport"
32+
33+
DESCRIPTION
34+
===========
35+
36+
This VMOD contains functions to control the HTTP2 transport built into
37+
Varnish-Cache.
38+
39+
$Function BOOL is()
40+
$Restrict client
41+
42+
Returns true when called on a session handled by the built-in HTTP2 transport.
43+
44+
$Function DURATION rapid_reset([DURATION threshold])
45+
$Restrict client
46+
47+
Get and optionally set the ``h2_rapid_reset`` parameter (See
48+
:ref:`varnishd(1)`) for this HTTP2 session only.
49+
50+
Returns -1 when used outside the HTTP2 transport. Otherwise returns
51+
the previous value.
52+
53+
If the call leads to a change in the rate limit parameters, the
54+
current budget as retuned by
55+
`h2.rapid_reset_budget()`_ is reset.
56+
57+
$Function INT rapid_reset_limit([INT number])
58+
$Restrict client
59+
60+
Get and optionally set the ``h2_rapid_reset_limit`` parameter (See
61+
:ref:`varnishd(1)`) for this HTTP2 session only.
62+
63+
Returns -1 when used outside the HTTP2 transport. Otherwise returns
64+
the previous value.
65+
66+
If the call leads to a change in the rate limit parameters, the
67+
current budget as retuned by
68+
`h2.rapid_reset_budget()`_ is reset.
69+
70+
$Function DURATION rapid_reset_period([DURATION duration])
71+
$Restrict client
72+
73+
Get and optionally set the ``h2_rapid_reset_period`` parameter (See
74+
:ref:`varnishd(1)`) for this HTTP2 session only.
75+
76+
Returns -1 when used outside the HTTP2 transport. Otherwise returns
77+
the previous value.
78+
79+
If the call leads to a change in the rate limit parameters, the
80+
current budget as retuned by
81+
`h2.rapid_reset_budget()`_ is reset.
82+
83+
$Function REAL rapid_reset_budget()
84+
$Restrict client
85+
86+
Return how many RST frames classified as "rapid" the client is still
87+
allowed to send before the session is going to be closed.
88+
89+
SEE ALSO
90+
========
91+
92+
* :ref:`varnishd(1)`
93+
* :ref:`vsl(7)`

0 commit comments

Comments
 (0)