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