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