blob: 14ae2c4a2b73213baf3903be6666b1172e7683e0 [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>
36#include <haproxy/xprt_quic.h>
37
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010038#if defined(DEBUG_H3)
39#define h3_debug_printf fprintf
40#define h3_debug_hexdump debug_hexdump
41#else
42#define h3_debug_printf(...) do { } while (0)
43#define h3_debug_hexdump(...) do { } while (0)
44#endif
45
Amaury Denoyelle302ecd42022-05-24 15:24:32 +020046#define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */
47#define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */
48#define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */
49#define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */
50#define H3_CF_UNI_QPACK_ENC_SET 0x00000010 /* Remote QPACK encoder stream opened */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010051
52/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +020053static uint64_t h3_settings_qpack_max_table_capacity = 0;
54static uint64_t h3_settings_qpack_blocked_streams = 4096;
55static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010056
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +020057struct h3c {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010058 struct qcc *qcc;
59 enum h3_err err;
60 uint32_t flags;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +020061
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010062 /* Settings */
63 uint64_t qpack_max_table_capacity;
64 uint64_t qpack_blocked_streams;
65 uint64_t max_field_section_size;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +020066
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010067 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +020068 /* Stats counters */
69 struct h3_counters *prx_counters;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010070};
71
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +020072DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010073
Amaury Denoyelle35550642022-05-24 15:14:53 +020074#define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */
Amaury Denoyellefc99a692022-05-24 15:25:19 +020075#define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */
Amaury Denoyelle35550642022-05-24 15:14:53 +020076
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020077struct h3s {
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +020078 enum h3s_t type;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +020079 int demux_frame_len;
80 int demux_frame_type;
Amaury Denoyelle35550642022-05-24 15:14:53 +020081
82 int flags;
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020083};
84
85DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s));
86
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010087/* Simple function to duplicate a buffer */
Amaury Denoyellec7dd9d62022-05-24 18:14:28 +020088static inline struct buffer h3_b_dup(const struct ncbuf *b)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010089{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020090 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010091}
92
Amaury Denoyelle35550642022-05-24 15:14:53 +020093/* Initialize an uni-stream <qcs> by reading its type from <rxbuf>.
94 *
95 * Returns 0 on success else non-zero.
96 */
97static int h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs,
98 struct ncbuf *rxbuf)
99{
100 /* decode unidirectional stream type */
101 struct h3s *h3s = qcs->ctx;
102 struct buffer b;
103 uint64_t type;
104 size_t len = 0, ret;
105
106 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
107 h3s->flags & H3_SF_UNI_INIT);
108
109 b = h3_b_dup(rxbuf);
110 ret = b_quic_dec_int(&type, &b, &len);
111 if (!ret) {
112 ABORT_NOW();
113 }
114
115 switch (type) {
116 case H3_UNI_S_T_CTRL:
117 if (h3c->flags & H3_CF_UNI_CTRL_SET) {
118 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
119 return 1;
120 }
121 h3c->flags |= H3_CF_UNI_CTRL_SET;
122 h3s->type = H3S_T_CTRL;
123 break;
124
125 case H3_UNI_S_T_PUSH:
126 /* TODO not supported for the moment */
127 h3s->type = H3S_T_PUSH;
128 break;
129
130 case H3_UNI_S_T_QPACK_DEC:
131 if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) {
132 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
133 return 1;
134 }
135 h3c->flags |= H3_CF_UNI_QPACK_DEC_SET;
136 h3s->type = H3S_T_QPACK_DEC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200137 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200138 break;
139
140 case H3_UNI_S_T_QPACK_ENC:
141 if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) {
142 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
143 return 1;
144 }
145 h3c->flags |= H3_CF_UNI_QPACK_ENC_SET;
146 h3s->type = H3S_T_QPACK_ENC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200147 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200148 break;
149
150 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200151 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
152 *
153 * Implementations MUST [...] abort reading on unidirectional
154 * streams that have unknown or unsupported types.
155 */
156 qcs->flags |= QC_SF_READ_ABORTED;
157 return 1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200158 };
159
160 h3s->flags |= H3_SF_UNI_INIT;
161 qcs_consume(qcs, len);
162
163 return 0;
164}
165
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200166/* Parse an uni-stream <qcs> from <rxbuf> which does not contains H3 frames.
167 * This may be used for QPACK encoder/decoder streams for example.
168 *
169 * Returns 0 on success else non-zero.
170 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200171static int h3_parse_uni_stream_no_h3(struct qcs *qcs, struct ncbuf *rxbuf)
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200172{
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200173 struct h3s *h3s = qcs->ctx;
174
175 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
176 !(h3s->flags & H3_SF_UNI_NO_H3));
177
178 switch (h3s->type) {
179 case H3S_T_QPACK_DEC:
180 if (!qpack_decode_dec(qcs, NULL))
181 return 1;
182 break;
183 case H3S_T_QPACK_ENC:
184 if (!qpack_decode_enc(qcs, NULL))
185 return 1;
186 break;
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200187 case H3S_T_UNKNOWN:
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200188 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200189 /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */
190 ABORT_NOW();
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200191 }
192
193 return 0;
194}
195
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100196/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
197 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
198 * Note that this function update <b> buffer to reflect the number of bytes consumed
199 * to decode the h3 frame header.
200 */
201static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
202 struct buffer *b)
203{
204 size_t hlen;
205
206 hlen = 0;
207 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
208 return 0;
209
210 return hlen;
211}
212
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200213/* Check if H3 frame of type <ftype> is valid when received on stream <qcs>.
214 *
215 * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should
216 * be reported.
217 */
218static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype)
219{
220 struct h3s *h3s = qcs->ctx;
221 const uint64_t id = qcs->id;
222
223 BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN);
224
225 switch (ftype) {
226 case H3_FT_DATA:
227 case H3_FT_HEADERS:
228 return h3s->type != H3S_T_CTRL;
229
230 case H3_FT_CANCEL_PUSH:
231 case H3_FT_GOAWAY:
232 case H3_FT_MAX_PUSH_ID:
233 /* Only allowed for control stream. First frame of control
234 * stream MUST be SETTINGS.
235 */
236 return h3s->type == H3S_T_CTRL &&
237 (h3c->flags & H3_CF_SETTINGS_RECV);
238
239 case H3_FT_SETTINGS:
240 /* draft-ietf-quic-http34 7.2.4. SETTINGS
241 *
242 * If an endpoint receives a second SETTINGS frame on the control
243 * stream, the endpoint MUST respond with a connection error of type
244 * H3_FRAME_UNEXPECTED.
245 */
246 return h3s->type == H3S_T_CTRL &&
247 !(h3c->flags & H3_CF_SETTINGS_RECV);
248
249 case H3_FT_PUSH_PROMISE:
250 return h3s->type != H3S_T_CTRL &&
251 (id & QCS_ID_SRV_INTIATOR_BIT);
252
253 default:
254 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
255 *
256 * Implementations MUST discard frames [...] that have unknown
257 * or unsupported types.
258 */
259 return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV);
260 }
261}
262
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100263/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
Willy Tarreau4596fe22022-05-17 19:07:51 +0200264 * in a local HTX buffer and transfer to the stream connector layer. <fin> must be
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100265 * set if this is the last data to transfer from this stream.
266 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200267 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100268 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200269static int h3_headers_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100270 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100271{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100272 struct buffer htx_buf = BUF_NULL;
273 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100274 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200275 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200276 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200277 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100278 struct ist meth = IST_NULL, path = IST_NULL;
279 //struct ist scheme = IST_NULL, authority = IST_NULL;
280 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200281 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100282
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200283 /* TODO support buffer wrapping */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200284 BUG_ON(ncb_head(buf) + len >= ncb_wrap(buf));
285 if (qpack_decode_fs((const unsigned char *)ncb_head(buf), len, tmp, list) < 0)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200286 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100287
288 qc_get_buf(qcs, &htx_buf);
289 BUG_ON(!b_size(&htx_buf));
290 htx = htx_from_buf(&htx_buf);
291
292 /* first treat pseudo-header to build the start line */
293 hdr_idx = 0;
294 while (1) {
295 if (isteq(list[hdr_idx].n, ist("")))
296 break;
297
298 if (istmatch(list[hdr_idx].n, ist(":"))) {
299 /* pseudo-header */
300 if (isteq(list[hdr_idx].n, ist(":method")))
301 meth = list[hdr_idx].v;
302 else if (isteq(list[hdr_idx].n, ist(":path")))
303 path = list[hdr_idx].v;
304 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
305 // scheme = list[hdr_idx].v;
306 else if (isteq(list[hdr_idx].n, ist(":authority")))
307 authority = list[hdr_idx].v;
308 }
309
310 ++hdr_idx;
311 }
312
313 flags |= HTX_SL_F_VER_11;
Amaury Denoyelle0fa14a62022-04-26 16:24:39 +0200314 flags |= HTX_SL_F_XFER_LEN;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100315
316 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
317 if (!sl)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200318 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100319
320 if (fin)
321 sl->flags |= HTX_SL_F_BODYLESS;
322
323 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
324 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
325
326 if (isttest(authority))
327 htx_add_header(htx, ist("host"), authority);
328
329 /* now treat standard headers */
330 hdr_idx = 0;
331 while (1) {
332 if (isteq(list[hdr_idx].n, ist("")))
333 break;
334
335 if (!istmatch(list[hdr_idx].n, ist(":")))
336 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
337
338 ++hdr_idx;
339 }
340
341 htx_add_endof(htx, HTX_BLK_EOH);
342 htx_to_buf(htx, &htx_buf);
343
344 if (fin)
345 htx->flags |= HTX_FL_EOM;
346
Willy Tarreau3215e732022-05-27 10:09:11 +0200347 if (!qc_attach_sc(qcs, &htx_buf))
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200348 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100349
Willy Tarreau4596fe22022-05-17 19:07:51 +0200350 /* buffer is transferred to the stream connector and set to NULL
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100351 * except on stream creation error.
352 */
353 b_free(&htx_buf);
354 offer_buffers(NULL, 1);
355
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200356 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100357}
358
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100359/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
360 * HTX buffer. <fin> must be set if this is the last data to transfer from this
361 * stream.
362 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200363 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100364 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200365static int h3_data_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100366 char fin)
367{
368 struct buffer *appbuf;
369 struct htx *htx = NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200370 size_t htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100371 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200372 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100373
374 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
375 BUG_ON(!appbuf);
376 htx = htx_from_buf(appbuf);
377
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200378 if (len > ncb_data(buf, 0)) {
379 len = ncb_data(buf, 0);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200380 fin = 0;
381 }
382
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200383 head = ncb_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200384 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100385 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200386 if (!htx_space) {
387 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200388 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200389 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200390
391 if (len > htx_space) {
392 len = htx_space;
393 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100394 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100395
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200396 if (head + len > ncb_wrap(buf)) {
397 size_t contig = ncb_wrap(buf) - head;
398 htx_sent = htx_add_data(htx, ist2(ncb_head(buf), contig));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200399 if (htx_sent < contig) {
400 qcs->flags |= QC_SF_DEM_FULL;
401 goto out;
402 }
403
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200404 len -= contig;
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200405 head = ncb_orig(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200406 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100407 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100408
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200409 htx_sent += htx_add_data(htx, ist2(head, len));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200410 if (htx_sent < len) {
411 qcs->flags |= QC_SF_DEM_FULL;
412 goto out;
413 }
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200414
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200415 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100416 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100417
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200418 out:
419 htx_to_buf(htx, appbuf);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200420 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100421}
422
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200423/* Parse a SETTINGS frame of length <len> of payload <rxbuf>.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200424 *
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200425 * Returns the number of bytes handled or a negative error code.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200426 */
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200427static size_t h3_parse_settings_frm(struct h3c *h3c, const struct ncbuf *rxbuf,
428 size_t len)
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200429{
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200430 struct buffer b;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200431 uint64_t id, value;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200432 size_t ret = 0;
433 long mask = 0; /* used to detect duplicated settings identifier */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200434
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200435 b = h3_b_dup(rxbuf);
436 b_set_data(&b, len);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200437
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200438 while (b_data(&b)) {
439 if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) {
440 h3c->err = H3_FRAME_ERROR;
441 return -1;
442 }
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200443
444 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
445 __func__, (unsigned long long)id, (unsigned long long)value);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200446
447 /* draft-ietf-quic-http34 7.2.4. SETTINGS
448 *
449 * The same setting identifier MUST NOT occur more than once in the
450 * SETTINGS frame. A receiver MAY treat the presence of duplicate
451 * setting identifiers as a connection error of type H3_SETTINGS_ERROR.
452 */
453
454 /* Ignore duplicate check for ID too big used for GREASE. */
455 if (id < sizeof(mask)) {
456 if (ha_bit_test(id, &mask)) {
457 h3c->err = H3_SETTINGS_ERROR;
458 return -1;
459 }
460 ha_bit_set(id, &mask);
461 }
462
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200463 switch (id) {
464 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
465 h3c->qpack_max_table_capacity = value;
466 break;
467 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
468 h3c->max_field_section_size = value;
469 break;
470 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
471 h3c->qpack_blocked_streams = value;
472 break;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200473
474 case H3_SETTINGS_RESERVED_0:
475 case H3_SETTINGS_RESERVED_2:
476 case H3_SETTINGS_RESERVED_3:
477 case H3_SETTINGS_RESERVED_4:
478 case H3_SETTINGS_RESERVED_5:
479 /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters
480 *
481 * Setting identifiers which were defined in [HTTP2] where there is no
482 * corresponding HTTP/3 setting have also been reserved
483 * (Section 11.2.2). These reserved settings MUST NOT be sent, and
484 * their receipt MUST be treated as a connection error of type
485 * H3_SETTINGS_ERROR.
486 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200487 h3c->err = H3_SETTINGS_ERROR;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200488 return -1;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200489 default:
490 /* MUST be ignored */
491 break;
492 }
493 }
494
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200495 return ret;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200496}
497
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100498/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
499 * that we received the last data of the stream.
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200500 *
501 * Returns 0 on success else non-zero.
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100502 */
503static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
504{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200505 struct ncbuf *rxbuf = &qcs->rx.ncbuf;
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200506 struct h3c *h3c = ctx;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200507 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200508 ssize_t ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100509
Amaury Denoyellebb970422022-04-12 16:40:52 +0200510 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200511 if (!ncb_data(rxbuf, 0))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100512 return 0;
513
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200514 if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) {
515 if (h3_init_uni_stream(h3c, qcs, rxbuf))
516 return 1;
517 }
518
519 if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) {
520 /* For non-h3 STREAM, parse it and return immediately. */
521 if (h3_parse_uni_stream_no_h3(qcs, rxbuf))
522 return 1;
523 return 0;
524 }
525
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200526 while (ncb_data(rxbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100527 uint64_t ftype, flen;
528 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100529 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100530
531 /* Work on a copy of <rxbuf> */
532 b = h3_b_dup(rxbuf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200533 if (!h3s->demux_frame_len) {
534 size_t hlen = h3_decode_frm_header(&ftype, &flen, &b);
535 if (!hlen)
536 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100537
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200538 h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n",
539 __func__, ftype, flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100540
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200541 h3s->demux_frame_type = ftype;
542 h3s->demux_frame_len = flen;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200543 qcs_consume(qcs, hlen);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200544 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100545
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200546 flen = h3s->demux_frame_len;
547 ftype = h3s->demux_frame_type;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200548
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200549 if (!h3_is_frame_valid(h3c, qcs, ftype)) {
550 qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED);
551 return 1;
552 }
553
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200554 /* Do not demux incomplete frames except H3 DATA which can be
555 * fragmented in multiple HTX blocks.
556 */
557 if (flen > b_data(&b) && ftype != H3_FT_DATA) {
558 /* Reject frames bigger than bufsize.
559 *
560 * TODO HEADERS should in complement be limited with H3
561 * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent
562 * excessive decompressed size.
563 */
564 if (flen > ncb_size(rxbuf)) {
565 qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD);
566 return 1;
567 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200568 break;
Amaury Denoyelleb5454d42022-05-12 16:56:16 +0200569 }
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200570
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200571 last_stream_frame = (fin && flen == ncb_total_data(rxbuf));
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100572
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200573 h3_inc_frame_type_cnt(h3c->prx_counters, ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100574 switch (ftype) {
575 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100576 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
577 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200578 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100579 break;
580 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100581 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
582 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200583 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100584 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200585 case H3_FT_CANCEL_PUSH:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100586 case H3_FT_PUSH_PROMISE:
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200587 case H3_FT_MAX_PUSH_ID:
588 case H3_FT_GOAWAY:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100589 /* Not supported */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200590 ret = flen;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100591 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200592 case H3_FT_SETTINGS:
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200593 ret = h3_parse_settings_frm(qcs->qcc->ctx, rxbuf, flen);
594 if (ret < 0) {
595 qcc_emit_cc_app(qcs->qcc, h3c->err);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200596 return 1;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200597 }
598 h3c->flags |= H3_CF_SETTINGS_RECV;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200599 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100600 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100601 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200602 *
603 * Implementations MUST discard frames [...] that have unknown
604 * or unsupported types.
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100605 */
606 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200607 ret = flen;
608 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100609 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200610
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200611 if (ret) {
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200612 BUG_ON(h3s->demux_frame_len < ret);
613 h3s->demux_frame_len -= ret;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200614 qcs_consume(qcs, ret);
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200615 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100616 }
617
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +0200618 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
619 * However, currently, io-cb of MUX does not handle Rx.
620 */
621
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100622 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100623}
624
Amaury Denoyellea5871362021-10-07 16:26:12 +0200625/* Returns buffer for data sending.
626 * May be NULL if the allocation failed.
627 */
628static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100629{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200630 if (!b_size(&qcs->tx.buf))
631 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100632
Amaury Denoyellea5871362021-10-07 16:26:12 +0200633 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100634}
635
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200636/* Function used to emit stream data from <qcs> control uni-stream */
637static int h3_control_send(struct qcs *qcs, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100638{
639 int ret;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200640 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100641 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200642 struct buffer pos, *res;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200643 size_t frm_len;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200644
645 BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100646
647 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200648 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100649
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200650 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
651 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
652 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
653 quic_int_getsize(h3_settings_qpack_blocked_streams);
654 if (h3_settings_max_field_section_size) {
655 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
656 quic_int_getsize(h3_settings_max_field_section_size);
657 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100658
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200659 b_quic_enc_int(&pos, H3_UNI_S_T_CTRL);
660 /* Build a SETTINGS frame */
661 b_quic_enc_int(&pos, H3_FT_SETTINGS);
662 b_quic_enc_int(&pos, frm_len);
663 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
664 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
665 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
666 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
667 if (h3_settings_max_field_section_size) {
668 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
669 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
670 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200671
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200672 res = mux_get_buf(qcs);
673 if (b_room(res) < b_data(&pos)) {
674 // TODO the mux should be put in blocked state, with
675 // the stream in state waiting for settings to be sent
676 ABORT_NOW();
677 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100678
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200679 ret = b_force_xfer(res, &pos, b_data(&pos));
680 if (ret > 0) {
681 h3c->flags |= H3_CF_SETTINGS_SENT;
682 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
683 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100684 }
685
686 return ret;
687}
688
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200689static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
690{
691 struct buffer outbuf;
692 struct buffer headers_buf = BUF_NULL;
693 struct buffer *res;
694 struct http_hdr list[global.tune.max_http_hdr];
695 struct htx_sl *sl;
696 struct htx_blk *blk;
697 enum htx_blk_type type;
698 int frame_length_size; /* size in bytes of frame length varint field */
699 int ret = 0;
700 int hdr;
701 int status = 0;
702
703 sl = NULL;
704 hdr = 0;
705 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
706 type = htx_get_blk_type(blk);
707
708 if (type == HTX_BLK_UNUSED)
709 continue;
710
711 if (type == HTX_BLK_EOH)
712 break;
713
714 if (type == HTX_BLK_RES_SL) {
715 /* start-line -> HEADERS h3 frame */
716 BUG_ON(sl);
717 sl = htx_get_blk_ptr(htx, blk);
718 /* TODO should be on h3 layer */
719 status = sl->info.res.status;
720 }
721 else if (type == HTX_BLK_HDR) {
722 list[hdr].n = htx_get_blk_name(htx, blk);
723 list[hdr].v = htx_get_blk_value(htx, blk);
724 hdr++;
725 }
726 else {
727 ABORT_NOW();
728 goto err;
729 }
730 }
731
732 BUG_ON(!sl);
733
734 list[hdr].n = ist("");
735
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200736 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200737
738 /* At least 5 bytes to store frame type + length as a varint max size */
739 if (b_room(res) < 5)
740 ABORT_NOW();
741
742 b_reset(&outbuf);
743 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
744 /* Start the headers after frame type + length */
745 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
746
747 if (qpack_encode_field_section_line(&headers_buf))
748 ABORT_NOW();
749 if (qpack_encode_int_status(&headers_buf, status))
750 ABORT_NOW();
751
752 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
753 if (isteq(list[hdr].n, ist("")))
754 break;
755
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100756 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
757 * Transfer codings (see Section 6.1 of [HTTP11]) are not
758 * defined for HTTP/3; the Transfer-Encoding header field MUST
759 * NOT be used.
760 */
761 if (isteq(list[hdr].n, ist("transfer-encoding")))
762 continue;
763
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200764 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
765 ABORT_NOW();
766 }
767
768 /* Now that all headers are encoded, we are certain that res buffer is
769 * big enough
770 */
771 frame_length_size = quic_int_getsize(b_data(&headers_buf));
772 res->head += 4 - frame_length_size;
773 b_putchr(res, 0x01); /* h3 HEADERS frame type */
774 if (!b_quic_enc_int(res, b_data(&headers_buf)))
775 ABORT_NOW();
776 b_add(res, b_data(&headers_buf));
777
778 ret = 0;
779 blk = htx_get_head_blk(htx);
780 while (blk) {
781 type = htx_get_blk_type(blk);
782 ret += htx_get_blksz(blk);
783 blk = htx_remove_blk(htx, blk);
784 if (type == HTX_BLK_EOH)
785 break;
786 }
787
788 return ret;
789
790 err:
791 return 0;
792}
793
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200794/* Returns the total of bytes sent. */
795static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
796{
797 struct buffer outbuf;
798 struct buffer *res;
799 size_t total = 0;
800 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200801 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200802 struct htx_blk *blk;
803 enum htx_blk_type type;
804
805 htx = htx_from_buf(buf);
806
807 new_frame:
808 if (!count || htx_is_empty(htx))
809 goto end;
810
811 blk = htx_get_head_blk(htx);
812 type = htx_get_blk_type(blk);
813 fsize = bsize = htx_get_blksz(blk);
814
815 if (type != HTX_BLK_DATA)
816 goto end;
817
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200818 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200819
820 if (fsize > count)
821 fsize = count;
822
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200823 /* h3 DATA headers : 1-byte frame type + varint frame length */
824 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200825
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200826 while (1) {
827 b_reset(&outbuf);
828 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
829 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
830 break;
831 b_slow_realign(res, trash.area, b_data(res));
832 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200833
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100834 /* Not enough room for headers and at least one data byte, block the
Willy Tarreau4596fe22022-05-17 19:07:51 +0200835 * stream. It is expected that the stream connector layer will subscribe
836 * on SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200837 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100838 if (b_size(&outbuf) <= hsize) {
839 qcs->flags |= QC_SF_BLK_MROOM;
840 goto end;
841 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200842
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200843 if (b_size(&outbuf) < hsize + fsize)
844 fsize = b_size(&outbuf) - hsize;
845 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200846
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200847 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
848 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
849
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200850 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200851 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200852 count -= fsize;
853
854 if (fsize == bsize)
855 htx_remove_blk(htx, blk);
856 else
857 htx_cut_data_blk(htx, blk, fsize);
858
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200859 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200860 b_add(res, b_data(&outbuf));
861 goto new_frame;
862
863 end:
864 return total;
865}
866
Willy Tarreau3215e732022-05-27 10:09:11 +0200867size_t h3_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200868{
869 size_t total = 0;
Willy Tarreau3215e732022-05-27 10:09:11 +0200870 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200871 struct htx *htx;
872 enum htx_blk_type btype;
873 struct htx_blk *blk;
874 uint32_t bsize;
875 int32_t idx;
876 int ret;
877
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100878 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100879
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200880 htx = htx_from_buf(buf);
881
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100882 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200883 idx = htx_get_head(htx);
884 blk = htx_get_blk(htx, idx);
885 btype = htx_get_blk_type(blk);
886 bsize = htx_get_blksz(blk);
887
888 /* Not implemented : QUIC on backend side */
889 BUG_ON(btype == HTX_BLK_REQ_SL);
890
891 switch (btype) {
892 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200893 /* start-line -> HEADERS h3 frame */
894 ret = h3_resp_headers_send(qcs, htx);
895 if (ret > 0) {
896 total += ret;
897 count -= ret;
898 if (ret < bsize)
899 goto out;
900 }
901 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200902
903 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200904 ret = h3_resp_data_send(qcs, buf, count);
905 if (ret > 0) {
906 htx = htx_from_buf(buf);
907 total += ret;
908 count -= ret;
909 if (ret < bsize)
910 goto out;
911 }
912 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200913
914 case HTX_BLK_TLR:
915 case HTX_BLK_EOT:
916 /* TODO trailers */
917
918 default:
919 htx_remove_blk(htx, blk);
920 total += bsize;
921 count -= bsize;
922 break;
923 }
924 }
925
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100926 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
927 qcs->flags |= QC_SF_FIN_STREAM;
928
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200929 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200930 if (total) {
931 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
932 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
933 }
934
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200935 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200936}
937
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200938static int h3_attach(struct qcs *qcs)
939{
940 struct h3s *h3s;
941
942 h3s = pool_alloc(pool_head_h3s);
943 if (!h3s)
944 return 1;
945
946 qcs->ctx = h3s;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200947 h3s->demux_frame_len = 0;
948 h3s->demux_frame_type = 0;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200949 h3s->flags = 0;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200950
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200951 if (quic_stream_is_bidi(qcs->id)) {
952 h3s->type = H3S_T_REQ;
953 }
954 else {
955 /* stream type must be decoded for unidirectional streams */
956 h3s->type = H3S_T_UNKNOWN;
957 }
958
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200959 return 0;
960}
961
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200962static void h3_detach(struct qcs *qcs)
963{
964 struct h3s *h3s = qcs->ctx;
965 pool_free(pool_head_h3s, h3s);
966 qcs->ctx = NULL;
967}
968
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100969static int h3_finalize(void *ctx)
970{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200971 struct h3c *h3c = ctx;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200972 struct qcs *qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100973
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200974 qcs = qcs_new(h3c->qcc, 0x3, QCS_SRV_UNI);
975 if (!qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100976 return 0;
977
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200978 h3_control_send(qcs, h3c);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100979
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100980 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100981}
982
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100983/* Initialize the HTTP/3 context for <qcc> mux.
984 * Return 1 if succeeded, 0 if not.
985 */
986static int h3_init(struct qcc *qcc)
987{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200988 struct h3c *h3c;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200989 struct quic_conn *qc = qcc->conn->handle.qc;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100990
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200991 h3c = pool_alloc(pool_head_h3c);
992 if (!h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100993 goto fail_no_h3;
994
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200995 h3c->qcc = qcc;
996 h3c->err = H3_NO_ERROR;
997 h3c->flags = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100998
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200999 qcc->ctx = h3c;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001000 h3c->prx_counters =
1001 EXTRA_COUNTERS_GET(qc->li->bind_conf->frontend->extra_counters_fe,
1002 &h3_stats_module);
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001003 LIST_INIT(&h3c->buf_wait.list);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001004
1005 return 1;
1006
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001007 fail_no_h3:
1008 return 0;
1009}
1010
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001011static void h3_release(void *ctx)
1012{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001013 struct h3c *h3c = ctx;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001014 pool_free(pool_head_h3c, h3c);
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001015}
1016
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001017/* Check if the H3 connection can still be considered as active.
1018 *
1019 * Return true if active else false.
1020 */
1021static int h3_is_active(const struct qcc *qcc, void *ctx)
1022{
1023 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
1024 return 1;
1025
1026 return 0;
1027}
1028
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001029/* Increment the h3 error code counters for <error_code> value */
1030static void h3_stats_inc_err_cnt(void *ctx, int err_code)
1031{
1032 struct h3c *h3c = ctx;
1033
1034 h3_inc_err_cnt(h3c->prx_counters, err_code);
1035}
1036
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001037/* HTTP/3 application layer operations */
1038const struct qcc_app_ops h3_ops = {
1039 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001040 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001041 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +01001042 .snd_buf = h3_snd_buf,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001043 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001044 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001045 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001046 .release = h3_release,
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001047 .inc_err_cnt = h3_stats_inc_err_cnt,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001048};