Code Coverage: vinyl-cache/bin/vinyld/http2/cache_http2.h
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2016 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
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
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 */
30
31
struct h2_sess;
32
struct h2_req;
33
struct h2h_decode;
34
struct h2_frame_s;
35
36
#include "hpack/vhp.h"
37
38
/**********************************************************************/
39
40
struct h2_error_s {
41
        const char                      *name;
42
        const char                      *txt;
43
        uint32_t                        val;
44
        int                             stream;
45
        int                             connection;
46
        int                             send_goaway;
47
        stream_close_t                  reason;
48
};
49
50
typedef const struct h2_error_s *h2_error;
51
52
#define H2_ERROR_MATCH(err, target)                     \
53 3076
        ((err) != NULL && (err)->val == (target)->val)
54
55
#define H2_CUSTOM_ERRORS
56
#define H2EC1(U,v,g,r,d) extern const struct h2_error_s H2CE_##U[1];
57
#define H2EC2(U,v,g,r,d) extern const struct h2_error_s H2SE_##U[1];
58
#define H2EC3(U,v,g,r,d) H2EC1(U,v,g,r,d) H2EC2(U,v,g,r,d)
59
#define H2_ERROR(NAME, val, sc, goaway, reason, desc)   \
60
        H2EC##sc(NAME, val, goaway, reason, desc)
61
#include "tbl/h2_error.h"
62
#undef H2EC1
63
#undef H2EC2
64
#undef H2EC3
65
66
/**********************************************************************/
67
68
typedef h2_error h2_rxframe_f(struct worker *, struct h2_sess *,
69
    struct h2_req *);
70
71
typedef const struct h2_frame_s *h2_frame;
72
73
struct h2_frame_s {
74
        const char      *name;
75
        h2_rxframe_f    *rxfunc;
76
        uint8_t         type;
77
        uint8_t         flags;
78
        h2_error        act_szero;
79
        h2_error        act_snonzero;
80
        h2_error        act_sidle;
81
        int             respect_window;
82
        h2_frame        continuation;
83
        uint8_t         final_flags;
84
        int             overhead;
85
};
86
87
#define H2_FRAME(l,U,...) extern const struct h2_frame_s H2_F_##U[1];
88
#include "tbl/h2_frames.h"
89
90
/**********************************************************************/
91
92
struct h2_settings {
93
#define H2_SETTING(U,l,...) uint32_t l;
94
#include "tbl/h2_settings.h"
95
};
96
97
typedef void h2_setsetting_f(struct h2_settings*, uint32_t);
98
99
struct h2_setting_s {
100
        const char      *name;
101
        h2_setsetting_f *setfunc;
102
        uint16_t        ident;
103
        uint32_t        defval;
104
        uint32_t        minval;
105
        uint32_t        maxval;
106
        h2_error        range_error;
107
};
108
109
#define H2_SETTING(U,...) extern const struct h2_setting_s H2_SET_##U[1];
110
#include "tbl/h2_settings.h"
111
112
/**********************************************************************/
113
114
enum h2_stream_e {
115
        H2_STREAM__DUMMY = -1,
116
#define H2_STREAM(U,s,d) H2_S_##U,
117
#include "tbl/h2_stream.h"
118
};
119
120
#define H2_FRAME_FLAGS(l,u,v)   extern const uint8_t H2FF_##u;
121
#include "tbl/h2_frames.h"
122
123
struct h2_rxbuf {
124
        unsigned                        magic;
125
#define H2_RXBUF_MAGIC                  0x73f9fb27
126
        unsigned                        size;
127
        uint64_t                        tail;
128
        uint64_t                        head;
129
        struct stv_buffer               *stvbuf;
130
        uint8_t                         data[];
131
};
132
133
struct h2_req {
134
        unsigned                        magic;
135
#define H2_REQ_MAGIC                    0x03411584
136
        uint32_t                        stream;
137
        int                             scheduled;
138
        enum h2_stream_e                state;
139
        int                             counted;
140
        struct h2_sess                  *h2sess;
141
        struct req                      *req;
142
        double                          t_send;
143
        double                          t_winupd;
144
        pthread_cond_t                  *cond;
145
        VTAILQ_ENTRY(h2_req)            list;
146
        int64_t                         t_window;
147
        int64_t                         r_window;
148
149
        /* Where to wake this stream up */
150
        struct worker                   *wrk;
151
152
        struct h2_rxbuf                 *rxbuf;
153
154
        VTAILQ_ENTRY(h2_req)            tx_list;
155
        h2_error                        error;
156
};
157
158
VTAILQ_HEAD(h2_req_s, h2_req);
159
160
struct h2_sess {
161
        unsigned                        magic;
162
#define H2_SESS_MAGIC                   0xa16f7e4b
163
164
        pthread_t                       rxthr;
165
        pthread_cond_t                  *cond;
166
        pthread_cond_t                  winupd_cond[1];
167
168
        struct sess                     *sess;
169
        int                             refcnt;
170
        int                             open_streams;
171
        int                             winup_streams;
172
        uint32_t                        highest_stream;
173
        int                             goaway;
174
        int                             bogosity;
175
        int                             do_sweep;
176
177
        struct h2_req                   *req0;
178
179
        struct h2_req_s                 streams;
180
181
        struct req                      *srq;
182
        struct ws                       *ws;
183
        struct http_conn                *htc;
184
        struct vsl_log                  *vsl;
185
        struct h2h_decode               *decode;
186
        struct vht_table                dectbl[1];
187
188
        unsigned                        rxf_len;
189
        unsigned                        rxf_type;
190
        unsigned                        rxf_flags;
191
        unsigned                        rxf_stream;
192
        uint8_t                         *rxf_data;
193
194
        struct h2_settings              remote_settings;
195
        struct h2_settings              local_settings;
196
197
        struct req                      *new_req;
198
        vtim_real                       t1;     // t_first for new_req
199
        uint32_t                        goaway_last_stream;
200
201
        VTAILQ_HEAD(,h2_req)            txqueue;
202
203
        h2_error                        error;
204
205
        // rst rate limit parameters, copied from h2_* parameters
206
        vtim_dur                        rapid_reset;
207
        int64_t                         rapid_reset_limit;
208
        vtim_dur                        rapid_reset_period;
209
210
        // rst rate limit state
211
        double                          rst_budget;
212
        vtim_real                       last_rst;
213
};
214
215 5457
#define ASSERT_RXTHR(h2) do {assert(h2->rxthr == pthread_self());} while(0)
216
217
/* http2/cache_http2_panic.c */
218
#ifdef TRANSPORT_MAGIC
219
vtr_sess_panic_f h2_sess_panic;
220
#endif
221
222
/* http2/cache_http2_deliver.c */
223
#ifdef TRANSPORT_MAGIC
224
vtr_deliver_f h2_deliver;
225
vtr_minimal_response_f h2_minimal_response;
226
#endif /* TRANSPORT_MAGIC */
227
228
/* http2/cache_http2_hpack.c */
229
struct h2h_decode {
230
        unsigned                        magic;
231
#define H2H_DECODE_MAGIC                0xd092bde4
232
233
        unsigned                        has_authority:1;
234
        unsigned                        has_scheme:1;
235
        h2_error                        error;
236
        enum vhd_ret_e                  vhd_ret;
237
        struct ws                       *ws;
238
        char                            *out;
239
        int64_t                         limit;
240
        size_t                          out_l;
241
        size_t                          out_u;
242
        size_t                          namelen;
243
        struct vhd_decode               vhd[1];
244
};
245
246
void h2h_decode_hdr_init(const struct h2_sess *h2);
247
h2_error h2h_decode_hdr_fini(const struct h2_sess *h2);
248
h2_error h2h_decode_bytes(struct h2_sess *h2, const uint8_t *ptr,
249
    size_t len);
