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