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