Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1 | /* |
| 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 Denoyelle | 9904355 | 2021-08-24 15:36:02 +0200 | [diff] [blame] | 20 | #include <haproxy/connection.h> |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 21 | #include <haproxy/conn_stream.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 22 | #include <haproxy/dynbuf.h> |
| 23 | #include <haproxy/h3.h> |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 24 | #include <haproxy/http.h> |
| 25 | #include <haproxy/htx.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 26 | #include <haproxy/istbuf.h> |
Amaury Denoyelle | 846cc04 | 2022-04-04 16:13:44 +0200 | [diff] [blame] | 27 | #include <haproxy/mux_quic.h> |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 28 | #include <haproxy/ncbuf.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 29 | #include <haproxy/pool.h> |
| 30 | #include <haproxy/qpack-dec.h> |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 31 | #include <haproxy/qpack-enc.h> |
| 32 | #include <haproxy/quic_enc.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 33 | #include <haproxy/tools.h> |
| 34 | #include <haproxy/xprt_quic.h> |
| 35 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 36 | #if defined(DEBUG_H3) |
| 37 | #define h3_debug_printf fprintf |
| 38 | #define h3_debug_hexdump debug_hexdump |
| 39 | #else |
| 40 | #define h3_debug_printf(...) do { } while (0) |
| 41 | #define h3_debug_hexdump(...) do { } while (0) |
| 42 | #endif |
| 43 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame^] | 44 | #define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */ |
| 45 | #define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */ |
| 46 | #define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */ |
| 47 | #define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */ |
| 48 | #define H3_CF_UNI_QPACK_ENC_SET 0x00000010 /* Remote QPACK encoder stream opened */ |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 49 | |
| 50 | /* Default settings */ |
Amaury Denoyelle | 3394939 | 2021-08-24 15:16:58 +0200 | [diff] [blame] | 51 | static uint64_t h3_settings_qpack_max_table_capacity = 0; |
| 52 | static uint64_t h3_settings_qpack_blocked_streams = 4096; |
| 53 | static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */ |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 54 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 55 | struct h3c { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 56 | struct qcc *qcc; |
| 57 | enum h3_err err; |
| 58 | uint32_t flags; |
| 59 | /* Locally initiated uni-streams */ |
| 60 | struct h3_uqs lqpack_enc; |
| 61 | struct h3_uqs lqpack_dec; |
| 62 | struct h3_uqs lctrl; |
| 63 | /* Remotely initiated uni-streams */ |
| 64 | struct h3_uqs rqpack_enc; |
| 65 | struct h3_uqs rqpack_dec; |
| 66 | struct h3_uqs rctrl; |
| 67 | /* Settings */ |
| 68 | uint64_t qpack_max_table_capacity; |
| 69 | uint64_t qpack_blocked_streams; |
| 70 | uint64_t max_field_section_size; |
| 71 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
| 72 | }; |
| 73 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 74 | DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c)); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 75 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 76 | #define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */ |
| 77 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 78 | struct h3s { |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 79 | enum h3s_t type; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 80 | int demux_frame_len; |
| 81 | int demux_frame_type; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 82 | |
| 83 | int flags; |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s)); |
| 87 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 88 | /* Simple function to duplicate a buffer */ |
Amaury Denoyelle | c7dd9d6 | 2022-05-24 18:14:28 +0200 | [diff] [blame] | 89 | static inline struct buffer h3_b_dup(const struct ncbuf *b) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 90 | { |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 91 | return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0)); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 94 | /* Initialize an uni-stream <qcs> by reading its type from <rxbuf>. |
| 95 | * |
| 96 | * Returns 0 on success else non-zero. |
| 97 | */ |
| 98 | static int h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs, |
| 99 | struct ncbuf *rxbuf) |
| 100 | { |
| 101 | /* decode unidirectional stream type */ |
| 102 | struct h3s *h3s = qcs->ctx; |
| 103 | struct buffer b; |
| 104 | uint64_t type; |
| 105 | size_t len = 0, ret; |
| 106 | |
| 107 | BUG_ON_HOT(!quic_stream_is_uni(qcs->id) || |
| 108 | h3s->flags & H3_SF_UNI_INIT); |
| 109 | |
| 110 | b = h3_b_dup(rxbuf); |
| 111 | ret = b_quic_dec_int(&type, &b, &len); |
| 112 | if (!ret) { |
| 113 | ABORT_NOW(); |
| 114 | } |
| 115 | |
| 116 | switch (type) { |
| 117 | case H3_UNI_S_T_CTRL: |
| 118 | if (h3c->flags & H3_CF_UNI_CTRL_SET) { |
| 119 | qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR); |
| 120 | return 1; |
| 121 | } |
| 122 | h3c->flags |= H3_CF_UNI_CTRL_SET; |
| 123 | h3s->type = H3S_T_CTRL; |
| 124 | break; |
| 125 | |
| 126 | case H3_UNI_S_T_PUSH: |
| 127 | /* TODO not supported for the moment */ |
| 128 | h3s->type = H3S_T_PUSH; |
| 129 | break; |
| 130 | |
| 131 | case H3_UNI_S_T_QPACK_DEC: |
| 132 | if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) { |
| 133 | qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR); |
| 134 | return 1; |
| 135 | } |
| 136 | h3c->flags |= H3_CF_UNI_QPACK_DEC_SET; |
| 137 | h3s->type = H3S_T_QPACK_DEC; |
| 138 | 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; |
| 147 | break; |
| 148 | |
| 149 | default: |
| 150 | break; |
| 151 | }; |
| 152 | |
| 153 | h3s->flags |= H3_SF_UNI_INIT; |
| 154 | qcs_consume(qcs, len); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 159 | /* Decode a h3 frame header made of two QUIC varints from <b> buffer. |
| 160 | * Returns the number of bytes consumed if there was enough data in <b>, 0 if not. |
| 161 | * Note that this function update <b> buffer to reflect the number of bytes consumed |
| 162 | * to decode the h3 frame header. |
| 163 | */ |
| 164 | static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen, |
| 165 | struct buffer *b) |
| 166 | { |
| 167 | size_t hlen; |
| 168 | |
| 169 | hlen = 0; |
| 170 | if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen)) |
| 171 | return 0; |
| 172 | |
| 173 | return hlen; |
| 174 | } |
| 175 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame^] | 176 | /* Check if H3 frame of type <ftype> is valid when received on stream <qcs>. |
| 177 | * |
| 178 | * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should |
| 179 | * be reported. |
| 180 | */ |
| 181 | static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype) |
| 182 | { |
| 183 | struct h3s *h3s = qcs->ctx; |
| 184 | const uint64_t id = qcs->id; |
| 185 | |
| 186 | BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN); |
| 187 | |
| 188 | switch (ftype) { |
| 189 | case H3_FT_DATA: |
| 190 | case H3_FT_HEADERS: |
| 191 | return h3s->type != H3S_T_CTRL; |
| 192 | |
| 193 | case H3_FT_CANCEL_PUSH: |
| 194 | case H3_FT_GOAWAY: |
| 195 | case H3_FT_MAX_PUSH_ID: |
| 196 | /* Only allowed for control stream. First frame of control |
| 197 | * stream MUST be SETTINGS. |
| 198 | */ |
| 199 | return h3s->type == H3S_T_CTRL && |
| 200 | (h3c->flags & H3_CF_SETTINGS_RECV); |
| 201 | |
| 202 | case H3_FT_SETTINGS: |
| 203 | /* draft-ietf-quic-http34 7.2.4. SETTINGS |
| 204 | * |
| 205 | * If an endpoint receives a second SETTINGS frame on the control |
| 206 | * stream, the endpoint MUST respond with a connection error of type |
| 207 | * H3_FRAME_UNEXPECTED. |
| 208 | */ |
| 209 | return h3s->type == H3S_T_CTRL && |
| 210 | !(h3c->flags & H3_CF_SETTINGS_RECV); |
| 211 | |
| 212 | case H3_FT_PUSH_PROMISE: |
| 213 | return h3s->type != H3S_T_CTRL && |
| 214 | (id & QCS_ID_SRV_INTIATOR_BIT); |
| 215 | |
| 216 | default: |
| 217 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
| 218 | * |
| 219 | * Implementations MUST discard frames [...] that have unknown |
| 220 | * or unsupported types. |
| 221 | */ |
| 222 | return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV); |
| 223 | } |
| 224 | } |
| 225 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 226 | /* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied |
| 227 | * in a local HTX buffer and transfer to the conn-stream layer. <fin> must be |
| 228 | * set if this is the last data to transfer from this stream. |
| 229 | * |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 230 | * Returns the number of bytes handled or a negative error code. |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 231 | */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 232 | static int h3_headers_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len, |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 233 | char fin) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 234 | { |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 235 | struct buffer htx_buf = BUF_NULL; |
| 236 | struct buffer *tmp = get_trash_chunk(); |
Amaury Denoyelle | 7059ebc | 2021-12-08 15:51:04 +0100 | [diff] [blame] | 237 | struct htx *htx = NULL; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 238 | struct htx_sl *sl; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 239 | struct http_hdr list[global.tune.max_http_hdr]; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 240 | unsigned int flags = HTX_SL_F_NONE; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 241 | struct ist meth = IST_NULL, path = IST_NULL; |
| 242 | //struct ist scheme = IST_NULL, authority = IST_NULL; |
| 243 | struct ist authority = IST_NULL; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 244 | int hdr_idx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 245 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 246 | /* TODO support buffer wrapping */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 247 | BUG_ON(ncb_head(buf) + len >= ncb_wrap(buf)); |
| 248 | if (qpack_decode_fs((const unsigned char *)ncb_head(buf), len, tmp, list) < 0) |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 249 | return -1; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 250 | |
| 251 | qc_get_buf(qcs, &htx_buf); |
| 252 | BUG_ON(!b_size(&htx_buf)); |
| 253 | htx = htx_from_buf(&htx_buf); |
| 254 | |
| 255 | /* first treat pseudo-header to build the start line */ |
| 256 | hdr_idx = 0; |
| 257 | while (1) { |
| 258 | if (isteq(list[hdr_idx].n, ist(""))) |
| 259 | break; |
| 260 | |
| 261 | if (istmatch(list[hdr_idx].n, ist(":"))) { |
| 262 | /* pseudo-header */ |
| 263 | if (isteq(list[hdr_idx].n, ist(":method"))) |
| 264 | meth = list[hdr_idx].v; |
| 265 | else if (isteq(list[hdr_idx].n, ist(":path"))) |
| 266 | path = list[hdr_idx].v; |
| 267 | //else if (isteq(list[hdr_idx].n, ist(":scheme"))) |
| 268 | // scheme = list[hdr_idx].v; |
| 269 | else if (isteq(list[hdr_idx].n, ist(":authority"))) |
| 270 | authority = list[hdr_idx].v; |
| 271 | } |
| 272 | |
| 273 | ++hdr_idx; |
| 274 | } |
| 275 | |
| 276 | flags |= HTX_SL_F_VER_11; |
Amaury Denoyelle | 0fa14a6 | 2022-04-26 16:24:39 +0200 | [diff] [blame] | 277 | flags |= HTX_SL_F_XFER_LEN; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 278 | |
| 279 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0")); |
| 280 | if (!sl) |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 281 | return -1; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 282 | |
| 283 | if (fin) |
| 284 | sl->flags |= HTX_SL_F_BODYLESS; |
| 285 | |
| 286 | sl->info.req.meth = find_http_meth(meth.ptr, meth.len); |
| 287 | BUG_ON(sl->info.req.meth == HTTP_METH_OTHER); |
| 288 | |
| 289 | if (isttest(authority)) |
| 290 | htx_add_header(htx, ist("host"), authority); |
| 291 | |
| 292 | /* now treat standard headers */ |
| 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 | htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v); |
| 300 | |
| 301 | ++hdr_idx; |
| 302 | } |
| 303 | |
| 304 | htx_add_endof(htx, HTX_BLK_EOH); |
| 305 | htx_to_buf(htx, &htx_buf); |
| 306 | |
| 307 | if (fin) |
| 308 | htx->flags |= HTX_FL_EOM; |
| 309 | |
Willy Tarreau | 01c2a4a | 2022-05-10 15:46:10 +0200 | [diff] [blame] | 310 | if (!qc_attach_cs(qcs, &htx_buf)) |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 311 | return -1; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 312 | |
| 313 | /* buffer is transferred to conn_stream and set to NULL |
| 314 | * except on stream creation error. |
| 315 | */ |
| 316 | b_free(&htx_buf); |
| 317 | offer_buffers(NULL, 1); |
| 318 | |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 319 | return len; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 322 | /* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs> |
| 323 | * HTX buffer. <fin> must be set if this is the last data to transfer from this |
| 324 | * stream. |
| 325 | * |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 326 | * Returns the number of bytes handled or a negative error code. |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 327 | */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 328 | static int h3_data_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len, |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 329 | char fin) |
| 330 | { |
| 331 | struct buffer *appbuf; |
| 332 | struct htx *htx = NULL; |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 333 | size_t htx_sent = 0; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 334 | int htx_space; |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 335 | char *head; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 336 | |
| 337 | appbuf = qc_get_buf(qcs, &qcs->rx.app_buf); |
| 338 | BUG_ON(!appbuf); |
| 339 | htx = htx_from_buf(appbuf); |
| 340 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 341 | if (len > ncb_data(buf, 0)) { |
| 342 | len = ncb_data(buf, 0); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 343 | fin = 0; |
| 344 | } |
| 345 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 346 | head = ncb_head(buf); |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 347 | retry: |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 348 | htx_space = htx_free_data_space(htx); |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 349 | if (!htx_space) { |
| 350 | qcs->flags |= QC_SF_DEM_FULL; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 351 | goto out; |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 352 | } |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 353 | |
| 354 | if (len > htx_space) { |
| 355 | len = htx_space; |
| 356 | fin = 0; |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 357 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 358 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 359 | if (head + len > ncb_wrap(buf)) { |
| 360 | size_t contig = ncb_wrap(buf) - head; |
| 361 | htx_sent = htx_add_data(htx, ist2(ncb_head(buf), contig)); |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 362 | if (htx_sent < contig) { |
| 363 | qcs->flags |= QC_SF_DEM_FULL; |
| 364 | goto out; |
| 365 | } |
| 366 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 367 | len -= contig; |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 368 | head = ncb_orig(buf); |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 369 | goto retry; |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 370 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 371 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 372 | htx_sent += htx_add_data(htx, ist2(head, len)); |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 373 | if (htx_sent < len) { |
| 374 | qcs->flags |= QC_SF_DEM_FULL; |
| 375 | goto out; |
| 376 | } |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 377 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 378 | if (fin && len == htx_sent) |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 379 | htx->flags |= HTX_FL_EOM; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 380 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 381 | out: |
| 382 | htx_to_buf(htx, appbuf); |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 383 | return htx_sent; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 386 | /* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate |
| 387 | * that we received the last data of the stream. |
Amaury Denoyelle | 0ffd6e7 | 2022-05-24 11:07:28 +0200 | [diff] [blame] | 388 | * |
| 389 | * Returns 0 on success else non-zero. |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 390 | */ |
| 391 | static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx) |
| 392 | { |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 393 | struct ncbuf *rxbuf = &qcs->rx.ncbuf; |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame^] | 394 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 395 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 396 | ssize_t ret; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 397 | |
Amaury Denoyelle | bb97042 | 2022-04-12 16:40:52 +0200 | [diff] [blame] | 398 | h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id); |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 399 | if (!ncb_data(rxbuf, 0)) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 400 | return 0; |
| 401 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 402 | while (ncb_data(rxbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 403 | uint64_t ftype, flen; |
| 404 | struct buffer b; |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 405 | char last_stream_frame = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 406 | |
| 407 | /* Work on a copy of <rxbuf> */ |
| 408 | b = h3_b_dup(rxbuf); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 409 | if (!h3s->demux_frame_len) { |
| 410 | size_t hlen = h3_decode_frm_header(&ftype, &flen, &b); |
| 411 | if (!hlen) |
| 412 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 413 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 414 | h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n", |
| 415 | __func__, ftype, flen); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 416 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 417 | h3s->demux_frame_type = ftype; |
| 418 | h3s->demux_frame_len = flen; |
Amaury Denoyelle | a977355 | 2022-05-16 14:38:25 +0200 | [diff] [blame] | 419 | qcs_consume(qcs, hlen); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 420 | } |
Amaury Denoyelle | 0484f92 | 2022-02-15 16:59:39 +0100 | [diff] [blame] | 421 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 422 | flen = h3s->demux_frame_len; |
| 423 | ftype = h3s->demux_frame_type; |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 424 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame^] | 425 | if (!h3_is_frame_valid(h3c, qcs, ftype)) { |
| 426 | qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED); |
| 427 | return 1; |
| 428 | } |
| 429 | |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 430 | /* Do not demux incomplete frames except H3 DATA which can be |
| 431 | * fragmented in multiple HTX blocks. |
| 432 | */ |
| 433 | if (flen > b_data(&b) && ftype != H3_FT_DATA) { |
| 434 | /* Reject frames bigger than bufsize. |
| 435 | * |
| 436 | * TODO HEADERS should in complement be limited with H3 |
| 437 | * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent |
| 438 | * excessive decompressed size. |
| 439 | */ |
| 440 | if (flen > ncb_size(rxbuf)) { |
| 441 | qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD); |
| 442 | return 1; |
| 443 | } |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 444 | break; |
Amaury Denoyelle | b5454d4 | 2022-05-12 16:56:16 +0200 | [diff] [blame] | 445 | } |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 446 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 447 | last_stream_frame = (fin && flen == ncb_total_data(rxbuf)); |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 448 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 449 | switch (ftype) { |
| 450 | case H3_FT_DATA: |
Amaury Denoyelle | 31e4f6e | 2022-02-15 17:30:27 +0100 | [diff] [blame] | 451 | ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame); |
| 452 | /* TODO handle error reporting. Stream closure required. */ |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 453 | if (ret < 0) { ABORT_NOW(); } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 454 | break; |
| 455 | case H3_FT_HEADERS: |
Amaury Denoyelle | 31e4f6e | 2022-02-15 17:30:27 +0100 | [diff] [blame] | 456 | ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame); |
| 457 | /* TODO handle error reporting. Stream closure required. */ |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 458 | if (ret < 0) { ABORT_NOW(); } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 459 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 460 | case H3_FT_PUSH_PROMISE: |
| 461 | /* Not supported */ |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 462 | ret = flen; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 463 | break; |
| 464 | default: |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 465 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame^] | 466 | * |
| 467 | * Implementations MUST discard frames [...] that have unknown |
| 468 | * or unsupported types. |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 469 | */ |
| 470 | h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype); |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 471 | ret = flen; |
| 472 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 473 | } |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 474 | |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 475 | if (ret) { |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 476 | BUG_ON(h3s->demux_frame_len < ret); |
| 477 | h3s->demux_frame_len -= ret; |
Amaury Denoyelle | a977355 | 2022-05-16 14:38:25 +0200 | [diff] [blame] | 478 | qcs_consume(qcs, ret); |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 479 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 480 | } |
| 481 | |
Amaury Denoyelle | 03cc62c | 2022-04-27 16:53:16 +0200 | [diff] [blame] | 482 | /* TODO may be useful to wakeup the MUX if blocked due to full buffer. |
| 483 | * However, currently, io-cb of MUX does not handle Rx. |
| 484 | */ |
| 485 | |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 486 | return 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* Parse a SETTINGS frame which must not be truncated with <flen> as length from |
| 490 | * <rxbuf> buffer. This function does not update this buffer. |
| 491 | * Returns 0 if something wrong happened, 1 if not. |
| 492 | */ |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 493 | static int h3_parse_settings_frm(struct h3c *h3c, const struct ncbuf *rxbuf, size_t flen) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 494 | { |
| 495 | uint64_t id, value; |
| 496 | const unsigned char *buf, *end; |
| 497 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 498 | buf = (const unsigned char *)ncb_head(rxbuf); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 499 | end = buf + flen; |
| 500 | |
Amaury Denoyelle | 160507d | 2022-05-24 16:30:11 +0200 | [diff] [blame] | 501 | while (buf < end) { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 502 | if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end)) |
| 503 | return 0; |
| 504 | |
| 505 | h3_debug_printf(stderr, "%s id: %llu value: %llu\n", |
| 506 | __func__, (unsigned long long)id, (unsigned long long)value); |
| 507 | switch (id) { |
| 508 | case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY: |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 509 | h3c->qpack_max_table_capacity = value; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 510 | break; |
| 511 | case H3_SETTINGS_MAX_FIELD_SECTION_SIZE: |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 512 | h3c->max_field_section_size = value; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 513 | break; |
| 514 | case H3_SETTINGS_QPACK_BLOCKED_STREAMS: |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 515 | h3c->qpack_blocked_streams = value; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 516 | break; |
| 517 | case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5: |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 518 | h3c->err = H3_SETTINGS_ERROR; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 519 | return 0; |
| 520 | default: |
| 521 | /* MUST be ignored */ |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | return 1; |
| 527 | } |
| 528 | |
| 529 | /* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as |
| 530 | * there is not enough received data. |
| 531 | * Returns 0 if something wrong happened, 1 if not. |
| 532 | */ |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 533 | static int h3_control_recv(struct qcs *qcs, void *ctx) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 534 | { |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 535 | struct ncbuf *rxbuf = &qcs->rx.ncbuf; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 536 | struct h3c *h3c = ctx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 537 | |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 538 | h3_debug_printf(stderr, "%s STREAM ID: %lu\n", __func__, qcs->id); |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 539 | if (!ncb_data(rxbuf, 0)) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 540 | return 1; |
| 541 | |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 542 | while (ncb_data(rxbuf, 0)) { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 543 | size_t hlen; |
| 544 | uint64_t ftype, flen; |
| 545 | struct buffer b; |
| 546 | |
| 547 | /* Work on a copy of <rxbuf> */ |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 548 | b = h3_b_dup(rxbuf); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 549 | hlen = h3_decode_frm_header(&ftype, &flen, &b); |
| 550 | if (!hlen) |
| 551 | break; |
| 552 | |
| 553 | h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__, |
| 554 | (unsigned long long)ftype, (unsigned long long)flen); |
| 555 | if (flen > b_data(&b)) |
| 556 | break; |
| 557 | |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 558 | qcs_consume(qcs, hlen); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 559 | /* From here, a frame must not be truncated */ |
| 560 | switch (ftype) { |
| 561 | case H3_FT_CANCEL_PUSH: |
Amaury Denoyelle | e1f3ff0 | 2021-12-06 14:26:52 +0100 | [diff] [blame] | 562 | /* XXX TODO XXX */ |
| 563 | ABORT_NOW(); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 564 | break; |
| 565 | case H3_FT_SETTINGS: |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 566 | if (!h3_parse_settings_frm(h3c, rxbuf, flen)) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 567 | return 0; |
| 568 | break; |
| 569 | case H3_FT_GOAWAY: |
Amaury Denoyelle | e1f3ff0 | 2021-12-06 14:26:52 +0100 | [diff] [blame] | 570 | /* XXX TODO XXX */ |
| 571 | ABORT_NOW(); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 572 | break; |
| 573 | case H3_FT_MAX_PUSH_ID: |
Amaury Denoyelle | e1f3ff0 | 2021-12-06 14:26:52 +0100 | [diff] [blame] | 574 | /* XXX TODO XXX */ |
| 575 | ABORT_NOW(); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 576 | break; |
| 577 | default: |
| 578 | /* Error */ |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 579 | h3c->err = H3_FRAME_UNEXPECTED; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 580 | return 0; |
| 581 | } |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 582 | qcs_consume(qcs, flen); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 583 | } |
| 584 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 585 | return 1; |
| 586 | } |
| 587 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 588 | /* Returns buffer for data sending. |
| 589 | * May be NULL if the allocation failed. |
| 590 | */ |
| 591 | static struct buffer *mux_get_buf(struct qcs *qcs) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 592 | { |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 593 | if (!b_size(&qcs->tx.buf)) |
| 594 | b_alloc(&qcs->tx.buf); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 595 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 596 | return &qcs->tx.buf; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 599 | /* Function used to emit stream data from <qcs> control uni-stream */ |
| 600 | static int h3_control_send(struct qcs *qcs, void *ctx) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 601 | { |
| 602 | int ret; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 603 | struct h3c *h3c = ctx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 604 | unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */ |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 605 | struct buffer pos, *res; |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 606 | size_t frm_len; |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 607 | |
| 608 | BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 609 | |
| 610 | ret = 0; |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 611 | pos = b_make((char *)data, sizeof(data), 0, 0); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 612 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 613 | frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) + |
| 614 | quic_int_getsize(h3_settings_qpack_max_table_capacity) + |
| 615 | quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) + |
| 616 | quic_int_getsize(h3_settings_qpack_blocked_streams); |
| 617 | if (h3_settings_max_field_section_size) { |
| 618 | frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) + |
| 619 | quic_int_getsize(h3_settings_max_field_section_size); |
| 620 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 621 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 622 | b_quic_enc_int(&pos, H3_UNI_S_T_CTRL); |
| 623 | /* Build a SETTINGS frame */ |
| 624 | b_quic_enc_int(&pos, H3_FT_SETTINGS); |
| 625 | b_quic_enc_int(&pos, frm_len); |
| 626 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY); |
| 627 | b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity); |
| 628 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS); |
| 629 | b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams); |
| 630 | if (h3_settings_max_field_section_size) { |
| 631 | b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE); |
| 632 | b_quic_enc_int(&pos, h3_settings_max_field_section_size); |
| 633 | } |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 634 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 635 | res = mux_get_buf(qcs); |
| 636 | if (b_room(res) < b_data(&pos)) { |
| 637 | // TODO the mux should be put in blocked state, with |
| 638 | // the stream in state waiting for settings to be sent |
| 639 | ABORT_NOW(); |
| 640 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 641 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 642 | ret = b_force_xfer(res, &pos, b_data(&pos)); |
| 643 | if (ret > 0) { |
| 644 | h3c->flags |= H3_CF_SETTINGS_SENT; |
| 645 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 646 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | return ret; |
| 650 | } |
| 651 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 652 | static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx) |
| 653 | { |
| 654 | struct buffer outbuf; |
| 655 | struct buffer headers_buf = BUF_NULL; |
| 656 | struct buffer *res; |
| 657 | struct http_hdr list[global.tune.max_http_hdr]; |
| 658 | struct htx_sl *sl; |
| 659 | struct htx_blk *blk; |
| 660 | enum htx_blk_type type; |
| 661 | int frame_length_size; /* size in bytes of frame length varint field */ |
| 662 | int ret = 0; |
| 663 | int hdr; |
| 664 | int status = 0; |
| 665 | |
| 666 | sl = NULL; |
| 667 | hdr = 0; |
| 668 | for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 669 | type = htx_get_blk_type(blk); |
| 670 | |
| 671 | if (type == HTX_BLK_UNUSED) |
| 672 | continue; |
| 673 | |
| 674 | if (type == HTX_BLK_EOH) |
| 675 | break; |
| 676 | |
| 677 | if (type == HTX_BLK_RES_SL) { |
| 678 | /* start-line -> HEADERS h3 frame */ |
| 679 | BUG_ON(sl); |
| 680 | sl = htx_get_blk_ptr(htx, blk); |
| 681 | /* TODO should be on h3 layer */ |
| 682 | status = sl->info.res.status; |
| 683 | } |
| 684 | else if (type == HTX_BLK_HDR) { |
| 685 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 686 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 687 | hdr++; |
| 688 | } |
| 689 | else { |
| 690 | ABORT_NOW(); |
| 691 | goto err; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | BUG_ON(!sl); |
| 696 | |
| 697 | list[hdr].n = ist(""); |
| 698 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 699 | res = mux_get_buf(qcs); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 700 | |
| 701 | /* At least 5 bytes to store frame type + length as a varint max size */ |
| 702 | if (b_room(res) < 5) |
| 703 | ABORT_NOW(); |
| 704 | |
| 705 | b_reset(&outbuf); |
| 706 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 707 | /* Start the headers after frame type + length */ |
| 708 | headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0); |
| 709 | |
| 710 | if (qpack_encode_field_section_line(&headers_buf)) |
| 711 | ABORT_NOW(); |
| 712 | if (qpack_encode_int_status(&headers_buf, status)) |
| 713 | ABORT_NOW(); |
| 714 | |
| 715 | for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) { |
| 716 | if (isteq(list[hdr].n, ist(""))) |
| 717 | break; |
| 718 | |
Amaury Denoyelle | ffafb3d | 2022-02-15 16:10:42 +0100 | [diff] [blame] | 719 | /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges |
| 720 | * Transfer codings (see Section 6.1 of [HTTP11]) are not |
| 721 | * defined for HTTP/3; the Transfer-Encoding header field MUST |
| 722 | * NOT be used. |
| 723 | */ |
| 724 | if (isteq(list[hdr].n, ist("transfer-encoding"))) |
| 725 | continue; |
| 726 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 727 | if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v)) |
| 728 | ABORT_NOW(); |
| 729 | } |
| 730 | |
| 731 | /* Now that all headers are encoded, we are certain that res buffer is |
| 732 | * big enough |
| 733 | */ |
| 734 | frame_length_size = quic_int_getsize(b_data(&headers_buf)); |
| 735 | res->head += 4 - frame_length_size; |
| 736 | b_putchr(res, 0x01); /* h3 HEADERS frame type */ |
| 737 | if (!b_quic_enc_int(res, b_data(&headers_buf))) |
| 738 | ABORT_NOW(); |
| 739 | b_add(res, b_data(&headers_buf)); |
| 740 | |
| 741 | ret = 0; |
| 742 | blk = htx_get_head_blk(htx); |
| 743 | while (blk) { |
| 744 | type = htx_get_blk_type(blk); |
| 745 | ret += htx_get_blksz(blk); |
| 746 | blk = htx_remove_blk(htx, blk); |
| 747 | if (type == HTX_BLK_EOH) |
| 748 | break; |
| 749 | } |
| 750 | |
| 751 | return ret; |
| 752 | |
| 753 | err: |
| 754 | return 0; |
| 755 | } |
| 756 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 757 | /* Returns the total of bytes sent. */ |
| 758 | static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count) |
| 759 | { |
| 760 | struct buffer outbuf; |
| 761 | struct buffer *res; |
| 762 | size_t total = 0; |
| 763 | struct htx *htx; |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 764 | int bsize, fsize, hsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 765 | struct htx_blk *blk; |
| 766 | enum htx_blk_type type; |
| 767 | |
| 768 | htx = htx_from_buf(buf); |
| 769 | |
| 770 | new_frame: |
| 771 | if (!count || htx_is_empty(htx)) |
| 772 | goto end; |
| 773 | |
| 774 | blk = htx_get_head_blk(htx); |
| 775 | type = htx_get_blk_type(blk); |
| 776 | fsize = bsize = htx_get_blksz(blk); |
| 777 | |
| 778 | if (type != HTX_BLK_DATA) |
| 779 | goto end; |
| 780 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 781 | res = mux_get_buf(qcs); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 782 | |
| 783 | if (fsize > count) |
| 784 | fsize = count; |
| 785 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 786 | /* h3 DATA headers : 1-byte frame type + varint frame length */ |
| 787 | hsize = 1 + QUIC_VARINT_MAX_SIZE; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 788 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 789 | while (1) { |
| 790 | b_reset(&outbuf); |
| 791 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 792 | if (b_size(&outbuf) > hsize || !b_space_wraps(res)) |
| 793 | break; |
| 794 | b_slow_realign(res, trash.area, b_data(res)); |
| 795 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 796 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 797 | /* Not enough room for headers and at least one data byte, block the |
| 798 | * stream. It is expected that the conn-stream layer will subscribe on |
| 799 | * SEND. |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 800 | */ |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 801 | if (b_size(&outbuf) <= hsize) { |
| 802 | qcs->flags |= QC_SF_BLK_MROOM; |
| 803 | goto end; |
| 804 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 805 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 806 | if (b_size(&outbuf) < hsize + fsize) |
| 807 | fsize = b_size(&outbuf) - hsize; |
| 808 | BUG_ON(fsize <= 0); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 809 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 810 | b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */ |
| 811 | b_quic_enc_int(&outbuf, fsize); /* h3 frame length */ |
| 812 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 813 | b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize); |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 814 | total += fsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 815 | count -= fsize; |
| 816 | |
| 817 | if (fsize == bsize) |
| 818 | htx_remove_blk(htx, blk); |
| 819 | else |
| 820 | htx_cut_data_blk(htx, blk, fsize); |
| 821 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 822 | /* commit the buffer */ |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 823 | b_add(res, b_data(&outbuf)); |
| 824 | goto new_frame; |
| 825 | |
| 826 | end: |
| 827 | return total; |
| 828 | } |
| 829 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 830 | size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 831 | { |
| 832 | size_t total = 0; |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 833 | struct qcs *qcs = __cs_mux(cs); |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 834 | struct htx *htx; |
| 835 | enum htx_blk_type btype; |
| 836 | struct htx_blk *blk; |
| 837 | uint32_t bsize; |
| 838 | int32_t idx; |
| 839 | int ret; |
| 840 | |
Amaury Denoyelle | d8769d1 | 2022-03-25 15:28:33 +0100 | [diff] [blame] | 841 | h3_debug_printf(stderr, "%s\n", __func__); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 842 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 843 | htx = htx_from_buf(buf); |
| 844 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 845 | while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) { |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 846 | idx = htx_get_head(htx); |
| 847 | blk = htx_get_blk(htx, idx); |
| 848 | btype = htx_get_blk_type(blk); |
| 849 | bsize = htx_get_blksz(blk); |
| 850 | |
| 851 | /* Not implemented : QUIC on backend side */ |
| 852 | BUG_ON(btype == HTX_BLK_REQ_SL); |
| 853 | |
| 854 | switch (btype) { |
| 855 | case HTX_BLK_RES_SL: |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 856 | /* start-line -> HEADERS h3 frame */ |
| 857 | ret = h3_resp_headers_send(qcs, htx); |
| 858 | if (ret > 0) { |
| 859 | total += ret; |
| 860 | count -= ret; |
| 861 | if (ret < bsize) |
| 862 | goto out; |
| 863 | } |
| 864 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 865 | |
| 866 | case HTX_BLK_DATA: |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 867 | ret = h3_resp_data_send(qcs, buf, count); |
| 868 | if (ret > 0) { |
| 869 | htx = htx_from_buf(buf); |
| 870 | total += ret; |
| 871 | count -= ret; |
| 872 | if (ret < bsize) |
| 873 | goto out; |
| 874 | } |
| 875 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 876 | |
| 877 | case HTX_BLK_TLR: |
| 878 | case HTX_BLK_EOT: |
| 879 | /* TODO trailers */ |
| 880 | |
| 881 | default: |
| 882 | htx_remove_blk(htx, blk); |
| 883 | total += bsize; |
| 884 | count -= bsize; |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | |
Amaury Denoyelle | c2025c1 | 2021-12-03 15:03:36 +0100 | [diff] [blame] | 889 | if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) |
| 890 | qcs->flags |= QC_SF_FIN_STREAM; |
| 891 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 892 | out: |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 893 | if (total) { |
| 894 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 895 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 896 | } |
| 897 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 898 | return total; |
Amaury Denoyelle | f52151d | 2021-08-24 16:11:18 +0200 | [diff] [blame] | 899 | } |
| 900 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 901 | static int h3_attach(struct qcs *qcs) |
| 902 | { |
| 903 | struct h3s *h3s; |
| 904 | |
| 905 | h3s = pool_alloc(pool_head_h3s); |
| 906 | if (!h3s) |
| 907 | return 1; |
| 908 | |
| 909 | qcs->ctx = h3s; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 910 | h3s->demux_frame_len = 0; |
| 911 | h3s->demux_frame_type = 0; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 912 | h3s->flags = 0; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 913 | |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 914 | if (quic_stream_is_bidi(qcs->id)) { |
| 915 | h3s->type = H3S_T_REQ; |
| 916 | } |
| 917 | else { |
| 918 | /* stream type must be decoded for unidirectional streams */ |
| 919 | h3s->type = H3S_T_UNKNOWN; |
| 920 | } |
| 921 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 922 | return 0; |
| 923 | } |
| 924 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 925 | /* Finalize the initialization of remotely initiated uni-stream <qcs>. |
| 926 | * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error |
| 927 | * to inform the QUIC mux layer of the encountered error. |
| 928 | */ |
| 929 | static int h3_attach_ruqs(struct qcs *qcs, void *ctx) |
| 930 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 931 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 932 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 933 | struct ncbuf *rxbuf = &qcs->rx.ncbuf; |
Amaury Denoyelle | 3db98e9 | 2022-05-13 15:41:04 +0200 | [diff] [blame] | 934 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 935 | if (h3_init_uni_stream(h3c, qcs, rxbuf)) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 936 | return 0; |
| 937 | |
| 938 | /* Note that for all the uni-streams below, this is an error to receive two times the |
| 939 | * same type of uni-stream (even for Push stream which is not supported at this time. |
| 940 | */ |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 941 | switch (h3s->type) { |
| 942 | case H3S_T_CTRL: |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 943 | h3c->rctrl.qcs = qcs; |
| 944 | h3c->rctrl.cb = h3_control_recv; |
| 945 | qcs_subscribe(qcs, SUB_RETRY_RECV, &h3c->rctrl.wait_event); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 946 | break; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 947 | case H3S_T_PUSH: |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 948 | /* NOT SUPPORTED */ |
| 949 | break; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 950 | case H3S_T_QPACK_ENC: |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 951 | h3c->rqpack_enc.qcs = qcs; |
| 952 | h3c->rqpack_enc.cb = qpack_decode_enc; |
| 953 | qcs_subscribe(qcs, SUB_RETRY_RECV, &h3c->rqpack_enc.wait_event); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 954 | break; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 955 | case H3S_T_QPACK_DEC: |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 956 | h3c->rqpack_dec.qcs = qcs; |
| 957 | h3c->rqpack_dec.cb = qpack_decode_dec; |
| 958 | qcs_subscribe(qcs, SUB_RETRY_RECV, &h3c->rqpack_dec.wait_event); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 959 | break; |
| 960 | default: |
| 961 | /* Error */ |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 962 | h3c->err = H3_STREAM_CREATION_ERROR; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | return 1; |
| 967 | } |
| 968 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 969 | static void h3_detach(struct qcs *qcs) |
| 970 | { |
| 971 | struct h3s *h3s = qcs->ctx; |
| 972 | pool_free(pool_head_h3s, h3s); |
| 973 | qcs->ctx = NULL; |
| 974 | } |
| 975 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 976 | static int h3_finalize(void *ctx) |
| 977 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 978 | struct h3c *h3c = ctx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 979 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 980 | h3c->lctrl.qcs = qcs_new(h3c->qcc, 0x3, QCS_SRV_UNI); |
| 981 | if (!h3c->lctrl.qcs) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 982 | return 0; |
| 983 | |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 984 | h3_control_send(h3c->lctrl.qcs, h3c); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 985 | |
| 986 | return 1; |
| 987 | } |
| 988 | |
| 989 | /* Tasklet dedicated to h3 incoming uni-streams */ |
| 990 | static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state) |
| 991 | { |
| 992 | struct h3_uqs *h3_uqs = ctx; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 993 | struct h3c *h3c = h3_uqs->qcs->qcc->ctx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 994 | |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 995 | h3_uqs->cb(h3_uqs->qcs, h3c); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 996 | return NULL; |
| 997 | } |
| 998 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 999 | /* Release all the tasklet attached to <h3_uqs> uni-stream */ |
| 1000 | static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs) |
| 1001 | { |
| 1002 | struct tasklet *t = h3_uqs->wait_event.tasklet; |
| 1003 | |
| 1004 | if (t) |
| 1005 | tasklet_free(t); |
| 1006 | } |
| 1007 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1008 | /* Release all the tasklet attached to <h3c> uni-streams */ |
| 1009 | static void h3_uqs_tasklets_release(struct h3c *h3c) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1010 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1011 | h3_uqs_tasklet_release(&h3c->rqpack_enc); |
| 1012 | h3_uqs_tasklet_release(&h3c->rqpack_dec); |
| 1013 | h3_uqs_tasklet_release(&h3c->rctrl); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /* Tasklet dedicated to h3 outgoing uni-streams */ |
| 1017 | __maybe_unused |
| 1018 | static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state) |
| 1019 | { |
| 1020 | struct h3_uqs *h3_uqs = ctx; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1021 | struct h3c *h3c = h3_uqs->qcs->qcc->ctx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1022 | |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 1023 | h3_uqs->cb(h3_uqs->qcs, h3c); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1024 | return NULL; |
| 1025 | } |
| 1026 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 1027 | /* Initialize <h3_uqs> uni-stream with <t> as tasklet */ |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1028 | static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3c *h3c, |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 1029 | int (*cb)(struct qcs *qcs, void *ctx), |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1030 | struct task *(*t)(struct task *, void *, unsigned int)) |
| 1031 | { |
| 1032 | h3_uqs->qcs = NULL; |
| 1033 | h3_uqs->cb = cb; |
| 1034 | h3_uqs->wait_event.tasklet = tasklet_new(); |
| 1035 | if (!h3_uqs->wait_event.tasklet) |
| 1036 | return 0; |
| 1037 | |
| 1038 | h3_uqs->wait_event.tasklet->process = t; |
| 1039 | h3_uqs->wait_event.tasklet->context = h3_uqs; |
Frédéric Lécaille | 000162e | 2022-04-01 09:04:57 +0200 | [diff] [blame] | 1040 | h3_uqs->wait_event.events = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1041 | return 1; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static inline void h3_uqs_release(struct h3_uqs *h3_uqs) |
| 1045 | { |
| 1046 | if (h3_uqs->qcs) |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 1047 | qcs_free(h3_uqs->qcs); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1048 | } |
| 1049 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1050 | static inline void h3_uqs_release_all(struct h3c *h3c) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1051 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1052 | h3_uqs_tasklet_release(&h3c->lctrl); |
| 1053 | h3_uqs_release(&h3c->lctrl); |
| 1054 | h3_uqs_tasklet_release(&h3c->lqpack_enc); |
| 1055 | h3_uqs_release(&h3c->lqpack_enc); |
| 1056 | h3_uqs_tasklet_release(&h3c->lqpack_dec); |
| 1057 | h3_uqs_release(&h3c->lqpack_dec); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | /* Initialize the HTTP/3 context for <qcc> mux. |
| 1061 | * Return 1 if succeeded, 0 if not. |
| 1062 | */ |
| 1063 | static int h3_init(struct qcc *qcc) |
| 1064 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1065 | struct h3c *h3c; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1066 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1067 | h3c = pool_alloc(pool_head_h3c); |
| 1068 | if (!h3c) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1069 | goto fail_no_h3; |
| 1070 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1071 | h3c->qcc = qcc; |
| 1072 | h3c->err = H3_NO_ERROR; |
| 1073 | h3c->flags = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1074 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1075 | if (!h3_uqs_init(&h3c->rqpack_enc, h3c, NULL, h3_uqs_task) || |
| 1076 | !h3_uqs_init(&h3c->rqpack_dec, h3c, NULL, h3_uqs_task) || |
| 1077 | !h3_uqs_init(&h3c->rctrl, h3c, h3_control_recv, h3_uqs_task)) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1078 | goto fail_no_h3_ruqs; |
| 1079 | |
Amaury Denoyelle | c6195d7 | 2022-05-23 11:39:14 +0200 | [diff] [blame] | 1080 | if (!h3_uqs_init(&h3c->lctrl, h3c, NULL, h3_uqs_task) || |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1081 | !h3_uqs_init(&h3c->lqpack_enc, h3c, NULL, h3_uqs_task) || |
| 1082 | !h3_uqs_init(&h3c->lqpack_dec, h3c, NULL, h3_uqs_task)) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1083 | goto fail_no_h3_luqs; |
| 1084 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1085 | qcc->ctx = h3c; |
| 1086 | LIST_INIT(&h3c->buf_wait.list); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1087 | |
| 1088 | return 1; |
| 1089 | |
| 1090 | fail_no_h3_ruqs: |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1091 | h3_uqs_release_all(h3c); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1092 | fail_no_h3_luqs: |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1093 | h3_uqs_tasklets_release(h3c); |
| 1094 | pool_free(pool_head_h3c, h3c); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1095 | fail_no_h3: |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1099 | static void h3_release(void *ctx) |
| 1100 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1101 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1102 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1103 | h3_uqs_release_all(h3c); |
| 1104 | h3_uqs_tasklets_release(h3c); |
| 1105 | pool_free(pool_head_h3c, h3c); |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1106 | } |
| 1107 | |
Amaury Denoyelle | 198d35f | 2022-04-01 17:56:58 +0200 | [diff] [blame] | 1108 | /* Check if the H3 connection can still be considered as active. |
| 1109 | * |
| 1110 | * Return true if active else false. |
| 1111 | */ |
| 1112 | static int h3_is_active(const struct qcc *qcc, void *ctx) |
| 1113 | { |
| 1114 | if (qcc->strms[QCS_CLT_BIDI].nb_streams) |
| 1115 | return 1; |
| 1116 | |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1120 | /* HTTP/3 application layer operations */ |
| 1121 | const struct qcc_app_ops h3_ops = { |
| 1122 | .init = h3_init, |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1123 | .attach = h3_attach, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1124 | .attach_ruqs = h3_attach_ruqs, |
| 1125 | .decode_qcs = h3_decode_qcs, |
Amaury Denoyelle | abbe91e | 2021-11-12 16:09:29 +0100 | [diff] [blame] | 1126 | .snd_buf = h3_snd_buf, |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1127 | .detach = h3_detach, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1128 | .finalize = h3_finalize, |
Amaury Denoyelle | 198d35f | 2022-04-01 17:56:58 +0200 | [diff] [blame] | 1129 | .is_active = h3_is_active, |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1130 | .release = h3_release, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1131 | }; |