Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QUIC mux-demux for connections |
| 3 | * |
| 4 | * Copyright 2021 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <import/eb32tree.h> |
| 14 | #include <haproxy/api.h> |
| 15 | #include <haproxy/cfgparse.h> |
| 16 | #include <haproxy/connection.h> |
| 17 | #include <haproxy/h3.h> |
| 18 | #include <haproxy/istbuf.h> |
| 19 | #include <haproxy/log.h> |
| 20 | #include <haproxy/mux_quic.h> |
| 21 | #include <haproxy/net_helper.h> |
| 22 | #include <haproxy/quic_frame.h> |
| 23 | #include <haproxy/session-t.h> |
| 24 | #include <haproxy/stats.h> |
| 25 | #include <haproxy/stream.h> |
| 26 | #include <haproxy/stream_interface.h> |
| 27 | #include <haproxy/trace.h> |
| 28 | |
| 29 | /* dummy streams returned for closed, error, refused, idle and states */ |
| 30 | static const struct qcs *qc_closed_stream; |
| 31 | |
| 32 | /* Connection flags (32 bit), in qcc->flags */ |
| 33 | #define QC_CF_NONE 0x00000000 |
| 34 | |
| 35 | /* Flags indicating why writing to the mux is blocked. */ |
| 36 | #define QC_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 37 | #define QC_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 38 | #define QC_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 39 | |
| 40 | /* Flags indicating why writing to the demux is blocked. |
| 41 | * The first two ones directly affect the ability for the mux to receive data |
| 42 | * from the connection. The other ones affect the mux's ability to demux |
| 43 | * received data. |
| 44 | */ |
| 45 | #define QC_CF_DEM_DFULL 0x00000004 // demux blocked on connection's demux buffer full |
| 46 | |
| 47 | #define QC_CF_DEM_MBUSY 0x00000008 // demux blocked on connection's mux side busy |
| 48 | #define QC_CF_DEM_MROOM 0x00000010 // demux blocked on lack of room in mux buffer |
| 49 | #define QC_CF_DEM_SALLOC 0x00000020 // demux blocked on lack of stream's request buffer |
| 50 | #define QC_CF_DEM_SFULL 0x00000040 // demux blocked on stream request buffer full |
| 51 | #define QC_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave |
| 52 | #define QC_CF_DEM_BLOCK_ANY 0x00000170 // aggregate of the demux flags above except DFULL |
| 53 | |
| 54 | /* other flags */ |
| 55 | #define QC_CF_IS_BACK 0x00008000 // this is an outgoing connection |
| 56 | |
| 57 | #define QC_SS_MASK(state) (1UL << (state)) |
| 58 | #define QC_SS_IDLE_BIT (1UL << QC_SS_IDLE) |
| 59 | #define QC_SS_RLOC_BIT (1UL << QC_SS_RLOC) |
| 60 | #define QC_SS_RREM_BIT (1UL << QC_SS_RREM) |
| 61 | #define QC_SS_OPEN_BIT (1UL << QC_SS_OPEN) |
| 62 | #define QC_SS_HREM_BIT (1UL << QC_SS_HREM) |
| 63 | #define QC_SS_HLOC_BIT (1UL << QC_SS_HLOC) |
| 64 | #define QC_SS_ERROR_BIT (1UL << QC_SS_ERROR) |
| 65 | #define QC_SS_CLOSED_BIT (1UL << QC_SS_CLOSED) |
| 66 | |
| 67 | |
| 68 | /* trace source and events */ |
| 69 | static void qc_trace(enum trace_level level, uint64_t mask, \ |
| 70 | const struct trace_source *src, |
| 71 | const struct ist where, const struct ist func, |
| 72 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 73 | |
| 74 | /* The event representation is split like this : |
| 75 | * strm - application layer |
| 76 | * qcs - internal QUIC stream |
| 77 | * qcc - internal QUIC connection |
| 78 | * conn - external connection |
| 79 | * |
| 80 | */ |
| 81 | static const struct trace_event qc_trace_events[] = { |
| 82 | #define QC_EV_QCC_NEW (1ULL << 0) |
| 83 | { .mask = QC_EV_QCC_NEW, .name = "qcc_new", .desc = "new QUIC connection" }, |
| 84 | #define QC_EV_QCC_RECV (1ULL << 1) |
| 85 | { .mask = QC_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" }, |
| 86 | #define QC_EV_QCC_SEND (1ULL << 2) |
| 87 | { .mask = QC_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" }, |
| 88 | #define QC_EV_QCC_FCTL (1ULL << 3) |
| 89 | { .mask = QC_EV_QCC_FCTL, .name = "qcc_fctl", .desc = "QUIC connection flow-controlled" }, |
| 90 | #define QC_EV_QCC_BLK (1ULL << 4) |
| 91 | { .mask = QC_EV_QCC_BLK, .name = "qcc_blk", .desc = "QUIC connection blocked" }, |
| 92 | #define QC_EV_QCC_WAKE (1ULL << 5) |
| 93 | { .mask = QC_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" }, |
| 94 | #define QC_EV_QCC_END (1ULL << 6) |
| 95 | { .mask = QC_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" }, |
| 96 | #define QC_EV_QCC_ERR (1ULL << 7) |
| 97 | { .mask = QC_EV_QCC_ERR, .name = "qcc_err", .desc = "error on QUIC connection" }, |
| 98 | #define QC_EV_TX_FRAME (1ULL << 8) |
| 99 | { .mask = QC_EV_TX_FRAME, .name = "tx_frame", .desc = "transmission of any QUIC frame" }, |
| 100 | #define QC_EV_QCS_NEW (1ULL << 9) |
| 101 | { .mask = QC_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" }, |
| 102 | #define QC_EV_QCS_GET (1ULL << 10) |
| 103 | { .mask = QC_EV_QCS_GET, .name = "qcs_get", .desc = "get QUIC stream by ID" }, |
| 104 | #define QC_EV_QCS_SEND (1ULL << 11) |
| 105 | { .mask = QC_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx for QUIC stream" }, |
| 106 | #define QC_EV_QCS_FCTL (1ULL << 12) |
| 107 | { .mask = QC_EV_QCS_FCTL, .name = "qcs_fctl", .desc = "QUIC stream flow-controlled" }, |
| 108 | #define QC_EV_QCS_BLK (1ULL << 13) |
| 109 | { .mask = QC_EV_QCS_BLK, .name = "qcs_blk", .desc = "QUIC stream blocked" }, |
| 110 | #define QC_EV_QCS_WAKE (1ULL << 14) |
| 111 | { .mask = QC_EV_QCS_WAKE, .name = "qcs_wake", .desc = "QUIC stream woken up" }, |
| 112 | #define QC_EV_QCS_END (1ULL << 15) |
| 113 | { .mask = QC_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" }, |
| 114 | #define QC_EV_QCS_ERR (1ULL << 16) |
| 115 | { .mask = QC_EV_QCS_ERR, .name = "qcs_err", .desc = "error on QUIC stream" }, |
| 116 | #define QC_EV_STRM_NEW (1ULL << 17) |
| 117 | { .mask = QC_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" }, |
| 118 | #define QC_EV_STRM_RECV (1ULL << 18) |
| 119 | { .mask = QC_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" }, |
| 120 | #define QC_EV_STRM_SEND (1ULL << 19) |
| 121 | { .mask = QC_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" }, |
| 122 | #define QC_EV_STRM_FULL (1ULL << 20) |
| 123 | { .mask = QC_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" }, |
| 124 | #define QC_EV_STRM_WAKE (1ULL << 21) |
| 125 | { .mask = QC_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" }, |
| 126 | #define QC_EV_STRM_SHUT (1ULL << 22) |
| 127 | { .mask = QC_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" }, |
| 128 | #define QC_EV_STRM_END (1ULL << 23) |
| 129 | { .mask = QC_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" }, |
| 130 | #define QC_EV_STRM_ERR (1ULL << 24) |
| 131 | { .mask = QC_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" }, |
| 132 | { } |
| 133 | }; |
| 134 | |
| 135 | static const struct name_desc qc_trace_lockon_args[4] = { |
| 136 | /* arg1 */ { /* already used by the connection */ }, |
| 137 | /* arg2 */ { .name = "qcs", .desc = "QUIC stream" }, |
| 138 | /* arg3 */ { }, |
| 139 | /* arg4 */ { } |
| 140 | }; |
| 141 | |
| 142 | static const struct name_desc qc_trace_decoding[] = { |
| 143 | #define QC_VERB_CLEAN 1 |
| 144 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 145 | #define QC_VERB_MINIMAL 2 |
| 146 | { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" }, |
| 147 | #define QC_VERB_SIMPLE 3 |
| 148 | { .name="simple", .desc="add request/response status line or frame info when available" }, |
| 149 | #define QC_VERB_ADVANCED 4 |
| 150 | { .name="advanced", .desc="add header fields or frame decoding when available" }, |
| 151 | #define QC_VERB_COMPLETE 5 |
| 152 | { .name="complete", .desc="add full data dump when available" }, |
| 153 | { /* end */ } |
| 154 | }; |
| 155 | |
| 156 | static struct trace_source trace_mux_quic = { |
| 157 | .name = IST("mux_quic"), |
| 158 | .desc = "QUIC multiplexer", |
| 159 | .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection |
| 160 | .default_cb = qc_trace, |
| 161 | .known_events = qc_trace_events, |
| 162 | .lockon_args = qc_trace_lockon_args, |
| 163 | .decoding = qc_trace_decoding, |
| 164 | .report_events = ~0, // report everything by default |
| 165 | }; |
| 166 | |
| 167 | #define TRACE_SOURCE &trace_mux_quic |
| 168 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 169 | |
| 170 | /* quic stats module */ |
| 171 | enum { |
| 172 | QC_ST_RESET_STREAM_RCVD, |
| 173 | |
| 174 | QC_ST_CONN_PROTO_ERR, |
| 175 | QC_ST_STRM_PROTO_ERR, |
| 176 | QC_ST_RESET_STREAM_SENT, |
| 177 | |
| 178 | QC_ST_OPEN_CONN, |
| 179 | QC_ST_OPEN_STREAM, |
| 180 | QC_ST_TOTAL_CONN, |
| 181 | QC_ST_TOTAL_STREAM, |
| 182 | |
| 183 | QC_STATS_COUNT /* must be the last member of the enum */ |
| 184 | }; |
| 185 | |
| 186 | static struct name_desc qc_stats[] = { |
| 187 | [QC_ST_RESET_STREAM_RCVD] = { .name = "qc_rst_stream_rcvd", |
| 188 | .desc = "Total number of received RESET_STREAM frames" }, |
| 189 | |
| 190 | [QC_ST_CONN_PROTO_ERR] = { .name = "qc_detected_conn_protocol_errors", |
| 191 | .desc = "Total number of connection protocol errors" }, |
| 192 | [QC_ST_STRM_PROTO_ERR] = { .name = "qc_detected_strm_protocol_errors", |
| 193 | .desc = "Total number of stream protocol errors" }, |
| 194 | [QC_ST_RESET_STREAM_SENT] = { .name = "qc_rst_stream_resp", |
| 195 | .desc = "Total number of RESET_STREAM sent on detected error" }, |
| 196 | |
| 197 | [QC_ST_OPEN_CONN] = { .name = "qc_open_connections", |
| 198 | .desc = "Count of currently open connections" }, |
| 199 | [QC_ST_OPEN_STREAM] = { .name = "qc_backend_open_streams", |
| 200 | .desc = "Count of currently open streams" }, |
| 201 | [QC_ST_TOTAL_CONN] = { .name = "qc_open_connections", |
| 202 | .desc = "Total number of connections" }, |
| 203 | [QC_ST_TOTAL_STREAM] = { .name = "qc_backend_open_streams", |
| 204 | .desc = "Total number of streams" }, |
| 205 | }; |
| 206 | |
| 207 | static struct qc_counters { |
| 208 | long long rst_stream_rcvd; /* total number of RESET_STREAM frame received */ |
| 209 | |
| 210 | long long conn_proto_err; /* total number of protocol errors detected */ |
| 211 | long long strm_proto_err; /* total number of protocol errors detected */ |
| 212 | long long rst_stream_resp; /* total number of RESET_STREAM frame sent on error */ |
| 213 | |
| 214 | long long open_conns; /* count of currently open connections */ |
| 215 | long long open_streams; /* count of currently open streams */ |
| 216 | long long total_conns; /* total number of connections */ |
| 217 | long long total_streams; /* total number of streams */ |
| 218 | } qc_counters; |
| 219 | |
| 220 | static void qc_fill_stats(void *data, struct field *stats) |
| 221 | { |
| 222 | struct qc_counters *counters = data; |
| 223 | |
| 224 | stats[QC_ST_RESET_STREAM_RCVD] = mkf_u64(FN_COUNTER, counters->rst_stream_rcvd); |
| 225 | |
| 226 | stats[QC_ST_CONN_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->conn_proto_err); |
| 227 | stats[QC_ST_STRM_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->strm_proto_err); |
| 228 | stats[QC_ST_RESET_STREAM_SENT] = mkf_u64(FN_COUNTER, counters->rst_stream_resp); |
| 229 | |
| 230 | stats[QC_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns); |
| 231 | stats[QC_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams); |
| 232 | stats[QC_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns); |
| 233 | stats[QC_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams); |
| 234 | } |
| 235 | |
| 236 | static struct stats_module qc_stats_module = { |
| 237 | .name = "quic", |
| 238 | .fill_stats = qc_fill_stats, |
| 239 | .stats = qc_stats, |
| 240 | .stats_count = QC_STATS_COUNT, |
| 241 | .counters = &qc_counters, |
| 242 | .counters_size = sizeof(qc_counters), |
| 243 | .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE), |
| 244 | .clearable = 1, |
| 245 | }; |
| 246 | |
| 247 | INITCALL1(STG_REGISTER, stats_register_module, &qc_stats_module); |
| 248 | |
| 249 | /* the qcc connection pool */ |
| 250 | DECLARE_STATIC_POOL(pool_head_qcc, "qcc", sizeof(struct qcc)); |
| 251 | /* the qcs stream pool */ |
| 252 | DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs)); |
| 253 | |
| 254 | static struct task *qc_timeout_task(struct task *t, void *context, unsigned int state); |
| 255 | static int qc_send(struct qcc *qcc); |
| 256 | static int qc_recv(struct qcc *qcc); |
| 257 | static int qc_process(struct qcc *qcc); |
| 258 | static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int state); |
| 259 | static inline struct qcs *qcc_st_by_id(struct qcc *qcc, int id); |
| 260 | static struct task *qc_deferred_shut(struct task *t, void *ctx, unsigned int state); |
| 261 | static struct qcs *qcc_bck_stream_new(struct qcc *qcc, int dir, |
| 262 | struct conn_stream *cs, struct session *sess); |
| 263 | static void qcs_alert(struct qcs *qcs); |
| 264 | |
| 265 | /* returns a qcc state as an abbreviated 3-letter string, or "???" if unknown */ |
| 266 | static inline const char *qcc_st_to_str(enum qc_cs st) |
| 267 | { |
| 268 | switch (st) { |
| 269 | case QC_CS_NOERR: return "NER"; |
| 270 | default: return "???"; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /* marks an error on the connection */ |
| 275 | void qc_error(struct qcc *qcc, int err) |
| 276 | { |
| 277 | TRACE_POINT(QC_EV_QCC_ERR, qcc->conn, 0, 0, (void *)(long)(err)); |
| 278 | qcc->errcode = err; |
| 279 | qcc->st0 = QC_CS_ERROR; |
| 280 | } |
| 281 | |
| 282 | static inline const char *qcs_rx_st_to_str(enum qcs_rx_st st) |
| 283 | { |
| 284 | switch (st) { |
| 285 | case QC_RX_SS_IDLE: return "IDL"; |
| 286 | case QC_RX_SS_RECV: return "RCV"; |
| 287 | case QC_RX_SS_SIZE_KNOWN: return "SKNWN"; |
| 288 | case QC_RX_SS_DATA_RECVD: return "DATARCVD"; |
| 289 | case QC_RX_SS_DATA_READ : return "DATAREAD"; |
| 290 | case QC_RX_SS_RST_RECVD: return "RSTRCVD"; |
| 291 | case QC_RX_SS_RST_READ: return "RSTREAD"; |
| 292 | default: return "???"; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | static inline const char *qcs_tx_st_to_str(enum qcs_tx_st st) |
| 297 | { |
| 298 | switch (st) { |
| 299 | case QC_TX_SS_IDLE: return "IDL"; |
| 300 | case QC_TX_SS_READY: return "READY"; |
| 301 | case QC_TX_SS_SEND: return "SEND"; |
| 302 | case QC_TX_SS_DATA_SENT: return "DATASENT"; |
| 303 | case QC_TX_SS_DATA_RECVD: return "DATARCVD"; |
| 304 | case QC_TX_SS_RST_SENT: return "RSTSENT"; |
| 305 | case QC_TX_SS_RST_RECVD: return "RSTRCVD"; |
| 306 | default: return "???"; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /* the QUIC traces always expect that arg1, if non-null, is of type connection |
| 311 | * (from which we can derive qcc), that arg2, if non-null, is of type qcs. |
| 312 | */ |
| 313 | static void qc_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 314 | const struct ist where, const struct ist func, |
| 315 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 316 | { |
| 317 | const struct connection *conn = a1; |
| 318 | const struct qcc *qcc = conn ? conn->ctx : NULL; |
| 319 | const struct qcs *qcs = a2; |
| 320 | |
| 321 | if (!qcc) |
| 322 | return; |
| 323 | |
| 324 | if (src->verbosity > QC_VERB_CLEAN) { |
| 325 | chunk_appendf(&trace_buf, " : qcc=%p(%c,%s)", |
| 326 | qcc, conn_is_back(conn) ? 'B' : 'F', qcc_st_to_str(qcc->st0)); |
| 327 | if (qcs) { |
| 328 | chunk_appendf(&trace_buf, " qcs=%p(rx.%s,tx.%s)", |
| 329 | qcs, qcs_rx_st_to_str(qcs->rx.st), qcs_tx_st_to_str(qcs->tx.st)); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /* Detect a pending read0 for a QUIC connection. It happens if a read0 is pending |
| 336 | * on the connection AND if there is no more data in the demux buffer. The |
| 337 | * function returns 1 to report a read0 or 0 otherwise. |
| 338 | */ |
| 339 | __maybe_unused |
| 340 | static int qcc_read0_pending(struct qcc *qcc) |
| 341 | { |
| 342 | if (conn_xprt_read0_pending(qcc->conn) && !qcc->rx.inmux) |
| 343 | return 1; |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* returns true if the connection is allowed to expire, false otherwise. A |
| 348 | * connection may expire when: |
| 349 | * - it has no stream |
| 350 | * - it has data in the mux buffer |
| 351 | * - it has streams in the blocked list |
| 352 | * - it has streams in the fctl list |
| 353 | * - it has streams in the send list |
| 354 | * Otherwise it means some streams are waiting in the data layer and it should |
| 355 | * not expire. |
| 356 | */ |
| 357 | __maybe_unused |
| 358 | static inline int qcc_may_expire(const struct qcc *qcc) |
| 359 | { |
| 360 | return eb_is_empty(&qcc->streams_by_id) || |
| 361 | br_data(qcc->mbuf) || |
| 362 | !LIST_ISEMPTY(&qcc->blocked_list) || |
| 363 | !LIST_ISEMPTY(&qcc->fctl_list) || |
| 364 | !LIST_ISEMPTY(&qcc->send_list); |
| 365 | } |
| 366 | |
| 367 | static __inline int |
| 368 | qcc_is_dead(const struct qcc *qcc) |
| 369 | { |
| 370 | if (eb_is_empty(&qcc->streams_by_id) && /* don't close if streams exist */ |
| 371 | ((qcc->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 372 | (qcc->st0 >= QC_CS_ERROR && !qcc->task) || /* a timeout stroke earlier */ |
| 373 | (!(qcc->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */ |
| 374 | (!br_data(qcc->mbuf) && /* mux buffer empty, also process clean events below */ |
| 375 | conn_xprt_read0_pending(qcc->conn)))) |
| 376 | return 1; |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /*****************************************************/ |
| 382 | /* functions below are for dynamic buffer management */ |
| 383 | /*****************************************************/ |
| 384 | |
| 385 | /* indicates whether or not the we may call the qc_recv() function to attempt |
| 386 | * to receive data into the buffer and/or demux pending data. The condition is |
| 387 | * a bit complex due to some API limits for now. The rules are the following : |
| 388 | * - if an error or a shutdown was detected on the connection and the buffer |
| 389 | * is empty, we must not attempt to receive |
| 390 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 391 | * we know there is nothing pending |
| 392 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 393 | * regardless of whether the demux buffer is full or not, so that only |
| 394 | * de demux part decides whether or not to block. This is needed because |
| 395 | * the connection API indeed prevents us from re-enabling receipt that is |
| 396 | * already enabled in a polled state, so we must always immediately stop |
| 397 | * as soon as the demux can't proceed so as never to hit an end of read |
| 398 | * with data pending in the buffers. |
| 399 | * - otherwise must may not attempt |
| 400 | */ |
| 401 | static inline int qc_recv_allowed(const struct qcc *qcc) |
| 402 | { |
| 403 | if (qcc->rx.inmux == 0 && |
| 404 | (qcc->st0 >= QC_CS_ERROR || |
| 405 | qcc->conn->flags & CO_FL_ERROR || |
| 406 | conn_xprt_read0_pending(qcc->conn))) |
| 407 | return 0; |
| 408 | |
| 409 | if (!(qcc->flags & QC_CF_DEM_BLOCK_ANY)) |
| 410 | return 1; |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* restarts reading on the connection if it was not enabled */ |
| 416 | static inline void qcc_restart_reading(const struct qcc *qcc, int consider_buffer) |
| 417 | { |
| 418 | if (!qc_recv_allowed(qcc)) |
| 419 | return; |
| 420 | |
| 421 | if ((!consider_buffer || !qcc->rx.inmux) |
| 422 | && (qcc->wait_event.events & SUB_RETRY_RECV)) |
| 423 | return; |
| 424 | |
| 425 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 426 | } |
| 427 | |
| 428 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The qcc |
| 429 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 430 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 431 | * impossible to wake up and we prefer to be woken up later. |
| 432 | */ |
| 433 | static int qc_buf_available(void *target) |
| 434 | { |
| 435 | struct qcc *qcc = target; |
| 436 | |
| 437 | if ((qcc->flags & QC_CF_MUX_MALLOC) && b_alloc(br_tail(qcc->mbuf))) { |
| 438 | qcc->flags &= ~QC_CF_MUX_MALLOC; |
| 439 | |
| 440 | if (qcc->flags & QC_CF_DEM_MROOM) { |
| 441 | qcc->flags &= ~QC_CF_DEM_MROOM; |
| 442 | qcc_restart_reading(qcc, 1); |
| 443 | } |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | #if 0 |
| 448 | if ((qcc->flags & QC_CF_DEM_SALLOC) && |
| 449 | (qcs = qcc_st_by_id(qcc, qcc->dsi)) && qcs->cs && |
| 450 | b_alloc_margin(&qcs->rxbuf, 0)) { |
| 451 | qcc->flags &= ~QC_CF_DEM_SALLOC; |
| 452 | qcc_restart_reading(qcc, 1); |
| 453 | return 1; |
| 454 | } |
| 455 | #endif |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | struct buffer *qc_get_buf(struct qcc *qcc, struct buffer *bptr) |
| 461 | { |
| 462 | struct buffer *buf = NULL; |
| 463 | |
| 464 | if (likely(!LIST_INLIST(&qcc->buf_wait.list)) && |
| 465 | unlikely((buf = b_alloc(bptr)) == NULL)) { |
| 466 | qcc->buf_wait.target = qcc; |
| 467 | qcc->buf_wait.wakeup_cb = qc_buf_available; |
| 468 | LIST_APPEND(&ti->buffer_wq, &qcc->buf_wait.list); |
| 469 | } |
| 470 | |
| 471 | return buf; |
| 472 | } |
| 473 | |
| 474 | __maybe_unused |
| 475 | static inline void qc_release_buf(struct qcc *qcc, struct buffer *bptr) |
| 476 | { |
| 477 | if (bptr->size) { |
| 478 | b_free(bptr); |
| 479 | offer_buffers(NULL, 1); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static inline void qc_release_mbuf(struct qcc *qcc) |
| 484 | { |
| 485 | struct buffer *buf; |
| 486 | unsigned int count = 0; |
| 487 | |
| 488 | while (b_size(buf = br_head_pick(qcc->mbuf))) { |
| 489 | b_free(buf); |
| 490 | count++; |
| 491 | } |
| 492 | if (count) |
| 493 | offer_buffers(NULL, count); |
| 494 | } |
| 495 | |
| 496 | /* returns the number of streams in use on a connection to figure if it's |
| 497 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 498 | * to know if it was the last one after a detach(). |
| 499 | */ |
| 500 | static int qc_used_streams(struct connection *conn) |
| 501 | { |
| 502 | struct qcc *qcc = conn->ctx; |
| 503 | |
| 504 | return qcc->nb_cs; |
| 505 | } |
| 506 | |
| 507 | /* returns the number of concurrent streams available on the connection with <dir> |
| 508 | * as direction |
| 509 | */ |
| 510 | static int qc_avail_streams(struct connection *conn, enum qcs_dir dir) |
| 511 | { |
| 512 | struct qcc *qcc = conn->ctx; |
| 513 | enum qcs_type qcs_type; |
| 514 | |
| 515 | if (qcc->st0 >= QC_CS_ERROR) |
| 516 | return 0; |
| 517 | |
| 518 | qcs_type = qcs_type_from_dir(qcc, dir); |
| 519 | |
| 520 | return qcc->strms[qcs_type].max_streams - qcc->strms[qcs_type].nb_streams; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /* returns the number of concurrent bidirectional streams available on the |
| 525 | * connection. |
| 526 | */ |
| 527 | static int qc_avail_streams_bidi(struct connection *conn) |
| 528 | { |
| 529 | return qc_avail_streams(conn, QCS_BIDI); |
| 530 | } |
| 531 | |
| 532 | /* returns the number of concurrent unidirectional streams available on the |
| 533 | * connection. |
| 534 | */ |
| 535 | static int qc_avail_streams_uni(struct connection *conn) |
| 536 | { |
| 537 | return qc_avail_streams(conn, QCS_UNI); |
| 538 | } |
| 539 | |
| 540 | /*****************************************************************/ |
| 541 | /* functions below are dedicated to the mux setup and management */ |
| 542 | /*****************************************************************/ |
| 543 | |
| 544 | /* Update the mux transport parameter after having received remote transpot parameters */ |
| 545 | void quic_mux_transport_params_update(struct qcc *qcc) |
| 546 | { |
| 547 | if (objt_listener(qcc->conn->target)) { |
| 548 | struct quic_transport_params *clt_params; |
| 549 | |
| 550 | /* Client parameters, params used to TX. */ |
| 551 | clt_params = &qcc->conn->qc->tx.params; |
| 552 | |
| 553 | qcc->tx.max_data = clt_params->initial_max_data; |
| 554 | /* Client initiated streams must respect the server flow control. */ |
| 555 | qcc->strms[QCS_CLT_BIDI].rx.max_data = clt_params->initial_max_stream_data_bidi_local; |
| 556 | qcc->strms[QCS_CLT_UNI].rx.max_data = clt_params->initial_max_stream_data_uni; |
| 557 | |
| 558 | /* Server initiated streams must respect the server flow control. */ |
| 559 | qcc->strms[QCS_SRV_BIDI].max_streams = clt_params->initial_max_streams_bidi; |
| 560 | qcc->strms[QCS_SRV_BIDI].tx.max_data = clt_params->initial_max_stream_data_bidi_remote; |
| 561 | |
| 562 | qcc->strms[QCS_SRV_UNI].max_streams = clt_params->initial_max_streams_uni; |
| 563 | qcc->strms[QCS_SRV_UNI].tx.max_data = clt_params->initial_max_stream_data_uni; |
| 564 | } |
| 565 | else { |
| 566 | struct quic_transport_params *srv_params; |
| 567 | |
| 568 | /* server parameters, TX params. */ |
| 569 | srv_params = &qcc->conn->qc->tx.params; |
| 570 | |
| 571 | qcc->tx.max_data = srv_params->initial_max_data; |
| 572 | /* Client initiated streams must respect the server flow control. */ |
| 573 | qcc->strms[QCS_CLT_BIDI].max_streams = srv_params->initial_max_streams_bidi; |
| 574 | qcc->strms[QCS_CLT_BIDI].tx.max_data = srv_params->initial_max_stream_data_bidi_remote; |
| 575 | |
| 576 | qcc->strms[QCS_CLT_UNI].max_streams = srv_params->initial_max_streams_uni; |
| 577 | qcc->strms[QCS_CLT_UNI].tx.max_data = srv_params->initial_max_stream_data_uni; |
| 578 | |
| 579 | /* Server initiated streams must respect the server flow control. */ |
| 580 | qcc->strms[QCS_SRV_BIDI].rx.max_data = srv_params->initial_max_stream_data_bidi_local; |
| 581 | qcc->strms[QCS_SRV_UNI].rx.max_data = srv_params->initial_max_stream_data_uni; |
| 582 | } |
| 583 | |
| 584 | /* Now that we have all the flow control information, we can finalize the application |
| 585 | * context. |
| 586 | */ |
| 587 | qcc->app_ops->finalize(qcc->ctx); |
| 588 | } |
| 589 | |
| 590 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 591 | * is already initialized before installing the mux, so we detect incoming |
| 592 | * connections from the fact that the context is still NULL (even during mux |
| 593 | * upgrades). <input> is always used as Input buffer and may contain data. It is |
| 594 | * the caller responsibility to not reuse it anymore. Returns < 0 on error. |
| 595 | */ |
| 596 | static int qc_init(struct connection *conn, struct proxy *prx, |
| 597 | struct session *sess, struct buffer *input) |
| 598 | { |
| 599 | struct qcc *qcc; |
| 600 | struct task *t = NULL; |
| 601 | void *conn_ctx = conn->ctx; |
| 602 | |
| 603 | TRACE_ENTER(QC_EV_QCC_NEW); |
| 604 | |
| 605 | qcc = pool_alloc(pool_head_qcc); |
| 606 | if (!qcc) |
| 607 | goto fail_no_qcc; |
| 608 | |
| 609 | if (conn_is_back(conn)) { |
| 610 | qcc->flags = QC_CF_IS_BACK; |
| 611 | qcc->shut_timeout = qcc->timeout = prx->timeout.server; |
| 612 | if (tick_isset(prx->timeout.serverfin)) |
| 613 | qcc->shut_timeout = prx->timeout.serverfin; |
| 614 | |
| 615 | qcc->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_be, |
| 616 | &qc_stats_module); |
| 617 | } else { |
| 618 | qcc->flags = QC_CF_NONE; |
| 619 | qcc->shut_timeout = qcc->timeout = prx->timeout.client; |
| 620 | if (tick_isset(prx->timeout.clientfin)) |
| 621 | qcc->shut_timeout = prx->timeout.clientfin; |
| 622 | |
| 623 | qcc->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 624 | &qc_stats_module); |
| 625 | } |
| 626 | |
| 627 | qcc->proxy = prx; |
| 628 | qcc->task = NULL; |
| 629 | if (tick_isset(qcc->timeout)) { |
| 630 | t = task_new(tid_bit); |
| 631 | if (!t) |
| 632 | goto fail; |
| 633 | |
| 634 | qcc->task = t; |
| 635 | t->process = qc_timeout_task; |
| 636 | t->context = qcc; |
| 637 | t->expire = tick_add(now_ms, qcc->timeout); |
| 638 | } |
| 639 | |
| 640 | qcc->wait_event.tasklet = tasklet_new(); |
| 641 | if (!qcc->wait_event.tasklet) |
| 642 | goto fail; |
| 643 | |
| 644 | qcc->wait_event.tasklet->process = qc_io_cb; |
| 645 | qcc->wait_event.tasklet->context = qcc; |
| 646 | qcc->wait_event.events = 0; |
| 647 | |
| 648 | /* Initialize the context. */ |
| 649 | qcc->st0 = QC_CS_NOERR; |
| 650 | qcc->conn = conn; |
| 651 | qcc->conn->qc->qcc = qcc; |
| 652 | |
| 653 | /* Application layer initialization. */ |
| 654 | qcc->app_ops = &h3_ops; |
| 655 | if (!qcc->app_ops->init(qcc)) |
| 656 | goto fail; |
| 657 | |
| 658 | /* The transports parameters which control the data sent have been stored |
| 659 | * in ->tx.params. The ones which control the received data are stored in |
| 660 | * in ->rx.params. |
| 661 | */ |
| 662 | if (objt_listener(qcc->conn->target)) { |
| 663 | struct quic_transport_params *srv_params; |
| 664 | |
| 665 | /* Server parameters, params used for RX flow control. */ |
| 666 | srv_params = &conn->qc->rx.params; |
| 667 | |
| 668 | qcc->rx.max_data = srv_params->initial_max_data; |
| 669 | qcc->tx.max_data = 0; |
| 670 | /* Client initiated streams must respect the server flow control. */ |
| 671 | qcc->strms[QCS_CLT_BIDI].max_streams = srv_params->initial_max_streams_bidi; |
| 672 | qcc->strms[QCS_CLT_BIDI].nb_streams = 0; |
| 673 | qcc->strms[QCS_CLT_BIDI].largest_id = -1; |
| 674 | qcc->strms[QCS_CLT_BIDI].rx.max_data = 0; |
| 675 | qcc->strms[QCS_CLT_BIDI].tx.max_data = srv_params->initial_max_stream_data_bidi_remote; |
| 676 | |
| 677 | qcc->strms[QCS_CLT_UNI].max_streams = srv_params->initial_max_streams_uni; |
| 678 | qcc->strms[QCS_CLT_UNI].nb_streams = 0; |
| 679 | qcc->strms[QCS_CLT_UNI].largest_id = -1; |
| 680 | qcc->strms[QCS_CLT_UNI].rx.max_data = 0; |
| 681 | qcc->strms[QCS_CLT_UNI].tx.max_data = srv_params->initial_max_stream_data_uni; |
| 682 | |
| 683 | /* Server initiated streams must respect the server flow control. */ |
| 684 | qcc->strms[QCS_SRV_BIDI].max_streams = 0; |
| 685 | qcc->strms[QCS_SRV_BIDI].nb_streams = 0; |
| 686 | qcc->strms[QCS_SRV_BIDI].largest_id = -1; |
| 687 | qcc->strms[QCS_SRV_BIDI].rx.max_data = srv_params->initial_max_stream_data_bidi_local; |
| 688 | qcc->strms[QCS_SRV_BIDI].tx.max_data = 0; |
| 689 | |
| 690 | qcc->strms[QCS_SRV_UNI].max_streams = 0; |
| 691 | qcc->strms[QCS_SRV_UNI].nb_streams = 0; |
| 692 | qcc->strms[QCS_SRV_UNI].largest_id = -1; |
| 693 | qcc->strms[QCS_SRV_UNI].rx.max_data = srv_params->initial_max_stream_data_uni; |
| 694 | qcc->strms[QCS_SRV_UNI].tx.max_data = 0; |
| 695 | } |
| 696 | else { |
| 697 | struct quic_transport_params *clt_params; |
| 698 | |
| 699 | /* client parameters, RX params. */ |
| 700 | clt_params = &conn->qc->rx.params; |
| 701 | |
| 702 | qcc->rx.max_data = clt_params->initial_max_data; |
| 703 | qcc->tx.max_data = 0; |
| 704 | /* Client initiated streams must respect the server flow control. */ |
| 705 | qcc->strms[QCS_CLT_BIDI].max_streams = 0; |
| 706 | qcc->strms[QCS_CLT_BIDI].nb_streams = 0; |
| 707 | qcc->strms[QCS_CLT_BIDI].largest_id = -1; |
| 708 | qcc->strms[QCS_CLT_BIDI].rx.max_data = clt_params->initial_max_stream_data_bidi_local; |
| 709 | qcc->strms[QCS_CLT_BIDI].tx.max_data = 0; |
| 710 | |
| 711 | qcc->strms[QCS_CLT_UNI].max_streams = 0; |
| 712 | qcc->strms[QCS_CLT_UNI].nb_streams = 0; |
| 713 | qcc->strms[QCS_CLT_UNI].largest_id = -1; |
| 714 | qcc->strms[QCS_CLT_UNI].rx.max_data = clt_params->initial_max_stream_data_uni; |
| 715 | qcc->strms[QCS_CLT_UNI].tx.max_data = 0; |
| 716 | |
| 717 | /* Server initiated streams must respect the server flow control. */ |
| 718 | qcc->strms[QCS_SRV_BIDI].max_streams = clt_params->initial_max_streams_bidi; |
| 719 | qcc->strms[QCS_SRV_BIDI].nb_streams = 0; |
| 720 | qcc->strms[QCS_SRV_BIDI].largest_id = -1; |
| 721 | qcc->strms[QCS_SRV_BIDI].rx.max_data = 0; |
| 722 | qcc->strms[QCS_SRV_BIDI].tx.max_data = clt_params->initial_max_stream_data_bidi_remote; |
| 723 | |
| 724 | qcc->strms[QCS_SRV_UNI].max_streams = clt_params->initial_max_streams_uni; |
| 725 | qcc->strms[QCS_SRV_UNI].nb_streams = 0; |
| 726 | qcc->strms[QCS_SRV_UNI].largest_id = -1; |
| 727 | qcc->strms[QCS_SRV_UNI].rx.max_data = 0; |
| 728 | qcc->strms[QCS_SRV_UNI].tx.max_data = clt_params->initial_max_stream_data_uni; |
| 729 | |
| 730 | } |
| 731 | |
| 732 | /* Initialize the streams counters. */ |
| 733 | qcc->nb_cs = 0; |
| 734 | qcc->stream_cnt = 0; |
| 735 | |
| 736 | br_init(qcc->mbuf, sizeof(qcc->mbuf) / sizeof(qcc->mbuf[0])); |
| 737 | qcc->streams_by_id = EB_ROOT_UNIQUE; |
| 738 | LIST_INIT(&qcc->send_list); |
| 739 | LIST_INIT(&qcc->fctl_list); |
| 740 | LIST_INIT(&qcc->blocked_list); |
| 741 | LIST_INIT(&qcc->buf_wait.list); |
| 742 | MT_LIST_INIT(&qcc->qcs_rxbuf_wlist); |
| 743 | |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 744 | HA_ATOMIC_STORE(&conn->ctx, qcc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 745 | |
| 746 | if (t) |
| 747 | task_queue(t); |
| 748 | |
| 749 | if (qcc->flags & QC_CF_IS_BACK) { |
| 750 | /* FIXME: For outgoing connections we need to immediately allocate streams. |
| 751 | * This highly depends on the QUIC application needs. |
| 752 | */ |
| 753 | } |
| 754 | |
| 755 | HA_ATOMIC_ADD(&qcc->px_counters->open_conns, 1); |
| 756 | HA_ATOMIC_ADD(&qcc->px_counters->total_conns, 1); |
| 757 | |
| 758 | /* prepare to read something */ |
| 759 | qcc_restart_reading(qcc, 1); |
| 760 | TRACE_LEAVE(QC_EV_QCC_NEW, conn); |
| 761 | return 0; |
| 762 | |
| 763 | fail: |
| 764 | task_destroy(t); |
| 765 | if (qcc->wait_event.tasklet) |
| 766 | tasklet_free(qcc->wait_event.tasklet); |
| 767 | pool_free(pool_head_qcc, qcc); |
| 768 | fail_no_qcc: |
| 769 | conn->ctx = conn_ctx; /* restore saved ctx */ |
| 770 | TRACE_DEVEL("leaving in error", QC_EV_QCC_NEW|QC_EV_QCC_END|QC_EV_QCC_ERR); |
| 771 | return -1; |
| 772 | } |
| 773 | |
| 774 | /* returns the stream associated with id <id> or NULL if not found */ |
| 775 | __maybe_unused |
| 776 | static inline struct qcs *qcc_st_by_id(struct qcc *qcc, int id) |
| 777 | { |
| 778 | struct eb64_node *node; |
| 779 | |
| 780 | node = eb64_lookup(&qcc->streams_by_id, id); |
| 781 | if (!node) |
| 782 | return (struct qcs *)qc_closed_stream; |
| 783 | |
| 784 | return container_of(node, struct qcs, by_id); |
| 785 | } |
| 786 | |
| 787 | /* release function. This one should be called to free all resources allocated |
| 788 | * to the mux. |
| 789 | */ |
| 790 | static void qc_release(struct qcc *qcc) |
| 791 | { |
| 792 | struct connection *conn = NULL; |
| 793 | |
| 794 | TRACE_ENTER(QC_EV_QCC_END); |
| 795 | |
| 796 | if (qcc) { |
| 797 | /* The connection must be aattached to this mux to be released */ |
| 798 | if (qcc->conn && qcc->conn->ctx == qcc) |
| 799 | conn = qcc->conn; |
| 800 | |
| 801 | TRACE_DEVEL("freeing qcc", QC_EV_QCC_END, conn); |
| 802 | |
| 803 | if (LIST_INLIST(&qcc->buf_wait.list)) |
| 804 | LIST_DELETE(&qcc->buf_wait.list); |
| 805 | |
| 806 | qc_release_mbuf(qcc); |
| 807 | |
| 808 | if (qcc->task) { |
| 809 | qcc->task->context = NULL; |
| 810 | task_wakeup(qcc->task, TASK_WOKEN_OTHER); |
| 811 | qcc->task = NULL; |
| 812 | } |
| 813 | if (qcc->wait_event.tasklet) |
| 814 | tasklet_free(qcc->wait_event.tasklet); |
| 815 | if (conn && qcc->wait_event.events != 0) |
| 816 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, qcc->wait_event.events, |
| 817 | &qcc->wait_event); |
| 818 | |
| 819 | HA_ATOMIC_SUB(&qcc->px_counters->open_conns, 1); |
| 820 | |
| 821 | pool_free(pool_head_qcc, qcc); |
| 822 | } |
| 823 | |
| 824 | if (conn) { |
| 825 | conn->mux = NULL; |
| 826 | conn->ctx = NULL; |
| 827 | TRACE_DEVEL("freeing conn", QC_EV_QCC_END, conn); |
| 828 | |
| 829 | conn_stop_tracking(conn); |
| 830 | conn_full_close(conn); |
| 831 | if (conn->destroy_cb) |
| 832 | conn->destroy_cb(conn); |
| 833 | conn_free(conn); |
| 834 | } |
| 835 | |
| 836 | TRACE_LEAVE(QC_EV_QCC_END); |
| 837 | } |
| 838 | |
| 839 | |
| 840 | /******************************************************/ |
| 841 | /* functions below are for the QUIC protocol processing */ |
| 842 | /******************************************************/ |
| 843 | |
| 844 | /* attempt to notify the data layer of recv availability */ |
| 845 | __maybe_unused |
| 846 | static void qcs_notify_recv(struct qcs *qcs) |
| 847 | { |
| 848 | if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) { |
| 849 | TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs); |
| 850 | tasklet_wakeup(qcs->subs->tasklet); |
| 851 | qcs->subs->events &= ~SUB_RETRY_RECV; |
| 852 | if (!qcs->subs->events) |
| 853 | qcs->subs = NULL; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /* attempt to notify the data layer of send availability */ |
| 858 | __maybe_unused |
| 859 | static void qcs_notify_send(struct qcs *qcs) |
| 860 | { |
| 861 | if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) { |
| 862 | TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs); |
| 863 | qcs->flags |= QC_SF_NOTIFIED; |
| 864 | tasklet_wakeup(qcs->subs->tasklet); |
| 865 | qcs->subs->events &= ~SUB_RETRY_SEND; |
| 866 | if (!qcs->subs->events) |
| 867 | qcs->subs = NULL; |
| 868 | } |
| 869 | else if (qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW)) { |
| 870 | TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs); |
| 871 | tasklet_wakeup(qcs->shut_tl); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | /* alerts the data layer, trying to wake it up by all means, following |
| 876 | * this sequence : |
| 877 | * - if the qcs' data layer is subscribed to recv, then it's woken up for recv |
| 878 | * - if its subscribed to send, then it's woken up for send |
| 879 | * - if it was subscribed to neither, its ->wake() callback is called |
| 880 | * It is safe to call this function with a closed stream which doesn't have a |
| 881 | * conn_stream anymore. |
| 882 | */ |
| 883 | __maybe_unused |
| 884 | static void qcs_alert(struct qcs *qcs) |
| 885 | { |
| 886 | TRACE_ENTER(QC_EV_QCS_WAKE, qcs->qcc->conn, qcs); |
| 887 | |
| 888 | if (qcs->subs || |
| 889 | (qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW))) { |
| 890 | qcs_notify_recv(qcs); |
| 891 | qcs_notify_send(qcs); |
| 892 | } |
| 893 | else if (qcs->cs && qcs->cs->data_cb->wake != NULL) { |
| 894 | TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs); |
| 895 | qcs->cs->data_cb->wake(qcs->cs); |
| 896 | } |
| 897 | |
| 898 | TRACE_LEAVE(QC_EV_QCS_WAKE, qcs->qcc->conn, qcs); |
| 899 | } |
| 900 | |
| 901 | /* marks stream <qcs> as CLOSED and decrement the number of active streams for |
| 902 | * its connection if the stream was not yet closed. Please use this exclusively |
| 903 | * before closing a stream to ensure stream count is well maintained. |
| 904 | */ |
| 905 | static inline void qcs_close(struct qcs *qcs) |
| 906 | { |
| 907 | TRACE_ENTER(QC_EV_QCS_END, qcs->qcc->conn, qcs); |
| 908 | /* XXX TO DO XXX */ |
| 909 | TRACE_LEAVE(QC_EV_QCS_END, qcs->qcc->conn, qcs); |
| 910 | } |
| 911 | |
| 912 | /* detaches an QUIC stream from its QCC and releases it to the QCS pool. */ |
| 913 | /* qcs_destroy should only ever be called by the thread that owns the stream, |
| 914 | * that means that a tasklet should be used if we want to destroy the qcs |
| 915 | * from another thread |
| 916 | */ |
| 917 | static void qcs_destroy(struct qcs *qcs) |
| 918 | { |
| 919 | struct connection *conn = qcs->qcc->conn; |
| 920 | |
| 921 | TRACE_ENTER(QC_EV_QCS_END, conn, qcs); |
| 922 | |
| 923 | qcs_close(qcs); |
| 924 | eb64_delete(&qcs->by_id); |
| 925 | if (b_size(&qcs->rx.buf)) { |
| 926 | b_free(&qcs->rx.buf); |
| 927 | offer_buffers(NULL, 1); |
| 928 | } |
| 929 | |
| 930 | if (qcs->subs) |
| 931 | qcs->subs->events = 0; |
| 932 | |
| 933 | /* There's no need to explicitly call unsubscribe here, the only |
| 934 | * reference left would be in the qcc send_list/fctl_list, and if |
| 935 | * we're in it, we're getting out anyway |
| 936 | */ |
| 937 | LIST_DEL_INIT(&qcs->list); |
| 938 | |
| 939 | /* ditto, calling tasklet_free() here should be ok */ |
| 940 | tasklet_free(qcs->shut_tl); |
| 941 | pool_free(pool_head_qcs, qcs); |
| 942 | |
| 943 | TRACE_LEAVE(QC_EV_QCS_END, conn); |
| 944 | } |
| 945 | |
| 946 | /* allocates a new bidirection stream <id> for connection <qcc> and adds it into qcc's |
| 947 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 948 | * causes of errors can be any failed memory allocation. The caller is |
| 949 | * responsible for checking if the connection may support an extra stream |
| 950 | * prior to calling this function. |
| 951 | */ |
| 952 | struct qcs *bidi_qcs_new(struct qcc *qcc, uint64_t id) |
| 953 | { |
| 954 | struct qcs *qcs; |
| 955 | enum qcs_type qcs_type; |
| 956 | |
| 957 | TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn); |
| 958 | |
| 959 | qcs = pool_alloc(pool_head_qcs); |
| 960 | if (!qcs) |
| 961 | goto out; |
| 962 | |
| 963 | qcs->shut_tl = tasklet_new(); |
| 964 | if (!qcs->shut_tl) { |
| 965 | pool_free(pool_head_qcs, qcs); |
| 966 | goto out; |
| 967 | } |
| 968 | |
| 969 | qcs_type = qcs_id_type(id); |
| 970 | qcs->qcc = qcc; |
| 971 | qcs->cs = NULL; |
| 972 | qcs->id = qcs->by_id.key = id; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 973 | qcs->flags = QC_SF_NONE; |
| 974 | |
| 975 | qcs->rx.buf = BUF_NULL; |
| 976 | qcs->rx.st = QC_RX_SS_IDLE; |
| 977 | qcs->rx.bytes = qcs->rx.offset = 0; |
| 978 | qcs->rx.max_data = qcc->strms[qcs_type].rx.max_data; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 979 | qcs->rx.buf = BUF_NULL; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 980 | qcs->rx.frms = EB_ROOT_UNIQUE; |
| 981 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 982 | qcs->tx.st = QC_TX_SS_IDLE; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 983 | qcs->tx.bytes = qcs->tx.offset = qcs->tx.ack_offset = 0; |
| 984 | qcs->tx.acked_frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 985 | qcs->tx.max_data = qcc->strms[qcs_type].tx.max_data; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 986 | qcs->tx.buf = BUF_NULL; |
Amaury Denoyelle | f52151d | 2021-08-24 16:11:18 +0200 | [diff] [blame] | 987 | br_init(qcs->tx.mbuf, sizeof(qcs->tx.mbuf) / sizeof(qcs->tx.mbuf[0])); |
Amaury Denoyelle | 42bb8aa | 2021-08-24 16:28:47 +0200 | [diff] [blame] | 988 | qcs->tx.left = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 989 | |
| 990 | eb64_insert(&qcc->streams_by_id, &qcs->by_id); |
| 991 | qcc->strms[qcs_type].nb_streams++; |
| 992 | qcc->stream_cnt++; |
| 993 | qcs->subs = NULL; |
| 994 | LIST_INIT(&qcs->list); |
| 995 | qcs->shut_tl->process = qc_deferred_shut; |
| 996 | qcs->shut_tl->context = qcs; |
| 997 | |
| 998 | HA_ATOMIC_ADD(&qcc->px_counters->open_streams, 1); |
| 999 | HA_ATOMIC_ADD(&qcc->px_counters->total_streams, 1); |
| 1000 | |
| 1001 | TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn, qcs); |
| 1002 | return qcs; |
| 1003 | |
| 1004 | out: |
| 1005 | TRACE_DEVEL("leaving in error", QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn); |
| 1006 | return NULL; |
| 1007 | } |
| 1008 | |
| 1009 | /* Release <qcs> outgoing uni-stream */ |
| 1010 | void qcs_release(struct qcs *qcs) |
| 1011 | { |
| 1012 | eb64_delete(&qcs->by_id); |
| 1013 | pool_free(pool_head_qcs, qcs); |
| 1014 | } |
| 1015 | |
| 1016 | /* Allocates a locally initiated unidirectional stream. */ |
| 1017 | struct qcs *luqs_new(struct qcc *qcc) |
| 1018 | { |
| 1019 | struct qcs *qcs; |
| 1020 | uint64_t next_id; |
| 1021 | enum qcs_type qcs_type; |
| 1022 | |
| 1023 | TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn); |
| 1024 | |
| 1025 | qcs = NULL; |
| 1026 | /* QCS_ID_DIR_BIT bit is set for unidirectional stream. */ |
| 1027 | if (objt_listener(qcc->conn->target)) |
| 1028 | qcs_type = QCS_ID_SRV_INTIATOR_BIT | QCS_ID_DIR_BIT; |
| 1029 | else |
| 1030 | qcs_type = QCS_ID_DIR_BIT; |
| 1031 | |
| 1032 | next_id = qcs_next_id(qcc, qcs_type); |
| 1033 | if (next_id == (uint64_t)-1) { |
| 1034 | TRACE_PROTO("No more stream available", QC_EV_QCS_NEW, qcc->conn); |
| 1035 | goto out; |
| 1036 | } |
| 1037 | |
| 1038 | qcs = pool_alloc(pool_head_qcs); |
| 1039 | if (!qcs) |
| 1040 | goto out; |
| 1041 | |
| 1042 | qcs->qcc = qcc; |
| 1043 | qcs->cs = NULL; |
| 1044 | qcs->id = qcs->by_id.key = next_id; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1045 | qcs->flags = QC_SF_NONE; |
| 1046 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1047 | qcs->tx.st = QC_TX_SS_IDLE; |
| 1048 | qcs->tx.max_data = qcc->strms[qcs_type].tx.max_data; |
| 1049 | qcs->tx.offset = qcs->tx.bytes = qcs->tx.ack_offset = 0; |
| 1050 | qcs->tx.acked_frms = EB_ROOT_UNIQUE; |
| 1051 | qcs->tx.buf = BUF_NULL; |
Amaury Denoyelle | f52151d | 2021-08-24 16:11:18 +0200 | [diff] [blame] | 1052 | br_init(qcs->tx.mbuf, sizeof(qcs->tx.mbuf) / sizeof(qcs->tx.mbuf[0])); |
Amaury Denoyelle | 42bb8aa | 2021-08-24 16:28:47 +0200 | [diff] [blame] | 1053 | qcs->tx.left = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1054 | |
| 1055 | qcs->subs = NULL; |
| 1056 | LIST_INIT(&qcs->list); |
| 1057 | eb64_insert(&qcc->streams_by_id, &qcs->by_id); |
| 1058 | |
| 1059 | TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn); |
| 1060 | return qcs; |
| 1061 | |
| 1062 | out: |
| 1063 | if (qcs) |
| 1064 | pool_free(pool_head_qcs, qcs); |
| 1065 | TRACE_DEVEL("leaving in error", QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn); |
| 1066 | return NULL; |
| 1067 | } |
| 1068 | |
| 1069 | /* Allocates a remotely initiated unidirectional stream. */ |
| 1070 | struct qcs *ruqs_new(struct qcc *qcc, uint64_t id) |
| 1071 | { |
| 1072 | struct qcs *qcs; |
| 1073 | enum qcs_type qcs_type; |
| 1074 | |
| 1075 | TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn); |
| 1076 | qcs = pool_alloc(pool_head_qcs); |
| 1077 | if (!qcs) |
| 1078 | goto out; |
| 1079 | |
| 1080 | qcs_type = qcs_id_type(id); |
| 1081 | |
| 1082 | qcs->qcc = qcc; |
| 1083 | qcs->cs = NULL; |
| 1084 | |
| 1085 | qcs->qcc = qcc; |
| 1086 | qcs->id = qcs->by_id.key = id; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1087 | qcs->flags = QC_SF_NONE; |
| 1088 | |
| 1089 | qcs->rx.st = QC_RX_SS_IDLE; |
| 1090 | qcs->rx.max_data = qcc->strms[qcs_type].rx.max_data; |
| 1091 | qcs->rx.offset = qcs->rx.bytes = 0; |
| 1092 | qcs->rx.buf = BUF_NULL; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1093 | qcs->rx.frms = EB_ROOT_UNIQUE; |
Amaury Denoyelle | f52151d | 2021-08-24 16:11:18 +0200 | [diff] [blame] | 1094 | br_init(qcs->tx.mbuf, sizeof(qcs->tx.mbuf) / sizeof(qcs->tx.mbuf[0])); |
Amaury Denoyelle | 42bb8aa | 2021-08-24 16:28:47 +0200 | [diff] [blame] | 1095 | qcs->tx.left = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1096 | |
| 1097 | qcs->subs = NULL; |
| 1098 | LIST_INIT(&qcs->list); |
| 1099 | eb64_insert(&qcc->streams_by_id, &qcs->by_id); |
| 1100 | |
| 1101 | TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn); |
| 1102 | return qcs; |
| 1103 | |
| 1104 | out: |
| 1105 | TRACE_DEVEL("leaving in error", QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn); |
| 1106 | return NULL; |
| 1107 | } |
| 1108 | |
| 1109 | /* attempt to notify the data layer of recv availability */ |
| 1110 | void ruqs_notify_recv(struct qcs *qcs) |
| 1111 | { |
| 1112 | if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) { |
| 1113 | TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn); |
| 1114 | tasklet_wakeup(qcs->subs->tasklet); |
| 1115 | qcs->subs->events &= ~SUB_RETRY_RECV; |
| 1116 | if (!qcs->subs->events) |
| 1117 | qcs->subs = NULL; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | /* Allocates a new stream associated to conn_stream <cs> on the qcc connection |
| 1122 | * with dir as direction and returns it, or NULL in case of memory allocation |
| 1123 | * error or if the highest possible stream ID was reached. |
| 1124 | */ |
| 1125 | static struct qcs *qcc_bck_stream_new(struct qcc *qcc, int dir, |
| 1126 | struct conn_stream *cs, struct session *sess) |
| 1127 | { |
| 1128 | struct qcs *qcs = NULL; |
| 1129 | enum qcs_type qcs_type; |
| 1130 | |
| 1131 | TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn); |
| 1132 | |
| 1133 | qcs_type = qcs_type_from_dir(qcc, dir); |
| 1134 | if (qcc->strms[qcs_type].largest_id + 1 >= qcc->strms[qcs_type].max_streams) |
| 1135 | goto out; |
| 1136 | |
| 1137 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1138 | qcs = bidi_qcs_new(qcc, qcc->strms[qcs_type].largest_id + 1); |
| 1139 | if (!qcs) |
| 1140 | goto out; |
| 1141 | |
| 1142 | qcs->cs = cs; |
| 1143 | qcs->sess = sess; |
| 1144 | cs->ctx = qcs; |
| 1145 | qcc->nb_cs++; |
| 1146 | |
| 1147 | out: |
| 1148 | if (likely(qcs)) |
| 1149 | TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn, qcs); |
| 1150 | else |
| 1151 | TRACE_LEAVE(QC_EV_QCS_NEW|QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn, qcs); |
| 1152 | return qcs; |
| 1153 | } |
| 1154 | |
| 1155 | /* Allocates a new bidirectional stream associated to conn_stream <cs> on the <qcc> connection |
| 1156 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1157 | * possible stream ID was reached. |
| 1158 | */ |
| 1159 | __maybe_unused |
| 1160 | static struct qcs *qcc_bck_stream_new_bidi(struct qcc *qcc, |
| 1161 | struct conn_stream *cs, struct session *sess) |
| 1162 | { |
| 1163 | return qcc_bck_stream_new(qcc, QCS_BIDI, cs, sess); |
| 1164 | } |
| 1165 | |
| 1166 | /* Allocates a new unidirectional stream associated to conn_stream <cs> on the <qcc> connection |
| 1167 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1168 | * possible stream ID was reached. |
| 1169 | */ |
| 1170 | __maybe_unused |
| 1171 | static struct qcs *qcc_bck_stream_new_uni(struct qcc *qcc, |
| 1172 | struct conn_stream *cs, struct session *sess) |
| 1173 | { |
| 1174 | return qcc_bck_stream_new(qcc, QCS_UNI, cs, sess); |
| 1175 | } |
| 1176 | |
| 1177 | |
| 1178 | /* wake a specific stream and assign its conn_stream some CS_FL_* flags among |
| 1179 | * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state |
| 1180 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1181 | * destroyed. |
| 1182 | */ |
| 1183 | static void qcs_wake_one_stream(struct qcs *qcs) |
| 1184 | { |
| 1185 | struct qcc *qcc = qcs->qcc; |
| 1186 | |
| 1187 | TRACE_ENTER(QC_EV_QCS_WAKE, qcc->conn, qcs); |
| 1188 | if (!qcs->cs) { |
| 1189 | /* this stream was already orphaned */ |
| 1190 | qcs_destroy(qcs); |
| 1191 | TRACE_DEVEL("leaving with no qcs", QC_EV_QCS_WAKE, qcc->conn); |
| 1192 | return; |
| 1193 | } |
| 1194 | /* XXX TO DO XXX */ |
| 1195 | TRACE_LEAVE(QC_EV_QCS_WAKE, qcc->conn); |
| 1196 | } |
| 1197 | |
| 1198 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1199 | * or unassigned. |
| 1200 | */ |
| 1201 | static void qc_wake_some_streams(struct qcc *qcc, int last) |
| 1202 | { |
| 1203 | struct eb64_node *node; |
| 1204 | struct qcs *qcs; |
| 1205 | |
| 1206 | TRACE_ENTER(QC_EV_QCS_WAKE, qcc->conn); |
| 1207 | |
| 1208 | /* Wake all streams with ID > last */ |
| 1209 | node = eb64_lookup_ge(&qcc->streams_by_id, last + 1); |
| 1210 | while (node) { |
| 1211 | qcs = container_of(node, struct qcs, by_id); |
| 1212 | node = eb64_next(node); |
| 1213 | qcs_wake_one_stream(qcs); |
| 1214 | } |
| 1215 | |
| 1216 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1217 | node = eb64_lookup(&qcc->streams_by_id, 0); |
| 1218 | while (node) { |
| 1219 | qcs = container_of(node, struct qcs, by_id); |
| 1220 | if (qcs->id > 0) |
| 1221 | break; |
| 1222 | node = eb64_next(node); |
| 1223 | qcs_wake_one_stream(qcs); |
| 1224 | } |
| 1225 | |
| 1226 | TRACE_LEAVE(QC_EV_QCS_WAKE, qcc->conn); |
| 1227 | } |
| 1228 | |
| 1229 | /* Wake up all blocked streams whose window size has become positive after the |
| 1230 | * mux's initial window was adjusted. This should be done after having processed |
| 1231 | * SETTINGS frames which have updated the mux's initial window size. |
| 1232 | */ |
| 1233 | __maybe_unused |
| 1234 | static void qcc_unblock_sfctl(struct qcc *qcc) |
| 1235 | { |
| 1236 | TRACE_ENTER(QC_EV_QCC_WAKE, qcc->conn); |
| 1237 | /* XXX TO DO XXX */ |
| 1238 | TRACE_LEAVE(QC_EV_QCC_WAKE, qcc->conn); |
| 1239 | } |
| 1240 | |
| 1241 | /* process Rx frames to be demultiplexed */ |
| 1242 | __maybe_unused |
| 1243 | static void qc_process_demux(struct qcc *qcc) |
| 1244 | { |
| 1245 | TRACE_ENTER(QC_EV_QCC_WAKE, qcc->conn); |
| 1246 | /* XXX TO DO XXX */ |
| 1247 | TRACE_LEAVE(QC_EV_QCC_WAKE, qcc->conn); |
| 1248 | } |
| 1249 | |
| 1250 | /* resume each qcs eligible for sending in list head <head> */ |
| 1251 | __maybe_unused |
| 1252 | static void qc_resume_each_sending_qcs(struct qcc *qcc, struct list *head) |
| 1253 | { |
| 1254 | struct qcs *qcs, *qcs_back; |
| 1255 | |
| 1256 | TRACE_ENTER(QC_EV_QCC_SEND|QC_EV_QCS_WAKE, qcc->conn); |
| 1257 | |
| 1258 | list_for_each_entry_safe(qcs, qcs_back, head, list) { |
| 1259 | if (qcc_wnd(qcc) <= 0 || |
| 1260 | qcc->flags & QC_CF_MUX_BLOCK_ANY || |
| 1261 | qcc->st0 >= QC_CS_ERROR) |
| 1262 | break; |
| 1263 | |
| 1264 | qcs->flags &= ~QC_SF_BLK_ANY; |
| 1265 | |
| 1266 | if (qcs->flags & QC_SF_NOTIFIED) |
| 1267 | continue; |
| 1268 | |
| 1269 | /* If the sender changed his mind and unsubscribed, let's just |
| 1270 | * remove the stream from the send_list. |
| 1271 | */ |
| 1272 | if (!(qcs->flags & (QC_SF_WANT_SHUTR|QC_SF_WANT_SHUTW)) && |
| 1273 | (!qcs->subs || !(qcs->subs->events & SUB_RETRY_SEND))) { |
| 1274 | LIST_DEL_INIT(&qcs->list); |
| 1275 | continue; |
| 1276 | } |
| 1277 | |
| 1278 | if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) { |
| 1279 | qcs->flags |= QC_SF_NOTIFIED; |
| 1280 | tasklet_wakeup(qcs->subs->tasklet); |
| 1281 | qcs->subs->events &= ~SUB_RETRY_SEND; |
| 1282 | if (!qcs->subs->events) |
| 1283 | qcs->subs = NULL; |
| 1284 | } |
| 1285 | else if (qcs->flags & (QC_SF_WANT_SHUTR|QC_SF_WANT_SHUTW)) { |
| 1286 | tasklet_wakeup(qcs->shut_tl); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | TRACE_LEAVE(QC_EV_QCC_SEND|QC_EV_QCS_WAKE, qcc->conn); |
| 1291 | } |
| 1292 | |
| 1293 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 1294 | * the end. |
| 1295 | */ |
| 1296 | __maybe_unused |
| 1297 | static int qc_process_mux(struct qcc *qcc) |
| 1298 | { |
| 1299 | TRACE_ENTER(QC_EV_QCC_WAKE, qcc->conn); |
| 1300 | /* XXX TO DO XXX */ |
| 1301 | TRACE_LEAVE(QC_EV_QCC_WAKE, qcc->conn); |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | |
| 1306 | /* Attempt to read data, and subscribe if none available. |
| 1307 | * The function returns 1 if data has been received, otherwise zero. |
| 1308 | */ |
| 1309 | __maybe_unused |
| 1310 | static int qc_recv(struct qcc *qcc) |
| 1311 | { |
| 1312 | TRACE_ENTER(QC_EV_QCC_RECV, qcc->conn); |
| 1313 | /* XXX TO DO XXX */ |
| 1314 | TRACE_LEAVE(QC_EV_QCC_RECV, qcc->conn); |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1318 | static int qcs_push_frame(struct qcs *qcs, struct buffer *payload, int fin, uint64_t offset) |
| 1319 | { |
| 1320 | struct quic_frame *frm; |
| 1321 | struct buffer *buf = &qcs->tx.buf; |
| 1322 | struct quic_enc_level *qel = &qcs->qcc->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 1323 | int total = 0; |
| 1324 | |
| 1325 | qc_get_buf(qcs->qcc, buf); |
| 1326 | total = b_force_xfer(buf, payload, QUIC_MIN(b_data(payload), b_room(buf))); |
| 1327 | frm = pool_zalloc(pool_head_quic_frame); |
| 1328 | if (!frm) |
| 1329 | goto err; |
| 1330 | |
| 1331 | frm->type = QUIC_FT_STREAM_8; |
| 1332 | if (fin) |
| 1333 | frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 1334 | if (offset) { |
| 1335 | frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 1336 | frm->stream.offset.key = offset; |
| 1337 | } |
| 1338 | frm->stream.qcs = qcs; |
| 1339 | frm->stream.buf = buf; |
| 1340 | frm->stream.id = qcs->by_id.key; |
| 1341 | if (total) { |
| 1342 | frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 1343 | frm->stream.len = total; |
| 1344 | } |
| 1345 | |
| 1346 | MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list); |
| 1347 | fprintf(stderr, "%s: total=%d fin=%d offset=%lu\n", __func__, total, fin, offset); |
| 1348 | return total; |
| 1349 | |
| 1350 | err: |
| 1351 | return -1; |
| 1352 | } |
| 1353 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1354 | /* Try to send data if possible. |
| 1355 | * The function returns 1 if data have been sent, otherwise zero. |
| 1356 | */ |
| 1357 | static int qc_send(struct qcc *qcc) |
| 1358 | { |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1359 | struct qcs *qcs; |
| 1360 | struct eb64_node *node; |
| 1361 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1362 | TRACE_ENTER(QC_EV_QCC_SEND, qcc->conn); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1363 | /* TODO simple loop through all streams and check if there is frames to |
| 1364 | * send |
| 1365 | */ |
| 1366 | node = eb64_first(&qcc->streams_by_id); |
| 1367 | while (node) { |
| 1368 | struct buffer *buf; |
| 1369 | qcs = container_of(node, struct qcs, by_id); |
| 1370 | for (buf = br_head(qcs->tx.mbuf); b_data(buf); buf = br_del_head(qcs->tx.mbuf)) { |
| 1371 | if (b_data(buf)) { |
| 1372 | int ret; |
| 1373 | char fin = 0; |
| 1374 | |
| 1375 | /* if FIN is activated, ensure the buffer to |
| 1376 | * send is the last |
| 1377 | */ |
| 1378 | if (qcs->flags & QC_SF_FIN_STREAM) { |
| 1379 | BUG_ON(qcs->tx.left < b_data(buf)); |
| 1380 | fin = !(qcs->tx.left - b_data(buf)); |
| 1381 | } |
| 1382 | |
| 1383 | ret = qcs_push_frame(qcs, buf, fin, qcs->tx.offset); |
| 1384 | if (ret <= 0) |
| 1385 | ABORT_NOW(); |
| 1386 | |
| 1387 | qcs->tx.left -= ret; |
| 1388 | qcs->tx.offset += ret; |
| 1389 | qcs->qcc->wait_event.events &= ~SUB_RETRY_SEND; |
| 1390 | } |
| 1391 | b_free(buf); |
| 1392 | } |
| 1393 | node = eb64_next(node); |
| 1394 | } |
| 1395 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1396 | TRACE_LEAVE(QC_EV_QCC_SEND, qcc->conn); |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | /* this is the tasklet referenced in qcc->wait_event.tasklet */ |
| 1401 | static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status) |
| 1402 | { |
| 1403 | struct connection *conn; |
| 1404 | struct tasklet *tl = (struct tasklet *)t; |
| 1405 | int conn_in_list; |
| 1406 | struct qcc *qcc; |
| 1407 | int ret = 0; |
| 1408 | |
| 1409 | |
| 1410 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 1411 | if (t->context == NULL) { |
| 1412 | /* The connection has been taken over by another thread, |
| 1413 | * we're no longer responsible for it, so just free the |
| 1414 | * tasklet, and do nothing. |
| 1415 | */ |
| 1416 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 1417 | tasklet_free(tl); |
| 1418 | goto leave; |
| 1419 | } |
| 1420 | qcc = ctx; |
| 1421 | conn = qcc->conn; |
| 1422 | |
| 1423 | TRACE_ENTER(QC_EV_QCC_WAKE, conn); |
| 1424 | |
| 1425 | conn_in_list = conn->flags & CO_FL_LIST_MASK; |
| 1426 | |
| 1427 | /* Remove the connection from the list, to be sure nobody attempts |
| 1428 | * to use it while we handle the I/O events |
| 1429 | */ |
| 1430 | if (conn_in_list) |
| 1431 | conn_delete_from_tree(&conn->hash_node->node); |
| 1432 | |
| 1433 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 1434 | |
| 1435 | if (!(qcc->wait_event.events & SUB_RETRY_SEND)) |
| 1436 | ret = qc_send(qcc); |
| 1437 | #if 0 |
| 1438 | if (!(qcc->wait_event.events & SUB_RETRY_RECV)) |
| 1439 | ret |= qc_recv(qcc); |
| 1440 | #endif |
| 1441 | // TODO redefine the proper condition here |
| 1442 | //if (ret || qcc->rx.inmux) |
| 1443 | ret = qc_process(qcc); |
| 1444 | |
| 1445 | /* If we were in an idle list, we want to add it back into it, |
| 1446 | * unless qc_process() returned -1, which mean it has destroyed |
| 1447 | * the connection (testing !ret is enough, if qc_process() wasn't |
| 1448 | * called then ret will be 0 anyway. |
| 1449 | */ |
| 1450 | if (!ret && conn_in_list) { |
| 1451 | struct server *srv = objt_server(conn->target); |
| 1452 | |
| 1453 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 1454 | if (conn_in_list == CO_FL_SAFE_LIST) |
| 1455 | ebmb_insert(&srv->per_thr[tid].safe_conns, |
| 1456 | &conn->hash_node->node, sizeof(conn->hash_node->hash)); |
| 1457 | else |
| 1458 | ebmb_insert(&srv->per_thr[tid].idle_conns, |
| 1459 | &conn->hash_node->node, sizeof(conn->hash_node->hash)); |
| 1460 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 1461 | } |
| 1462 | |
| 1463 | leave: |
| 1464 | TRACE_LEAVE(QC_EV_QCC_WAKE); |
| 1465 | return NULL; |
| 1466 | } |
| 1467 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1468 | /* callback called on any event by the connection handler. |
| 1469 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 1470 | * destruction of the connection (which normally doesn not happen in quic). |
| 1471 | */ |
| 1472 | static int qc_process(struct qcc *qcc) |
| 1473 | { |
| 1474 | struct connection *conn = qcc->conn; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1475 | |
| 1476 | TRACE_ENTER(QC_EV_QCC_WAKE, conn); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1477 | TRACE_LEAVE(QC_EV_QCC_WAKE, conn); |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
| 1482 | static int qc_wake(struct connection *conn) |
| 1483 | { |
| 1484 | struct qcc *qcc = conn->ctx; |
| 1485 | int ret; |
| 1486 | |
| 1487 | TRACE_ENTER(QC_EV_QCC_WAKE, conn); |
| 1488 | ret = qc_process(qcc); |
| 1489 | if (ret >= 0) |
| 1490 | qc_wake_some_streams(qcc, 0); |
| 1491 | TRACE_LEAVE(QC_EV_QCC_WAKE); |
| 1492 | return ret; |
| 1493 | } |
| 1494 | |
| 1495 | /* Connection timeout management. The principle is that if there's no receipt |
| 1496 | * nor sending for a certain amount of time, the connection is closed. If the |
| 1497 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 1498 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 1499 | * GOAWAY frame. |
| 1500 | */ |
| 1501 | static struct task *qc_timeout_task(struct task *t, void *context, unsigned int state) |
| 1502 | { |
| 1503 | TRACE_ENTER(QC_EV_QCC_WAKE); |
| 1504 | /* XXX TO DO XXX */ |
| 1505 | TRACE_LEAVE(QC_EV_QCC_WAKE); |
| 1506 | return NULL; |
| 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | /*******************************************/ |
| 1511 | /* functions below are used by the streams */ |
| 1512 | /*******************************************/ |
| 1513 | |
| 1514 | /* |
| 1515 | * Attach a new stream to a connection |
| 1516 | * (Used for outgoing connections) |
| 1517 | */ |
| 1518 | static struct conn_stream *qc_attach(struct connection *conn, struct session *sess) |
| 1519 | { |
| 1520 | struct conn_stream *cs; |
| 1521 | struct qcs *qcs; |
| 1522 | struct qcc *qcc = conn->ctx; |
| 1523 | |
| 1524 | TRACE_ENTER(QC_EV_QCS_NEW, conn); |
| 1525 | cs = cs_new(conn, conn->target); |
| 1526 | if (!cs) { |
| 1527 | TRACE_DEVEL("leaving on CS allocation failure", QC_EV_QCS_NEW|QC_EV_QCS_ERR, conn); |
| 1528 | return NULL; |
| 1529 | } |
| 1530 | qcs = qcc_bck_stream_new(qcc, QCS_BIDI, cs, sess); |
| 1531 | if (!qcs) { |
| 1532 | TRACE_DEVEL("leaving on stream creation failure", QC_EV_QCS_NEW|QC_EV_QCS_ERR, conn); |
| 1533 | cs_free(cs); |
| 1534 | return NULL; |
| 1535 | } |
| 1536 | TRACE_LEAVE(QC_EV_QCS_NEW, conn, qcs); |
| 1537 | return cs; |
| 1538 | } |
| 1539 | |
| 1540 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 1541 | * We have to scan because we may have some orphan streams. It might be |
| 1542 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 1543 | * orphans. |
| 1544 | */ |
| 1545 | static const struct conn_stream *qc_get_first_cs(const struct connection *conn) |
| 1546 | { |
| 1547 | struct qcc *qcc = conn->ctx; |
| 1548 | struct qcs *qcs; |
| 1549 | struct eb64_node *node; |
| 1550 | |
| 1551 | node = eb64_first(&qcc->streams_by_id); |
| 1552 | while (node) { |
| 1553 | qcs = container_of(node, struct qcs, by_id); |
| 1554 | if (qcs->cs) |
| 1555 | return qcs->cs; |
| 1556 | node = eb64_next(node); |
| 1557 | } |
| 1558 | return NULL; |
| 1559 | } |
| 1560 | |
| 1561 | static int qc_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 1562 | { |
| 1563 | int ret = 0; |
| 1564 | struct qcc *qcc = conn->ctx; |
| 1565 | |
| 1566 | switch (mux_ctl) { |
| 1567 | case MUX_STATUS: |
| 1568 | /* Only consider the mux to be ready if we had no error. */ |
| 1569 | if (qcc->st0 < QC_CS_ERROR) |
| 1570 | ret |= MUX_STATUS_READY; |
| 1571 | return ret; |
| 1572 | case MUX_EXIT_STATUS: |
| 1573 | return MUX_ES_UNKNOWN; |
| 1574 | default: |
| 1575 | return -1; |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * Destroy the mux and the associated connection, if it is no longer used |
| 1581 | */ |
| 1582 | static void qc_destroy(void *ctx) |
| 1583 | { |
| 1584 | struct qcc *qcc = ctx; |
| 1585 | |
| 1586 | TRACE_ENTER(QC_EV_QCC_END, qcc->conn); |
| 1587 | if (eb_is_empty(&qcc->streams_by_id) || !qcc->conn || qcc->conn->ctx != qcc) |
| 1588 | qc_release(qcc); |
| 1589 | TRACE_LEAVE(QC_EV_QCC_END); |
| 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | * Detach the stream from the connection and possibly release the connection. |
| 1594 | */ |
| 1595 | static void qc_detach(struct conn_stream *cs) |
| 1596 | { |
| 1597 | struct qcs *qcs = cs->ctx; |
| 1598 | |
| 1599 | TRACE_ENTER(QC_EV_STRM_END, qcs ? qcs->qcc->conn : NULL, qcs); |
| 1600 | /* XXX TO DO XXX */ |
| 1601 | TRACE_LEAVE(QC_EV_STRM_END, qcs ? qcs->qcc->conn : NULL); |
| 1602 | } |
| 1603 | |
| 1604 | /* Performs a synchronous or asynchronous shutr(). */ |
| 1605 | static void qc_do_shutr(struct qcs *qcs) |
| 1606 | { |
| 1607 | struct qcc *qcc = qcs->qcc; |
| 1608 | |
| 1609 | TRACE_ENTER(QC_EV_STRM_SHUT, qcc->conn, qcs); |
| 1610 | /* XXX TO DO XXX */ |
| 1611 | TRACE_LEAVE(QC_EV_STRM_SHUT, qcc->conn, qcs); |
| 1612 | return; |
| 1613 | } |
| 1614 | |
| 1615 | /* Performs a synchronous or asynchronous shutw(). */ |
| 1616 | static void qc_do_shutw(struct qcs *qcs) |
| 1617 | { |
| 1618 | struct qcc *qcc = qcs->qcc; |
| 1619 | |
| 1620 | TRACE_ENTER(QC_EV_STRM_SHUT, qcc->conn, qcs); |
| 1621 | /* XXX TO DO XXX */ |
| 1622 | TRACE_LEAVE(QC_EV_STRM_SHUT, qcc->conn, qcs); |
| 1623 | return; |
| 1624 | } |
| 1625 | |
| 1626 | /* This is the tasklet referenced in qcs->shut_tl, it is used for |
| 1627 | * deferred shutdowns when the qc_detach() was done but the mux buffer was full |
| 1628 | * and prevented the last frame from being emitted. |
| 1629 | */ |
| 1630 | static struct task *qc_deferred_shut(struct task *t, void *ctx, unsigned int state) |
| 1631 | { |
| 1632 | struct qcs *qcs = ctx; |
| 1633 | struct qcc *qcc = qcs->qcc; |
| 1634 | |
| 1635 | TRACE_ENTER(QC_EV_STRM_SHUT, qcc->conn, qcs); |
| 1636 | |
| 1637 | if (qcs->flags & QC_SF_NOTIFIED) { |
| 1638 | /* some data processing remains to be done first */ |
| 1639 | goto end; |
| 1640 | } |
| 1641 | |
| 1642 | if (qcs->flags & QC_SF_WANT_SHUTW) |
| 1643 | qc_do_shutw(qcs); |
| 1644 | |
| 1645 | if (qcs->flags & QC_SF_WANT_SHUTR) |
| 1646 | qc_do_shutr(qcs); |
| 1647 | |
| 1648 | if (!(qcs->flags & (QC_SF_WANT_SHUTR|QC_SF_WANT_SHUTW))) { |
| 1649 | /* We're done trying to send, remove ourself from the send_list */ |
| 1650 | LIST_DEL_INIT(&qcs->list); |
| 1651 | |
| 1652 | if (!qcs->cs) { |
| 1653 | qcs_destroy(qcs); |
| 1654 | if (qcc_is_dead(qcc)) |
| 1655 | qc_release(qcc); |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | end: |
| 1660 | TRACE_LEAVE(QC_EV_STRM_SHUT); |
| 1661 | return NULL; |
| 1662 | } |
| 1663 | |
| 1664 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
| 1665 | static void qc_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 1666 | { |
| 1667 | |
| 1668 | struct qcs *qcs = cs->ctx; |
| 1669 | |
| 1670 | TRACE_ENTER(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs); |
| 1671 | if (cs->flags & CS_FL_KILL_CONN) |
| 1672 | qcs->flags |= QC_SF_KILL_CONN; |
| 1673 | |
| 1674 | if (mode) |
| 1675 | qc_do_shutr(qcs); |
| 1676 | |
| 1677 | TRACE_LEAVE(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs); |
| 1678 | } |
| 1679 | |
| 1680 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
| 1681 | static void qc_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 1682 | { |
| 1683 | struct qcs *qcs = cs->ctx; |
| 1684 | |
| 1685 | TRACE_ENTER(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs); |
| 1686 | if (cs->flags & CS_FL_KILL_CONN) |
| 1687 | qcs->flags |= QC_SF_KILL_CONN; |
| 1688 | |
| 1689 | qc_do_shutw(qcs); |
| 1690 | TRACE_LEAVE(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs); |
| 1691 | } |
| 1692 | |
| 1693 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 1694 | * event subscriber <es> is not allowed to change from a previous call as long |
| 1695 | * as at least one event is still subscribed. The <event_type> must only be a |
| 1696 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 1697 | */ |
| 1698 | static int qc_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
| 1699 | { |
| 1700 | struct qcs *qcs = cs->ctx; |
| 1701 | struct qcc *qcc = qcs->qcc; |
| 1702 | |
| 1703 | TRACE_ENTER(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcc->conn, qcs); |
| 1704 | |
| 1705 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 1706 | BUG_ON(qcs->subs && qcs->subs != es); |
| 1707 | |
| 1708 | es->events |= event_type; |
| 1709 | qcs->subs = es; |
| 1710 | |
| 1711 | if (event_type & SUB_RETRY_RECV) |
| 1712 | TRACE_DEVEL("subscribe(recv)", QC_EV_STRM_RECV, qcc->conn, qcs); |
| 1713 | |
| 1714 | if (event_type & SUB_RETRY_SEND) { |
| 1715 | TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcc->conn, qcs); |
| 1716 | if (!(qcs->flags & QC_SF_BLK_SFCTL) && |
| 1717 | !LIST_INLIST(&qcs->list)) { |
| 1718 | if (qcs->flags & QC_SF_BLK_MFCTL) |
| 1719 | LIST_APPEND(&qcc->fctl_list, &qcs->list); |
| 1720 | else |
| 1721 | LIST_APPEND(&qcc->send_list, &qcs->list); |
| 1722 | } |
| 1723 | } |
| 1724 | TRACE_LEAVE(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcc->conn, qcs); |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 1729 | * The <es> pointer is not allowed to differ from the one passed to the |
| 1730 | * subscribe() call. It always returns zero. |
| 1731 | */ |
| 1732 | static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
| 1733 | { |
| 1734 | struct qcs *qcs = cs->ctx; |
| 1735 | |
| 1736 | TRACE_ENTER(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcs->qcc->conn, qcs); |
| 1737 | |
| 1738 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 1739 | BUG_ON(qcs->subs && qcs->subs != es); |
| 1740 | |
| 1741 | es->events &= ~event_type; |
| 1742 | if (!es->events) |
| 1743 | qcs->subs = NULL; |
| 1744 | |
| 1745 | if (event_type & SUB_RETRY_RECV) |
| 1746 | TRACE_DEVEL("unsubscribe(recv)", QC_EV_STRM_RECV, qcs->qcc->conn, qcs); |
| 1747 | |
| 1748 | if (event_type & SUB_RETRY_SEND) { |
| 1749 | TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcs->qcc->conn, qcs); |
| 1750 | qcs->flags &= ~QC_SF_NOTIFIED; |
| 1751 | if (!(qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW))) |
| 1752 | LIST_DEL_INIT(&qcs->list); |
| 1753 | } |
| 1754 | |
| 1755 | TRACE_LEAVE(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcs->qcc->conn, qcs); |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | |
| 1760 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 1761 | * event subscriber <es> is not allowed to change from a previous call as long |
| 1762 | * as at least one event is still subscribed. The <event_type> must only be a |
| 1763 | * SUB_RETRY_RECV. It always returns 0. |
| 1764 | */ |
| 1765 | static int ruqs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es) |
| 1766 | { |
| 1767 | struct qcc *qcc = qcs->qcc; |
| 1768 | |
| 1769 | TRACE_ENTER(QC_EV_STRM_RECV, qcc->conn, qcs); |
| 1770 | |
| 1771 | BUG_ON(event_type & ~SUB_RETRY_RECV); |
| 1772 | BUG_ON(qcs->subs && qcs->subs != es); |
| 1773 | |
| 1774 | es->events |= event_type; |
| 1775 | qcs->subs = es; |
| 1776 | |
| 1777 | if (event_type & SUB_RETRY_RECV) |
| 1778 | TRACE_DEVEL("subscribe(recv)", QC_EV_STRM_RECV, qcc->conn, qcs); |
| 1779 | |
| 1780 | TRACE_LEAVE(QC_EV_STRM_RECV, qcc->conn, qcs); |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 1785 | * The <es> pointer is not allowed to differ from the one passed to the |
| 1786 | * subscribe() call. It always returns zero. |
| 1787 | */ |
| 1788 | static int ruqs_unsubscribe(struct qcs *qcs, int event_type, struct wait_event *es) |
| 1789 | { |
| 1790 | TRACE_ENTER(QC_EV_STRM_RECV, qcs->qcc->conn, qcs); |
| 1791 | |
| 1792 | BUG_ON(event_type & ~SUB_RETRY_RECV); |
| 1793 | BUG_ON(qcs->subs && qcs->subs != es); |
| 1794 | |
| 1795 | es->events &= ~event_type; |
| 1796 | if (!es->events) |
| 1797 | qcs->subs = NULL; |
| 1798 | |
| 1799 | if (event_type & SUB_RETRY_RECV) |
| 1800 | TRACE_DEVEL("unsubscribe(recv)", QC_EV_STRM_RECV, qcs->qcc->conn, qcs); |
| 1801 | |
| 1802 | TRACE_LEAVE(QC_EV_STRM_RECV, qcs->qcc->conn, qcs); |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
| 1806 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 1807 | * event subscriber <es> is not allowed to change from a previous call as long |
| 1808 | * as at least one event is still subscribed. The <event_type> must only be |
| 1809 | * SUB_RETRY_SEND. It always returns 0. |
| 1810 | */ |
| 1811 | static int luqs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es) |
| 1812 | { |
| 1813 | struct qcc *qcc = qcs->qcc; |
| 1814 | |
| 1815 | TRACE_ENTER(QC_EV_STRM_SEND, qcc->conn, qcs); |
| 1816 | |
| 1817 | BUG_ON(event_type & ~SUB_RETRY_SEND); |
| 1818 | BUG_ON(qcs->subs && qcs->subs != es); |
| 1819 | |
| 1820 | es->events |= event_type; |
| 1821 | qcs->subs = es; |
| 1822 | |
| 1823 | if (event_type & SUB_RETRY_SEND) { |
| 1824 | TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcc->conn, qcs); |
| 1825 | if (!(qcs->flags & QC_SF_BLK_SFCTL) && |
| 1826 | !LIST_INLIST(&qcs->list)) { |
| 1827 | if (qcs->flags & QC_SF_BLK_MFCTL) |
| 1828 | LIST_APPEND(&qcc->fctl_list, &qcs->list); |
| 1829 | else |
| 1830 | LIST_APPEND(&qcc->send_list, &qcs->list); |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | TRACE_LEAVE(QC_EV_STRM_SEND, qcc->conn, qcs); |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
| 1838 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 1839 | * The <es> pointer is not allowed to differ from the one passed to the |
| 1840 | * subscribe() call. It always returns zero. |
| 1841 | */ |
| 1842 | static int luqs_unsubscribe(struct qcs *qcs, int event_type, struct wait_event *es) |
| 1843 | { |
| 1844 | struct qcc *qcc = qcs->qcc; |
| 1845 | |
| 1846 | TRACE_ENTER(QC_EV_STRM_SEND, qcc->conn, qcs); |
| 1847 | |
| 1848 | BUG_ON(event_type & ~SUB_RETRY_SEND); |
| 1849 | BUG_ON(qcs->subs && qcs->subs != es); |
| 1850 | |
| 1851 | es->events &= ~event_type; |
| 1852 | if (!es->events) |
| 1853 | qcs->subs = NULL; |
| 1854 | |
| 1855 | if (event_type & SUB_RETRY_SEND) { |
| 1856 | TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcc->conn, qcs); |
| 1857 | qcs->flags &= ~QC_SF_NOTIFIED; |
| 1858 | if (!(qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW))) |
| 1859 | LIST_DEL_INIT(&qcs->list); |
| 1860 | } |
| 1861 | |
| 1862 | TRACE_LEAVE(QC_EV_STRM_SEND, qcc->conn, qcs); |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
| 1866 | /* Called from the upper layer, to receive data */ |
| 1867 | static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 1868 | { |
| 1869 | struct qcs *qcs = cs->ctx; |
| 1870 | struct qcc *qcc = qcs->qcc; |
| 1871 | int ret; |
| 1872 | |
| 1873 | ret = 0; |
| 1874 | TRACE_ENTER(QC_EV_STRM_RECV, qcc->conn, qcs); |
| 1875 | /* XXX TO DO XXX */ |
| 1876 | TRACE_LEAVE(QC_EV_STRM_RECV, qcc->conn, qcs); |
| 1877 | return ret; |
| 1878 | } |
| 1879 | |
| 1880 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 1881 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 1882 | * flags may be updated on the conn_stream. |
| 1883 | */ |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1884 | size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1885 | { |
| 1886 | struct qcs *qcs = cs->ctx; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1887 | |
| 1888 | TRACE_ENTER(QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn, qcs); |
| 1889 | |
| 1890 | if (count) { |
| 1891 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 1892 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 1893 | } |
| 1894 | |
| 1895 | TRACE_LEAVE(QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1896 | return count; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 1900 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 1901 | * flags may be updated on the outgoing uni-stream. |
| 1902 | */ |
| 1903 | __maybe_unused |
| 1904 | static size_t _qcs_snd_buf(struct qcs *qcs, struct buffer *buf, size_t count, int flags) |
| 1905 | { |
| 1906 | size_t total = 0; |
| 1907 | struct qcc *qcc = qcs->qcc; |
| 1908 | struct buffer *res; |
| 1909 | struct quic_tx_frm *frm; |
| 1910 | |
| 1911 | TRACE_ENTER(QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn); |
| 1912 | |
| 1913 | if (!count) |
| 1914 | goto out; |
| 1915 | |
| 1916 | res = br_tail(qcc->mbuf); |
| 1917 | if (!qc_get_buf(qcc, res)) { |
| 1918 | qcc->flags |= QC_CF_MUX_MALLOC; |
| 1919 | goto out; |
| 1920 | } |
| 1921 | |
| 1922 | while (count) { |
| 1923 | size_t try, room; |
| 1924 | |
| 1925 | room = b_room(res); |
| 1926 | if (!room) { |
| 1927 | if ((res = br_tail_add(qcc->mbuf)) != NULL) |
| 1928 | continue; |
| 1929 | |
| 1930 | qcc->flags |= QC_CF_MUX_MALLOC; |
| 1931 | break; |
| 1932 | } |
| 1933 | |
| 1934 | try = count; |
| 1935 | if (try > room) |
| 1936 | try = room; |
| 1937 | |
| 1938 | total += b_xfer(res, buf, try); |
| 1939 | count -= try; |
| 1940 | } |
| 1941 | |
| 1942 | if (total) { |
| 1943 | |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1944 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1945 | if (!frm) { /* XXX XXX */ } |
| 1946 | } |
| 1947 | |
| 1948 | out: |
| 1949 | TRACE_LEAVE(QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn); |
| 1950 | return total; |
| 1951 | |
| 1952 | err: |
| 1953 | TRACE_DEVEL("leaving on stream error", QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn); |
| 1954 | return total; |
| 1955 | } |
| 1956 | |
| 1957 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 1958 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 1959 | * flags may be updated on the mux. |
| 1960 | */ |
| 1961 | size_t luqs_snd_buf(struct qcs *qcs, struct buffer *buf, size_t count, int flags) |
| 1962 | { |
| 1963 | size_t room, total = 0; |
| 1964 | struct qcc *qcc = qcs->qcc; |
| 1965 | struct buffer *res; |
| 1966 | |
| 1967 | TRACE_ENTER(QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn); |
| 1968 | if (!count) |
| 1969 | goto out; |
| 1970 | |
| 1971 | res = &qcs->tx.buf; |
| 1972 | if (!qc_get_buf(qcc, res)) { |
| 1973 | qcc->flags |= QC_CF_MUX_MALLOC; |
| 1974 | goto out; |
| 1975 | } |
| 1976 | |
| 1977 | room = b_room(res); |
| 1978 | if (!room) |
| 1979 | goto out; |
| 1980 | |
| 1981 | if (count > room) |
| 1982 | count = room; |
| 1983 | |
| 1984 | total += b_xfer(res, buf, count); |
Amaury Denoyelle | 7b1d3d6 | 2021-08-27 15:05:29 +0200 | [diff] [blame] | 1985 | qcs_push_frame(qcs, res, 0, 0); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1986 | |
| 1987 | out: |
| 1988 | TRACE_LEAVE(QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn); |
| 1989 | return total; |
| 1990 | |
| 1991 | err: |
| 1992 | TRACE_DEVEL("leaving on stream error", QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn); |
| 1993 | return total; |
| 1994 | } |
| 1995 | |
| 1996 | /* for debugging with CLI's "show fd" command */ |
| 1997 | static int qc_show_fd(struct buffer *msg, struct connection *conn) |
| 1998 | { |
| 1999 | struct qcc *qcc = conn->ctx; |
| 2000 | struct qcs *qcs = NULL; |
| 2001 | struct eb64_node *node; |
| 2002 | int fctl_cnt = 0; |
| 2003 | int send_cnt = 0; |
| 2004 | int tree_cnt = 0; |
| 2005 | int orph_cnt = 0; |
| 2006 | struct buffer *hmbuf, *tmbuf; |
| 2007 | |
| 2008 | if (!qcc) |
| 2009 | return 0; |
| 2010 | |
| 2011 | list_for_each_entry(qcs, &qcc->fctl_list, list) |
| 2012 | fctl_cnt++; |
| 2013 | |
| 2014 | list_for_each_entry(qcs, &qcc->send_list, list) |
| 2015 | send_cnt++; |
| 2016 | |
| 2017 | qcs = NULL; |
| 2018 | node = eb64_first(&qcc->streams_by_id); |
| 2019 | while (node) { |
| 2020 | qcs = container_of(node, struct qcs, by_id); |
| 2021 | tree_cnt++; |
| 2022 | if (!qcs->cs) |
| 2023 | orph_cnt++; |
| 2024 | node = eb64_next(node); |
| 2025 | } |
| 2026 | |
| 2027 | hmbuf = br_head(qcc->mbuf); |
| 2028 | tmbuf = br_tail(qcc->mbuf); |
| 2029 | chunk_appendf(msg, " qcc.st0=%s .flg=0x%04x" |
| 2030 | " clt.nb_streams_bidi=%llu srv.nb_streams_bidi=%llu" |
| 2031 | " clt.nb_streams_uni=%llu srv.nb_streams_uni=%llu" |
| 2032 | " .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 2033 | " .orph_cnt=%d .sub=%d" |
| 2034 | " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]", |
| 2035 | qcc_st_to_str(qcc->st0), qcc->flags, |
| 2036 | (unsigned long long)qcc->strms[QCS_CLT_BIDI].nb_streams, |
| 2037 | (unsigned long long)qcc->strms[QCS_SRV_BIDI].nb_streams, |
| 2038 | (unsigned long long)qcc->strms[QCS_CLT_UNI].nb_streams, |
| 2039 | (unsigned long long)qcc->strms[QCS_SRV_UNI].nb_streams, |
| 2040 | qcc->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt, |
| 2041 | qcc->wait_event.events, |
| 2042 | br_head_idx(qcc->mbuf), br_tail_idx(qcc->mbuf), br_size(qcc->mbuf), |
| 2043 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 2044 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
| 2045 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 2046 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
| 2047 | |
| 2048 | if (qcs) { |
| 2049 | chunk_appendf(msg, " last_qcs=%p .id=%llu rx.st=%s tx.st=%s .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 2050 | qcs, (unsigned long long)qcs->id, |
| 2051 | qcs_rx_st_to_str(qcs->rx.st), qcs_tx_st_to_str(qcs->tx.st), qcs->flags, |
| 2052 | (unsigned int)b_data(&qcs->rx.buf), b_orig(&qcs->rx.buf), |
| 2053 | (unsigned int)b_head_ofs(&qcs->rx.buf), (unsigned int)b_size(&qcs->rx.buf), |
| 2054 | qcs->cs); |
| 2055 | if (qcs->cs) |
| 2056 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 2057 | qcs->cs->flags, qcs->cs->data); |
| 2058 | } |
| 2059 | |
| 2060 | return 0; |
| 2061 | } |
| 2062 | |
| 2063 | /* Migrate the the connection to the current thread. |
| 2064 | * Return 0 if successful, non-zero otherwise. |
| 2065 | * Expected to be called with the old thread lock held. |
| 2066 | */ |
| 2067 | static int qc_takeover(struct connection *conn, int orig_tid) |
| 2068 | { |
| 2069 | struct qcc *qcc = conn->ctx; |
| 2070 | struct task *task; |
| 2071 | |
| 2072 | if (fd_takeover(conn->handle.fd, conn) != 0) |
| 2073 | return -1; |
| 2074 | |
| 2075 | if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) { |
| 2076 | /* We failed to takeover the xprt, even if the connection may |
| 2077 | * still be valid, flag it as error'd, as we have already |
| 2078 | * taken over the fd, and wake the tasklet, so that it will |
| 2079 | * destroy it. |
| 2080 | */ |
| 2081 | conn->flags |= CO_FL_ERROR; |
| 2082 | tasklet_wakeup_on(qcc->wait_event.tasklet, orig_tid); |
| 2083 | return -1; |
| 2084 | } |
| 2085 | |
| 2086 | if (qcc->wait_event.events) |
| 2087 | qcc->conn->xprt->unsubscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 2088 | qcc->wait_event.events, &qcc->wait_event); |
| 2089 | /* To let the tasklet know it should free itself, and do nothing else, |
| 2090 | * set its context to NULL. |
| 2091 | */ |
| 2092 | qcc->wait_event.tasklet->context = NULL; |
| 2093 | tasklet_wakeup_on(qcc->wait_event.tasklet, orig_tid); |
| 2094 | |
| 2095 | task = qcc->task; |
| 2096 | if (task) { |
| 2097 | task->context = NULL; |
| 2098 | qcc->task = NULL; |
| 2099 | __ha_barrier_store(); |
| 2100 | task_kill(task); |
| 2101 | |
| 2102 | qcc->task = task_new(tid_bit); |
| 2103 | if (!qcc->task) { |
| 2104 | qc_release(qcc); |
| 2105 | return -1; |
| 2106 | } |
| 2107 | |
| 2108 | qcc->task->process = qc_timeout_task; |
| 2109 | qcc->task->context = qcc; |
| 2110 | } |
| 2111 | |
| 2112 | qcc->wait_event.tasklet = tasklet_new(); |
| 2113 | if (!qcc->wait_event.tasklet) { |
| 2114 | qc_release(qcc); |
| 2115 | return -1; |
| 2116 | } |
| 2117 | |
| 2118 | qcc->wait_event.tasklet->process = qc_io_cb; |
| 2119 | qcc->wait_event.tasklet->context = qcc; |
| 2120 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 2121 | SUB_RETRY_RECV, &qcc->wait_event); |
| 2122 | |
| 2123 | return 0; |
| 2124 | } |
| 2125 | |
| 2126 | /****************************************/ |
| 2127 | /* MUX initialization and instantiation */ |
| 2128 | /***************************************/ |
| 2129 | |
| 2130 | /* The mux operations */ |
| 2131 | static const struct mux_ops qc_ops = { |
| 2132 | .init = qc_init, |
| 2133 | .wake = qc_wake, |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 2134 | //.snd_buf = qc_snd_buf, |
| 2135 | .snd_buf = h3_snd_buf, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2136 | .rcv_buf = qc_rcv_buf, |
| 2137 | .subscribe = qc_subscribe, |
| 2138 | .unsubscribe = qc_unsubscribe, |
| 2139 | .ruqs_subscribe = ruqs_subscribe, |
| 2140 | .ruqs_unsubscribe = ruqs_unsubscribe, |
| 2141 | .luqs_subscribe = luqs_subscribe, |
| 2142 | .luqs_unsubscribe = luqs_unsubscribe, |
| 2143 | .attach = qc_attach, |
| 2144 | .get_first_cs = qc_get_first_cs, |
| 2145 | .detach = qc_detach, |
| 2146 | .destroy = qc_destroy, |
| 2147 | .avail_streams_bidi = qc_avail_streams_bidi, |
| 2148 | .avail_streams_uni = qc_avail_streams_uni, |
| 2149 | .used_streams = qc_used_streams, |
| 2150 | .shutr = qc_shutr, |
| 2151 | .shutw = qc_shutw, |
| 2152 | .ctl = qc_ctl, |
| 2153 | .show_fd = qc_show_fd, |
| 2154 | .takeover = qc_takeover, |
| 2155 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX|MX_FL_HOL_RISK, |
| 2156 | .name = "QUIC", |
| 2157 | }; |
| 2158 | |
| 2159 | static struct mux_proto_list mux_proto_quic = |
| 2160 | { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &qc_ops }; |
| 2161 | |
| 2162 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic); |
| 2163 | |