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

Commit cfba263

Browse files
daghfdridi
authored andcommitted
Add vmod_h2 to control rapid_reset parameters per session
Backport of 2cfb561 and 1a406e3 Conflicts: bin/varnishtest/vmods.h configure.ac lib/Makefile.am
1 parent bdf0c89 commit cfba263

9 files changed

Lines changed: 327 additions & 2 deletions

File tree

bin/varnishtest/vmods.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ VTC_VMOD(vtc)
3535
VTC_VMOD(blob)
3636
VTC_VMOD(unix)
3737
VTC_VMOD(proxy)
38+
VTC_VMOD(h2)
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(10);
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 == 100
82+
expect resp.http.rapid-reset-limit-n == 10
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 == 10.000
86+
} -run
87+
} -start
88+
89+
client c1 -wait
90+
client c2 -wait

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ AC_CONFIG_FILES([
784784
lib/libvmod_blob/Makefile
785785
lib/libvmod_unix/Makefile
786786
lib/libvmod_proxy/Makefile
787+
lib/libvmod_h2/Makefile
787788
man/Makefile
788789
varnishapi.pc
789790
varnishapi-uninstalled.pc

lib/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ SUBDIRS = \
1212
libvmod_vtc \
1313
libvmod_blob \
1414
libvmod_unix \
15-
libvmod_proxy
15+
libvmod_proxy \
16+
libvmod_h2

lib/libvmod_h2/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
libvmod_h2_la_SOURCES = vmod_h2.c
3+
include $(srcdir)/automake_boilerplate.am
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
# Boilerplate generated by vmodtool.py - changes will be overwritten
3+
4+
AM_LDFLAGS = $(AM_LT_LDFLAGS)
5+
6+
AM_CPPFLAGS = \
7+
-I$(top_srcdir)/include \
8+
-I$(top_srcdir)/bin/varnishd \
9+
-I$(top_builddir)/include
10+
11+
vmoddir = $(pkglibdir)/vmods
12+
vmodtool = $(top_srcdir)/lib/libvcc/vmodtool.py
13+
vmodtoolargs = --strict --boilerplate
14+
15+
vmod_LTLIBRARIES = libvmod_h2.la
16+
17+
libvmod_h2_la_CFLAGS =
18+
19+
libvmod_h2_la_LDFLAGS = \
20+
$(AM_LDFLAGS) \
21+
$(VMOD_LDFLAGS)
22+
23+
nodist_libvmod_h2_la_SOURCES = vcc_if.c vcc_if.h
24+
25+
$(libvmod_h2_la_OBJECTS): vcc_if.h
26+
27+
vcc_if.h vmod_h2.rst vmod_h2.man.rst: vcc_if.c
28+
29+
vcc_if.c: $(vmodtool) $(srcdir)/vmod.vcc
30+
@PYTHON@ $(vmodtool) $(vmodtoolargs) $(srcdir)/vmod.vcc
31+
32+
EXTRA_DIST = vmod.vcc automake_boilerplate.am
33+
34+
CLEANFILES = $(builddir)/vcc_if.c $(builddir)/vcc_if.h \
35+
$(builddir)/vmod_h2.rst \
36+
$(builddir)/vmod_h2.man.rst
37+

lib/libvmod_h2/vmod.vcc

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
41+
Returns true when called on a session handled by the built-in HTTP2 transport.
42+
43+
$Function DURATION rapid_reset([DURATION threshold])
44+
45+
Get and optionally set the ``h2_rapid_reset`` parameter (See
46+
:ref:`varnishd(1)`) for this HTTP2 session only.
47+
48+
Returns -1 when used outside the HTTP2 transport. Otherwise returns
49+
the previous value.
50+
51+
If the call leads to a change in the rate limit parameters, the
52+
current budget as retuned by
53+
`h2.rapid_reset_budget()` is reset.
54+
55+
$Function INT rapid_reset_limit([INT number])
56+
57+
Get and optionally set the ``h2_rapid_reset_limit`` parameter (See
58+
:ref:`varnishd(1)`) for this HTTP2 session only.
59+
60+
Returns -1 when used outside the HTTP2 transport. Otherwise returns
61+
the previous value.
62+
63+
If the call leads to a change in the rate limit parameters, the
64+
current budget as retuned by
65+
`h2.rapid_reset_budget()` is reset.
66+
67+
$Function DURATION rapid_reset_period([DURATION duration])
68+
69+
Get and optionally set the ``h2_rapid_reset_period`` parameter (See
70+
:ref:`varnishd(1)`) for this HTTP2 session only.
71+
72+
Returns -1 when used outside the HTTP2 transport. Otherwise returns
73+
the previous value.
74+
75+
If the call leads to a change in the rate limit parameters, the
76+
current budget as retuned by
77+
`h2.rapid_reset_budget()` is reset.
78+
79+
$Function REAL rapid_reset_budget()
80+
81+
Return how many RST frames classified as "rapid" the client is still
82+
allowed to send before the session is going to be closed.
83+
84+
SEE ALSO
85+
========
86+
87+
* :ref:`varnishd(1)`
88+
* :ref:`vsl(7)`

lib/libvmod_h2/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_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 != &H2_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 vmod_##name##_arg *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+
}

man/Makefile.am

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ dist_man_MANS = \
2121
vmod_vtc.3 \
2222
vmod_blob.3 \
2323
vmod_unix.3 \
24-
vmod_proxy.3
24+
vmod_proxy.3 \
25+
vmod_h2.3
2526

2627
CLEANFILES = $(dist_man_MANS)
2728

@@ -109,4 +110,7 @@ vmod_unix.3: $(top_builddir)/lib/libvmod_unix/vmod_unix.man.rst
109110
vmod_proxy.3: $(top_builddir)/lib/libvmod_proxy/vmod_proxy.man.rst
110111
${RST2MAN} $(RST2ANY_FLAGS) $? $@
111112

113+
vmod_h2.3: $(top_builddir)/lib/libvmod_h2/vmod_h2.man.rst
114+
${RST2MAN} $(RST2ANY_FLAGS) $? $@
115+
112116
.NOPATH: $(dist_man_MANS)

0 commit comments

Comments
 (0)