blob: 5f2757ff8bbefb627d545945e100b969fa95cc0b [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 Denoyelle7b0f1222022-02-14 17:13:55 +010096/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
97 * in a local HTX buffer and transfer to the conn-stream layer. <fin> must be
98 * set if this is the last data to transfer from this stream.
99 *
100 * Returns 0 on success else non-zero.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100101 */
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100102static int h3_headers_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
103 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100104{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100105 struct buffer htx_buf = BUF_NULL;
106 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100107 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200108 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200109 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100110 struct conn_stream *cs;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200111 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100112 struct ist meth = IST_NULL, path = IST_NULL;
113 //struct ist scheme = IST_NULL, authority = IST_NULL;
114 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200115 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100116
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100117 if (qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp, list) < 0)
118 return 1;
119
120 qc_get_buf(qcs, &htx_buf);
121 BUG_ON(!b_size(&htx_buf));
122 htx = htx_from_buf(&htx_buf);
123
124 /* first treat pseudo-header to build the start line */
125 hdr_idx = 0;
126 while (1) {
127 if (isteq(list[hdr_idx].n, ist("")))
128 break;
129
130 if (istmatch(list[hdr_idx].n, ist(":"))) {
131 /* pseudo-header */
132 if (isteq(list[hdr_idx].n, ist(":method")))
133 meth = list[hdr_idx].v;
134 else if (isteq(list[hdr_idx].n, ist(":path")))
135 path = list[hdr_idx].v;
136 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
137 // scheme = list[hdr_idx].v;
138 else if (isteq(list[hdr_idx].n, ist(":authority")))
139 authority = list[hdr_idx].v;
140 }
141
142 ++hdr_idx;
143 }
144
145 flags |= HTX_SL_F_VER_11;
146
147 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
148 if (!sl)
149 return 1;
150
151 if (fin)
152 sl->flags |= HTX_SL_F_BODYLESS;
153
154 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
155 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
156
157 if (isttest(authority))
158 htx_add_header(htx, ist("host"), authority);
159
160 /* now treat standard headers */
161 hdr_idx = 0;
162 while (1) {
163 if (isteq(list[hdr_idx].n, ist("")))
164 break;
165
166 if (!istmatch(list[hdr_idx].n, ist(":")))
167 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
168
169 ++hdr_idx;
170 }
171
172 htx_add_endof(htx, HTX_BLK_EOH);
173 htx_to_buf(htx, &htx_buf);
174
175 if (fin)
176 htx->flags |= HTX_FL_EOM;
177
178 cs = cs_new(qcs->qcc->conn, qcs->qcc->conn->target);
Frédéric Lécaille59509b52022-02-15 09:25:06 +0100179 if (!cs)
180 return 1;
181
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100182 cs->flags |= CS_FL_NOT_FIRST;
183 cs->ctx = qcs;
184 stream_create_from_cs(cs, &htx_buf);
185
186 /* buffer is transferred to conn_stream and set to NULL
187 * except on stream creation error.
188 */
189 b_free(&htx_buf);
190 offer_buffers(NULL, 1);
191
192 return 0;
193}
194
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100195/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
196 * HTX buffer. <fin> must be set if this is the last data to transfer from this
197 * stream.
198 *
199 * Returns 0 on success else non-zero
200 */
201static int h3_data_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
202 char fin)
203{
204 struct buffer *appbuf;
205 struct htx *htx = NULL;
206 size_t htx_sent;
207 int htx_space;
208
209 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
210 BUG_ON(!appbuf);
211 htx = htx_from_buf(appbuf);
212
213 htx_space = htx_free_data_space(htx);
214 BUG_ON(!htx_space || htx_space < len);
215
216 htx_sent = htx_add_data(htx, ist2(b_head(buf), len));
217 /* TODO handle full appbuf */
218 BUG_ON(htx_sent < len);
219
220 if (fin)
221 htx->flags |= HTX_FL_EOM;
222 htx_to_buf(htx, appbuf);
223
224 return 0;
225}
226
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100227/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
228 * that we received the last data of the stream.
229 * Returns <0 on error else 0.
230 */
231static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
232{
233 struct buffer *rxbuf = &qcs->rx.buf;
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100234 int ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100235
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100236 h3_debug_printf(stderr, "%s: STREAM ID: %llu\n", __func__, qcs->by_id.key);
237 if (!b_data(rxbuf))
238 return 0;
239
240 while (b_data(rxbuf)) {
241 size_t hlen;
242 uint64_t ftype, flen;
243 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100244 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100245
246 /* Work on a copy of <rxbuf> */
247 b = h3_b_dup(rxbuf);
248 hlen = h3_decode_frm_header(&ftype, &flen, &b);
249 if (!hlen)
250 break;
251
252 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
253 (unsigned long long)ftype, (unsigned long long)flen);
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100254 if (flen > b_data(&b) && !b_full(rxbuf))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100255 break;
256
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100257 /* TODO handle full rxbuf */
258 BUG_ON(flen > b_size(rxbuf));
259
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100260 b_del(rxbuf, hlen);
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100261 last_stream_frame = (fin && flen == b_data(rxbuf));
262
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100263 switch (ftype) {
264 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100265 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
266 /* TODO handle error reporting. Stream closure required. */
267 BUG_ON(ret);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100268 break;
269 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100270 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
271 /* TODO handle error reporting. Stream closure required. */
272 BUG_ON(ret);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100273 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100274 case H3_FT_PUSH_PROMISE:
275 /* Not supported */
276 break;
277 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100278 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
279 * unknown frame types MUST be ignored
280 */
281 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100282 }
283 b_del(rxbuf, flen);
284 }
285
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100286 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100287}
288
289/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
290 * <rxbuf> buffer. This function does not update this buffer.
291 * Returns 0 if something wrong happened, 1 if not.
292 */
293static int h3_parse_settings_frm(struct h3 *h3, const struct buffer *rxbuf, size_t flen)
294{
295 uint64_t id, value;
296 const unsigned char *buf, *end;
297
298 buf = (const unsigned char *)b_head(rxbuf);
299 end = buf + flen;
300
301 while (buf <= end) {
302 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
303 return 0;
304
305 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
306 __func__, (unsigned long long)id, (unsigned long long)value);
307 switch (id) {
308 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
309 h3->qpack_max_table_capacity = value;
310 break;
311 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
312 h3->max_field_section_size = value;
313 break;
314 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
315 h3->qpack_blocked_streams = value;
316 break;
317 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
318 h3->err = H3_SETTINGS_ERROR;
319 return 0;
320 default:
321 /* MUST be ignored */
322 break;
323 }
324 }
325
326 return 1;
327}
328
329/* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as
330 * there is not enough received data.
331 * Returns 0 if something wrong happened, 1 if not.
332 */
333static int h3_control_recv(struct h3_uqs *h3_uqs, void *ctx)
334{
335 struct buffer *rxbuf = &h3_uqs->qcs->rx.buf;
336 struct h3 *h3 = ctx;
337
338 h3_debug_printf(stderr, "%s STREAM ID: %llu\n", __func__, h3_uqs->qcs->by_id.key);
339 if (!b_data(rxbuf))
340 return 1;
341
342 while (b_data(rxbuf)) {
343 size_t hlen;
344 uint64_t ftype, flen;
345 struct buffer b;
346
347 /* Work on a copy of <rxbuf> */
348 b = h3_b_dup(rxbuf);
349 hlen = h3_decode_frm_header(&ftype, &flen, &b);
350 if (!hlen)
351 break;
352
353 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
354 (unsigned long long)ftype, (unsigned long long)flen);
355 if (flen > b_data(&b))
356 break;
357
358 b_del(rxbuf, hlen);
359 /* From here, a frame must not be truncated */
360 switch (ftype) {
361 case H3_FT_CANCEL_PUSH:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100362 /* XXX TODO XXX */
363 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100364 break;
365 case H3_FT_SETTINGS:
366 if (!h3_parse_settings_frm(h3, rxbuf, flen))
367 return 0;
368 break;
369 case H3_FT_GOAWAY:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100370 /* XXX TODO XXX */
371 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100372 break;
373 case H3_FT_MAX_PUSH_ID:
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 default:
378 /* Error */
379 h3->err = H3_FRAME_UNEXPECTED;
380 return 0;
381 }
382 b_del(rxbuf, flen);
383 }
384
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100385 /* Handle the case where remaining data are present in the buffer. This
386 * can happen if there is an incomplete frame. In this case, subscribe
387 * on the lower layer to restart receive operation.
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100388 */
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100389 if (b_data(rxbuf))
390 qcs_subscribe(h3_uqs->qcs, SUB_RETRY_RECV, &h3_uqs->wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100391
392 return 1;
393}
394
Amaury Denoyellea5871362021-10-07 16:26:12 +0200395/* Returns buffer for data sending.
396 * May be NULL if the allocation failed.
397 */
398static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100399{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200400 if (!b_size(&qcs->tx.buf))
401 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100402
Amaury Denoyellea5871362021-10-07 16:26:12 +0200403 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100404}
405
406/* Function used to emit stream data from <h3_uqs> control uni-stream */
407static int h3_control_send(struct h3_uqs *h3_uqs, void *ctx)
408{
409 int ret;
410 struct h3 *h3 = ctx;
411 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200412 struct buffer pos, *res;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100413
414 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200415 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100416 if (!(h3->flags & H3_CF_SETTINGS_SENT)) {
417 struct qcs *qcs = h3_uqs->qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100418 size_t frm_len;
419
420 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
421 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
422 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
423 quic_int_getsize(h3_settings_qpack_blocked_streams);
424 if (h3_settings_max_field_section_size) {
425 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
426 quic_int_getsize(h3_settings_max_field_section_size);
427 }
428
Amaury Denoyellea5871362021-10-07 16:26:12 +0200429 b_quic_enc_int(&pos, H3_UNI_STRM_TP_CONTROL_STREAM);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100430 /* Build a SETTINGS frame */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200431 b_quic_enc_int(&pos, H3_FT_SETTINGS);
432 b_quic_enc_int(&pos, frm_len);
433 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
434 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
435 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
436 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100437 if (h3_settings_max_field_section_size) {
Amaury Denoyellea5871362021-10-07 16:26:12 +0200438 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
439 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100440 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200441
442 res = mux_get_buf(qcs);
443 if (b_room(res) < b_data(&pos)) {
444 // TODO the mux should be put in blocked state, with
445 // the stream in state waiting for settings to be sent
446 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100447 }
448
Amaury Denoyellea5871362021-10-07 16:26:12 +0200449 ret = b_force_xfer(res, &pos, b_data(&pos));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100450 if (ret > 0) {
451 h3->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200452 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
453 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100454 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100455 }
456
457 return ret;
458}
459
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200460static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
461{
462 struct buffer outbuf;
463 struct buffer headers_buf = BUF_NULL;
464 struct buffer *res;
465 struct http_hdr list[global.tune.max_http_hdr];
466 struct htx_sl *sl;
467 struct htx_blk *blk;
468 enum htx_blk_type type;
469 int frame_length_size; /* size in bytes of frame length varint field */
470 int ret = 0;
471 int hdr;
472 int status = 0;
473
474 sl = NULL;
475 hdr = 0;
476 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
477 type = htx_get_blk_type(blk);
478
479 if (type == HTX_BLK_UNUSED)
480 continue;
481
482 if (type == HTX_BLK_EOH)
483 break;
484
485 if (type == HTX_BLK_RES_SL) {
486 /* start-line -> HEADERS h3 frame */
487 BUG_ON(sl);
488 sl = htx_get_blk_ptr(htx, blk);
489 /* TODO should be on h3 layer */
490 status = sl->info.res.status;
491 }
492 else if (type == HTX_BLK_HDR) {
493 list[hdr].n = htx_get_blk_name(htx, blk);
494 list[hdr].v = htx_get_blk_value(htx, blk);
495 hdr++;
496 }
497 else {
498 ABORT_NOW();
499 goto err;
500 }
501 }
502
503 BUG_ON(!sl);
504
505 list[hdr].n = ist("");
506
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200507 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200508
509 /* At least 5 bytes to store frame type + length as a varint max size */
510 if (b_room(res) < 5)
511 ABORT_NOW();
512
513 b_reset(&outbuf);
514 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
515 /* Start the headers after frame type + length */
516 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
517
518 if (qpack_encode_field_section_line(&headers_buf))
519 ABORT_NOW();
520 if (qpack_encode_int_status(&headers_buf, status))
521 ABORT_NOW();
522
523 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
524 if (isteq(list[hdr].n, ist("")))
525 break;
526
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100527 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
528 * Transfer codings (see Section 6.1 of [HTTP11]) are not
529 * defined for HTTP/3; the Transfer-Encoding header field MUST
530 * NOT be used.
531 */
532 if (isteq(list[hdr].n, ist("transfer-encoding")))
533 continue;
534
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200535 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
536 ABORT_NOW();
537 }
538
539 /* Now that all headers are encoded, we are certain that res buffer is
540 * big enough
541 */
542 frame_length_size = quic_int_getsize(b_data(&headers_buf));
543 res->head += 4 - frame_length_size;
544 b_putchr(res, 0x01); /* h3 HEADERS frame type */
545 if (!b_quic_enc_int(res, b_data(&headers_buf)))
546 ABORT_NOW();
547 b_add(res, b_data(&headers_buf));
548
549 ret = 0;
550 blk = htx_get_head_blk(htx);
551 while (blk) {
552 type = htx_get_blk_type(blk);
553 ret += htx_get_blksz(blk);
554 blk = htx_remove_blk(htx, blk);
555 if (type == HTX_BLK_EOH)
556 break;
557 }
558
559 return ret;
560
561 err:
562 return 0;
563}
564
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200565/* Returns the total of bytes sent. */
566static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
567{
568 struct buffer outbuf;
569 struct buffer *res;
570 size_t total = 0;
571 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200572 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200573 struct htx_blk *blk;
574 enum htx_blk_type type;
575
576 htx = htx_from_buf(buf);
577
578 new_frame:
579 if (!count || htx_is_empty(htx))
580 goto end;
581
582 blk = htx_get_head_blk(htx);
583 type = htx_get_blk_type(blk);
584 fsize = bsize = htx_get_blksz(blk);
585
586 if (type != HTX_BLK_DATA)
587 goto end;
588
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200589 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200590
591 if (fsize > count)
592 fsize = count;
593
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200594 /* h3 DATA headers : 1-byte frame type + varint frame length */
595 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200596
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200597 while (1) {
598 b_reset(&outbuf);
599 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
600 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
601 break;
602 b_slow_realign(res, trash.area, b_data(res));
603 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200604
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100605 /* Not enough room for headers and at least one data byte, block the
606 * stream. It is expected that the conn-stream layer will subscribe on
607 * SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200608 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100609 if (b_size(&outbuf) <= hsize) {
610 qcs->flags |= QC_SF_BLK_MROOM;
611 goto end;
612 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200613
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200614 if (b_size(&outbuf) < hsize + fsize)
615 fsize = b_size(&outbuf) - hsize;
616 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200617
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200618 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
619 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
620
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200621 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200622 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200623 count -= fsize;
624
625 if (fsize == bsize)
626 htx_remove_blk(htx, blk);
627 else
628 htx_cut_data_blk(htx, blk, fsize);
629
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200630 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200631 b_add(res, b_data(&outbuf));
632 goto new_frame;
633
634 end:
635 return total;
636}
637
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200638size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
639{
640 size_t total = 0;
641 struct qcs *qcs = cs->ctx;
642 struct htx *htx;
643 enum htx_blk_type btype;
644 struct htx_blk *blk;
645 uint32_t bsize;
646 int32_t idx;
647 int ret;
648
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100649 fprintf(stderr, "%s\n", __func__);
650
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200651 htx = htx_from_buf(buf);
652
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100653 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200654 idx = htx_get_head(htx);
655 blk = htx_get_blk(htx, idx);
656 btype = htx_get_blk_type(blk);
657 bsize = htx_get_blksz(blk);
658
659 /* Not implemented : QUIC on backend side */
660 BUG_ON(btype == HTX_BLK_REQ_SL);
661
662 switch (btype) {
663 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200664 /* start-line -> HEADERS h3 frame */
665 ret = h3_resp_headers_send(qcs, htx);
666 if (ret > 0) {
667 total += ret;
668 count -= ret;
669 if (ret < bsize)
670 goto out;
671 }
672 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200673
674 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200675 ret = h3_resp_data_send(qcs, buf, count);
676 if (ret > 0) {
677 htx = htx_from_buf(buf);
678 total += ret;
679 count -= ret;
680 if (ret < bsize)
681 goto out;
682 }
683 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200684
685 case HTX_BLK_TLR:
686 case HTX_BLK_EOT:
687 /* TODO trailers */
688
689 default:
690 htx_remove_blk(htx, blk);
691 total += bsize;
692 count -= bsize;
693 break;
694 }
695 }
696
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100697 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
698 qcs->flags |= QC_SF_FIN_STREAM;
699
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200700 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200701 if (total) {
702 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
703 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
704 }
705
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200706 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200707}
708
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100709/* Finalize the initialization of remotely initiated uni-stream <qcs>.
710 * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error
711 * to inform the QUIC mux layer of the encountered error.
712 */
713static int h3_attach_ruqs(struct qcs *qcs, void *ctx)
714{
715 uint64_t strm_type;
716 struct h3 *h3 = ctx;
717 struct buffer *rxbuf = &qcs->rx.buf;
718
719 /* First octets: the uni-stream type */
720 if (!b_quic_dec_int(&strm_type, rxbuf, NULL) || strm_type > H3_UNI_STRM_TP_MAX)
721 return 0;
722
723 /* Note that for all the uni-streams below, this is an error to receive two times the
724 * same type of uni-stream (even for Push stream which is not supported at this time.
725 */
726 switch (strm_type) {
727 case H3_UNI_STRM_TP_CONTROL_STREAM:
728 if (h3->rctrl.qcs) {
729 h3->err = H3_STREAM_CREATION_ERROR;
730 return 0;
731 }
732
733 h3->rctrl.qcs = qcs;
734 h3->rctrl.cb = h3_control_recv;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100735 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100736 break;
737 case H3_UNI_STRM_TP_PUSH_STREAM:
738 /* NOT SUPPORTED */
739 break;
740 case H3_UNI_STRM_TP_QPACK_ENCODER:
741 if (h3->rqpack_enc.qcs) {
742 h3->err = H3_STREAM_CREATION_ERROR;
743 return 0;
744 }
745
746 h3->rqpack_enc.qcs = qcs;
747 h3->rqpack_enc.cb = qpack_decode_enc;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100748 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_enc.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100749 break;
750 case H3_UNI_STRM_TP_QPACK_DECODER:
751 if (h3->rqpack_dec.qcs) {
752 h3->err = H3_STREAM_CREATION_ERROR;
753 return 0;
754 }
755
756 h3->rqpack_dec.qcs = qcs;
757 h3->rqpack_dec.cb = qpack_decode_dec;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100758 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_dec.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100759 break;
760 default:
761 /* Error */
762 h3->err = H3_STREAM_CREATION_ERROR;
763 return 0;
764 }
765
766 return 1;
767}
768
769static int h3_finalize(void *ctx)
770{
771 struct h3 *h3 = ctx;
772
Amaury Denoyelleb7880542022-02-09 10:28:53 +0100773 h3->lctrl.qcs = qcs_new(h3->qcc, 0x3, QCS_SRV_UNI);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100774 if (!h3->lctrl.qcs)
775 return 0;
776
777 /* Wakeup ->lctrl uni-stream */
Frédéric Lécaillee16f0bd2021-08-23 09:50:29 +0200778 h3_control_send(&h3->lctrl, h3);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100779
780 return 1;
781}
782
783/* Tasklet dedicated to h3 incoming uni-streams */
784static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state)
785{
786 struct h3_uqs *h3_uqs = ctx;
787 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
788
789 h3_uqs->cb(h3_uqs, h3);
790 return NULL;
791}
792
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100793/* Release all the tasklet attached to <h3_uqs> uni-stream */
794static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs)
795{
796 struct tasklet *t = h3_uqs->wait_event.tasklet;
797
798 if (t)
799 tasklet_free(t);
800}
801
802/* Release all the tasklet attached to <h3> uni-streams */
803static void h3_uqs_tasklets_release(struct h3 *h3)
804{
805 h3_uqs_tasklet_release(&h3->rqpack_enc);
806 h3_uqs_tasklet_release(&h3->rqpack_dec);
807 h3_uqs_tasklet_release(&h3->rctrl);
808}
809
810/* Tasklet dedicated to h3 outgoing uni-streams */
811__maybe_unused
812static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state)
813{
814 struct h3_uqs *h3_uqs = ctx;
815 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
816
817 h3_uqs->cb(h3_uqs, h3);
818 return NULL;
819}
820
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500821/* Initialize <h3_uqs> uni-stream with <t> as tasklet */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100822static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3 *h3,
823 int (*cb)(struct h3_uqs *h3_uqs, void *ctx),
824 struct task *(*t)(struct task *, void *, unsigned int))
825{
826 h3_uqs->qcs = NULL;
827 h3_uqs->cb = cb;
828 h3_uqs->wait_event.tasklet = tasklet_new();
829 if (!h3_uqs->wait_event.tasklet)
830 return 0;
831
832 h3_uqs->wait_event.tasklet->process = t;
833 h3_uqs->wait_event.tasklet->context = h3_uqs;
834 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100835}
836
837static inline void h3_uqs_release(struct h3_uqs *h3_uqs)
838{
839 if (h3_uqs->qcs)
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100840 uni_qcs_free(h3_uqs->qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100841}
842
843static inline void h3_uqs_release_all(struct h3 *h3)
844{
845 h3_uqs_tasklet_release(&h3->lctrl);
846 h3_uqs_release(&h3->lctrl);
847 h3_uqs_tasklet_release(&h3->lqpack_enc);
848 h3_uqs_release(&h3->lqpack_enc);
849 h3_uqs_tasklet_release(&h3->lqpack_dec);
850 h3_uqs_release(&h3->lqpack_dec);
851}
852
853/* Initialize the HTTP/3 context for <qcc> mux.
854 * Return 1 if succeeded, 0 if not.
855 */
856static int h3_init(struct qcc *qcc)
857{
858 struct h3 *h3;
859
860 h3 = pool_alloc(pool_head_h3);
861 if (!h3)
862 goto fail_no_h3;
863
864 h3->qcc = qcc;
865 h3->err = H3_NO_ERROR;
866 h3->flags = 0;
867
868 if (!h3_uqs_init(&h3->rqpack_enc, h3, NULL, h3_uqs_task) ||
869 !h3_uqs_init(&h3->rqpack_dec, h3, NULL, h3_uqs_task) ||
870 !h3_uqs_init(&h3->rctrl, h3, h3_control_recv, h3_uqs_task))
871 goto fail_no_h3_ruqs;
872
873 if (!h3_uqs_init(&h3->lctrl, h3, h3_control_send, h3_uqs_task) ||
874 !h3_uqs_init(&h3->lqpack_enc, h3, NULL, h3_uqs_task) ||
875 !h3_uqs_init(&h3->lqpack_dec, h3, NULL, h3_uqs_task))
876 goto fail_no_h3_luqs;
877
878 qcc->ctx = h3;
879 LIST_INIT(&h3->buf_wait.list);
880
881 return 1;
882
883 fail_no_h3_ruqs:
884 h3_uqs_release_all(h3);
885 fail_no_h3_luqs:
886 h3_uqs_tasklets_release(h3);
887 pool_free(pool_head_h3, h3);
888 fail_no_h3:
889 return 0;
890}
891
892/* HTTP/3 application layer operations */
893const struct qcc_app_ops h3_ops = {
894 .init = h3_init,
895 .attach_ruqs = h3_attach_ruqs,
896 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +0100897 .snd_buf = h3_snd_buf,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100898 .finalize = h3_finalize,
899};