blob: 3ff45ffe1c382b90c95a190a8a9a072edb85a8cf [file] [log] [blame]
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001/*
2 * HTTP/3 protocol processing
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation, version 2.1
7 * exclusively.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <haproxy/buf.h>
Amaury Denoyelle99043552021-08-24 15:36:02 +020020#include <haproxy/connection.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010021#include <haproxy/dynbuf.h>
22#include <haproxy/h3.h>
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +020023#include <haproxy/h3_stats.h>
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +020024#include <haproxy/http.h>
25#include <haproxy/htx.h>
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +020026#include <haproxy/intops.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010027#include <haproxy/istbuf.h>
Amaury Denoyelle846cc042022-04-04 16:13:44 +020028#include <haproxy/mux_quic.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020029#include <haproxy/ncbuf.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010030#include <haproxy/pool.h>
31#include <haproxy/qpack-dec.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020032#include <haproxy/qpack-enc.h>
33#include <haproxy/quic_enc.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020034#include <haproxy/stconn.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010035#include <haproxy/tools.h>
36#include <haproxy/xprt_quic.h>
37
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010038#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
Amaury Denoyelle302ecd42022-05-24 15:24:32 +020046#define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */
47#define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */
48#define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */
49#define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */
50#define H3_CF_UNI_QPACK_ENC_SET 0x00000010 /* Remote QPACK encoder stream opened */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010051
52/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +020053static uint64_t h3_settings_qpack_max_table_capacity = 0;
54static uint64_t h3_settings_qpack_blocked_streams = 4096;
55static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010056
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +020057struct h3c {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010058 struct qcc *qcc;
59 enum h3_err err;
60 uint32_t flags;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +020061
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010062 /* Settings */
63 uint64_t qpack_max_table_capacity;
64 uint64_t qpack_blocked_streams;
65 uint64_t max_field_section_size;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +020066
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010067 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +020068 /* Stats counters */
69 struct h3_counters *prx_counters;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010070};
71
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +020072DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010073
Amaury Denoyelle35550642022-05-24 15:14:53 +020074#define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */
Amaury Denoyellefc99a692022-05-24 15:25:19 +020075#define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */
Amaury Denoyelle35550642022-05-24 15:14:53 +020076
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020077struct h3s {
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +020078 enum h3s_t type;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +020079 int demux_frame_len;
80 int demux_frame_type;
Amaury Denoyelle35550642022-05-24 15:14:53 +020081
82 int flags;
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020083};
84
85DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s));
86
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010087/* Simple function to duplicate a buffer */
Amaury Denoyellec7dd9d62022-05-24 18:14:28 +020088static inline struct buffer h3_b_dup(const struct ncbuf *b)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010089{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020090 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010091}
92
Amaury Denoyelle35550642022-05-24 15:14:53 +020093/* Initialize an uni-stream <qcs> by reading its type from <rxbuf>.
94 *
95 * Returns 0 on success else non-zero.
96 */
97static int h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs,
98 struct ncbuf *rxbuf)
99{
100 /* decode unidirectional stream type */
101 struct h3s *h3s = qcs->ctx;
102 struct buffer b;
103 uint64_t type;
104 size_t len = 0, ret;
105
106 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
107 h3s->flags & H3_SF_UNI_INIT);
108
109 b = h3_b_dup(rxbuf);
110 ret = b_quic_dec_int(&type, &b, &len);
111 if (!ret) {
112 ABORT_NOW();
113 }
114
115 switch (type) {
116 case H3_UNI_S_T_CTRL:
117 if (h3c->flags & H3_CF_UNI_CTRL_SET) {
118 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
119 return 1;
120 }
121 h3c->flags |= H3_CF_UNI_CTRL_SET;
122 h3s->type = H3S_T_CTRL;
123 break;
124
125 case H3_UNI_S_T_PUSH:
126 /* TODO not supported for the moment */
127 h3s->type = H3S_T_PUSH;
128 break;
129
130 case H3_UNI_S_T_QPACK_DEC:
131 if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) {
132 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
133 return 1;
134 }
135 h3c->flags |= H3_CF_UNI_QPACK_DEC_SET;
136 h3s->type = H3S_T_QPACK_DEC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200137 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200138 break;
139
140 case H3_UNI_S_T_QPACK_ENC:
141 if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) {
142 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
143 return 1;
144 }
145 h3c->flags |= H3_CF_UNI_QPACK_ENC_SET;
146 h3s->type = H3S_T_QPACK_ENC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200147 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200148 break;
149
150 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200151 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
152 *
153 * Implementations MUST [...] abort reading on unidirectional
154 * streams that have unknown or unsupported types.
155 */
156 qcs->flags |= QC_SF_READ_ABORTED;
157 return 1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200158 };
159
160 h3s->flags |= H3_SF_UNI_INIT;
161 qcs_consume(qcs, len);
162
163 return 0;
164}
165
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200166/* Parse an uni-stream <qcs> from <rxbuf> which does not contains H3 frames.
167 * This may be used for QPACK encoder/decoder streams for example.
168 *
169 * Returns 0 on success else non-zero.
170 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200171static int h3_parse_uni_stream_no_h3(struct qcs *qcs, struct ncbuf *rxbuf)
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200172{
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200173 struct h3s *h3s = qcs->ctx;
174
175 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
176 !(h3s->flags & H3_SF_UNI_NO_H3));
177
178 switch (h3s->type) {
179 case H3S_T_QPACK_DEC:
180 if (!qpack_decode_dec(qcs, NULL))
181 return 1;
182 break;
183 case H3S_T_QPACK_ENC:
184 if (!qpack_decode_enc(qcs, NULL))
185 return 1;
186 break;
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200187 case H3S_T_UNKNOWN:
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200188 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200189 /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */
190 ABORT_NOW();
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200191 }
192
193 return 0;
194}
195
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100196/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
197 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
198 * Note that this function update <b> buffer to reflect the number of bytes consumed
199 * to decode the h3 frame header.
200 */
201static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
202 struct buffer *b)
203{
204 size_t hlen;
205
206 hlen = 0;
207 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
208 return 0;
209
210 return hlen;
211}
212
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200213/* Check if H3 frame of type <ftype> is valid when received on stream <qcs>.
214 *
215 * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should
216 * be reported.
217 */
218static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype)
219{
220 struct h3s *h3s = qcs->ctx;
221 const uint64_t id = qcs->id;
222
223 BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN);
224
225 switch (ftype) {
226 case H3_FT_DATA:
227 case H3_FT_HEADERS:
228 return h3s->type != H3S_T_CTRL;
229
230 case H3_FT_CANCEL_PUSH:
231 case H3_FT_GOAWAY:
232 case H3_FT_MAX_PUSH_ID:
233 /* Only allowed for control stream. First frame of control
234 * stream MUST be SETTINGS.
235 */
236 return h3s->type == H3S_T_CTRL &&
237 (h3c->flags & H3_CF_SETTINGS_RECV);
238
239 case H3_FT_SETTINGS:
240 /* draft-ietf-quic-http34 7.2.4. SETTINGS
241 *
242 * If an endpoint receives a second SETTINGS frame on the control
243 * stream, the endpoint MUST respond with a connection error of type
244 * H3_FRAME_UNEXPECTED.
245 */
246 return h3s->type == H3S_T_CTRL &&
247 !(h3c->flags & H3_CF_SETTINGS_RECV);
248
249 case H3_FT_PUSH_PROMISE:
250 return h3s->type != H3S_T_CTRL &&
251 (id & QCS_ID_SRV_INTIATOR_BIT);
252
253 default:
254 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
255 *
256 * Implementations MUST discard frames [...] that have unknown
257 * or unsupported types.
258 */
259 return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV);
260 }
261}
262
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100263/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
Willy Tarreau4596fe22022-05-17 19:07:51 +0200264 * in a local HTX buffer and transfer to the stream connector layer. <fin> must be
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100265 * set if this is the last data to transfer from this stream.
266 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200267 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100268 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200269static int h3_headers_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100270 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100271{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100272 struct buffer htx_buf = BUF_NULL;
273 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100274 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200275 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200276 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200277 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100278 struct ist meth = IST_NULL, path = IST_NULL;
279 //struct ist scheme = IST_NULL, authority = IST_NULL;
280 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200281 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100282
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200283 /* TODO support buffer wrapping */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200284 BUG_ON(ncb_head(buf) + len >= ncb_wrap(buf));
285 if (qpack_decode_fs((const unsigned char *)ncb_head(buf), len, tmp, list) < 0)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200286 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100287
288 qc_get_buf(qcs, &htx_buf);
289 BUG_ON(!b_size(&htx_buf));
290 htx = htx_from_buf(&htx_buf);
291
292 /* first treat pseudo-header to build the start line */
293 hdr_idx = 0;
294 while (1) {
295 if (isteq(list[hdr_idx].n, ist("")))
296 break;
297
298 if (istmatch(list[hdr_idx].n, ist(":"))) {
299 /* pseudo-header */
300 if (isteq(list[hdr_idx].n, ist(":method")))
301 meth = list[hdr_idx].v;
302 else if (isteq(list[hdr_idx].n, ist(":path")))
303 path = list[hdr_idx].v;
304 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
305 // scheme = list[hdr_idx].v;
306 else if (isteq(list[hdr_idx].n, ist(":authority")))
307 authority = list[hdr_idx].v;
308 }
309
310 ++hdr_idx;
311 }
312
313 flags |= HTX_SL_F_VER_11;
Amaury Denoyelle0fa14a62022-04-26 16:24:39 +0200314 flags |= HTX_SL_F_XFER_LEN;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100315
316 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
317 if (!sl)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200318 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100319
320 if (fin)
321 sl->flags |= HTX_SL_F_BODYLESS;
322
323 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100324
325 if (isttest(authority))
326 htx_add_header(htx, ist("host"), authority);
327
328 /* now treat standard headers */
329 hdr_idx = 0;
330 while (1) {
331 if (isteq(list[hdr_idx].n, ist("")))
332 break;
333
334 if (!istmatch(list[hdr_idx].n, ist(":")))
335 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
336
337 ++hdr_idx;
338 }
339
340 htx_add_endof(htx, HTX_BLK_EOH);
341 htx_to_buf(htx, &htx_buf);
342
343 if (fin)
344 htx->flags |= HTX_FL_EOM;
345
Willy Tarreau3215e732022-05-27 10:09:11 +0200346 if (!qc_attach_sc(qcs, &htx_buf))
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200347 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100348
Willy Tarreau4596fe22022-05-17 19:07:51 +0200349 /* buffer is transferred to the stream connector and set to NULL
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100350 * except on stream creation error.
351 */
352 b_free(&htx_buf);
353 offer_buffers(NULL, 1);
354
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200355 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100356}
357
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100358/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
359 * HTX buffer. <fin> must be set if this is the last data to transfer from this
360 * stream.
361 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200362 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100363 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200364static int h3_data_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100365 char fin)
366{
367 struct buffer *appbuf;
368 struct htx *htx = NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200369 size_t htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100370 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200371 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100372
373 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
374 BUG_ON(!appbuf);
375 htx = htx_from_buf(appbuf);
376
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200377 if (len > ncb_data(buf, 0)) {
378 len = ncb_data(buf, 0);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200379 fin = 0;
380 }
381
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200382 head = ncb_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200383 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100384 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200385 if (!htx_space) {
386 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200387 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200388 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200389
390 if (len > htx_space) {
391 len = htx_space;
392 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100393 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100394
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200395 if (head + len > ncb_wrap(buf)) {
396 size_t contig = ncb_wrap(buf) - head;
397 htx_sent = htx_add_data(htx, ist2(ncb_head(buf), contig));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200398 if (htx_sent < contig) {
399 qcs->flags |= QC_SF_DEM_FULL;
400 goto out;
401 }
402
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200403 len -= contig;
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200404 head = ncb_orig(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200405 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100406 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100407
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200408 htx_sent += htx_add_data(htx, ist2(head, len));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200409 if (htx_sent < len) {
410 qcs->flags |= QC_SF_DEM_FULL;
411 goto out;
412 }
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200413
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200414 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100415 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100416
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200417 out:
418 htx_to_buf(htx, appbuf);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200419 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100420}
421
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200422/* Parse a SETTINGS frame of length <len> of payload <rxbuf>.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200423 *
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200424 * Returns the number of bytes handled or a negative error code.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200425 */
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200426static size_t h3_parse_settings_frm(struct h3c *h3c, const struct ncbuf *rxbuf,
427 size_t len)
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200428{
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200429 struct buffer b;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200430 uint64_t id, value;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200431 size_t ret = 0;
432 long mask = 0; /* used to detect duplicated settings identifier */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200433
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200434 b = h3_b_dup(rxbuf);
435 b_set_data(&b, len);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200436
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200437 while (b_data(&b)) {
438 if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) {
439 h3c->err = H3_FRAME_ERROR;
440 return -1;
441 }
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200442
443 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
444 __func__, (unsigned long long)id, (unsigned long long)value);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200445
446 /* draft-ietf-quic-http34 7.2.4. SETTINGS
447 *
448 * The same setting identifier MUST NOT occur more than once in the
449 * SETTINGS frame. A receiver MAY treat the presence of duplicate
450 * setting identifiers as a connection error of type H3_SETTINGS_ERROR.
451 */
452
453 /* Ignore duplicate check for ID too big used for GREASE. */
454 if (id < sizeof(mask)) {
455 if (ha_bit_test(id, &mask)) {
456 h3c->err = H3_SETTINGS_ERROR;
457 return -1;
458 }
459 ha_bit_set(id, &mask);
460 }
461
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200462 switch (id) {
463 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
464 h3c->qpack_max_table_capacity = value;
465 break;
466 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
467 h3c->max_field_section_size = value;
468 break;
469 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
470 h3c->qpack_blocked_streams = value;
471 break;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200472
473 case H3_SETTINGS_RESERVED_0:
474 case H3_SETTINGS_RESERVED_2:
475 case H3_SETTINGS_RESERVED_3:
476 case H3_SETTINGS_RESERVED_4:
477 case H3_SETTINGS_RESERVED_5:
478 /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters
479 *
480 * Setting identifiers which were defined in [HTTP2] where there is no
481 * corresponding HTTP/3 setting have also been reserved
482 * (Section 11.2.2). These reserved settings MUST NOT be sent, and
483 * their receipt MUST be treated as a connection error of type
484 * H3_SETTINGS_ERROR.
485 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200486 h3c->err = H3_SETTINGS_ERROR;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200487 return -1;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200488 default:
489 /* MUST be ignored */
490 break;
491 }
492 }
493
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200494 return ret;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200495}
496
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100497/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
498 * that we received the last data of the stream.
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200499 *
500 * Returns 0 on success else non-zero.
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100501 */
502static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
503{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200504 struct ncbuf *rxbuf = &qcs->rx.ncbuf;
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200505 struct h3c *h3c = ctx;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200506 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200507 ssize_t ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100508
Amaury Denoyellebb970422022-04-12 16:40:52 +0200509 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200510 if (!ncb_data(rxbuf, 0))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100511 return 0;
512
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200513 if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) {
514 if (h3_init_uni_stream(h3c, qcs, rxbuf))
515 return 1;
516 }
517
518 if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) {
519 /* For non-h3 STREAM, parse it and return immediately. */
520 if (h3_parse_uni_stream_no_h3(qcs, rxbuf))
521 return 1;
522 return 0;
523 }
524
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200525 while (ncb_data(rxbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100526 uint64_t ftype, flen;
527 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100528 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100529
530 /* Work on a copy of <rxbuf> */
531 b = h3_b_dup(rxbuf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200532 if (!h3s->demux_frame_len) {
533 size_t hlen = h3_decode_frm_header(&ftype, &flen, &b);
534 if (!hlen)
535 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100536
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200537 h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n",
538 __func__, ftype, flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100539
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200540 h3s->demux_frame_type = ftype;
541 h3s->demux_frame_len = flen;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200542 qcs_consume(qcs, hlen);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200543 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100544
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200545 flen = h3s->demux_frame_len;
546 ftype = h3s->demux_frame_type;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200547
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200548 if (!h3_is_frame_valid(h3c, qcs, ftype)) {
549 qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED);
550 return 1;
551 }
552
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200553 /* Do not demux incomplete frames except H3 DATA which can be
554 * fragmented in multiple HTX blocks.
555 */
556 if (flen > b_data(&b) && ftype != H3_FT_DATA) {
557 /* Reject frames bigger than bufsize.
558 *
559 * TODO HEADERS should in complement be limited with H3
560 * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent
561 * excessive decompressed size.
562 */
563 if (flen > ncb_size(rxbuf)) {
564 qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD);
565 return 1;
566 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200567 break;
Amaury Denoyelleb5454d42022-05-12 16:56:16 +0200568 }
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200569
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200570 last_stream_frame = (fin && flen == ncb_total_data(rxbuf));
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100571
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200572 h3_inc_frame_type_cnt(h3c->prx_counters, ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100573 switch (ftype) {
574 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100575 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
576 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200577 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100578 break;
579 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100580 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
581 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200582 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100583 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200584 case H3_FT_CANCEL_PUSH:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100585 case H3_FT_PUSH_PROMISE:
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200586 case H3_FT_MAX_PUSH_ID:
587 case H3_FT_GOAWAY:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100588 /* Not supported */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200589 ret = flen;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100590 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200591 case H3_FT_SETTINGS:
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200592 ret = h3_parse_settings_frm(qcs->qcc->ctx, rxbuf, flen);
593 if (ret < 0) {
594 qcc_emit_cc_app(qcs->qcc, h3c->err);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200595 return 1;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200596 }
597 h3c->flags |= H3_CF_SETTINGS_RECV;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200598 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100599 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100600 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200601 *
602 * Implementations MUST discard frames [...] that have unknown
603 * or unsupported types.
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100604 */
605 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200606 ret = flen;
607 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100608 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200609
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200610 if (ret) {
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200611 BUG_ON(h3s->demux_frame_len < ret);
612 h3s->demux_frame_len -= ret;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200613 qcs_consume(qcs, ret);
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200614 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100615 }
616
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +0200617 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
618 * However, currently, io-cb of MUX does not handle Rx.
619 */
620
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100621 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100622}
623
Amaury Denoyellea5871362021-10-07 16:26:12 +0200624/* Returns buffer for data sending.
625 * May be NULL if the allocation failed.
626 */
627static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100628{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200629 if (!b_size(&qcs->tx.buf))
630 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100631
Amaury Denoyellea5871362021-10-07 16:26:12 +0200632 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100633}
634
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200635/* Function used to emit stream data from <qcs> control uni-stream */
636static int h3_control_send(struct qcs *qcs, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100637{
638 int ret;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200639 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100640 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200641 struct buffer pos, *res;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200642 size_t frm_len;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200643
644 BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100645
646 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200647 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100648
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200649 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
650 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
651 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
652 quic_int_getsize(h3_settings_qpack_blocked_streams);
653 if (h3_settings_max_field_section_size) {
654 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
655 quic_int_getsize(h3_settings_max_field_section_size);
656 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100657
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200658 b_quic_enc_int(&pos, H3_UNI_S_T_CTRL);
659 /* Build a SETTINGS frame */
660 b_quic_enc_int(&pos, H3_FT_SETTINGS);
661 b_quic_enc_int(&pos, frm_len);
662 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
663 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
664 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
665 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
666 if (h3_settings_max_field_section_size) {
667 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
668 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
669 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200670
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200671 res = mux_get_buf(qcs);
672 if (b_room(res) < b_data(&pos)) {
673 // TODO the mux should be put in blocked state, with
674 // the stream in state waiting for settings to be sent
675 ABORT_NOW();
676 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100677
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200678 ret = b_force_xfer(res, &pos, b_data(&pos));
679 if (ret > 0) {
680 h3c->flags |= H3_CF_SETTINGS_SENT;
681 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
682 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100683 }
684
685 return ret;
686}
687
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200688static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
689{
690 struct buffer outbuf;
691 struct buffer headers_buf = BUF_NULL;
692 struct buffer *res;
693 struct http_hdr list[global.tune.max_http_hdr];
694 struct htx_sl *sl;
695 struct htx_blk *blk;
696 enum htx_blk_type type;
697 int frame_length_size; /* size in bytes of frame length varint field */
698 int ret = 0;
699 int hdr;
700 int status = 0;
701
702 sl = NULL;
703 hdr = 0;
704 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
705 type = htx_get_blk_type(blk);
706
707 if (type == HTX_BLK_UNUSED)
708 continue;
709
710 if (type == HTX_BLK_EOH)
711 break;
712
713 if (type == HTX_BLK_RES_SL) {
714 /* start-line -> HEADERS h3 frame */
715 BUG_ON(sl);
716 sl = htx_get_blk_ptr(htx, blk);
717 /* TODO should be on h3 layer */
718 status = sl->info.res.status;
719 }
720 else if (type == HTX_BLK_HDR) {
721 list[hdr].n = htx_get_blk_name(htx, blk);
722 list[hdr].v = htx_get_blk_value(htx, blk);
723 hdr++;
724 }
725 else {
726 ABORT_NOW();
727 goto err;
728 }
729 }
730
731 BUG_ON(!sl);
732
733 list[hdr].n = ist("");
734
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200735 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200736
737 /* At least 5 bytes to store frame type + length as a varint max size */
738 if (b_room(res) < 5)
739 ABORT_NOW();
740
741 b_reset(&outbuf);
742 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
743 /* Start the headers after frame type + length */
744 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
745
746 if (qpack_encode_field_section_line(&headers_buf))
747 ABORT_NOW();
748 if (qpack_encode_int_status(&headers_buf, status))
749 ABORT_NOW();
750
751 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
752 if (isteq(list[hdr].n, ist("")))
753 break;
754
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100755 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
756 * Transfer codings (see Section 6.1 of [HTTP11]) are not
757 * defined for HTTP/3; the Transfer-Encoding header field MUST
758 * NOT be used.
759 */
760 if (isteq(list[hdr].n, ist("transfer-encoding")))
761 continue;
762
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200763 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
764 ABORT_NOW();
765 }
766
767 /* Now that all headers are encoded, we are certain that res buffer is
768 * big enough
769 */
770 frame_length_size = quic_int_getsize(b_data(&headers_buf));
771 res->head += 4 - frame_length_size;
772 b_putchr(res, 0x01); /* h3 HEADERS frame type */
773 if (!b_quic_enc_int(res, b_data(&headers_buf)))
774 ABORT_NOW();
775 b_add(res, b_data(&headers_buf));
776
777 ret = 0;
778 blk = htx_get_head_blk(htx);
779 while (blk) {
780 type = htx_get_blk_type(blk);
781 ret += htx_get_blksz(blk);
782 blk = htx_remove_blk(htx, blk);
783 if (type == HTX_BLK_EOH)
784 break;
785 }
786
787 return ret;
788
789 err:
790 return 0;
791}
792
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200793/* Returns the total of bytes sent. */
794static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
795{
796 struct buffer outbuf;
797 struct buffer *res;
798 size_t total = 0;
799 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200800 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200801 struct htx_blk *blk;
802 enum htx_blk_type type;
803
804 htx = htx_from_buf(buf);
805
806 new_frame:
807 if (!count || htx_is_empty(htx))
808 goto end;
809
810 blk = htx_get_head_blk(htx);
811 type = htx_get_blk_type(blk);
812 fsize = bsize = htx_get_blksz(blk);
813
814 if (type != HTX_BLK_DATA)
815 goto end;
816
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200817 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200818
819 if (fsize > count)
820 fsize = count;
821
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200822 /* h3 DATA headers : 1-byte frame type + varint frame length */
823 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200824
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200825 while (1) {
826 b_reset(&outbuf);
827 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
828 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
829 break;
830 b_slow_realign(res, trash.area, b_data(res));
831 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200832
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100833 /* Not enough room for headers and at least one data byte, block the
Willy Tarreau4596fe22022-05-17 19:07:51 +0200834 * stream. It is expected that the stream connector layer will subscribe
835 * on SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200836 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100837 if (b_size(&outbuf) <= hsize) {
838 qcs->flags |= QC_SF_BLK_MROOM;
839 goto end;
840 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200841
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200842 if (b_size(&outbuf) < hsize + fsize)
843 fsize = b_size(&outbuf) - hsize;
844 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200845
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200846 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
847 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
848
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200849 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200850 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200851 count -= fsize;
852
853 if (fsize == bsize)
854 htx_remove_blk(htx, blk);
855 else
856 htx_cut_data_blk(htx, blk, fsize);
857
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200858 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200859 b_add(res, b_data(&outbuf));
860 goto new_frame;
861
862 end:
863 return total;
864}
865
Willy Tarreau3215e732022-05-27 10:09:11 +0200866size_t h3_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200867{
868 size_t total = 0;
Willy Tarreau3215e732022-05-27 10:09:11 +0200869 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200870 struct htx *htx;
871 enum htx_blk_type btype;
872 struct htx_blk *blk;
873 uint32_t bsize;
874 int32_t idx;
875 int ret;
876
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100877 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100878
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200879 htx = htx_from_buf(buf);
880
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100881 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200882 idx = htx_get_head(htx);
883 blk = htx_get_blk(htx, idx);
884 btype = htx_get_blk_type(blk);
885 bsize = htx_get_blksz(blk);
886
887 /* Not implemented : QUIC on backend side */
888 BUG_ON(btype == HTX_BLK_REQ_SL);
889
890 switch (btype) {
891 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200892 /* start-line -> HEADERS h3 frame */
893 ret = h3_resp_headers_send(qcs, htx);
894 if (ret > 0) {
895 total += ret;
896 count -= ret;
897 if (ret < bsize)
898 goto out;
899 }
900 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200901
902 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200903 ret = h3_resp_data_send(qcs, buf, count);
904 if (ret > 0) {
905 htx = htx_from_buf(buf);
906 total += ret;
907 count -= ret;
908 if (ret < bsize)
909 goto out;
910 }
911 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200912
913 case HTX_BLK_TLR:
914 case HTX_BLK_EOT:
915 /* TODO trailers */
916
917 default:
918 htx_remove_blk(htx, blk);
919 total += bsize;
920 count -= bsize;
921 break;
922 }
923 }
924
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100925 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
926 qcs->flags |= QC_SF_FIN_STREAM;
927
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200928 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200929 if (total) {
930 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
931 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
932 }
933
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200934 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200935}
936
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200937static int h3_attach(struct qcs *qcs)
938{
939 struct h3s *h3s;
940
941 h3s = pool_alloc(pool_head_h3s);
942 if (!h3s)
943 return 1;
944
945 qcs->ctx = h3s;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200946 h3s->demux_frame_len = 0;
947 h3s->demux_frame_type = 0;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200948 h3s->flags = 0;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200949
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200950 if (quic_stream_is_bidi(qcs->id)) {
951 h3s->type = H3S_T_REQ;
952 }
953 else {
954 /* stream type must be decoded for unidirectional streams */
955 h3s->type = H3S_T_UNKNOWN;
956 }
957
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200958 return 0;
959}
960
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200961static void h3_detach(struct qcs *qcs)
962{
963 struct h3s *h3s = qcs->ctx;
964 pool_free(pool_head_h3s, h3s);
965 qcs->ctx = NULL;
966}
967
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100968static int h3_finalize(void *ctx)
969{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200970 struct h3c *h3c = ctx;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200971 struct qcs *qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100972
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200973 qcs = qcs_new(h3c->qcc, 0x3, QCS_SRV_UNI);
974 if (!qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100975 return 0;
976
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200977 h3_control_send(qcs, h3c);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100978
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100979 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100980}
981
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100982/* Initialize the HTTP/3 context for <qcc> mux.
983 * Return 1 if succeeded, 0 if not.
984 */
985static int h3_init(struct qcc *qcc)
986{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200987 struct h3c *h3c;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200988 struct quic_conn *qc = qcc->conn->handle.qc;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100989
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200990 h3c = pool_alloc(pool_head_h3c);
991 if (!h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100992 goto fail_no_h3;
993
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200994 h3c->qcc = qcc;
995 h3c->err = H3_NO_ERROR;
996 h3c->flags = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100997
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200998 qcc->ctx = h3c;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200999 h3c->prx_counters =
1000 EXTRA_COUNTERS_GET(qc->li->bind_conf->frontend->extra_counters_fe,
1001 &h3_stats_module);
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001002 LIST_INIT(&h3c->buf_wait.list);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001003
1004 return 1;
1005
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001006 fail_no_h3:
1007 return 0;
1008}
1009
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001010static void h3_release(void *ctx)
1011{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001012 struct h3c *h3c = ctx;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001013 pool_free(pool_head_h3c, h3c);
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001014}
1015
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001016/* Check if the H3 connection can still be considered as active.
1017 *
1018 * Return true if active else false.
1019 */
1020static int h3_is_active(const struct qcc *qcc, void *ctx)
1021{
1022 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
1023 return 1;
1024
1025 return 0;
1026}
1027
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001028/* Increment the h3 error code counters for <error_code> value */
1029static void h3_stats_inc_err_cnt(void *ctx, int err_code)
1030{
1031 struct h3c *h3c = ctx;
1032
1033 h3_inc_err_cnt(h3c->prx_counters, err_code);
1034}
1035
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001036/* HTTP/3 application layer operations */
1037const struct qcc_app_ops h3_ops = {
1038 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001039 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001040 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +01001041 .snd_buf = h3_snd_buf,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001042 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001043 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001044 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001045 .release = h3_release,
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001046 .inc_err_cnt = h3_stats_inc_err_cnt,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001047};