blob: ec219a5c0d3d8a738c5c5778270785149e3d2f73 [file] [log] [blame]
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001/*
2 * HTTP/3 protocol processing
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation, version 2.1
7 * exclusively.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <haproxy/buf.h>
Amaury Denoyelle99043552021-08-24 15:36:02 +020020#include <haproxy/connection.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010021#include <haproxy/conn_stream.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010022#include <haproxy/dynbuf.h>
23#include <haproxy/h3.h>
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +020024#include <haproxy/http.h>
25#include <haproxy/htx.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010026#include <haproxy/istbuf.h>
Amaury Denoyelle846cc042022-04-04 16:13:44 +020027#include <haproxy/mux_quic.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010028#include <haproxy/pool.h>
29#include <haproxy/qpack-dec.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020030#include <haproxy/qpack-enc.h>
31#include <haproxy/quic_enc.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010032#include <haproxy/tools.h>
33#include <haproxy/xprt_quic.h>
34
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010035#if defined(DEBUG_H3)
36#define h3_debug_printf fprintf
37#define h3_debug_hexdump debug_hexdump
38#else
39#define h3_debug_printf(...) do { } while (0)
40#define h3_debug_hexdump(...) do { } while (0)
41#endif
42
43#define H3_CF_SETTINGS_SENT 0x00000001
44
45/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +020046static uint64_t h3_settings_qpack_max_table_capacity = 0;
47static uint64_t h3_settings_qpack_blocked_streams = 4096;
48static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010049
50struct h3 {
51 struct qcc *qcc;
52 enum h3_err err;
53 uint32_t flags;
54 /* Locally initiated uni-streams */
55 struct h3_uqs lqpack_enc;
56 struct h3_uqs lqpack_dec;
57 struct h3_uqs lctrl;
58 /* Remotely initiated uni-streams */
59 struct h3_uqs rqpack_enc;
60 struct h3_uqs rqpack_dec;
61 struct h3_uqs rctrl;
62 /* Settings */
63 uint64_t qpack_max_table_capacity;
64 uint64_t qpack_blocked_streams;
65 uint64_t max_field_section_size;
66 struct buffer_wait buf_wait; /* wait list for buffer allocations */
67};
68
69DECLARE_STATIC_POOL(pool_head_h3, "h3", sizeof(struct h3));
70
71/* Simple function to duplicate a buffer */
72static inline struct buffer h3_b_dup(struct buffer *b)
73{
74 return b_make(b->area, b->size, b->head, b->data);
75}
76
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010077/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
78 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
79 * Note that this function update <b> buffer to reflect the number of bytes consumed
80 * to decode the h3 frame header.
81 */
82static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
83 struct buffer *b)
84{
85 size_t hlen;
86
87 hlen = 0;
88 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
89 return 0;
90
91 return hlen;
92}
93
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +010094/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
95 * in a local HTX buffer and transfer to the conn-stream layer. <fin> must be
96 * set if this is the last data to transfer from this stream.
97 *
98 * Returns 0 on success else non-zero.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +010099 */
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100100static int h3_headers_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
101 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100102{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100103 struct buffer htx_buf = BUF_NULL;
104 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100105 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200106 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200107 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100108 struct conn_stream *cs;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200109 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100110 struct ist meth = IST_NULL, path = IST_NULL;
111 //struct ist scheme = IST_NULL, authority = IST_NULL;
112 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200113 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100114
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100115 if (qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp, list) < 0)
116 return 1;
117
118 qc_get_buf(qcs, &htx_buf);
119 BUG_ON(!b_size(&htx_buf));
120 htx = htx_from_buf(&htx_buf);
121
122 /* first treat pseudo-header to build the start line */
123 hdr_idx = 0;
124 while (1) {
125 if (isteq(list[hdr_idx].n, ist("")))
126 break;
127
128 if (istmatch(list[hdr_idx].n, ist(":"))) {
129 /* pseudo-header */
130 if (isteq(list[hdr_idx].n, ist(":method")))
131 meth = list[hdr_idx].v;
132 else if (isteq(list[hdr_idx].n, ist(":path")))
133 path = list[hdr_idx].v;
134 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
135 // scheme = list[hdr_idx].v;
136 else if (isteq(list[hdr_idx].n, ist(":authority")))
137 authority = list[hdr_idx].v;
138 }
139
140 ++hdr_idx;
141 }
142
143 flags |= HTX_SL_F_VER_11;
144
145 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
146 if (!sl)
147 return 1;
148
149 if (fin)
150 sl->flags |= HTX_SL_F_BODYLESS;
151
152 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
153 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
154
155 if (isttest(authority))
156 htx_add_header(htx, ist("host"), authority);
157
158 /* now treat standard headers */
159 hdr_idx = 0;
160 while (1) {
161 if (isteq(list[hdr_idx].n, ist("")))
162 break;
163
164 if (!istmatch(list[hdr_idx].n, ist(":")))
165 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
166
167 ++hdr_idx;
168 }
169
170 htx_add_endof(htx, HTX_BLK_EOH);
171 htx_to_buf(htx, &htx_buf);
172
173 if (fin)
174 htx->flags |= HTX_FL_EOM;
175
Amaury Denoyelle846cc042022-04-04 16:13:44 +0200176 cs = qc_attach_cs(qcs, &htx_buf);
Frédéric Lécaille59509b52022-02-15 09:25:06 +0100177 if (!cs)
178 return 1;
Christopher Faulete9e48202022-03-22 18:13:29 +0100179 cs->endp->flags |= CS_EP_NOT_FIRST;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100180
181 /* buffer is transferred to conn_stream and set to NULL
182 * except on stream creation error.
183 */
184 b_free(&htx_buf);
185 offer_buffers(NULL, 1);
186
187 return 0;
188}
189
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100190/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
191 * HTX buffer. <fin> must be set if this is the last data to transfer from this
192 * stream.
193 *
194 * Returns 0 on success else non-zero
195 */
196static int h3_data_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
197 char fin)
198{
199 struct buffer *appbuf;
200 struct htx *htx = NULL;
201 size_t htx_sent;
202 int htx_space;
203
204 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
205 BUG_ON(!appbuf);
206 htx = htx_from_buf(appbuf);
207
208 htx_space = htx_free_data_space(htx);
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100209 if (!htx_space || htx_space < len) {
210 ABORT_NOW(); /* TODO handle this case properly */
211 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100212
213 htx_sent = htx_add_data(htx, ist2(b_head(buf), len));
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100214 if (htx_sent < len) {
215 ABORT_NOW(); /* TODO handle this case properly */
216 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100217
218 if (fin)
219 htx->flags |= HTX_FL_EOM;
220 htx_to_buf(htx, appbuf);
221
222 return 0;
223}
224
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100225/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
226 * that we received the last data of the stream.
227 * Returns <0 on error else 0.
228 */
229static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
230{
231 struct buffer *rxbuf = &qcs->rx.buf;
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100232 int ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100233
Amaury Denoyellebb970422022-04-12 16:40:52 +0200234 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100235 if (!b_data(rxbuf))
236 return 0;
237
238 while (b_data(rxbuf)) {
239 size_t hlen;
240 uint64_t ftype, flen;
241 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100242 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100243
244 /* Work on a copy of <rxbuf> */
245 b = h3_b_dup(rxbuf);
246 hlen = h3_decode_frm_header(&ftype, &flen, &b);
247 if (!hlen)
248 break;
249
250 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
251 (unsigned long long)ftype, (unsigned long long)flen);
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100252 if (flen > b_data(&b) && !b_full(rxbuf))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100253 break;
254
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100255 /* TODO handle full rxbuf */
256 BUG_ON(flen > b_size(rxbuf));
257
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100258 b_del(rxbuf, hlen);
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100259 last_stream_frame = (fin && flen == b_data(rxbuf));
260
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100261 switch (ftype) {
262 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100263 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
264 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100265 if (ret) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100266 break;
267 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100268 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
269 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100270 if (ret) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100271 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100272 case H3_FT_PUSH_PROMISE:
273 /* Not supported */
274 break;
275 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100276 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
277 * unknown frame types MUST be ignored
278 */
279 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100280 }
281 b_del(rxbuf, flen);
282 }
283
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100284 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100285}
286
287/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
288 * <rxbuf> buffer. This function does not update this buffer.
289 * Returns 0 if something wrong happened, 1 if not.
290 */
291static int h3_parse_settings_frm(struct h3 *h3, const struct buffer *rxbuf, size_t flen)
292{
293 uint64_t id, value;
294 const unsigned char *buf, *end;
295
296 buf = (const unsigned char *)b_head(rxbuf);
297 end = buf + flen;
298
299 while (buf <= end) {
300 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
301 return 0;
302
303 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
304 __func__, (unsigned long long)id, (unsigned long long)value);
305 switch (id) {
306 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
307 h3->qpack_max_table_capacity = value;
308 break;
309 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
310 h3->max_field_section_size = value;
311 break;
312 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
313 h3->qpack_blocked_streams = value;
314 break;
315 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
316 h3->err = H3_SETTINGS_ERROR;
317 return 0;
318 default:
319 /* MUST be ignored */
320 break;
321 }
322 }
323
324 return 1;
325}
326
327/* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as
328 * there is not enough received data.
329 * Returns 0 if something wrong happened, 1 if not.
330 */
331static int h3_control_recv(struct h3_uqs *h3_uqs, void *ctx)
332{
333 struct buffer *rxbuf = &h3_uqs->qcs->rx.buf;
334 struct h3 *h3 = ctx;
335
Amaury Denoyellebb970422022-04-12 16:40:52 +0200336 h3_debug_printf(stderr, "%s STREAM ID: %lu\n", __func__, h3_uqs->qcs->id);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100337 if (!b_data(rxbuf))
338 return 1;
339
340 while (b_data(rxbuf)) {
341 size_t hlen;
342 uint64_t ftype, flen;
343 struct buffer b;
344
345 /* Work on a copy of <rxbuf> */
346 b = h3_b_dup(rxbuf);
347 hlen = h3_decode_frm_header(&ftype, &flen, &b);
348 if (!hlen)
349 break;
350
351 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
352 (unsigned long long)ftype, (unsigned long long)flen);
353 if (flen > b_data(&b))
354 break;
355
356 b_del(rxbuf, hlen);
357 /* From here, a frame must not be truncated */
358 switch (ftype) {
359 case H3_FT_CANCEL_PUSH:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100360 /* XXX TODO XXX */
361 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100362 break;
363 case H3_FT_SETTINGS:
364 if (!h3_parse_settings_frm(h3, rxbuf, flen))
365 return 0;
366 break;
367 case H3_FT_GOAWAY:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100368 /* XXX TODO XXX */
369 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100370 break;
371 case H3_FT_MAX_PUSH_ID:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100372 /* XXX TODO XXX */
373 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100374 break;
375 default:
376 /* Error */
377 h3->err = H3_FRAME_UNEXPECTED;
378 return 0;
379 }
380 b_del(rxbuf, flen);
381 }
382
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100383 /* Handle the case where remaining data are present in the buffer. This
384 * can happen if there is an incomplete frame. In this case, subscribe
385 * on the lower layer to restart receive operation.
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100386 */
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100387 if (b_data(rxbuf))
388 qcs_subscribe(h3_uqs->qcs, SUB_RETRY_RECV, &h3_uqs->wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100389
390 return 1;
391}
392
Amaury Denoyellea5871362021-10-07 16:26:12 +0200393/* Returns buffer for data sending.
394 * May be NULL if the allocation failed.
395 */
396static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100397{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200398 if (!b_size(&qcs->tx.buf))
399 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100400
Amaury Denoyellea5871362021-10-07 16:26:12 +0200401 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100402}
403
404/* Function used to emit stream data from <h3_uqs> control uni-stream */
405static int h3_control_send(struct h3_uqs *h3_uqs, void *ctx)
406{
407 int ret;
408 struct h3 *h3 = ctx;
409 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200410 struct buffer pos, *res;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100411
412 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200413 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100414 if (!(h3->flags & H3_CF_SETTINGS_SENT)) {
415 struct qcs *qcs = h3_uqs->qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100416 size_t frm_len;
417
418 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
419 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
420 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
421 quic_int_getsize(h3_settings_qpack_blocked_streams);
422 if (h3_settings_max_field_section_size) {
423 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
424 quic_int_getsize(h3_settings_max_field_section_size);
425 }
426
Amaury Denoyellea5871362021-10-07 16:26:12 +0200427 b_quic_enc_int(&pos, H3_UNI_STRM_TP_CONTROL_STREAM);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100428 /* Build a SETTINGS frame */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200429 b_quic_enc_int(&pos, H3_FT_SETTINGS);
430 b_quic_enc_int(&pos, frm_len);
431 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
432 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
433 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
434 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100435 if (h3_settings_max_field_section_size) {
Amaury Denoyellea5871362021-10-07 16:26:12 +0200436 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
437 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100438 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200439
440 res = mux_get_buf(qcs);
441 if (b_room(res) < b_data(&pos)) {
442 // TODO the mux should be put in blocked state, with
443 // the stream in state waiting for settings to be sent
444 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100445 }
446
Amaury Denoyellea5871362021-10-07 16:26:12 +0200447 ret = b_force_xfer(res, &pos, b_data(&pos));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100448 if (ret > 0) {
449 h3->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200450 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
451 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100452 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100453 }
454
455 return ret;
456}
457
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200458static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
459{
460 struct buffer outbuf;
461 struct buffer headers_buf = BUF_NULL;
462 struct buffer *res;
463 struct http_hdr list[global.tune.max_http_hdr];
464 struct htx_sl *sl;
465 struct htx_blk *blk;
466 enum htx_blk_type type;
467 int frame_length_size; /* size in bytes of frame length varint field */
468 int ret = 0;
469 int hdr;
470 int status = 0;
471
472 sl = NULL;
473 hdr = 0;
474 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
475 type = htx_get_blk_type(blk);
476
477 if (type == HTX_BLK_UNUSED)
478 continue;
479
480 if (type == HTX_BLK_EOH)
481 break;
482
483 if (type == HTX_BLK_RES_SL) {
484 /* start-line -> HEADERS h3 frame */
485 BUG_ON(sl);
486 sl = htx_get_blk_ptr(htx, blk);
487 /* TODO should be on h3 layer */
488 status = sl->info.res.status;
489 }
490 else if (type == HTX_BLK_HDR) {
491 list[hdr].n = htx_get_blk_name(htx, blk);
492 list[hdr].v = htx_get_blk_value(htx, blk);
493 hdr++;
494 }
495 else {
496 ABORT_NOW();
497 goto err;
498 }
499 }
500
501 BUG_ON(!sl);
502
503 list[hdr].n = ist("");
504
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200505 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200506
507 /* At least 5 bytes to store frame type + length as a varint max size */
508 if (b_room(res) < 5)
509 ABORT_NOW();
510
511 b_reset(&outbuf);
512 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
513 /* Start the headers after frame type + length */
514 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
515
516 if (qpack_encode_field_section_line(&headers_buf))
517 ABORT_NOW();
518 if (qpack_encode_int_status(&headers_buf, status))
519 ABORT_NOW();
520
521 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
522 if (isteq(list[hdr].n, ist("")))
523 break;
524
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100525 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
526 * Transfer codings (see Section 6.1 of [HTTP11]) are not
527 * defined for HTTP/3; the Transfer-Encoding header field MUST
528 * NOT be used.
529 */
530 if (isteq(list[hdr].n, ist("transfer-encoding")))
531 continue;
532
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200533 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
534 ABORT_NOW();
535 }
536
537 /* Now that all headers are encoded, we are certain that res buffer is
538 * big enough
539 */
540 frame_length_size = quic_int_getsize(b_data(&headers_buf));
541 res->head += 4 - frame_length_size;
542 b_putchr(res, 0x01); /* h3 HEADERS frame type */
543 if (!b_quic_enc_int(res, b_data(&headers_buf)))
544 ABORT_NOW();
545 b_add(res, b_data(&headers_buf));
546
547 ret = 0;
548 blk = htx_get_head_blk(htx);
549 while (blk) {
550 type = htx_get_blk_type(blk);
551 ret += htx_get_blksz(blk);
552 blk = htx_remove_blk(htx, blk);
553 if (type == HTX_BLK_EOH)
554 break;
555 }
556
557 return ret;
558
559 err:
560 return 0;
561}
562
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200563/* Returns the total of bytes sent. */
564static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
565{
566 struct buffer outbuf;
567 struct buffer *res;
568 size_t total = 0;
569 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200570 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200571 struct htx_blk *blk;
572 enum htx_blk_type type;
573
574 htx = htx_from_buf(buf);
575
576 new_frame:
577 if (!count || htx_is_empty(htx))
578 goto end;
579
580 blk = htx_get_head_blk(htx);
581 type = htx_get_blk_type(blk);
582 fsize = bsize = htx_get_blksz(blk);
583
584 if (type != HTX_BLK_DATA)
585 goto end;
586
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200587 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200588
589 if (fsize > count)
590 fsize = count;
591
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200592 /* h3 DATA headers : 1-byte frame type + varint frame length */
593 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200594
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200595 while (1) {
596 b_reset(&outbuf);
597 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
598 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
599 break;
600 b_slow_realign(res, trash.area, b_data(res));
601 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200602
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100603 /* Not enough room for headers and at least one data byte, block the
604 * stream. It is expected that the conn-stream layer will subscribe on
605 * SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200606 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100607 if (b_size(&outbuf) <= hsize) {
608 qcs->flags |= QC_SF_BLK_MROOM;
609 goto end;
610 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200611
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200612 if (b_size(&outbuf) < hsize + fsize)
613 fsize = b_size(&outbuf) - hsize;
614 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200615
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200616 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
617 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
618
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200619 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200620 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200621 count -= fsize;
622
623 if (fsize == bsize)
624 htx_remove_blk(htx, blk);
625 else
626 htx_cut_data_blk(htx, blk, fsize);
627
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200628 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200629 b_add(res, b_data(&outbuf));
630 goto new_frame;
631
632 end:
633 return total;
634}
635
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200636size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
637{
638 size_t total = 0;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100639 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200640 struct htx *htx;
641 enum htx_blk_type btype;
642 struct htx_blk *blk;
643 uint32_t bsize;
644 int32_t idx;
645 int ret;
646
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100647 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100648
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200649 htx = htx_from_buf(buf);
650
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100651 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200652 idx = htx_get_head(htx);
653 blk = htx_get_blk(htx, idx);
654 btype = htx_get_blk_type(blk);
655 bsize = htx_get_blksz(blk);
656
657 /* Not implemented : QUIC on backend side */
658 BUG_ON(btype == HTX_BLK_REQ_SL);
659
660 switch (btype) {
661 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200662 /* start-line -> HEADERS h3 frame */
663 ret = h3_resp_headers_send(qcs, htx);
664 if (ret > 0) {
665 total += ret;
666 count -= ret;
667 if (ret < bsize)
668 goto out;
669 }
670 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200671
672 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200673 ret = h3_resp_data_send(qcs, buf, count);
674 if (ret > 0) {
675 htx = htx_from_buf(buf);
676 total += ret;
677 count -= ret;
678 if (ret < bsize)
679 goto out;
680 }
681 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200682
683 case HTX_BLK_TLR:
684 case HTX_BLK_EOT:
685 /* TODO trailers */
686
687 default:
688 htx_remove_blk(htx, blk);
689 total += bsize;
690 count -= bsize;
691 break;
692 }
693 }
694
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100695 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
696 qcs->flags |= QC_SF_FIN_STREAM;
697
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200698 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200699 if (total) {
700 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
701 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
702 }
703
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200704 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200705}
706
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100707/* Finalize the initialization of remotely initiated uni-stream <qcs>.
708 * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error
709 * to inform the QUIC mux layer of the encountered error.
710 */
711static int h3_attach_ruqs(struct qcs *qcs, void *ctx)
712{
713 uint64_t strm_type;
714 struct h3 *h3 = ctx;
715 struct buffer *rxbuf = &qcs->rx.buf;
716
717 /* First octets: the uni-stream type */
718 if (!b_quic_dec_int(&strm_type, rxbuf, NULL) || strm_type > H3_UNI_STRM_TP_MAX)
719 return 0;
720
721 /* Note that for all the uni-streams below, this is an error to receive two times the
722 * same type of uni-stream (even for Push stream which is not supported at this time.
723 */
724 switch (strm_type) {
725 case H3_UNI_STRM_TP_CONTROL_STREAM:
726 if (h3->rctrl.qcs) {
727 h3->err = H3_STREAM_CREATION_ERROR;
728 return 0;
729 }
730
731 h3->rctrl.qcs = qcs;
732 h3->rctrl.cb = h3_control_recv;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100733 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100734 break;
735 case H3_UNI_STRM_TP_PUSH_STREAM:
736 /* NOT SUPPORTED */
737 break;
738 case H3_UNI_STRM_TP_QPACK_ENCODER:
739 if (h3->rqpack_enc.qcs) {
740 h3->err = H3_STREAM_CREATION_ERROR;
741 return 0;
742 }
743
744 h3->rqpack_enc.qcs = qcs;
745 h3->rqpack_enc.cb = qpack_decode_enc;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100746 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_enc.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100747 break;
748 case H3_UNI_STRM_TP_QPACK_DECODER:
749 if (h3->rqpack_dec.qcs) {
750 h3->err = H3_STREAM_CREATION_ERROR;
751 return 0;
752 }
753
754 h3->rqpack_dec.qcs = qcs;
755 h3->rqpack_dec.cb = qpack_decode_dec;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100756 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_dec.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100757 break;
758 default:
759 /* Error */
760 h3->err = H3_STREAM_CREATION_ERROR;
761 return 0;
762 }
763
764 return 1;
765}
766
767static int h3_finalize(void *ctx)
768{
769 struct h3 *h3 = ctx;
770
Amaury Denoyelleb7880542022-02-09 10:28:53 +0100771 h3->lctrl.qcs = qcs_new(h3->qcc, 0x3, QCS_SRV_UNI);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100772 if (!h3->lctrl.qcs)
773 return 0;
774
775 /* Wakeup ->lctrl uni-stream */
Frédéric Lécaillee16f0bd2021-08-23 09:50:29 +0200776 h3_control_send(&h3->lctrl, h3);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100777
778 return 1;
779}
780
781/* Tasklet dedicated to h3 incoming uni-streams */
782static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state)
783{
784 struct h3_uqs *h3_uqs = ctx;
785 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
786
787 h3_uqs->cb(h3_uqs, h3);
788 return NULL;
789}
790
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100791/* Release all the tasklet attached to <h3_uqs> uni-stream */
792static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs)
793{
794 struct tasklet *t = h3_uqs->wait_event.tasklet;
795
796 if (t)
797 tasklet_free(t);
798}
799
800/* Release all the tasklet attached to <h3> uni-streams */
801static void h3_uqs_tasklets_release(struct h3 *h3)
802{
803 h3_uqs_tasklet_release(&h3->rqpack_enc);
804 h3_uqs_tasklet_release(&h3->rqpack_dec);
805 h3_uqs_tasklet_release(&h3->rctrl);
806}
807
808/* Tasklet dedicated to h3 outgoing uni-streams */
809__maybe_unused
810static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state)
811{
812 struct h3_uqs *h3_uqs = ctx;
813 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
814
815 h3_uqs->cb(h3_uqs, h3);
816 return NULL;
817}
818
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500819/* Initialize <h3_uqs> uni-stream with <t> as tasklet */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100820static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3 *h3,
821 int (*cb)(struct h3_uqs *h3_uqs, void *ctx),
822 struct task *(*t)(struct task *, void *, unsigned int))
823{
824 h3_uqs->qcs = NULL;
825 h3_uqs->cb = cb;
826 h3_uqs->wait_event.tasklet = tasklet_new();
827 if (!h3_uqs->wait_event.tasklet)
828 return 0;
829
830 h3_uqs->wait_event.tasklet->process = t;
831 h3_uqs->wait_event.tasklet->context = h3_uqs;
Frédéric Lécaille000162e2022-04-01 09:04:57 +0200832 h3_uqs->wait_event.events = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100833 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100834}
835
836static inline void h3_uqs_release(struct h3_uqs *h3_uqs)
837{
838 if (h3_uqs->qcs)
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200839 qcs_free(h3_uqs->qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100840}
841
842static inline void h3_uqs_release_all(struct h3 *h3)
843{
844 h3_uqs_tasklet_release(&h3->lctrl);
845 h3_uqs_release(&h3->lctrl);
846 h3_uqs_tasklet_release(&h3->lqpack_enc);
847 h3_uqs_release(&h3->lqpack_enc);
848 h3_uqs_tasklet_release(&h3->lqpack_dec);
849 h3_uqs_release(&h3->lqpack_dec);
850}
851
852/* Initialize the HTTP/3 context for <qcc> mux.
853 * Return 1 if succeeded, 0 if not.
854 */
855static int h3_init(struct qcc *qcc)
856{
857 struct h3 *h3;
858
859 h3 = pool_alloc(pool_head_h3);
860 if (!h3)
861 goto fail_no_h3;
862
863 h3->qcc = qcc;
864 h3->err = H3_NO_ERROR;
865 h3->flags = 0;
866
867 if (!h3_uqs_init(&h3->rqpack_enc, h3, NULL, h3_uqs_task) ||
868 !h3_uqs_init(&h3->rqpack_dec, h3, NULL, h3_uqs_task) ||
869 !h3_uqs_init(&h3->rctrl, h3, h3_control_recv, h3_uqs_task))
870 goto fail_no_h3_ruqs;
871
872 if (!h3_uqs_init(&h3->lctrl, h3, h3_control_send, h3_uqs_task) ||
873 !h3_uqs_init(&h3->lqpack_enc, h3, NULL, h3_uqs_task) ||
874 !h3_uqs_init(&h3->lqpack_dec, h3, NULL, h3_uqs_task))
875 goto fail_no_h3_luqs;
876
877 qcc->ctx = h3;
878 LIST_INIT(&h3->buf_wait.list);
879
880 return 1;
881
882 fail_no_h3_ruqs:
883 h3_uqs_release_all(h3);
884 fail_no_h3_luqs:
885 h3_uqs_tasklets_release(h3);
886 pool_free(pool_head_h3, h3);
887 fail_no_h3:
888 return 0;
889}
890
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200891static void h3_release(void *ctx)
892{
893 struct h3 *h3 = ctx;
894
895 h3_uqs_release_all(h3);
896 h3_uqs_tasklets_release(h3);
897 pool_free(pool_head_h3, h3);
898}
899
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200900/* Check if the H3 connection can still be considered as active.
901 *
902 * Return true if active else false.
903 */
904static int h3_is_active(const struct qcc *qcc, void *ctx)
905{
906 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
907 return 1;
908
909 return 0;
910}
911
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100912/* HTTP/3 application layer operations */
913const struct qcc_app_ops h3_ops = {
914 .init = h3_init,
915 .attach_ruqs = h3_attach_ruqs,
916 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +0100917 .snd_buf = h3_snd_buf,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100918 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200919 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200920 .release = h3_release,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100921};