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