blob: 0fce7b0faa091e702e561e1345a2ed970b47a7cc [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;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100179
180 /* buffer is transferred to conn_stream and set to NULL
181 * except on stream creation error.
182 */
183 b_free(&htx_buf);
184 offer_buffers(NULL, 1);
185
186 return 0;
187}
188
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100189/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
190 * HTX buffer. <fin> must be set if this is the last data to transfer from this
191 * stream.
192 *
193 * Returns 0 on success else non-zero
194 */
195static int h3_data_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
196 char fin)
197{
198 struct buffer *appbuf;
199 struct htx *htx = NULL;
200 size_t htx_sent;
201 int htx_space;
202
203 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
204 BUG_ON(!appbuf);
205 htx = htx_from_buf(appbuf);
206
207 htx_space = htx_free_data_space(htx);
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100208 if (!htx_space || htx_space < len) {
209 ABORT_NOW(); /* TODO handle this case properly */
210 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100211
212 htx_sent = htx_add_data(htx, ist2(b_head(buf), len));
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100213 if (htx_sent < len) {
214 ABORT_NOW(); /* TODO handle this case properly */
215 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100216
217 if (fin)
218 htx->flags |= HTX_FL_EOM;
219 htx_to_buf(htx, appbuf);
220
221 return 0;
222}
223
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100224/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
225 * that we received the last data of the stream.
226 * Returns <0 on error else 0.
227 */
228static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
229{
230 struct buffer *rxbuf = &qcs->rx.buf;
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100231 int ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100232
Amaury Denoyellebb970422022-04-12 16:40:52 +0200233 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100234 if (!b_data(rxbuf))
235 return 0;
236
237 while (b_data(rxbuf)) {
238 size_t hlen;
239 uint64_t ftype, flen;
240 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100241 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100242
243 /* Work on a copy of <rxbuf> */
244 b = h3_b_dup(rxbuf);
245 hlen = h3_decode_frm_header(&ftype, &flen, &b);
246 if (!hlen)
247 break;
248
249 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
250 (unsigned long long)ftype, (unsigned long long)flen);
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100251 if (flen > b_data(&b) && !b_full(rxbuf))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100252 break;
253
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100254 /* TODO handle full rxbuf */
255 BUG_ON(flen > b_size(rxbuf));
256
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100257 b_del(rxbuf, hlen);
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100258 last_stream_frame = (fin && flen == b_data(rxbuf));
259
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100260 switch (ftype) {
261 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100262 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
263 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100264 if (ret) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100265 break;
266 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100267 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
268 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100269 if (ret) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100270 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100271 case H3_FT_PUSH_PROMISE:
272 /* Not supported */
273 break;
274 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100275 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
276 * unknown frame types MUST be ignored
277 */
278 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100279 }
280 b_del(rxbuf, flen);
281 }
282
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100283 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100284}
285
286/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
287 * <rxbuf> buffer. This function does not update this buffer.
288 * Returns 0 if something wrong happened, 1 if not.
289 */
290static int h3_parse_settings_frm(struct h3 *h3, const struct buffer *rxbuf, size_t flen)
291{
292 uint64_t id, value;
293 const unsigned char *buf, *end;
294
295 buf = (const unsigned char *)b_head(rxbuf);
296 end = buf + flen;
297
298 while (buf <= end) {
299 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
300 return 0;
301
302 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
303 __func__, (unsigned long long)id, (unsigned long long)value);
304 switch (id) {
305 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
306 h3->qpack_max_table_capacity = value;
307 break;
308 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
309 h3->max_field_section_size = value;
310 break;
311 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
312 h3->qpack_blocked_streams = value;
313 break;
314 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
315 h3->err = H3_SETTINGS_ERROR;
316 return 0;
317 default:
318 /* MUST be ignored */
319 break;
320 }
321 }
322
323 return 1;
324}
325
326/* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as
327 * there is not enough received data.
328 * Returns 0 if something wrong happened, 1 if not.
329 */
330static int h3_control_recv(struct h3_uqs *h3_uqs, void *ctx)
331{
332 struct buffer *rxbuf = &h3_uqs->qcs->rx.buf;
333 struct h3 *h3 = ctx;
334
Amaury Denoyellebb970422022-04-12 16:40:52 +0200335 h3_debug_printf(stderr, "%s STREAM ID: %lu\n", __func__, h3_uqs->qcs->id);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100336 if (!b_data(rxbuf))
337 return 1;
338
339 while (b_data(rxbuf)) {
340 size_t hlen;
341 uint64_t ftype, flen;
342 struct buffer b;
343
344 /* Work on a copy of <rxbuf> */
345 b = h3_b_dup(rxbuf);
346 hlen = h3_decode_frm_header(&ftype, &flen, &b);
347 if (!hlen)
348 break;
349
350 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
351 (unsigned long long)ftype, (unsigned long long)flen);
352 if (flen > b_data(&b))
353 break;
354
355 b_del(rxbuf, hlen);
356 /* From here, a frame must not be truncated */
357 switch (ftype) {
358 case H3_FT_CANCEL_PUSH:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100359 /* XXX TODO XXX */
360 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100361 break;
362 case H3_FT_SETTINGS:
363 if (!h3_parse_settings_frm(h3, rxbuf, flen))
364 return 0;
365 break;
366 case H3_FT_GOAWAY:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100367 /* XXX TODO XXX */
368 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100369 break;
370 case H3_FT_MAX_PUSH_ID:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100371 /* XXX TODO XXX */
372 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100373 break;
374 default:
375 /* Error */
376 h3->err = H3_FRAME_UNEXPECTED;
377 return 0;
378 }
379 b_del(rxbuf, flen);
380 }
381
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100382 /* Handle the case where remaining data are present in the buffer. This
383 * can happen if there is an incomplete frame. In this case, subscribe
384 * on the lower layer to restart receive operation.
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100385 */
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100386 if (b_data(rxbuf))
387 qcs_subscribe(h3_uqs->qcs, SUB_RETRY_RECV, &h3_uqs->wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100388
389 return 1;
390}
391
Amaury Denoyellea5871362021-10-07 16:26:12 +0200392/* Returns buffer for data sending.
393 * May be NULL if the allocation failed.
394 */
395static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100396{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200397 if (!b_size(&qcs->tx.buf))
398 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100399
Amaury Denoyellea5871362021-10-07 16:26:12 +0200400 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100401}
402
403/* Function used to emit stream data from <h3_uqs> control uni-stream */
404static int h3_control_send(struct h3_uqs *h3_uqs, void *ctx)
405{
406 int ret;
407 struct h3 *h3 = ctx;
408 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200409 struct buffer pos, *res;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100410
411 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200412 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100413 if (!(h3->flags & H3_CF_SETTINGS_SENT)) {
414 struct qcs *qcs = h3_uqs->qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100415 size_t frm_len;
416
417 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
418 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
419 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
420 quic_int_getsize(h3_settings_qpack_blocked_streams);
421 if (h3_settings_max_field_section_size) {
422 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
423 quic_int_getsize(h3_settings_max_field_section_size);
424 }
425
Amaury Denoyellea5871362021-10-07 16:26:12 +0200426 b_quic_enc_int(&pos, H3_UNI_STRM_TP_CONTROL_STREAM);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100427 /* Build a SETTINGS frame */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200428 b_quic_enc_int(&pos, H3_FT_SETTINGS);
429 b_quic_enc_int(&pos, frm_len);
430 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
431 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
432 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
433 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100434 if (h3_settings_max_field_section_size) {
Amaury Denoyellea5871362021-10-07 16:26:12 +0200435 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
436 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100437 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200438
439 res = mux_get_buf(qcs);
440 if (b_room(res) < b_data(&pos)) {
441 // TODO the mux should be put in blocked state, with
442 // the stream in state waiting for settings to be sent
443 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100444 }
445
Amaury Denoyellea5871362021-10-07 16:26:12 +0200446 ret = b_force_xfer(res, &pos, b_data(&pos));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100447 if (ret > 0) {
448 h3->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200449 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
450 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100451 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100452 }
453
454 return ret;
455}
456
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200457static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
458{
459 struct buffer outbuf;
460 struct buffer headers_buf = BUF_NULL;
461 struct buffer *res;
462 struct http_hdr list[global.tune.max_http_hdr];
463 struct htx_sl *sl;
464 struct htx_blk *blk;
465 enum htx_blk_type type;
466 int frame_length_size; /* size in bytes of frame length varint field */
467 int ret = 0;
468 int hdr;
469 int status = 0;
470
471 sl = NULL;
472 hdr = 0;
473 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
474 type = htx_get_blk_type(blk);
475
476 if (type == HTX_BLK_UNUSED)
477 continue;
478
479 if (type == HTX_BLK_EOH)
480 break;
481
482 if (type == HTX_BLK_RES_SL) {
483 /* start-line -> HEADERS h3 frame */
484 BUG_ON(sl);
485 sl = htx_get_blk_ptr(htx, blk);
486 /* TODO should be on h3 layer */
487 status = sl->info.res.status;
488 }
489 else if (type == HTX_BLK_HDR) {
490 list[hdr].n = htx_get_blk_name(htx, blk);
491 list[hdr].v = htx_get_blk_value(htx, blk);
492 hdr++;
493 }
494 else {
495 ABORT_NOW();
496 goto err;
497 }
498 }
499
500 BUG_ON(!sl);
501
502 list[hdr].n = ist("");
503
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200504 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200505
506 /* At least 5 bytes to store frame type + length as a varint max size */
507 if (b_room(res) < 5)
508 ABORT_NOW();
509
510 b_reset(&outbuf);
511 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
512 /* Start the headers after frame type + length */
513 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
514
515 if (qpack_encode_field_section_line(&headers_buf))
516 ABORT_NOW();
517 if (qpack_encode_int_status(&headers_buf, status))
518 ABORT_NOW();
519
520 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
521 if (isteq(list[hdr].n, ist("")))
522 break;
523
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100524 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
525 * Transfer codings (see Section 6.1 of [HTTP11]) are not
526 * defined for HTTP/3; the Transfer-Encoding header field MUST
527 * NOT be used.
528 */
529 if (isteq(list[hdr].n, ist("transfer-encoding")))
530 continue;
531
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200532 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
533 ABORT_NOW();
534 }
535
536 /* Now that all headers are encoded, we are certain that res buffer is
537 * big enough
538 */
539 frame_length_size = quic_int_getsize(b_data(&headers_buf));
540 res->head += 4 - frame_length_size;
541 b_putchr(res, 0x01); /* h3 HEADERS frame type */
542 if (!b_quic_enc_int(res, b_data(&headers_buf)))
543 ABORT_NOW();
544 b_add(res, b_data(&headers_buf));
545
546 ret = 0;
547 blk = htx_get_head_blk(htx);
548 while (blk) {
549 type = htx_get_blk_type(blk);
550 ret += htx_get_blksz(blk);
551 blk = htx_remove_blk(htx, blk);
552 if (type == HTX_BLK_EOH)
553 break;
554 }
555
556 return ret;
557
558 err:
559 return 0;
560}
561
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200562/* Returns the total of bytes sent. */
563static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
564{
565 struct buffer outbuf;
566 struct buffer *res;
567 size_t total = 0;
568 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200569 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200570 struct htx_blk *blk;
571 enum htx_blk_type type;
572
573 htx = htx_from_buf(buf);
574
575 new_frame:
576 if (!count || htx_is_empty(htx))
577 goto end;
578
579 blk = htx_get_head_blk(htx);
580 type = htx_get_blk_type(blk);
581 fsize = bsize = htx_get_blksz(blk);
582
583 if (type != HTX_BLK_DATA)
584 goto end;
585
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200586 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200587
588 if (fsize > count)
589 fsize = count;
590
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200591 /* h3 DATA headers : 1-byte frame type + varint frame length */
592 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200593
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200594 while (1) {
595 b_reset(&outbuf);
596 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
597 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
598 break;
599 b_slow_realign(res, trash.area, b_data(res));
600 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200601
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100602 /* Not enough room for headers and at least one data byte, block the
603 * stream. It is expected that the conn-stream layer will subscribe on
604 * SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200605 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100606 if (b_size(&outbuf) <= hsize) {
607 qcs->flags |= QC_SF_BLK_MROOM;
608 goto end;
609 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200610
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200611 if (b_size(&outbuf) < hsize + fsize)
612 fsize = b_size(&outbuf) - hsize;
613 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200614
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200615 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
616 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
617
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200618 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200619 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200620 count -= fsize;
621
622 if (fsize == bsize)
623 htx_remove_blk(htx, blk);
624 else
625 htx_cut_data_blk(htx, blk, fsize);
626
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200627 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200628 b_add(res, b_data(&outbuf));
629 goto new_frame;
630
631 end:
632 return total;
633}
634
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200635size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
636{
637 size_t total = 0;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100638 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200639 struct htx *htx;
640 enum htx_blk_type btype;
641 struct htx_blk *blk;
642 uint32_t bsize;
643 int32_t idx;
644 int ret;
645
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100646 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100647
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200648 htx = htx_from_buf(buf);
649
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100650 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200651 idx = htx_get_head(htx);
652 blk = htx_get_blk(htx, idx);
653 btype = htx_get_blk_type(blk);
654 bsize = htx_get_blksz(blk);
655
656 /* Not implemented : QUIC on backend side */
657 BUG_ON(btype == HTX_BLK_REQ_SL);
658
659 switch (btype) {
660 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200661 /* start-line -> HEADERS h3 frame */
662 ret = h3_resp_headers_send(qcs, htx);
663 if (ret > 0) {
664 total += ret;
665 count -= ret;
666 if (ret < bsize)
667 goto out;
668 }
669 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200670
671 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200672 ret = h3_resp_data_send(qcs, buf, count);
673 if (ret > 0) {
674 htx = htx_from_buf(buf);
675 total += ret;
676 count -= ret;
677 if (ret < bsize)
678 goto out;
679 }
680 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200681
682 case HTX_BLK_TLR:
683 case HTX_BLK_EOT:
684 /* TODO trailers */
685
686 default:
687 htx_remove_blk(htx, blk);
688 total += bsize;
689 count -= bsize;
690 break;
691 }
692 }
693
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100694 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
695 qcs->flags |= QC_SF_FIN_STREAM;
696
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200697 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200698 if (total) {
699 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
700 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
701 }
702
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200703 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200704}
705
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100706/* Finalize the initialization of remotely initiated uni-stream <qcs>.
707 * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error
708 * to inform the QUIC mux layer of the encountered error.
709 */
710static int h3_attach_ruqs(struct qcs *qcs, void *ctx)
711{
712 uint64_t strm_type;
713 struct h3 *h3 = ctx;
714 struct buffer *rxbuf = &qcs->rx.buf;
715
716 /* First octets: the uni-stream type */
717 if (!b_quic_dec_int(&strm_type, rxbuf, NULL) || strm_type > H3_UNI_STRM_TP_MAX)
718 return 0;
719
720 /* Note that for all the uni-streams below, this is an error to receive two times the
721 * same type of uni-stream (even for Push stream which is not supported at this time.
722 */
723 switch (strm_type) {
724 case H3_UNI_STRM_TP_CONTROL_STREAM:
725 if (h3->rctrl.qcs) {
726 h3->err = H3_STREAM_CREATION_ERROR;
727 return 0;
728 }
729
730 h3->rctrl.qcs = qcs;
731 h3->rctrl.cb = h3_control_recv;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100732 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100733 break;
734 case H3_UNI_STRM_TP_PUSH_STREAM:
735 /* NOT SUPPORTED */
736 break;
737 case H3_UNI_STRM_TP_QPACK_ENCODER:
738 if (h3->rqpack_enc.qcs) {
739 h3->err = H3_STREAM_CREATION_ERROR;
740 return 0;
741 }
742
743 h3->rqpack_enc.qcs = qcs;
744 h3->rqpack_enc.cb = qpack_decode_enc;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100745 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_enc.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100746 break;
747 case H3_UNI_STRM_TP_QPACK_DECODER:
748 if (h3->rqpack_dec.qcs) {
749 h3->err = H3_STREAM_CREATION_ERROR;
750 return 0;
751 }
752
753 h3->rqpack_dec.qcs = qcs;
754 h3->rqpack_dec.cb = qpack_decode_dec;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100755 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_dec.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100756 break;
757 default:
758 /* Error */
759 h3->err = H3_STREAM_CREATION_ERROR;
760 return 0;
761 }
762
763 return 1;
764}
765
766static int h3_finalize(void *ctx)
767{
768 struct h3 *h3 = ctx;
769
Amaury Denoyelleb7880542022-02-09 10:28:53 +0100770 h3->lctrl.qcs = qcs_new(h3->qcc, 0x3, QCS_SRV_UNI);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100771 if (!h3->lctrl.qcs)
772 return 0;
773
774 /* Wakeup ->lctrl uni-stream */
Frédéric Lécaillee16f0bd2021-08-23 09:50:29 +0200775 h3_control_send(&h3->lctrl, h3);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100776
777 return 1;
778}
779
780/* Tasklet dedicated to h3 incoming uni-streams */
781static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state)
782{
783 struct h3_uqs *h3_uqs = ctx;
784 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
785
786 h3_uqs->cb(h3_uqs, h3);
787 return NULL;
788}
789
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100790/* Release all the tasklet attached to <h3_uqs> uni-stream */
791static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs)
792{
793 struct tasklet *t = h3_uqs->wait_event.tasklet;
794
795 if (t)
796 tasklet_free(t);
797}
798
799/* Release all the tasklet attached to <h3> uni-streams */
800static void h3_uqs_tasklets_release(struct h3 *h3)
801{
802 h3_uqs_tasklet_release(&h3->rqpack_enc);
803 h3_uqs_tasklet_release(&h3->rqpack_dec);
804 h3_uqs_tasklet_release(&h3->rctrl);
805}
806
807/* Tasklet dedicated to h3 outgoing uni-streams */
808__maybe_unused
809static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state)
810{
811 struct h3_uqs *h3_uqs = ctx;
812 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
813
814 h3_uqs->cb(h3_uqs, h3);
815 return NULL;
816}
817
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500818/* Initialize <h3_uqs> uni-stream with <t> as tasklet */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100819static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3 *h3,
820 int (*cb)(struct h3_uqs *h3_uqs, void *ctx),
821 struct task *(*t)(struct task *, void *, unsigned int))
822{
823 h3_uqs->qcs = NULL;
824 h3_uqs->cb = cb;
825 h3_uqs->wait_event.tasklet = tasklet_new();
826 if (!h3_uqs->wait_event.tasklet)
827 return 0;
828
829 h3_uqs->wait_event.tasklet->process = t;
830 h3_uqs->wait_event.tasklet->context = h3_uqs;
Frédéric Lécaille000162e2022-04-01 09:04:57 +0200831 h3_uqs->wait_event.events = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100832 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100833}
834
835static inline void h3_uqs_release(struct h3_uqs *h3_uqs)
836{
837 if (h3_uqs->qcs)
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200838 qcs_free(h3_uqs->qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100839}
840
841static inline void h3_uqs_release_all(struct h3 *h3)
842{
843 h3_uqs_tasklet_release(&h3->lctrl);
844 h3_uqs_release(&h3->lctrl);
845 h3_uqs_tasklet_release(&h3->lqpack_enc);
846 h3_uqs_release(&h3->lqpack_enc);
847 h3_uqs_tasklet_release(&h3->lqpack_dec);
848 h3_uqs_release(&h3->lqpack_dec);
849}
850
851/* Initialize the HTTP/3 context for <qcc> mux.
852 * Return 1 if succeeded, 0 if not.
853 */
854static int h3_init(struct qcc *qcc)
855{
856 struct h3 *h3;
857
858 h3 = pool_alloc(pool_head_h3);
859 if (!h3)
860 goto fail_no_h3;
861
862 h3->qcc = qcc;
863 h3->err = H3_NO_ERROR;
864 h3->flags = 0;
865
866 if (!h3_uqs_init(&h3->rqpack_enc, h3, NULL, h3_uqs_task) ||
867 !h3_uqs_init(&h3->rqpack_dec, h3, NULL, h3_uqs_task) ||
868 !h3_uqs_init(&h3->rctrl, h3, h3_control_recv, h3_uqs_task))
869 goto fail_no_h3_ruqs;
870
871 if (!h3_uqs_init(&h3->lctrl, h3, h3_control_send, h3_uqs_task) ||
872 !h3_uqs_init(&h3->lqpack_enc, h3, NULL, h3_uqs_task) ||
873 !h3_uqs_init(&h3->lqpack_dec, h3, NULL, h3_uqs_task))
874 goto fail_no_h3_luqs;
875
876 qcc->ctx = h3;
877 LIST_INIT(&h3->buf_wait.list);
878
879 return 1;
880
881 fail_no_h3_ruqs:
882 h3_uqs_release_all(h3);
883 fail_no_h3_luqs:
884 h3_uqs_tasklets_release(h3);
885 pool_free(pool_head_h3, h3);
886 fail_no_h3:
887 return 0;
888}
889
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200890static void h3_release(void *ctx)
891{
892 struct h3 *h3 = ctx;
893
894 h3_uqs_release_all(h3);
895 h3_uqs_tasklets_release(h3);
896 pool_free(pool_head_h3, h3);
897}
898
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200899/* Check if the H3 connection can still be considered as active.
900 *
901 * Return true if active else false.
902 */
903static int h3_is_active(const struct qcc *qcc, void *ctx)
904{
905 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
906 return 1;
907
908 return 0;
909}
910
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100911/* HTTP/3 application layer operations */
912const struct qcc_app_ops h3_ops = {
913 .init = h3_init,
914 .attach_ruqs = h3_attach_ruqs,
915 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +0100916 .snd_buf = h3_snd_buf,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100917 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200918 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200919 .release = h3_release,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100920};