blob: 68fc1025a689234775cdb6b971e9f9bd2cafad81 [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 Denoyelleb49fa1a2021-08-24 15:30:12 +0200115 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100116 struct ist meth = IST_NULL, path = IST_NULL;
117 //struct ist scheme = IST_NULL, authority = IST_NULL;
118 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200119 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100120
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200121 /* TODO support buffer wrapping */
122 BUG_ON(b_contig_data(buf, 0) != b_data(buf));
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100123 if (qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp, list) < 0)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200124 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100125
126 qc_get_buf(qcs, &htx_buf);
127 BUG_ON(!b_size(&htx_buf));
128 htx = htx_from_buf(&htx_buf);
129
130 /* first treat pseudo-header to build the start line */
131 hdr_idx = 0;
132 while (1) {
133 if (isteq(list[hdr_idx].n, ist("")))
134 break;
135
136 if (istmatch(list[hdr_idx].n, ist(":"))) {
137 /* pseudo-header */
138 if (isteq(list[hdr_idx].n, ist(":method")))
139 meth = list[hdr_idx].v;
140 else if (isteq(list[hdr_idx].n, ist(":path")))
141 path = list[hdr_idx].v;
142 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
143 // scheme = list[hdr_idx].v;
144 else if (isteq(list[hdr_idx].n, ist(":authority")))
145 authority = list[hdr_idx].v;
146 }
147
148 ++hdr_idx;
149 }
150
151 flags |= HTX_SL_F_VER_11;
Amaury Denoyelle0fa14a62022-04-26 16:24:39 +0200152 flags |= HTX_SL_F_XFER_LEN;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100153
154 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
155 if (!sl)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200156 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100157
158 if (fin)
159 sl->flags |= HTX_SL_F_BODYLESS;
160
161 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
162 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
163
164 if (isttest(authority))
165 htx_add_header(htx, ist("host"), authority);
166
167 /* now treat standard headers */
168 hdr_idx = 0;
169 while (1) {
170 if (isteq(list[hdr_idx].n, ist("")))
171 break;
172
173 if (!istmatch(list[hdr_idx].n, ist(":")))
174 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
175
176 ++hdr_idx;
177 }
178
179 htx_add_endof(htx, HTX_BLK_EOH);
180 htx_to_buf(htx, &htx_buf);
181
182 if (fin)
183 htx->flags |= HTX_FL_EOM;
184
Willy Tarreau01c2a4a2022-05-10 15:46:10 +0200185 if (!qc_attach_cs(qcs, &htx_buf))
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200186 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100187
188 /* buffer is transferred to conn_stream and set to NULL
189 * except on stream creation error.
190 */
191 b_free(&htx_buf);
192 offer_buffers(NULL, 1);
193
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200194 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100195}
196
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100197/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
198 * HTX buffer. <fin> must be set if this is the last data to transfer from this
199 * stream.
200 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200201 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100202 */
203static int h3_data_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
204 char fin)
205{
206 struct buffer *appbuf;
207 struct htx *htx = NULL;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200208 size_t contig = 0, htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100209 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200210 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100211
212 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
213 BUG_ON(!appbuf);
214 htx = htx_from_buf(appbuf);
215
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200216 if (len > b_data(buf)) {
217 len = b_data(buf);
218 fin = 0;
219 }
220
221 head = b_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200222 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100223 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200224 if (!htx_space) {
225 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200226 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200227 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200228
229 if (len > htx_space) {
230 len = htx_space;
231 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100232 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100233
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200234 contig = b_contig_data(buf, contig);
235 if (len > contig) {
236 htx_sent = htx_add_data(htx, ist2(b_head(buf), contig));
237 head = b_orig(buf);
238 len -= contig;
239 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100240 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100241
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200242 htx_sent += htx_add_data(htx, ist2(head, len));
243 BUG_ON(htx_sent < len);
244
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200245 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100246 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100247
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200248 out:
249 htx_to_buf(htx, appbuf);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200250 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100251}
252
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100253/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
254 * that we received the last data of the stream.
255 * Returns <0 on error else 0.
256 */
257static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
258{
259 struct buffer *rxbuf = &qcs->rx.buf;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200260 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200261 ssize_t ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100262
Amaury Denoyellebb970422022-04-12 16:40:52 +0200263 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100264 if (!b_data(rxbuf))
265 return 0;
266
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200267 while (b_data(rxbuf) && !(qcs->flags & QC_SF_DEM_FULL)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100268 uint64_t ftype, flen;
269 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100270 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100271
272 /* Work on a copy of <rxbuf> */
273 b = h3_b_dup(rxbuf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200274 if (!h3s->demux_frame_len) {
275 size_t hlen = h3_decode_frm_header(&ftype, &flen, &b);
276 if (!hlen)
277 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100278
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200279 h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n",
280 __func__, ftype, flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100281
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200282 b_del(rxbuf, hlen);
283 h3s->demux_frame_type = ftype;
284 h3s->demux_frame_len = flen;
285 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100286
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200287 flen = h3s->demux_frame_len;
288 ftype = h3s->demux_frame_type;
289 if (flen > b_data(&b) && !b_full(rxbuf))
290 break;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100291 last_stream_frame = (fin && flen == b_data(rxbuf));
292
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100293 switch (ftype) {
294 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100295 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
296 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200297 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100298 break;
299 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100300 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
301 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200302 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100303 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100304 case H3_FT_PUSH_PROMISE:
305 /* Not supported */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200306 ret = MIN(b_data(rxbuf), flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100307 break;
308 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100309 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
310 * unknown frame types MUST be ignored
311 */
312 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200313 ret = MIN(b_data(rxbuf), flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100314 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200315
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200316 if (ret) {
317 b_del(rxbuf, ret);
318 BUG_ON(h3s->demux_frame_len < ret);
319 h3s->demux_frame_len -= ret;
320 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100321 }
322
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +0200323 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
324 * However, currently, io-cb of MUX does not handle Rx.
325 */
326
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100327 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100328}
329
330/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
331 * <rxbuf> buffer. This function does not update this buffer.
332 * Returns 0 if something wrong happened, 1 if not.
333 */
334static int h3_parse_settings_frm(struct h3 *h3, const struct buffer *rxbuf, size_t flen)
335{
336 uint64_t id, value;
337 const unsigned char *buf, *end;
338
339 buf = (const unsigned char *)b_head(rxbuf);
340 end = buf + flen;
341
342 while (buf <= end) {
343 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
344 return 0;
345
346 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
347 __func__, (unsigned long long)id, (unsigned long long)value);
348 switch (id) {
349 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
350 h3->qpack_max_table_capacity = value;
351 break;
352 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
353 h3->max_field_section_size = value;
354 break;
355 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
356 h3->qpack_blocked_streams = value;
357 break;
358 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
359 h3->err = H3_SETTINGS_ERROR;
360 return 0;
361 default:
362 /* MUST be ignored */
363 break;
364 }
365 }
366
367 return 1;
368}
369
370/* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as
371 * there is not enough received data.
372 * Returns 0 if something wrong happened, 1 if not.
373 */
374static int h3_control_recv(struct h3_uqs *h3_uqs, void *ctx)
375{
376 struct buffer *rxbuf = &h3_uqs->qcs->rx.buf;
377 struct h3 *h3 = ctx;
378
Amaury Denoyellebb970422022-04-12 16:40:52 +0200379 h3_debug_printf(stderr, "%s STREAM ID: %lu\n", __func__, h3_uqs->qcs->id);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100380 if (!b_data(rxbuf))
381 return 1;
382
383 while (b_data(rxbuf)) {
384 size_t hlen;
385 uint64_t ftype, flen;
386 struct buffer b;
387
388 /* Work on a copy of <rxbuf> */
389 b = h3_b_dup(rxbuf);
390 hlen = h3_decode_frm_header(&ftype, &flen, &b);
391 if (!hlen)
392 break;
393
394 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
395 (unsigned long long)ftype, (unsigned long long)flen);
396 if (flen > b_data(&b))
397 break;
398
399 b_del(rxbuf, hlen);
400 /* From here, a frame must not be truncated */
401 switch (ftype) {
402 case H3_FT_CANCEL_PUSH:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100403 /* XXX TODO XXX */
404 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100405 break;
406 case H3_FT_SETTINGS:
407 if (!h3_parse_settings_frm(h3, rxbuf, flen))
408 return 0;
409 break;
410 case H3_FT_GOAWAY:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100411 /* XXX TODO XXX */
412 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100413 break;
414 case H3_FT_MAX_PUSH_ID:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100415 /* XXX TODO XXX */
416 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100417 break;
418 default:
419 /* Error */
420 h3->err = H3_FRAME_UNEXPECTED;
421 return 0;
422 }
423 b_del(rxbuf, flen);
424 }
425
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100426 /* Handle the case where remaining data are present in the buffer. This
427 * can happen if there is an incomplete frame. In this case, subscribe
428 * on the lower layer to restart receive operation.
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100429 */
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100430 if (b_data(rxbuf))
431 qcs_subscribe(h3_uqs->qcs, SUB_RETRY_RECV, &h3_uqs->wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100432
433 return 1;
434}
435
Amaury Denoyellea5871362021-10-07 16:26:12 +0200436/* Returns buffer for data sending.
437 * May be NULL if the allocation failed.
438 */
439static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100440{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200441 if (!b_size(&qcs->tx.buf))
442 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100443
Amaury Denoyellea5871362021-10-07 16:26:12 +0200444 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100445}
446
447/* Function used to emit stream data from <h3_uqs> control uni-stream */
448static int h3_control_send(struct h3_uqs *h3_uqs, void *ctx)
449{
450 int ret;
451 struct h3 *h3 = ctx;
452 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200453 struct buffer pos, *res;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100454
455 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200456 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100457 if (!(h3->flags & H3_CF_SETTINGS_SENT)) {
458 struct qcs *qcs = h3_uqs->qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100459 size_t frm_len;
460
461 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
462 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
463 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
464 quic_int_getsize(h3_settings_qpack_blocked_streams);
465 if (h3_settings_max_field_section_size) {
466 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
467 quic_int_getsize(h3_settings_max_field_section_size);
468 }
469
Amaury Denoyellea5871362021-10-07 16:26:12 +0200470 b_quic_enc_int(&pos, H3_UNI_STRM_TP_CONTROL_STREAM);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100471 /* Build a SETTINGS frame */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200472 b_quic_enc_int(&pos, H3_FT_SETTINGS);
473 b_quic_enc_int(&pos, frm_len);
474 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
475 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
476 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
477 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100478 if (h3_settings_max_field_section_size) {
Amaury Denoyellea5871362021-10-07 16:26:12 +0200479 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
480 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100481 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200482
483 res = mux_get_buf(qcs);
484 if (b_room(res) < b_data(&pos)) {
485 // TODO the mux should be put in blocked state, with
486 // the stream in state waiting for settings to be sent
487 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100488 }
489
Amaury Denoyellea5871362021-10-07 16:26:12 +0200490 ret = b_force_xfer(res, &pos, b_data(&pos));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100491 if (ret > 0) {
492 h3->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200493 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
494 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100495 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100496 }
497
498 return ret;
499}
500
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200501static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
502{
503 struct buffer outbuf;
504 struct buffer headers_buf = BUF_NULL;
505 struct buffer *res;
506 struct http_hdr list[global.tune.max_http_hdr];
507 struct htx_sl *sl;
508 struct htx_blk *blk;
509 enum htx_blk_type type;
510 int frame_length_size; /* size in bytes of frame length varint field */
511 int ret = 0;
512 int hdr;
513 int status = 0;
514
515 sl = NULL;
516 hdr = 0;
517 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
518 type = htx_get_blk_type(blk);
519
520 if (type == HTX_BLK_UNUSED)
521 continue;
522
523 if (type == HTX_BLK_EOH)
524 break;
525
526 if (type == HTX_BLK_RES_SL) {
527 /* start-line -> HEADERS h3 frame */
528 BUG_ON(sl);
529 sl = htx_get_blk_ptr(htx, blk);
530 /* TODO should be on h3 layer */
531 status = sl->info.res.status;
532 }
533 else if (type == HTX_BLK_HDR) {
534 list[hdr].n = htx_get_blk_name(htx, blk);
535 list[hdr].v = htx_get_blk_value(htx, blk);
536 hdr++;
537 }
538 else {
539 ABORT_NOW();
540 goto err;
541 }
542 }
543
544 BUG_ON(!sl);
545
546 list[hdr].n = ist("");
547
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200548 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200549
550 /* At least 5 bytes to store frame type + length as a varint max size */
551 if (b_room(res) < 5)
552 ABORT_NOW();
553
554 b_reset(&outbuf);
555 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
556 /* Start the headers after frame type + length */
557 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
558
559 if (qpack_encode_field_section_line(&headers_buf))
560 ABORT_NOW();
561 if (qpack_encode_int_status(&headers_buf, status))
562 ABORT_NOW();
563
564 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
565 if (isteq(list[hdr].n, ist("")))
566 break;
567
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100568 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
569 * Transfer codings (see Section 6.1 of [HTTP11]) are not
570 * defined for HTTP/3; the Transfer-Encoding header field MUST
571 * NOT be used.
572 */
573 if (isteq(list[hdr].n, ist("transfer-encoding")))
574 continue;
575
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200576 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
577 ABORT_NOW();
578 }
579
580 /* Now that all headers are encoded, we are certain that res buffer is
581 * big enough
582 */
583 frame_length_size = quic_int_getsize(b_data(&headers_buf));
584 res->head += 4 - frame_length_size;
585 b_putchr(res, 0x01); /* h3 HEADERS frame type */
586 if (!b_quic_enc_int(res, b_data(&headers_buf)))
587 ABORT_NOW();
588 b_add(res, b_data(&headers_buf));
589
590 ret = 0;
591 blk = htx_get_head_blk(htx);
592 while (blk) {
593 type = htx_get_blk_type(blk);
594 ret += htx_get_blksz(blk);
595 blk = htx_remove_blk(htx, blk);
596 if (type == HTX_BLK_EOH)
597 break;
598 }
599
600 return ret;
601
602 err:
603 return 0;
604}
605
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200606/* Returns the total of bytes sent. */
607static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
608{
609 struct buffer outbuf;
610 struct buffer *res;
611 size_t total = 0;
612 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200613 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200614 struct htx_blk *blk;
615 enum htx_blk_type type;
616
617 htx = htx_from_buf(buf);
618
619 new_frame:
620 if (!count || htx_is_empty(htx))
621 goto end;
622
623 blk = htx_get_head_blk(htx);
624 type = htx_get_blk_type(blk);
625 fsize = bsize = htx_get_blksz(blk);
626
627 if (type != HTX_BLK_DATA)
628 goto end;
629
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200630 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200631
632 if (fsize > count)
633 fsize = count;
634
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200635 /* h3 DATA headers : 1-byte frame type + varint frame length */
636 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200637
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200638 while (1) {
639 b_reset(&outbuf);
640 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
641 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
642 break;
643 b_slow_realign(res, trash.area, b_data(res));
644 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200645
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100646 /* Not enough room for headers and at least one data byte, block the
647 * stream. It is expected that the conn-stream layer will subscribe on
648 * SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200649 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100650 if (b_size(&outbuf) <= hsize) {
651 qcs->flags |= QC_SF_BLK_MROOM;
652 goto end;
653 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200654
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200655 if (b_size(&outbuf) < hsize + fsize)
656 fsize = b_size(&outbuf) - hsize;
657 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200658
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200659 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
660 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
661
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200662 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200663 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200664 count -= fsize;
665
666 if (fsize == bsize)
667 htx_remove_blk(htx, blk);
668 else
669 htx_cut_data_blk(htx, blk, fsize);
670
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200671 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200672 b_add(res, b_data(&outbuf));
673 goto new_frame;
674
675 end:
676 return total;
677}
678
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200679size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
680{
681 size_t total = 0;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100682 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200683 struct htx *htx;
684 enum htx_blk_type btype;
685 struct htx_blk *blk;
686 uint32_t bsize;
687 int32_t idx;
688 int ret;
689
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100690 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100691
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200692 htx = htx_from_buf(buf);
693
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100694 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200695 idx = htx_get_head(htx);
696 blk = htx_get_blk(htx, idx);
697 btype = htx_get_blk_type(blk);
698 bsize = htx_get_blksz(blk);
699
700 /* Not implemented : QUIC on backend side */
701 BUG_ON(btype == HTX_BLK_REQ_SL);
702
703 switch (btype) {
704 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200705 /* start-line -> HEADERS h3 frame */
706 ret = h3_resp_headers_send(qcs, htx);
707 if (ret > 0) {
708 total += ret;
709 count -= ret;
710 if (ret < bsize)
711 goto out;
712 }
713 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200714
715 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200716 ret = h3_resp_data_send(qcs, buf, count);
717 if (ret > 0) {
718 htx = htx_from_buf(buf);
719 total += ret;
720 count -= ret;
721 if (ret < bsize)
722 goto out;
723 }
724 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200725
726 case HTX_BLK_TLR:
727 case HTX_BLK_EOT:
728 /* TODO trailers */
729
730 default:
731 htx_remove_blk(htx, blk);
732 total += bsize;
733 count -= bsize;
734 break;
735 }
736 }
737
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100738 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
739 qcs->flags |= QC_SF_FIN_STREAM;
740
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200741 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200742 if (total) {
743 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
744 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
745 }
746
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200747 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200748}
749
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200750static int h3_attach(struct qcs *qcs)
751{
752 struct h3s *h3s;
753
754 h3s = pool_alloc(pool_head_h3s);
755 if (!h3s)
756 return 1;
757
758 qcs->ctx = h3s;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200759 h3s->demux_frame_len = 0;
760 h3s->demux_frame_type = 0;
761
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200762 return 0;
763}
764
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100765/* Finalize the initialization of remotely initiated uni-stream <qcs>.
766 * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error
767 * to inform the QUIC mux layer of the encountered error.
768 */
769static int h3_attach_ruqs(struct qcs *qcs, void *ctx)
770{
771 uint64_t strm_type;
772 struct h3 *h3 = ctx;
773 struct buffer *rxbuf = &qcs->rx.buf;
774
775 /* First octets: the uni-stream type */
776 if (!b_quic_dec_int(&strm_type, rxbuf, NULL) || strm_type > H3_UNI_STRM_TP_MAX)
777 return 0;
778
779 /* Note that for all the uni-streams below, this is an error to receive two times the
780 * same type of uni-stream (even for Push stream which is not supported at this time.
781 */
782 switch (strm_type) {
783 case H3_UNI_STRM_TP_CONTROL_STREAM:
784 if (h3->rctrl.qcs) {
785 h3->err = H3_STREAM_CREATION_ERROR;
786 return 0;
787 }
788
789 h3->rctrl.qcs = qcs;
790 h3->rctrl.cb = h3_control_recv;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100791 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100792 break;
793 case H3_UNI_STRM_TP_PUSH_STREAM:
794 /* NOT SUPPORTED */
795 break;
796 case H3_UNI_STRM_TP_QPACK_ENCODER:
797 if (h3->rqpack_enc.qcs) {
798 h3->err = H3_STREAM_CREATION_ERROR;
799 return 0;
800 }
801
802 h3->rqpack_enc.qcs = qcs;
803 h3->rqpack_enc.cb = qpack_decode_enc;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100804 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_enc.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100805 break;
806 case H3_UNI_STRM_TP_QPACK_DECODER:
807 if (h3->rqpack_dec.qcs) {
808 h3->err = H3_STREAM_CREATION_ERROR;
809 return 0;
810 }
811
812 h3->rqpack_dec.qcs = qcs;
813 h3->rqpack_dec.cb = qpack_decode_dec;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100814 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_dec.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100815 break;
816 default:
817 /* Error */
818 h3->err = H3_STREAM_CREATION_ERROR;
819 return 0;
820 }
821
822 return 1;
823}
824
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200825static void h3_detach(struct qcs *qcs)
826{
827 struct h3s *h3s = qcs->ctx;
828 pool_free(pool_head_h3s, h3s);
829 qcs->ctx = NULL;
830}
831
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100832static int h3_finalize(void *ctx)
833{
834 struct h3 *h3 = ctx;
835
Amaury Denoyelleb7880542022-02-09 10:28:53 +0100836 h3->lctrl.qcs = qcs_new(h3->qcc, 0x3, QCS_SRV_UNI);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100837 if (!h3->lctrl.qcs)
838 return 0;
839
840 /* Wakeup ->lctrl uni-stream */
Frédéric Lécaillee16f0bd2021-08-23 09:50:29 +0200841 h3_control_send(&h3->lctrl, h3);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100842
843 return 1;
844}
845
846/* Tasklet dedicated to h3 incoming uni-streams */
847static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state)
848{
849 struct h3_uqs *h3_uqs = ctx;
850 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
851
852 h3_uqs->cb(h3_uqs, h3);
853 return NULL;
854}
855
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100856/* Release all the tasklet attached to <h3_uqs> uni-stream */
857static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs)
858{
859 struct tasklet *t = h3_uqs->wait_event.tasklet;
860
861 if (t)
862 tasklet_free(t);
863}
864
865/* Release all the tasklet attached to <h3> uni-streams */
866static void h3_uqs_tasklets_release(struct h3 *h3)
867{
868 h3_uqs_tasklet_release(&h3->rqpack_enc);
869 h3_uqs_tasklet_release(&h3->rqpack_dec);
870 h3_uqs_tasklet_release(&h3->rctrl);
871}
872
873/* Tasklet dedicated to h3 outgoing uni-streams */
874__maybe_unused
875static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state)
876{
877 struct h3_uqs *h3_uqs = ctx;
878 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
879
880 h3_uqs->cb(h3_uqs, h3);
881 return NULL;
882}
883
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500884/* Initialize <h3_uqs> uni-stream with <t> as tasklet */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100885static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3 *h3,
886 int (*cb)(struct h3_uqs *h3_uqs, void *ctx),
887 struct task *(*t)(struct task *, void *, unsigned int))
888{
889 h3_uqs->qcs = NULL;
890 h3_uqs->cb = cb;
891 h3_uqs->wait_event.tasklet = tasklet_new();
892 if (!h3_uqs->wait_event.tasklet)
893 return 0;
894
895 h3_uqs->wait_event.tasklet->process = t;
896 h3_uqs->wait_event.tasklet->context = h3_uqs;
Frédéric Lécaille000162e2022-04-01 09:04:57 +0200897 h3_uqs->wait_event.events = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100898 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100899}
900
901static inline void h3_uqs_release(struct h3_uqs *h3_uqs)
902{
903 if (h3_uqs->qcs)
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200904 qcs_free(h3_uqs->qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100905}
906
907static inline void h3_uqs_release_all(struct h3 *h3)
908{
909 h3_uqs_tasklet_release(&h3->lctrl);
910 h3_uqs_release(&h3->lctrl);
911 h3_uqs_tasklet_release(&h3->lqpack_enc);
912 h3_uqs_release(&h3->lqpack_enc);
913 h3_uqs_tasklet_release(&h3->lqpack_dec);
914 h3_uqs_release(&h3->lqpack_dec);
915}
916
917/* Initialize the HTTP/3 context for <qcc> mux.
918 * Return 1 if succeeded, 0 if not.
919 */
920static int h3_init(struct qcc *qcc)
921{
922 struct h3 *h3;
923
924 h3 = pool_alloc(pool_head_h3);
925 if (!h3)
926 goto fail_no_h3;
927
928 h3->qcc = qcc;
929 h3->err = H3_NO_ERROR;
930 h3->flags = 0;
931
932 if (!h3_uqs_init(&h3->rqpack_enc, h3, NULL, h3_uqs_task) ||
933 !h3_uqs_init(&h3->rqpack_dec, h3, NULL, h3_uqs_task) ||
934 !h3_uqs_init(&h3->rctrl, h3, h3_control_recv, h3_uqs_task))
935 goto fail_no_h3_ruqs;
936
937 if (!h3_uqs_init(&h3->lctrl, h3, h3_control_send, h3_uqs_task) ||
938 !h3_uqs_init(&h3->lqpack_enc, h3, NULL, h3_uqs_task) ||
939 !h3_uqs_init(&h3->lqpack_dec, h3, NULL, h3_uqs_task))
940 goto fail_no_h3_luqs;
941
942 qcc->ctx = h3;
943 LIST_INIT(&h3->buf_wait.list);
944
945 return 1;
946
947 fail_no_h3_ruqs:
948 h3_uqs_release_all(h3);
949 fail_no_h3_luqs:
950 h3_uqs_tasklets_release(h3);
951 pool_free(pool_head_h3, h3);
952 fail_no_h3:
953 return 0;
954}
955
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200956static void h3_release(void *ctx)
957{
958 struct h3 *h3 = ctx;
959
960 h3_uqs_release_all(h3);
961 h3_uqs_tasklets_release(h3);
962 pool_free(pool_head_h3, h3);
963}
964
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200965/* Check if the H3 connection can still be considered as active.
966 *
967 * Return true if active else false.
968 */
969static int h3_is_active(const struct qcc *qcc, void *ctx)
970{
971 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
972 return 1;
973
974 return 0;
975}
976
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100977/* HTTP/3 application layer operations */
978const struct qcc_app_ops h3_ops = {
979 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200980 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100981 .attach_ruqs = h3_attach_ruqs,
982 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +0100983 .snd_buf = h3_snd_buf,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200984 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100985 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200986 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200987 .release = h3_release,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100988};