blob: d75c7d04c58403fafa34d7a1fee1b1f2b4dcfe44 [file] [log] [blame]
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001/*
2 * HTTP/3 protocol processing
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation, version 2.1
7 * exclusively.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <haproxy/buf.h>
Amaury Denoyelle99043552021-08-24 15:36:02 +020020#include <haproxy/connection.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010021#include <haproxy/dynbuf.h>
22#include <haproxy/h3.h>
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +020023#include <haproxy/h3_stats.h>
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +020024#include <haproxy/http.h>
25#include <haproxy/htx.h>
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +020026#include <haproxy/intops.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010027#include <haproxy/istbuf.h>
Amaury Denoyelle846cc042022-04-04 16:13:44 +020028#include <haproxy/mux_quic.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010029#include <haproxy/pool.h>
30#include <haproxy/qpack-dec.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020031#include <haproxy/qpack-enc.h>
32#include <haproxy/quic_enc.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020033#include <haproxy/stconn.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010034#include <haproxy/tools.h>
Amaury Denoyelle016aa932022-05-30 15:49:36 +020035#include <haproxy/trace.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010036#include <haproxy/xprt_quic.h>
37
Amaury Denoyelle016aa932022-05-30 15:49:36 +020038/* trace source and events */
39static void h3_trace(enum trace_level level, uint64_t mask,
40 const struct trace_source *src,
41 const struct ist where, const struct ist func,
42 const void *a1, const void *a2, const void *a3, const void *a4);
43
44static const struct trace_event h3_trace_events[] = {
Amaury Denoyelle494512d2022-05-30 15:50:34 +020045#define H3_EV_RX_FRAME (1ULL << 0)
46 { .mask = H3_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H3 frame" },
47#define H3_EV_RX_DATA (1ULL << 1)
48 { .mask = H3_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H3 DATA frame" },
49#define H3_EV_RX_HDR (1ULL << 2)
50 { .mask = H3_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H3 HEADERS frame" },
51#define H3_EV_RX_SETTINGS (1ULL << 3)
52 { .mask = H3_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H3 SETTINGS frame" },
Amaury Denoyellea717eb72022-05-30 15:51:01 +020053#define H3_EV_TX_DATA (1ULL << 4)
54 { .mask = H3_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H3 DATA frame" },
55#define H3_EV_TX_HDR (1ULL << 5)
56 { .mask = H3_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H3 HEADERS frame" },
57#define H3_EV_TX_SETTINGS (1ULL << 6)
58 { .mask = H3_EV_TX_SETTINGS, .name = "tx_settings", .desc = "transmission of H3 SETTINGS frame" },
Amaury Denoyelled5581d52022-05-30 15:51:31 +020059#define H3_EV_H3S_NEW (1ULL << 7)
60 { .mask = H3_EV_H3S_NEW, .name = "h3s_new", .desc = "new H3 stream" },
61#define H3_EV_H3S_END (1ULL << 8)
62 { .mask = H3_EV_H3S_END, .name = "h3s_end", .desc = "H3 stream terminated" },
Amaury Denoyelle016aa932022-05-30 15:49:36 +020063 { }
64};
65
66static const struct name_desc h3_trace_lockon_args[4] = {
67 /* arg1 */ { /* already used by the connection */ },
68 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
69 /* arg3 */ { },
70 /* arg4 */ { }
71};
72
73static const struct name_desc h3_trace_decoding[] = {
74#define H3_VERB_CLEAN 1
75 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
76#define H3_VERB_MINIMAL 2
77 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
78 { /* end */ }
79};
80
81struct trace_source trace_h3 = {
82 .name = IST("h3"),
83 .desc = "HTTP/3 transcoder",
84 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
85 .default_cb = h3_trace,
86 .known_events = h3_trace_events,
87 .lockon_args = h3_trace_lockon_args,
88 .decoding = h3_trace_decoding,
89 .report_events = ~0, /* report everything by default */
90};
91
92#define TRACE_SOURCE &trace_h3
93INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
94
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010095#if defined(DEBUG_H3)
96#define h3_debug_printf fprintf
97#define h3_debug_hexdump debug_hexdump
98#else
99#define h3_debug_printf(...) do { } while (0)
100#define h3_debug_hexdump(...) do { } while (0)
101#endif
102
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200103#define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */
104#define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */
105#define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */
106#define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */
107#define H3_CF_UNI_QPACK_ENC_SET 0x00000010 /* Remote QPACK encoder stream opened */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100108
109/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +0200110static uint64_t h3_settings_qpack_max_table_capacity = 0;
111static uint64_t h3_settings_qpack_blocked_streams = 4096;
112static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100113
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200114struct h3c {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100115 struct qcc *qcc;
Amaury Denoyelled7010392022-07-13 15:17:29 +0200116 struct qcs *ctrl_strm; /* Control stream */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100117 enum h3_err err;
118 uint32_t flags;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200119
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100120 /* Settings */
121 uint64_t qpack_max_table_capacity;
122 uint64_t qpack_blocked_streams;
123 uint64_t max_field_section_size;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200124
Amaury Denoyelle114c9c82022-03-28 14:53:45 +0200125 uint64_t id_goaway; /* stream ID used for a GOAWAY frame */
126
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100127 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200128 /* Stats counters */
129 struct h3_counters *prx_counters;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100130};
131
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200132DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100133
Amaury Denoyelle35550642022-05-24 15:14:53 +0200134#define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200135#define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */
Amaury Denoyelle35550642022-05-24 15:14:53 +0200136
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200137struct h3s {
Amaury Denoyellec0156792022-06-03 15:29:07 +0200138 struct h3c *h3c;
139
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200140 enum h3s_t type;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200141 int demux_frame_len;
142 int demux_frame_type;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200143
144 int flags;
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200145};
146
147DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s));
148
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200149/* Initialize an uni-stream <qcs> by reading its type from <b>.
Amaury Denoyelle35550642022-05-24 15:14:53 +0200150 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200151 * Returns the count of consumed bytes or a negative error code.
Amaury Denoyelle35550642022-05-24 15:14:53 +0200152 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200153static ssize_t h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs,
154 struct buffer *b)
Amaury Denoyelle35550642022-05-24 15:14:53 +0200155{
156 /* decode unidirectional stream type */
157 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200158 uint64_t type;
159 size_t len = 0, ret;
160
Amaury Denoyelled5581d52022-05-30 15:51:31 +0200161 TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
162
Amaury Denoyelle35550642022-05-24 15:14:53 +0200163 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
164 h3s->flags & H3_SF_UNI_INIT);
165
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200166 ret = b_quic_dec_int(&type, b, &len);
Amaury Denoyelle35550642022-05-24 15:14:53 +0200167 if (!ret) {
168 ABORT_NOW();
169 }
170
171 switch (type) {
172 case H3_UNI_S_T_CTRL:
173 if (h3c->flags & H3_CF_UNI_CTRL_SET) {
Amaury Denoyelled666d742022-07-13 15:15:58 +0200174 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200175 return -1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200176 }
177 h3c->flags |= H3_CF_UNI_CTRL_SET;
178 h3s->type = H3S_T_CTRL;
179 break;
180
181 case H3_UNI_S_T_PUSH:
182 /* TODO not supported for the moment */
183 h3s->type = H3S_T_PUSH;
184 break;
185
186 case H3_UNI_S_T_QPACK_DEC:
187 if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) {
Amaury Denoyelled666d742022-07-13 15:15:58 +0200188 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200189 return -1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200190 }
191 h3c->flags |= H3_CF_UNI_QPACK_DEC_SET;
192 h3s->type = H3S_T_QPACK_DEC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200193 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200194 break;
195
196 case H3_UNI_S_T_QPACK_ENC:
197 if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) {
Amaury Denoyelled666d742022-07-13 15:15:58 +0200198 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200199 return -1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200200 }
201 h3c->flags |= H3_CF_UNI_QPACK_ENC_SET;
202 h3s->type = H3S_T_QPACK_ENC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200203 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200204 break;
205
206 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200207 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
208 *
209 * Implementations MUST [...] abort reading on unidirectional
210 * streams that have unknown or unsupported types.
211 */
212 qcs->flags |= QC_SF_READ_ABORTED;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200213 return -1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200214 };
215
216 h3s->flags |= H3_SF_UNI_INIT;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200217
Amaury Denoyelled5581d52022-05-30 15:51:31 +0200218 TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200219 return len;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200220}
221
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200222/* Parse an uni-stream <qcs> from <rxbuf> which does not contains H3 frames.
223 * This may be used for QPACK encoder/decoder streams for example.
224 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200225 * Returns the number of consumed bytes or a negative error code.
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200226 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200227static ssize_t h3_parse_uni_stream_no_h3(struct qcs *qcs, struct buffer *b)
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200228{
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200229 struct h3s *h3s = qcs->ctx;
230
231 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
232 !(h3s->flags & H3_SF_UNI_NO_H3));
233
234 switch (h3s->type) {
235 case H3S_T_QPACK_DEC:
Amaury Denoyelle53eef462022-06-14 16:34:32 +0200236 if (qpack_decode_dec(b, NULL))
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200237 return -1;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200238 break;
239 case H3S_T_QPACK_ENC:
Amaury Denoyelle53eef462022-06-14 16:34:32 +0200240 if (qpack_decode_enc(b, NULL))
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200241 return -1;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200242 break;
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200243 case H3S_T_UNKNOWN:
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200244 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200245 /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */
246 ABORT_NOW();
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200247 }
248
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200249 /* TODO adjust return code */
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200250 return 0;
251}
252
Amaury Denoyelle88d5dd12022-05-31 11:44:52 +0200253/* Decode a H3 frame header from <rxbuf> buffer. The frame type is stored in
254 * <ftype> and length in <flen>.
255 *
256 * Returns the size of the H3 frame header. Note that the input buffer is not
257 * consumed.
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100258 */
259static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200260 struct buffer *b)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100261{
262 size_t hlen;
263
264 hlen = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200265 if (!b_quic_dec_int(ftype, b, &hlen) ||
266 !b_quic_dec_int(flen, b, &hlen)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100267 return 0;
Amaury Denoyelle88d5dd12022-05-31 11:44:52 +0200268 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100269
270 return hlen;
271}
272
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200273/* Check if H3 frame of type <ftype> is valid when received on stream <qcs>.
274 *
275 * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should
276 * be reported.
277 */
278static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype)
279{
280 struct h3s *h3s = qcs->ctx;
281 const uint64_t id = qcs->id;
282
283 BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN);
284
285 switch (ftype) {
286 case H3_FT_DATA:
287 case H3_FT_HEADERS:
288 return h3s->type != H3S_T_CTRL;
289
290 case H3_FT_CANCEL_PUSH:
291 case H3_FT_GOAWAY:
292 case H3_FT_MAX_PUSH_ID:
293 /* Only allowed for control stream. First frame of control
294 * stream MUST be SETTINGS.
295 */
296 return h3s->type == H3S_T_CTRL &&
297 (h3c->flags & H3_CF_SETTINGS_RECV);
298
299 case H3_FT_SETTINGS:
300 /* draft-ietf-quic-http34 7.2.4. SETTINGS
301 *
302 * If an endpoint receives a second SETTINGS frame on the control
303 * stream, the endpoint MUST respond with a connection error of type
304 * H3_FRAME_UNEXPECTED.
305 */
306 return h3s->type == H3S_T_CTRL &&
307 !(h3c->flags & H3_CF_SETTINGS_RECV);
308
309 case H3_FT_PUSH_PROMISE:
310 return h3s->type != H3S_T_CTRL &&
311 (id & QCS_ID_SRV_INTIATOR_BIT);
312
313 default:
314 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
315 *
316 * Implementations MUST discard frames [...] that have unknown
317 * or unsupported types.
318 */
319 return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV);
320 }
321}
322
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100323/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
Willy Tarreau4596fe22022-05-17 19:07:51 +0200324 * in a local HTX buffer and transfer to the stream connector layer. <fin> must be
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100325 * set if this is the last data to transfer from this stream.
326 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200327 * Returns the number of consumed bytes or a negative error code.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100328 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200329static ssize_t h3_headers_to_htx(struct qcs *qcs, const struct buffer *buf,
330 uint64_t len, char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100331{
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200332 struct h3s *h3s = qcs->ctx;
333 struct h3c *h3c = h3s->h3c;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100334 struct buffer htx_buf = BUF_NULL;
335 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100336 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200337 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200338 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200339 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100340 struct ist meth = IST_NULL, path = IST_NULL;
341 //struct ist scheme = IST_NULL, authority = IST_NULL;
342 struct ist authority = IST_NULL;
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200343 int hdr_idx, ret;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100344
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200345 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
346
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200347 /* TODO support buffer wrapping */
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200348 BUG_ON(b_head(buf) + len >= b_wrap(buf));
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200349 ret = qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp,
350 list, sizeof(list) / sizeof(list[0]));
351 if (ret < 0) {
352 TRACE_ERROR("QPACK decoding error", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
353 h3c->err = -ret;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200354 return -1;
Amaury Denoyelle60ef19f2022-06-14 17:38:36 +0200355 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100356
357 qc_get_buf(qcs, &htx_buf);
358 BUG_ON(!b_size(&htx_buf));
359 htx = htx_from_buf(&htx_buf);
360
361 /* first treat pseudo-header to build the start line */
362 hdr_idx = 0;
363 while (1) {
364 if (isteq(list[hdr_idx].n, ist("")))
365 break;
366
367 if (istmatch(list[hdr_idx].n, ist(":"))) {
368 /* pseudo-header */
369 if (isteq(list[hdr_idx].n, ist(":method")))
370 meth = list[hdr_idx].v;
371 else if (isteq(list[hdr_idx].n, ist(":path")))
372 path = list[hdr_idx].v;
373 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
374 // scheme = list[hdr_idx].v;
375 else if (isteq(list[hdr_idx].n, ist(":authority")))
376 authority = list[hdr_idx].v;
377 }
378
379 ++hdr_idx;
380 }
381
382 flags |= HTX_SL_F_VER_11;
Amaury Denoyelle0fa14a62022-04-26 16:24:39 +0200383 flags |= HTX_SL_F_XFER_LEN;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100384
385 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200386 if (!sl) {
387 h3c->err = H3_INTERNAL_ERROR;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200388 return -1;
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200389 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100390
391 if (fin)
392 sl->flags |= HTX_SL_F_BODYLESS;
393
394 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100395
396 if (isttest(authority))
397 htx_add_header(htx, ist("host"), authority);
398
399 /* now treat standard headers */
400 hdr_idx = 0;
401 while (1) {
402 if (isteq(list[hdr_idx].n, ist("")))
403 break;
404
405 if (!istmatch(list[hdr_idx].n, ist(":")))
406 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
407
408 ++hdr_idx;
409 }
410
411 htx_add_endof(htx, HTX_BLK_EOH);
412 htx_to_buf(htx, &htx_buf);
413
414 if (fin)
415 htx->flags |= HTX_FL_EOM;
416
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200417 if (!qc_attach_sc(qcs, &htx_buf)) {
418 h3c->err = H3_INTERNAL_ERROR;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200419 return -1;
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200420 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100421
Amaury Denoyelle114c9c82022-03-28 14:53:45 +0200422 /* RFC 9114 5.2. Connection Shutdown
423 *
424 * The GOAWAY frame contains an identifier that
425 * indicates to the receiver the range of requests or pushes that were
426 * or might be processed in this connection. The server sends a client-
427 * initiated bidirectional stream ID; the client sends a push ID.
428 * Requests or pushes with the indicated identifier or greater are
429 * rejected (Section 4.1.1) by the sender of the GOAWAY. This
430 * identifier MAY be zero if no requests or pushes were processed.
431 */
432 if (qcs->id >= h3c->id_goaway)
433 h3c->id_goaway = qcs->id + 4;
434
Willy Tarreau4596fe22022-05-17 19:07:51 +0200435 /* buffer is transferred to the stream connector and set to NULL
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100436 * except on stream creation error.
437 */
438 b_free(&htx_buf);
439 offer_buffers(NULL, 1);
440
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200441 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200442 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100443}
444
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100445/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
446 * HTX buffer. <fin> must be set if this is the last data to transfer from this
447 * stream.
448 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200449 * Returns the number of consumed bytes or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100450 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200451static ssize_t h3_data_to_htx(struct qcs *qcs, const struct buffer *buf,
452 uint64_t len, char fin)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100453{
454 struct buffer *appbuf;
455 struct htx *htx = NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200456 size_t htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100457 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200458 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100459
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200460 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs);
461
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100462 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
463 BUG_ON(!appbuf);
464 htx = htx_from_buf(appbuf);
465
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200466 if (len > b_data(buf)) {
467 len = b_data(buf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200468 fin = 0;
469 }
470
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200471 head = b_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200472 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100473 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200474 if (!htx_space) {
475 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200476 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200477 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200478
479 if (len > htx_space) {
480 len = htx_space;
481 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100482 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100483
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200484 if (head + len > b_wrap(buf)) {
485 size_t contig = b_wrap(buf) - head;
486 htx_sent = htx_add_data(htx, ist2(b_head(buf), contig));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200487 if (htx_sent < contig) {
488 qcs->flags |= QC_SF_DEM_FULL;
489 goto out;
490 }
491
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200492 len -= contig;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200493 head = b_orig(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200494 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100495 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100496
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200497 htx_sent += htx_add_data(htx, ist2(head, len));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200498 if (htx_sent < len) {
499 qcs->flags |= QC_SF_DEM_FULL;
500 goto out;
501 }
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200502
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200503 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100504 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100505
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200506 out:
507 htx_to_buf(htx, appbuf);
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200508
509 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200510 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100511}
512
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200513/* Parse a SETTINGS frame of length <len> of payload <buf>.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200514 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200515 * Returns the number of consumed bytes or a negative error code.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200516 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200517static ssize_t h3_parse_settings_frm(struct h3c *h3c, const struct buffer *buf,
518 size_t len)
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200519{
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200520 struct buffer b;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200521 uint64_t id, value;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200522 size_t ret = 0;
523 long mask = 0; /* used to detect duplicated settings identifier */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200524
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200525 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS, h3c->qcc->conn);
526
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200527 /* Work on a copy of <buf>. */
Amaury Denoyelle3a2fcfd2022-06-09 11:54:38 +0200528 b = b_make(b_orig(buf), b_size(buf), b_head_ofs(buf), len);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200529
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200530 while (b_data(&b)) {
531 if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) {
532 h3c->err = H3_FRAME_ERROR;
533 return -1;
534 }
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200535
536 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
537 __func__, (unsigned long long)id, (unsigned long long)value);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200538
539 /* draft-ietf-quic-http34 7.2.4. SETTINGS
540 *
541 * The same setting identifier MUST NOT occur more than once in the
542 * SETTINGS frame. A receiver MAY treat the presence of duplicate
543 * setting identifiers as a connection error of type H3_SETTINGS_ERROR.
544 */
545
546 /* Ignore duplicate check for ID too big used for GREASE. */
547 if (id < sizeof(mask)) {
548 if (ha_bit_test(id, &mask)) {
549 h3c->err = H3_SETTINGS_ERROR;
550 return -1;
551 }
552 ha_bit_set(id, &mask);
553 }
554
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200555 switch (id) {
556 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
557 h3c->qpack_max_table_capacity = value;
558 break;
559 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
560 h3c->max_field_section_size = value;
561 break;
562 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
563 h3c->qpack_blocked_streams = value;
564 break;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200565
566 case H3_SETTINGS_RESERVED_0:
567 case H3_SETTINGS_RESERVED_2:
568 case H3_SETTINGS_RESERVED_3:
569 case H3_SETTINGS_RESERVED_4:
570 case H3_SETTINGS_RESERVED_5:
571 /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters
572 *
573 * Setting identifiers which were defined in [HTTP2] where there is no
574 * corresponding HTTP/3 setting have also been reserved
575 * (Section 11.2.2). These reserved settings MUST NOT be sent, and
576 * their receipt MUST be treated as a connection error of type
577 * H3_SETTINGS_ERROR.
578 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200579 h3c->err = H3_SETTINGS_ERROR;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200580 return -1;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200581 default:
582 /* MUST be ignored */
583 break;
584 }
585 }
586
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200587 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200588 return ret;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200589}
590
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100591/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
592 * that we received the last data of the stream.
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200593 *
594 * Returns 0 on success else non-zero.
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100595 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200596static ssize_t h3_decode_qcs(struct qcs *qcs, struct buffer *b, int fin)
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100597{
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200598 struct h3s *h3s = qcs->ctx;
Amaury Denoyellec0156792022-06-03 15:29:07 +0200599 struct h3c *h3c = h3s->h3c;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200600 ssize_t total = 0, ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100601
Amaury Denoyellebb970422022-04-12 16:40:52 +0200602 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200603 if (!b_data(b))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100604 return 0;
605
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200606 if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) {
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200607 if ((ret = h3_init_uni_stream(h3c, qcs, b)) < 0)
608 return -1;
609
610 total += ret;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200611 }
612
613 if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) {
614 /* For non-h3 STREAM, parse it and return immediately. */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200615 if ((ret = h3_parse_uni_stream_no_h3(qcs, b)) < 0)
616 return -1;
617
618 total += ret;
619 return total;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200620 }
621
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200622 while (b_data(b) && !(qcs->flags & QC_SF_DEM_FULL)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100623 uint64_t ftype, flen;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100624 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100625
626 /* Work on a copy of <rxbuf> */
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200627 if (!h3s->demux_frame_len) {
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200628 size_t hlen = h3_decode_frm_header(&ftype, &flen, b);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200629 if (!hlen)
630 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100631
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200632 h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n",
633 __func__, ftype, flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100634
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200635 h3s->demux_frame_type = ftype;
636 h3s->demux_frame_len = flen;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200637 total += hlen;
Amaury Denoyelle417c7c02022-05-31 14:18:33 +0200638
639 if (!h3_is_frame_valid(h3c, qcs, ftype)) {
Amaury Denoyelled666d742022-07-13 15:15:58 +0200640 qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED, 1);
Amaury Denoyelledca4c532022-06-07 18:24:34 +0200641 return -1;
Amaury Denoyelle417c7c02022-05-31 14:18:33 +0200642 }
643
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200644 if (!b_data(b))
Amaury Denoyelle417c7c02022-05-31 14:18:33 +0200645 break;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200646 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100647
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200648 flen = h3s->demux_frame_len;
649 ftype = h3s->demux_frame_type;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200650
651 /* Do not demux incomplete frames except H3 DATA which can be
652 * fragmented in multiple HTX blocks.
653 */
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200654 if (flen > b_data(b) && ftype != H3_FT_DATA) {
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200655 /* Reject frames bigger than bufsize.
656 *
657 * TODO HEADERS should in complement be limited with H3
658 * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent
659 * excessive decompressed size.
660 */
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200661 if (flen > QC_S_RX_BUF_SZ) {
Amaury Denoyelled666d742022-07-13 15:15:58 +0200662 qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD, 1);
Amaury Denoyelledca4c532022-06-07 18:24:34 +0200663 return -1;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200664 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200665 break;
Amaury Denoyelleb5454d42022-05-12 16:56:16 +0200666 }
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200667
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200668 last_stream_frame = (fin && flen == b_data(b));
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100669
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200670 h3_inc_frame_type_cnt(h3c->prx_counters, ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100671 switch (ftype) {
672 case H3_FT_DATA:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200673 ret = h3_data_to_htx(qcs, b, flen, last_stream_frame);
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100674 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200675 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100676 break;
677 case H3_FT_HEADERS:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200678 ret = h3_headers_to_htx(qcs, b, flen, last_stream_frame);
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200679 if (ret < 0) {
680 /* TODO for some error, it may be preferable to
681 * only close the stream once RESET_STREAM is
682 * supported.
683 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200684 qcc_emit_cc_app(qcs->qcc, h3c->err, 1);
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200685 return -1;
686 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100687 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200688 case H3_FT_CANCEL_PUSH:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100689 case H3_FT_PUSH_PROMISE:
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200690 case H3_FT_MAX_PUSH_ID:
691 case H3_FT_GOAWAY:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100692 /* Not supported */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200693 ret = flen;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100694 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200695 case H3_FT_SETTINGS:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200696 ret = h3_parse_settings_frm(qcs->qcc->ctx, b, flen);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200697 if (ret < 0) {
Amaury Denoyelled666d742022-07-13 15:15:58 +0200698 qcc_emit_cc_app(qcs->qcc, h3c->err, 1);
Amaury Denoyelledca4c532022-06-07 18:24:34 +0200699 return -1;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200700 }
701 h3c->flags |= H3_CF_SETTINGS_RECV;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200702 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100703 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100704 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200705 *
706 * Implementations MUST discard frames [...] that have unknown
707 * or unsupported types.
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100708 */
709 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200710 ret = flen;
711 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100712 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200713
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200714 if (ret) {
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200715 BUG_ON(h3s->demux_frame_len < ret);
716 h3s->demux_frame_len -= ret;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200717 b_del(b, ret);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200718 total += ret;
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200719 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100720 }
721
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +0200722 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
723 * However, currently, io-cb of MUX does not handle Rx.
724 */
725
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200726 return total;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100727}
728
Amaury Denoyellea5871362021-10-07 16:26:12 +0200729/* Returns buffer for data sending.
730 * May be NULL if the allocation failed.
731 */
732static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100733{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200734 if (!b_size(&qcs->tx.buf))
735 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100736
Amaury Denoyellea5871362021-10-07 16:26:12 +0200737 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100738}
739
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200740/* Function used to emit stream data from <qcs> control uni-stream */
741static int h3_control_send(struct qcs *qcs, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100742{
743 int ret;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200744 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100745 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200746 struct buffer pos, *res;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200747 size_t frm_len;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200748
Amaury Denoyellea717eb72022-05-30 15:51:01 +0200749 TRACE_ENTER(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs);
750
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200751 BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100752
753 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200754 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100755
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200756 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
757 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
758 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
759 quic_int_getsize(h3_settings_qpack_blocked_streams);
760 if (h3_settings_max_field_section_size) {
761 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
762 quic_int_getsize(h3_settings_max_field_section_size);
763 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100764
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200765 b_quic_enc_int(&pos, H3_UNI_S_T_CTRL);
766 /* Build a SETTINGS frame */
767 b_quic_enc_int(&pos, H3_FT_SETTINGS);
768 b_quic_enc_int(&pos, frm_len);
769 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
770 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
771 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
772 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
773 if (h3_settings_max_field_section_size) {
774 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
775 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
776 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200777
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200778 res = mux_get_buf(qcs);
779 if (b_room(res) < b_data(&pos)) {
780 // TODO the mux should be put in blocked state, with
781 // the stream in state waiting for settings to be sent
782 ABORT_NOW();
783 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100784
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200785 ret = b_force_xfer(res, &pos, b_data(&pos));
786 if (ret > 0) {
787 h3c->flags |= H3_CF_SETTINGS_SENT;
788 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
789 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100790 }
791
Amaury Denoyellea717eb72022-05-30 15:51:01 +0200792 TRACE_LEAVE(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100793 return ret;
794}
795
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200796static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
797{
798 struct buffer outbuf;
799 struct buffer headers_buf = BUF_NULL;
800 struct buffer *res;
801 struct http_hdr list[global.tune.max_http_hdr];
802 struct htx_sl *sl;
803 struct htx_blk *blk;
804 enum htx_blk_type type;
805 int frame_length_size; /* size in bytes of frame length varint field */
806 int ret = 0;
807 int hdr;
808 int status = 0;
809
Amaury Denoyellea717eb72022-05-30 15:51:01 +0200810 TRACE_ENTER(H3_EV_TX_HDR, qcs->qcc->conn, qcs);
811
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200812 sl = NULL;
813 hdr = 0;
814 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
815 type = htx_get_blk_type(blk);
816
817 if (type == HTX_BLK_UNUSED)
818 continue;
819
820 if (type == HTX_BLK_EOH)
821 break;
822
823 if (type == HTX_BLK_RES_SL) {
824 /* start-line -> HEADERS h3 frame */
825 BUG_ON(sl);
826 sl = htx_get_blk_ptr(htx, blk);
827 /* TODO should be on h3 layer */
828 status = sl->info.res.status;
829 }
830 else if (type == HTX_BLK_HDR) {
Amaury Denoyelle60ef19f2022-06-14 17:38:36 +0200831 if (unlikely(hdr >= sizeof(list) / sizeof(list[0]) - 1))
Amaury Denoyellefa7fadc2022-06-15 15:52:27 +0200832 goto err;
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200833 list[hdr].n = htx_get_blk_name(htx, blk);
834 list[hdr].v = htx_get_blk_value(htx, blk);
835 hdr++;
836 }
837 else {
838 ABORT_NOW();
839 goto err;
840 }
841 }
842
843 BUG_ON(!sl);
844
845 list[hdr].n = ist("");
846
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200847 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200848
849 /* At least 5 bytes to store frame type + length as a varint max size */
850 if (b_room(res) < 5)
851 ABORT_NOW();
852
853 b_reset(&outbuf);
854 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
855 /* Start the headers after frame type + length */
856 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
857
858 if (qpack_encode_field_section_line(&headers_buf))
859 ABORT_NOW();
860 if (qpack_encode_int_status(&headers_buf, status))
861 ABORT_NOW();
862
863 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
864 if (isteq(list[hdr].n, ist("")))
865 break;
866
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100867 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
868 * Transfer codings (see Section 6.1 of [HTTP11]) are not
869 * defined for HTTP/3; the Transfer-Encoding header field MUST
870 * NOT be used.
871 */
872 if (isteq(list[hdr].n, ist("transfer-encoding")))
873 continue;
874
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200875 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
876 ABORT_NOW();
877 }
878
879 /* Now that all headers are encoded, we are certain that res buffer is
880 * big enough
881 */
882 frame_length_size = quic_int_getsize(b_data(&headers_buf));
883 res->head += 4 - frame_length_size;
884 b_putchr(res, 0x01); /* h3 HEADERS frame type */
885 if (!b_quic_enc_int(res, b_data(&headers_buf)))
886 ABORT_NOW();
887 b_add(res, b_data(&headers_buf));
888
889 ret = 0;
890 blk = htx_get_head_blk(htx);
891 while (blk) {
892 type = htx_get_blk_type(blk);
893 ret += htx_get_blksz(blk);
894 blk = htx_remove_blk(htx, blk);
895 if (type == HTX_BLK_EOH)
896 break;
897 }
898
Amaury Denoyellea717eb72022-05-30 15:51:01 +0200899 TRACE_LEAVE(H3_EV_TX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200900 return ret;
901
902 err:
Amaury Denoyellea717eb72022-05-30 15:51:01 +0200903 TRACE_DEVEL("leaving on error", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200904 return 0;
905}
906
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200907/* Returns the total of bytes sent. */
908static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
909{
910 struct buffer outbuf;
911 struct buffer *res;
912 size_t total = 0;
913 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200914 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200915 struct htx_blk *blk;
916 enum htx_blk_type type;
917
Amaury Denoyellea717eb72022-05-30 15:51:01 +0200918 TRACE_ENTER(H3_EV_TX_DATA, qcs->qcc->conn, qcs);
919
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200920 htx = htx_from_buf(buf);
921
922 new_frame:
923 if (!count || htx_is_empty(htx))
924 goto end;
925
926 blk = htx_get_head_blk(htx);
927 type = htx_get_blk_type(blk);
928 fsize = bsize = htx_get_blksz(blk);
929
930 if (type != HTX_BLK_DATA)
931 goto end;
932
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200933 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200934
935 if (fsize > count)
936 fsize = count;
937
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200938 /* h3 DATA headers : 1-byte frame type + varint frame length */
939 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200940
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200941 while (1) {
942 b_reset(&outbuf);
943 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
944 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
945 break;
946 b_slow_realign(res, trash.area, b_data(res));
947 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200948
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100949 /* Not enough room for headers and at least one data byte, block the
Willy Tarreau4596fe22022-05-17 19:07:51 +0200950 * stream. It is expected that the stream connector layer will subscribe
951 * on SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200952 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100953 if (b_size(&outbuf) <= hsize) {
954 qcs->flags |= QC_SF_BLK_MROOM;
955 goto end;
956 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200957
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200958 if (b_size(&outbuf) < hsize + fsize)
959 fsize = b_size(&outbuf) - hsize;
960 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200961
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200962 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
963 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
964
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200965 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200966 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200967 count -= fsize;
968
969 if (fsize == bsize)
970 htx_remove_blk(htx, blk);
971 else
972 htx_cut_data_blk(htx, blk, fsize);
973
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200974 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200975 b_add(res, b_data(&outbuf));
976 goto new_frame;
977
978 end:
Amaury Denoyellea717eb72022-05-30 15:51:01 +0200979 TRACE_LEAVE(H3_EV_TX_DATA, qcs->qcc->conn, qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200980 return total;
981}
982
Willy Tarreau3215e732022-05-27 10:09:11 +0200983size_t h3_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200984{
985 size_t total = 0;
Willy Tarreau3215e732022-05-27 10:09:11 +0200986 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200987 struct htx *htx;
988 enum htx_blk_type btype;
989 struct htx_blk *blk;
990 uint32_t bsize;
991 int32_t idx;
992 int ret;
993
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100994 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100995
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200996 htx = htx_from_buf(buf);
997
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100998 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200999 idx = htx_get_head(htx);
1000 blk = htx_get_blk(htx, idx);
1001 btype = htx_get_blk_type(blk);
1002 bsize = htx_get_blksz(blk);
1003
1004 /* Not implemented : QUIC on backend side */
1005 BUG_ON(btype == HTX_BLK_REQ_SL);
1006
1007 switch (btype) {
1008 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001009 /* start-line -> HEADERS h3 frame */
1010 ret = h3_resp_headers_send(qcs, htx);
1011 if (ret > 0) {
1012 total += ret;
1013 count -= ret;
1014 if (ret < bsize)
1015 goto out;
1016 }
1017 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001018
1019 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001020 ret = h3_resp_data_send(qcs, buf, count);
1021 if (ret > 0) {
1022 htx = htx_from_buf(buf);
1023 total += ret;
1024 count -= ret;
1025 if (ret < bsize)
1026 goto out;
1027 }
1028 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001029
1030 case HTX_BLK_TLR:
1031 case HTX_BLK_EOT:
1032 /* TODO trailers */
1033
1034 default:
1035 htx_remove_blk(htx, blk);
1036 total += bsize;
1037 count -= bsize;
1038 break;
1039 }
1040 }
1041
Amaury Denoyellec2025c12021-12-03 15:03:36 +01001042 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
1043 qcs->flags |= QC_SF_FIN_STREAM;
1044
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001045 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001046 if (total) {
1047 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
1048 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
1049 }
1050
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001051 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +02001052}
1053
Amaury Denoyellec0156792022-06-03 15:29:07 +02001054static int h3_attach(struct qcs *qcs, void *conn_ctx)
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001055{
1056 struct h3s *h3s;
1057
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001058 TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
1059
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001060 h3s = pool_alloc(pool_head_h3s);
1061 if (!h3s)
1062 return 1;
1063
1064 qcs->ctx = h3s;
Amaury Denoyellec0156792022-06-03 15:29:07 +02001065 h3s->h3c = conn_ctx;
1066
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001067 h3s->demux_frame_len = 0;
1068 h3s->demux_frame_type = 0;
Amaury Denoyelle35550642022-05-24 15:14:53 +02001069 h3s->flags = 0;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001070
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +02001071 if (quic_stream_is_bidi(qcs->id)) {
1072 h3s->type = H3S_T_REQ;
1073 }
1074 else {
1075 /* stream type must be decoded for unidirectional streams */
1076 h3s->type = H3S_T_UNKNOWN;
1077 }
1078
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001079 TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001080 return 0;
1081}
1082
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001083static void h3_detach(struct qcs *qcs)
1084{
1085 struct h3s *h3s = qcs->ctx;
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001086
1087 TRACE_ENTER(H3_EV_H3S_END, qcs->qcc->conn, qcs);
1088
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001089 pool_free(pool_head_h3s, h3s);
1090 qcs->ctx = NULL;
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001091
1092 TRACE_LEAVE(H3_EV_H3S_END, qcs->qcc->conn, qcs);
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001093}
1094
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001095static int h3_finalize(void *ctx)
1096{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001097 struct h3c *h3c = ctx;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001098 struct qcs *qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001099
Amaury Denoyelleb1437232022-07-08 11:53:22 +02001100 qcs = qcc_init_stream_local(h3c->qcc, 0);
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001101 if (!qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001102 return 0;
1103
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001104 h3_control_send(qcs, h3c);
Amaury Denoyelled7010392022-07-13 15:17:29 +02001105 h3c->ctrl_strm = qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001106
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001107 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001108}
1109
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001110/* Generate a GOAWAY frame for <h3c> connection on the control stream.
1111 *
1112 * Returns 0 on success else non-zero.
1113 */
1114static int h3_send_goaway(struct h3c *h3c)
1115{
1116 struct qcs *qcs = h3c->ctrl_strm;
1117 struct buffer pos, *res;
1118 unsigned char data[3 * QUIC_VARINT_MAX_SIZE];
1119 size_t frm_len = quic_int_getsize(h3c->id_goaway);
1120
1121 if (!qcs)
1122 return 1;
1123
1124 pos = b_make((char *)data, sizeof(data), 0, 0);
1125
1126 b_quic_enc_int(&pos, H3_FT_GOAWAY);
1127 b_quic_enc_int(&pos, frm_len);
1128 b_quic_enc_int(&pos, h3c->id_goaway);
1129
1130 res = mux_get_buf(qcs);
1131 if (!res || b_room(res) < b_data(&pos)) {
1132 /* Do not try forcefully to emit GOAWAY if no space left. */
1133 return 1;
1134 }
1135
1136 b_force_xfer(res, &pos, b_data(&pos));
1137
1138 return 0;
1139}
1140
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001141/* Initialize the HTTP/3 context for <qcc> mux.
1142 * Return 1 if succeeded, 0 if not.
1143 */
1144static int h3_init(struct qcc *qcc)
1145{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001146 struct h3c *h3c;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001147 struct quic_conn *qc = qcc->conn->handle.qc;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001148
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001149 h3c = pool_alloc(pool_head_h3c);
1150 if (!h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001151 goto fail_no_h3;
1152
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001153 h3c->qcc = qcc;
Amaury Denoyelled7010392022-07-13 15:17:29 +02001154 h3c->ctrl_strm = NULL;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001155 h3c->err = H3_NO_ERROR;
1156 h3c->flags = 0;
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001157 h3c->id_goaway = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001158
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001159 qcc->ctx = h3c;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001160 h3c->prx_counters =
1161 EXTRA_COUNTERS_GET(qc->li->bind_conf->frontend->extra_counters_fe,
1162 &h3_stats_module);
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001163 LIST_INIT(&h3c->buf_wait.list);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001164
1165 return 1;
1166
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001167 fail_no_h3:
1168 return 0;
1169}
1170
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001171static void h3_release(void *ctx)
1172{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001173 struct h3c *h3c = ctx;
Amaury Denoyelle069288b2022-07-15 10:58:25 +02001174
1175 /* RFC 9114 5.2. Connection Shutdown
1176 *
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001177 * Even when a connection is not idle, either endpoint can decide to
1178 * stop using the connection and initiate a graceful connection close.
1179 * Endpoints initiate the graceful shutdown of an HTTP/3 connection by
1180 * sending a GOAWAY frame.
1181 */
1182 h3_send_goaway(h3c);
1183
1184 /* RFC 9114 5.2. Connection Shutdown
1185 *
Amaury Denoyelle069288b2022-07-15 10:58:25 +02001186 * An endpoint that completes a
1187 * graceful shutdown SHOULD use the H3_NO_ERROR error code when closing
1188 * the connection.
1189 */
1190 qcc_emit_cc_app(h3c->qcc, H3_NO_ERROR, 0);
1191
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001192 pool_free(pool_head_h3c, h3c);
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001193}
1194
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001195/* Check if the H3 connection can still be considered as active.
1196 *
1197 * Return true if active else false.
1198 */
1199static int h3_is_active(const struct qcc *qcc, void *ctx)
1200{
1201 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
1202 return 1;
1203
1204 return 0;
1205}
1206
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001207/* Increment the h3 error code counters for <error_code> value */
1208static void h3_stats_inc_err_cnt(void *ctx, int err_code)
1209{
1210 struct h3c *h3c = ctx;
1211
1212 h3_inc_err_cnt(h3c->prx_counters, err_code);
1213}
1214
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001215/* h3 trace handler */
1216static void h3_trace(enum trace_level level, uint64_t mask,
1217 const struct trace_source *src,
1218 const struct ist where, const struct ist func,
1219 const void *a1, const void *a2, const void *a3, const void *a4)
1220{
1221 const struct connection *conn = a1;
1222 const struct qcc *qcc = conn ? conn->ctx : NULL;
1223 const struct qcs *qcs = a2;
1224
1225 if (src->verbosity > H3_VERB_CLEAN) {
1226 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1227
1228 if (qcs)
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001229 chunk_appendf(&trace_buf, " qcs=%p(%llu)", qcs, (ull)qcs->id);
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001230 }
1231}
1232
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001233/* HTTP/3 application layer operations */
1234const struct qcc_app_ops h3_ops = {
1235 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001236 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001237 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +01001238 .snd_buf = h3_snd_buf,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001239 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001240 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001241 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001242 .release = h3_release,
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001243 .inc_err_cnt = h3_stats_inc_err_cnt,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001244};