250
251
/* cache_http2_send.c */
252 927
#define H2_SEND_HELD(h2, r2) (VTAILQ_FIRST(&(h2)->txqueue) == (r2))
253
void H2_Send_Get(struct worker *, struct h2_sess *, struct h2_req *);
254
void H2_Send_Rel(struct h2_sess *, const struct h2_req *);
255
256
void H2_Send_Frame(struct worker *, struct h2_sess *,
257
    h2_frame type, uint8_t flags, uint32_t len, uint32_t stream,
258
    const void *);
259
260
void H2_Send_RST(struct worker *wrk, struct h2_sess *h2,
261
    const struct h2_req *r2, uint32_t stream, h2_error h2e);
262
void H2_Send_GOAWAY(struct worker *wrk, struct h2_sess *h2,
263
    const struct h2_req *r2, h2_error h2e);
264
265
void H2_Send(struct worker *, struct h2_req *, h2_frame type, uint8_t flags,
266
    uint32_t len, const void *, uint64_t *acct);
267
268
/* cache_http2_proto.c */
269
struct h2_req * h2_new_req(struct h2_sess *, unsigned stream, struct req *);
270
h2_error h2_stream_tmo(struct h2_sess *, const struct h2_req *, vtim_real, vtim_real *);
271
void h2_del_req(struct worker *, struct h2_req *);
272
void h2_kill_req(struct worker *, struct h2_sess *, struct h2_req *, h2_error);
273
int h2_rxframe(struct worker *, struct h2_sess *);
274
h2_error h2_set_setting(struct h2_sess *, const uint8_t *);
275
void h2_req_body(struct req*);
276
int h2_rapid_reset_check(struct worker *wrk, struct h2_sess *h2,
277
    const struct h2_req *r2);
278
h2_error h2_rapid_reset_charge(struct worker *wrk, struct h2_sess *h2,
279
    const struct h2_req *r2);
280
task_func_t h2_do_req;
281
#ifdef TRANSPORT_MAGIC
282
vtr_req_fail_f h2_req_fail;
283
#endif
284
285
/* cache_http2_session.c */
286
void
287
H2S_Lock_VSLb(const struct h2_sess *, enum VSL_tag_e, const char *, ...);