blob: cd8f32d5544d887a941c52f3e3a0414659bca771 [file] [log] [blame]
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001/*
2 * HTTP/3 protocol processing
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation, version 2.1
7 * exclusively.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <haproxy/buf.h>
Amaury Denoyelle99043552021-08-24 15:36:02 +020020#include <haproxy/connection.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010021#include <haproxy/dynbuf.h>
22#include <haproxy/h3.h>
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +020023#include <haproxy/http.h>
24#include <haproxy/htx.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010025#include <haproxy/istbuf.h>
26#include <haproxy/mux_quic.h>
27#include <haproxy/pool.h>
28#include <haproxy/qpack-dec.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020029#include <haproxy/qpack-enc.h>
30#include <haproxy/quic_enc.h>
Amaury Denoyelle99043552021-08-24 15:36:02 +020031#include <haproxy/stream.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010032#include <haproxy/tools.h>
33#include <haproxy/xprt_quic.h>
34
35#define DEBUG_H3
36
37#if defined(DEBUG_H3)
38#define h3_debug_printf fprintf
39#define h3_debug_hexdump debug_hexdump
40#else
41#define h3_debug_printf(...) do { } while (0)
42#define h3_debug_hexdump(...) do { } while (0)
43#endif
44
45#define H3_CF_SETTINGS_SENT 0x00000001
46
47/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +020048static uint64_t h3_settings_qpack_max_table_capacity = 0;
49static uint64_t h3_settings_qpack_blocked_streams = 4096;
50static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010051
52struct h3 {
53 struct qcc *qcc;
54 enum h3_err err;
55 uint32_t flags;
56 /* Locally initiated uni-streams */
57 struct h3_uqs lqpack_enc;
58 struct h3_uqs lqpack_dec;
59 struct h3_uqs lctrl;
60 /* Remotely initiated uni-streams */
61 struct h3_uqs rqpack_enc;
62 struct h3_uqs rqpack_dec;
63 struct h3_uqs rctrl;
64 /* Settings */
65 uint64_t qpack_max_table_capacity;
66 uint64_t qpack_blocked_streams;
67 uint64_t max_field_section_size;
68 struct buffer_wait buf_wait; /* wait list for buffer allocations */
69};
70
71DECLARE_STATIC_POOL(pool_head_h3, "h3", sizeof(struct h3));
72
73/* Simple function to duplicate a buffer */
74static inline struct buffer h3_b_dup(struct buffer *b)
75{
76 return b_make(b->area, b->size, b->head, b->data);
77}
78
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010079/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
80 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
81 * Note that this function update <b> buffer to reflect the number of bytes consumed
82 * to decode the h3 frame header.
83 */
84static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
85 struct buffer *b)
86{
87 size_t hlen;
88
89 hlen = 0;
90 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
91 return 0;
92
93 return hlen;
94}
95
96/* Decode <qcs> remotely initiated bidi-stream */
97static int h3_decode_qcs(struct qcs *qcs, void *ctx)
98{
99 struct buffer *rxbuf = &qcs->rx.buf;
100 struct h3 *h3 = ctx;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200101 struct htx *htx;
102 struct htx_sl *sl;
Amaury Denoyelle99043552021-08-24 15:36:02 +0200103 struct conn_stream *cs;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200104 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200105 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200106 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100107
108 h3_debug_printf(stderr, "%s: STREAM ID: %llu\n", __func__, qcs->by_id.key);
109 if (!b_data(rxbuf))
110 return 0;
111
112 while (b_data(rxbuf)) {
113 size_t hlen;
114 uint64_t ftype, flen;
115 struct buffer b;
116
117 /* Work on a copy of <rxbuf> */
118 b = h3_b_dup(rxbuf);
119 hlen = h3_decode_frm_header(&ftype, &flen, &b);
120 if (!hlen)
121 break;
122
123 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
124 (unsigned long long)ftype, (unsigned long long)flen);
125 if (flen > b_data(&b))
126 break;
127
128 b_del(rxbuf, hlen);
129 switch (ftype) {
130 case H3_FT_DATA:
131 break;
132 case H3_FT_HEADERS:
133 {
134 const unsigned char *buf = (const unsigned char *)b_head(rxbuf);
135 size_t len = b_data(rxbuf);
136 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200137 struct ist meth = IST_NULL, path = IST_NULL;
138 struct ist scheme = IST_NULL, authority = IST_NULL;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100139
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200140 if (qpack_decode_fs(buf, len, tmp, list) < 0) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100141 h3->err = QPACK_DECOMPRESSION_FAILED;
142 return -1;
143 }
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200144
145 struct buffer htx_buf = BUF_NULL;
146 b_alloc(&htx_buf);
147 htx = htx_from_buf(&htx_buf);
148
149 /* first treat pseudo-header to build the start line */
150 hdr_idx = 0;
151 while (1) {
152 if (isteq(list[hdr_idx].n, ist("")))
153 break;
154
155 if (istmatch(list[hdr_idx].n, ist(":"))) {
156 /* pseudo-header */
157 if (isteq(list[hdr_idx].n, ist(":method")))
158 meth = list[hdr_idx].v;
159 else if (isteq(list[hdr_idx].n, ist(":path")))
160 path = list[hdr_idx].v;
161 else if (isteq(list[hdr_idx].n, ist(":scheme")))
162 scheme = list[hdr_idx].v;
163 else if (isteq(list[hdr_idx].n, ist(":authority")))
164 authority = list[hdr_idx].v;
165 }
166
167 ++hdr_idx;
168 }
169
170 flags |= HTX_SL_F_VER_11;
171
172 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
173 sl->flags |= HTX_SL_F_BODYLESS;
174 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
175 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
176
177 if (isttest(authority))
178 htx_add_header(htx, ist("host"), authority);
179
180 /* now treat standard headers */
181 hdr_idx = 0;
182 while (1) {
183 if (isteq(list[hdr_idx].n, ist("")))
184 break;
185
186 if (!istmatch(list[hdr_idx].n, ist(":")))
187 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
188
189 ++hdr_idx;
190 }
191
192 htx_add_endof(htx, HTX_BLK_EOH);
193 htx_to_buf(htx, &htx_buf);
Amaury Denoyelle99043552021-08-24 15:36:02 +0200194
195 cs = cs_new(qcs->qcc->conn, qcs->qcc->conn->target);
196 cs->ctx = qcs;
197 stream_create_from_cs(cs, &htx_buf);
198
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500199 /* buffer is transferred to conn_stream and set to NULL
Amaury Denoyelle99043552021-08-24 15:36:02 +0200200 * except on stream creation error.
201 */
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200202 b_free(&htx_buf);
203
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100204 break;
205 }
206 case H3_FT_PUSH_PROMISE:
207 /* Not supported */
208 break;
209 default:
210 /* Error */
211 h3->err = H3_FRAME_UNEXPECTED;
212 return -1;
213 }
214 b_del(rxbuf, flen);
215 }
216
217 return 1;
218}
219
220/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
221 * <rxbuf> buffer. This function does not update this buffer.
222 * Returns 0 if something wrong happened, 1 if not.
223 */
224static int h3_parse_settings_frm(struct h3 *h3, const struct buffer *rxbuf, size_t flen)
225{
226 uint64_t id, value;
227 const unsigned char *buf, *end;
228
229 buf = (const unsigned char *)b_head(rxbuf);
230 end = buf + flen;
231
232 while (buf <= end) {
233 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
234 return 0;
235
236 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
237 __func__, (unsigned long long)id, (unsigned long long)value);
238 switch (id) {
239 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
240 h3->qpack_max_table_capacity = value;
241 break;
242 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
243 h3->max_field_section_size = value;
244 break;
245 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
246 h3->qpack_blocked_streams = value;
247 break;
248 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
249 h3->err = H3_SETTINGS_ERROR;
250 return 0;
251 default:
252 /* MUST be ignored */
253 break;
254 }
255 }
256
257 return 1;
258}
259
260/* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as
261 * there is not enough received data.
262 * Returns 0 if something wrong happened, 1 if not.
263 */
264static int h3_control_recv(struct h3_uqs *h3_uqs, void *ctx)
265{
266 struct buffer *rxbuf = &h3_uqs->qcs->rx.buf;
267 struct h3 *h3 = ctx;
268
269 h3_debug_printf(stderr, "%s STREAM ID: %llu\n", __func__, h3_uqs->qcs->by_id.key);
270 if (!b_data(rxbuf))
271 return 1;
272
273 while (b_data(rxbuf)) {
274 size_t hlen;
275 uint64_t ftype, flen;
276 struct buffer b;
277
278 /* Work on a copy of <rxbuf> */
279 b = h3_b_dup(rxbuf);
280 hlen = h3_decode_frm_header(&ftype, &flen, &b);
281 if (!hlen)
282 break;
283
284 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
285 (unsigned long long)ftype, (unsigned long long)flen);
286 if (flen > b_data(&b))
287 break;
288
289 b_del(rxbuf, hlen);
290 /* From here, a frame must not be truncated */
291 switch (ftype) {
292 case H3_FT_CANCEL_PUSH:
293 break;
294 case H3_FT_SETTINGS:
295 if (!h3_parse_settings_frm(h3, rxbuf, flen))
296 return 0;
297 break;
298 case H3_FT_GOAWAY:
299 break;
300 case H3_FT_MAX_PUSH_ID:
301 break;
302 default:
303 /* Error */
304 h3->err = H3_FRAME_UNEXPECTED;
305 return 0;
306 }
307 b_del(rxbuf, flen);
308 }
309
310 if (b_data(rxbuf))
311 h3->qcc->conn->mux->ruqs_subscribe(h3_uqs->qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event);
312
313 return 1;
314}
315
Amaury Denoyellea5871362021-10-07 16:26:12 +0200316/* Returns buffer for data sending.
317 * May be NULL if the allocation failed.
318 */
319static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100320{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200321 if (!b_size(&qcs->tx.buf))
322 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100323
Amaury Denoyellea5871362021-10-07 16:26:12 +0200324 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100325}
326
327/* Function used to emit stream data from <h3_uqs> control uni-stream */
328static int h3_control_send(struct h3_uqs *h3_uqs, void *ctx)
329{
330 int ret;
331 struct h3 *h3 = ctx;
332 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200333 struct buffer pos, *res;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100334
335 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200336 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100337 if (!(h3->flags & H3_CF_SETTINGS_SENT)) {
338 struct qcs *qcs = h3_uqs->qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100339 size_t frm_len;
340
341 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
342 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
343 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
344 quic_int_getsize(h3_settings_qpack_blocked_streams);
345 if (h3_settings_max_field_section_size) {
346 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
347 quic_int_getsize(h3_settings_max_field_section_size);
348 }
349
Amaury Denoyellea5871362021-10-07 16:26:12 +0200350 b_quic_enc_int(&pos, H3_UNI_STRM_TP_CONTROL_STREAM);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100351 /* Build a SETTINGS frame */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200352 b_quic_enc_int(&pos, H3_FT_SETTINGS);
353 b_quic_enc_int(&pos, frm_len);
354 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
355 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
356 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
357 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100358 if (h3_settings_max_field_section_size) {
Amaury Denoyellea5871362021-10-07 16:26:12 +0200359 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
360 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100361 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200362
363 res = mux_get_buf(qcs);
364 if (b_room(res) < b_data(&pos)) {
365 // TODO the mux should be put in blocked state, with
366 // the stream in state waiting for settings to be sent
367 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100368 }
369
Amaury Denoyellea5871362021-10-07 16:26:12 +0200370 ret = b_force_xfer(res, &pos, b_data(&pos));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100371 if (ret > 0) {
372 h3->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200373 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
374 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100375 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100376 }
377
378 return ret;
379}
380
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200381static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
382{
383 struct buffer outbuf;
384 struct buffer headers_buf = BUF_NULL;
385 struct buffer *res;
386 struct http_hdr list[global.tune.max_http_hdr];
387 struct htx_sl *sl;
388 struct htx_blk *blk;
389 enum htx_blk_type type;
390 int frame_length_size; /* size in bytes of frame length varint field */
391 int ret = 0;
392 int hdr;
393 int status = 0;
394
395 sl = NULL;
396 hdr = 0;
397 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
398 type = htx_get_blk_type(blk);
399
400 if (type == HTX_BLK_UNUSED)
401 continue;
402
403 if (type == HTX_BLK_EOH)
404 break;
405
406 if (type == HTX_BLK_RES_SL) {
407 /* start-line -> HEADERS h3 frame */
408 BUG_ON(sl);
409 sl = htx_get_blk_ptr(htx, blk);
410 /* TODO should be on h3 layer */
411 status = sl->info.res.status;
412 }
413 else if (type == HTX_BLK_HDR) {
414 list[hdr].n = htx_get_blk_name(htx, blk);
415 list[hdr].v = htx_get_blk_value(htx, blk);
416 hdr++;
417 }
418 else {
419 ABORT_NOW();
420 goto err;
421 }
422 }
423
424 BUG_ON(!sl);
425
426 list[hdr].n = ist("");
427
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200428 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200429
430 /* At least 5 bytes to store frame type + length as a varint max size */
431 if (b_room(res) < 5)
432 ABORT_NOW();
433
434 b_reset(&outbuf);
435 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
436 /* Start the headers after frame type + length */
437 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
438
439 if (qpack_encode_field_section_line(&headers_buf))
440 ABORT_NOW();
441 if (qpack_encode_int_status(&headers_buf, status))
442 ABORT_NOW();
443
444 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
445 if (isteq(list[hdr].n, ist("")))
446 break;
447
448 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
449 ABORT_NOW();
450 }
451
452 /* Now that all headers are encoded, we are certain that res buffer is
453 * big enough
454 */
455 frame_length_size = quic_int_getsize(b_data(&headers_buf));
456 res->head += 4 - frame_length_size;
457 b_putchr(res, 0x01); /* h3 HEADERS frame type */
458 if (!b_quic_enc_int(res, b_data(&headers_buf)))
459 ABORT_NOW();
460 b_add(res, b_data(&headers_buf));
461
462 ret = 0;
463 blk = htx_get_head_blk(htx);
464 while (blk) {
465 type = htx_get_blk_type(blk);
466 ret += htx_get_blksz(blk);
467 blk = htx_remove_blk(htx, blk);
468 if (type == HTX_BLK_EOH)
469 break;
470 }
471
Amaury Denoyelle42bb8aa2021-08-24 16:28:47 +0200472 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx) && status >= 200)
473 qcs->flags |= QC_SF_FIN_STREAM;
474
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200475 return ret;
476
477 err:
478 return 0;
479}
480
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200481/* Returns the total of bytes sent. */
482static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
483{
484 struct buffer outbuf;
485 struct buffer *res;
486 size_t total = 0;
487 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200488 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200489 struct htx_blk *blk;
490 enum htx_blk_type type;
491
492 htx = htx_from_buf(buf);
493
494 new_frame:
495 if (!count || htx_is_empty(htx))
496 goto end;
497
498 blk = htx_get_head_blk(htx);
499 type = htx_get_blk_type(blk);
500 fsize = bsize = htx_get_blksz(blk);
501
502 if (type != HTX_BLK_DATA)
503 goto end;
504
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200505 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200506
507 if (fsize > count)
508 fsize = count;
509
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200510 /* h3 DATA headers : 1-byte frame type + varint frame length */
511 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200512
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200513 while (1) {
514 b_reset(&outbuf);
515 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
516 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
517 break;
518 b_slow_realign(res, trash.area, b_data(res));
519 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200520
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200521 /* not enough room for headers and at least one data byte, block the
522 * stream
523 */
524 if (b_size(&outbuf) <= hsize) {
525 qcs->flags |= QC_SF_BLK_MROOM;
526 goto end;
527 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200528
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200529 if (b_size(&outbuf) < hsize + fsize)
530 fsize = b_size(&outbuf) - hsize;
531 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200532
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200533 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
534 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
535
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200536 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200537 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200538 count -= fsize;
539
540 if (fsize == bsize)
541 htx_remove_blk(htx, blk);
542 else
543 htx_cut_data_blk(htx, blk, fsize);
544
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200545 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200546 b_add(res, b_data(&outbuf));
547 goto new_frame;
548
549 end:
550 return total;
551}
552
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200553size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
554{
555 size_t total = 0;
556 struct qcs *qcs = cs->ctx;
557 struct htx *htx;
558 enum htx_blk_type btype;
559 struct htx_blk *blk;
560 uint32_t bsize;
561 int32_t idx;
562 int ret;
563
564 htx = htx_from_buf(buf);
565
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200566 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200567 idx = htx_get_head(htx);
568 blk = htx_get_blk(htx, idx);
569 btype = htx_get_blk_type(blk);
570 bsize = htx_get_blksz(blk);
571
572 /* Not implemented : QUIC on backend side */
573 BUG_ON(btype == HTX_BLK_REQ_SL);
574
575 switch (btype) {
576 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200577 /* start-line -> HEADERS h3 frame */
578 ret = h3_resp_headers_send(qcs, htx);
579 if (ret > 0) {
580 total += ret;
581 count -= ret;
582 if (ret < bsize)
583 goto out;
584 }
585 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200586
587 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200588 ret = h3_resp_data_send(qcs, buf, count);
589 if (ret > 0) {
590 htx = htx_from_buf(buf);
591 total += ret;
592 count -= ret;
593 if (ret < bsize)
594 goto out;
595 }
596 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200597
598 case HTX_BLK_TLR:
599 case HTX_BLK_EOT:
600 /* TODO trailers */
601
602 default:
603 htx_remove_blk(htx, blk);
604 total += bsize;
605 count -= bsize;
606 break;
607 }
608 }
609
Amaury Denoyelle42bb8aa2021-08-24 16:28:47 +0200610 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
611 qcs->flags |= QC_SF_FIN_STREAM;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200612
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200613 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200614 if (total) {
615 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
616 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
617 }
618
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200619 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200620}
621
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100622/* Finalize the initialization of remotely initiated uni-stream <qcs>.
623 * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error
624 * to inform the QUIC mux layer of the encountered error.
625 */
626static int h3_attach_ruqs(struct qcs *qcs, void *ctx)
627{
628 uint64_t strm_type;
629 struct h3 *h3 = ctx;
630 struct buffer *rxbuf = &qcs->rx.buf;
631
632 /* First octets: the uni-stream type */
633 if (!b_quic_dec_int(&strm_type, rxbuf, NULL) || strm_type > H3_UNI_STRM_TP_MAX)
634 return 0;
635
636 /* Note that for all the uni-streams below, this is an error to receive two times the
637 * same type of uni-stream (even for Push stream which is not supported at this time.
638 */
639 switch (strm_type) {
640 case H3_UNI_STRM_TP_CONTROL_STREAM:
641 if (h3->rctrl.qcs) {
642 h3->err = H3_STREAM_CREATION_ERROR;
643 return 0;
644 }
645
646 h3->rctrl.qcs = qcs;
647 h3->rctrl.cb = h3_control_recv;
648 h3->qcc->conn->mux->ruqs_subscribe(qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event);
649 break;
650 case H3_UNI_STRM_TP_PUSH_STREAM:
651 /* NOT SUPPORTED */
652 break;
653 case H3_UNI_STRM_TP_QPACK_ENCODER:
654 if (h3->rqpack_enc.qcs) {
655 h3->err = H3_STREAM_CREATION_ERROR;
656 return 0;
657 }
658
659 h3->rqpack_enc.qcs = qcs;
660 h3->rqpack_enc.cb = qpack_decode_enc;
661 h3->qcc->conn->mux->ruqs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_enc.wait_event);
662 break;
663 case H3_UNI_STRM_TP_QPACK_DECODER:
664 if (h3->rqpack_dec.qcs) {
665 h3->err = H3_STREAM_CREATION_ERROR;
666 return 0;
667 }
668
669 h3->rqpack_dec.qcs = qcs;
670 h3->rqpack_dec.cb = qpack_decode_dec;
671 h3->qcc->conn->mux->ruqs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_dec.wait_event);
672 break;
673 default:
674 /* Error */
675 h3->err = H3_STREAM_CREATION_ERROR;
676 return 0;
677 }
678
679 return 1;
680}
681
682static int h3_finalize(void *ctx)
683{
684 struct h3 *h3 = ctx;
685
686 h3->lctrl.qcs = luqs_new(h3->qcc);
687 if (!h3->lctrl.qcs)
688 return 0;
689
690 /* Wakeup ->lctrl uni-stream */
Frédéric Lécaillee16f0bd2021-08-23 09:50:29 +0200691 h3_control_send(&h3->lctrl, h3);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100692
693 return 1;
694}
695
696/* Tasklet dedicated to h3 incoming uni-streams */
697static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state)
698{
699 struct h3_uqs *h3_uqs = ctx;
700 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
701
702 h3_uqs->cb(h3_uqs, h3);
703 return NULL;
704}
705
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100706/* Release all the tasklet attached to <h3_uqs> uni-stream */
707static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs)
708{
709 struct tasklet *t = h3_uqs->wait_event.tasklet;
710
711 if (t)
712 tasklet_free(t);
713}
714
715/* Release all the tasklet attached to <h3> uni-streams */
716static void h3_uqs_tasklets_release(struct h3 *h3)
717{
718 h3_uqs_tasklet_release(&h3->rqpack_enc);
719 h3_uqs_tasklet_release(&h3->rqpack_dec);
720 h3_uqs_tasklet_release(&h3->rctrl);
721}
722
723/* Tasklet dedicated to h3 outgoing uni-streams */
724__maybe_unused
725static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state)
726{
727 struct h3_uqs *h3_uqs = ctx;
728 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
729
730 h3_uqs->cb(h3_uqs, h3);
731 return NULL;
732}
733
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500734/* Initialize <h3_uqs> uni-stream with <t> as tasklet */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100735static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3 *h3,
736 int (*cb)(struct h3_uqs *h3_uqs, void *ctx),
737 struct task *(*t)(struct task *, void *, unsigned int))
738{
739 h3_uqs->qcs = NULL;
740 h3_uqs->cb = cb;
741 h3_uqs->wait_event.tasklet = tasklet_new();
742 if (!h3_uqs->wait_event.tasklet)
743 return 0;
744
745 h3_uqs->wait_event.tasklet->process = t;
746 h3_uqs->wait_event.tasklet->context = h3_uqs;
747 return 1;
748
749 err:
750 tasklet_free(h3_uqs->wait_event.tasklet);
751 return 0;
752}
753
754static inline void h3_uqs_release(struct h3_uqs *h3_uqs)
755{
756 if (h3_uqs->qcs)
757 qcs_release(h3_uqs->qcs);
758}
759
760static inline void h3_uqs_release_all(struct h3 *h3)
761{
762 h3_uqs_tasklet_release(&h3->lctrl);
763 h3_uqs_release(&h3->lctrl);
764 h3_uqs_tasklet_release(&h3->lqpack_enc);
765 h3_uqs_release(&h3->lqpack_enc);
766 h3_uqs_tasklet_release(&h3->lqpack_dec);
767 h3_uqs_release(&h3->lqpack_dec);
768}
769
770/* Initialize the HTTP/3 context for <qcc> mux.
771 * Return 1 if succeeded, 0 if not.
772 */
773static int h3_init(struct qcc *qcc)
774{
775 struct h3 *h3;
776
777 h3 = pool_alloc(pool_head_h3);
778 if (!h3)
779 goto fail_no_h3;
780
781 h3->qcc = qcc;
782 h3->err = H3_NO_ERROR;
783 h3->flags = 0;
784
785 if (!h3_uqs_init(&h3->rqpack_enc, h3, NULL, h3_uqs_task) ||
786 !h3_uqs_init(&h3->rqpack_dec, h3, NULL, h3_uqs_task) ||
787 !h3_uqs_init(&h3->rctrl, h3, h3_control_recv, h3_uqs_task))
788 goto fail_no_h3_ruqs;
789
790 if (!h3_uqs_init(&h3->lctrl, h3, h3_control_send, h3_uqs_task) ||
791 !h3_uqs_init(&h3->lqpack_enc, h3, NULL, h3_uqs_task) ||
792 !h3_uqs_init(&h3->lqpack_dec, h3, NULL, h3_uqs_task))
793 goto fail_no_h3_luqs;
794
795 qcc->ctx = h3;
796 LIST_INIT(&h3->buf_wait.list);
797
798 return 1;
799
800 fail_no_h3_ruqs:
801 h3_uqs_release_all(h3);
802 fail_no_h3_luqs:
803 h3_uqs_tasklets_release(h3);
804 pool_free(pool_head_h3, h3);
805 fail_no_h3:
806 return 0;
807}
808
809/* HTTP/3 application layer operations */
810const struct qcc_app_ops h3_ops = {
811 .init = h3_init,
812 .attach_ruqs = h3_attach_ruqs,
813 .decode_qcs = h3_decode_qcs,
814 .finalize = h3_finalize,
815};