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 | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 27 | #include <haproxy/mux_quic-t.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 28 | #include <haproxy/pool.h> |
| 29 | #include <haproxy/qpack-dec.h> |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 30 | #include <haproxy/qpack-enc.h> |
| 31 | #include <haproxy/quic_enc.h> |
Amaury Denoyelle | 9904355 | 2021-08-24 15:36:02 +0200 | [diff] [blame] | 32 | #include <haproxy/stream.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 | |
| 44 | #define H3_CF_SETTINGS_SENT 0x00000001 |
| 45 | |
| 46 | /* Default settings */ |
Amaury Denoyelle | 3394939 | 2021-08-24 15:16:58 +0200 | [diff] [blame] | 47 | static uint64_t h3_settings_qpack_max_table_capacity = 0; |
| 48 | static uint64_t h3_settings_qpack_blocked_streams = 4096; |
| 49 | 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] | 50 | |
| 51 | struct h3 { |
| 52 | struct qcc *qcc; |
| 53 | enum h3_err err; |
| 54 | uint32_t flags; |
| 55 | /* Locally initiated uni-streams */ |
| 56 | struct h3_uqs lqpack_enc; |
| 57 | struct h3_uqs lqpack_dec; |
| 58 | struct h3_uqs lctrl; |
| 59 | /* Remotely initiated uni-streams */ |
| 60 | struct h3_uqs rqpack_enc; |
| 61 | struct h3_uqs rqpack_dec; |
| 62 | struct h3_uqs rctrl; |
| 63 | /* Settings */ |
| 64 | uint64_t qpack_max_table_capacity; |
| 65 | uint64_t qpack_blocked_streams; |
| 66 | uint64_t max_field_section_size; |
| 67 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
| 68 | }; |
| 69 | |
| 70 | DECLARE_STATIC_POOL(pool_head_h3, "h3", sizeof(struct h3)); |
| 71 | |
| 72 | /* Simple function to duplicate a buffer */ |
| 73 | static inline struct buffer h3_b_dup(struct buffer *b) |
| 74 | { |
| 75 | return b_make(b->area, b->size, b->head, b->data); |
| 76 | } |
| 77 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 78 | /* Decode a h3 frame header made of two QUIC varints from <b> buffer. |
| 79 | * Returns the number of bytes consumed if there was enough data in <b>, 0 if not. |
| 80 | * Note that this function update <b> buffer to reflect the number of bytes consumed |
| 81 | * to decode the h3 frame header. |
| 82 | */ |
| 83 | static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen, |
| 84 | struct buffer *b) |
| 85 | { |
| 86 | size_t hlen; |
| 87 | |
| 88 | hlen = 0; |
| 89 | if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen)) |
| 90 | return 0; |
| 91 | |
| 92 | return hlen; |
| 93 | } |
| 94 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 95 | /* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied |
| 96 | * in a local HTX buffer and transfer to the conn-stream layer. <fin> must be |
| 97 | * set if this is the last data to transfer from this stream. |
| 98 | * |
| 99 | * Returns 0 on success else non-zero. |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 100 | */ |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 101 | static int h3_headers_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len, |
| 102 | char fin) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 103 | { |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 104 | struct buffer htx_buf = BUF_NULL; |
| 105 | struct buffer *tmp = get_trash_chunk(); |
Amaury Denoyelle | 7059ebc | 2021-12-08 15:51:04 +0100 | [diff] [blame] | 106 | struct htx *htx = NULL; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 107 | struct htx_sl *sl; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 108 | struct http_hdr list[global.tune.max_http_hdr]; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 109 | struct conn_stream *cs; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 110 | unsigned int flags = HTX_SL_F_NONE; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 111 | struct ist meth = IST_NULL, path = IST_NULL; |
| 112 | //struct ist scheme = IST_NULL, authority = IST_NULL; |
| 113 | struct ist authority = IST_NULL; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 114 | int hdr_idx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 115 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 116 | if (qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp, list) < 0) |
| 117 | return 1; |
| 118 | |
| 119 | qc_get_buf(qcs, &htx_buf); |
| 120 | BUG_ON(!b_size(&htx_buf)); |
| 121 | htx = htx_from_buf(&htx_buf); |
| 122 | |
| 123 | /* first treat pseudo-header to build the start line */ |
| 124 | hdr_idx = 0; |
| 125 | while (1) { |
| 126 | if (isteq(list[hdr_idx].n, ist(""))) |
| 127 | break; |
| 128 | |
| 129 | if (istmatch(list[hdr_idx].n, ist(":"))) { |
| 130 | /* pseudo-header */ |
| 131 | if (isteq(list[hdr_idx].n, ist(":method"))) |
| 132 | meth = list[hdr_idx].v; |
| 133 | else if (isteq(list[hdr_idx].n, ist(":path"))) |
| 134 | path = list[hdr_idx].v; |
| 135 | //else if (isteq(list[hdr_idx].n, ist(":scheme"))) |
| 136 | // scheme = list[hdr_idx].v; |
| 137 | else if (isteq(list[hdr_idx].n, ist(":authority"))) |
| 138 | authority = list[hdr_idx].v; |
| 139 | } |
| 140 | |
| 141 | ++hdr_idx; |
| 142 | } |
| 143 | |
| 144 | flags |= HTX_SL_F_VER_11; |
| 145 | |
| 146 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0")); |
| 147 | if (!sl) |
| 148 | return 1; |
| 149 | |
| 150 | if (fin) |
| 151 | sl->flags |= HTX_SL_F_BODYLESS; |
| 152 | |
| 153 | sl->info.req.meth = find_http_meth(meth.ptr, meth.len); |
| 154 | BUG_ON(sl->info.req.meth == HTTP_METH_OTHER); |
| 155 | |
| 156 | if (isttest(authority)) |
| 157 | htx_add_header(htx, ist("host"), authority); |
| 158 | |
| 159 | /* now treat standard headers */ |
| 160 | hdr_idx = 0; |
| 161 | while (1) { |
| 162 | if (isteq(list[hdr_idx].n, ist(""))) |
| 163 | break; |
| 164 | |
| 165 | if (!istmatch(list[hdr_idx].n, ist(":"))) |
| 166 | htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v); |
| 167 | |
| 168 | ++hdr_idx; |
| 169 | } |
| 170 | |
| 171 | htx_add_endof(htx, HTX_BLK_EOH); |
| 172 | htx_to_buf(htx, &htx_buf); |
| 173 | |
| 174 | if (fin) |
| 175 | htx->flags |= HTX_FL_EOM; |
| 176 | |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 177 | cs = cs_new(); |
Frédéric Lécaille | 59509b5 | 2022-02-15 09:25:06 +0100 | [diff] [blame] | 178 | if (!cs) |
| 179 | return 1; |
Christopher Faulet | 9264a2c | 2022-02-24 11:13:57 +0100 | [diff] [blame] | 180 | cs_attach_endp(cs, &qcs->qcc->conn->obj_type, qcs); |
Frédéric Lécaille | 59509b5 | 2022-02-15 09:25:06 +0100 | [diff] [blame] | 181 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 182 | cs->flags |= CS_FL_NOT_FIRST; |
| 183 | cs->ctx = qcs; |
Christopher Faulet | 9264a2c | 2022-02-24 11:13:57 +0100 | [diff] [blame] | 184 | stream_new(qcs->qcc->conn->owner, cs, &htx_buf); |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 185 | |
| 186 | /* buffer is transferred to conn_stream and set to NULL |
| 187 | * except on stream creation error. |
| 188 | */ |
| 189 | b_free(&htx_buf); |
| 190 | offer_buffers(NULL, 1); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 195 | /* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs> |
| 196 | * HTX buffer. <fin> must be set if this is the last data to transfer from this |
| 197 | * stream. |
| 198 | * |
| 199 | * Returns 0 on success else non-zero |
| 200 | */ |
| 201 | static int h3_data_to_htx(struct qcs *qcs, struct buffer *buf, uint64_t len, |
| 202 | char fin) |
| 203 | { |
| 204 | struct buffer *appbuf; |
| 205 | struct htx *htx = NULL; |
| 206 | size_t htx_sent; |
| 207 | int htx_space; |
| 208 | |
| 209 | appbuf = qc_get_buf(qcs, &qcs->rx.app_buf); |
| 210 | BUG_ON(!appbuf); |
| 211 | htx = htx_from_buf(appbuf); |
| 212 | |
| 213 | htx_space = htx_free_data_space(htx); |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 214 | if (!htx_space || htx_space < len) { |
| 215 | ABORT_NOW(); /* TODO handle this case properly */ |
| 216 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 217 | |
| 218 | htx_sent = htx_add_data(htx, ist2(b_head(buf), len)); |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 219 | if (htx_sent < len) { |
| 220 | ABORT_NOW(); /* TODO handle this case properly */ |
| 221 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 222 | |
| 223 | if (fin) |
| 224 | htx->flags |= HTX_FL_EOM; |
| 225 | htx_to_buf(htx, appbuf); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 230 | /* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate |
| 231 | * that we received the last data of the stream. |
| 232 | * Returns <0 on error else 0. |
| 233 | */ |
| 234 | static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx) |
| 235 | { |
| 236 | struct buffer *rxbuf = &qcs->rx.buf; |
Amaury Denoyelle | 31e4f6e | 2022-02-15 17:30:27 +0100 | [diff] [blame] | 237 | int ret; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 238 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 239 | h3_debug_printf(stderr, "%s: STREAM ID: %llu\n", __func__, qcs->by_id.key); |
| 240 | if (!b_data(rxbuf)) |
| 241 | return 0; |
| 242 | |
| 243 | while (b_data(rxbuf)) { |
| 244 | size_t hlen; |
| 245 | uint64_t ftype, flen; |
| 246 | struct buffer b; |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 247 | char last_stream_frame = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 248 | |
| 249 | /* Work on a copy of <rxbuf> */ |
| 250 | b = h3_b_dup(rxbuf); |
| 251 | hlen = h3_decode_frm_header(&ftype, &flen, &b); |
| 252 | if (!hlen) |
| 253 | break; |
| 254 | |
| 255 | h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__, |
| 256 | (unsigned long long)ftype, (unsigned long long)flen); |
Amaury Denoyelle | 0484f92 | 2022-02-15 16:59:39 +0100 | [diff] [blame] | 257 | if (flen > b_data(&b) && !b_full(rxbuf)) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 258 | break; |
| 259 | |
Amaury Denoyelle | 0484f92 | 2022-02-15 16:59:39 +0100 | [diff] [blame] | 260 | /* TODO handle full rxbuf */ |
| 261 | BUG_ON(flen > b_size(rxbuf)); |
| 262 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 263 | b_del(rxbuf, hlen); |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 264 | last_stream_frame = (fin && flen == b_data(rxbuf)); |
| 265 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 266 | switch (ftype) { |
| 267 | case H3_FT_DATA: |
Amaury Denoyelle | 31e4f6e | 2022-02-15 17:30:27 +0100 | [diff] [blame] | 268 | ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame); |
| 269 | /* TODO handle error reporting. Stream closure required. */ |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 270 | if (ret) { ABORT_NOW(); } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 271 | break; |
| 272 | case H3_FT_HEADERS: |
Amaury Denoyelle | 31e4f6e | 2022-02-15 17:30:27 +0100 | [diff] [blame] | 273 | ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame); |
| 274 | /* TODO handle error reporting. Stream closure required. */ |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 275 | if (ret) { ABORT_NOW(); } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 276 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 277 | case H3_FT_PUSH_PROMISE: |
| 278 | /* Not supported */ |
| 279 | break; |
| 280 | default: |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 281 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
| 282 | * unknown frame types MUST be ignored |
| 283 | */ |
| 284 | h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 285 | } |
| 286 | b_del(rxbuf, flen); |
| 287 | } |
| 288 | |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 289 | return 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* Parse a SETTINGS frame which must not be truncated with <flen> as length from |
| 293 | * <rxbuf> buffer. This function does not update this buffer. |
| 294 | * Returns 0 if something wrong happened, 1 if not. |
| 295 | */ |
| 296 | static int h3_parse_settings_frm(struct h3 *h3, const struct buffer *rxbuf, size_t flen) |
| 297 | { |
| 298 | uint64_t id, value; |
| 299 | const unsigned char *buf, *end; |
| 300 | |
| 301 | buf = (const unsigned char *)b_head(rxbuf); |
| 302 | end = buf + flen; |
| 303 | |
| 304 | while (buf <= end) { |
| 305 | if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end)) |
| 306 | return 0; |
| 307 | |
| 308 | h3_debug_printf(stderr, "%s id: %llu value: %llu\n", |
| 309 | __func__, (unsigned long long)id, (unsigned long long)value); |
| 310 | switch (id) { |
| 311 | case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY: |
| 312 | h3->qpack_max_table_capacity = value; |
| 313 | break; |
| 314 | case H3_SETTINGS_MAX_FIELD_SECTION_SIZE: |
| 315 | h3->max_field_section_size = value; |
| 316 | break; |
| 317 | case H3_SETTINGS_QPACK_BLOCKED_STREAMS: |
| 318 | h3->qpack_blocked_streams = value; |
| 319 | break; |
| 320 | case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5: |
| 321 | h3->err = H3_SETTINGS_ERROR; |
| 322 | return 0; |
| 323 | default: |
| 324 | /* MUST be ignored */ |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return 1; |
| 330 | } |
| 331 | |
| 332 | /* Decode <qcs> remotely initiated uni-stream. We stop parsing a frame as soon as |
| 333 | * there is not enough received data. |
| 334 | * Returns 0 if something wrong happened, 1 if not. |
| 335 | */ |
| 336 | static int h3_control_recv(struct h3_uqs *h3_uqs, void *ctx) |
| 337 | { |
| 338 | struct buffer *rxbuf = &h3_uqs->qcs->rx.buf; |
| 339 | struct h3 *h3 = ctx; |
| 340 | |
| 341 | h3_debug_printf(stderr, "%s STREAM ID: %llu\n", __func__, h3_uqs->qcs->by_id.key); |
| 342 | if (!b_data(rxbuf)) |
| 343 | return 1; |
| 344 | |
| 345 | while (b_data(rxbuf)) { |
| 346 | size_t hlen; |
| 347 | uint64_t ftype, flen; |
| 348 | struct buffer b; |
| 349 | |
| 350 | /* Work on a copy of <rxbuf> */ |
| 351 | b = h3_b_dup(rxbuf); |
| 352 | hlen = h3_decode_frm_header(&ftype, &flen, &b); |
| 353 | if (!hlen) |
| 354 | break; |
| 355 | |
| 356 | h3_debug_printf(stderr, "%s: ftype: %llu, flen: %llu\n", __func__, |
| 357 | (unsigned long long)ftype, (unsigned long long)flen); |
| 358 | if (flen > b_data(&b)) |
| 359 | break; |
| 360 | |
| 361 | b_del(rxbuf, hlen); |
| 362 | /* From here, a frame must not be truncated */ |
| 363 | switch (ftype) { |
| 364 | case H3_FT_CANCEL_PUSH: |
Amaury Denoyelle | e1f3ff0 | 2021-12-06 14:26:52 +0100 | [diff] [blame] | 365 | /* XXX TODO XXX */ |
| 366 | ABORT_NOW(); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 367 | break; |
| 368 | case H3_FT_SETTINGS: |
| 369 | if (!h3_parse_settings_frm(h3, rxbuf, flen)) |
| 370 | return 0; |
| 371 | break; |
| 372 | case H3_FT_GOAWAY: |
Amaury Denoyelle | e1f3ff0 | 2021-12-06 14:26:52 +0100 | [diff] [blame] | 373 | /* XXX TODO XXX */ |
| 374 | ABORT_NOW(); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 375 | break; |
| 376 | case H3_FT_MAX_PUSH_ID: |
Amaury Denoyelle | e1f3ff0 | 2021-12-06 14:26:52 +0100 | [diff] [blame] | 377 | /* XXX TODO XXX */ |
| 378 | ABORT_NOW(); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 379 | break; |
| 380 | default: |
| 381 | /* Error */ |
| 382 | h3->err = H3_FRAME_UNEXPECTED; |
| 383 | return 0; |
| 384 | } |
| 385 | b_del(rxbuf, flen); |
| 386 | } |
| 387 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 388 | /* Handle the case where remaining data are present in the buffer. This |
| 389 | * can happen if there is an incomplete frame. In this case, subscribe |
| 390 | * on the lower layer to restart receive operation. |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 391 | */ |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 392 | if (b_data(rxbuf)) |
| 393 | qcs_subscribe(h3_uqs->qcs, SUB_RETRY_RECV, &h3_uqs->wait_event); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 394 | |
| 395 | return 1; |
| 396 | } |
| 397 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 398 | /* Returns buffer for data sending. |
| 399 | * May be NULL if the allocation failed. |
| 400 | */ |
| 401 | static struct buffer *mux_get_buf(struct qcs *qcs) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 402 | { |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 403 | if (!b_size(&qcs->tx.buf)) |
| 404 | b_alloc(&qcs->tx.buf); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 405 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 406 | return &qcs->tx.buf; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* Function used to emit stream data from <h3_uqs> control uni-stream */ |
| 410 | static int h3_control_send(struct h3_uqs *h3_uqs, void *ctx) |
| 411 | { |
| 412 | int ret; |
| 413 | struct h3 *h3 = ctx; |
| 414 | 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] | 415 | struct buffer pos, *res; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 416 | |
| 417 | ret = 0; |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 418 | pos = b_make((char *)data, sizeof(data), 0, 0); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 419 | if (!(h3->flags & H3_CF_SETTINGS_SENT)) { |
| 420 | struct qcs *qcs = h3_uqs->qcs; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 421 | size_t frm_len; |
| 422 | |
| 423 | frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) + |
| 424 | quic_int_getsize(h3_settings_qpack_max_table_capacity) + |
| 425 | quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) + |
| 426 | quic_int_getsize(h3_settings_qpack_blocked_streams); |
| 427 | if (h3_settings_max_field_section_size) { |
| 428 | frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) + |
| 429 | quic_int_getsize(h3_settings_max_field_section_size); |
| 430 | } |
| 431 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 432 | b_quic_enc_int(&pos, H3_UNI_STRM_TP_CONTROL_STREAM); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 433 | /* Build a SETTINGS frame */ |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 434 | b_quic_enc_int(&pos, H3_FT_SETTINGS); |
| 435 | b_quic_enc_int(&pos, frm_len); |
| 436 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY); |
| 437 | b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity); |
| 438 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS); |
| 439 | b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 440 | if (h3_settings_max_field_section_size) { |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 441 | b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE); |
| 442 | b_quic_enc_int(&pos, h3_settings_max_field_section_size); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 443 | } |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 444 | |
| 445 | res = mux_get_buf(qcs); |
| 446 | if (b_room(res) < b_data(&pos)) { |
| 447 | // TODO the mux should be put in blocked state, with |
| 448 | // the stream in state waiting for settings to be sent |
| 449 | ABORT_NOW(); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 450 | } |
| 451 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 452 | ret = b_force_xfer(res, &pos, b_data(&pos)); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 453 | if (ret > 0) { |
| 454 | h3->flags |= H3_CF_SETTINGS_SENT; |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 455 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 456 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 457 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | return ret; |
| 461 | } |
| 462 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 463 | static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx) |
| 464 | { |
| 465 | struct buffer outbuf; |
| 466 | struct buffer headers_buf = BUF_NULL; |
| 467 | struct buffer *res; |
| 468 | struct http_hdr list[global.tune.max_http_hdr]; |
| 469 | struct htx_sl *sl; |
| 470 | struct htx_blk *blk; |
| 471 | enum htx_blk_type type; |
| 472 | int frame_length_size; /* size in bytes of frame length varint field */ |
| 473 | int ret = 0; |
| 474 | int hdr; |
| 475 | int status = 0; |
| 476 | |
| 477 | sl = NULL; |
| 478 | hdr = 0; |
| 479 | for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 480 | type = htx_get_blk_type(blk); |
| 481 | |
| 482 | if (type == HTX_BLK_UNUSED) |
| 483 | continue; |
| 484 | |
| 485 | if (type == HTX_BLK_EOH) |
| 486 | break; |
| 487 | |
| 488 | if (type == HTX_BLK_RES_SL) { |
| 489 | /* start-line -> HEADERS h3 frame */ |
| 490 | BUG_ON(sl); |
| 491 | sl = htx_get_blk_ptr(htx, blk); |
| 492 | /* TODO should be on h3 layer */ |
| 493 | status = sl->info.res.status; |
| 494 | } |
| 495 | else if (type == HTX_BLK_HDR) { |
| 496 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 497 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 498 | hdr++; |
| 499 | } |
| 500 | else { |
| 501 | ABORT_NOW(); |
| 502 | goto err; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | BUG_ON(!sl); |
| 507 | |
| 508 | list[hdr].n = ist(""); |
| 509 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 510 | res = mux_get_buf(qcs); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 511 | |
| 512 | /* At least 5 bytes to store frame type + length as a varint max size */ |
| 513 | if (b_room(res) < 5) |
| 514 | ABORT_NOW(); |
| 515 | |
| 516 | b_reset(&outbuf); |
| 517 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 518 | /* Start the headers after frame type + length */ |
| 519 | headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0); |
| 520 | |
| 521 | if (qpack_encode_field_section_line(&headers_buf)) |
| 522 | ABORT_NOW(); |
| 523 | if (qpack_encode_int_status(&headers_buf, status)) |
| 524 | ABORT_NOW(); |
| 525 | |
| 526 | for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) { |
| 527 | if (isteq(list[hdr].n, ist(""))) |
| 528 | break; |
| 529 | |
Amaury Denoyelle | ffafb3d | 2022-02-15 16:10:42 +0100 | [diff] [blame] | 530 | /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges |
| 531 | * Transfer codings (see Section 6.1 of [HTTP11]) are not |
| 532 | * defined for HTTP/3; the Transfer-Encoding header field MUST |
| 533 | * NOT be used. |
| 534 | */ |
| 535 | if (isteq(list[hdr].n, ist("transfer-encoding"))) |
| 536 | continue; |
| 537 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 538 | if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v)) |
| 539 | ABORT_NOW(); |
| 540 | } |
| 541 | |
| 542 | /* Now that all headers are encoded, we are certain that res buffer is |
| 543 | * big enough |
| 544 | */ |
| 545 | frame_length_size = quic_int_getsize(b_data(&headers_buf)); |
| 546 | res->head += 4 - frame_length_size; |
| 547 | b_putchr(res, 0x01); /* h3 HEADERS frame type */ |
| 548 | if (!b_quic_enc_int(res, b_data(&headers_buf))) |
| 549 | ABORT_NOW(); |
| 550 | b_add(res, b_data(&headers_buf)); |
| 551 | |
| 552 | ret = 0; |
| 553 | blk = htx_get_head_blk(htx); |
| 554 | while (blk) { |
| 555 | type = htx_get_blk_type(blk); |
| 556 | ret += htx_get_blksz(blk); |
| 557 | blk = htx_remove_blk(htx, blk); |
| 558 | if (type == HTX_BLK_EOH) |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | return ret; |
| 563 | |
| 564 | err: |
| 565 | return 0; |
| 566 | } |
| 567 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 568 | /* Returns the total of bytes sent. */ |
| 569 | static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count) |
| 570 | { |
| 571 | struct buffer outbuf; |
| 572 | struct buffer *res; |
| 573 | size_t total = 0; |
| 574 | struct htx *htx; |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 575 | int bsize, fsize, hsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 576 | struct htx_blk *blk; |
| 577 | enum htx_blk_type type; |
| 578 | |
| 579 | htx = htx_from_buf(buf); |
| 580 | |
| 581 | new_frame: |
| 582 | if (!count || htx_is_empty(htx)) |
| 583 | goto end; |
| 584 | |
| 585 | blk = htx_get_head_blk(htx); |
| 586 | type = htx_get_blk_type(blk); |
| 587 | fsize = bsize = htx_get_blksz(blk); |
| 588 | |
| 589 | if (type != HTX_BLK_DATA) |
| 590 | goto end; |
| 591 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 592 | res = mux_get_buf(qcs); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 593 | |
| 594 | if (fsize > count) |
| 595 | fsize = count; |
| 596 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 597 | /* h3 DATA headers : 1-byte frame type + varint frame length */ |
| 598 | hsize = 1 + QUIC_VARINT_MAX_SIZE; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 599 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 600 | while (1) { |
| 601 | b_reset(&outbuf); |
| 602 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 603 | if (b_size(&outbuf) > hsize || !b_space_wraps(res)) |
| 604 | break; |
| 605 | b_slow_realign(res, trash.area, b_data(res)); |
| 606 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 607 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 608 | /* Not enough room for headers and at least one data byte, block the |
| 609 | * stream. It is expected that the conn-stream layer will subscribe on |
| 610 | * SEND. |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 611 | */ |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 612 | if (b_size(&outbuf) <= hsize) { |
| 613 | qcs->flags |= QC_SF_BLK_MROOM; |
| 614 | goto end; |
| 615 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 616 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 617 | if (b_size(&outbuf) < hsize + fsize) |
| 618 | fsize = b_size(&outbuf) - hsize; |
| 619 | BUG_ON(fsize <= 0); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 620 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 621 | b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */ |
| 622 | b_quic_enc_int(&outbuf, fsize); /* h3 frame length */ |
| 623 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 624 | b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize); |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 625 | total += fsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 626 | count -= fsize; |
| 627 | |
| 628 | if (fsize == bsize) |
| 629 | htx_remove_blk(htx, blk); |
| 630 | else |
| 631 | htx_cut_data_blk(htx, blk, fsize); |
| 632 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 633 | /* commit the buffer */ |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 634 | b_add(res, b_data(&outbuf)); |
| 635 | goto new_frame; |
| 636 | |
| 637 | end: |
| 638 | return total; |
| 639 | } |
| 640 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 641 | size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 642 | { |
| 643 | size_t total = 0; |
| 644 | struct qcs *qcs = cs->ctx; |
| 645 | struct htx *htx; |
| 646 | enum htx_blk_type btype; |
| 647 | struct htx_blk *blk; |
| 648 | uint32_t bsize; |
| 649 | int32_t idx; |
| 650 | int ret; |
| 651 | |
Amaury Denoyelle | d8769d1 | 2022-03-25 15:28:33 +0100 | [diff] [blame] | 652 | h3_debug_printf(stderr, "%s\n", __func__); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 653 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 654 | htx = htx_from_buf(buf); |
| 655 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 656 | while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) { |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 657 | idx = htx_get_head(htx); |
| 658 | blk = htx_get_blk(htx, idx); |
| 659 | btype = htx_get_blk_type(blk); |
| 660 | bsize = htx_get_blksz(blk); |
| 661 | |
| 662 | /* Not implemented : QUIC on backend side */ |
| 663 | BUG_ON(btype == HTX_BLK_REQ_SL); |
| 664 | |
| 665 | switch (btype) { |
| 666 | case HTX_BLK_RES_SL: |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 667 | /* start-line -> HEADERS h3 frame */ |
| 668 | ret = h3_resp_headers_send(qcs, htx); |
| 669 | if (ret > 0) { |
| 670 | total += ret; |
| 671 | count -= ret; |
| 672 | if (ret < bsize) |
| 673 | goto out; |
| 674 | } |
| 675 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 676 | |
| 677 | case HTX_BLK_DATA: |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 678 | ret = h3_resp_data_send(qcs, buf, count); |
| 679 | if (ret > 0) { |
| 680 | htx = htx_from_buf(buf); |
| 681 | total += ret; |
| 682 | count -= ret; |
| 683 | if (ret < bsize) |
| 684 | goto out; |
| 685 | } |
| 686 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 687 | |
| 688 | case HTX_BLK_TLR: |
| 689 | case HTX_BLK_EOT: |
| 690 | /* TODO trailers */ |
| 691 | |
| 692 | default: |
| 693 | htx_remove_blk(htx, blk); |
| 694 | total += bsize; |
| 695 | count -= bsize; |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | |
Amaury Denoyelle | c2025c1 | 2021-12-03 15:03:36 +0100 | [diff] [blame] | 700 | if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) |
| 701 | qcs->flags |= QC_SF_FIN_STREAM; |
| 702 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 703 | out: |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 704 | if (total) { |
| 705 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 706 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 707 | } |
| 708 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 709 | return total; |
Amaury Denoyelle | f52151d | 2021-08-24 16:11:18 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 712 | /* Finalize the initialization of remotely initiated uni-stream <qcs>. |
| 713 | * Return 1 if succeeded, 0 if not. In this latter case, set the ->err h3 error |
| 714 | * to inform the QUIC mux layer of the encountered error. |
| 715 | */ |
| 716 | static int h3_attach_ruqs(struct qcs *qcs, void *ctx) |
| 717 | { |
| 718 | uint64_t strm_type; |
| 719 | struct h3 *h3 = ctx; |
| 720 | struct buffer *rxbuf = &qcs->rx.buf; |
| 721 | |
| 722 | /* First octets: the uni-stream type */ |
| 723 | if (!b_quic_dec_int(&strm_type, rxbuf, NULL) || strm_type > H3_UNI_STRM_TP_MAX) |
| 724 | return 0; |
| 725 | |
| 726 | /* Note that for all the uni-streams below, this is an error to receive two times the |
| 727 | * same type of uni-stream (even for Push stream which is not supported at this time. |
| 728 | */ |
| 729 | switch (strm_type) { |
| 730 | case H3_UNI_STRM_TP_CONTROL_STREAM: |
| 731 | if (h3->rctrl.qcs) { |
| 732 | h3->err = H3_STREAM_CREATION_ERROR; |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | h3->rctrl.qcs = qcs; |
| 737 | h3->rctrl.cb = h3_control_recv; |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 738 | qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rctrl.wait_event); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 739 | break; |
| 740 | case H3_UNI_STRM_TP_PUSH_STREAM: |
| 741 | /* NOT SUPPORTED */ |
| 742 | break; |
| 743 | case H3_UNI_STRM_TP_QPACK_ENCODER: |
| 744 | if (h3->rqpack_enc.qcs) { |
| 745 | h3->err = H3_STREAM_CREATION_ERROR; |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | h3->rqpack_enc.qcs = qcs; |
| 750 | h3->rqpack_enc.cb = qpack_decode_enc; |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 751 | qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_enc.wait_event); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 752 | break; |
| 753 | case H3_UNI_STRM_TP_QPACK_DECODER: |
| 754 | if (h3->rqpack_dec.qcs) { |
| 755 | h3->err = H3_STREAM_CREATION_ERROR; |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | h3->rqpack_dec.qcs = qcs; |
| 760 | h3->rqpack_dec.cb = qpack_decode_dec; |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 761 | qcs_subscribe(qcs, SUB_RETRY_RECV, &h3->rqpack_dec.wait_event); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 762 | break; |
| 763 | default: |
| 764 | /* Error */ |
| 765 | h3->err = H3_STREAM_CREATION_ERROR; |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | return 1; |
| 770 | } |
| 771 | |
| 772 | static int h3_finalize(void *ctx) |
| 773 | { |
| 774 | struct h3 *h3 = ctx; |
| 775 | |
Amaury Denoyelle | b788054 | 2022-02-09 10:28:53 +0100 | [diff] [blame] | 776 | h3->lctrl.qcs = qcs_new(h3->qcc, 0x3, QCS_SRV_UNI); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 777 | if (!h3->lctrl.qcs) |
| 778 | return 0; |
| 779 | |
| 780 | /* Wakeup ->lctrl uni-stream */ |
Frédéric Lécaille | e16f0bd | 2021-08-23 09:50:29 +0200 | [diff] [blame] | 781 | h3_control_send(&h3->lctrl, h3); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 782 | |
| 783 | return 1; |
| 784 | } |
| 785 | |
| 786 | /* Tasklet dedicated to h3 incoming uni-streams */ |
| 787 | static struct task *h3_uqs_task(struct task *t, void *ctx, unsigned int state) |
| 788 | { |
| 789 | struct h3_uqs *h3_uqs = ctx; |
| 790 | struct h3 *h3 = h3_uqs->qcs->qcc->ctx; |
| 791 | |
| 792 | h3_uqs->cb(h3_uqs, h3); |
| 793 | return NULL; |
| 794 | } |
| 795 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 796 | /* Release all the tasklet attached to <h3_uqs> uni-stream */ |
| 797 | static inline void h3_uqs_tasklet_release(struct h3_uqs *h3_uqs) |
| 798 | { |
| 799 | struct tasklet *t = h3_uqs->wait_event.tasklet; |
| 800 | |
| 801 | if (t) |
| 802 | tasklet_free(t); |
| 803 | } |
| 804 | |
| 805 | /* Release all the tasklet attached to <h3> uni-streams */ |
| 806 | static void h3_uqs_tasklets_release(struct h3 *h3) |
| 807 | { |
| 808 | h3_uqs_tasklet_release(&h3->rqpack_enc); |
| 809 | h3_uqs_tasklet_release(&h3->rqpack_dec); |
| 810 | h3_uqs_tasklet_release(&h3->rctrl); |
| 811 | } |
| 812 | |
| 813 | /* Tasklet dedicated to h3 outgoing uni-streams */ |
| 814 | __maybe_unused |
| 815 | static struct task *h3_uqs_send_task(struct task *t, void *ctx, unsigned int state) |
| 816 | { |
| 817 | struct h3_uqs *h3_uqs = ctx; |
| 818 | struct h3 *h3 = h3_uqs->qcs->qcc->ctx; |
| 819 | |
| 820 | h3_uqs->cb(h3_uqs, h3); |
| 821 | return NULL; |
| 822 | } |
| 823 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 824 | /* Initialize <h3_uqs> uni-stream with <t> as tasklet */ |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 825 | static int h3_uqs_init(struct h3_uqs *h3_uqs, struct h3 *h3, |
| 826 | int (*cb)(struct h3_uqs *h3_uqs, void *ctx), |
| 827 | struct task *(*t)(struct task *, void *, unsigned int)) |
| 828 | { |
| 829 | h3_uqs->qcs = NULL; |
| 830 | h3_uqs->cb = cb; |
| 831 | h3_uqs->wait_event.tasklet = tasklet_new(); |
| 832 | if (!h3_uqs->wait_event.tasklet) |
| 833 | return 0; |
| 834 | |
| 835 | h3_uqs->wait_event.tasklet->process = t; |
| 836 | h3_uqs->wait_event.tasklet->context = h3_uqs; |
| 837 | return 1; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | static inline void h3_uqs_release(struct h3_uqs *h3_uqs) |
| 841 | { |
| 842 | if (h3_uqs->qcs) |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 843 | qcs_free(h3_uqs->qcs); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static inline void h3_uqs_release_all(struct h3 *h3) |
| 847 | { |
| 848 | h3_uqs_tasklet_release(&h3->lctrl); |
| 849 | h3_uqs_release(&h3->lctrl); |
| 850 | h3_uqs_tasklet_release(&h3->lqpack_enc); |
| 851 | h3_uqs_release(&h3->lqpack_enc); |
| 852 | h3_uqs_tasklet_release(&h3->lqpack_dec); |
| 853 | h3_uqs_release(&h3->lqpack_dec); |
| 854 | } |
| 855 | |
| 856 | /* Initialize the HTTP/3 context for <qcc> mux. |
| 857 | * Return 1 if succeeded, 0 if not. |
| 858 | */ |
| 859 | static int h3_init(struct qcc *qcc) |
| 860 | { |
| 861 | struct h3 *h3; |
| 862 | |
| 863 | h3 = pool_alloc(pool_head_h3); |
| 864 | if (!h3) |
| 865 | goto fail_no_h3; |
| 866 | |
| 867 | h3->qcc = qcc; |
| 868 | h3->err = H3_NO_ERROR; |
| 869 | h3->flags = 0; |
| 870 | |
| 871 | if (!h3_uqs_init(&h3->rqpack_enc, h3, NULL, h3_uqs_task) || |
| 872 | !h3_uqs_init(&h3->rqpack_dec, h3, NULL, h3_uqs_task) || |
| 873 | !h3_uqs_init(&h3->rctrl, h3, h3_control_recv, h3_uqs_task)) |
| 874 | goto fail_no_h3_ruqs; |
| 875 | |
| 876 | if (!h3_uqs_init(&h3->lctrl, h3, h3_control_send, h3_uqs_task) || |
| 877 | !h3_uqs_init(&h3->lqpack_enc, h3, NULL, h3_uqs_task) || |
| 878 | !h3_uqs_init(&h3->lqpack_dec, h3, NULL, h3_uqs_task)) |
| 879 | goto fail_no_h3_luqs; |
| 880 | |
| 881 | qcc->ctx = h3; |
| 882 | LIST_INIT(&h3->buf_wait.list); |
| 883 | |
| 884 | return 1; |
| 885 | |
| 886 | fail_no_h3_ruqs: |
| 887 | h3_uqs_release_all(h3); |
| 888 | fail_no_h3_luqs: |
| 889 | h3_uqs_tasklets_release(h3); |
| 890 | pool_free(pool_head_h3, h3); |
| 891 | fail_no_h3: |
| 892 | return 0; |
| 893 | } |
| 894 | |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame^] | 895 | static void h3_release(void *ctx) |
| 896 | { |
| 897 | struct h3 *h3 = ctx; |
| 898 | |
| 899 | h3_uqs_release_all(h3); |
| 900 | h3_uqs_tasklets_release(h3); |
| 901 | pool_free(pool_head_h3, h3); |
| 902 | } |
| 903 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 904 | /* HTTP/3 application layer operations */ |
| 905 | const struct qcc_app_ops h3_ops = { |
| 906 | .init = h3_init, |
| 907 | .attach_ruqs = h3_attach_ruqs, |
| 908 | .decode_qcs = h3_decode_qcs, |
Amaury Denoyelle | abbe91e | 2021-11-12 16:09:29 +0100 | [diff] [blame] | 909 | .snd_buf = h3_snd_buf, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 910 | .finalize = h3_finalize, |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame^] | 911 | .release = h3_release, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 912 | }; |