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