blob: 7fbd483c6b0127d50e407c5baf693414884a3b63 [file] [log] [blame]
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001/*
2 * HTTP/3 protocol processing
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation, version 2.1
7 * exclusively.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <haproxy/buf.h>
Amaury Denoyelle99043552021-08-24 15:36:02 +020020#include <haproxy/connection.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010021#include <haproxy/conn_stream.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010022#include <haproxy/dynbuf.h>
23#include <haproxy/h3.h>
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +020024#include <haproxy/http.h>
25#include <haproxy/htx.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010026#include <haproxy/istbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010027#include <haproxy/mux_quic-t.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010028#include <haproxy/pool.h>
29#include <haproxy/qpack-dec.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020030#include <haproxy/qpack-enc.h>
31#include <haproxy/quic_enc.h>
Amaury Denoyelle99043552021-08-24 15:36:02 +020032#include <haproxy/stream.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010033#include <haproxy/tools.h>
34#include <haproxy/xprt_quic.h>
35
36#define DEBUG_H3
37
38#if defined(DEBUG_H3)
39#define h3_debug_printf fprintf
40#define h3_debug_hexdump debug_hexdump
41#else
42#define h3_debug_printf(...) do { } while (0)
43#define h3_debug_hexdump(...) do { } while (0)
44#endif
45
46#define H3_CF_SETTINGS_SENT 0x00000001
47
48/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +020049static uint64_t h3_settings_qpack_max_table_capacity = 0;
50static uint64_t h3_settings_qpack_blocked_streams = 4096;
51static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010052
53struct h3 {
54 struct qcc *qcc;
55 enum h3_err err;
56 uint32_t flags;
57 /* Locally initiated uni-streams */
58 struct h3_uqs lqpack_enc;
59 struct h3_uqs lqpack_dec;
60 struct h3_uqs lctrl;
61 /* Remotely initiated uni-streams */
62 struct h3_uqs rqpack_enc;
63 struct h3_uqs rqpack_dec;
64 struct h3_uqs rctrl;
65 /* Settings */
66 uint64_t qpack_max_table_capacity;
67 uint64_t qpack_blocked_streams;
68 uint64_t max_field_section_size;
69 struct buffer_wait buf_wait; /* wait list for buffer allocations */
70};
71
72DECLARE_STATIC_POOL(pool_head_h3, "h3", sizeof(struct h3));
73
74/* Simple function to duplicate a buffer */
75static inline struct buffer h3_b_dup(struct buffer *b)
76{
77 return b_make(b->area, b->size, b->head, b->data);
78}
79
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010080/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
81 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
82 * Note that this function update <b> buffer to reflect the number of bytes consumed
83 * to decode the h3 frame header.
84 */
85static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
86 struct buffer *b)
87{
88 size_t hlen;
89
90 hlen = 0;
91 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
92 return 0;
93
94 return hlen;
95}
96
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +010097/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
98 * in a local HTX buffer and transfer to the conn-stream layer. <fin> must be
99 * set if this is the last data to transfer from this stream.
100 *
101 * Returns 0 on success else non-zero.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100102 */
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100103static int h3_headers_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
104 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100105{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100106 struct buffer htx_buf = BUF_NULL;
107 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100108 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200109 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200110 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100111 struct conn_stream *cs;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200112 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100113 struct ist meth = IST_NULL, path = IST_NULL;
114 //struct ist scheme = IST_NULL, authority = IST_NULL;
115 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200116 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100117
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100118 if (qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp, list) < 0)
119 return 1;
120
121 qc_get_buf(qcs, &htx_buf);
122 BUG_ON(!b_size(&htx_buf));
123 htx = htx_from_buf(&htx_buf);
124
125 /* first treat pseudo-header to build the start line */
126 hdr_idx = 0;
127 while (1) {
128 if (isteq(list[hdr_idx].n, ist("")))
129 break;
130
131 if (istmatch(list[hdr_idx].n, ist(":"))) {
132 /* pseudo-header */
133 if (isteq(list[hdr_idx].n, ist(":method")))
134 meth = list[hdr_idx].v;
135 else if (isteq(list[hdr_idx].n, ist(":path")))
136 path = list[hdr_idx].v;
137 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
138 // scheme = list[hdr_idx].v;
139 else if (isteq(list[hdr_idx].n, ist(":authority")))
140 authority = list[hdr_idx].v;
141 }
142
143 ++hdr_idx;
144 }
145
146 flags |= HTX_SL_F_VER_11;
147
148 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
149 if (!sl)
150 return 1;
151
152 if (fin)
153 sl->flags |= HTX_SL_F_BODYLESS;
154
155 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
156 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
157
158 if (isttest(authority))
159 htx_add_header(htx, ist("host"), authority);
160
161 /* now treat standard headers */
162 hdr_idx = 0;
163 while (1) {
164 if (isteq(list[hdr_idx].n, ist("")))
165 break;
166
167 if (!istmatch(list[hdr_idx].n, ist(":")))
168 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
169
170 ++hdr_idx;
171 }
172
173 htx_add_endof(htx, HTX_BLK_EOH);
174 htx_to_buf(htx, &htx_buf);
175
176 if (fin)
177 htx->flags |= HTX_FL_EOM;
178
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100179 cs = cs_new();
Frédéric Lécaille59509b52022-02-15 09:25:06 +0100180 if (!cs)
181 return 1;
Christopher Faulet9264a2c2022-02-24 11:13:57 +0100182 cs_attach_endp(cs, &qcs->qcc->conn->obj_type, qcs);
Frédéric Lécaille59509b52022-02-15 09:25:06 +0100183
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100184 cs->flags |= CS_FL_NOT_FIRST;
185 cs->ctx = qcs;
Christopher Faulet9264a2c2022-02-24 11:13:57 +0100186 stream_new(qcs->qcc->conn->owner, cs, &htx_buf);
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100187
188 /* buffer is transferred to conn_stream and set to NULL
189 * except on stream creation error.
190 */
191 b_free(&htx_buf);
192 offer_buffers(NULL, 1);
193
194 return 0;
195}
196
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100197/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
198 * HTX buffer. <fin> must be set if this is the last data to transfer from this
199 * stream.
200 *
201 * Returns 0 on success else non-zero
202 */
203static int h3_data_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len,
204 char fin)
205{
206 struct buffer *appbuf;
207 struct htx *htx = NULL;
208 size_t htx_sent;
209 int htx_space;
210
211 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
212 BUG_ON(!appbuf);
213 htx = htx_from_buf(appbuf);
214
215 htx_space = htx_free_data_space(htx);
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100216 if (!htx_space || htx_space < len) {
217 ABORT_NOW(); /* TODO handle this case properly */
218 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100219
220 htx_sent = htx_add_data(htx, ist2(b_head(buf), len));
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100221 if (htx_sent < len) {
222 ABORT_NOW(); /* TODO handle this case properly */
223 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100224
225 if (fin)
226 htx->flags |= HTX_FL_EOM;
227 htx_to_buf(htx, appbuf);
228
229 return 0;
230}
231
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100232/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
233 * that we received the last data of the stream.
234 * Returns <0 on error else 0.
235 */
236static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
237{
238 struct buffer *rxbuf = &qcs->rx.buf;
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100239 int ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100240
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100241 h3_debug_printf(stderr, "%s: STREAM ID: %llu\n", __func__, qcs->by_id.key);
242 if (!b_data(rxbuf))
243 return 0;
244
245 while (b_data(rxbuf)) {
246 size_t hlen;
247 uint64_t ftype, flen;
248 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100249 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100250
251 /* Work on a copy of <rxbuf> */
252 b = h3_b_dup(rxbuf);
253 hlen = h3_decode_frm_header(&ftype, &flen, &b);
254 if (!hlen)
255 break;
256
257 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
258 (unsigned long long)ftype, (unsigned long long)flen);
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100259 if (flen > b_data(&b) && !b_full(rxbuf))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100260 break;
261
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100262 /* TODO handle full rxbuf */
263 BUG_ON(flen > b_size(rxbuf));
264
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100265 b_del(rxbuf, hlen);
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100266 last_stream_frame = (fin && flen == b_data(rxbuf));
267
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100268 switch (ftype) {
269 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100270 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
271 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100272 if (ret) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100273 break;
274 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100275 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
276 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100277 if (ret) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100278 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100279 case H3_FT_PUSH_PROMISE:
280 /* Not supported */
281 break;
282 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100283 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
284 * unknown frame types MUST be ignored
285 */
286 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100287 }
288 b_del(rxbuf, flen);
289 }
290
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100291 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100292}
293
294/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
295 * <rxbuf> buffer. This function does not update this buffer.
296 * Returns 0 if something wrong happened, 1 if not.
297 */
298static int h3_parse_settings_frm(struct h3 *h3, const struct buffer *rxbuf, size_t flen)
299{
300 uint64_t id, value;
301 const unsigned char *buf, *end;
302
303 buf = (const unsigned char *)b_head(rxbuf);
304 end = buf + flen;
305
306 while (buf <= end) {
307 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
308 return 0;
309
310 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
311 __func__, (unsigned long long)id, (unsigned long long)value);
312 switch (id) {
313 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
314 h3->qpack_max_table_capacity = value;
315 break;
316 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
317 h3->max_field_section_size = value;
318 break;
319 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
320 h3->qpack_blocked_streams = value;
321 break;
322 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
323 h3->err = H3_SETTINGS_ERROR;
324 return 0;
325 default:
326 /* MUST be ignored */
327 break;
328 }
329 }
330
331 return 1;
332}
333
334/* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as
335 * there is not enough received data.
336 * Returns 0 if something wrong happened, 1 if not.
337 */
338static int h3_control_recv(struct h3_uqs *h3_uqs, void *ctx)
339{
340 struct buffer *rxbuf = &h3_uqs->qcs->rx.buf;
341 struct h3 *h3 = ctx;
342
343 h3_debug_printf(stderr, "%s STREAM ID: %llu\n", __func__, h3_uqs->qcs->by_id.key);
344 if (!b_data(rxbuf))
345 return 1;
346
347 while (b_data(rxbuf)) {
348 size_t hlen;
349 uint64_t ftype, flen;
350 struct buffer b;
351
352 /* Work on a copy of <rxbuf> */
353 b = h3_b_dup(rxbuf);
354 hlen = h3_decode_frm_header(&ftype, &flen, &b);
355 if (!hlen)
356 break;
357
358 h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__,
359 (unsigned long long)ftype, (unsigned long long)flen);
360 if (flen > b_data(&b))
361 break;
362
363 b_del(rxbuf, hlen);
364 /* From here, a frame must not be truncated */
365 switch (ftype) {
366 case H3_FT_CANCEL_PUSH:
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_SETTINGS:
371 if (!h3_parse_settings_frm(h3, rxbuf, flen))
372 return 0;
373 break;
374 case H3_FT_GOAWAY:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100375 /* XXX TODO XXX */
376 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100377 break;
378 case H3_FT_MAX_PUSH_ID:
Amaury Denoyellee1f3ff02021-12-06 14:26:52 +0100379 /* XXX TODO XXX */
380 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100381 break;
382 default:
383 /* Error */
384 h3->err = H3_FRAME_UNEXPECTED;
385 return 0;
386 }
387 b_del(rxbuf, flen);
388 }
389
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100390 /* Handle the case where remaining data are present in the buffer. This
391 * can happen if there is an incomplete frame. In this case, subscribe
392 * on the lower layer to restart receive operation.
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100393 */
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100394 if (b_data(rxbuf))
395 qcs_subscribe(h3_uqs->qcs, SUB_RETRY_RECV, &h3_uqs->wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100396
397 return 1;
398}
399
Amaury Denoyellea5871362021-10-07 16:26:12 +0200400/* Returns buffer for data sending.
401 * May be NULL if the allocation failed.
402 */
403static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100404{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200405 if (!b_size(&qcs->tx.buf))
406 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100407
Amaury Denoyellea5871362021-10-07 16:26:12 +0200408 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100409}
410
411/* Function used to emit stream data from <h3_uqs> control uni-stream */
412static int h3_control_send(struct h3_uqs *h3_uqs, void *ctx)
413{
414 int ret;
415 struct h3 *h3 = ctx;
416 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200417 struct buffer pos, *res;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100418
419 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200420 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100421 if (!(h3->flags & H3_CF_SETTINGS_SENT)) {
422 struct qcs *qcs = h3_uqs->qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100423 size_t frm_len;
424
425 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
426 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
427 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
428 quic_int_getsize(h3_settings_qpack_blocked_streams);
429 if (h3_settings_max_field_section_size) {
430 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
431 quic_int_getsize(h3_settings_max_field_section_size);
432 }
433
Amaury Denoyellea5871362021-10-07 16:26:12 +0200434 b_quic_enc_int(&pos, H3_UNI_STRM_TP_CONTROL_STREAM);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100435 /* Build a SETTINGS frame */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200436 b_quic_enc_int(&pos, H3_FT_SETTINGS);
437 b_quic_enc_int(&pos, frm_len);
438 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
439 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
440 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
441 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100442 if (h3_settings_max_field_section_size) {
Amaury Denoyellea5871362021-10-07 16:26:12 +0200443 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
444 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100445 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200446
447 res = mux_get_buf(qcs);
448 if (b_room(res) < b_data(&pos)) {
449 // TODO the mux should be put in blocked state, with
450 // the stream in state waiting for settings to be sent
451 ABORT_NOW();
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100452 }
453
Amaury Denoyellea5871362021-10-07 16:26:12 +0200454 ret = b_force_xfer(res, &pos, b_data(&pos));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100455 if (ret > 0) {
456 h3->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200457 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
458 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100459 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100460 }
461
462 return ret;
463}
464
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200465static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
466{
467 struct buffer outbuf;
468 struct buffer headers_buf = BUF_NULL;
469 struct buffer *res;
470 struct http_hdr list[global.tune.max_http_hdr];
471 struct htx_sl *sl;
472 struct htx_blk *blk;
473 enum htx_blk_type type;
474 int frame_length_size; /* size in bytes of frame length varint field */
475 int ret = 0;
476 int hdr;
477 int status = 0;
478
479 sl = NULL;
480 hdr = 0;
481 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
482 type = htx_get_blk_type(blk);
483
484 if (type == HTX_BLK_UNUSED)
485 continue;
486
487 if (type == HTX_BLK_EOH)
488 break;
489
490 if (type == HTX_BLK_RES_SL) {
491 /* start-line -> HEADERS h3 frame */
492 BUG_ON(sl);
493 sl = htx_get_blk_ptr(htx, blk);
494 /* TODO should be on h3 layer */
495 status = sl->info.res.status;
496 }
497 else if (type == HTX_BLK_HDR) {
498 list[hdr].n = htx_get_blk_name(htx, blk);
499 list[hdr].v = htx_get_blk_value(htx, blk);
500 hdr++;
501 }
502 else {
503 ABORT_NOW();
504 goto err;
505 }
506 }
507
508 BUG_ON(!sl);
509
510 list[hdr].n = ist("");
511
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200512 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200513
514 /* At least 5 bytes to store frame type + length as a varint max size */
515 if (b_room(res) < 5)
516 ABORT_NOW();
517
518 b_reset(&outbuf);
519 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
520 /* Start the headers after frame type + length */
521 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
522
523 if (qpack_encode_field_section_line(&headers_buf))
524 ABORT_NOW();
525 if (qpack_encode_int_status(&headers_buf, status))
526 ABORT_NOW();
527
528 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
529 if (isteq(list[hdr].n, ist("")))
530 break;
531
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100532 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
533 * Transfer codings (see Section 6.1 of [HTTP11]) are not
534 * defined for HTTP/3; the Transfer-Encoding header field MUST
535 * NOT be used.
536 */
537 if (isteq(list[hdr].n, ist("transfer-encoding")))
538 continue;
539
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200540 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
541 ABORT_NOW();
542 }
543
544 /* Now that all headers are encoded, we are certain that res buffer is
545 * big enough
546 */
547 frame_length_size = quic_int_getsize(b_data(&headers_buf));
548 res->head += 4 - frame_length_size;
549 b_putchr(res, 0x01); /* h3 HEADERS frame type */
550 if (!b_quic_enc_int(res, b_data(&headers_buf)))
551 ABORT_NOW();
552 b_add(res, b_data(&headers_buf));
553
554 ret = 0;
555 blk = htx_get_head_blk(htx);
556 while (blk) {
557 type = htx_get_blk_type(blk);
558 ret += htx_get_blksz(blk);
559 blk = htx_remove_blk(htx, blk);
560 if (type == HTX_BLK_EOH)
561 break;
562 }
563
564 return ret;
565
566 err:
567 return 0;
568}
569
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200570/* Returns the total of bytes sent. */
571static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
572{
573 struct buffer outbuf;
574 struct buffer *res;
575 size_t total = 0;
576 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200577 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200578 struct htx_blk *blk;
579 enum htx_blk_type type;
580
581 htx = htx_from_buf(buf);
582
583 new_frame:
584 if (!count || htx_is_empty(htx))
585 goto end;
586
587 blk = htx_get_head_blk(htx);
588 type = htx_get_blk_type(blk);
589 fsize = bsize = htx_get_blksz(blk);
590
591 if (type != HTX_BLK_DATA)
592 goto end;
593
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200594 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200595
596 if (fsize > count)
597 fsize = count;
598
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200599 /* h3 DATA headers : 1-byte frame type + varint frame length */
600 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200601
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200602 while (1) {
603 b_reset(&outbuf);
604 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
605 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
606 break;
607 b_slow_realign(res, trash.area, b_data(res));
608 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200609
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100610 /* Not enough room for headers and at least one data byte, block the
611 * stream. It is expected that the conn-stream layer will subscribe on
612 * SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200613 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100614 if (b_size(&outbuf) <= hsize) {
615 qcs->flags |= QC_SF_BLK_MROOM;
616 goto end;
617 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200618
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200619 if (b_size(&outbuf) < hsize + fsize)
620 fsize = b_size(&outbuf) - hsize;
621 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200622
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200623 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
624 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
625
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200626 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200627 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200628 count -= fsize;
629
630 if (fsize == bsize)
631 htx_remove_blk(htx, blk);
632 else
633 htx_cut_data_blk(htx, blk, fsize);
634
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200635 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200636 b_add(res, b_data(&outbuf));
637 goto new_frame;
638
639 end:
640 return total;
641}
642
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200643size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
644{
645 size_t total = 0;
646 struct qcs *qcs = cs->ctx;
647 struct htx *htx;
648 enum htx_blk_type btype;
649 struct htx_blk *blk;
650 uint32_t bsize;
651 int32_t idx;
652 int ret;
653
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100654 fprintf(stderr, "%s\n", __func__);
655
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200656 htx = htx_from_buf(buf);
657
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100658 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200659 idx = htx_get_head(htx);
660 blk = htx_get_blk(htx, idx);
661 btype = htx_get_blk_type(blk);
662 bsize = htx_get_blksz(blk);
663
664 /* Not implemented : QUIC on backend side */
665 BUG_ON(btype == HTX_BLK_REQ_SL);
666
667 switch (btype) {
668 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200669 /* start-line -> HEADERS h3 frame */
670 ret = h3_resp_headers_send(qcs, htx);
671 if (ret > 0) {
672 total += ret;
673 count -= ret;
674 if (ret < bsize)
675 goto out;
676 }
677 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200678
679 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200680 ret = h3_resp_data_send(qcs, buf, count);
681 if (ret > 0) {
682 htx = htx_from_buf(buf);
683 total += ret;
684 count -= ret;
685 if (ret < bsize)
686 goto out;
687 }
688 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200689
690 case HTX_BLK_TLR:
691 case HTX_BLK_EOT:
692 /* TODO trailers */
693
694 default:
695 htx_remove_blk(htx, blk);
696 total += bsize;
697 count -= bsize;
698 break;
699 }
700 }
701
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100702 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
703 qcs->flags |= QC_SF_FIN_STREAM;
704
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200705 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200706 if (total) {
707 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
708 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
709 }
710
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200711 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200712}
713
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100714/* Finalize the initialization of remotely initiated uni-stream <qcs>.
715 * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error
716 * to inform the QUIC mux layer of the encountered error.
717 */
718static int h3_attach_ruqs(struct qcs *qcs, void *ctx)
719{
720 uint64_t strm_type;
721 struct h3 *h3 = ctx;
722 struct buffer *rxbuf = &qcs->rx.buf;
723
724 /* First octets: the uni-stream type */
725 if (!b_quic_dec_int(&strm_type, rxbuf, NULL) || strm_type > H3_UNI_STRM_TP_MAX)
726 return 0;
727
728 /* Note that for all the uni-streams below, this is an error to receive two times the
729 * same type of uni-stream (even for Push stream which is not supported at this time.
730 */
731 switch (strm_type) {
732 case H3_UNI_STRM_TP_CONTROL_STREAM:
733 if (h3->rctrl.qcs) {
734 h3->err = H3_STREAM_CREATION_ERROR;
735 return 0;
736 }
737
738 h3->rctrl.qcs = qcs;
739 h3->rctrl.cb = h3_control_recv;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100740 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100741 break;
742 case H3_UNI_STRM_TP_PUSH_STREAM:
743 /* NOT SUPPORTED */
744 break;
745 case H3_UNI_STRM_TP_QPACK_ENCODER:
746 if (h3->rqpack_enc.qcs) {
747 h3->err = H3_STREAM_CREATION_ERROR;
748 return 0;
749 }
750
751 h3->rqpack_enc.qcs = qcs;
752 h3->rqpack_enc.cb = qpack_decode_enc;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100753 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_enc.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100754 break;
755 case H3_UNI_STRM_TP_QPACK_DECODER:
756 if (h3->rqpack_dec.qcs) {
757 h3->err = H3_STREAM_CREATION_ERROR;
758 return 0;
759 }
760
761 h3->rqpack_dec.qcs = qcs;
762 h3->rqpack_dec.cb = qpack_decode_dec;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100763 qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_dec.wait_event);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100764 break;
765 default:
766 /* Error */
767 h3->err = H3_STREAM_CREATION_ERROR;
768 return 0;
769 }
770
771 return 1;
772}
773
774static int h3_finalize(void *ctx)
775{
776 struct h3 *h3 = ctx;
777
Amaury Denoyelleb7880542022-02-09 10:28:53 +0100778 h3->lctrl.qcs = qcs_new(h3->qcc, 0x3, QCS_SRV_UNI);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100779 if (!h3->lctrl.qcs)
780 return 0;
781
782 /* Wakeup ->lctrl uni-stream */
Frédéric Lécaillee16f0bd2021-08-23 09:50:29 +0200783 h3_control_send(&h3->lctrl, h3);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100784
785 return 1;
786}
787
788/* Tasklet dedicated to h3 incoming uni-streams */
789static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state)
790{
791 struct h3_uqs *h3_uqs = ctx;
792 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
793
794 h3_uqs->cb(h3_uqs, h3);
795 return NULL;
796}
797
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100798/* Release all the tasklet attached to <h3_uqs> uni-stream */
799static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs)
800{
801 struct tasklet *t = h3_uqs->wait_event.tasklet;
802
803 if (t)
804 tasklet_free(t);
805}
806
807/* Release all the tasklet attached to <h3> uni-streams */
808static void h3_uqs_tasklets_release(struct h3 *h3)
809{
810 h3_uqs_tasklet_release(&h3->rqpack_enc);
811 h3_uqs_tasklet_release(&h3->rqpack_dec);
812 h3_uqs_tasklet_release(&h3->rctrl);
813}
814
815/* Tasklet dedicated to h3 outgoing uni-streams */
816__maybe_unused
817static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state)
818{
819 struct h3_uqs *h3_uqs = ctx;
820 struct h3 *h3 = h3_uqs->qcs->qcc->ctx;
821
822 h3_uqs->cb(h3_uqs, h3);
823 return NULL;
824}
825
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500826/* Initialize <h3_uqs> uni-stream with <t> as tasklet */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100827static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3 *h3,
828 int (*cb)(struct h3_uqs *h3_uqs, void *ctx),
829 struct task *(*t)(struct task *, void *, unsigned int))
830{
831 h3_uqs->qcs = NULL;
832 h3_uqs->cb = cb;
833 h3_uqs->wait_event.tasklet = tasklet_new();
834 if (!h3_uqs->wait_event.tasklet)
835 return 0;
836
837 h3_uqs->wait_event.tasklet->process = t;
838 h3_uqs->wait_event.tasklet->context = h3_uqs;
839 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100840}
841
842static inline void h3_uqs_release(struct h3_uqs *h3_uqs)
843{
844 if (h3_uqs->qcs)
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100845 uni_qcs_free(h3_uqs->qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100846}
847
848static inline void h3_uqs_release_all(struct h3 *h3)
849{
850 h3_uqs_tasklet_release(&h3->lctrl);
851 h3_uqs_release(&h3->lctrl);
852 h3_uqs_tasklet_release(&h3->lqpack_enc);
853 h3_uqs_release(&h3->lqpack_enc);
854 h3_uqs_tasklet_release(&h3->lqpack_dec);
855 h3_uqs_release(&h3->lqpack_dec);
856}
857
858/* Initialize the HTTP/3 context for <qcc> mux.
859 * Return 1 if succeeded, 0 if not.
860 */
861static int h3_init(struct qcc *qcc)
862{
863 struct h3 *h3;
864
865 h3 = pool_alloc(pool_head_h3);
866 if (!h3)
867 goto fail_no_h3;
868
869 h3->qcc = qcc;
870 h3->err = H3_NO_ERROR;
871 h3->flags = 0;
872
873 if (!h3_uqs_init(&h3->rqpack_enc, h3, NULL, h3_uqs_task) ||
874 !h3_uqs_init(&h3->rqpack_dec, h3, NULL, h3_uqs_task) ||
875 !h3_uqs_init(&h3->rctrl, h3, h3_control_recv, h3_uqs_task))
876 goto fail_no_h3_ruqs;
877
878 if (!h3_uqs_init(&h3->lctrl, h3, h3_control_send, h3_uqs_task) ||
879 !h3_uqs_init(&h3->lqpack_enc, h3, NULL, h3_uqs_task) ||
880 !h3_uqs_init(&h3->lqpack_dec, h3, NULL, h3_uqs_task))
881 goto fail_no_h3_luqs;
882
883 qcc->ctx = h3;
884 LIST_INIT(&h3->buf_wait.list);
885
886 return 1;
887
888 fail_no_h3_ruqs:
889 h3_uqs_release_all(h3);
890 fail_no_h3_luqs:
891 h3_uqs_tasklets_release(h3);
892 pool_free(pool_head_h3, h3);
893 fail_no_h3:
894 return 0;
895}
896
897/* HTTP/3 application layer operations */
898const struct qcc_app_ops h3_ops = {
899 .init = h3_init,
900 .attach_ruqs = h3_attach_ruqs,
901 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +0100902 .snd_buf = h3_snd_buf,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100903 .finalize = h3_finalize,
904};