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