blob: 49750f93c8e8d8d3b18412ad99e7b2444b6cd97b [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>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020029#include <haproxy/ncbuf.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010030#include <haproxy/pool.h>
31#include <haproxy/qpack-dec.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020032#include <haproxy/qpack-enc.h>
33#include <haproxy/quic_enc.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020034#include <haproxy/stconn.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010035#include <haproxy/tools.h>
Amaury Denoyelle016aa932022-05-30 15:49:36 +020036#include <haproxy/trace.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010037#include <haproxy/xprt_quic.h>
38
Amaury Denoyelle016aa932022-05-30 15:49:36 +020039/* trace source and events */
40static void h3_trace(enum trace_level level, uint64_t mask,
41 const struct trace_source *src,
42 const struct ist where, const struct ist func,
43 const void *a1, const void *a2, const void *a3, const void *a4);
44
45static const struct trace_event h3_trace_events[] = {
Amaury Denoyelle494512d2022-05-30 15:50:34 +020046#define H3_EV_RX_FRAME (1ULL << 0)
47 { .mask = H3_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H3 frame" },
48#define H3_EV_RX_DATA (1ULL << 1)
49 { .mask = H3_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H3 DATA frame" },
50#define H3_EV_RX_HDR (1ULL << 2)
51 { .mask = H3_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H3 HEADERS frame" },
52#define H3_EV_RX_SETTINGS (1ULL << 3)
53 { .mask = H3_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H3 SETTINGS frame" },
Amaury Denoyelle016aa932022-05-30 15:49:36 +020054 { }
55};
56
57static const struct name_desc h3_trace_lockon_args[4] = {
58 /* arg1 */ { /* already used by the connection */ },
59 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
60 /* arg3 */ { },
61 /* arg4 */ { }
62};
63
64static const struct name_desc h3_trace_decoding[] = {
65#define H3_VERB_CLEAN 1
66 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
67#define H3_VERB_MINIMAL 2
68 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
69 { /* end */ }
70};
71
72struct trace_source trace_h3 = {
73 .name = IST("h3"),
74 .desc = "HTTP/3 transcoder",
75 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
76 .default_cb = h3_trace,
77 .known_events = h3_trace_events,
78 .lockon_args = h3_trace_lockon_args,
79 .decoding = h3_trace_decoding,
80 .report_events = ~0, /* report everything by default */
81};
82
83#define TRACE_SOURCE &trace_h3
84INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
85
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010086#if defined(DEBUG_H3)
87#define h3_debug_printf fprintf
88#define h3_debug_hexdump debug_hexdump
89#else
90#define h3_debug_printf(...) do { } while (0)
91#define h3_debug_hexdump(...) do { } while (0)
92#endif
93
Amaury Denoyelle302ecd42022-05-24 15:24:32 +020094#define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */
95#define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */
96#define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */
97#define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */
98#define H3_CF_UNI_QPACK_ENC_SET 0x00000010 /* Remote QPACK encoder stream opened */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010099
100/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +0200101static uint64_t h3_settings_qpack_max_table_capacity = 0;
102static uint64_t h3_settings_qpack_blocked_streams = 4096;
103static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100104
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200105struct h3c {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100106 struct qcc *qcc;
107 enum h3_err err;
108 uint32_t flags;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200109
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100110 /* Settings */
111 uint64_t qpack_max_table_capacity;
112 uint64_t qpack_blocked_streams;
113 uint64_t max_field_section_size;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200114
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100115 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200116 /* Stats counters */
117 struct h3_counters *prx_counters;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100118};
119
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200120DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100121
Amaury Denoyelle35550642022-05-24 15:14:53 +0200122#define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200123#define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */
Amaury Denoyelle35550642022-05-24 15:14:53 +0200124
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200125struct h3s {
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200126 enum h3s_t type;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200127 int demux_frame_len;
128 int demux_frame_type;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200129
130 int flags;
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200131};
132
133DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s));
134
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100135/* Simple function to duplicate a buffer */
Amaury Denoyellec7dd9d62022-05-24 18:14:28 +0200136static inline struct buffer h3_b_dup(const struct ncbuf *b)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100137{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200138 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100139}
140
Amaury Denoyelle35550642022-05-24 15:14:53 +0200141/* Initialize an uni-stream <qcs> by reading its type from <rxbuf>.
142 *
143 * Returns 0 on success else non-zero.
144 */
145static int h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs,
146 struct ncbuf *rxbuf)
147{
148 /* decode unidirectional stream type */
149 struct h3s *h3s = qcs->ctx;
150 struct buffer b;
151 uint64_t type;
152 size_t len = 0, ret;
153
154 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
155 h3s->flags & H3_SF_UNI_INIT);
156
157 b = h3_b_dup(rxbuf);
158 ret = b_quic_dec_int(&type, &b, &len);
159 if (!ret) {
160 ABORT_NOW();
161 }
162
163 switch (type) {
164 case H3_UNI_S_T_CTRL:
165 if (h3c->flags & H3_CF_UNI_CTRL_SET) {
166 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
167 return 1;
168 }
169 h3c->flags |= H3_CF_UNI_CTRL_SET;
170 h3s->type = H3S_T_CTRL;
171 break;
172
173 case H3_UNI_S_T_PUSH:
174 /* TODO not supported for the moment */
175 h3s->type = H3S_T_PUSH;
176 break;
177
178 case H3_UNI_S_T_QPACK_DEC:
179 if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) {
180 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
181 return 1;
182 }
183 h3c->flags |= H3_CF_UNI_QPACK_DEC_SET;
184 h3s->type = H3S_T_QPACK_DEC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200185 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200186 break;
187
188 case H3_UNI_S_T_QPACK_ENC:
189 if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) {
190 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
191 return 1;
192 }
193 h3c->flags |= H3_CF_UNI_QPACK_ENC_SET;
194 h3s->type = H3S_T_QPACK_ENC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200195 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200196 break;
197
198 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200199 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
200 *
201 * Implementations MUST [...] abort reading on unidirectional
202 * streams that have unknown or unsupported types.
203 */
204 qcs->flags |= QC_SF_READ_ABORTED;
205 return 1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200206 };
207
208 h3s->flags |= H3_SF_UNI_INIT;
209 qcs_consume(qcs, len);
210
211 return 0;
212}
213
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200214/* Parse an uni-stream <qcs> from <rxbuf> which does not contains H3 frames.
215 * This may be used for QPACK encoder/decoder streams for example.
216 *
217 * Returns 0 on success else non-zero.
218 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200219static int h3_parse_uni_stream_no_h3(struct qcs *qcs, struct ncbuf *rxbuf)
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200220{
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200221 struct h3s *h3s = qcs->ctx;
222
223 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
224 !(h3s->flags & H3_SF_UNI_NO_H3));
225
226 switch (h3s->type) {
227 case H3S_T_QPACK_DEC:
228 if (!qpack_decode_dec(qcs, NULL))
229 return 1;
230 break;
231 case H3S_T_QPACK_ENC:
232 if (!qpack_decode_enc(qcs, NULL))
233 return 1;
234 break;
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200235 case H3S_T_UNKNOWN:
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200236 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200237 /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */
238 ABORT_NOW();
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200239 }
240
241 return 0;
242}
243
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100244/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
245 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
246 * Note that this function update <b> buffer to reflect the number of bytes consumed
247 * to decode the h3 frame header.
248 */
249static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
250 struct buffer *b)
251{
252 size_t hlen;
253
254 hlen = 0;
255 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
256 return 0;
257
258 return hlen;
259}
260
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200261/* Check if H3 frame of type <ftype> is valid when received on stream <qcs>.
262 *
263 * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should
264 * be reported.
265 */
266static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype)
267{
268 struct h3s *h3s = qcs->ctx;
269 const uint64_t id = qcs->id;
270
271 BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN);
272
273 switch (ftype) {
274 case H3_FT_DATA:
275 case H3_FT_HEADERS:
276 return h3s->type != H3S_T_CTRL;
277
278 case H3_FT_CANCEL_PUSH:
279 case H3_FT_GOAWAY:
280 case H3_FT_MAX_PUSH_ID:
281 /* Only allowed for control stream. First frame of control
282 * stream MUST be SETTINGS.
283 */
284 return h3s->type == H3S_T_CTRL &&
285 (h3c->flags & H3_CF_SETTINGS_RECV);
286
287 case H3_FT_SETTINGS:
288 /* draft-ietf-quic-http34 7.2.4. SETTINGS
289 *
290 * If an endpoint receives a second SETTINGS frame on the control
291 * stream, the endpoint MUST respond with a connection error of type
292 * H3_FRAME_UNEXPECTED.
293 */
294 return h3s->type == H3S_T_CTRL &&
295 !(h3c->flags & H3_CF_SETTINGS_RECV);
296
297 case H3_FT_PUSH_PROMISE:
298 return h3s->type != H3S_T_CTRL &&
299 (id & QCS_ID_SRV_INTIATOR_BIT);
300
301 default:
302 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
303 *
304 * Implementations MUST discard frames [...] that have unknown
305 * or unsupported types.
306 */
307 return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV);
308 }
309}
310
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100311/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
Willy Tarreau4596fe22022-05-17 19:07:51 +0200312 * in a local HTX buffer and transfer to the stream connector layer. <fin> must be
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100313 * set if this is the last data to transfer from this stream.
314 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200315 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100316 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200317static int h3_headers_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100318 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100319{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100320 struct buffer htx_buf = BUF_NULL;
321 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100322 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200323 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200324 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200325 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100326 struct ist meth = IST_NULL, path = IST_NULL;
327 //struct ist scheme = IST_NULL, authority = IST_NULL;
328 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200329 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100330
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200331 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
332
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200333 /* TODO support buffer wrapping */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200334 BUG_ON(ncb_head(buf) + len >= ncb_wrap(buf));
335 if (qpack_decode_fs((const unsigned char *)ncb_head(buf), len, tmp, list) < 0)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200336 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100337
338 qc_get_buf(qcs, &htx_buf);
339 BUG_ON(!b_size(&htx_buf));
340 htx = htx_from_buf(&htx_buf);
341
342 /* first treat pseudo-header to build the start line */
343 hdr_idx = 0;
344 while (1) {
345 if (isteq(list[hdr_idx].n, ist("")))
346 break;
347
348 if (istmatch(list[hdr_idx].n, ist(":"))) {
349 /* pseudo-header */
350 if (isteq(list[hdr_idx].n, ist(":method")))
351 meth = list[hdr_idx].v;
352 else if (isteq(list[hdr_idx].n, ist(":path")))
353 path = list[hdr_idx].v;
354 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
355 // scheme = list[hdr_idx].v;
356 else if (isteq(list[hdr_idx].n, ist(":authority")))
357 authority = list[hdr_idx].v;
358 }
359
360 ++hdr_idx;
361 }
362
363 flags |= HTX_SL_F_VER_11;
Amaury Denoyelle0fa14a62022-04-26 16:24:39 +0200364 flags |= HTX_SL_F_XFER_LEN;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100365
366 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
367 if (!sl)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200368 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100369
370 if (fin)
371 sl->flags |= HTX_SL_F_BODYLESS;
372
373 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100374
375 if (isttest(authority))
376 htx_add_header(htx, ist("host"), authority);
377
378 /* now treat standard headers */
379 hdr_idx = 0;
380 while (1) {
381 if (isteq(list[hdr_idx].n, ist("")))
382 break;
383
384 if (!istmatch(list[hdr_idx].n, ist(":")))
385 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
386
387 ++hdr_idx;
388 }
389
390 htx_add_endof(htx, HTX_BLK_EOH);
391 htx_to_buf(htx, &htx_buf);
392
393 if (fin)
394 htx->flags |= HTX_FL_EOM;
395
Willy Tarreau3215e732022-05-27 10:09:11 +0200396 if (!qc_attach_sc(qcs, &htx_buf))
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200397 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100398
Willy Tarreau4596fe22022-05-17 19:07:51 +0200399 /* buffer is transferred to the stream connector and set to NULL
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100400 * except on stream creation error.
401 */
402 b_free(&htx_buf);
403 offer_buffers(NULL, 1);
404
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200405 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200406 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100407}
408
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100409/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
410 * HTX buffer. <fin> must be set if this is the last data to transfer from this
411 * stream.
412 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200413 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100414 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200415static int h3_data_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100416 char fin)
417{
418 struct buffer *appbuf;
419 struct htx *htx = NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200420 size_t htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100421 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200422 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100423
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200424 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs);
425
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100426 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
427 BUG_ON(!appbuf);
428 htx = htx_from_buf(appbuf);
429
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200430 if (len > ncb_data(buf, 0)) {
431 len = ncb_data(buf, 0);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200432 fin = 0;
433 }
434
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200435 head = ncb_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200436 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100437 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200438 if (!htx_space) {
439 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200440 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200441 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200442
443 if (len > htx_space) {
444 len = htx_space;
445 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100446 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100447
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200448 if (head + len > ncb_wrap(buf)) {
449 size_t contig = ncb_wrap(buf) - head;
450 htx_sent = htx_add_data(htx, ist2(ncb_head(buf), contig));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200451 if (htx_sent < contig) {
452 qcs->flags |= QC_SF_DEM_FULL;
453 goto out;
454 }
455
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200456 len -= contig;
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200457 head = ncb_orig(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200458 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100459 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100460
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200461 htx_sent += htx_add_data(htx, ist2(head, len));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200462 if (htx_sent < len) {
463 qcs->flags |= QC_SF_DEM_FULL;
464 goto out;
465 }
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200466
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200467 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100468 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100469
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200470 out:
471 htx_to_buf(htx, appbuf);
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200472
473 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200474 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100475}
476
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200477/* Parse a SETTINGS frame of length <len> of payload <rxbuf>.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200478 *
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200479 * Returns the number of bytes handled or a negative error code.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200480 */
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200481static size_t h3_parse_settings_frm(struct h3c *h3c, const struct ncbuf *rxbuf,
482 size_t len)
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200483{
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200484 struct buffer b;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200485 uint64_t id, value;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200486 size_t ret = 0;
487 long mask = 0; /* used to detect duplicated settings identifier */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200488
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200489 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS, h3c->qcc->conn);
490
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200491 b = h3_b_dup(rxbuf);
492 b_set_data(&b, len);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200493
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200494 while (b_data(&b)) {
495 if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) {
496 h3c->err = H3_FRAME_ERROR;
497 return -1;
498 }
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200499
500 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
501 __func__, (unsigned long long)id, (unsigned long long)value);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200502
503 /* draft-ietf-quic-http34 7.2.4. SETTINGS
504 *
505 * The same setting identifier MUST NOT occur more than once in the
506 * SETTINGS frame. A receiver MAY treat the presence of duplicate
507 * setting identifiers as a connection error of type H3_SETTINGS_ERROR.
508 */
509
510 /* Ignore duplicate check for ID too big used for GREASE. */
511 if (id < sizeof(mask)) {
512 if (ha_bit_test(id, &mask)) {
513 h3c->err = H3_SETTINGS_ERROR;
514 return -1;
515 }
516 ha_bit_set(id, &mask);
517 }
518
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200519 switch (id) {
520 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
521 h3c->qpack_max_table_capacity = value;
522 break;
523 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
524 h3c->max_field_section_size = value;
525 break;
526 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
527 h3c->qpack_blocked_streams = value;
528 break;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200529
530 case H3_SETTINGS_RESERVED_0:
531 case H3_SETTINGS_RESERVED_2:
532 case H3_SETTINGS_RESERVED_3:
533 case H3_SETTINGS_RESERVED_4:
534 case H3_SETTINGS_RESERVED_5:
535 /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters
536 *
537 * Setting identifiers which were defined in [HTTP2] where there is no
538 * corresponding HTTP/3 setting have also been reserved
539 * (Section 11.2.2). These reserved settings MUST NOT be sent, and
540 * their receipt MUST be treated as a connection error of type
541 * H3_SETTINGS_ERROR.
542 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200543 h3c->err = H3_SETTINGS_ERROR;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200544 return -1;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200545 default:
546 /* MUST be ignored */
547 break;
548 }
549 }
550
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200551 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200552 return ret;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200553}
554
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100555/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
556 * that we received the last data of the stream.
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200557 *
558 * Returns 0 on success else non-zero.
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100559 */
560static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
561{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200562 struct ncbuf *rxbuf = &qcs->rx.ncbuf;
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200563 struct h3c *h3c = ctx;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200564 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200565 ssize_t ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100566
Amaury Denoyellebb970422022-04-12 16:40:52 +0200567 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200568 if (!ncb_data(rxbuf, 0))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100569 return 0;
570
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200571 if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) {
572 if (h3_init_uni_stream(h3c, qcs, rxbuf))
573 return 1;
574 }
575
576 if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) {
577 /* For non-h3 STREAM, parse it and return immediately. */
578 if (h3_parse_uni_stream_no_h3(qcs, rxbuf))
579 return 1;
580 return 0;
581 }
582
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200583 while (ncb_data(rxbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100584 uint64_t ftype, flen;
585 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100586 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100587
588 /* Work on a copy of <rxbuf> */
589 b = h3_b_dup(rxbuf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200590 if (!h3s->demux_frame_len) {
591 size_t hlen = h3_decode_frm_header(&ftype, &flen, &b);
592 if (!hlen)
593 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100594
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200595 h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n",
596 __func__, ftype, flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100597
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200598 h3s->demux_frame_type = ftype;
599 h3s->demux_frame_len = flen;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200600 qcs_consume(qcs, hlen);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200601 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100602
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200603 flen = h3s->demux_frame_len;
604 ftype = h3s->demux_frame_type;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200605
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200606 if (!h3_is_frame_valid(h3c, qcs, ftype)) {
607 qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED);
608 return 1;
609 }
610
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200611 /* Do not demux incomplete frames except H3 DATA which can be
612 * fragmented in multiple HTX blocks.
613 */
614 if (flen > b_data(&b) && ftype != H3_FT_DATA) {
615 /* Reject frames bigger than bufsize.
616 *
617 * TODO HEADERS should in complement be limited with H3
618 * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent
619 * excessive decompressed size.
620 */
621 if (flen > ncb_size(rxbuf)) {
622 qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD);
623 return 1;
624 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200625 break;
Amaury Denoyelleb5454d42022-05-12 16:56:16 +0200626 }
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200627
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200628 last_stream_frame = (fin && flen == ncb_total_data(rxbuf));
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100629
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200630 h3_inc_frame_type_cnt(h3c->prx_counters, ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100631 switch (ftype) {
632 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100633 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
634 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200635 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100636 break;
637 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100638 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
639 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200640 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100641 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200642 case H3_FT_CANCEL_PUSH:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100643 case H3_FT_PUSH_PROMISE:
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200644 case H3_FT_MAX_PUSH_ID:
645 case H3_FT_GOAWAY:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100646 /* Not supported */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200647 ret = flen;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100648 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200649 case H3_FT_SETTINGS:
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200650 ret = h3_parse_settings_frm(qcs->qcc->ctx, rxbuf, flen);
651 if (ret < 0) {
652 qcc_emit_cc_app(qcs->qcc, h3c->err);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200653 return 1;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200654 }
655 h3c->flags |= H3_CF_SETTINGS_RECV;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200656 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100657 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100658 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200659 *
660 * Implementations MUST discard frames [...] that have unknown
661 * or unsupported types.
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100662 */
663 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200664 ret = flen;
665 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100666 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200667
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200668 if (ret) {
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200669 BUG_ON(h3s->demux_frame_len < ret);
670 h3s->demux_frame_len -= ret;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200671 qcs_consume(qcs, ret);
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200672 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100673 }
674
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +0200675 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
676 * However, currently, io-cb of MUX does not handle Rx.
677 */
678
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100679 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100680}
681
Amaury Denoyellea5871362021-10-07 16:26:12 +0200682/* Returns buffer for data sending.
683 * May be NULL if the allocation failed.
684 */
685static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100686{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200687 if (!b_size(&qcs->tx.buf))
688 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100689
Amaury Denoyellea5871362021-10-07 16:26:12 +0200690 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100691}
692
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200693/* Function used to emit stream data from <qcs> control uni-stream */
694static int h3_control_send(struct qcs *qcs, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100695{
696 int ret;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200697 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100698 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200699 struct buffer pos, *res;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200700 size_t frm_len;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200701
702 BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100703
704 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200705 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100706
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200707 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
708 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
709 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
710 quic_int_getsize(h3_settings_qpack_blocked_streams);
711 if (h3_settings_max_field_section_size) {
712 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
713 quic_int_getsize(h3_settings_max_field_section_size);
714 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100715
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200716 b_quic_enc_int(&pos, H3_UNI_S_T_CTRL);
717 /* Build a SETTINGS frame */
718 b_quic_enc_int(&pos, H3_FT_SETTINGS);
719 b_quic_enc_int(&pos, frm_len);
720 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
721 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
722 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
723 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
724 if (h3_settings_max_field_section_size) {
725 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
726 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
727 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200728
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200729 res = mux_get_buf(qcs);
730 if (b_room(res) < b_data(&pos)) {
731 // TODO the mux should be put in blocked state, with
732 // the stream in state waiting for settings to be sent
733 ABORT_NOW();
734 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100735
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200736 ret = b_force_xfer(res, &pos, b_data(&pos));
737 if (ret > 0) {
738 h3c->flags |= H3_CF_SETTINGS_SENT;
739 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
740 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100741 }
742
743 return ret;
744}
745
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200746static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
747{
748 struct buffer outbuf;
749 struct buffer headers_buf = BUF_NULL;
750 struct buffer *res;
751 struct http_hdr list[global.tune.max_http_hdr];
752 struct htx_sl *sl;
753 struct htx_blk *blk;
754 enum htx_blk_type type;
755 int frame_length_size; /* size in bytes of frame length varint field */
756 int ret = 0;
757 int hdr;
758 int status = 0;
759
760 sl = NULL;
761 hdr = 0;
762 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
763 type = htx_get_blk_type(blk);
764
765 if (type == HTX_BLK_UNUSED)
766 continue;
767
768 if (type == HTX_BLK_EOH)
769 break;
770
771 if (type == HTX_BLK_RES_SL) {
772 /* start-line -> HEADERS h3 frame */
773 BUG_ON(sl);
774 sl = htx_get_blk_ptr(htx, blk);
775 /* TODO should be on h3 layer */
776 status = sl->info.res.status;
777 }
778 else if (type == HTX_BLK_HDR) {
779 list[hdr].n = htx_get_blk_name(htx, blk);
780 list[hdr].v = htx_get_blk_value(htx, blk);
781 hdr++;
782 }
783 else {
784 ABORT_NOW();
785 goto err;
786 }
787 }
788
789 BUG_ON(!sl);
790
791 list[hdr].n = ist("");
792
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200793 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200794
795 /* At least 5 bytes to store frame type + length as a varint max size */
796 if (b_room(res) < 5)
797 ABORT_NOW();
798
799 b_reset(&outbuf);
800 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
801 /* Start the headers after frame type + length */
802 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
803
804 if (qpack_encode_field_section_line(&headers_buf))
805 ABORT_NOW();
806 if (qpack_encode_int_status(&headers_buf, status))
807 ABORT_NOW();
808
809 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
810 if (isteq(list[hdr].n, ist("")))
811 break;
812
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100813 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
814 * Transfer codings (see Section 6.1 of [HTTP11]) are not
815 * defined for HTTP/3; the Transfer-Encoding header field MUST
816 * NOT be used.
817 */
818 if (isteq(list[hdr].n, ist("transfer-encoding")))
819 continue;
820
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200821 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
822 ABORT_NOW();
823 }
824
825 /* Now that all headers are encoded, we are certain that res buffer is
826 * big enough
827 */
828 frame_length_size = quic_int_getsize(b_data(&headers_buf));
829 res->head += 4 - frame_length_size;
830 b_putchr(res, 0x01); /* h3 HEADERS frame type */
831 if (!b_quic_enc_int(res, b_data(&headers_buf)))
832 ABORT_NOW();
833 b_add(res, b_data(&headers_buf));
834
835 ret = 0;
836 blk = htx_get_head_blk(htx);
837 while (blk) {
838 type = htx_get_blk_type(blk);
839 ret += htx_get_blksz(blk);
840 blk = htx_remove_blk(htx, blk);
841 if (type == HTX_BLK_EOH)
842 break;
843 }
844
845 return ret;
846
847 err:
848 return 0;
849}
850
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200851/* Returns the total of bytes sent. */
852static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
853{
854 struct buffer outbuf;
855 struct buffer *res;
856 size_t total = 0;
857 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200858 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200859 struct htx_blk *blk;
860 enum htx_blk_type type;
861
862 htx = htx_from_buf(buf);
863
864 new_frame:
865 if (!count || htx_is_empty(htx))
866 goto end;
867
868 blk = htx_get_head_blk(htx);
869 type = htx_get_blk_type(blk);
870 fsize = bsize = htx_get_blksz(blk);
871
872 if (type != HTX_BLK_DATA)
873 goto end;
874
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200875 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200876
877 if (fsize > count)
878 fsize = count;
879
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200880 /* h3 DATA headers : 1-byte frame type + varint frame length */
881 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200882
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200883 while (1) {
884 b_reset(&outbuf);
885 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
886 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
887 break;
888 b_slow_realign(res, trash.area, b_data(res));
889 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200890
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100891 /* Not enough room for headers and at least one data byte, block the
Willy Tarreau4596fe22022-05-17 19:07:51 +0200892 * stream. It is expected that the stream connector layer will subscribe
893 * on SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200894 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100895 if (b_size(&outbuf) <= hsize) {
896 qcs->flags |= QC_SF_BLK_MROOM;
897 goto end;
898 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200899
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200900 if (b_size(&outbuf) < hsize + fsize)
901 fsize = b_size(&outbuf) - hsize;
902 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200903
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200904 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
905 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
906
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200907 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200908 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200909 count -= fsize;
910
911 if (fsize == bsize)
912 htx_remove_blk(htx, blk);
913 else
914 htx_cut_data_blk(htx, blk, fsize);
915
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200916 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200917 b_add(res, b_data(&outbuf));
918 goto new_frame;
919
920 end:
921 return total;
922}
923
Willy Tarreau3215e732022-05-27 10:09:11 +0200924size_t h3_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200925{
926 size_t total = 0;
Willy Tarreau3215e732022-05-27 10:09:11 +0200927 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200928 struct htx *htx;
929 enum htx_blk_type btype;
930 struct htx_blk *blk;
931 uint32_t bsize;
932 int32_t idx;
933 int ret;
934
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100935 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100936
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200937 htx = htx_from_buf(buf);
938
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100939 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200940 idx = htx_get_head(htx);
941 blk = htx_get_blk(htx, idx);
942 btype = htx_get_blk_type(blk);
943 bsize = htx_get_blksz(blk);
944
945 /* Not implemented : QUIC on backend side */
946 BUG_ON(btype == HTX_BLK_REQ_SL);
947
948 switch (btype) {
949 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200950 /* start-line -> HEADERS h3 frame */
951 ret = h3_resp_headers_send(qcs, htx);
952 if (ret > 0) {
953 total += ret;
954 count -= ret;
955 if (ret < bsize)
956 goto out;
957 }
958 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200959
960 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200961 ret = h3_resp_data_send(qcs, buf, count);
962 if (ret > 0) {
963 htx = htx_from_buf(buf);
964 total += ret;
965 count -= ret;
966 if (ret < bsize)
967 goto out;
968 }
969 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200970
971 case HTX_BLK_TLR:
972 case HTX_BLK_EOT:
973 /* TODO trailers */
974
975 default:
976 htx_remove_blk(htx, blk);
977 total += bsize;
978 count -= bsize;
979 break;
980 }
981 }
982
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100983 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
984 qcs->flags |= QC_SF_FIN_STREAM;
985
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200986 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200987 if (total) {
988 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
989 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
990 }
991
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200992 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200993}
994
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200995static int h3_attach(struct qcs *qcs)
996{
997 struct h3s *h3s;
998
999 h3s = pool_alloc(pool_head_h3s);
1000 if (!h3s)
1001 return 1;
1002
1003 qcs->ctx = h3s;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001004 h3s->demux_frame_len = 0;
1005 h3s->demux_frame_type = 0;
Amaury Denoyelle35550642022-05-24 15:14:53 +02001006 h3s->flags = 0;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001007
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +02001008 if (quic_stream_is_bidi(qcs->id)) {
1009 h3s->type = H3S_T_REQ;
1010 }
1011 else {
1012 /* stream type must be decoded for unidirectional streams */
1013 h3s->type = H3S_T_UNKNOWN;
1014 }
1015
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001016 return 0;
1017}
1018
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001019static void h3_detach(struct qcs *qcs)
1020{
1021 struct h3s *h3s = qcs->ctx;
1022 pool_free(pool_head_h3s, h3s);
1023 qcs->ctx = NULL;
1024}
1025
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001026static int h3_finalize(void *ctx)
1027{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001028 struct h3c *h3c = ctx;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001029 struct qcs *qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001030
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001031 qcs = qcs_new(h3c->qcc, 0x3, QCS_SRV_UNI);
1032 if (!qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001033 return 0;
1034
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001035 h3_control_send(qcs, h3c);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001036
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001037 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001038}
1039
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001040/* Initialize the HTTP/3 context for <qcc> mux.
1041 * Return 1 if succeeded, 0 if not.
1042 */
1043static int h3_init(struct qcc *qcc)
1044{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001045 struct h3c *h3c;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001046 struct quic_conn *qc = qcc->conn->handle.qc;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001047
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001048 h3c = pool_alloc(pool_head_h3c);
1049 if (!h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001050 goto fail_no_h3;
1051
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001052 h3c->qcc = qcc;
1053 h3c->err = H3_NO_ERROR;
1054 h3c->flags = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001055
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001056 qcc->ctx = h3c;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001057 h3c->prx_counters =
1058 EXTRA_COUNTERS_GET(qc->li->bind_conf->frontend->extra_counters_fe,
1059 &h3_stats_module);
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001060 LIST_INIT(&h3c->buf_wait.list);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001061
1062 return 1;
1063
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001064 fail_no_h3:
1065 return 0;
1066}
1067
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001068static void h3_release(void *ctx)
1069{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001070 struct h3c *h3c = ctx;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001071 pool_free(pool_head_h3c, h3c);
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001072}
1073
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001074/* Check if the H3 connection can still be considered as active.
1075 *
1076 * Return true if active else false.
1077 */
1078static int h3_is_active(const struct qcc *qcc, void *ctx)
1079{
1080 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
1081 return 1;
1082
1083 return 0;
1084}
1085
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001086/* Increment the h3 error code counters for <error_code> value */
1087static void h3_stats_inc_err_cnt(void *ctx, int err_code)
1088{
1089 struct h3c *h3c = ctx;
1090
1091 h3_inc_err_cnt(h3c->prx_counters, err_code);
1092}
1093
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001094/* h3 trace handler */
1095static void h3_trace(enum trace_level level, uint64_t mask,
1096 const struct trace_source *src,
1097 const struct ist where, const struct ist func,
1098 const void *a1, const void *a2, const void *a3, const void *a4)
1099{
1100 const struct connection *conn = a1;
1101 const struct qcc *qcc = conn ? conn->ctx : NULL;
1102 const struct qcs *qcs = a2;
1103
1104 if (src->verbosity > H3_VERB_CLEAN) {
1105 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1106
1107 if (qcs)
1108 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
1109 }
1110}
1111
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001112/* HTTP/3 application layer operations */
1113const struct qcc_app_ops h3_ops = {
1114 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001115 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001116 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +01001117 .snd_buf = h3_snd_buf,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001118 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001119 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001120 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001121 .release = h3_release,
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001122 .inc_err_cnt = h3_stats_inc_err_cnt,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001123};