Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HTTP/3 protocol processing |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation, version 2.1 |
| 7 | * exclusively. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include <haproxy/buf.h> |
Amaury Denoyelle | 9904355 | 2021-08-24 15:36:02 +0200 | [diff] [blame] | 20 | #include <haproxy/connection.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 21 | #include <haproxy/dynbuf.h> |
| 22 | #include <haproxy/h3.h> |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 23 | #include <haproxy/h3_stats.h> |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 24 | #include <haproxy/http.h> |
| 25 | #include <haproxy/htx.h> |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 26 | #include <haproxy/intops.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 27 | #include <haproxy/istbuf.h> |
Amaury Denoyelle | 846cc04 | 2022-04-04 16:13:44 +0200 | [diff] [blame] | 28 | #include <haproxy/mux_quic.h> |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 29 | #include <haproxy/ncbuf.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 30 | #include <haproxy/pool.h> |
| 31 | #include <haproxy/qpack-dec.h> |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 32 | #include <haproxy/qpack-enc.h> |
| 33 | #include <haproxy/quic_enc.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 34 | #include <haproxy/stconn.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 35 | #include <haproxy/tools.h> |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 36 | #include <haproxy/trace.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 37 | #include <haproxy/xprt_quic.h> |
| 38 | |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 39 | /* trace source and events */ |
| 40 | static void h3_trace(enum trace_level level, uint64_t mask, |
| 41 | const struct trace_source *src, |
| 42 | const struct ist where, const struct ist func, |
| 43 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 44 | |
| 45 | static const struct trace_event h3_trace_events[] = { |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 46 | #define H3_EV_RX_FRAME (1ULL << 0) |
| 47 | { .mask = H3_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H3 frame" }, |
| 48 | #define H3_EV_RX_DATA (1ULL << 1) |
| 49 | { .mask = H3_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H3 DATA frame" }, |
| 50 | #define H3_EV_RX_HDR (1ULL << 2) |
| 51 | { .mask = H3_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H3 HEADERS frame" }, |
| 52 | #define H3_EV_RX_SETTINGS (1ULL << 3) |
| 53 | { .mask = H3_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H3 SETTINGS frame" }, |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 54 | #define H3_EV_TX_DATA (1ULL << 4) |
| 55 | { .mask = H3_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H3 DATA frame" }, |
| 56 | #define H3_EV_TX_HDR (1ULL << 5) |
| 57 | { .mask = H3_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H3 HEADERS frame" }, |
| 58 | #define H3_EV_TX_SETTINGS (1ULL << 6) |
| 59 | { .mask = H3_EV_TX_SETTINGS, .name = "tx_settings", .desc = "transmission of H3 SETTINGS frame" }, |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 60 | #define H3_EV_H3S_NEW (1ULL << 7) |
| 61 | { .mask = H3_EV_H3S_NEW, .name = "h3s_new", .desc = "new H3 stream" }, |
| 62 | #define H3_EV_H3S_END (1ULL << 8) |
| 63 | { .mask = H3_EV_H3S_END, .name = "h3s_end", .desc = "H3 stream terminated" }, |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 64 | { } |
| 65 | }; |
| 66 | |
| 67 | static const struct name_desc h3_trace_lockon_args[4] = { |
| 68 | /* arg1 */ { /* already used by the connection */ }, |
| 69 | /* arg2 */ { .name="qcs", .desc="QUIC stream" }, |
| 70 | /* arg3 */ { }, |
| 71 | /* arg4 */ { } |
| 72 | }; |
| 73 | |
| 74 | static const struct name_desc h3_trace_decoding[] = { |
| 75 | #define H3_VERB_CLEAN 1 |
| 76 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 77 | #define H3_VERB_MINIMAL 2 |
| 78 | { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" }, |
| 79 | { /* end */ } |
| 80 | }; |
| 81 | |
| 82 | struct trace_source trace_h3 = { |
| 83 | .name = IST("h3"), |
| 84 | .desc = "HTTP/3 transcoder", |
| 85 | .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */ |
| 86 | .default_cb = h3_trace, |
| 87 | .known_events = h3_trace_events, |
| 88 | .lockon_args = h3_trace_lockon_args, |
| 89 | .decoding = h3_trace_decoding, |
| 90 | .report_events = ~0, /* report everything by default */ |
| 91 | }; |
| 92 | |
| 93 | #define TRACE_SOURCE &trace_h3 |
| 94 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 95 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 96 | #if defined(DEBUG_H3) |
| 97 | #define h3_debug_printf fprintf |
| 98 | #define h3_debug_hexdump debug_hexdump |
| 99 | #else |
| 100 | #define h3_debug_printf(...) do { } while (0) |
| 101 | #define h3_debug_hexdump(...) do { } while (0) |
| 102 | #endif |
| 103 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 104 | #define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */ |
| 105 | #define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */ |
| 106 | #define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */ |
| 107 | #define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */ |
| 108 | #define H3_CF_UNI_QPACK_ENC_SET 0x00000010 /* Remote QPACK encoder stream opened */ |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 109 | |
| 110 | /* Default settings */ |
Amaury Denoyelle | 3394939 | 2021-08-24 15:16:58 +0200 | [diff] [blame] | 111 | static uint64_t h3_settings_qpack_max_table_capacity = 0; |
| 112 | static uint64_t h3_settings_qpack_blocked_streams = 4096; |
| 113 | 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] | 114 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 115 | struct h3c { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 116 | struct qcc *qcc; |
| 117 | enum h3_err err; |
| 118 | uint32_t flags; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 119 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 120 | /* Settings */ |
| 121 | uint64_t qpack_max_table_capacity; |
| 122 | uint64_t qpack_blocked_streams; |
| 123 | uint64_t max_field_section_size; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 124 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 125 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 126 | /* Stats counters */ |
| 127 | struct h3_counters *prx_counters; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 130 | 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] | 131 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 132 | #define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */ |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 133 | #define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */ |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 134 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 135 | struct h3s { |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 136 | enum h3s_t type; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 137 | int demux_frame_len; |
| 138 | int demux_frame_type; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 139 | |
| 140 | int flags; |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s)); |
| 144 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 145 | /* Simple function to duplicate a buffer */ |
Amaury Denoyelle | c7dd9d6 | 2022-05-24 18:14:28 +0200 | [diff] [blame] | 146 | 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] | 147 | { |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 148 | 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] | 149 | } |
| 150 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 151 | /* Initialize an uni-stream <qcs> by reading its type from <rxbuf>. |
| 152 | * |
| 153 | * Returns 0 on success else non-zero. |
| 154 | */ |
| 155 | static int h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs, |
| 156 | struct ncbuf *rxbuf) |
| 157 | { |
| 158 | /* decode unidirectional stream type */ |
| 159 | struct h3s *h3s = qcs->ctx; |
| 160 | struct buffer b; |
| 161 | uint64_t type; |
| 162 | size_t len = 0, ret; |
| 163 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 164 | TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
| 165 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 166 | BUG_ON_HOT(!quic_stream_is_uni(qcs->id) || |
| 167 | h3s->flags & H3_SF_UNI_INIT); |
| 168 | |
| 169 | b = h3_b_dup(rxbuf); |
| 170 | ret = b_quic_dec_int(&type, &b, &len); |
| 171 | if (!ret) { |
| 172 | ABORT_NOW(); |
| 173 | } |
| 174 | |
| 175 | switch (type) { |
| 176 | case H3_UNI_S_T_CTRL: |
| 177 | if (h3c->flags & H3_CF_UNI_CTRL_SET) { |
| 178 | qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR); |
| 179 | return 1; |
| 180 | } |
| 181 | h3c->flags |= H3_CF_UNI_CTRL_SET; |
| 182 | h3s->type = H3S_T_CTRL; |
| 183 | break; |
| 184 | |
| 185 | case H3_UNI_S_T_PUSH: |
| 186 | /* TODO not supported for the moment */ |
| 187 | h3s->type = H3S_T_PUSH; |
| 188 | break; |
| 189 | |
| 190 | case H3_UNI_S_T_QPACK_DEC: |
| 191 | if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) { |
| 192 | qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR); |
| 193 | return 1; |
| 194 | } |
| 195 | h3c->flags |= H3_CF_UNI_QPACK_DEC_SET; |
| 196 | h3s->type = H3S_T_QPACK_DEC; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 197 | h3s->flags |= H3_SF_UNI_NO_H3; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 198 | break; |
| 199 | |
| 200 | case H3_UNI_S_T_QPACK_ENC: |
| 201 | if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) { |
| 202 | qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR); |
| 203 | return 1; |
| 204 | } |
| 205 | h3c->flags |= H3_CF_UNI_QPACK_ENC_SET; |
| 206 | h3s->type = H3S_T_QPACK_ENC; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 207 | h3s->flags |= H3_SF_UNI_NO_H3; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 208 | break; |
| 209 | |
| 210 | default: |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 211 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
| 212 | * |
| 213 | * Implementations MUST [...] abort reading on unidirectional |
| 214 | * streams that have unknown or unsupported types. |
| 215 | */ |
| 216 | qcs->flags |= QC_SF_READ_ABORTED; |
| 217 | return 1; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | h3s->flags |= H3_SF_UNI_INIT; |
| 221 | qcs_consume(qcs, len); |
| 222 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 223 | TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 227 | /* Parse an uni-stream <qcs> from <rxbuf> which does not contains H3 frames. |
| 228 | * This may be used for QPACK encoder/decoder streams for example. |
| 229 | * |
| 230 | * Returns 0 on success else non-zero. |
| 231 | */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 232 | static int h3_parse_uni_stream_no_h3(struct qcs *qcs, struct ncbuf *rxbuf) |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 233 | { |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 234 | struct h3s *h3s = qcs->ctx; |
| 235 | |
| 236 | BUG_ON_HOT(!quic_stream_is_uni(qcs->id) || |
| 237 | !(h3s->flags & H3_SF_UNI_NO_H3)); |
| 238 | |
| 239 | switch (h3s->type) { |
| 240 | case H3S_T_QPACK_DEC: |
Amaury Denoyelle | 5869cb6 | 2022-05-31 15:21:27 +0200 | [diff] [blame] | 241 | if (qpack_decode_dec(qcs, NULL)) |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 242 | return 1; |
| 243 | break; |
| 244 | case H3S_T_QPACK_ENC: |
Amaury Denoyelle | 5869cb6 | 2022-05-31 15:21:27 +0200 | [diff] [blame] | 245 | if (qpack_decode_enc(qcs, NULL)) |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 246 | return 1; |
| 247 | break; |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 248 | case H3S_T_UNKNOWN: |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 249 | default: |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 250 | /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */ |
| 251 | ABORT_NOW(); |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 257 | /* Decode a H3 frame header from <rxbuf> buffer. The frame type is stored in |
| 258 | * <ftype> and length in <flen>. |
| 259 | * |
| 260 | * Returns the size of the H3 frame header. Note that the input buffer is not |
| 261 | * consumed. |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 262 | */ |
| 263 | static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen, |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 264 | struct ncbuf *rxbuf) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 265 | { |
| 266 | size_t hlen; |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 267 | struct buffer b = h3_b_dup(rxbuf); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 268 | |
| 269 | hlen = 0; |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 270 | if (!b_quic_dec_int(ftype, &b, &hlen) || |
| 271 | !b_quic_dec_int(flen, &b, &hlen)) { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 272 | return 0; |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 273 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 274 | |
| 275 | return hlen; |
| 276 | } |
| 277 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 278 | /* Check if H3 frame of type <ftype> is valid when received on stream <qcs>. |
| 279 | * |
| 280 | * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should |
| 281 | * be reported. |
| 282 | */ |
| 283 | static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype) |
| 284 | { |
| 285 | struct h3s *h3s = qcs->ctx; |
| 286 | const uint64_t id = qcs->id; |
| 287 | |
| 288 | BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN); |
| 289 | |
| 290 | switch (ftype) { |
| 291 | case H3_FT_DATA: |
| 292 | case H3_FT_HEADERS: |
| 293 | return h3s->type != H3S_T_CTRL; |
| 294 | |
| 295 | case H3_FT_CANCEL_PUSH: |
| 296 | case H3_FT_GOAWAY: |
| 297 | case H3_FT_MAX_PUSH_ID: |
| 298 | /* Only allowed for control stream. First frame of control |
| 299 | * stream MUST be SETTINGS. |
| 300 | */ |
| 301 | return h3s->type == H3S_T_CTRL && |
| 302 | (h3c->flags & H3_CF_SETTINGS_RECV); |
| 303 | |
| 304 | case H3_FT_SETTINGS: |
| 305 | /* draft-ietf-quic-http34 7.2.4. SETTINGS |
| 306 | * |
| 307 | * If an endpoint receives a second SETTINGS frame on the control |
| 308 | * stream, the endpoint MUST respond with a connection error of type |
| 309 | * H3_FRAME_UNEXPECTED. |
| 310 | */ |
| 311 | return h3s->type == H3S_T_CTRL && |
| 312 | !(h3c->flags & H3_CF_SETTINGS_RECV); |
| 313 | |
| 314 | case H3_FT_PUSH_PROMISE: |
| 315 | return h3s->type != H3S_T_CTRL && |
| 316 | (id & QCS_ID_SRV_INTIATOR_BIT); |
| 317 | |
| 318 | default: |
| 319 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
| 320 | * |
| 321 | * Implementations MUST discard frames [...] that have unknown |
| 322 | * or unsupported types. |
| 323 | */ |
| 324 | return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV); |
| 325 | } |
| 326 | } |
| 327 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 328 | /* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 329 | * in a local HTX buffer and transfer to the stream connector layer. <fin> must be |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 330 | * set if this is the last data to transfer from this stream. |
| 331 | * |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 332 | * Returns the number of bytes handled or a negative error code. |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 333 | */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 334 | 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] | 335 | char fin) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 336 | { |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 337 | struct buffer htx_buf = BUF_NULL; |
| 338 | struct buffer *tmp = get_trash_chunk(); |
Amaury Denoyelle | 7059ebc | 2021-12-08 15:51:04 +0100 | [diff] [blame] | 339 | struct htx *htx = NULL; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 340 | struct htx_sl *sl; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 341 | struct http_hdr list[global.tune.max_http_hdr]; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 342 | unsigned int flags = HTX_SL_F_NONE; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 343 | struct ist meth = IST_NULL, path = IST_NULL; |
| 344 | //struct ist scheme = IST_NULL, authority = IST_NULL; |
| 345 | struct ist authority = IST_NULL; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 346 | int hdr_idx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 347 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 348 | TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 349 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 350 | /* TODO support buffer wrapping */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 351 | BUG_ON(ncb_head(buf) + len >= ncb_wrap(buf)); |
| 352 | 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] | 353 | return -1; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 354 | |
| 355 | qc_get_buf(qcs, &htx_buf); |
| 356 | BUG_ON(!b_size(&htx_buf)); |
| 357 | htx = htx_from_buf(&htx_buf); |
| 358 | |
| 359 | /* first treat pseudo-header to build the start line */ |
| 360 | hdr_idx = 0; |
| 361 | while (1) { |
| 362 | if (isteq(list[hdr_idx].n, ist(""))) |
| 363 | break; |
| 364 | |
| 365 | if (istmatch(list[hdr_idx].n, ist(":"))) { |
| 366 | /* pseudo-header */ |
| 367 | if (isteq(list[hdr_idx].n, ist(":method"))) |
| 368 | meth = list[hdr_idx].v; |
| 369 | else if (isteq(list[hdr_idx].n, ist(":path"))) |
| 370 | path = list[hdr_idx].v; |
| 371 | //else if (isteq(list[hdr_idx].n, ist(":scheme"))) |
| 372 | // scheme = list[hdr_idx].v; |
| 373 | else if (isteq(list[hdr_idx].n, ist(":authority"))) |
| 374 | authority = list[hdr_idx].v; |
| 375 | } |
| 376 | |
| 377 | ++hdr_idx; |
| 378 | } |
| 379 | |
| 380 | flags |= HTX_SL_F_VER_11; |
Amaury Denoyelle | 0fa14a6 | 2022-04-26 16:24:39 +0200 | [diff] [blame] | 381 | flags |= HTX_SL_F_XFER_LEN; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 382 | |
| 383 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0")); |
| 384 | if (!sl) |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 385 | return -1; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 386 | |
| 387 | if (fin) |
| 388 | sl->flags |= HTX_SL_F_BODYLESS; |
| 389 | |
| 390 | sl->info.req.meth = find_http_meth(meth.ptr, meth.len); |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 391 | |
| 392 | if (isttest(authority)) |
| 393 | htx_add_header(htx, ist("host"), authority); |
| 394 | |
| 395 | /* now treat standard headers */ |
| 396 | hdr_idx = 0; |
| 397 | while (1) { |
| 398 | if (isteq(list[hdr_idx].n, ist(""))) |
| 399 | break; |
| 400 | |
| 401 | if (!istmatch(list[hdr_idx].n, ist(":"))) |
| 402 | htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v); |
| 403 | |
| 404 | ++hdr_idx; |
| 405 | } |
| 406 | |
| 407 | htx_add_endof(htx, HTX_BLK_EOH); |
| 408 | htx_to_buf(htx, &htx_buf); |
| 409 | |
| 410 | if (fin) |
| 411 | htx->flags |= HTX_FL_EOM; |
| 412 | |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 413 | if (!qc_attach_sc(qcs, &htx_buf)) |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 414 | return -1; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 415 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 416 | /* buffer is transferred to the stream connector and set to NULL |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 417 | * except on stream creation error. |
| 418 | */ |
| 419 | b_free(&htx_buf); |
| 420 | offer_buffers(NULL, 1); |
| 421 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 422 | TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 423 | return len; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 426 | /* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs> |
| 427 | * HTX buffer. <fin> must be set if this is the last data to transfer from this |
| 428 | * stream. |
| 429 | * |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 430 | * Returns the number of bytes handled or a negative error code. |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 431 | */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 432 | 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] | 433 | char fin) |
| 434 | { |
| 435 | struct buffer *appbuf; |
| 436 | struct htx *htx = NULL; |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 437 | size_t htx_sent = 0; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 438 | int htx_space; |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 439 | char *head; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 440 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 441 | TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs); |
| 442 | |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 443 | appbuf = qc_get_buf(qcs, &qcs->rx.app_buf); |
| 444 | BUG_ON(!appbuf); |
| 445 | htx = htx_from_buf(appbuf); |
| 446 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 447 | if (len > ncb_data(buf, 0)) { |
| 448 | len = ncb_data(buf, 0); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 449 | fin = 0; |
| 450 | } |
| 451 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 452 | head = ncb_head(buf); |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 453 | retry: |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 454 | htx_space = htx_free_data_space(htx); |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 455 | if (!htx_space) { |
| 456 | qcs->flags |= QC_SF_DEM_FULL; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 457 | goto out; |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 458 | } |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 459 | |
| 460 | if (len > htx_space) { |
| 461 | len = htx_space; |
| 462 | fin = 0; |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 463 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 464 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 465 | if (head + len > ncb_wrap(buf)) { |
| 466 | size_t contig = ncb_wrap(buf) - head; |
| 467 | htx_sent = htx_add_data(htx, ist2(ncb_head(buf), contig)); |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 468 | if (htx_sent < contig) { |
| 469 | qcs->flags |= QC_SF_DEM_FULL; |
| 470 | goto out; |
| 471 | } |
| 472 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 473 | len -= contig; |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 474 | head = ncb_orig(buf); |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 475 | goto retry; |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 476 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 477 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 478 | htx_sent += htx_add_data(htx, ist2(head, len)); |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 479 | if (htx_sent < len) { |
| 480 | qcs->flags |= QC_SF_DEM_FULL; |
| 481 | goto out; |
| 482 | } |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 483 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 484 | if (fin && len == htx_sent) |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 485 | htx->flags |= HTX_FL_EOM; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 486 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 487 | out: |
| 488 | htx_to_buf(htx, appbuf); |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 489 | |
| 490 | TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 491 | return htx_sent; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 494 | /* Parse a SETTINGS frame of length <len> of payload <rxbuf>. |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 495 | * |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 496 | * Returns the number of bytes handled or a negative error code. |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 497 | */ |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 498 | static size_t h3_parse_settings_frm(struct h3c *h3c, const struct ncbuf *rxbuf, |
| 499 | size_t len) |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 500 | { |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 501 | struct buffer b; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 502 | uint64_t id, value; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 503 | size_t ret = 0; |
| 504 | long mask = 0; /* used to detect duplicated settings identifier */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 505 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 506 | TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS, h3c->qcc->conn); |
| 507 | |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 508 | b = h3_b_dup(rxbuf); |
| 509 | b_set_data(&b, len); |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 510 | |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 511 | while (b_data(&b)) { |
| 512 | if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) { |
| 513 | h3c->err = H3_FRAME_ERROR; |
| 514 | return -1; |
| 515 | } |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 516 | |
| 517 | h3_debug_printf(stderr, "%s id: %llu value: %llu\n", |
| 518 | __func__, (unsigned long long)id, (unsigned long long)value); |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 519 | |
| 520 | /* draft-ietf-quic-http34 7.2.4. SETTINGS |
| 521 | * |
| 522 | * The same setting identifier MUST NOT occur more than once in the |
| 523 | * SETTINGS frame. A receiver MAY treat the presence of duplicate |
| 524 | * setting identifiers as a connection error of type H3_SETTINGS_ERROR. |
| 525 | */ |
| 526 | |
| 527 | /* Ignore duplicate check for ID too big used for GREASE. */ |
| 528 | if (id < sizeof(mask)) { |
| 529 | if (ha_bit_test(id, &mask)) { |
| 530 | h3c->err = H3_SETTINGS_ERROR; |
| 531 | return -1; |
| 532 | } |
| 533 | ha_bit_set(id, &mask); |
| 534 | } |
| 535 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 536 | switch (id) { |
| 537 | case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY: |
| 538 | h3c->qpack_max_table_capacity = value; |
| 539 | break; |
| 540 | case H3_SETTINGS_MAX_FIELD_SECTION_SIZE: |
| 541 | h3c->max_field_section_size = value; |
| 542 | break; |
| 543 | case H3_SETTINGS_QPACK_BLOCKED_STREAMS: |
| 544 | h3c->qpack_blocked_streams = value; |
| 545 | break; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 546 | |
| 547 | case H3_SETTINGS_RESERVED_0: |
| 548 | case H3_SETTINGS_RESERVED_2: |
| 549 | case H3_SETTINGS_RESERVED_3: |
| 550 | case H3_SETTINGS_RESERVED_4: |
| 551 | case H3_SETTINGS_RESERVED_5: |
| 552 | /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters |
| 553 | * |
| 554 | * Setting identifiers which were defined in [HTTP2] where there is no |
| 555 | * corresponding HTTP/3 setting have also been reserved |
| 556 | * (Section 11.2.2). These reserved settings MUST NOT be sent, and |
| 557 | * their receipt MUST be treated as a connection error of type |
| 558 | * H3_SETTINGS_ERROR. |
| 559 | */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 560 | h3c->err = H3_SETTINGS_ERROR; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 561 | return -1; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 562 | default: |
| 563 | /* MUST be ignored */ |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 568 | TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS); |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 569 | return ret; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 572 | /* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate |
| 573 | * that we received the last data of the stream. |
Amaury Denoyelle | 0ffd6e7 | 2022-05-24 11:07:28 +0200 | [diff] [blame] | 574 | * |
| 575 | * Returns 0 on success else non-zero. |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 576 | */ |
| 577 | static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx) |
| 578 | { |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 579 | struct ncbuf *rxbuf = &qcs->rx.ncbuf; |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 580 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 581 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 582 | ssize_t ret; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 583 | |
Amaury Denoyelle | bb97042 | 2022-04-12 16:40:52 +0200 | [diff] [blame] | 584 | h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id); |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 585 | if (!ncb_data(rxbuf, 0)) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 586 | return 0; |
| 587 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 588 | if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) { |
| 589 | if (h3_init_uni_stream(h3c, qcs, rxbuf)) |
| 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) { |
| 594 | /* For non-h3 STREAM, parse it and return immediately. */ |
| 595 | if (h3_parse_uni_stream_no_h3(qcs, rxbuf)) |
| 596 | return 1; |
| 597 | return 0; |
| 598 | } |
| 599 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 600 | 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] | 601 | uint64_t ftype, flen; |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 602 | char last_stream_frame = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 603 | |
| 604 | /* Work on a copy of <rxbuf> */ |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 605 | if (!h3s->demux_frame_len) { |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 606 | size_t hlen = h3_decode_frm_header(&ftype, &flen, rxbuf); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 607 | if (!hlen) |
| 608 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 609 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 610 | h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n", |
| 611 | __func__, ftype, flen); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 612 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 613 | h3s->demux_frame_type = ftype; |
| 614 | h3s->demux_frame_len = flen; |
Amaury Denoyelle | 417c7c0 | 2022-05-31 14:18:33 +0200 | [diff] [blame] | 615 | |
| 616 | if (!h3_is_frame_valid(h3c, qcs, ftype)) { |
| 617 | qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED); |
| 618 | return 1; |
| 619 | } |
| 620 | |
Amaury Denoyelle | a977355 | 2022-05-16 14:38:25 +0200 | [diff] [blame] | 621 | qcs_consume(qcs, hlen); |
Amaury Denoyelle | 417c7c0 | 2022-05-31 14:18:33 +0200 | [diff] [blame] | 622 | if (!ncb_data(rxbuf, 0)) |
| 623 | break; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 624 | } |
Amaury Denoyelle | 0484f92 | 2022-02-15 16:59:39 +0100 | [diff] [blame] | 625 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 626 | flen = h3s->demux_frame_len; |
| 627 | ftype = h3s->demux_frame_type; |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 628 | |
| 629 | /* Do not demux incomplete frames except H3 DATA which can be |
| 630 | * fragmented in multiple HTX blocks. |
| 631 | */ |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 632 | if (flen > ncb_data(rxbuf, 0) && ftype != H3_FT_DATA) { |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 633 | /* Reject frames bigger than bufsize. |
| 634 | * |
| 635 | * TODO HEADERS should in complement be limited with H3 |
| 636 | * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent |
| 637 | * excessive decompressed size. |
| 638 | */ |
| 639 | if (flen > ncb_size(rxbuf)) { |
| 640 | qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD); |
| 641 | return 1; |
| 642 | } |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 643 | break; |
Amaury Denoyelle | b5454d4 | 2022-05-12 16:56:16 +0200 | [diff] [blame] | 644 | } |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 645 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 646 | last_stream_frame = (fin && flen == ncb_total_data(rxbuf)); |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 647 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 648 | h3_inc_frame_type_cnt(h3c->prx_counters, ftype); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 649 | switch (ftype) { |
| 650 | case H3_FT_DATA: |
Amaury Denoyelle | 31e4f6e | 2022-02-15 17:30:27 +0100 | [diff] [blame] | 651 | ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame); |
| 652 | /* TODO handle error reporting. Stream closure required. */ |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 653 | if (ret < 0) { ABORT_NOW(); } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 654 | break; |
| 655 | case H3_FT_HEADERS: |
Amaury Denoyelle | 31e4f6e | 2022-02-15 17:30:27 +0100 | [diff] [blame] | 656 | ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame); |
| 657 | /* TODO handle error reporting. Stream closure required. */ |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 658 | if (ret < 0) { ABORT_NOW(); } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 659 | break; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 660 | case H3_FT_CANCEL_PUSH: |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 661 | case H3_FT_PUSH_PROMISE: |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 662 | case H3_FT_MAX_PUSH_ID: |
| 663 | case H3_FT_GOAWAY: |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 664 | /* Not supported */ |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 665 | ret = flen; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 666 | break; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 667 | case H3_FT_SETTINGS: |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 668 | ret = h3_parse_settings_frm(qcs->qcc->ctx, rxbuf, flen); |
| 669 | if (ret < 0) { |
| 670 | qcc_emit_cc_app(qcs->qcc, h3c->err); |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 671 | return 1; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 672 | } |
| 673 | h3c->flags |= H3_CF_SETTINGS_RECV; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 674 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 675 | default: |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 676 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 677 | * |
| 678 | * Implementations MUST discard frames [...] that have unknown |
| 679 | * or unsupported types. |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 680 | */ |
| 681 | h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype); |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 682 | ret = flen; |
| 683 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 684 | } |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 685 | |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 686 | if (ret) { |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 687 | BUG_ON(h3s->demux_frame_len < ret); |
| 688 | h3s->demux_frame_len -= ret; |
Amaury Denoyelle | a977355 | 2022-05-16 14:38:25 +0200 | [diff] [blame] | 689 | qcs_consume(qcs, ret); |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 690 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Amaury Denoyelle | 03cc62c | 2022-04-27 16:53:16 +0200 | [diff] [blame] | 693 | /* TODO may be useful to wakeup the MUX if blocked due to full buffer. |
| 694 | * However, currently, io-cb of MUX does not handle Rx. |
| 695 | */ |
| 696 | |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 697 | return 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 700 | /* Returns buffer for data sending. |
| 701 | * May be NULL if the allocation failed. |
| 702 | */ |
| 703 | static struct buffer *mux_get_buf(struct qcs *qcs) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 704 | { |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 705 | if (!b_size(&qcs->tx.buf)) |
| 706 | b_alloc(&qcs->tx.buf); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 707 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 708 | return &qcs->tx.buf; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 709 | } |
| 710 | |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 711 | /* Function used to emit stream data from <qcs> control uni-stream */ |
| 712 | 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] | 713 | { |
| 714 | int ret; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 715 | struct h3c *h3c = ctx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 716 | 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] | 717 | struct buffer pos, *res; |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 718 | size_t frm_len; |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 719 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 720 | TRACE_ENTER(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs); |
| 721 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 722 | BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 723 | |
| 724 | ret = 0; |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 725 | pos = b_make((char *)data, sizeof(data), 0, 0); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 726 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 727 | frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) + |
| 728 | quic_int_getsize(h3_settings_qpack_max_table_capacity) + |
| 729 | quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) + |
| 730 | quic_int_getsize(h3_settings_qpack_blocked_streams); |
| 731 | if (h3_settings_max_field_section_size) { |
| 732 | frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) + |
| 733 | quic_int_getsize(h3_settings_max_field_section_size); |
| 734 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 735 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 736 | b_quic_enc_int(&pos, H3_UNI_S_T_CTRL); |
| 737 | /* Build a SETTINGS frame */ |
| 738 | b_quic_enc_int(&pos, H3_FT_SETTINGS); |
| 739 | b_quic_enc_int(&pos, frm_len); |
| 740 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY); |
| 741 | b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity); |
| 742 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS); |
| 743 | b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams); |
| 744 | if (h3_settings_max_field_section_size) { |
| 745 | b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE); |
| 746 | b_quic_enc_int(&pos, h3_settings_max_field_section_size); |
| 747 | } |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 748 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 749 | res = mux_get_buf(qcs); |
| 750 | if (b_room(res) < b_data(&pos)) { |
| 751 | // TODO the mux should be put in blocked state, with |
| 752 | // the stream in state waiting for settings to be sent |
| 753 | ABORT_NOW(); |
| 754 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 755 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 756 | ret = b_force_xfer(res, &pos, b_data(&pos)); |
| 757 | if (ret > 0) { |
| 758 | h3c->flags |= H3_CF_SETTINGS_SENT; |
| 759 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 760 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 763 | TRACE_LEAVE(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 764 | return ret; |
| 765 | } |
| 766 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 767 | static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx) |
| 768 | { |
| 769 | struct buffer outbuf; |
| 770 | struct buffer headers_buf = BUF_NULL; |
| 771 | struct buffer *res; |
| 772 | struct http_hdr list[global.tune.max_http_hdr]; |
| 773 | struct htx_sl *sl; |
| 774 | struct htx_blk *blk; |
| 775 | enum htx_blk_type type; |
| 776 | int frame_length_size; /* size in bytes of frame length varint field */ |
| 777 | int ret = 0; |
| 778 | int hdr; |
| 779 | int status = 0; |
| 780 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 781 | TRACE_ENTER(H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
| 782 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 783 | sl = NULL; |
| 784 | hdr = 0; |
| 785 | for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 786 | type = htx_get_blk_type(blk); |
| 787 | |
| 788 | if (type == HTX_BLK_UNUSED) |
| 789 | continue; |
| 790 | |
| 791 | if (type == HTX_BLK_EOH) |
| 792 | break; |
| 793 | |
| 794 | if (type == HTX_BLK_RES_SL) { |
| 795 | /* start-line -> HEADERS h3 frame */ |
| 796 | BUG_ON(sl); |
| 797 | sl = htx_get_blk_ptr(htx, blk); |
| 798 | /* TODO should be on h3 layer */ |
| 799 | status = sl->info.res.status; |
| 800 | } |
| 801 | else if (type == HTX_BLK_HDR) { |
| 802 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 803 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 804 | hdr++; |
| 805 | } |
| 806 | else { |
| 807 | ABORT_NOW(); |
| 808 | goto err; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | BUG_ON(!sl); |
| 813 | |
| 814 | list[hdr].n = ist(""); |
| 815 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 816 | res = mux_get_buf(qcs); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 817 | |
| 818 | /* At least 5 bytes to store frame type + length as a varint max size */ |
| 819 | if (b_room(res) < 5) |
| 820 | ABORT_NOW(); |
| 821 | |
| 822 | b_reset(&outbuf); |
| 823 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 824 | /* Start the headers after frame type + length */ |
| 825 | headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0); |
| 826 | |
| 827 | if (qpack_encode_field_section_line(&headers_buf)) |
| 828 | ABORT_NOW(); |
| 829 | if (qpack_encode_int_status(&headers_buf, status)) |
| 830 | ABORT_NOW(); |
| 831 | |
| 832 | for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) { |
| 833 | if (isteq(list[hdr].n, ist(""))) |
| 834 | break; |
| 835 | |
Amaury Denoyelle | ffafb3d | 2022-02-15 16:10:42 +0100 | [diff] [blame] | 836 | /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges |
| 837 | * Transfer codings (see Section 6.1 of [HTTP11]) are not |
| 838 | * defined for HTTP/3; the Transfer-Encoding header field MUST |
| 839 | * NOT be used. |
| 840 | */ |
| 841 | if (isteq(list[hdr].n, ist("transfer-encoding"))) |
| 842 | continue; |
| 843 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 844 | if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v)) |
| 845 | ABORT_NOW(); |
| 846 | } |
| 847 | |
| 848 | /* Now that all headers are encoded, we are certain that res buffer is |
| 849 | * big enough |
| 850 | */ |
| 851 | frame_length_size = quic_int_getsize(b_data(&headers_buf)); |
| 852 | res->head += 4 - frame_length_size; |
| 853 | b_putchr(res, 0x01); /* h3 HEADERS frame type */ |
| 854 | if (!b_quic_enc_int(res, b_data(&headers_buf))) |
| 855 | ABORT_NOW(); |
| 856 | b_add(res, b_data(&headers_buf)); |
| 857 | |
| 858 | ret = 0; |
| 859 | blk = htx_get_head_blk(htx); |
| 860 | while (blk) { |
| 861 | type = htx_get_blk_type(blk); |
| 862 | ret += htx_get_blksz(blk); |
| 863 | blk = htx_remove_blk(htx, blk); |
| 864 | if (type == HTX_BLK_EOH) |
| 865 | break; |
| 866 | } |
| 867 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 868 | TRACE_LEAVE(H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 869 | return ret; |
| 870 | |
| 871 | err: |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 872 | TRACE_DEVEL("leaving on error", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 873 | return 0; |
| 874 | } |
| 875 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 876 | /* Returns the total of bytes sent. */ |
| 877 | static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count) |
| 878 | { |
| 879 | struct buffer outbuf; |
| 880 | struct buffer *res; |
| 881 | size_t total = 0; |
| 882 | struct htx *htx; |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 883 | int bsize, fsize, hsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 884 | struct htx_blk *blk; |
| 885 | enum htx_blk_type type; |
| 886 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 887 | TRACE_ENTER(H3_EV_TX_DATA, qcs->qcc->conn, qcs); |
| 888 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 889 | htx = htx_from_buf(buf); |
| 890 | |
| 891 | new_frame: |
| 892 | if (!count || htx_is_empty(htx)) |
| 893 | goto end; |
| 894 | |
| 895 | blk = htx_get_head_blk(htx); |
| 896 | type = htx_get_blk_type(blk); |
| 897 | fsize = bsize = htx_get_blksz(blk); |
| 898 | |
| 899 | if (type != HTX_BLK_DATA) |
| 900 | goto end; |
| 901 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 902 | res = mux_get_buf(qcs); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 903 | |
| 904 | if (fsize > count) |
| 905 | fsize = count; |
| 906 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 907 | /* h3 DATA headers : 1-byte frame type + varint frame length */ |
| 908 | hsize = 1 + QUIC_VARINT_MAX_SIZE; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 909 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 910 | while (1) { |
| 911 | b_reset(&outbuf); |
| 912 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 913 | if (b_size(&outbuf) > hsize || !b_space_wraps(res)) |
| 914 | break; |
| 915 | b_slow_realign(res, trash.area, b_data(res)); |
| 916 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 917 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 918 | /* Not enough room for headers and at least one data byte, block the |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 919 | * stream. It is expected that the stream connector layer will subscribe |
| 920 | * on SEND. |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 921 | */ |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 922 | if (b_size(&outbuf) <= hsize) { |
| 923 | qcs->flags |= QC_SF_BLK_MROOM; |
| 924 | goto end; |
| 925 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 926 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 927 | if (b_size(&outbuf) < hsize + fsize) |
| 928 | fsize = b_size(&outbuf) - hsize; |
| 929 | BUG_ON(fsize <= 0); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 930 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 931 | b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */ |
| 932 | b_quic_enc_int(&outbuf, fsize); /* h3 frame length */ |
| 933 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 934 | b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize); |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 935 | total += fsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 936 | count -= fsize; |
| 937 | |
| 938 | if (fsize == bsize) |
| 939 | htx_remove_blk(htx, blk); |
| 940 | else |
| 941 | htx_cut_data_blk(htx, blk, fsize); |
| 942 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 943 | /* commit the buffer */ |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 944 | b_add(res, b_data(&outbuf)); |
| 945 | goto new_frame; |
| 946 | |
| 947 | end: |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 948 | TRACE_LEAVE(H3_EV_TX_DATA, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 949 | return total; |
| 950 | } |
| 951 | |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 952 | size_t h3_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags) |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 953 | { |
| 954 | size_t total = 0; |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 955 | struct qcs *qcs = __sc_mux_strm(sc); |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 956 | struct htx *htx; |
| 957 | enum htx_blk_type btype; |
| 958 | struct htx_blk *blk; |
| 959 | uint32_t bsize; |
| 960 | int32_t idx; |
| 961 | int ret; |
| 962 | |
Amaury Denoyelle | d8769d1 | 2022-03-25 15:28:33 +0100 | [diff] [blame] | 963 | h3_debug_printf(stderr, "%s\n", __func__); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 964 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 965 | htx = htx_from_buf(buf); |
| 966 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 967 | while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) { |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 968 | idx = htx_get_head(htx); |
| 969 | blk = htx_get_blk(htx, idx); |
| 970 | btype = htx_get_blk_type(blk); |
| 971 | bsize = htx_get_blksz(blk); |
| 972 | |
| 973 | /* Not implemented : QUIC on backend side */ |
| 974 | BUG_ON(btype == HTX_BLK_REQ_SL); |
| 975 | |
| 976 | switch (btype) { |
| 977 | case HTX_BLK_RES_SL: |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 978 | /* start-line -> HEADERS h3 frame */ |
| 979 | ret = h3_resp_headers_send(qcs, htx); |
| 980 | if (ret > 0) { |
| 981 | total += ret; |
| 982 | count -= ret; |
| 983 | if (ret < bsize) |
| 984 | goto out; |
| 985 | } |
| 986 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 987 | |
| 988 | case HTX_BLK_DATA: |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 989 | ret = h3_resp_data_send(qcs, buf, count); |
| 990 | if (ret > 0) { |
| 991 | htx = htx_from_buf(buf); |
| 992 | total += ret; |
| 993 | count -= ret; |
| 994 | if (ret < bsize) |
| 995 | goto out; |
| 996 | } |
| 997 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 998 | |
| 999 | case HTX_BLK_TLR: |
| 1000 | case HTX_BLK_EOT: |
| 1001 | /* TODO trailers */ |
| 1002 | |
| 1003 | default: |
| 1004 | htx_remove_blk(htx, blk); |
| 1005 | total += bsize; |
| 1006 | count -= bsize; |
| 1007 | break; |
| 1008 | } |
| 1009 | } |
| 1010 | |
Amaury Denoyelle | c2025c1 | 2021-12-03 15:03:36 +0100 | [diff] [blame] | 1011 | if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) |
| 1012 | qcs->flags |= QC_SF_FIN_STREAM; |
| 1013 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1014 | out: |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1015 | if (total) { |
| 1016 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 1017 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 1018 | } |
| 1019 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1020 | return total; |
Amaury Denoyelle | f52151d | 2021-08-24 16:11:18 +0200 | [diff] [blame] | 1021 | } |
| 1022 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1023 | static int h3_attach(struct qcs *qcs) |
| 1024 | { |
| 1025 | struct h3s *h3s; |
| 1026 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 1027 | TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
| 1028 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1029 | h3s = pool_alloc(pool_head_h3s); |
| 1030 | if (!h3s) |
| 1031 | return 1; |
| 1032 | |
| 1033 | qcs->ctx = h3s; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1034 | h3s->demux_frame_len = 0; |
| 1035 | h3s->demux_frame_type = 0; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 1036 | h3s->flags = 0; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1037 | |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 1038 | if (quic_stream_is_bidi(qcs->id)) { |
| 1039 | h3s->type = H3S_T_REQ; |
| 1040 | } |
| 1041 | else { |
| 1042 | /* stream type must be decoded for unidirectional streams */ |
| 1043 | h3s->type = H3S_T_UNKNOWN; |
| 1044 | } |
| 1045 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 1046 | TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1047 | return 0; |
| 1048 | } |
| 1049 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1050 | static void h3_detach(struct qcs *qcs) |
| 1051 | { |
| 1052 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 1053 | |
| 1054 | TRACE_ENTER(H3_EV_H3S_END, qcs->qcc->conn, qcs); |
| 1055 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1056 | pool_free(pool_head_h3s, h3s); |
| 1057 | qcs->ctx = NULL; |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 1058 | |
| 1059 | TRACE_LEAVE(H3_EV_H3S_END, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1060 | } |
| 1061 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1062 | static int h3_finalize(void *ctx) |
| 1063 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1064 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 1065 | struct qcs *qcs; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1066 | |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 1067 | qcs = qcs_new(h3c->qcc, 0x3, QCS_SRV_UNI); |
| 1068 | if (!qcs) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1069 | return 0; |
| 1070 | |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 1071 | h3_control_send(qcs, h3c); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1072 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1073 | return 1; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1076 | /* Initialize the HTTP/3 context for <qcc> mux. |
| 1077 | * Return 1 if succeeded, 0 if not. |
| 1078 | */ |
| 1079 | static int h3_init(struct qcc *qcc) |
| 1080 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1081 | struct h3c *h3c; |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 1082 | struct quic_conn *qc = qcc->conn->handle.qc; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1083 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1084 | h3c = pool_alloc(pool_head_h3c); |
| 1085 | if (!h3c) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1086 | goto fail_no_h3; |
| 1087 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1088 | h3c->qcc = qcc; |
| 1089 | h3c->err = H3_NO_ERROR; |
| 1090 | h3c->flags = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1091 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1092 | qcc->ctx = h3c; |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 1093 | h3c->prx_counters = |
| 1094 | EXTRA_COUNTERS_GET(qc->li->bind_conf->frontend->extra_counters_fe, |
| 1095 | &h3_stats_module); |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1096 | LIST_INIT(&h3c->buf_wait.list); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1097 | |
| 1098 | return 1; |
| 1099 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1100 | fail_no_h3: |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1104 | static void h3_release(void *ctx) |
| 1105 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1106 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1107 | pool_free(pool_head_h3c, h3c); |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
Amaury Denoyelle | 198d35f | 2022-04-01 17:56:58 +0200 | [diff] [blame] | 1110 | /* Check if the H3 connection can still be considered as active. |
| 1111 | * |
| 1112 | * Return true if active else false. |
| 1113 | */ |
| 1114 | static int h3_is_active(const struct qcc *qcc, void *ctx) |
| 1115 | { |
| 1116 | if (qcc->strms[QCS_CLT_BIDI].nb_streams) |
| 1117 | return 1; |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 1122 | /* Increment the h3 error code counters for <error_code> value */ |
| 1123 | static void h3_stats_inc_err_cnt(void *ctx, int err_code) |
| 1124 | { |
| 1125 | struct h3c *h3c = ctx; |
| 1126 | |
| 1127 | h3_inc_err_cnt(h3c->prx_counters, err_code); |
| 1128 | } |
| 1129 | |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 1130 | /* h3 trace handler */ |
| 1131 | static void h3_trace(enum trace_level level, uint64_t mask, |
| 1132 | const struct trace_source *src, |
| 1133 | const struct ist where, const struct ist func, |
| 1134 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 1135 | { |
| 1136 | const struct connection *conn = a1; |
| 1137 | const struct qcc *qcc = conn ? conn->ctx : NULL; |
| 1138 | const struct qcs *qcs = a2; |
| 1139 | |
| 1140 | if (src->verbosity > H3_VERB_CLEAN) { |
| 1141 | chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc); |
| 1142 | |
| 1143 | if (qcs) |
| 1144 | chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id); |
| 1145 | } |
| 1146 | } |
| 1147 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1148 | /* HTTP/3 application layer operations */ |
| 1149 | const struct qcc_app_ops h3_ops = { |
| 1150 | .init = h3_init, |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1151 | .attach = h3_attach, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1152 | .decode_qcs = h3_decode_qcs, |
Amaury Denoyelle | abbe91e | 2021-11-12 16:09:29 +0100 | [diff] [blame] | 1153 | .snd_buf = h3_snd_buf, |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1154 | .detach = h3_detach, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1155 | .finalize = h3_finalize, |
Amaury Denoyelle | 198d35f | 2022-04-01 17:56:58 +0200 | [diff] [blame] | 1156 | .is_active = h3_is_active, |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1157 | .release = h3_release, |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 1158 | .inc_err_cnt = h3_stats_inc_err_cnt, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1159 | }; |