blob: 13dbc70353d1e87cb33879382e2f4a234c0392f9 [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>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010021#include <haproxy/conn_stream.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010022#include <haproxy/dynbuf.h>
23#include <haproxy/h3.h>
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +020024#include <haproxy/http.h>
25#include <haproxy/htx.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010026#include <haproxy/istbuf.h>
Amaury Denoyelle846cc042022-04-04 16:13:44 +020027#include <haproxy/mux_quic.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020028#include <haproxy/ncbuf.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010029#include <haproxy/pool.h>
30#include <haproxy/qpack-dec.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020031#include <haproxy/qpack-enc.h>
32#include <haproxy/quic_enc.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010033#include <haproxy/tools.h>
34#include <haproxy/xprt_quic.h>
35
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010036#if defined(DEBUG_H3)
37#define h3_debug_printf fprintf
38#define h3_debug_hexdump debug_hexdump
39#else
40#define h3_debug_printf(...) do { } while (0)
41#define h3_debug_hexdump(...) do { } while (0)
42#endif
43
44#define H3_CF_SETTINGS_SENT 0x00000001
45
46/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +020047static uint64_t h3_settings_qpack_max_table_capacity = 0;
48static uint64_t h3_settings_qpack_blocked_streams = 4096;
49static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010050
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +020051struct h3c {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010052 struct qcc *qcc;
53 enum h3_err err;
54 uint32_t flags;
55 /* Locally initiated uni-streams */
56 struct h3_uqs lqpack_enc;
57 struct h3_uqs lqpack_dec;
58 struct h3_uqs lctrl;
59 /* Remotely initiated uni-streams */
60 struct h3_uqs rqpack_enc;
61 struct h3_uqs rqpack_dec;
62 struct h3_uqs rctrl;
63 /* Settings */
64 uint64_t qpack_max_table_capacity;
65 uint64_t qpack_blocked_streams;
66 uint64_t max_field_section_size;
67 struct buffer_wait buf_wait; /* wait list for buffer allocations */
68};
69
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +020070DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010071
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020072struct h3s {
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +020073 int demux_frame_len;
74 int demux_frame_type;
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020075};
76
77DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s));
78
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010079/* Simple function to duplicate a buffer */
Amaury Denoyellec7dd9d62022-05-24 18:14:28 +020080static inline struct buffer h3_b_dup(const struct ncbuf *b)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010081{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020082 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010083}
84
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010085/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
86 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
87 * Note that this function update <b> buffer to reflect the number of bytes consumed
88 * to decode the h3 frame header.
89 */
90static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
91 struct buffer *b)
92{
93 size_t hlen;
94
95 hlen = 0;
96 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
97 return 0;
98
99 return hlen;
100}
101
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100102/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
103 * in a local HTX buffer and transfer to the conn-stream layer. <fin> must be
104 * set if this is the last data to transfer from this stream.
105 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200106 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100107 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200108static int h3_headers_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100109 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100110{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100111 struct buffer htx_buf = BUF_NULL;
112 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100113 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200114 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200115 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200116 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100117 struct ist meth = IST_NULL, path = IST_NULL;
118 //struct ist scheme = IST_NULL, authority = IST_NULL;
119 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200120 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100121
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200122 /* TODO support buffer wrapping */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200123 BUG_ON(ncb_head(buf) + len >= ncb_wrap(buf));
124 if (qpack_decode_fs((const unsigned char *)ncb_head(buf), len, tmp, list) < 0)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200125 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100126
127 qc_get_buf(qcs, &htx_buf);
128 BUG_ON(!b_size(&htx_buf));
129 htx = htx_from_buf(&htx_buf);
130
131 /* first treat pseudo-header to build the start line */
132 hdr_idx = 0;
133 while (1) {
134 if (isteq(list[hdr_idx].n, ist("")))
135 break;
136
137 if (istmatch(list[hdr_idx].n, ist(":"))) {
138 /* pseudo-header */
139 if (isteq(list[hdr_idx].n, ist(":method")))
140 meth = list[hdr_idx].v;
141 else if (isteq(list[hdr_idx].n, ist(":path")))
142 path = list[hdr_idx].v;
143 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
144 // scheme = list[hdr_idx].v;
145 else if (isteq(list[hdr_idx].n, ist(":authority")))
146 authority = list[hdr_idx].v;
147 }
148
149 ++hdr_idx;
150 }
151
152 flags |= HTX_SL_F_VER_11;
Amaury Denoyelle0fa14a62022-04-26 16:24:39 +0200153 flags |= HTX_SL_F_XFER_LEN;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100154
155 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
156 if (!sl)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200157 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100158
159 if (fin)
160 sl->flags |= HTX_SL_F_BODYLESS;
161
162 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
163 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
164
165 if (isttest(authority))
166 htx_add_header(htx, ist("host"), authority);
167
168 /* now treat standard headers */
169 hdr_idx = 0;
170 while (1) {
171 if (isteq(list[hdr_idx].n, ist("")))
172 break;
173
174 if (!istmatch(list[hdr_idx].n, ist(":")))
175 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
176
177 ++hdr_idx;
178 }
179
180 htx_add_endof(htx, HTX_BLK_EOH);
181 htx_to_buf(htx, &htx_buf);
182
183 if (fin)
184 htx->flags |= HTX_FL_EOM;
185
Willy Tarreau01c2a4a2022-05-10 15:46:10 +0200186 if (!qc_attach_cs(qcs, &htx_buf))
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200187 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100188
189 /* buffer is transferred to conn_stream and set to NULL
190 * except on stream creation error.
191 */
192 b_free(&htx_buf);
193 offer_buffers(NULL, 1);
194
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200195 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100196}
197
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100198/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
199 * HTX buffer. <fin> must be set if this is the last data to transfer from this
200 * stream.
201 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200202 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100203 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200204static int h3_data_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100205 char fin)
206{
207 struct buffer *appbuf;
208 struct htx *htx = NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200209 size_t htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100210 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200211 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100212
213 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
214 BUG_ON(!appbuf);
215 htx = htx_from_buf(appbuf);
216
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200217 if (len > ncb_data(buf, 0)) {
218 len = ncb_data(buf, 0);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200219 fin = 0;
220 }
221
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200222 head = ncb_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200223 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100224 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200225 if (!htx_space) {
226 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200227 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200228 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200229
230 if (len > htx_space) {
231 len = htx_space;
232 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100233 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100234
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200235 if (head + len > ncb_wrap(buf)) {
236 size_t contig = ncb_wrap(buf) - head;
237 htx_sent = htx_add_data(htx, ist2(ncb_head(buf), contig));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200238 if (htx_sent < contig) {
239 qcs->flags |= QC_SF_DEM_FULL;
240 goto out;
241 }
242
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200243 len -= contig;
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200244 head = ncb_orig(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200245 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100246 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100247
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200248 htx_sent += htx_add_data(htx, ist2(head, len));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200249 if (htx_sent < len) {
250 qcs->flags |= QC_SF_DEM_FULL;
251 goto out;
252 }
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200253
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200254 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100255 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100256
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200257 out:
258 htx_to_buf(htx, appbuf);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200259 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100260}
261
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100262/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
263 * that we received the last data of the stream.
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200264 *
265 * Returns 0 on success else non-zero.
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100266 */
267static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
268{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200269 struct ncbuf *rxbuf = &qcs->rx.ncbuf;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200270 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200271 ssize_t ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100272
Amaury Denoyellebb970422022-04-12 16:40:52 +0200273 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200274 if (!ncb_data(rxbuf, 0))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100275 return 0;
276
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200277 while (ncb_data(rxbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100278 uint64_t ftype, flen;
279 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100280 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100281
282 /* Work on a copy of <rxbuf> */
283 b = h3_b_dup(rxbuf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200284 if (!h3s->demux_frame_len) {
285 size_t hlen = h3_decode_frm_header(&ftype, &flen, &b);
286 if (!hlen)
287 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100288
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200289 h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n",
290 __func__, ftype, flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100291
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200292 h3s->demux_frame_type = ftype;
293 h3s->demux_frame_len = flen;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200294 qcs_consume(qcs, hlen);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200295 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100296
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200297 flen = h3s->demux_frame_len;
298 ftype = h3s->demux_frame_type;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200299
300 /* Do not demux incomplete frames except H3 DATA which can be
301 * fragmented in multiple HTX blocks.
302 */
303 if (flen > b_data(&b) && ftype != H3_FT_DATA) {
304 /* Reject frames bigger than bufsize.
305 *
306 * TODO HEADERS should in complement be limited with H3
307 * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent
308 * excessive decompressed size.
309 */
310 if (flen > ncb_size(rxbuf)) {
311 qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD);
312 return 1;
313 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200314 break;
Amaury Denoyelleb5454d42022-05-12 16:56:16 +0200315 }
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200316
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200317 last_stream_frame = (fin && flen == ncb_total_data(rxbuf));
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100318
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100319 switch (ftype) {
320 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100321 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
322 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200323 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100324 break;
325 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100326 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
327 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200328 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100329 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100330 case H3_FT_PUSH_PROMISE:
331 /* Not supported */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200332 ret = flen;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100333 break;
334 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100335 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
336 * unknown frame types MUST be ignored
337 */
338 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200339 ret = flen;
340 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100341 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200342
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200343 if (ret) {
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200344 BUG_ON(h3s->demux_frame_len < ret);
345 h3s->demux_frame_len -= ret;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200346 qcs_consume(qcs, ret);
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200347 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100348 }
349
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +0200350 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
351 * However, currently, io-cb of MUX does not handle Rx.
352 */
353
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100354 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100355}
356
357/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
358 * <rxbuf> buffer. This function does not update this buffer.
359 * Returns 0 if something wrong happened, 1 if not.
360 */
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200361static int h3_parse_settings_frm(struct h3c *h3c, const struct ncbuf *rxbuf, size_t flen)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100362{
363 uint64_t id, value;
364 const unsigned char *buf, *end;
365
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200366 buf = (const unsigned char *)ncb_head(rxbuf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100367 end = buf + flen;
368
Amaury Denoyelle160507d2022-05-24 16:30:11 +0200369 while (buf < end) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100370 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
371 return 0;
372
373 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
374 __func__, (unsigned long long)id, (unsigned long long)value);
375 switch (id) {
376 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200377 h3c->qpack_max_table_capacity = value;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100378 break;
379 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200380 h3c->max_field_section_size = value;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100381 break;
382 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200383 h3c->qpack_blocked_streams = value;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100384 break;
385 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200386 h3c->err = H3_SETTINGS_ERROR;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100387 return 0;
388 default:
389 /* MUST be ignored */
390 break;
391 }
392 }
393
394 return 1;
395}
396
397/* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as
398 * there is not enough received data.
399 * Returns 0 if something wrong happened, 1 if not.
400 */
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200401static int h3_control_recv(struct qcs *qcs, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100402{
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200403 struct ncbuf *rxbuf = &qcs->rx.ncbuf;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200404 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100405
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200406 h3_debug_printf(stderr, "%s STREAM ID: %lu\n", __func__, qcs->id);
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200407 if (!ncb_data(rxbuf, 0))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100408 return 1;
409
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200410 while (ncb_data(rxbuf, 0)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100411 size_t hlen;
412 uint64_t ftype, flen;
413 struct buffer b;
414
415 /* Work on a copy of <rxbuf> */
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200416 b = h3_b_dup(rxbuf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100417 hlen = h3_decode_frm_header(&ftype, &flen, &b);
418 if (!hlen)
419 break;
420
421 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
422 (unsigned long long)ftype, (unsigned long long)flen);
423 if (flen > b_data(&b))
424 break;
425
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200426 qcs_consume(qcs, hlen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100427 /* From here, a frame must not be truncated */
428 switch (ftype) {
429 case H3_FT_CANCEL_PUSH:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100430 /* XXX TODO XXX */
431 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100432 break;
433 case H3_FT_SETTINGS:
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200434 if (!h3_parse_settings_frm(h3c, rxbuf, flen))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100435 return 0;
436 break;
437 case H3_FT_GOAWAY:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100438 /* XXX TODO XXX */
439 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100440 break;
441 case H3_FT_MAX_PUSH_ID:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100442 /* XXX TODO XXX */
443 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100444 break;
445 default:
446 /* Error */
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200447 h3c->err = H3_FRAME_UNEXPECTED;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100448 return 0;
449 }
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200450 qcs_consume(qcs, flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100451 }
452
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100453 return 1;
454}
455
Amaury Denoyellea5871362021-10-07 16:26:12 +0200456/* Returns buffer for data sending.
457 * May be NULL if the allocation failed.
458 */
459static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100460{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200461 if (!b_size(&qcs->tx.buf))
462 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100463
Amaury Denoyellea5871362021-10-07 16:26:12 +0200464 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100465}
466
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200467/* Function used to emit stream data from <qcs> control uni-stream */
468static int h3_control_send(struct qcs *qcs, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100469{
470 int ret;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200471 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100472 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200473 struct buffer pos, *res;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200474 size_t frm_len;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200475
476 BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100477
478 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200479 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100480
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200481 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
482 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
483 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
484 quic_int_getsize(h3_settings_qpack_blocked_streams);
485 if (h3_settings_max_field_section_size) {
486 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
487 quic_int_getsize(h3_settings_max_field_section_size);
488 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100489
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200490 b_quic_enc_int(&pos, H3_UNI_S_T_CTRL);
491 /* Build a SETTINGS frame */
492 b_quic_enc_int(&pos, H3_FT_SETTINGS);
493 b_quic_enc_int(&pos, frm_len);
494 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
495 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
496 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
497 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
498 if (h3_settings_max_field_section_size) {
499 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
500 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
501 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200502
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200503 res = mux_get_buf(qcs);
504 if (b_room(res) < b_data(&pos)) {
505 // TODO the mux should be put in blocked state, with
506 // the stream in state waiting for settings to be sent
507 ABORT_NOW();
508 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100509
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200510 ret = b_force_xfer(res, &pos, b_data(&pos));
511 if (ret > 0) {
512 h3c->flags |= H3_CF_SETTINGS_SENT;
513 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
514 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100515 }
516
517 return ret;
518}
519
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200520static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
521{
522 struct buffer outbuf;
523 struct buffer headers_buf = BUF_NULL;
524 struct buffer *res;
525 struct http_hdr list[global.tune.max_http_hdr];
526 struct htx_sl *sl;
527 struct htx_blk *blk;
528 enum htx_blk_type type;
529 int frame_length_size; /* size in bytes of frame length varint field */
530 int ret = 0;
531 int hdr;
532 int status = 0;
533
534 sl = NULL;
535 hdr = 0;
536 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
537 type = htx_get_blk_type(blk);
538
539 if (type == HTX_BLK_UNUSED)
540 continue;
541
542 if (type == HTX_BLK_EOH)
543 break;
544
545 if (type == HTX_BLK_RES_SL) {
546 /* start-line -> HEADERS h3 frame */
547 BUG_ON(sl);
548 sl = htx_get_blk_ptr(htx, blk);
549 /* TODO should be on h3 layer */
550 status = sl->info.res.status;
551 }
552 else if (type == HTX_BLK_HDR) {
553 list[hdr].n = htx_get_blk_name(htx, blk);
554 list[hdr].v = htx_get_blk_value(htx, blk);
555 hdr++;
556 }
557 else {
558 ABORT_NOW();
559 goto err;
560 }
561 }
562
563 BUG_ON(!sl);
564
565 list[hdr].n = ist("");
566
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200567 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200568
569 /* At least 5 bytes to store frame type + length as a varint max size */
570 if (b_room(res) < 5)
571 ABORT_NOW();
572
573 b_reset(&outbuf);
574 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
575 /* Start the headers after frame type + length */
576 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
577
578 if (qpack_encode_field_section_line(&headers_buf))
579 ABORT_NOW();
580 if (qpack_encode_int_status(&headers_buf, status))
581 ABORT_NOW();
582
583 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
584 if (isteq(list[hdr].n, ist("")))
585 break;
586
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100587 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
588 * Transfer codings (see Section 6.1 of [HTTP11]) are not
589 * defined for HTTP/3; the Transfer-Encoding header field MUST
590 * NOT be used.
591 */
592 if (isteq(list[hdr].n, ist("transfer-encoding")))
593 continue;
594
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200595 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
596 ABORT_NOW();
597 }
598
599 /* Now that all headers are encoded, we are certain that res buffer is
600 * big enough
601 */
602 frame_length_size = quic_int_getsize(b_data(&headers_buf));
603 res->head += 4 - frame_length_size;
604 b_putchr(res, 0x01); /* h3 HEADERS frame type */
605 if (!b_quic_enc_int(res, b_data(&headers_buf)))
606 ABORT_NOW();
607 b_add(res, b_data(&headers_buf));
608
609 ret = 0;
610 blk = htx_get_head_blk(htx);
611 while (blk) {
612 type = htx_get_blk_type(blk);
613 ret += htx_get_blksz(blk);
614 blk = htx_remove_blk(htx, blk);
615 if (type == HTX_BLK_EOH)
616 break;
617 }
618
619 return ret;
620
621 err:
622 return 0;
623}
624
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200625/* Returns the total of bytes sent. */
626static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
627{
628 struct buffer outbuf;
629 struct buffer *res;
630 size_t total = 0;
631 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200632 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200633 struct htx_blk *blk;
634 enum htx_blk_type type;
635
636 htx = htx_from_buf(buf);
637
638 new_frame:
639 if (!count || htx_is_empty(htx))
640 goto end;
641
642 blk = htx_get_head_blk(htx);
643 type = htx_get_blk_type(blk);
644 fsize = bsize = htx_get_blksz(blk);
645
646 if (type != HTX_BLK_DATA)
647 goto end;
648
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200649 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200650
651 if (fsize > count)
652 fsize = count;
653
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200654 /* h3 DATA headers : 1-byte frame type + varint frame length */
655 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200656
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200657 while (1) {
658 b_reset(&outbuf);
659 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
660 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
661 break;
662 b_slow_realign(res, trash.area, b_data(res));
663 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200664
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100665 /* Not enough room for headers and at least one data byte, block the
666 * stream. It is expected that the conn-stream layer will subscribe on
667 * SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200668 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100669 if (b_size(&outbuf) <= hsize) {
670 qcs->flags |= QC_SF_BLK_MROOM;
671 goto end;
672 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200673
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200674 if (b_size(&outbuf) < hsize + fsize)
675 fsize = b_size(&outbuf) - hsize;
676 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200677
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200678 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
679 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
680
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200681 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200682 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200683 count -= fsize;
684
685 if (fsize == bsize)
686 htx_remove_blk(htx, blk);
687 else
688 htx_cut_data_blk(htx, blk, fsize);
689
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200690 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200691 b_add(res, b_data(&outbuf));
692 goto new_frame;
693
694 end:
695 return total;
696}
697
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200698size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
699{
700 size_t total = 0;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100701 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200702 struct htx *htx;
703 enum htx_blk_type btype;
704 struct htx_blk *blk;
705 uint32_t bsize;
706 int32_t idx;
707 int ret;
708
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100709 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100710
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200711 htx = htx_from_buf(buf);
712
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100713 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200714 idx = htx_get_head(htx);
715 blk = htx_get_blk(htx, idx);
716 btype = htx_get_blk_type(blk);
717 bsize = htx_get_blksz(blk);
718
719 /* Not implemented : QUIC on backend side */
720 BUG_ON(btype == HTX_BLK_REQ_SL);
721
722 switch (btype) {
723 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200724 /* start-line -> HEADERS h3 frame */
725 ret = h3_resp_headers_send(qcs, htx);
726 if (ret > 0) {
727 total += ret;
728 count -= ret;
729 if (ret < bsize)
730 goto out;
731 }
732 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200733
734 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200735 ret = h3_resp_data_send(qcs, buf, count);
736 if (ret > 0) {
737 htx = htx_from_buf(buf);
738 total += ret;
739 count -= ret;
740 if (ret < bsize)
741 goto out;
742 }
743 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200744
745 case HTX_BLK_TLR:
746 case HTX_BLK_EOT:
747 /* TODO trailers */
748
749 default:
750 htx_remove_blk(htx, blk);
751 total += bsize;
752 count -= bsize;
753 break;
754 }
755 }
756
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100757 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
758 qcs->flags |= QC_SF_FIN_STREAM;
759
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200760 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200761 if (total) {
762 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
763 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
764 }
765
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200766 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200767}
768
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200769static int h3_attach(struct qcs *qcs)
770{
771 struct h3s *h3s;
772
773 h3s = pool_alloc(pool_head_h3s);
774 if (!h3s)
775 return 1;
776
777 qcs->ctx = h3s;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200778 h3s->demux_frame_len = 0;
779 h3s->demux_frame_type = 0;
780
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200781 return 0;
782}
783
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100784/* Finalize the initialization of remotely initiated uni-stream <qcs>.
785 * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error
786 * to inform the QUIC mux layer of the encountered error.
787 */
788static int h3_attach_ruqs(struct qcs *qcs, void *ctx)
789{
790 uint64_t strm_type;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200791 struct h3c *h3c = ctx;
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200792 struct ncbuf *rxbuf = &qcs->rx.ncbuf;
793 struct buffer b;
794 size_t len = 0;
795
796 b = h3_b_dup(rxbuf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100797
798 /* First octets: the uni-stream type */
Amaury Denoyelle081479d2022-05-23 14:35:15 +0200799 if (!b_quic_dec_int(&strm_type, &b, &len) || strm_type > H3_UNI_S_T_MAX)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100800 return 0;
801
Amaury Denoyellea9773552022-05-16 14:38:25 +0200802 qcs_consume(qcs, len);
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200803
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100804 /* Note that for all the uni-streams below, this is an error to receive two times the
805 * same type of uni-stream (even for Push stream which is not supported at this time.
806 */
807 switch (strm_type) {
Amaury Denoyelle081479d2022-05-23 14:35:15 +0200808 case H3_UNI_S_T_CTRL:
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200809 if (h3c->rctrl.qcs) {
810 h3c->err = H3_STREAM_CREATION_ERROR;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100811 return 0;
812 }
813
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200814 h3c->rctrl.qcs = qcs;
815 h3c->rctrl.cb = h3_control_recv;
816 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3c->rctrl.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100817 break;
Amaury Denoyelle081479d2022-05-23 14:35:15 +0200818 case H3_UNI_S_T_PUSH:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100819 /* NOT SUPPORTED */
820 break;
Amaury Denoyelle081479d2022-05-23 14:35:15 +0200821 case H3_UNI_S_T_QPACK_ENC:
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200822 if (h3c->rqpack_enc.qcs) {
823 h3c->err = H3_STREAM_CREATION_ERROR;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100824 return 0;
825 }
826
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200827 h3c->rqpack_enc.qcs = qcs;
828 h3c->rqpack_enc.cb = qpack_decode_enc;
829 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3c->rqpack_enc.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100830 break;
Amaury Denoyelle081479d2022-05-23 14:35:15 +0200831 case H3_UNI_S_T_QPACK_DEC:
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200832 if (h3c->rqpack_dec.qcs) {
833 h3c->err = H3_STREAM_CREATION_ERROR;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100834 return 0;
835 }
836
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200837 h3c->rqpack_dec.qcs = qcs;
838 h3c->rqpack_dec.cb = qpack_decode_dec;
839 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3c->rqpack_dec.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100840 break;
841 default:
842 /* Error */
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200843 h3c->err = H3_STREAM_CREATION_ERROR;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100844 return 0;
845 }
846
847 return 1;
848}
849
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200850static void h3_detach(struct qcs *qcs)
851{
852 struct h3s *h3s = qcs->ctx;
853 pool_free(pool_head_h3s, h3s);
854 qcs->ctx = NULL;
855}
856
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100857static int h3_finalize(void *ctx)
858{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200859 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100860
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200861 h3c->lctrl.qcs = qcs_new(h3c->qcc, 0x3, QCS_SRV_UNI);
862 if (!h3c->lctrl.qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100863 return 0;
864
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200865 h3_control_send(h3c->lctrl.qcs, h3c);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100866
867 return 1;
868}
869
870/* Tasklet dedicated to h3 incoming uni-streams */
871static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state)
872{
873 struct h3_uqs *h3_uqs = ctx;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200874 struct h3c *h3c = h3_uqs->qcs->qcc->ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100875
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200876 h3_uqs->cb(h3_uqs->qcs, h3c);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100877 return NULL;
878}
879
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100880/* Release all the tasklet attached to <h3_uqs> uni-stream */
881static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs)
882{
883 struct tasklet *t = h3_uqs->wait_event.tasklet;
884
885 if (t)
886 tasklet_free(t);
887}
888
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200889/* Release all the tasklet attached to <h3c> uni-streams */
890static void h3_uqs_tasklets_release(struct h3c *h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100891{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200892 h3_uqs_tasklet_release(&h3c->rqpack_enc);
893 h3_uqs_tasklet_release(&h3c->rqpack_dec);
894 h3_uqs_tasklet_release(&h3c->rctrl);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100895}
896
897/* Tasklet dedicated to h3 outgoing uni-streams */
898__maybe_unused
899static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state)
900{
901 struct h3_uqs *h3_uqs = ctx;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200902 struct h3c *h3c = h3_uqs->qcs->qcc->ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100903
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200904 h3_uqs->cb(h3_uqs->qcs, h3c);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100905 return NULL;
906}
907
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500908/* Initialize <h3_uqs> uni-stream with <t> as tasklet */
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200909static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3c *h3c,
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200910 int (*cb)(struct qcs *qcs, void *ctx),
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100911 struct task *(*t)(struct task *, void *, unsigned int))
912{
913 h3_uqs->qcs = NULL;
914 h3_uqs->cb = cb;
915 h3_uqs->wait_event.tasklet = tasklet_new();
916 if (!h3_uqs->wait_event.tasklet)
917 return 0;
918
919 h3_uqs->wait_event.tasklet->process = t;
920 h3_uqs->wait_event.tasklet->context = h3_uqs;
Frédéric Lécaille000162e2022-04-01 09:04:57 +0200921 h3_uqs->wait_event.events = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100922 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100923}
924
925static inline void h3_uqs_release(struct h3_uqs *h3_uqs)
926{
927 if (h3_uqs->qcs)
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200928 qcs_free(h3_uqs->qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100929}
930
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200931static inline void h3_uqs_release_all(struct h3c *h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100932{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200933 h3_uqs_tasklet_release(&h3c->lctrl);
934 h3_uqs_release(&h3c->lctrl);
935 h3_uqs_tasklet_release(&h3c->lqpack_enc);
936 h3_uqs_release(&h3c->lqpack_enc);
937 h3_uqs_tasklet_release(&h3c->lqpack_dec);
938 h3_uqs_release(&h3c->lqpack_dec);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100939}
940
941/* Initialize the HTTP/3 context for <qcc> mux.
942 * Return 1 if succeeded, 0 if not.
943 */
944static int h3_init(struct qcc *qcc)
945{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200946 struct h3c *h3c;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100947
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200948 h3c = pool_alloc(pool_head_h3c);
949 if (!h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100950 goto fail_no_h3;
951
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200952 h3c->qcc = qcc;
953 h3c->err = H3_NO_ERROR;
954 h3c->flags = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100955
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200956 if (!h3_uqs_init(&h3c->rqpack_enc, h3c, NULL, h3_uqs_task) ||
957 !h3_uqs_init(&h3c->rqpack_dec, h3c, NULL, h3_uqs_task) ||
958 !h3_uqs_init(&h3c->rctrl, h3c, h3_control_recv, h3_uqs_task))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100959 goto fail_no_h3_ruqs;
960
Amaury Denoyellec6195d72022-05-23 11:39:14 +0200961 if (!h3_uqs_init(&h3c->lctrl, h3c, NULL, h3_uqs_task) ||
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200962 !h3_uqs_init(&h3c->lqpack_enc, h3c, NULL, h3_uqs_task) ||
963 !h3_uqs_init(&h3c->lqpack_dec, h3c, NULL, h3_uqs_task))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100964 goto fail_no_h3_luqs;
965
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200966 qcc->ctx = h3c;
967 LIST_INIT(&h3c->buf_wait.list);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100968
969 return 1;
970
971 fail_no_h3_ruqs:
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200972 h3_uqs_release_all(h3c);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100973 fail_no_h3_luqs:
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200974 h3_uqs_tasklets_release(h3c);
975 pool_free(pool_head_h3c, h3c);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100976 fail_no_h3:
977 return 0;
978}
979
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200980static void h3_release(void *ctx)
981{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200982 struct h3c *h3c = ctx;
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200983
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200984 h3_uqs_release_all(h3c);
985 h3_uqs_tasklets_release(h3c);
986 pool_free(pool_head_h3c, h3c);
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200987}
988
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200989/* Check if the H3 connection can still be considered as active.
990 *
991 * Return true if active else false.
992 */
993static int h3_is_active(const struct qcc *qcc, void *ctx)
994{
995 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
996 return 1;
997
998 return 0;
999}
1000
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001001/* HTTP/3 application layer operations */
1002const struct qcc_app_ops h3_ops = {
1003 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001004 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001005 .attach_ruqs = h3_attach_ruqs,
1006 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +01001007 .snd_buf = h3_snd_buf,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001008 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001009 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001010 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001011 .release = h3_release,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001012};