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