blob: 79d746001571085a754b6fba36e92c3a8272a50a [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>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010026#include <haproxy/mux_quic-t.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010027#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
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +010096/* Decode <qcs> remotely initiated bidi-stream.
97 * Returns <0 on error else 0.
98 */
Amaury Denoyelledb443382021-11-30 11:23:29 +010099static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100100{
101 struct buffer *rxbuf = &qcs->rx.buf;
102 struct h3 *h3 = ctx;
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100103 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200104 struct htx_sl *sl;
Amaury Denoyelle99043552021-08-24 15:36:02 +0200105 struct conn_stream *cs;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200106 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200107 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200108 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100109
110 h3_debug_printf(stderr, "%s: STREAM ID: %llu\n", __func__, qcs->by_id.key);
111 if (!b_data(rxbuf))
112 return 0;
113
114 while (b_data(rxbuf)) {
115 size_t hlen;
116 uint64_t ftype, flen;
117 struct buffer b;
118
119 /* Work on a copy of <rxbuf> */
120 b = h3_b_dup(rxbuf);
121 hlen = h3_decode_frm_header(&ftype, &flen, &b);
122 if (!hlen)
123 break;
124
125 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
126 (unsigned long long)ftype, (unsigned long long)flen);
127 if (flen > b_data(&b))
128 break;
129
130 b_del(rxbuf, hlen);
131 switch (ftype) {
132 case H3_FT_DATA:
133 break;
134 case H3_FT_HEADERS:
135 {
136 const unsigned char *buf = (const unsigned char *)b_head(rxbuf);
137 size_t len = b_data(rxbuf);
Amaury Denoyelle3cae4042021-11-08 08:57:18 +0100138 struct buffer htx_buf = BUF_NULL;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100139 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200140 struct ist meth = IST_NULL, path = IST_NULL;
Amaury Denoyelle3cae4042021-11-08 08:57:18 +0100141 //struct ist scheme = IST_NULL, authority = IST_NULL;
142 struct ist authority = IST_NULL;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100143
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200144 if (qpack_decode_fs(buf, len, tmp, list) < 0) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100145 h3->err = QPACK_DECOMPRESSION_FAILED;
146 return -1;
147 }
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200148
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200149 b_alloc(&htx_buf);
150 htx = htx_from_buf(&htx_buf);
151
152 /* first treat pseudo-header to build the start line */
153 hdr_idx = 0;
154 while (1) {
155 if (isteq(list[hdr_idx].n, ist("")))
156 break;
157
158 if (istmatch(list[hdr_idx].n, ist(":"))) {
159 /* pseudo-header */
160 if (isteq(list[hdr_idx].n, ist(":method")))
161 meth = list[hdr_idx].v;
162 else if (isteq(list[hdr_idx].n, ist(":path")))
163 path = list[hdr_idx].v;
Amaury Denoyelle3cae4042021-11-08 08:57:18 +0100164 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
165 // scheme = list[hdr_idx].v;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200166 else if (isteq(list[hdr_idx].n, ist(":authority")))
167 authority = list[hdr_idx].v;
168 }
169
170 ++hdr_idx;
171 }
172
173 flags |= HTX_SL_F_VER_11;
174
175 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100176 if (!sl)
177 goto fail;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200178 sl->flags |= HTX_SL_F_BODYLESS;
179 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
180 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
181
182 if (isttest(authority))
183 htx_add_header(htx, ist("host"), authority);
184
185 /* now treat standard headers */
186 hdr_idx = 0;
187 while (1) {
188 if (isteq(list[hdr_idx].n, ist("")))
189 break;
190
191 if (!istmatch(list[hdr_idx].n, ist(":")))
192 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
193
194 ++hdr_idx;
195 }
196
197 htx_add_endof(htx, HTX_BLK_EOH);
198 htx_to_buf(htx, &htx_buf);
Amaury Denoyelle99043552021-08-24 15:36:02 +0200199
200 cs = cs_new(qcs->qcc->conn, qcs->qcc->conn->target);
201 cs->ctx = qcs;
202 stream_create_from_cs(cs, &htx_buf);
203
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500204 /* buffer is transferred to conn_stream and set to NULL
Amaury Denoyelle99043552021-08-24 15:36:02 +0200205 * except on stream creation error.
206 */
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200207 b_free(&htx_buf);
208
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100209 break;
210 }
211 case H3_FT_PUSH_PROMISE:
212 /* Not supported */
213 break;
214 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100215 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
216 * unknown frame types MUST be ignored
217 */
218 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100219 }
220 b_del(rxbuf, flen);
221 }
222
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100223 if (htx) {
224 if (fin && !b_data(rxbuf))
225 htx->flags |= HTX_FL_EOM;
226 }
Amaury Denoyelledb443382021-11-30 11:23:29 +0100227
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100228 return 0;
229
230 fail:
231 return -1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100232}
233
234/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
235 * <rxbuf> buffer. This function does not update this buffer.
236 * Returns 0 if something wrong happened, 1 if not.
237 */
238static int h3_parse_settings_frm(struct h3 *h3, const struct buffer *rxbuf, size_t flen)
239{
240 uint64_t id, value;
241 const unsigned char *buf, *end;
242
243 buf = (const unsigned char *)b_head(rxbuf);
244 end = buf + flen;
245
246 while (buf <= end) {
247 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
248 return 0;
249
250 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
251 __func__, (unsigned long long)id, (unsigned long long)value);
252 switch (id) {
253 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
254 h3->qpack_max_table_capacity = value;
255 break;
256 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
257 h3->max_field_section_size = value;
258 break;
259 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
260 h3->qpack_blocked_streams = value;
261 break;
262 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
263 h3->err = H3_SETTINGS_ERROR;
264 return 0;
265 default:
266 /* MUST be ignored */
267 break;
268 }
269 }
270
271 return 1;
272}
273
274/* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as
275 * there is not enough received data.
276 * Returns 0 if something wrong happened, 1 if not.
277 */
278static int h3_control_recv(struct h3_uqs *h3_uqs, void *ctx)
279{
280 struct buffer *rxbuf = &h3_uqs->qcs->rx.buf;
281 struct h3 *h3 = ctx;
282
283 h3_debug_printf(stderr, "%s STREAM ID: %llu\n", __func__, h3_uqs->qcs->by_id.key);
284 if (!b_data(rxbuf))
285 return 1;
286
287 while (b_data(rxbuf)) {
288 size_t hlen;
289 uint64_t ftype, flen;
290 struct buffer b;
291
292 /* Work on a copy of <rxbuf> */
293 b = h3_b_dup(rxbuf);
294 hlen = h3_decode_frm_header(&ftype, &flen, &b);
295 if (!hlen)
296 break;
297
298 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
299 (unsigned long long)ftype, (unsigned long long)flen);
300 if (flen > b_data(&b))
301 break;
302
303 b_del(rxbuf, hlen);
304 /* From here, a frame must not be truncated */
305 switch (ftype) {
306 case H3_FT_CANCEL_PUSH:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100307 /* XXX TODO XXX */
308 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100309 break;
310 case H3_FT_SETTINGS:
311 if (!h3_parse_settings_frm(h3, rxbuf, flen))
312 return 0;
313 break;
314 case H3_FT_GOAWAY:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100315 /* XXX TODO XXX */
316 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100317 break;
318 case H3_FT_MAX_PUSH_ID:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100319 /* XXX TODO XXX */
320 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100321 break;
322 default:
323 /* Error */
324 h3->err = H3_FRAME_UNEXPECTED;
325 return 0;
326 }
327 b_del(rxbuf, flen);
328 }
329
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100330 /* Handle the case where remaining data are present in the buffer. This
331 * can happen if there is an incomplete frame. In this case, subscribe
332 * on the lower layer to restart receive operation.
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100333 */
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100334 if (b_data(rxbuf))
335 qcs_subscribe(h3_uqs->qcs, SUB_RETRY_RECV, &h3_uqs->wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100336
337 return 1;
338}
339
Amaury Denoyellea5871362021-10-07 16:26:12 +0200340/* Returns buffer for data sending.
341 * May be NULL if the allocation failed.
342 */
343static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100344{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200345 if (!b_size(&qcs->tx.buf))
346 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100347
Amaury Denoyellea5871362021-10-07 16:26:12 +0200348 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100349}
350
351/* Function used to emit stream data from <h3_uqs> control uni-stream */
352static int h3_control_send(struct h3_uqs *h3_uqs, void *ctx)
353{
354 int ret;
355 struct h3 *h3 = ctx;
356 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200357 struct buffer pos, *res;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100358
359 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200360 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100361 if (!(h3->flags & H3_CF_SETTINGS_SENT)) {
362 struct qcs *qcs = h3_uqs->qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100363 size_t frm_len;
364
365 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
366 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
367 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
368 quic_int_getsize(h3_settings_qpack_blocked_streams);
369 if (h3_settings_max_field_section_size) {
370 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
371 quic_int_getsize(h3_settings_max_field_section_size);
372 }
373
Amaury Denoyellea5871362021-10-07 16:26:12 +0200374 b_quic_enc_int(&pos, H3_UNI_STRM_TP_CONTROL_STREAM);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100375 /* Build a SETTINGS frame */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200376 b_quic_enc_int(&pos, H3_FT_SETTINGS);
377 b_quic_enc_int(&pos, frm_len);
378 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
379 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
380 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
381 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100382 if (h3_settings_max_field_section_size) {
Amaury Denoyellea5871362021-10-07 16:26:12 +0200383 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
384 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100385 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200386
387 res = mux_get_buf(qcs);
388 if (b_room(res) < b_data(&pos)) {
389 // TODO the mux should be put in blocked state, with
390 // the stream in state waiting for settings to be sent
391 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100392 }
393
Amaury Denoyellea5871362021-10-07 16:26:12 +0200394 ret = b_force_xfer(res, &pos, b_data(&pos));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100395 if (ret > 0) {
396 h3->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200397 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
398 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100399 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100400 }
401
402 return ret;
403}
404
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200405static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
406{
407 struct buffer outbuf;
408 struct buffer headers_buf = BUF_NULL;
409 struct buffer *res;
410 struct http_hdr list[global.tune.max_http_hdr];
411 struct htx_sl *sl;
412 struct htx_blk *blk;
413 enum htx_blk_type type;
414 int frame_length_size; /* size in bytes of frame length varint field */
415 int ret = 0;
416 int hdr;
417 int status = 0;
418
419 sl = NULL;
420 hdr = 0;
421 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
422 type = htx_get_blk_type(blk);
423
424 if (type == HTX_BLK_UNUSED)
425 continue;
426
427 if (type == HTX_BLK_EOH)
428 break;
429
430 if (type == HTX_BLK_RES_SL) {
431 /* start-line -> HEADERS h3 frame */
432 BUG_ON(sl);
433 sl = htx_get_blk_ptr(htx, blk);
434 /* TODO should be on h3 layer */
435 status = sl->info.res.status;
436 }
437 else if (type == HTX_BLK_HDR) {
438 list[hdr].n = htx_get_blk_name(htx, blk);
439 list[hdr].v = htx_get_blk_value(htx, blk);
440 hdr++;
441 }
442 else {
443 ABORT_NOW();
444 goto err;
445 }
446 }
447
448 BUG_ON(!sl);
449
450 list[hdr].n = ist("");
451
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200452 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200453
454 /* At least 5 bytes to store frame type + length as a varint max size */
455 if (b_room(res) < 5)
456 ABORT_NOW();
457
458 b_reset(&outbuf);
459 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
460 /* Start the headers after frame type + length */
461 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
462
463 if (qpack_encode_field_section_line(&headers_buf))
464 ABORT_NOW();
465 if (qpack_encode_int_status(&headers_buf, status))
466 ABORT_NOW();
467
468 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
469 if (isteq(list[hdr].n, ist("")))
470 break;
471
472 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
473 ABORT_NOW();
474 }
475
476 /* Now that all headers are encoded, we are certain that res buffer is
477 * big enough
478 */
479 frame_length_size = quic_int_getsize(b_data(&headers_buf));
480 res->head += 4 - frame_length_size;
481 b_putchr(res, 0x01); /* h3 HEADERS frame type */
482 if (!b_quic_enc_int(res, b_data(&headers_buf)))
483 ABORT_NOW();
484 b_add(res, b_data(&headers_buf));
485
486 ret = 0;
487 blk = htx_get_head_blk(htx);
488 while (blk) {
489 type = htx_get_blk_type(blk);
490 ret += htx_get_blksz(blk);
491 blk = htx_remove_blk(htx, blk);
492 if (type == HTX_BLK_EOH)
493 break;
494 }
495
496 return ret;
497
498 err:
499 return 0;
500}
501
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200502/* Returns the total of bytes sent. */
503static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
504{
505 struct buffer outbuf;
506 struct buffer *res;
507 size_t total = 0;
508 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200509 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200510 struct htx_blk *blk;
511 enum htx_blk_type type;
512
513 htx = htx_from_buf(buf);
514
515 new_frame:
516 if (!count || htx_is_empty(htx))
517 goto end;
518
519 blk = htx_get_head_blk(htx);
520 type = htx_get_blk_type(blk);
521 fsize = bsize = htx_get_blksz(blk);
522
523 if (type != HTX_BLK_DATA)
524 goto end;
525
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200526 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200527
528 if (fsize > count)
529 fsize = count;
530
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200531 /* h3 DATA headers : 1-byte frame type + varint frame length */
532 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200533
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200534 while (1) {
535 b_reset(&outbuf);
536 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
537 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
538 break;
539 b_slow_realign(res, trash.area, b_data(res));
540 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200541
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100542 /* Not enough room for headers and at least one data byte, block the
543 * stream. It is expected that the conn-stream layer will subscribe on
544 * SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200545 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100546 if (b_size(&outbuf) <= hsize) {
547 qcs->flags |= QC_SF_BLK_MROOM;
548 goto end;
549 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200550
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200551 if (b_size(&outbuf) < hsize + fsize)
552 fsize = b_size(&outbuf) - hsize;
553 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200554
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200555 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
556 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
557
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200558 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200559 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200560 count -= fsize;
561
562 if (fsize == bsize)
563 htx_remove_blk(htx, blk);
564 else
565 htx_cut_data_blk(htx, blk, fsize);
566
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200567 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200568 b_add(res, b_data(&outbuf));
569 goto new_frame;
570
571 end:
572 return total;
573}
574
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200575size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
576{
577 size_t total = 0;
578 struct qcs *qcs = cs->ctx;
579 struct htx *htx;
580 enum htx_blk_type btype;
581 struct htx_blk *blk;
582 uint32_t bsize;
583 int32_t idx;
584 int ret;
585
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100586 fprintf(stderr, "%s\n", __func__);
587
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200588 htx = htx_from_buf(buf);
589
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100590 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200591 idx = htx_get_head(htx);
592 blk = htx_get_blk(htx, idx);
593 btype = htx_get_blk_type(blk);
594 bsize = htx_get_blksz(blk);
595
596 /* Not implemented : QUIC on backend side */
597 BUG_ON(btype == HTX_BLK_REQ_SL);
598
599 switch (btype) {
600 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200601 /* start-line -> HEADERS h3 frame */
602 ret = h3_resp_headers_send(qcs, htx);
603 if (ret > 0) {
604 total += ret;
605 count -= ret;
606 if (ret < bsize)
607 goto out;
608 }
609 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200610
611 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200612 ret = h3_resp_data_send(qcs, buf, count);
613 if (ret > 0) {
614 htx = htx_from_buf(buf);
615 total += ret;
616 count -= ret;
617 if (ret < bsize)
618 goto out;
619 }
620 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200621
622 case HTX_BLK_TLR:
623 case HTX_BLK_EOT:
624 /* TODO trailers */
625
626 default:
627 htx_remove_blk(htx, blk);
628 total += bsize;
629 count -= bsize;
630 break;
631 }
632 }
633
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100634 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
635 qcs->flags |= QC_SF_FIN_STREAM;
636
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200637 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200638 if (total) {
639 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
640 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
641 }
642
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200643 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200644}
645
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100646/* Finalize the initialization of remotely initiated uni-stream <qcs>.
647 * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error
648 * to inform the QUIC mux layer of the encountered error.
649 */
650static int h3_attach_ruqs(struct qcs *qcs, void *ctx)
651{
652 uint64_t strm_type;
653 struct h3 *h3 = ctx;
654 struct buffer *rxbuf = &qcs->rx.buf;
655
656 /* First octets: the uni-stream type */
657 if (!b_quic_dec_int(&strm_type, rxbuf, NULL) || strm_type > H3_UNI_STRM_TP_MAX)
658 return 0;
659
660 /* Note that for all the uni-streams below, this is an error to receive two times the
661 * same type of uni-stream (even for Push stream which is not supported at this time.
662 */
663 switch (strm_type) {
664 case H3_UNI_STRM_TP_CONTROL_STREAM:
665 if (h3->rctrl.qcs) {
666 h3->err = H3_STREAM_CREATION_ERROR;
667 return 0;
668 }
669
670 h3->rctrl.qcs = qcs;
671 h3->rctrl.cb = h3_control_recv;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100672 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100673 break;
674 case H3_UNI_STRM_TP_PUSH_STREAM:
675 /* NOT SUPPORTED */
676 break;
677 case H3_UNI_STRM_TP_QPACK_ENCODER:
678 if (h3->rqpack_enc.qcs) {
679 h3->err = H3_STREAM_CREATION_ERROR;
680 return 0;
681 }
682
683 h3->rqpack_enc.qcs = qcs;
684 h3->rqpack_enc.cb = qpack_decode_enc;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100685 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_enc.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100686 break;
687 case H3_UNI_STRM_TP_QPACK_DECODER:
688 if (h3->rqpack_dec.qcs) {
689 h3->err = H3_STREAM_CREATION_ERROR;
690 return 0;
691 }
692
693 h3->rqpack_dec.qcs = qcs;
694 h3->rqpack_dec.cb = qpack_decode_dec;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100695 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_dec.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100696 break;
697 default:
698 /* Error */
699 h3->err = H3_STREAM_CREATION_ERROR;
700 return 0;
701 }
702
703 return 1;
704}
705
706static int h3_finalize(void *ctx)
707{
708 struct h3 *h3 = ctx;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100709 int lctrl_id;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100710
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100711 lctrl_id = qcs_get_next_id(h3->qcc, QCS_SRV_UNI);
712 h3->lctrl.qcs = qcs_new(h3->qcc, lctrl_id, QCS_SRV_UNI);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100713 if (!h3->lctrl.qcs)
714 return 0;
715
716 /* Wakeup ->lctrl uni-stream */
Frédéric Lécaillee16f0bd2021-08-23 09:50:29 +0200717 h3_control_send(&h3->lctrl, h3);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100718
719 return 1;
720}
721
722/* Tasklet dedicated to h3 incoming uni-streams */
723static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state)
724{
725 struct h3_uqs *h3_uqs = ctx;
726 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
727
728 h3_uqs->cb(h3_uqs, h3);
729 return NULL;
730}
731
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100732/* Release all the tasklet attached to <h3_uqs> uni-stream */
733static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs)
734{
735 struct tasklet *t = h3_uqs->wait_event.tasklet;
736
737 if (t)
738 tasklet_free(t);
739}
740
741/* Release all the tasklet attached to <h3> uni-streams */
742static void h3_uqs_tasklets_release(struct h3 *h3)
743{
744 h3_uqs_tasklet_release(&h3->rqpack_enc);
745 h3_uqs_tasklet_release(&h3->rqpack_dec);
746 h3_uqs_tasklet_release(&h3->rctrl);
747}
748
749/* Tasklet dedicated to h3 outgoing uni-streams */
750__maybe_unused
751static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state)
752{
753 struct h3_uqs *h3_uqs = ctx;
754 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
755
756 h3_uqs->cb(h3_uqs, h3);
757 return NULL;
758}
759
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500760/* Initialize <h3_uqs> uni-stream with <t> as tasklet */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100761static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3 *h3,
762 int (*cb)(struct h3_uqs *h3_uqs, void *ctx),
763 struct task *(*t)(struct task *, void *, unsigned int))
764{
765 h3_uqs->qcs = NULL;
766 h3_uqs->cb = cb;
767 h3_uqs->wait_event.tasklet = tasklet_new();
768 if (!h3_uqs->wait_event.tasklet)
769 return 0;
770
771 h3_uqs->wait_event.tasklet->process = t;
772 h3_uqs->wait_event.tasklet->context = h3_uqs;
773 return 1;
774
775 err:
776 tasklet_free(h3_uqs->wait_event.tasklet);
777 return 0;
778}
779
780static inline void h3_uqs_release(struct h3_uqs *h3_uqs)
781{
782 if (h3_uqs->qcs)
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100783 uni_qcs_free(h3_uqs->qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100784}
785
786static inline void h3_uqs_release_all(struct h3 *h3)
787{
788 h3_uqs_tasklet_release(&h3->lctrl);
789 h3_uqs_release(&h3->lctrl);
790 h3_uqs_tasklet_release(&h3->lqpack_enc);
791 h3_uqs_release(&h3->lqpack_enc);
792 h3_uqs_tasklet_release(&h3->lqpack_dec);
793 h3_uqs_release(&h3->lqpack_dec);
794}
795
796/* Initialize the HTTP/3 context for <qcc> mux.
797 * Return 1 if succeeded, 0 if not.
798 */
799static int h3_init(struct qcc *qcc)
800{
801 struct h3 *h3;
802
803 h3 = pool_alloc(pool_head_h3);
804 if (!h3)
805 goto fail_no_h3;
806
807 h3->qcc = qcc;
808 h3->err = H3_NO_ERROR;
809 h3->flags = 0;
810
811 if (!h3_uqs_init(&h3->rqpack_enc, h3, NULL, h3_uqs_task) ||
812 !h3_uqs_init(&h3->rqpack_dec, h3, NULL, h3_uqs_task) ||
813 !h3_uqs_init(&h3->rctrl, h3, h3_control_recv, h3_uqs_task))
814 goto fail_no_h3_ruqs;
815
816 if (!h3_uqs_init(&h3->lctrl, h3, h3_control_send, h3_uqs_task) ||
817 !h3_uqs_init(&h3->lqpack_enc, h3, NULL, h3_uqs_task) ||
818 !h3_uqs_init(&h3->lqpack_dec, h3, NULL, h3_uqs_task))
819 goto fail_no_h3_luqs;
820
821 qcc->ctx = h3;
822 LIST_INIT(&h3->buf_wait.list);
823
824 return 1;
825
826 fail_no_h3_ruqs:
827 h3_uqs_release_all(h3);
828 fail_no_h3_luqs:
829 h3_uqs_tasklets_release(h3);
830 pool_free(pool_head_h3, h3);
831 fail_no_h3:
832 return 0;
833}
834
835/* HTTP/3 application layer operations */
836const struct qcc_app_ops h3_ops = {
837 .init = h3_init,
838 .attach_ruqs = h3_attach_ruqs,
839 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +0100840 .snd_buf = h3_snd_buf,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100841 .finalize = h3_finalize,
842};