blob: 61ea09a722dd091075cfc640bb7b4b4236b38cc2 [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>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010028#include <haproxy/pool.h>
29#include <haproxy/qpack-dec.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020030#include <haproxy/qpack-enc.h>
31#include <haproxy/quic_enc.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010032#include <haproxy/tools.h>
33#include <haproxy/xprt_quic.h>
34
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010035#if defined(DEBUG_H3)
36#define h3_debug_printf fprintf
37#define h3_debug_hexdump debug_hexdump
38#else
39#define h3_debug_printf(...) do { } while (0)
40#define h3_debug_hexdump(...) do { } while (0)
41#endif
42
43#define H3_CF_SETTINGS_SENT 0x00000001
44
45/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +020046static uint64_t h3_settings_qpack_max_table_capacity = 0;
47static uint64_t h3_settings_qpack_blocked_streams = 4096;
48static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010049
50struct h3 {
51 struct qcc *qcc;
52 enum h3_err err;
53 uint32_t flags;
54 /* Locally initiated uni-streams */
55 struct h3_uqs lqpack_enc;
56 struct h3_uqs lqpack_dec;
57 struct h3_uqs lctrl;
58 /* Remotely initiated uni-streams */
59 struct h3_uqs rqpack_enc;
60 struct h3_uqs rqpack_dec;
61 struct h3_uqs rctrl;
62 /* Settings */
63 uint64_t qpack_max_table_capacity;
64 uint64_t qpack_blocked_streams;
65 uint64_t max_field_section_size;
66 struct buffer_wait buf_wait; /* wait list for buffer allocations */
67};
68
69DECLARE_STATIC_POOL(pool_head_h3, "h3", sizeof(struct h3));
70
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020071struct h3s {
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +020072 int demux_frame_len;
73 int demux_frame_type;
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020074};
75
76DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s));
77
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010078/* Simple function to duplicate a buffer */
79static inline struct buffer h3_b_dup(struct buffer *b)
80{
81 return b_make(b->area, b->size, b->head, b->data);
82}
83
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010084/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
85 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
86 * Note that this function update <b> buffer to reflect the number of bytes consumed
87 * to decode the h3 frame header.
88 */
89static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
90 struct buffer *b)
91{
92 size_t hlen;
93
94 hlen = 0;
95 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
96 return 0;
97
98 return hlen;
99}
100
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100101/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
102 * in a local HTX buffer and transfer to the conn-stream layer. <fin> must be
103 * set if this is the last data to transfer from this stream.
104 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200105 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100106 */
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100107static int h3_headers_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
108 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100109{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100110 struct buffer htx_buf = BUF_NULL;
111 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100112 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200113 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200114 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100115 struct conn_stream *cs;
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 */
123 BUG_ON(b_contig_data(buf, 0) != b_data(buf));
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100124 if (qpack_decode_fs((const unsigned char *)b_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
Amaury Denoyelle846cc042022-04-04 16:13:44 +0200186 cs = qc_attach_cs(qcs, &htx_buf);
Frédéric Lécaille59509b52022-02-15 09:25:06 +0100187 if (!cs)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200188 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100189
190 /* buffer is transferred to conn_stream and set to NULL
191 * except on stream creation error.
192 */
193 b_free(&htx_buf);
194 offer_buffers(NULL, 1);
195
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200196 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100197}
198
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100199/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
200 * HTX buffer. <fin> must be set if this is the last data to transfer from this
201 * stream.
202 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200203 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100204 */
205static int h3_data_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
206 char fin)
207{
208 struct buffer *appbuf;
209 struct htx *htx = NULL;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200210 size_t contig = 0, htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100211 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200212 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100213
214 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
215 BUG_ON(!appbuf);
216 htx = htx_from_buf(appbuf);
217
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200218 if (len > b_data(buf)) {
219 len = b_data(buf);
220 fin = 0;
221 }
222
223 head = b_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200224 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100225 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200226 if (!htx_space) {
227 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200228 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200229 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200230
231 if (len > htx_space) {
232 len = htx_space;
233 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100234 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100235
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200236 contig = b_contig_data(buf, contig);
237 if (len > contig) {
238 htx_sent = htx_add_data(htx, ist2(b_head(buf), contig));
239 head = b_orig(buf);
240 len -= contig;
241 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100242 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100243
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200244 htx_sent += htx_add_data(htx, ist2(head, len));
245 BUG_ON(htx_sent < len);
246
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200247 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100248 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100249
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200250 out:
251 htx_to_buf(htx, appbuf);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200252 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100253}
254
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100255/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
256 * that we received the last data of the stream.
257 * Returns <0 on error else 0.
258 */
259static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
260{
261 struct buffer *rxbuf = &qcs->rx.buf;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200262 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200263 ssize_t ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100264
Amaury Denoyellebb970422022-04-12 16:40:52 +0200265 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100266 if (!b_data(rxbuf))
267 return 0;
268
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200269 while (b_data(rxbuf) && !(qcs->flags & QC_SF_DEM_FULL)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100270 uint64_t ftype, flen;
271 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100272 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100273
274 /* Work on a copy of <rxbuf> */
275 b = h3_b_dup(rxbuf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200276 if (!h3s->demux_frame_len) {
277 size_t hlen = h3_decode_frm_header(&ftype, &flen, &b);
278 if (!hlen)
279 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100280
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200281 h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n",
282 __func__, ftype, flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100283
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200284 b_del(rxbuf, hlen);
285 h3s->demux_frame_type = ftype;
286 h3s->demux_frame_len = flen;
287 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100288
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200289 flen = h3s->demux_frame_len;
290 ftype = h3s->demux_frame_type;
291 if (flen > b_data(&b) && !b_full(rxbuf))
292 break;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100293 last_stream_frame = (fin && flen == b_data(rxbuf));
294
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100295 switch (ftype) {
296 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100297 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
298 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200299 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100300 break;
301 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100302 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
303 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200304 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100305 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100306 case H3_FT_PUSH_PROMISE:
307 /* Not supported */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200308 ret = MIN(b_data(rxbuf), flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100309 break;
310 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100311 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
312 * unknown frame types MUST be ignored
313 */
314 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200315 ret = MIN(b_data(rxbuf), flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100316 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200317
318 if (!ret)
319 break;
320
321 b_del(rxbuf, ret);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200322 BUG_ON(h3s->demux_frame_len < ret);
323 h3s->demux_frame_len -= ret;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100324 }
325
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +0200326 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
327 * However, currently, io-cb of MUX does not handle Rx.
328 */
329
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100330 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100331}
332
333/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
334 * <rxbuf> buffer. This function does not update this buffer.
335 * Returns 0 if something wrong happened, 1 if not.
336 */
337static int h3_parse_settings_frm(struct h3 *h3, const struct buffer *rxbuf, size_t flen)
338{
339 uint64_t id, value;
340 const unsigned char *buf, *end;
341
342 buf = (const unsigned char *)b_head(rxbuf);
343 end = buf + flen;
344
345 while (buf <= end) {
346 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
347 return 0;
348
349 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
350 __func__, (unsigned long long)id, (unsigned long long)value);
351 switch (id) {
352 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
353 h3->qpack_max_table_capacity = value;
354 break;
355 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
356 h3->max_field_section_size = value;
357 break;
358 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
359 h3->qpack_blocked_streams = value;
360 break;
361 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
362 h3->err = H3_SETTINGS_ERROR;
363 return 0;
364 default:
365 /* MUST be ignored */
366 break;
367 }
368 }
369
370 return 1;
371}
372
373/* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as
374 * there is not enough received data.
375 * Returns 0 if something wrong happened, 1 if not.
376 */
377static int h3_control_recv(struct h3_uqs *h3_uqs, void *ctx)
378{
379 struct buffer *rxbuf = &h3_uqs->qcs->rx.buf;
380 struct h3 *h3 = ctx;
381
Amaury Denoyellebb970422022-04-12 16:40:52 +0200382 h3_debug_printf(stderr, "%s STREAM ID: %lu\n", __func__, h3_uqs->qcs->id);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100383 if (!b_data(rxbuf))
384 return 1;
385
386 while (b_data(rxbuf)) {
387 size_t hlen;
388 uint64_t ftype, flen;
389 struct buffer b;
390
391 /* Work on a copy of <rxbuf> */
392 b = h3_b_dup(rxbuf);
393 hlen = h3_decode_frm_header(&ftype, &flen, &b);
394 if (!hlen)
395 break;
396
397 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
398 (unsigned long long)ftype, (unsigned long long)flen);
399 if (flen > b_data(&b))
400 break;
401
402 b_del(rxbuf, hlen);
403 /* From here, a frame must not be truncated */
404 switch (ftype) {
405 case H3_FT_CANCEL_PUSH:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100406 /* XXX TODO XXX */
407 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100408 break;
409 case H3_FT_SETTINGS:
410 if (!h3_parse_settings_frm(h3, rxbuf, flen))
411 return 0;
412 break;
413 case H3_FT_GOAWAY:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100414 /* XXX TODO XXX */
415 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100416 break;
417 case H3_FT_MAX_PUSH_ID:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100418 /* XXX TODO XXX */
419 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100420 break;
421 default:
422 /* Error */
423 h3->err = H3_FRAME_UNEXPECTED;
424 return 0;
425 }
426 b_del(rxbuf, flen);
427 }
428
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100429 /* Handle the case where remaining data are present in the buffer. This
430 * can happen if there is an incomplete frame. In this case, subscribe
431 * on the lower layer to restart receive operation.
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100432 */
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100433 if (b_data(rxbuf))
434 qcs_subscribe(h3_uqs->qcs, SUB_RETRY_RECV, &h3_uqs->wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100435
436 return 1;
437}
438
Amaury Denoyellea5871362021-10-07 16:26:12 +0200439/* Returns buffer for data sending.
440 * May be NULL if the allocation failed.
441 */
442static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100443{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200444 if (!b_size(&qcs->tx.buf))
445 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100446
Amaury Denoyellea5871362021-10-07 16:26:12 +0200447 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100448}
449
450/* Function used to emit stream data from <h3_uqs> control uni-stream */
451static int h3_control_send(struct h3_uqs *h3_uqs, void *ctx)
452{
453 int ret;
454 struct h3 *h3 = ctx;
455 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200456 struct buffer pos, *res;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100457
458 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200459 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100460 if (!(h3->flags & H3_CF_SETTINGS_SENT)) {
461 struct qcs *qcs = h3_uqs->qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100462 size_t frm_len;
463
464 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
465 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
466 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
467 quic_int_getsize(h3_settings_qpack_blocked_streams);
468 if (h3_settings_max_field_section_size) {
469 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
470 quic_int_getsize(h3_settings_max_field_section_size);
471 }
472
Amaury Denoyellea5871362021-10-07 16:26:12 +0200473 b_quic_enc_int(&pos, H3_UNI_STRM_TP_CONTROL_STREAM);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100474 /* Build a SETTINGS frame */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200475 b_quic_enc_int(&pos, H3_FT_SETTINGS);
476 b_quic_enc_int(&pos, frm_len);
477 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
478 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
479 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
480 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100481 if (h3_settings_max_field_section_size) {
Amaury Denoyellea5871362021-10-07 16:26:12 +0200482 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
483 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100484 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200485
486 res = mux_get_buf(qcs);
487 if (b_room(res) < b_data(&pos)) {
488 // TODO the mux should be put in blocked state, with
489 // the stream in state waiting for settings to be sent
490 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100491 }
492
Amaury Denoyellea5871362021-10-07 16:26:12 +0200493 ret = b_force_xfer(res, &pos, b_data(&pos));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100494 if (ret > 0) {
495 h3->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200496 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
497 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100498 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100499 }
500
501 return ret;
502}
503
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200504static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
505{
506 struct buffer outbuf;
507 struct buffer headers_buf = BUF_NULL;
508 struct buffer *res;
509 struct http_hdr list[global.tune.max_http_hdr];
510 struct htx_sl *sl;
511 struct htx_blk *blk;
512 enum htx_blk_type type;
513 int frame_length_size; /* size in bytes of frame length varint field */
514 int ret = 0;
515 int hdr;
516 int status = 0;
517
518 sl = NULL;
519 hdr = 0;
520 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
521 type = htx_get_blk_type(blk);
522
523 if (type == HTX_BLK_UNUSED)
524 continue;
525
526 if (type == HTX_BLK_EOH)
527 break;
528
529 if (type == HTX_BLK_RES_SL) {
530 /* start-line -> HEADERS h3 frame */
531 BUG_ON(sl);
532 sl = htx_get_blk_ptr(htx, blk);
533 /* TODO should be on h3 layer */
534 status = sl->info.res.status;
535 }
536 else if (type == HTX_BLK_HDR) {
537 list[hdr].n = htx_get_blk_name(htx, blk);
538 list[hdr].v = htx_get_blk_value(htx, blk);
539 hdr++;
540 }
541 else {
542 ABORT_NOW();
543 goto err;
544 }
545 }
546
547 BUG_ON(!sl);
548
549 list[hdr].n = ist("");
550
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200551 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200552
553 /* At least 5 bytes to store frame type + length as a varint max size */
554 if (b_room(res) < 5)
555 ABORT_NOW();
556
557 b_reset(&outbuf);
558 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
559 /* Start the headers after frame type + length */
560 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
561
562 if (qpack_encode_field_section_line(&headers_buf))
563 ABORT_NOW();
564 if (qpack_encode_int_status(&headers_buf, status))
565 ABORT_NOW();
566
567 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
568 if (isteq(list[hdr].n, ist("")))
569 break;
570
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100571 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
572 * Transfer codings (see Section 6.1 of [HTTP11]) are not
573 * defined for HTTP/3; the Transfer-Encoding header field MUST
574 * NOT be used.
575 */
576 if (isteq(list[hdr].n, ist("transfer-encoding")))
577 continue;
578
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200579 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
580 ABORT_NOW();
581 }
582
583 /* Now that all headers are encoded, we are certain that res buffer is
584 * big enough
585 */
586 frame_length_size = quic_int_getsize(b_data(&headers_buf));
587 res->head += 4 - frame_length_size;
588 b_putchr(res, 0x01); /* h3 HEADERS frame type */
589 if (!b_quic_enc_int(res, b_data(&headers_buf)))
590 ABORT_NOW();
591 b_add(res, b_data(&headers_buf));
592
593 ret = 0;
594 blk = htx_get_head_blk(htx);
595 while (blk) {
596 type = htx_get_blk_type(blk);
597 ret += htx_get_blksz(blk);
598 blk = htx_remove_blk(htx, blk);
599 if (type == HTX_BLK_EOH)
600 break;
601 }
602
603 return ret;
604
605 err:
606 return 0;
607}
608
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200609/* Returns the total of bytes sent. */
610static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
611{
612 struct buffer outbuf;
613 struct buffer *res;
614 size_t total = 0;
615 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200616 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200617 struct htx_blk *blk;
618 enum htx_blk_type type;
619
620 htx = htx_from_buf(buf);
621
622 new_frame:
623 if (!count || htx_is_empty(htx))
624 goto end;
625
626 blk = htx_get_head_blk(htx);
627 type = htx_get_blk_type(blk);
628 fsize = bsize = htx_get_blksz(blk);
629
630 if (type != HTX_BLK_DATA)
631 goto end;
632
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200633 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200634
635 if (fsize > count)
636 fsize = count;
637
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200638 /* h3 DATA headers : 1-byte frame type + varint frame length */
639 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200640
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200641 while (1) {
642 b_reset(&outbuf);
643 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
644 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
645 break;
646 b_slow_realign(res, trash.area, b_data(res));
647 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200648
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100649 /* Not enough room for headers and at least one data byte, block the
650 * stream. It is expected that the conn-stream layer will subscribe on
651 * SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200652 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100653 if (b_size(&outbuf) <= hsize) {
654 qcs->flags |= QC_SF_BLK_MROOM;
655 goto end;
656 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200657
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200658 if (b_size(&outbuf) < hsize + fsize)
659 fsize = b_size(&outbuf) - hsize;
660 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200661
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200662 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
663 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
664
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200665 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200666 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200667 count -= fsize;
668
669 if (fsize == bsize)
670 htx_remove_blk(htx, blk);
671 else
672 htx_cut_data_blk(htx, blk, fsize);
673
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200674 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200675 b_add(res, b_data(&outbuf));
676 goto new_frame;
677
678 end:
679 return total;
680}
681
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200682size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
683{
684 size_t total = 0;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100685 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200686 struct htx *htx;
687 enum htx_blk_type btype;
688 struct htx_blk *blk;
689 uint32_t bsize;
690 int32_t idx;
691 int ret;
692
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100693 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100694
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200695 htx = htx_from_buf(buf);
696
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100697 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200698 idx = htx_get_head(htx);
699 blk = htx_get_blk(htx, idx);
700 btype = htx_get_blk_type(blk);
701 bsize = htx_get_blksz(blk);
702
703 /* Not implemented : QUIC on backend side */
704 BUG_ON(btype == HTX_BLK_REQ_SL);
705
706 switch (btype) {
707 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200708 /* start-line -> HEADERS h3 frame */
709 ret = h3_resp_headers_send(qcs, htx);
710 if (ret > 0) {
711 total += ret;
712 count -= ret;
713 if (ret < bsize)
714 goto out;
715 }
716 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200717
718 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200719 ret = h3_resp_data_send(qcs, buf, count);
720 if (ret > 0) {
721 htx = htx_from_buf(buf);
722 total += ret;
723 count -= ret;
724 if (ret < bsize)
725 goto out;
726 }
727 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200728
729 case HTX_BLK_TLR:
730 case HTX_BLK_EOT:
731 /* TODO trailers */
732
733 default:
734 htx_remove_blk(htx, blk);
735 total += bsize;
736 count -= bsize;
737 break;
738 }
739 }
740
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100741 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
742 qcs->flags |= QC_SF_FIN_STREAM;
743
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200744 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200745 if (total) {
746 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
747 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
748 }
749
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200750 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200751}
752
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200753static int h3_attach(struct qcs *qcs)
754{
755 struct h3s *h3s;
756
757 h3s = pool_alloc(pool_head_h3s);
758 if (!h3s)
759 return 1;
760
761 qcs->ctx = h3s;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200762 h3s->demux_frame_len = 0;
763 h3s->demux_frame_type = 0;
764
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200765 return 0;
766}
767
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100768/* Finalize the initialization of remotely initiated uni-stream <qcs>.
769 * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error
770 * to inform the QUIC mux layer of the encountered error.
771 */
772static int h3_attach_ruqs(struct qcs *qcs, void *ctx)
773{
774 uint64_t strm_type;
775 struct h3 *h3 = ctx;
776 struct buffer *rxbuf = &qcs->rx.buf;
777
778 /* First octets: the uni-stream type */
779 if (!b_quic_dec_int(&strm_type, rxbuf, NULL) || strm_type > H3_UNI_STRM_TP_MAX)
780 return 0;
781
782 /* Note that for all the uni-streams below, this is an error to receive two times the
783 * same type of uni-stream (even for Push stream which is not supported at this time.
784 */
785 switch (strm_type) {
786 case H3_UNI_STRM_TP_CONTROL_STREAM:
787 if (h3->rctrl.qcs) {
788 h3->err = H3_STREAM_CREATION_ERROR;
789 return 0;
790 }
791
792 h3->rctrl.qcs = qcs;
793 h3->rctrl.cb = h3_control_recv;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100794 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100795 break;
796 case H3_UNI_STRM_TP_PUSH_STREAM:
797 /* NOT SUPPORTED */
798 break;
799 case H3_UNI_STRM_TP_QPACK_ENCODER:
800 if (h3->rqpack_enc.qcs) {
801 h3->err = H3_STREAM_CREATION_ERROR;
802 return 0;
803 }
804
805 h3->rqpack_enc.qcs = qcs;
806 h3->rqpack_enc.cb = qpack_decode_enc;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100807 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_enc.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100808 break;
809 case H3_UNI_STRM_TP_QPACK_DECODER:
810 if (h3->rqpack_dec.qcs) {
811 h3->err = H3_STREAM_CREATION_ERROR;
812 return 0;
813 }
814
815 h3->rqpack_dec.qcs = qcs;
816 h3->rqpack_dec.cb = qpack_decode_dec;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100817 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_dec.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100818 break;
819 default:
820 /* Error */
821 h3->err = H3_STREAM_CREATION_ERROR;
822 return 0;
823 }
824
825 return 1;
826}
827
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200828static void h3_detach(struct qcs *qcs)
829{
830 struct h3s *h3s = qcs->ctx;
831 pool_free(pool_head_h3s, h3s);
832 qcs->ctx = NULL;
833}
834
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100835static int h3_finalize(void *ctx)
836{
837 struct h3 *h3 = ctx;
838
Amaury Denoyelleb7880542022-02-09 10:28:53 +0100839 h3->lctrl.qcs = qcs_new(h3->qcc, 0x3, QCS_SRV_UNI);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100840 if (!h3->lctrl.qcs)
841 return 0;
842
843 /* Wakeup ->lctrl uni-stream */
Frédéric Lécaillee16f0bd2021-08-23 09:50:29 +0200844 h3_control_send(&h3->lctrl, h3);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100845
846 return 1;
847}
848
849/* Tasklet dedicated to h3 incoming uni-streams */
850static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state)
851{
852 struct h3_uqs *h3_uqs = ctx;
853 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
854
855 h3_uqs->cb(h3_uqs, h3);
856 return NULL;
857}
858
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100859/* Release all the tasklet attached to <h3_uqs> uni-stream */
860static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs)
861{
862 struct tasklet *t = h3_uqs->wait_event.tasklet;
863
864 if (t)
865 tasklet_free(t);
866}
867
868/* Release all the tasklet attached to <h3> uni-streams */
869static void h3_uqs_tasklets_release(struct h3 *h3)
870{
871 h3_uqs_tasklet_release(&h3->rqpack_enc);
872 h3_uqs_tasklet_release(&h3->rqpack_dec);
873 h3_uqs_tasklet_release(&h3->rctrl);
874}
875
876/* Tasklet dedicated to h3 outgoing uni-streams */
877__maybe_unused
878static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state)
879{
880 struct h3_uqs *h3_uqs = ctx;
881 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
882
883 h3_uqs->cb(h3_uqs, h3);
884 return NULL;
885}
886
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500887/* Initialize <h3_uqs> uni-stream with <t> as tasklet */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100888static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3 *h3,
889 int (*cb)(struct h3_uqs *h3_uqs, void *ctx),
890 struct task *(*t)(struct task *, void *, unsigned int))
891{
892 h3_uqs->qcs = NULL;
893 h3_uqs->cb = cb;
894 h3_uqs->wait_event.tasklet = tasklet_new();
895 if (!h3_uqs->wait_event.tasklet)
896 return 0;
897
898 h3_uqs->wait_event.tasklet->process = t;
899 h3_uqs->wait_event.tasklet->context = h3_uqs;
Frédéric Lécaille000162e2022-04-01 09:04:57 +0200900 h3_uqs->wait_event.events = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100901 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100902}
903
904static inline void h3_uqs_release(struct h3_uqs *h3_uqs)
905{
906 if (h3_uqs->qcs)
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200907 qcs_free(h3_uqs->qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100908}
909
910static inline void h3_uqs_release_all(struct h3 *h3)
911{
912 h3_uqs_tasklet_release(&h3->lctrl);
913 h3_uqs_release(&h3->lctrl);
914 h3_uqs_tasklet_release(&h3->lqpack_enc);
915 h3_uqs_release(&h3->lqpack_enc);
916 h3_uqs_tasklet_release(&h3->lqpack_dec);
917 h3_uqs_release(&h3->lqpack_dec);
918}
919
920/* Initialize the HTTP/3 context for <qcc> mux.
921 * Return 1 if succeeded, 0 if not.
922 */
923static int h3_init(struct qcc *qcc)
924{
925 struct h3 *h3;
926
927 h3 = pool_alloc(pool_head_h3);
928 if (!h3)
929 goto fail_no_h3;
930
931 h3->qcc = qcc;
932 h3->err = H3_NO_ERROR;
933 h3->flags = 0;
934
935 if (!h3_uqs_init(&h3->rqpack_enc, h3, NULL, h3_uqs_task) ||
936 !h3_uqs_init(&h3->rqpack_dec, h3, NULL, h3_uqs_task) ||
937 !h3_uqs_init(&h3->rctrl, h3, h3_control_recv, h3_uqs_task))
938 goto fail_no_h3_ruqs;
939
940 if (!h3_uqs_init(&h3->lctrl, h3, h3_control_send, h3_uqs_task) ||
941 !h3_uqs_init(&h3->lqpack_enc, h3, NULL, h3_uqs_task) ||
942 !h3_uqs_init(&h3->lqpack_dec, h3, NULL, h3_uqs_task))
943 goto fail_no_h3_luqs;
944
945 qcc->ctx = h3;
946 LIST_INIT(&h3->buf_wait.list);
947
948 return 1;
949
950 fail_no_h3_ruqs:
951 h3_uqs_release_all(h3);
952 fail_no_h3_luqs:
953 h3_uqs_tasklets_release(h3);
954 pool_free(pool_head_h3, h3);
955 fail_no_h3:
956 return 0;
957}
958
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200959static void h3_release(void *ctx)
960{
961 struct h3 *h3 = ctx;
962
963 h3_uqs_release_all(h3);
964 h3_uqs_tasklets_release(h3);
965 pool_free(pool_head_h3, h3);
966}
967
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200968/* Check if the H3 connection can still be considered as active.
969 *
970 * Return true if active else false.
971 */
972static int h3_is_active(const struct qcc *qcc, void *ctx)
973{
974 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
975 return 1;
976
977 return 0;
978}
979
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100980/* HTTP/3 application layer operations */
981const struct qcc_app_ops h3_ops = {
982 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200983 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100984 .attach_ruqs = h3_attach_ruqs,
985 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +0100986 .snd_buf = h3_snd_buf,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200987 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100988 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200989 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200990 .release = h3_release,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100991};