blob: 31b9c00d72bb7642373904cbfd30c80a509a86f2 [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 Denoyelledeed7772021-12-03 11:36:46 +010027#include <haproxy/mux_quic-t.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>
Amaury Denoyelle99043552021-08-24 15:36:02 +020032#include <haproxy/stream.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010033#include <haproxy/tools.h>
34#include <haproxy/xprt_quic.h>
35
36#define DEBUG_H3
37
38#if defined(DEBUG_H3)
39#define h3_debug_printf fprintf
40#define h3_debug_hexdump debug_hexdump
41#else
42#define h3_debug_printf(...) do { } while (0)
43#define h3_debug_hexdump(...) do { } while (0)
44#endif
45
46#define H3_CF_SETTINGS_SENT 0x00000001
47
48/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +020049static uint64_t h3_settings_qpack_max_table_capacity = 0;
50static uint64_t h3_settings_qpack_blocked_streams = 4096;
51static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010052
53struct h3 {
54 struct qcc *qcc;
55 enum h3_err err;
56 uint32_t flags;
57 /* Locally initiated uni-streams */
58 struct h3_uqs lqpack_enc;
59 struct h3_uqs lqpack_dec;
60 struct h3_uqs lctrl;
61 /* Remotely initiated uni-streams */
62 struct h3_uqs rqpack_enc;
63 struct h3_uqs rqpack_dec;
64 struct h3_uqs rctrl;
65 /* Settings */
66 uint64_t qpack_max_table_capacity;
67 uint64_t qpack_blocked_streams;
68 uint64_t max_field_section_size;
69 struct buffer_wait buf_wait; /* wait list for buffer allocations */
70};
71
72DECLARE_STATIC_POOL(pool_head_h3, "h3", sizeof(struct h3));
73
74/* Simple function to duplicate a buffer */
75static inline struct buffer h3_b_dup(struct buffer *b)
76{
77 return b_make(b->area, b->size, b->head, b->data);
78}
79
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010080/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
81 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
82 * Note that this function update <b> buffer to reflect the number of bytes consumed
83 * to decode the h3 frame header.
84 */
85static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
86 struct buffer *b)
87{
88 size_t hlen;
89
90 hlen = 0;
91 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
92 return 0;
93
94 return hlen;
95}
96
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +010097/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
98 * in a local HTX buffer and transfer to the conn-stream layer. <fin> must be
99 * set if this is the last data to transfer from this stream.
100 *
101 * Returns 0 on success else non-zero.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100102 */
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100103static int h3_headers_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
104 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100105{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100106 struct buffer htx_buf = BUF_NULL;
107 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100108 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200109 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200110 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100111 struct conn_stream *cs;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200112 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100113 struct ist meth = IST_NULL, path = IST_NULL;
114 //struct ist scheme = IST_NULL, authority = IST_NULL;
115 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200116 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100117
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100118 if (qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp, list) < 0)
119 return 1;
120
121 qc_get_buf(qcs, &htx_buf);
122 BUG_ON(!b_size(&htx_buf));
123 htx = htx_from_buf(&htx_buf);
124
125 /* first treat pseudo-header to build the start line */
126 hdr_idx = 0;
127 while (1) {
128 if (isteq(list[hdr_idx].n, ist("")))
129 break;
130
131 if (istmatch(list[hdr_idx].n, ist(":"))) {
132 /* pseudo-header */
133 if (isteq(list[hdr_idx].n, ist(":method")))
134 meth = list[hdr_idx].v;
135 else if (isteq(list[hdr_idx].n, ist(":path")))
136 path = list[hdr_idx].v;
137 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
138 // scheme = list[hdr_idx].v;
139 else if (isteq(list[hdr_idx].n, ist(":authority")))
140 authority = list[hdr_idx].v;
141 }
142
143 ++hdr_idx;
144 }
145
146 flags |= HTX_SL_F_VER_11;
147
148 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
149 if (!sl)
150 return 1;
151
152 if (fin)
153 sl->flags |= HTX_SL_F_BODYLESS;
154
155 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
156 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
157
158 if (isttest(authority))
159 htx_add_header(htx, ist("host"), authority);
160
161 /* now treat standard headers */
162 hdr_idx = 0;
163 while (1) {
164 if (isteq(list[hdr_idx].n, ist("")))
165 break;
166
167 if (!istmatch(list[hdr_idx].n, ist(":")))
168 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
169
170 ++hdr_idx;
171 }
172
173 htx_add_endof(htx, HTX_BLK_EOH);
174 htx_to_buf(htx, &htx_buf);
175
176 if (fin)
177 htx->flags |= HTX_FL_EOM;
178
179 cs = cs_new(qcs->qcc->conn, qcs->qcc->conn->target);
Frédéric Lécaille59509b52022-02-15 09:25:06 +0100180 if (!cs)
181 return 1;
182
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100183 cs->flags |= CS_FL_NOT_FIRST;
184 cs->ctx = qcs;
185 stream_create_from_cs(cs, &htx_buf);
186
187 /* buffer is transferred to conn_stream and set to NULL
188 * except on stream creation error.
189 */
190 b_free(&htx_buf);
191 offer_buffers(NULL, 1);
192
193 return 0;
194}
195
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100196/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
197 * HTX buffer. <fin> must be set if this is the last data to transfer from this
198 * stream.
199 *
200 * Returns 0 on success else non-zero
201 */
202static int h3_data_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
203 char fin)
204{
205 struct buffer *appbuf;
206 struct htx *htx = NULL;
207 size_t htx_sent;
208 int htx_space;
209
210 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
211 BUG_ON(!appbuf);
212 htx = htx_from_buf(appbuf);
213
214 htx_space = htx_free_data_space(htx);
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100215 if (!htx_space || htx_space < len) {
216 ABORT_NOW(); /* TODO handle this case properly */
217 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100218
219 htx_sent = htx_add_data(htx, ist2(b_head(buf), len));
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100220 if (htx_sent < len) {
221 ABORT_NOW(); /* TODO handle this case properly */
222 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100223
224 if (fin)
225 htx->flags |= HTX_FL_EOM;
226 htx_to_buf(htx, appbuf);
227
228 return 0;
229}
230
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100231/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
232 * that we received the last data of the stream.
233 * Returns <0 on error else 0.
234 */
235static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
236{
237 struct buffer *rxbuf = &qcs->rx.buf;
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100238 int ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100239
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100240 h3_debug_printf(stderr, "%s: STREAM ID: %llu\n", __func__, qcs->by_id.key);
241 if (!b_data(rxbuf))
242 return 0;
243
244 while (b_data(rxbuf)) {
245 size_t hlen;
246 uint64_t ftype, flen;
247 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100248 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100249
250 /* Work on a copy of <rxbuf> */
251 b = h3_b_dup(rxbuf);
252 hlen = h3_decode_frm_header(&ftype, &flen, &b);
253 if (!hlen)
254 break;
255
256 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
257 (unsigned long long)ftype, (unsigned long long)flen);
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100258 if (flen > b_data(&b) && !b_full(rxbuf))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100259 break;
260
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100261 /* TODO handle full rxbuf */
262 BUG_ON(flen > b_size(rxbuf));
263
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100264 b_del(rxbuf, hlen);
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100265 last_stream_frame = (fin && flen == b_data(rxbuf));
266
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100267 switch (ftype) {
268 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100269 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
270 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100271 if (ret) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100272 break;
273 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100274 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
275 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100276 if (ret) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100277 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100278 case H3_FT_PUSH_PROMISE:
279 /* Not supported */
280 break;
281 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100282 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
283 * unknown frame types MUST be ignored
284 */
285 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100286 }
287 b_del(rxbuf, flen);
288 }
289
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100290 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100291}
292
293/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
294 * <rxbuf> buffer. This function does not update this buffer.
295 * Returns 0 if something wrong happened, 1 if not.
296 */
297static int h3_parse_settings_frm(struct h3 *h3, const struct buffer *rxbuf, size_t flen)
298{
299 uint64_t id, value;
300 const unsigned char *buf, *end;
301
302 buf = (const unsigned char *)b_head(rxbuf);
303 end = buf + flen;
304
305 while (buf <= end) {
306 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
307 return 0;
308
309 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
310 __func__, (unsigned long long)id, (unsigned long long)value);
311 switch (id) {
312 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
313 h3->qpack_max_table_capacity = value;
314 break;
315 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
316 h3->max_field_section_size = value;
317 break;
318 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
319 h3->qpack_blocked_streams = value;
320 break;
321 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
322 h3->err = H3_SETTINGS_ERROR;
323 return 0;
324 default:
325 /* MUST be ignored */
326 break;
327 }
328 }
329
330 return 1;
331}
332
333/* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as
334 * there is not enough received data.
335 * Returns 0 if something wrong happened, 1 if not.
336 */
337static int h3_control_recv(struct h3_uqs *h3_uqs, void *ctx)
338{
339 struct buffer *rxbuf = &h3_uqs->qcs->rx.buf;
340 struct h3 *h3 = ctx;
341
342 h3_debug_printf(stderr, "%s STREAM ID: %llu\n", __func__, h3_uqs->qcs->by_id.key);
343 if (!b_data(rxbuf))
344 return 1;
345
346 while (b_data(rxbuf)) {
347 size_t hlen;
348 uint64_t ftype, flen;
349 struct buffer b;
350
351 /* Work on a copy of <rxbuf> */
352 b = h3_b_dup(rxbuf);
353 hlen = h3_decode_frm_header(&ftype, &flen, &b);
354 if (!hlen)
355 break;
356
357 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
358 (unsigned long long)ftype, (unsigned long long)flen);
359 if (flen > b_data(&b))
360 break;
361
362 b_del(rxbuf, hlen);
363 /* From here, a frame must not be truncated */
364 switch (ftype) {
365 case H3_FT_CANCEL_PUSH:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100366 /* XXX TODO XXX */
367 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100368 break;
369 case H3_FT_SETTINGS:
370 if (!h3_parse_settings_frm(h3, rxbuf, flen))
371 return 0;
372 break;
373 case H3_FT_GOAWAY:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100374 /* XXX TODO XXX */
375 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100376 break;
377 case H3_FT_MAX_PUSH_ID:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100378 /* XXX TODO XXX */
379 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100380 break;
381 default:
382 /* Error */
383 h3->err = H3_FRAME_UNEXPECTED;
384 return 0;
385 }
386 b_del(rxbuf, flen);
387 }
388
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100389 /* Handle the case where remaining data are present in the buffer. This
390 * can happen if there is an incomplete frame. In this case, subscribe
391 * on the lower layer to restart receive operation.
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100392 */
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100393 if (b_data(rxbuf))
394 qcs_subscribe(h3_uqs->qcs, SUB_RETRY_RECV, &h3_uqs->wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100395
396 return 1;
397}
398
Amaury Denoyellea5871362021-10-07 16:26:12 +0200399/* Returns buffer for data sending.
400 * May be NULL if the allocation failed.
401 */
402static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100403{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200404 if (!b_size(&qcs->tx.buf))
405 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100406
Amaury Denoyellea5871362021-10-07 16:26:12 +0200407 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100408}
409
410/* Function used to emit stream data from <h3_uqs> control uni-stream */
411static int h3_control_send(struct h3_uqs *h3_uqs, void *ctx)
412{
413 int ret;
414 struct h3 *h3 = ctx;
415 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200416 struct buffer pos, *res;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100417
418 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200419 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100420 if (!(h3->flags & H3_CF_SETTINGS_SENT)) {
421 struct qcs *qcs = h3_uqs->qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100422 size_t frm_len;
423
424 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
425 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
426 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
427 quic_int_getsize(h3_settings_qpack_blocked_streams);
428 if (h3_settings_max_field_section_size) {
429 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
430 quic_int_getsize(h3_settings_max_field_section_size);
431 }
432
Amaury Denoyellea5871362021-10-07 16:26:12 +0200433 b_quic_enc_int(&pos, H3_UNI_STRM_TP_CONTROL_STREAM);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100434 /* Build a SETTINGS frame */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200435 b_quic_enc_int(&pos, H3_FT_SETTINGS);
436 b_quic_enc_int(&pos, frm_len);
437 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
438 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
439 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
440 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100441 if (h3_settings_max_field_section_size) {
Amaury Denoyellea5871362021-10-07 16:26:12 +0200442 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
443 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100444 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200445
446 res = mux_get_buf(qcs);
447 if (b_room(res) < b_data(&pos)) {
448 // TODO the mux should be put in blocked state, with
449 // the stream in state waiting for settings to be sent
450 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100451 }
452
Amaury Denoyellea5871362021-10-07 16:26:12 +0200453 ret = b_force_xfer(res, &pos, b_data(&pos));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100454 if (ret > 0) {
455 h3->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200456 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
457 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100458 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100459 }
460
461 return ret;
462}
463
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200464static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
465{
466 struct buffer outbuf;
467 struct buffer headers_buf = BUF_NULL;
468 struct buffer *res;
469 struct http_hdr list[global.tune.max_http_hdr];
470 struct htx_sl *sl;
471 struct htx_blk *blk;
472 enum htx_blk_type type;
473 int frame_length_size; /* size in bytes of frame length varint field */
474 int ret = 0;
475 int hdr;
476 int status = 0;
477
478 sl = NULL;
479 hdr = 0;
480 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
481 type = htx_get_blk_type(blk);
482
483 if (type == HTX_BLK_UNUSED)
484 continue;
485
486 if (type == HTX_BLK_EOH)
487 break;
488
489 if (type == HTX_BLK_RES_SL) {
490 /* start-line -> HEADERS h3 frame */
491 BUG_ON(sl);
492 sl = htx_get_blk_ptr(htx, blk);
493 /* TODO should be on h3 layer */
494 status = sl->info.res.status;
495 }
496 else if (type == HTX_BLK_HDR) {
497 list[hdr].n = htx_get_blk_name(htx, blk);
498 list[hdr].v = htx_get_blk_value(htx, blk);
499 hdr++;
500 }
501 else {
502 ABORT_NOW();
503 goto err;
504 }
505 }
506
507 BUG_ON(!sl);
508
509 list[hdr].n = ist("");
510
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200511 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200512
513 /* At least 5 bytes to store frame type + length as a varint max size */
514 if (b_room(res) < 5)
515 ABORT_NOW();
516
517 b_reset(&outbuf);
518 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
519 /* Start the headers after frame type + length */
520 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
521
522 if (qpack_encode_field_section_line(&headers_buf))
523 ABORT_NOW();
524 if (qpack_encode_int_status(&headers_buf, status))
525 ABORT_NOW();
526
527 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
528 if (isteq(list[hdr].n, ist("")))
529 break;
530
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100531 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
532 * Transfer codings (see Section 6.1 of [HTTP11]) are not
533 * defined for HTTP/3; the Transfer-Encoding header field MUST
534 * NOT be used.
535 */
536 if (isteq(list[hdr].n, ist("transfer-encoding")))
537 continue;
538
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200539 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
540 ABORT_NOW();
541 }
542
543 /* Now that all headers are encoded, we are certain that res buffer is
544 * big enough
545 */
546 frame_length_size = quic_int_getsize(b_data(&headers_buf));
547 res->head += 4 - frame_length_size;
548 b_putchr(res, 0x01); /* h3 HEADERS frame type */
549 if (!b_quic_enc_int(res, b_data(&headers_buf)))
550 ABORT_NOW();
551 b_add(res, b_data(&headers_buf));
552
553 ret = 0;
554 blk = htx_get_head_blk(htx);
555 while (blk) {
556 type = htx_get_blk_type(blk);
557 ret += htx_get_blksz(blk);
558 blk = htx_remove_blk(htx, blk);
559 if (type == HTX_BLK_EOH)
560 break;
561 }
562
563 return ret;
564
565 err:
566 return 0;
567}
568
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200569/* Returns the total of bytes sent. */
570static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
571{
572 struct buffer outbuf;
573 struct buffer *res;
574 size_t total = 0;
575 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200576 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200577 struct htx_blk *blk;
578 enum htx_blk_type type;
579
580 htx = htx_from_buf(buf);
581
582 new_frame:
583 if (!count || htx_is_empty(htx))
584 goto end;
585
586 blk = htx_get_head_blk(htx);
587 type = htx_get_blk_type(blk);
588 fsize = bsize = htx_get_blksz(blk);
589
590 if (type != HTX_BLK_DATA)
591 goto end;
592
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200593 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200594
595 if (fsize > count)
596 fsize = count;
597
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200598 /* h3 DATA headers : 1-byte frame type + varint frame length */
599 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200600
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200601 while (1) {
602 b_reset(&outbuf);
603 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
604 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
605 break;
606 b_slow_realign(res, trash.area, b_data(res));
607 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200608
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100609 /* Not enough room for headers and at least one data byte, block the
610 * stream. It is expected that the conn-stream layer will subscribe on
611 * SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200612 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100613 if (b_size(&outbuf) <= hsize) {
614 qcs->flags |= QC_SF_BLK_MROOM;
615 goto end;
616 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200617
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200618 if (b_size(&outbuf) < hsize + fsize)
619 fsize = b_size(&outbuf) - hsize;
620 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200621
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200622 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
623 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
624
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200625 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200626 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200627 count -= fsize;
628
629 if (fsize == bsize)
630 htx_remove_blk(htx, blk);
631 else
632 htx_cut_data_blk(htx, blk, fsize);
633
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200634 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200635 b_add(res, b_data(&outbuf));
636 goto new_frame;
637
638 end:
639 return total;
640}
641
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200642size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
643{
644 size_t total = 0;
645 struct qcs *qcs = cs->ctx;
646 struct htx *htx;
647 enum htx_blk_type btype;
648 struct htx_blk *blk;
649 uint32_t bsize;
650 int32_t idx;
651 int ret;
652
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100653 fprintf(stderr, "%s\n", __func__);
654
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200655 htx = htx_from_buf(buf);
656
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100657 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200658 idx = htx_get_head(htx);
659 blk = htx_get_blk(htx, idx);
660 btype = htx_get_blk_type(blk);
661 bsize = htx_get_blksz(blk);
662
663 /* Not implemented : QUIC on backend side */
664 BUG_ON(btype == HTX_BLK_REQ_SL);
665
666 switch (btype) {
667 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200668 /* start-line -> HEADERS h3 frame */
669 ret = h3_resp_headers_send(qcs, htx);
670 if (ret > 0) {
671 total += ret;
672 count -= ret;
673 if (ret < bsize)
674 goto out;
675 }
676 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200677
678 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200679 ret = h3_resp_data_send(qcs, buf, count);
680 if (ret > 0) {
681 htx = htx_from_buf(buf);
682 total += ret;
683 count -= ret;
684 if (ret < bsize)
685 goto out;
686 }
687 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200688
689 case HTX_BLK_TLR:
690 case HTX_BLK_EOT:
691 /* TODO trailers */
692
693 default:
694 htx_remove_blk(htx, blk);
695 total += bsize;
696 count -= bsize;
697 break;
698 }
699 }
700
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100701 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
702 qcs->flags |= QC_SF_FIN_STREAM;
703
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200704 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200705 if (total) {
706 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
707 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
708 }
709
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200710 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200711}
712
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100713/* Finalize the initialization of remotely initiated uni-stream <qcs>.
714 * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error
715 * to inform the QUIC mux layer of the encountered error.
716 */
717static int h3_attach_ruqs(struct qcs *qcs, void *ctx)
718{
719 uint64_t strm_type;
720 struct h3 *h3 = ctx;
721 struct buffer *rxbuf = &qcs->rx.buf;
722
723 /* First octets: the uni-stream type */
724 if (!b_quic_dec_int(&strm_type, rxbuf, NULL) || strm_type > H3_UNI_STRM_TP_MAX)
725 return 0;
726
727 /* Note that for all the uni-streams below, this is an error to receive two times the
728 * same type of uni-stream (even for Push stream which is not supported at this time.
729 */
730 switch (strm_type) {
731 case H3_UNI_STRM_TP_CONTROL_STREAM:
732 if (h3->rctrl.qcs) {
733 h3->err = H3_STREAM_CREATION_ERROR;
734 return 0;
735 }
736
737 h3->rctrl.qcs = qcs;
738 h3->rctrl.cb = h3_control_recv;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100739 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100740 break;
741 case H3_UNI_STRM_TP_PUSH_STREAM:
742 /* NOT SUPPORTED */
743 break;
744 case H3_UNI_STRM_TP_QPACK_ENCODER:
745 if (h3->rqpack_enc.qcs) {
746 h3->err = H3_STREAM_CREATION_ERROR;
747 return 0;
748 }
749
750 h3->rqpack_enc.qcs = qcs;
751 h3->rqpack_enc.cb = qpack_decode_enc;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100752 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_enc.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100753 break;
754 case H3_UNI_STRM_TP_QPACK_DECODER:
755 if (h3->rqpack_dec.qcs) {
756 h3->err = H3_STREAM_CREATION_ERROR;
757 return 0;
758 }
759
760 h3->rqpack_dec.qcs = qcs;
761 h3->rqpack_dec.cb = qpack_decode_dec;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100762 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_dec.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100763 break;
764 default:
765 /* Error */
766 h3->err = H3_STREAM_CREATION_ERROR;
767 return 0;
768 }
769
770 return 1;
771}
772
773static int h3_finalize(void *ctx)
774{
775 struct h3 *h3 = ctx;
776
Amaury Denoyelleb7880542022-02-09 10:28:53 +0100777 h3->lctrl.qcs = qcs_new(h3->qcc, 0x3, QCS_SRV_UNI);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100778 if (!h3->lctrl.qcs)
779 return 0;
780
781 /* Wakeup ->lctrl uni-stream */
Frédéric Lécaillee16f0bd2021-08-23 09:50:29 +0200782 h3_control_send(&h3->lctrl, h3);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100783
784 return 1;
785}
786
787/* Tasklet dedicated to h3 incoming uni-streams */
788static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state)
789{
790 struct h3_uqs *h3_uqs = ctx;
791 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
792
793 h3_uqs->cb(h3_uqs, h3);
794 return NULL;
795}
796
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100797/* Release all the tasklet attached to <h3_uqs> uni-stream */
798static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs)
799{
800 struct tasklet *t = h3_uqs->wait_event.tasklet;
801
802 if (t)
803 tasklet_free(t);
804}
805
806/* Release all the tasklet attached to <h3> uni-streams */
807static void h3_uqs_tasklets_release(struct h3 *h3)
808{
809 h3_uqs_tasklet_release(&h3->rqpack_enc);
810 h3_uqs_tasklet_release(&h3->rqpack_dec);
811 h3_uqs_tasklet_release(&h3->rctrl);
812}
813
814/* Tasklet dedicated to h3 outgoing uni-streams */
815__maybe_unused
816static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state)
817{
818 struct h3_uqs *h3_uqs = ctx;
819 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
820
821 h3_uqs->cb(h3_uqs, h3);
822 return NULL;
823}
824
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500825/* Initialize <h3_uqs> uni-stream with <t> as tasklet */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100826static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3 *h3,
827 int (*cb)(struct h3_uqs *h3_uqs, void *ctx),
828 struct task *(*t)(struct task *, void *, unsigned int))
829{
830 h3_uqs->qcs = NULL;
831 h3_uqs->cb = cb;
832 h3_uqs->wait_event.tasklet = tasklet_new();
833 if (!h3_uqs->wait_event.tasklet)
834 return 0;
835
836 h3_uqs->wait_event.tasklet->process = t;
837 h3_uqs->wait_event.tasklet->context = h3_uqs;
838 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100839}
840
841static inline void h3_uqs_release(struct h3_uqs *h3_uqs)
842{
843 if (h3_uqs->qcs)
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100844 uni_qcs_free(h3_uqs->qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100845}
846
847static inline void h3_uqs_release_all(struct h3 *h3)
848{
849 h3_uqs_tasklet_release(&h3->lctrl);
850 h3_uqs_release(&h3->lctrl);
851 h3_uqs_tasklet_release(&h3->lqpack_enc);
852 h3_uqs_release(&h3->lqpack_enc);
853 h3_uqs_tasklet_release(&h3->lqpack_dec);
854 h3_uqs_release(&h3->lqpack_dec);
855}
856
857/* Initialize the HTTP/3 context for <qcc> mux.
858 * Return 1 if succeeded, 0 if not.
859 */
860static int h3_init(struct qcc *qcc)
861{
862 struct h3 *h3;
863
864 h3 = pool_alloc(pool_head_h3);
865 if (!h3)
866 goto fail_no_h3;
867
868 h3->qcc = qcc;
869 h3->err = H3_NO_ERROR;
870 h3->flags = 0;
871
872 if (!h3_uqs_init(&h3->rqpack_enc, h3, NULL, h3_uqs_task) ||
873 !h3_uqs_init(&h3->rqpack_dec, h3, NULL, h3_uqs_task) ||
874 !h3_uqs_init(&h3->rctrl, h3, h3_control_recv, h3_uqs_task))
875 goto fail_no_h3_ruqs;
876
877 if (!h3_uqs_init(&h3->lctrl, h3, h3_control_send, h3_uqs_task) ||
878 !h3_uqs_init(&h3->lqpack_enc, h3, NULL, h3_uqs_task) ||
879 !h3_uqs_init(&h3->lqpack_dec, h3, NULL, h3_uqs_task))
880 goto fail_no_h3_luqs;
881
882 qcc->ctx = h3;
883 LIST_INIT(&h3->buf_wait.list);
884
885 return 1;
886
887 fail_no_h3_ruqs:
888 h3_uqs_release_all(h3);
889 fail_no_h3_luqs:
890 h3_uqs_tasklets_release(h3);
891 pool_free(pool_head_h3, h3);
892 fail_no_h3:
893 return 0;
894}
895
896/* HTTP/3 application layer operations */
897const struct qcc_app_ops h3_ops = {
898 .init = h3_init,
899 .attach_ruqs = h3_attach_ruqs,
900 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +0100901 .snd_buf = h3_snd_buf,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100902 .finalize = h3_finalize,
903};