Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1 | #include <haproxy/mux_quic.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 3 | #include <import/eb64tree.h> |
| 4 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 5 | #include <haproxy/api.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 6 | #include <haproxy/connection.h> |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 7 | #include <haproxy/dynbuf.h> |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 8 | #include <haproxy/htx.h> |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 9 | #include <haproxy/list.h> |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 10 | #include <haproxy/ncbuf.h> |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 11 | #include <haproxy/pool.h> |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 12 | #include <haproxy/quic_stream.h> |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 13 | #include <haproxy/quic_tp-t.h> |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 14 | #include <haproxy/ssl_sock-t.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 15 | #include <haproxy/stconn.h> |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 16 | #include <haproxy/trace.h> |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 17 | #include <haproxy/xprt_quic.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 18 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 19 | DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc)); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 20 | DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs)); |
| 21 | |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 22 | /* trace source and events */ |
| 23 | static void qmux_trace(enum trace_level level, uint64_t mask, |
| 24 | const struct trace_source *src, |
| 25 | const struct ist where, const struct ist func, |
| 26 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 27 | |
| 28 | static const struct trace_event qmux_trace_events[] = { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 29 | #define QMUX_EV_QCC_NEW (1ULL << 0) |
| 30 | { .mask = QMUX_EV_QCC_NEW , .name = "qcc_new", .desc = "new QUIC connection" }, |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 31 | #define QMUX_EV_QCC_RECV (1ULL << 1) |
| 32 | { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" }, |
| 33 | #define QMUX_EV_QCC_SEND (1ULL << 2) |
| 34 | { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" }, |
| 35 | #define QMUX_EV_QCC_WAKE (1ULL << 3) |
| 36 | { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" }, |
| 37 | #define QMUX_EV_QCC_END (1ULL << 4) |
| 38 | { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" }, |
| 39 | #define QMUX_EV_QCC_NQCS (1ULL << 5) |
| 40 | { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" }, |
| 41 | #define QMUX_EV_QCS_NEW (1ULL << 6) |
| 42 | { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" }, |
| 43 | #define QMUX_EV_QCS_RECV (1ULL << 7) |
| 44 | { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" }, |
| 45 | #define QMUX_EV_QCS_SEND (1ULL << 8) |
| 46 | { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" }, |
| 47 | #define QMUX_EV_QCS_END (1ULL << 9) |
| 48 | { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" }, |
| 49 | #define QMUX_EV_STRM_RECV (1ULL << 10) |
| 50 | { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" }, |
| 51 | #define QMUX_EV_STRM_SEND (1ULL << 11) |
| 52 | { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" }, |
| 53 | #define QMUX_EV_STRM_END (1ULL << 12) |
| 54 | { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" }, |
Amaury Denoyelle | fa29f33 | 2022-03-25 09:09:40 +0100 | [diff] [blame] | 55 | #define QMUX_EV_SEND_FRM (1ULL << 13) |
| 56 | { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" }, |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 57 | /* special event dedicated to qcs_xfer_data */ |
| 58 | #define QMUX_EV_QCS_XFER_DATA (1ULL << 14) |
| 59 | { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" }, |
| 60 | /* special event dedicated to qcs_build_stream_frm */ |
| 61 | #define QMUX_EV_QCS_BUILD_STRM (1ULL << 15) |
| 62 | { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" }, |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 63 | #define QMUX_EV_PROTO_ERR (1ULL << 16) |
| 64 | { .mask = QMUX_EV_PROTO_ERR, .name = "proto_err", .desc = "protocol error" }, |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 65 | { } |
| 66 | }; |
| 67 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 68 | /* custom arg for QMUX_EV_QCS_XFER_DATA */ |
| 69 | struct qcs_xfer_data_trace_arg { |
| 70 | size_t prep; |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 71 | int xfer; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 72 | }; |
| 73 | /* custom arg for QMUX_EV_QCS_BUILD_STRM */ |
| 74 | struct qcs_build_stream_trace_arg { |
| 75 | size_t len; |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 76 | char fin; |
| 77 | uint64_t offset; |
| 78 | }; |
| 79 | |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 80 | static const struct name_desc qmux_trace_lockon_args[4] = { |
| 81 | /* arg1 */ { /* already used by the connection */ }, |
| 82 | /* arg2 */ { .name="qcs", .desc="QUIC stream" }, |
| 83 | /* arg3 */ { }, |
| 84 | /* arg4 */ { } |
| 85 | }; |
| 86 | |
| 87 | static const struct name_desc qmux_trace_decoding[] = { |
| 88 | #define QMUX_VERB_CLEAN 1 |
| 89 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 90 | #define QMUX_VERB_MINIMAL 2 |
| 91 | { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" }, |
| 92 | { /* end */ } |
| 93 | }; |
| 94 | |
| 95 | struct trace_source trace_qmux = { |
| 96 | .name = IST("qmux"), |
| 97 | .desc = "QUIC multiplexer", |
| 98 | .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */ |
| 99 | .default_cb = qmux_trace, |
| 100 | .known_events = qmux_trace_events, |
| 101 | .lockon_args = qmux_trace_lockon_args, |
| 102 | .decoding = qmux_trace_decoding, |
| 103 | .report_events = ~0, /* report everything by default */ |
| 104 | }; |
| 105 | |
| 106 | #define TRACE_SOURCE &trace_qmux |
| 107 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 108 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 109 | /* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 110 | * send/receive operations. |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 111 | */ |
| 112 | static void qcc_emit_cc(struct qcc *qcc, int err) |
| 113 | { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 114 | TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn); |
| 115 | |
| 116 | TRACE_STATE("set CONNECTION_CLOSE on quic-conn", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 117 | quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err)); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 118 | qcc->flags |= QC_CF_CC_EMIT; |
| 119 | tasklet_wakeup(qcc->wait_event.tasklet); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 120 | |
| 121 | TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 124 | /* Allocate a new QUIC streams with id <id> and type <type>. */ |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 125 | static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 126 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 127 | struct qcs *qcs; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 128 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 129 | TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn); |
| 130 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 131 | qcs = pool_alloc(pool_head_qcs); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 132 | if (!qcs) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 133 | TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn); |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 134 | return NULL; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 135 | } |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 136 | |
| 137 | qcs->stream = NULL; |
| 138 | qcs->qcc = qcc; |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 139 | qcs->sd = NULL; |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 140 | qcs->flags = QC_SF_NONE; |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 141 | qcs->st = QC_SS_IDLE; |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 142 | qcs->ctx = NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 143 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 144 | /* App callback attach may register the stream for http-request wait. |
| 145 | * These fields must be initialed before. |
| 146 | */ |
| 147 | LIST_INIT(&qcs->el_opening); |
| 148 | qcs->start = TICK_ETERNITY; |
| 149 | |
Amaury Denoyelle | 93fba32 | 2022-05-24 16:53:14 +0200 | [diff] [blame] | 150 | /* Allocate transport layer stream descriptor. Only needed for TX. */ |
| 151 | if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) { |
| 152 | struct quic_conn *qc = qcc->conn->handle.qc; |
| 153 | qcs->stream = qc_stream_desc_new(id, type, qcs, qc); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 154 | if (!qcs->stream) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 155 | TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | 93fba32 | 2022-05-24 16:53:14 +0200 | [diff] [blame] | 156 | goto err; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 157 | } |
Amaury Denoyelle | 93fba32 | 2022-05-24 16:53:14 +0200 | [diff] [blame] | 158 | } |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 159 | |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 160 | qcs->id = qcs->by_id.key = id; |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 161 | if (qcc->app_ops->attach) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 162 | if (qcc->app_ops->attach(qcs, qcc->ctx)) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 163 | TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 164 | goto err; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 165 | } |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 166 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 167 | |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 168 | /* store transport layer stream descriptor in qcc tree */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 169 | eb64_insert(&qcc->streams_by_id, &qcs->by_id); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 170 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 171 | qcc->strms[type].nb_streams++; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 172 | |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 173 | /* If stream is local, use peer remote-limit, or else the opposite. */ |
| 174 | /* TODO use uni limit for unidirectional streams */ |
| 175 | qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r : |
| 176 | qcc->rfctl.msd_bidi_l; |
| 177 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 178 | qcs->rx.ncbuf = NCBUF_NULL; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 179 | qcs->rx.app_buf = BUF_NULL; |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 180 | qcs->rx.offset = qcs->rx.offset_max = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 181 | |
Amaury Denoyelle | 44d0912 | 2022-04-26 11:21:10 +0200 | [diff] [blame] | 182 | /* TODO use uni limit for unidirectional streams */ |
| 183 | qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l : |
| 184 | qcc->lfctl.msd_bidi_r; |
Amaury Denoyelle | a977355 | 2022-05-16 14:38:25 +0200 | [diff] [blame] | 185 | qcs->rx.msd_init = qcs->rx.msd; |
Amaury Denoyelle | 44d0912 | 2022-04-26 11:21:10 +0200 | [diff] [blame] | 186 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 187 | qcs->tx.buf = BUF_NULL; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 188 | qcs->tx.offset = 0; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 189 | qcs->tx.sent_offset = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 190 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 191 | qcs->wait_event.tasklet = NULL; |
| 192 | qcs->wait_event.events = 0; |
| 193 | qcs->subs = NULL; |
| 194 | |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 195 | qcs->err = 0; |
| 196 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 197 | out: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 198 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 199 | return qcs; |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 200 | |
| 201 | err: |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 202 | if (qcs->ctx && qcc->app_ops->detach) |
| 203 | qcc->app_ops->detach(qcs); |
| 204 | |
Amaury Denoyelle | 93fba32 | 2022-05-24 16:53:14 +0200 | [diff] [blame] | 205 | qc_stream_desc_release(qcs->stream); |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 206 | pool_free(pool_head_qcs, qcs); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 207 | |
| 208 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 209 | return NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 212 | static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf) |
| 213 | { |
| 214 | struct buffer buf; |
| 215 | |
Amaury Denoyelle | 7313f5e | 2022-05-17 18:53:21 +0200 | [diff] [blame] | 216 | if (ncb_is_null(ncbuf)) |
| 217 | return; |
| 218 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 219 | buf = b_make(ncbuf->area, ncbuf->size, 0, 0); |
| 220 | b_free(&buf); |
Amaury Denoyelle | 7313f5e | 2022-05-17 18:53:21 +0200 | [diff] [blame] | 221 | offer_buffers(NULL, 1); |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 222 | |
| 223 | *ncbuf = NCBUF_NULL; |
| 224 | } |
| 225 | |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 226 | /* Free a qcs. This function must only be done to remove a stream on allocation |
| 227 | * error or connection shutdown. Else use qcs_destroy which handle all the |
| 228 | * QUIC connection mechanism. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 229 | */ |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 230 | static void qcs_free(struct qcs *qcs) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 231 | { |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 232 | struct qcc *qcc = qcs->qcc; |
| 233 | |
| 234 | TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 235 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 236 | qc_free_ncbuf(qcs, &qcs->rx.ncbuf); |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 237 | b_free(&qcs->tx.buf); |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 238 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 239 | BUG_ON(!qcc->strms[qcs_id_type(qcs->id)].nb_streams); |
| 240 | --qcc->strms[qcs_id_type(qcs->id)].nb_streams; |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 241 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 242 | if (qcs->ctx && qcc->app_ops->detach) |
| 243 | qcc->app_ops->detach(qcs); |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 244 | |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 245 | qc_stream_desc_release(qcs->stream); |
| 246 | |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 247 | BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN)); |
| 248 | sedesc_free(qcs->sd); |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 249 | |
| 250 | eb64_delete(&qcs->by_id); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 251 | pool_free(pool_head_qcs, qcs); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 252 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 253 | TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Amaury Denoyelle | 3abeb57 | 2022-07-04 11:42:27 +0200 | [diff] [blame] | 256 | static forceinline struct stconn *qcs_sc(const struct qcs *qcs) |
| 257 | { |
| 258 | return qcs->sd ? qcs->sd->sc : NULL; |
| 259 | } |
| 260 | |
Amaury Denoyelle | bd6ec1b | 2022-07-25 11:53:18 +0200 | [diff] [blame] | 261 | /* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */ |
| 262 | static forceinline void qcc_reset_idle_start(struct qcc *qcc) |
| 263 | { |
| 264 | qcc->idle_start = now_ms; |
| 265 | } |
| 266 | |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 267 | /* Decrement <qcc> sc. */ |
| 268 | static forceinline void qcc_rm_sc(struct qcc *qcc) |
| 269 | { |
| 270 | BUG_ON_HOT(!qcc->nb_sc); |
| 271 | --qcc->nb_sc; |
Amaury Denoyelle | bd6ec1b | 2022-07-25 11:53:18 +0200 | [diff] [blame] | 272 | |
| 273 | /* Reset qcc idle start for http-keep-alive timeout. Timeout will be |
| 274 | * refreshed after this on stream detach. |
| 275 | */ |
| 276 | if (!qcc->nb_sc && !qcc->nb_hreq) |
| 277 | qcc_reset_idle_start(qcc); |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* Decrement <qcc> hreq. */ |
| 281 | static forceinline void qcc_rm_hreq(struct qcc *qcc) |
| 282 | { |
| 283 | BUG_ON_HOT(!qcc->nb_hreq); |
| 284 | --qcc->nb_hreq; |
Amaury Denoyelle | bd6ec1b | 2022-07-25 11:53:18 +0200 | [diff] [blame] | 285 | |
| 286 | /* Reset qcc idle start for http-keep-alive timeout. Timeout will be |
| 287 | * refreshed after this on I/O handler. |
| 288 | */ |
| 289 | if (!qcc->nb_sc && !qcc->nb_hreq) |
| 290 | qcc_reset_idle_start(qcc); |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 293 | static inline int qcc_is_dead(const struct qcc *qcc) |
| 294 | { |
| 295 | /* Mux connection is considered dead if : |
| 296 | * - all stream-desc are detached AND |
| 297 | * = connection is on error OR |
| 298 | * = mux timeout has already fired or is unset |
| 299 | */ |
| 300 | if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)) |
| 301 | return 1; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* Return true if the mux timeout should be armed. */ |
| 307 | static inline int qcc_may_expire(struct qcc *qcc) |
| 308 | { |
| 309 | return !qcc->nb_sc; |
| 310 | } |
| 311 | |
| 312 | /* Refresh the timeout on <qcc> if needed depending on its state. */ |
| 313 | static void qcc_refresh_timeout(struct qcc *qcc) |
| 314 | { |
| 315 | const struct proxy *px = qcc->proxy; |
| 316 | |
| 317 | TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn); |
| 318 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 319 | if (!qcc->task) { |
| 320 | TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 321 | goto leave; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 322 | } |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 323 | |
Amaury Denoyelle | 6ec9837 | 2022-08-01 17:59:38 +0200 | [diff] [blame] | 324 | /* Check if upper layer is responsible of timeout management. */ |
| 325 | if (!qcc_may_expire(qcc)) { |
| 326 | TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn); |
| 327 | qcc->task->expire = TICK_ETERNITY; |
| 328 | task_queue(qcc->task); |
| 329 | goto leave; |
| 330 | } |
| 331 | |
| 332 | /* TODO if connection is idle on frontend and proxy is disabled, remove |
| 333 | * it with global close_spread delay applied. |
| 334 | */ |
| 335 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 336 | /* TODO implement client/server-fin timeout for graceful shutdown */ |
Amaury Denoyelle | 6ec9837 | 2022-08-01 17:59:38 +0200 | [diff] [blame] | 337 | |
| 338 | /* Frontend timeout management |
| 339 | * - detached streams with data left to send -> default timeout |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 340 | * - stream waiting on incomplete request or no stream yet activated -> timeout http-request |
Amaury Denoyelle | 6ec9837 | 2022-08-01 17:59:38 +0200 | [diff] [blame] | 341 | * - idle after stream processing -> timeout http-keep-alive |
| 342 | */ |
| 343 | if (!conn_is_back(qcc->conn)) { |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 344 | if (qcc->nb_hreq) { |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 345 | TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn); |
| 346 | qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout); |
Amaury Denoyelle | 6ec9837 | 2022-08-01 17:59:38 +0200 | [diff] [blame] | 347 | task_queue(qcc->task); |
| 348 | goto leave; |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 349 | } |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 350 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 351 | if (!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) { |
| 352 | int timeout = px->timeout.httpreq; |
| 353 | struct qcs *qcs = NULL; |
| 354 | int base_time; |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 355 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 356 | /* Use start time of first stream waiting on HTTP or |
| 357 | * qcc idle if no stream not yet used. |
| 358 | */ |
| 359 | if (likely(!LIST_ISEMPTY(&qcc->opening_list))) |
| 360 | qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening); |
| 361 | base_time = qcs ? qcs->start : qcc->idle_start; |
| 362 | |
| 363 | TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs); |
| 364 | qcc->task->expire = tick_add_ifset(base_time, timeout); |
| 365 | } |
| 366 | else { |
| 367 | /* Use http-request timeout if keep-alive timeout not set */ |
| 368 | int timeout = tick_isset(px->timeout.httpka) ? |
| 369 | px->timeout.httpka : px->timeout.httpreq; |
| 370 | |
| 371 | TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn); |
| 372 | qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout); |
| 373 | } |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 374 | } |
Amaury Denoyelle | 6ec9837 | 2022-08-01 17:59:38 +0200 | [diff] [blame] | 375 | |
| 376 | /* fallback to default timeout if frontend specific undefined or for |
| 377 | * backend connections. |
| 378 | */ |
| 379 | if (!tick_isset(qcc->task->expire)) { |
| 380 | TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn); |
| 381 | qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout); |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 384 | task_queue(qcc->task); |
| 385 | |
| 386 | leave: |
| 387 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn); |
| 388 | } |
| 389 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 390 | /* Mark a stream as open if it was idle. This can be used on every |
| 391 | * successful emission/reception operation to update the stream state. |
| 392 | */ |
| 393 | static void qcs_idle_open(struct qcs *qcs) |
| 394 | { |
| 395 | /* This operation must not be used if the stream is already closed. */ |
| 396 | BUG_ON_HOT(qcs->st == QC_SS_CLO); |
| 397 | |
| 398 | if (qcs->st == QC_SS_IDLE) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 399 | TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 400 | qcs->st = QC_SS_OPEN; |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | /* Close the local channel of <qcs> instance. */ |
| 405 | static void qcs_close_local(struct qcs *qcs) |
| 406 | { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 407 | TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
| 408 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 409 | /* The stream must have already been opened. */ |
| 410 | BUG_ON_HOT(qcs->st == QC_SS_IDLE); |
| 411 | |
| 412 | /* This operation cannot be used multiple times. */ |
| 413 | BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO); |
| 414 | |
| 415 | if (quic_stream_is_bidi(qcs->id)) { |
| 416 | qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC; |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 417 | qcc_rm_hreq(qcs->qcc); |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 418 | } |
| 419 | else { |
| 420 | /* Only local uni streams are valid for this operation. */ |
| 421 | BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id)); |
| 422 | qcs->st = QC_SS_CLO; |
| 423 | } |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* Close the remote channel of <qcs> instance. */ |
| 427 | static void qcs_close_remote(struct qcs *qcs) |
| 428 | { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 429 | TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs); |
| 430 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 431 | /* The stream must have already been opened. */ |
| 432 | BUG_ON_HOT(qcs->st == QC_SS_IDLE); |
| 433 | |
| 434 | /* This operation cannot be used multiple times. */ |
| 435 | BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO); |
| 436 | |
| 437 | if (quic_stream_is_bidi(qcs->id)) { |
| 438 | qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM; |
| 439 | } |
| 440 | else { |
| 441 | /* Only remote uni streams are valid for this operation. */ |
| 442 | BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id)); |
| 443 | qcs->st = QC_SS_CLO; |
| 444 | } |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static int qcs_is_close_local(struct qcs *qcs) |
| 448 | { |
| 449 | return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO; |
| 450 | } |
| 451 | |
| 452 | static __maybe_unused int qcs_is_close_remote(struct qcs *qcs) |
| 453 | { |
| 454 | return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO; |
| 455 | } |
| 456 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 457 | struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 458 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 459 | struct buffer *buf = b_alloc(bptr); |
| 460 | BUG_ON(!buf); |
| 461 | return buf; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Amaury Denoyelle | a441ec9 | 2022-07-04 15:48:57 +0200 | [diff] [blame] | 464 | static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf) |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 465 | { |
| 466 | struct buffer buf = BUF_NULL; |
| 467 | |
| 468 | if (ncb_is_null(ncbuf)) { |
| 469 | b_alloc(&buf); |
| 470 | BUG_ON(b_is_null(&buf)); |
| 471 | |
| 472 | *ncbuf = ncb_make(buf.area, buf.size, 0); |
| 473 | ncb_init(ncbuf, 0); |
| 474 | } |
| 475 | |
| 476 | return ncbuf; |
| 477 | } |
| 478 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 479 | /* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if |
Amaury Denoyelle | 4561f84 | 2022-07-06 14:54:34 +0200 | [diff] [blame] | 480 | * initialized. |
| 481 | */ |
| 482 | static void qcs_alert(struct qcs *qcs) |
| 483 | { |
| 484 | if (qcs->subs) { |
| 485 | qcs_notify_recv(qcs); |
| 486 | qcs_notify_send(qcs); |
| 487 | } |
| 488 | else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) { |
| 489 | qcs->sd->sc->app_ops->wake(qcs->sd->sc); |
| 490 | } |
| 491 | } |
| 492 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 493 | int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es) |
| 494 | { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 495 | struct qcc *qcc = qcs->qcc; |
| 496 | |
| 497 | TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs); |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 498 | |
| 499 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 500 | BUG_ON(qcs->subs && qcs->subs != es); |
| 501 | |
| 502 | es->events |= event_type; |
| 503 | qcs->subs = es; |
| 504 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 505 | if (event_type & SUB_RETRY_RECV) |
| 506 | TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs); |
| 507 | |
| 508 | if (event_type & SUB_RETRY_SEND) |
| 509 | TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs); |
| 510 | |
| 511 | TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs); |
| 512 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | void qcs_notify_recv(struct qcs *qcs) |
| 517 | { |
| 518 | if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) { |
| 519 | tasklet_wakeup(qcs->subs->tasklet); |
| 520 | qcs->subs->events &= ~SUB_RETRY_RECV; |
| 521 | if (!qcs->subs->events) |
| 522 | qcs->subs = NULL; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void qcs_notify_send(struct qcs *qcs) |
| 527 | { |
| 528 | if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) { |
| 529 | tasklet_wakeup(qcs->subs->tasklet); |
| 530 | qcs->subs->events &= ~SUB_RETRY_SEND; |
| 531 | if (!qcs->subs->events) |
| 532 | qcs->subs = NULL; |
| 533 | } |
| 534 | } |
| 535 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 536 | /* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a |
| 537 | * bidirectional stream, else an unidirectional stream is opened. The next |
| 538 | * available ID on the connection will be used according to the stream type. |
| 539 | * |
| 540 | * Returns the allocated stream instance or NULL on error. |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 541 | */ |
Amaury Denoyelle | b143723 | 2022-07-08 11:53:22 +0200 | [diff] [blame] | 542 | struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi) |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 543 | { |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 544 | struct qcs *qcs; |
| 545 | enum qcs_type type; |
| 546 | uint64_t *next; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 547 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 548 | TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn); |
| 549 | |
| 550 | if (bidi) { |
| 551 | next = &qcc->next_bidi_l; |
| 552 | type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 553 | } |
| 554 | else { |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 555 | next = &qcc->next_uni_l; |
| 556 | type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI; |
| 557 | } |
| 558 | |
| 559 | /* TODO ensure that we won't overflow remote peer flow control limit on |
| 560 | * streams. Else, we should emit a STREAMS_BLOCKED frame. |
| 561 | */ |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 562 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 563 | qcs = qcs_new(qcc, *next, type); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 564 | if (!qcs) { |
| 565 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn); |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 566 | return NULL; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 567 | } |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 568 | |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 569 | TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 570 | *next += 4; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 571 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 572 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 573 | return qcs; |
| 574 | } |
| 575 | |
| 576 | /* Open a remote initiated stream for the connection <qcc> with ID <id>. The |
| 577 | * caller is responsible to ensure that a stream with the same ID was not |
| 578 | * already opened. This function will also create all intermediaries streams |
| 579 | * with ID smaller than <id> not already opened before. |
| 580 | * |
| 581 | * Returns the allocated stream instance or NULL on error. |
| 582 | */ |
Amaury Denoyelle | b143723 | 2022-07-08 11:53:22 +0200 | [diff] [blame] | 583 | static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id) |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 584 | { |
| 585 | struct qcs *qcs = NULL; |
| 586 | enum qcs_type type; |
Amaury Denoyelle | bf3c208 | 2022-08-16 11:29:08 +0200 | [diff] [blame] | 587 | uint64_t *largest, max_id; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 588 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 589 | TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn); |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 590 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 591 | BUG_ON_HOT(quic_stream_is_local(qcc, id)); |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 592 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 593 | if (quic_stream_is_bidi(id)) { |
| 594 | largest = &qcc->largest_bidi_r; |
| 595 | type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI; |
| 596 | } |
| 597 | else { |
| 598 | largest = &qcc->largest_uni_r; |
| 599 | type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI; |
| 600 | } |
| 601 | |
Amaury Denoyelle | bf3c208 | 2022-08-16 11:29:08 +0200 | [diff] [blame] | 602 | /* RFC 9000 4.6. Controlling Concurrency |
| 603 | * |
| 604 | * An endpoint that receives a frame with a stream ID exceeding the |
| 605 | * limit it has sent MUST treat this as a connection error of type |
| 606 | * STREAM_LIMIT_ERROR |
| 607 | */ |
| 608 | max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 : |
| 609 | qcc->lfctl.ms_uni * 4; |
| 610 | if (id >= max_id) { |
| 611 | TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn); |
| 612 | qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR); |
| 613 | goto err; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* Only stream ID not already opened can be used. */ |
| 617 | BUG_ON(id < *largest); |
| 618 | |
| 619 | while (id >= *largest) { |
Amaury Denoyelle | fd79ddb | 2022-08-16 11:13:45 +0200 | [diff] [blame] | 620 | const char *str = *largest < id ? "initializing intermediary remote stream" : |
| 621 | "initializing remote stream"; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 622 | |
| 623 | qcs = qcs_new(qcc, *largest, type); |
| 624 | if (!qcs) { |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 625 | /* TODO emit RESET_STREAM */ |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 626 | TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 627 | goto err; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 628 | } |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 629 | |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 630 | TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 631 | *largest += 4; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 632 | } |
| 633 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 634 | out: |
| 635 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 636 | return qcs; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 637 | |
| 638 | err: |
| 639 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn); |
| 640 | return NULL; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* Use this function for a stream <id> which is not in <qcc> stream tree. It |
| 644 | * returns true if the associated stream is closed. |
| 645 | */ |
| 646 | static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id) |
| 647 | { |
| 648 | uint64_t *largest; |
| 649 | |
| 650 | /* This function must only be used for stream not present in the stream tree. */ |
| 651 | BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id)); |
| 652 | |
| 653 | if (quic_stream_is_local(qcc, id)) { |
| 654 | largest = quic_stream_is_uni(id) ? &qcc->next_uni_l : |
| 655 | &qcc->next_bidi_l; |
| 656 | } |
| 657 | else { |
| 658 | largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r : |
| 659 | &qcc->largest_bidi_r; |
| 660 | } |
| 661 | |
| 662 | return id < *largest; |
| 663 | } |
| 664 | |
| 665 | /* Retrieve the stream instance from <id> ID. This can be used when receiving |
| 666 | * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 667 | * frames. Set to false <receive_only> or <send_only> if these particular types |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 668 | * of streams are not allowed. If the stream instance is found, it is stored in |
| 669 | * <out>. |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 670 | * |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 671 | * Returns 0 on success else non-zero. On error, a RESET_STREAM or a |
| 672 | * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL |
| 673 | * on success if the stream has already been closed. |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 674 | */ |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 675 | int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only, |
| 676 | struct qcs **out) |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 677 | { |
| 678 | struct eb64_node *node; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 679 | |
| 680 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 681 | *out = NULL; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 682 | |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 683 | if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 684 | TRACE_ERROR("receive-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id); |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 685 | qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 686 | goto err; |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 690 | TRACE_ERROR("send-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id); |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 691 | qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 692 | goto err; |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 693 | } |
| 694 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 695 | /* Search the stream in the connection tree. */ |
| 696 | node = eb64_lookup(&qcc->streams_by_id, id); |
| 697 | if (node) { |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 698 | *out = eb64_entry(node, struct qcs, by_id); |
| 699 | TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 700 | goto out; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /* Check if stream is already closed. */ |
| 704 | if (qcc_stream_id_is_closed(qcc, id)) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 705 | TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id); |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 706 | /* Consider this as a success even if <out> is left NULL. */ |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 707 | goto out; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* Create the stream. This is valid only for remote initiated one. A |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 711 | * local stream must have already been explicitly created by the |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 712 | * application protocol layer. |
| 713 | */ |
| 714 | if (quic_stream_is_local(qcc, id)) { |
| 715 | /* RFC 9000 19.8. STREAM Frames |
| 716 | * |
| 717 | * An endpoint MUST terminate the connection with error |
| 718 | * STREAM_STATE_ERROR if it receives a STREAM frame for a locally |
| 719 | * initiated stream that has not yet been created, or for a send-only |
| 720 | * stream. |
| 721 | */ |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 722 | TRACE_ERROR("locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id); |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 723 | qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 724 | goto err; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 725 | } |
| 726 | else { |
| 727 | /* Remote stream not found - try to open it. */ |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 728 | *out = qcc_init_stream_remote(qcc, id); |
| 729 | if (!*out) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 730 | TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 731 | goto err; |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 732 | } |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 733 | } |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 734 | |
| 735 | out: |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 736 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out); |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 737 | return 0; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 738 | |
| 739 | err: |
| 740 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 741 | return 1; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 744 | /* Simple function to duplicate a buffer */ |
| 745 | static inline struct buffer qcs_b_dup(const struct ncbuf *b) |
| 746 | { |
| 747 | return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0)); |
| 748 | } |
| 749 | |
Amaury Denoyelle | 36d4b5e | 2022-07-01 11:25:40 +0200 | [diff] [blame] | 750 | /* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may |
| 751 | * be allocated for the peer if needed. |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 752 | */ |
| 753 | static void qcs_consume(struct qcs *qcs, uint64_t bytes) |
| 754 | { |
| 755 | struct qcc *qcc = qcs->qcc; |
| 756 | struct quic_frame *frm; |
| 757 | struct ncbuf *buf = &qcs->rx.ncbuf; |
| 758 | enum ncb_ret ret; |
| 759 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 760 | TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 761 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 762 | ret = ncb_advance(buf, bytes); |
| 763 | if (ret) { |
| 764 | ABORT_NOW(); /* should not happens because removal only in data */ |
| 765 | } |
| 766 | |
| 767 | if (ncb_is_empty(buf)) |
| 768 | qc_free_ncbuf(qcs, buf); |
| 769 | |
| 770 | qcs->rx.offset += bytes; |
| 771 | if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 772 | TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 773 | frm = pool_zalloc(pool_head_quic_frame); |
| 774 | BUG_ON(!frm); /* TODO handle this properly */ |
| 775 | |
| 776 | qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init; |
| 777 | |
| 778 | LIST_INIT(&frm->reflist); |
| 779 | frm->type = QUIC_FT_MAX_STREAM_DATA; |
| 780 | frm->max_stream_data.id = qcs->id; |
| 781 | frm->max_stream_data.max_stream_data = qcs->rx.msd; |
| 782 | |
| 783 | LIST_APPEND(&qcc->lfctl.frms, &frm->list); |
| 784 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 785 | } |
| 786 | |
| 787 | qcc->lfctl.offsets_consume += bytes; |
| 788 | if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 789 | TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 790 | frm = pool_zalloc(pool_head_quic_frame); |
| 791 | BUG_ON(!frm); /* TODO handle this properly */ |
| 792 | |
| 793 | qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init; |
| 794 | |
| 795 | LIST_INIT(&frm->reflist); |
| 796 | frm->type = QUIC_FT_MAX_DATA; |
| 797 | frm->max_data.max_data = qcc->lfctl.md; |
| 798 | |
| 799 | LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list); |
| 800 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 801 | } |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 802 | |
| 803 | TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 806 | /* Decode the content of STREAM frames already received on the stream instance |
| 807 | * <qcs>. |
| 808 | * |
| 809 | * Returns 0 on success else non-zero. |
| 810 | */ |
| 811 | static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs) |
| 812 | { |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 813 | struct buffer b; |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 814 | ssize_t ret; |
Amaury Denoyelle | 6befccd | 2022-07-01 11:26:04 +0200 | [diff] [blame] | 815 | int fin = 0; |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 816 | |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 817 | TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 818 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 819 | b = qcs_b_dup(&qcs->rx.ncbuf); |
Amaury Denoyelle | 6befccd | 2022-07-01 11:26:04 +0200 | [diff] [blame] | 820 | |
| 821 | /* Signal FIN to application if STREAM FIN received and there is no gap |
| 822 | * in the Rx buffer. |
| 823 | */ |
Amaury Denoyelle | 3f39b40 | 2022-07-01 16:11:03 +0200 | [diff] [blame] | 824 | if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf)) |
Amaury Denoyelle | 6befccd | 2022-07-01 11:26:04 +0200 | [diff] [blame] | 825 | fin = 1; |
| 826 | |
| 827 | ret = qcc->app_ops->decode_qcs(qcs, &b, fin); |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 828 | if (ret < 0) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 829 | TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 830 | goto err; |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 831 | } |
| 832 | |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 833 | if (ret) { |
| 834 | qcs_consume(qcs, ret); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 835 | qcs_notify_recv(qcs); |
| 836 | } |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 837 | |
| 838 | TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 839 | return 0; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 840 | |
| 841 | err: |
| 842 | TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 843 | return 1; |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 846 | /* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 847 | * code. To close the connection right away, set <immediate> : this is useful |
| 848 | * when dealing with a connection fatal error. Else a graceful shutdown will be |
| 849 | * conducted : the error-code is only registered. The lower layer is |
| 850 | * responsible to close the connection when deemed suitable. Note that in this |
| 851 | * case the error code might be overwritten if an immediate close is requested |
| 852 | * in the interval. |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 853 | */ |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 854 | void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate) |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 855 | { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 856 | TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn); |
| 857 | |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 858 | if (immediate) { |
| 859 | quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err)); |
| 860 | qcc->flags |= QC_CF_CC_EMIT; |
| 861 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 862 | } |
| 863 | else { |
| 864 | /* Only register the error code for graceful shutdown. */ |
| 865 | qcc->conn->handle.qc->err = quic_err_app(err); |
| 866 | } |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 867 | |
| 868 | TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn); |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */ |
| 872 | void qcc_reset_stream(struct qcs *qcs, int err) |
| 873 | { |
| 874 | struct qcc *qcc = qcs->qcc; |
| 875 | |
| 876 | if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs)) |
| 877 | return; |
| 878 | |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 879 | TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs); |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 880 | qcs->flags |= QC_SF_TO_RESET; |
| 881 | qcs->err = err; |
| 882 | tasklet_wakeup(qcc->wait_event.tasklet); |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 883 | } |
| 884 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 885 | /* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>. |
| 886 | * Returns 0 on success else non-zero. |
| 887 | */ |
| 888 | int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops) |
| 889 | { |
| 890 | TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn); |
| 891 | |
| 892 | qcc->app_ops = app_ops; |
| 893 | if (qcc->app_ops->init && !qcc->app_ops->init(qcc)) { |
| 894 | TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn); |
| 895 | goto err; |
| 896 | } |
| 897 | |
| 898 | TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn); |
| 899 | |
| 900 | if (qcc->app_ops->finalize) |
| 901 | qcc->app_ops->finalize(qcc->ctx); |
| 902 | |
| 903 | TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn); |
| 904 | return 0; |
| 905 | |
| 906 | err: |
| 907 | TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn); |
| 908 | return 1; |
| 909 | } |
| 910 | |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 911 | /* Handle a new STREAM frame for stream with id <id>. Payload is pointed by |
| 912 | * <data> with length <len> and represents the offset <offset>. <fin> is set if |
| 913 | * the QUIC frame FIN bit is set. |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 914 | * |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 915 | * Returns 0 on success else non-zero. On error, the received frame should not |
| 916 | * be acknowledged. |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 917 | */ |
| 918 | int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset, |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 919 | char fin, char *data) |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 920 | { |
| 921 | struct qcs *qcs; |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 922 | enum ncb_ret ret; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 923 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 924 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
| 925 | |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 926 | if (qcc->flags & QC_CF_CC_EMIT) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 927 | TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn); |
| 928 | goto err; |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Amaury Denoyelle | 6754d7e | 2022-05-23 16:12:49 +0200 | [diff] [blame] | 931 | /* RFC 9000 19.8. STREAM Frames |
| 932 | * |
| 933 | * An endpoint MUST terminate the connection with error |
| 934 | * STREAM_STATE_ERROR if it receives a STREAM frame for a locally |
| 935 | * initiated stream that has not yet been created, or for a send-only |
| 936 | * stream. |
| 937 | */ |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 938 | if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 939 | TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn); |
| 940 | goto err; |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | if (!qcs) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 944 | TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn); |
| 945 | goto out; |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 946 | } |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 947 | |
Amaury Denoyelle | bf91e39 | 2022-07-04 10:02:04 +0200 | [diff] [blame] | 948 | /* RFC 9000 4.5. Stream Final Size |
| 949 | * |
| 950 | * Once a final size for a stream is known, it cannot change. If a |
| 951 | * RESET_STREAM or STREAM frame is received indicating a change in the |
| 952 | * final size for the stream, an endpoint SHOULD respond with an error |
| 953 | * of type FINAL_SIZE_ERROR; see Section 11 for details on error |
| 954 | * handling. |
| 955 | */ |
| 956 | if (qcs->flags & QC_SF_SIZE_KNOWN && |
| 957 | (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 958 | TRACE_ERROR("final size error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, qcc->conn, qcs); |
Amaury Denoyelle | bf91e39 | 2022-07-04 10:02:04 +0200 | [diff] [blame] | 959 | qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 960 | goto err; |
Amaury Denoyelle | bf91e39 | 2022-07-04 10:02:04 +0200 | [diff] [blame] | 961 | } |
| 962 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 963 | if (offset + len <= qcs->rx.offset) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 964 | TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 965 | goto out; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 966 | } |
| 967 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 968 | TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 969 | qcs_idle_open(qcs); |
| 970 | |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 971 | if (offset + len > qcs->rx.offset_max) { |
| 972 | uint64_t diff = offset + len - qcs->rx.offset_max; |
| 973 | qcs->rx.offset_max = offset + len; |
| 974 | qcc->lfctl.offsets_recv += diff; |
| 975 | |
| 976 | if (offset + len > qcs->rx.msd || |
| 977 | qcc->lfctl.offsets_recv > qcc->lfctl.md) { |
| 978 | /* RFC 9000 4.1. Data Flow Control |
| 979 | * |
| 980 | * A receiver MUST close the connection with an error |
| 981 | * of type FLOW_CONTROL_ERROR if the sender violates |
| 982 | * the advertised connection or stream data limits |
| 983 | */ |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 984 | TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 985 | qcc->conn, qcs); |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 986 | qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 987 | goto err; |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 988 | } |
| 989 | } |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 990 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 991 | if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) { |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 992 | /* TODO should mark qcs as full */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 993 | ABORT_NOW(); |
| 994 | return 1; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 995 | } |
| 996 | |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 997 | TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 998 | if (offset < qcs->rx.offset) { |
Frédéric Lécaille | a18c333 | 2022-07-04 09:54:58 +0200 | [diff] [blame] | 999 | size_t diff = qcs->rx.offset - offset; |
| 1000 | |
| 1001 | len -= diff; |
| 1002 | data += diff; |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1003 | offset = qcs->rx.offset; |
| 1004 | } |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1005 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1006 | ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE); |
| 1007 | if (ret != NCB_RET_OK) { |
| 1008 | if (ret == NCB_RET_DATA_REJ) { |
Amaury Denoyelle | cc3d716 | 2022-05-20 15:14:57 +0200 | [diff] [blame] | 1009 | /* RFC 9000 2.2. Sending and Receiving Data |
| 1010 | * |
| 1011 | * An endpoint could receive data for a stream at the |
| 1012 | * same stream offset multiple times. Data that has |
| 1013 | * already been received can be discarded. The data at |
| 1014 | * a given offset MUST NOT change if it is sent |
| 1015 | * multiple times; an endpoint MAY treat receipt of |
| 1016 | * different data at the same offset within a stream as |
| 1017 | * a connection error of type PROTOCOL_VIOLATION. |
| 1018 | */ |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 1019 | TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1020 | qcc->conn, qcs); |
Amaury Denoyelle | cc3d716 | 2022-05-20 15:14:57 +0200 | [diff] [blame] | 1021 | qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION); |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1022 | } |
| 1023 | else if (ret == NCB_RET_GAP_SIZE) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 1024 | TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, |
| 1025 | qcc->conn, qcs); |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1026 | } |
| 1027 | return 1; |
| 1028 | } |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1029 | |
| 1030 | if (fin) |
Amaury Denoyelle | 3f39b40 | 2022-07-01 16:11:03 +0200 | [diff] [blame] | 1031 | qcs->flags |= QC_SF_SIZE_KNOWN; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1032 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1033 | if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf)) |
| 1034 | qcs_close_remote(qcs); |
| 1035 | |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 1036 | if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) { |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 1037 | qcc_decode_qcs(qcc, qcs); |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 1038 | qcc_refresh_timeout(qcc); |
| 1039 | } |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 1040 | |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 1041 | if (qcs->flags & QC_SF_READ_ABORTED) { |
| 1042 | /* TODO should send a STOP_SENDING */ |
| 1043 | qcs_free(qcs); |
| 1044 | } |
| 1045 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1046 | out: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1047 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1048 | return 0; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1049 | |
| 1050 | err: |
| 1051 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 1052 | return 1; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 1055 | /* Handle a new MAX_DATA frame. <max> must contains the maximum data field of |
| 1056 | * the frame. |
| 1057 | * |
| 1058 | * Returns 0 on success else non-zero. |
| 1059 | */ |
| 1060 | int qcc_recv_max_data(struct qcc *qcc, uint64_t max) |
| 1061 | { |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1062 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
| 1063 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1064 | TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 1065 | if (qcc->rfctl.md < max) { |
| 1066 | qcc->rfctl.md = max; |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1067 | TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 1068 | |
| 1069 | if (qcc->flags & QC_CF_BLK_MFCTL) { |
| 1070 | qcc->flags &= ~QC_CF_BLK_MFCTL; |
| 1071 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 1072 | } |
| 1073 | } |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1074 | |
| 1075 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 1076 | return 0; |
| 1077 | } |
| 1078 | |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1079 | /* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data |
| 1080 | * field of the frame and <id> is the identifier of the QUIC stream. |
| 1081 | * |
Amaury Denoyelle | b68559a | 2022-07-06 15:45:20 +0200 | [diff] [blame] | 1082 | * Returns 0 on success else non-zero. On error, the received frame should not |
| 1083 | * be acknowledged. |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1084 | */ |
| 1085 | int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max) |
| 1086 | { |
| 1087 | struct qcs *qcs; |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1088 | |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1089 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
| 1090 | |
Amaury Denoyelle | b68559a | 2022-07-06 15:45:20 +0200 | [diff] [blame] | 1091 | /* RFC 9000 19.10. MAX_STREAM_DATA Frames |
| 1092 | * |
| 1093 | * Receiving a MAX_STREAM_DATA frame for a locally |
| 1094 | * initiated stream that has not yet been created MUST be treated as a |
| 1095 | * connection error of type STREAM_STATE_ERROR. An endpoint that |
| 1096 | * receives a MAX_STREAM_DATA frame for a receive-only stream MUST |
| 1097 | * terminate the connection with error STREAM_STATE_ERROR. |
| 1098 | */ |
| 1099 | if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) { |
| 1100 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 1101 | return 1; |
| 1102 | } |
| 1103 | |
| 1104 | if (qcs) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1105 | TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1106 | if (max > qcs->tx.msd) { |
| 1107 | qcs->tx.msd = max; |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1108 | TRACE_DEVEL("increase remote max-stream-data", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1109 | |
| 1110 | if (qcs->flags & QC_SF_BLK_SFCTL) { |
| 1111 | qcs->flags &= ~QC_SF_BLK_SFCTL; |
| 1112 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 1117 | if (qcc_may_expire(qcc) && !qcc->nb_hreq) |
| 1118 | qcc_refresh_timeout(qcc); |
| 1119 | |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1120 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 1124 | /* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be |
| 1125 | * specified in <err>. |
| 1126 | * |
| 1127 | * Returns 0 on success else non-zero. On error, the received frame should not |
| 1128 | * be acknowledged. |
| 1129 | */ |
| 1130 | int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err) |
| 1131 | { |
| 1132 | struct qcs *qcs; |
| 1133 | |
| 1134 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
| 1135 | |
| 1136 | /* RFC 9000 19.5. STOP_SENDING Frames |
| 1137 | * |
| 1138 | * Receiving a STOP_SENDING frame for a |
| 1139 | * locally initiated stream that has not yet been created MUST be |
| 1140 | * treated as a connection error of type STREAM_STATE_ERROR. An |
| 1141 | * endpoint that receives a STOP_SENDING frame for a receive-only stream |
| 1142 | * MUST terminate the connection with error STREAM_STATE_ERROR. |
| 1143 | */ |
| 1144 | if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) { |
| 1145 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 1146 | return 1; |
| 1147 | } |
| 1148 | |
| 1149 | if (!qcs) |
| 1150 | goto out; |
| 1151 | |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 1152 | TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 96ca1b7 | 2022-08-09 17:36:38 +0200 | [diff] [blame] | 1153 | qcs_idle_open(qcs); |
| 1154 | |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 1155 | /* RFC 9000 3.5. Solicited State Transitions |
| 1156 | * |
| 1157 | * An endpoint that receives a STOP_SENDING frame |
| 1158 | * MUST send a RESET_STREAM frame if the stream is in the "Ready" or |
| 1159 | * "Send" state. If the stream is in the "Data Sent" state, the |
| 1160 | * endpoint MAY defer sending the RESET_STREAM frame until the packets |
| 1161 | * containing outstanding data are acknowledged or declared lost. If |
| 1162 | * any outstanding data is declared lost, the endpoint SHOULD send a |
| 1163 | * RESET_STREAM frame instead of retransmitting the data. |
| 1164 | * |
| 1165 | * An endpoint SHOULD copy the error code from the STOP_SENDING frame to |
| 1166 | * the RESET_STREAM frame it sends, but it can use any application error |
| 1167 | * code. |
| 1168 | */ |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 1169 | qcc_reset_stream(qcs, err); |
| 1170 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 1171 | if (qcc_may_expire(qcc) && !qcc->nb_hreq) |
| 1172 | qcc_refresh_timeout(qcc); |
| 1173 | |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 1174 | out: |
| 1175 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1179 | /* Signal the closing of remote stream with id <id>. Flow-control for new |
| 1180 | * streams may be allocated for the peer if needed. |
| 1181 | */ |
| 1182 | static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id) |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1183 | { |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1184 | struct quic_frame *frm; |
| 1185 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1186 | TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn); |
| 1187 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1188 | if (quic_stream_is_bidi(id)) { |
| 1189 | ++qcc->lfctl.cl_bidi_r; |
| 1190 | if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1191 | TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1192 | frm = pool_zalloc(pool_head_quic_frame); |
| 1193 | BUG_ON(!frm); /* TODO handle this properly */ |
| 1194 | |
| 1195 | LIST_INIT(&frm->reflist); |
| 1196 | frm->type = QUIC_FT_MAX_STREAMS_BIDI; |
| 1197 | frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi + |
| 1198 | qcc->lfctl.cl_bidi_r; |
| 1199 | LIST_APPEND(&qcc->lfctl.frms, &frm->list); |
| 1200 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 1201 | |
| 1202 | qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r; |
| 1203 | qcc->lfctl.cl_bidi_r = 0; |
| 1204 | } |
| 1205 | } |
| 1206 | else { |
Amaury Denoyelle | bf3c208 | 2022-08-16 11:29:08 +0200 | [diff] [blame] | 1207 | /* TODO in HTTP/3 unidirectional streams cannot be closed or a |
| 1208 | * H3_CLOSED_CRITICAL_STREAM will be triggered before |
| 1209 | * entering here. If a new application protocol is supported it |
| 1210 | * might be necessary to implement MAX_STREAMS_UNI emission. |
| 1211 | */ |
| 1212 | ABORT_NOW(); |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1213 | } |
| 1214 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1215 | TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn); |
| 1216 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1217 | return 0; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 1220 | /* detaches the QUIC stream from its QCC and releases it to the QCS pool. */ |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1221 | static void qcs_destroy(struct qcs *qcs) |
| 1222 | { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1223 | struct connection *conn = qcs->qcc->conn; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 1224 | const uint64_t id = qcs->id; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1225 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1226 | TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1227 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1228 | if (quic_stream_is_remote(qcs->qcc, id)) |
| 1229 | qcc_release_remote_stream(qcs->qcc, id); |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1230 | |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 1231 | qcs_free(qcs); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1232 | |
| 1233 | TRACE_LEAVE(QMUX_EV_QCS_END, conn); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1234 | } |
| 1235 | |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1236 | /* Transfer as much as possible data on <qcs> from <in> to <out>. This is done |
| 1237 | * in respect with available flow-control at stream and connection level. |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1238 | * |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 1239 | * Returns the total bytes of transferred data. |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1240 | */ |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1241 | static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in) |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1242 | { |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1243 | struct qcc *qcc = qcs->qcc; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1244 | int left, to_xfer; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1245 | int total = 0; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1246 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1247 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1248 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1249 | qc_get_buf(qcs, out); |
| 1250 | |
| 1251 | /* |
| 1252 | * QCS out buffer diagram |
| 1253 | * head left to_xfer |
| 1254 | * -------------> ----------> -----> |
Amaury Denoyelle | e0320b8 | 2022-03-11 19:12:23 +0100 | [diff] [blame] | 1255 | * -------------------------------------------------- |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1256 | * |...............|xxxxxxxxxxx|<<<<< |
Amaury Denoyelle | e0320b8 | 2022-03-11 19:12:23 +0100 | [diff] [blame] | 1257 | * -------------------------------------------------- |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1258 | * ^ ack-off ^ sent-off ^ off |
| 1259 | * |
| 1260 | * STREAM frame |
| 1261 | * ^ ^ |
| 1262 | * |xxxxxxxxxxxxxxxxx| |
| 1263 | */ |
| 1264 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1265 | BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1266 | BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset); |
Amaury Denoyelle | 78fa559 | 2022-06-10 15:18:12 +0200 | [diff] [blame] | 1267 | BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1268 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1269 | left = qcs->tx.offset - qcs->tx.sent_offset; |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 1270 | to_xfer = QUIC_MIN(b_data(in), b_room(out)); |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 1271 | |
| 1272 | BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd); |
| 1273 | /* do not exceed flow control limit */ |
| 1274 | if (qcs->tx.offset + to_xfer > qcs->tx.msd) |
| 1275 | to_xfer = qcs->tx.msd - qcs->tx.offset; |
| 1276 | |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1277 | BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md); |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1278 | /* do not overcome flow control limit on connection */ |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1279 | if (qcc->tx.offsets + to_xfer > qcc->rfctl.md) |
| 1280 | to_xfer = qcc->rfctl.md - qcc->tx.offsets; |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1281 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1282 | if (!left && !to_xfer) |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 1283 | goto out; |
| 1284 | |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 1285 | total = b_force_xfer(out, in, to_xfer); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1286 | |
| 1287 | out: |
| 1288 | { |
| 1289 | struct qcs_xfer_data_trace_arg arg = { |
| 1290 | .prep = b_data(out), .xfer = total, |
| 1291 | }; |
| 1292 | TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA, |
| 1293 | qcc->conn, qcs, &arg); |
| 1294 | } |
| 1295 | |
| 1296 | return total; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1297 | } |
| 1298 | |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 1299 | /* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame |
| 1300 | * is appended in <frm_list>. Set <fin> if this is supposed to be the last |
| 1301 | * stream frame. |
| 1302 | * |
| 1303 | * Returns the length of the STREAM frame or a negative error code. |
| 1304 | */ |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1305 | static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin, |
| 1306 | struct list *frm_list) |
| 1307 | { |
| 1308 | struct qcc *qcc = qcs->qcc; |
| 1309 | struct quic_frame *frm; |
| 1310 | int head, total; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1311 | uint64_t base_off; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1312 | |
| 1313 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1314 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1315 | /* if ack_offset < buf_offset, it points to an older buffer. */ |
| 1316 | base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset); |
| 1317 | BUG_ON(qcs->tx.sent_offset < base_off); |
| 1318 | |
| 1319 | head = qcs->tx.sent_offset - base_off; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1320 | total = b_data(out) - head; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1321 | BUG_ON(total < 0); |
| 1322 | |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1323 | if (!total && !fin) { |
| 1324 | /* No need to send anything if total is NULL and no FIN to signal. */ |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1325 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1326 | return 0; |
| 1327 | } |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1328 | BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) || |
| 1329 | (total && qcs->tx.sent_offset >= qcs->tx.offset)); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1330 | BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset); |
Amaury Denoyelle | 78fa559 | 2022-06-10 15:18:12 +0200 | [diff] [blame] | 1331 | BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1332 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1333 | TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1334 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1335 | if (!frm) { |
| 1336 | TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1337 | goto err; |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1338 | } |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1339 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1340 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1341 | frm->type = QUIC_FT_STREAM_8; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1342 | frm->stream.stream = qcs->stream; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 1343 | frm->stream.id = qcs->id; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1344 | frm->stream.buf = out; |
| 1345 | frm->stream.data = (unsigned char *)b_peek(out, head); |
| 1346 | |
Amaury Denoyelle | fecfa0d | 2021-12-07 16:50:14 +0100 | [diff] [blame] | 1347 | /* FIN is positioned only when the buffer has been totally emptied. */ |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1348 | if (fin) |
| 1349 | frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1350 | |
| 1351 | if (qcs->tx.sent_offset) { |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1352 | frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1353 | frm->stream.offset.key = qcs->tx.sent_offset; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1354 | } |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1355 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1356 | frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 1357 | frm->stream.len = total; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1358 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1359 | LIST_APPEND(frm_list, &frm->list); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1360 | |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 1361 | out: |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 1362 | { |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1363 | struct qcs_build_stream_trace_arg arg = { |
| 1364 | .len = frm->stream.len, .fin = fin, |
| 1365 | .offset = frm->stream.offset.key, |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 1366 | }; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1367 | TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM, |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 1368 | qcc->conn, qcs, &arg); |
| 1369 | } |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1370 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1371 | return total; |
| 1372 | |
| 1373 | err: |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1374 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1375 | return -1; |
| 1376 | } |
| 1377 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 1378 | /* Check after transferring data from qcs.tx.buf if FIN must be set on the next |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1379 | * STREAM frame for <qcs>. |
| 1380 | * |
| 1381 | * Returns true if FIN must be set else false. |
| 1382 | */ |
| 1383 | static int qcs_stream_fin(struct qcs *qcs) |
| 1384 | { |
| 1385 | return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf); |
| 1386 | } |
| 1387 | |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 1388 | /* This function must be called by the upper layer to inform about the sending |
| 1389 | * of a STREAM frame for <qcs> instance. The frame is of <data> length and on |
| 1390 | * <offset>. |
| 1391 | */ |
| 1392 | void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset) |
| 1393 | { |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1394 | struct qcc *qcc = qcs->qcc; |
| 1395 | uint64_t diff; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1396 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1397 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1398 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1399 | BUG_ON(offset > qcs->tx.sent_offset); |
Amaury Denoyelle | 78fa559 | 2022-06-10 15:18:12 +0200 | [diff] [blame] | 1400 | BUG_ON(offset + data > qcs->tx.offset); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1401 | |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 1402 | /* check if the STREAM frame has already been notified. It can happen |
| 1403 | * for retransmission. |
| 1404 | */ |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1405 | if (offset + data < qcs->tx.sent_offset) { |
| 1406 | TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1407 | goto out; |
| 1408 | } |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1409 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1410 | qcs_idle_open(qcs); |
| 1411 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1412 | diff = offset + data - qcs->tx.sent_offset; |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1413 | if (diff) { |
| 1414 | /* increase offset sum on connection */ |
| 1415 | qcc->tx.sent_offsets += diff; |
| 1416 | BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md); |
| 1417 | if (qcc->tx.sent_offsets == qcc->rfctl.md) |
| 1418 | qcc->flags |= QC_CF_BLK_MFCTL; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1419 | |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1420 | /* increase offset on stream */ |
| 1421 | qcs->tx.sent_offset += diff; |
| 1422 | BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd); |
| 1423 | BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset); |
| 1424 | if (qcs->tx.sent_offset == qcs->tx.msd) |
| 1425 | qcs->flags |= QC_SF_BLK_SFCTL; |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1426 | |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1427 | if (qcs->tx.offset == qcs->tx.sent_offset && |
| 1428 | b_full(&qcs->stream->buf->buf)) { |
| 1429 | qc_stream_buf_release(qcs->stream); |
| 1430 | /* prepare qcs for immediate send retry if data to send */ |
| 1431 | if (b_data(&qcs->tx.buf)) |
| 1432 | LIST_APPEND(&qcc->send_retry_list, &qcs->el); |
| 1433 | } |
| 1434 | } |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1435 | |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1436 | if (qcs->tx.offset == qcs->tx.sent_offset && qcs_stream_fin(qcs)) { |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1437 | /* Close stream locally. */ |
| 1438 | qcs_close_local(qcs); |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1439 | /* Reset flag to not emit multiple FIN STREAM frames. */ |
| 1440 | qcs->flags &= ~QC_SF_FIN_STREAM; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1441 | } |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1442 | |
| 1443 | out: |
| 1444 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 1445 | } |
| 1446 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1447 | /* Wrapper for send on transport layer. Send a list of frames <frms> for the |
| 1448 | * connection <qcc>. |
| 1449 | * |
| 1450 | * Returns 0 if all data sent with success else non-zero. |
| 1451 | */ |
| 1452 | static int qc_send_frames(struct qcc *qcc, struct list *frms) |
| 1453 | { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1454 | TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn); |
| 1455 | |
| 1456 | if (LIST_ISEMPTY(frms)) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1457 | TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn); |
| 1458 | goto err; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1459 | } |
Frédéric Lécaille | 4e22f28 | 2022-03-18 18:38:19 +0100 | [diff] [blame] | 1460 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1461 | LIST_INIT(&qcc->send_retry_list); |
| 1462 | |
Amaury Denoyelle | 7046756 | 2022-08-17 16:33:13 +0200 | [diff] [blame] | 1463 | qc_send_mux(qcc->conn->handle.qc, frms); |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 1464 | |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 1465 | /* If there is frames left at this stage, transport layer is blocked. |
| 1466 | * Subscribe on it to retry later. |
| 1467 | */ |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1468 | if (!LIST_ISEMPTY(frms)) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1469 | TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1470 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 1471 | SUB_RETRY_SEND, &qcc->wait_event); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1472 | goto err; |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1473 | } |
| 1474 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1475 | TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 1476 | return 0; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1477 | |
| 1478 | err: |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1479 | TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1480 | return 1; |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | /* Emit a RESET_STREAM on <qcs>. |
| 1484 | * |
| 1485 | * Returns 0 if the frame has been successfully sent else non-zero. |
| 1486 | */ |
| 1487 | static int qcs_send_reset(struct qcs *qcs) |
| 1488 | { |
| 1489 | struct list frms = LIST_HEAD_INIT(frms); |
| 1490 | struct quic_frame *frm; |
| 1491 | |
| 1492 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
| 1493 | |
| 1494 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1495 | if (!frm) { |
| 1496 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 1497 | return 1; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1498 | } |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 1499 | |
| 1500 | LIST_INIT(&frm->reflist); |
| 1501 | frm->type = QUIC_FT_RESET_STREAM; |
| 1502 | frm->reset_stream.id = qcs->id; |
| 1503 | frm->reset_stream.app_error_code = qcs->err; |
| 1504 | frm->reset_stream.final_size = qcs->tx.sent_offset; |
| 1505 | |
| 1506 | LIST_APPEND(&frms, &frm->list); |
| 1507 | if (qc_send_frames(qcs->qcc, &frms)) { |
| 1508 | pool_free(pool_head_quic_frame, frm); |
| 1509 | TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
| 1510 | return 1; |
| 1511 | } |
| 1512 | |
| 1513 | if (qcs_sc(qcs)) { |
| 1514 | se_fl_set_error(qcs->sd); |
| 1515 | qcs_alert(qcs); |
| 1516 | } |
| 1517 | |
| 1518 | qcs_close_local(qcs); |
| 1519 | qcs->flags &= ~QC_SF_TO_RESET; |
| 1520 | |
| 1521 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1522 | return 0; |
| 1523 | } |
| 1524 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1525 | /* Used internally by qc_send function. Proceed to send for <qcs>. This will |
| 1526 | * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1527 | * is then generated and inserted in <frms> list. |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1528 | * |
| 1529 | * Returns the total bytes transferred between qcs and quic_stream buffers. Can |
| 1530 | * be null if out buffer cannot be allocated. |
| 1531 | */ |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1532 | static int _qc_send_qcs(struct qcs *qcs, struct list *frms) |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1533 | { |
| 1534 | struct qcc *qcc = qcs->qcc; |
| 1535 | struct buffer *buf = &qcs->tx.buf; |
| 1536 | struct buffer *out = qc_stream_buf_get(qcs->stream); |
| 1537 | int xfer = 0; |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1538 | char fin = 0; |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1539 | |
| 1540 | /* Allocate <out> buffer if necessary. */ |
| 1541 | if (!out) { |
| 1542 | if (qcc->flags & QC_CF_CONN_FULL) |
| 1543 | return 0; |
| 1544 | |
| 1545 | out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset); |
| 1546 | if (!out) { |
| 1547 | qcc->flags |= QC_CF_CONN_FULL; |
| 1548 | return 0; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | /* Transfer data from <buf> to <out>. */ |
| 1553 | if (b_data(buf)) { |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1554 | xfer = qcs_xfer_data(qcs, out, buf); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1555 | if (xfer > 0) { |
| 1556 | qcs_notify_send(qcs); |
| 1557 | qcs->flags &= ~QC_SF_BLK_MROOM; |
| 1558 | } |
| 1559 | |
| 1560 | qcs->tx.offset += xfer; |
Amaury Denoyelle | 78fa559 | 2022-06-10 15:18:12 +0200 | [diff] [blame] | 1561 | BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd); |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1562 | qcc->tx.offsets += xfer; |
Amaury Denoyelle | 78fa559 | 2022-06-10 15:18:12 +0200 | [diff] [blame] | 1563 | BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | /* out buffer cannot be emptied if qcs offsets differ. */ |
| 1567 | BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset); |
| 1568 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 1569 | /* FIN is set if all incoming data were transferred. */ |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1570 | fin = qcs_stream_fin(qcs); |
| 1571 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1572 | /* Build a new STREAM frame with <out> buffer. */ |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1573 | if (qcs->tx.sent_offset != qcs->tx.offset || fin) { |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1574 | int ret; |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1575 | ret = qcs_build_stream_frm(qcs, out, fin, frms); |
Amaury Denoyelle | b50f311 | 2022-04-28 14:41:50 +0200 | [diff] [blame] | 1576 | if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ } |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | return xfer; |
| 1580 | } |
| 1581 | |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1582 | /* Proceed to sending. Loop through all available streams for the <qcc> |
| 1583 | * instance and try to send as much as possible. |
| 1584 | * |
| 1585 | * Returns the total of bytes sent to the transport layer. |
| 1586 | */ |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1587 | static int qc_send(struct qcc *qcc) |
| 1588 | { |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1589 | struct list frms = LIST_HEAD_INIT(frms); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1590 | struct eb64_node *node; |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1591 | struct qcs *qcs, *qcs_tmp; |
| 1592 | int total = 0, tmp_total = 0; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1593 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1594 | TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn); |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 1595 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1596 | if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) { |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1597 | qcc->conn->flags |= CO_FL_ERROR; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1598 | TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn); |
| 1599 | goto err; |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1602 | if (!LIST_ISEMPTY(&qcc->lfctl.frms)) { |
| 1603 | if (qc_send_frames(qcc, &qcc->lfctl.frms)) { |
| 1604 | TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn); |
| 1605 | goto out; |
| 1606 | } |
| 1607 | } |
Amaury Denoyelle | c933780 | 2022-04-04 16:36:34 +0200 | [diff] [blame] | 1608 | |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1609 | if (qcc->flags & QC_CF_BLK_MFCTL) |
| 1610 | return 0; |
| 1611 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1612 | /* loop through all streams, construct STREAM frames if data available. |
| 1613 | * TODO optimize the loop to favor streams which are not too heavy. |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1614 | */ |
| 1615 | node = eb64_first(&qcc->streams_by_id); |
| 1616 | while (node) { |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1617 | int ret; |
Amaury Denoyelle | c6195d7 | 2022-05-23 11:39:14 +0200 | [diff] [blame] | 1618 | uint64_t id; |
| 1619 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1620 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | c6195d7 | 2022-05-23 11:39:14 +0200 | [diff] [blame] | 1621 | id = qcs->id; |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1622 | |
Amaury Denoyelle | c6195d7 | 2022-05-23 11:39:14 +0200 | [diff] [blame] | 1623 | if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) { |
Amaury Denoyelle | e2ec942 | 2022-03-10 16:46:18 +0100 | [diff] [blame] | 1624 | node = eb64_next(node); |
| 1625 | continue; |
| 1626 | } |
| 1627 | |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 1628 | if (qcs->flags & QC_SF_TO_RESET) { |
| 1629 | qcs_send_reset(qcs); |
| 1630 | node = eb64_next(node); |
| 1631 | continue; |
| 1632 | } |
| 1633 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1634 | if (qcs_is_close_local(qcs)) { |
| 1635 | node = eb64_next(node); |
| 1636 | continue; |
| 1637 | } |
| 1638 | |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 1639 | if (qcs->flags & QC_SF_BLK_SFCTL) { |
| 1640 | node = eb64_next(node); |
| 1641 | continue; |
| 1642 | } |
| 1643 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1644 | if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) { |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 1645 | node = eb64_next(node); |
| 1646 | continue; |
| 1647 | } |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1648 | |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1649 | ret = _qc_send_qcs(qcs, &frms); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1650 | total += ret; |
| 1651 | node = eb64_next(node); |
| 1652 | } |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1653 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1654 | if (qc_send_frames(qcc, &frms)) { |
| 1655 | /* data rejected by transport layer, do not retry. */ |
| 1656 | goto out; |
| 1657 | } |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1658 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1659 | retry: |
| 1660 | tmp_total = 0; |
| 1661 | list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) { |
| 1662 | int ret; |
| 1663 | BUG_ON(!b_data(&qcs->tx.buf)); |
| 1664 | BUG_ON(qc_stream_buf_get(qcs->stream)); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1665 | |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1666 | ret = _qc_send_qcs(qcs, &frms); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1667 | tmp_total += ret; |
| 1668 | LIST_DELETE(&qcs->el); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1669 | } |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 1670 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1671 | total += tmp_total; |
| 1672 | if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list)) |
| 1673 | goto retry; |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 1674 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1675 | out: |
Amaury Denoyelle | 43c090c | 2022-06-10 15:16:40 +0200 | [diff] [blame] | 1676 | /* Deallocate frames that the transport layer has rejected. */ |
| 1677 | if (!LIST_ISEMPTY(&frms)) { |
| 1678 | struct quic_frame *frm, *frm2; |
| 1679 | list_for_each_entry_safe(frm, frm2, &frms, list) { |
| 1680 | LIST_DELETE(&frm->list); |
| 1681 | pool_free(pool_head_quic_frame, frm); |
| 1682 | } |
| 1683 | } |
| 1684 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1685 | TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1686 | return total; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1687 | |
| 1688 | err: |
| 1689 | TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn); |
| 1690 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1691 | } |
| 1692 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1693 | /* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs |
| 1694 | * operation. |
| 1695 | * |
| 1696 | * Returns 0 on success else non-zero. |
| 1697 | */ |
| 1698 | static int qc_recv(struct qcc *qcc) |
| 1699 | { |
| 1700 | struct eb64_node *node; |
| 1701 | struct qcs *qcs; |
| 1702 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1703 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | e1cad8b | 2022-05-23 18:52:11 +0200 | [diff] [blame] | 1704 | |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 1705 | if (qcc->flags & QC_CF_CC_EMIT) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1706 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 1707 | return 0; |
| 1708 | } |
| 1709 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1710 | node = eb64_first(&qcc->streams_by_id); |
| 1711 | while (node) { |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1712 | uint64_t id; |
| 1713 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1714 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1715 | id = qcs->id; |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1716 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1717 | if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) { |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1718 | node = eb64_next(node); |
| 1719 | continue; |
| 1720 | } |
| 1721 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1722 | if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) { |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1723 | node = eb64_next(node); |
| 1724 | continue; |
| 1725 | } |
| 1726 | |
| 1727 | qcc_decode_qcs(qcc, qcs); |
| 1728 | node = eb64_next(node); |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 1729 | |
| 1730 | if (qcs->flags & QC_SF_READ_ABORTED) { |
| 1731 | /* TODO should send a STOP_SENDING */ |
| 1732 | qcs_free(qcs); |
| 1733 | } |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1734 | } |
| 1735 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1736 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1737 | return 0; |
| 1738 | } |
| 1739 | |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1740 | |
| 1741 | /* Release all streams which have their transfer operation achieved. |
Amaury Denoyelle | 6a4aebf | 2022-02-01 10:16:05 +0100 | [diff] [blame] | 1742 | * |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1743 | * Returns true if at least one stream is released. |
Amaury Denoyelle | 6a4aebf | 2022-02-01 10:16:05 +0100 | [diff] [blame] | 1744 | */ |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1745 | static int qc_purge_streams(struct qcc *qcc) |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1746 | { |
| 1747 | struct eb64_node *node; |
| 1748 | int release = 0; |
| 1749 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1750 | TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1751 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1752 | node = eb64_first(&qcc->streams_by_id); |
| 1753 | while (node) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1754 | struct qcs *qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1755 | node = eb64_next(node); |
| 1756 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1757 | /* Release not attached closed streams. */ |
| 1758 | if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 1759 | TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1760 | qcs_destroy(qcs); |
| 1761 | release = 1; |
| 1762 | continue; |
| 1763 | } |
| 1764 | |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1765 | /* Release detached streams with empty buffer. */ |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1766 | if (qcs->flags & QC_SF_DETACH) { |
Amaury Denoyelle | 20d1f84 | 2022-07-11 11:23:17 +0200 | [diff] [blame] | 1767 | if (qcs_is_close_local(qcs)) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 1768 | TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1769 | qcs_destroy(qcs); |
| 1770 | release = 1; |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1771 | continue; |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1772 | } |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1773 | |
| 1774 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 1775 | SUB_RETRY_SEND, &qcc->wait_event); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1779 | TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1780 | return release; |
| 1781 | } |
| 1782 | |
Amaury Denoyelle | c49d5d1 | 2022-07-15 10:32:53 +0200 | [diff] [blame] | 1783 | /* release function. This one should be called to free all resources allocated |
| 1784 | * to the mux. |
| 1785 | */ |
| 1786 | static void qc_release(struct qcc *qcc) |
| 1787 | { |
| 1788 | struct connection *conn = qcc->conn; |
| 1789 | struct eb64_node *node; |
| 1790 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1791 | TRACE_ENTER(QMUX_EV_QCC_END, conn); |
Amaury Denoyelle | c49d5d1 | 2022-07-15 10:32:53 +0200 | [diff] [blame] | 1792 | |
| 1793 | if (qcc->app_ops && qcc->app_ops->release) { |
| 1794 | /* Application protocol with dedicated connection closing |
| 1795 | * procedure. |
| 1796 | */ |
| 1797 | qcc->app_ops->release(qcc->ctx); |
Amaury Denoyelle | a154dc0 | 2022-06-13 17:09:01 +0200 | [diff] [blame] | 1798 | |
| 1799 | /* useful if application protocol should emit some closing |
| 1800 | * frames. For example HTTP/3 GOAWAY frame. |
| 1801 | */ |
| 1802 | qc_send(qcc); |
Amaury Denoyelle | c49d5d1 | 2022-07-15 10:32:53 +0200 | [diff] [blame] | 1803 | } |
| 1804 | else { |
| 1805 | qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0); |
| 1806 | } |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1807 | TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn); |
Amaury Denoyelle | c49d5d1 | 2022-07-15 10:32:53 +0200 | [diff] [blame] | 1808 | |
| 1809 | if (qcc->task) { |
| 1810 | task_destroy(qcc->task); |
| 1811 | qcc->task = NULL; |
| 1812 | } |
| 1813 | |
| 1814 | if (qcc->wait_event.tasklet) |
| 1815 | tasklet_free(qcc->wait_event.tasklet); |
| 1816 | if (conn && qcc->wait_event.events) { |
| 1817 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, |
| 1818 | qcc->wait_event.events, |
| 1819 | &qcc->wait_event); |
| 1820 | } |
| 1821 | |
| 1822 | /* liberate remaining qcs instances */ |
| 1823 | node = eb64_first(&qcc->streams_by_id); |
| 1824 | while (node) { |
| 1825 | struct qcs *qcs = eb64_entry(node, struct qcs, by_id); |
| 1826 | node = eb64_next(node); |
| 1827 | qcs_free(qcs); |
| 1828 | } |
| 1829 | |
| 1830 | while (!LIST_ISEMPTY(&qcc->lfctl.frms)) { |
| 1831 | struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list); |
| 1832 | LIST_DELETE(&frm->list); |
| 1833 | pool_free(pool_head_quic_frame, frm); |
| 1834 | } |
| 1835 | |
| 1836 | pool_free(pool_head_qcc, qcc); |
| 1837 | |
| 1838 | if (conn) { |
| 1839 | LIST_DEL_INIT(&conn->stopping_list); |
| 1840 | |
| 1841 | conn->handle.qc->conn = NULL; |
| 1842 | conn->mux = NULL; |
| 1843 | conn->ctx = NULL; |
| 1844 | |
| 1845 | TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn); |
| 1846 | |
| 1847 | conn_stop_tracking(conn); |
| 1848 | conn_full_close(conn); |
| 1849 | if (conn->destroy_cb) |
| 1850 | conn->destroy_cb(conn); |
| 1851 | conn_free(conn); |
| 1852 | } |
| 1853 | |
| 1854 | TRACE_LEAVE(QMUX_EV_QCC_END); |
| 1855 | } |
| 1856 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1857 | static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status) |
| 1858 | { |
Amaury Denoyelle | 769e9ff | 2021-10-05 11:43:50 +0200 | [diff] [blame] | 1859 | struct qcc *qcc = ctx; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1860 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1861 | TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1862 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1863 | qc_send(qcc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1864 | |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1865 | if (qc_purge_streams(qcc)) { |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1866 | if (qcc_is_dead(qcc)) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1867 | TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1868 | goto release; |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1869 | } |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1870 | } |
| 1871 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1872 | qc_recv(qcc); |
| 1873 | |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 1874 | /* TODO check if qcc proxy is disabled. If yes, use graceful shutdown |
| 1875 | * to close the connection. |
| 1876 | */ |
| 1877 | |
| 1878 | qcc_refresh_timeout(qcc); |
| 1879 | |
Amaury Denoyelle | d397385 | 2022-07-25 14:56:54 +0200 | [diff] [blame] | 1880 | end: |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1881 | TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn); |
| 1882 | return NULL; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1883 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1884 | release: |
| 1885 | qc_release(qcc); |
| 1886 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1887 | return NULL; |
| 1888 | } |
| 1889 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1890 | static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state) |
| 1891 | { |
| 1892 | struct qcc *qcc = ctx; |
| 1893 | int expired = tick_is_expired(t->expire, now_ms); |
| 1894 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1895 | TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1896 | |
| 1897 | if (qcc) { |
| 1898 | if (!expired) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1899 | TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn); |
| 1900 | goto requeue; |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | if (!qcc_may_expire(qcc)) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1904 | TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1905 | t->expire = TICK_ETERNITY; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1906 | goto requeue; |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1910 | task_destroy(t); |
Amaury Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 1911 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1912 | if (!qcc) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1913 | TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE); |
| 1914 | goto out; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1915 | } |
Amaury Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 1916 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1917 | qcc->task = NULL; |
| 1918 | |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 1919 | /* TODO depending on the timeout condition, different shutdown mode |
| 1920 | * should be used. For http keep-alive or disabled proxy, a graceful |
| 1921 | * shutdown should occurs. For all other cases, an immediate close |
| 1922 | * seems legitimate. |
| 1923 | */ |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1924 | if (qcc_is_dead(qcc)) { |
| 1925 | TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1926 | qc_release(qcc); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1927 | } |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1928 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1929 | out: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1930 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1931 | return NULL; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1932 | |
| 1933 | requeue: |
| 1934 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
| 1935 | return t; |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1936 | } |
| 1937 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1938 | static int qc_init(struct connection *conn, struct proxy *prx, |
| 1939 | struct session *sess, struct buffer *input) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1940 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1941 | struct qcc *qcc; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 1942 | struct quic_transport_params *lparams, *rparams; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1943 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1944 | TRACE_ENTER(QMUX_EV_QCC_NEW); |
| 1945 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1946 | qcc = pool_alloc(pool_head_qcc); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1947 | if (!qcc) { |
| 1948 | TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1949 | goto fail_no_qcc; |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1950 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1951 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1952 | qcc->conn = conn; |
| 1953 | conn->ctx = qcc; |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 1954 | qcc->nb_hreq = qcc->nb_sc = 0; |
Amaury Denoyelle | ce1f30d | 2022-02-01 15:14:24 +0100 | [diff] [blame] | 1955 | qcc->flags = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1956 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1957 | qcc->app_ops = NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1958 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1959 | qcc->streams_by_id = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1960 | |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1961 | /* Server parameters, params used for RX flow control. */ |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1962 | lparams = &conn->handle.qc->rx.params; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1963 | |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1964 | qcc->rx.max_data = lparams->initial_max_data; |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1965 | qcc->tx.sent_offsets = qcc->tx.offsets = 0; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1966 | |
| 1967 | /* Client initiated streams must respect the server flow control. */ |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1968 | qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1969 | qcc->strms[QCS_CLT_BIDI].nb_streams = 0; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1970 | qcc->strms[QCS_CLT_BIDI].rx.max_data = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1971 | qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1972 | |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1973 | qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1974 | qcc->strms[QCS_CLT_UNI].nb_streams = 0; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1975 | qcc->strms[QCS_CLT_UNI].rx.max_data = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1976 | qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1977 | |
| 1978 | /* Server initiated streams must respect the server flow control. */ |
| 1979 | qcc->strms[QCS_SRV_BIDI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1980 | qcc->strms[QCS_SRV_BIDI].nb_streams = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1981 | qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1982 | qcc->strms[QCS_SRV_BIDI].tx.max_data = 0; |
| 1983 | |
| 1984 | qcc->strms[QCS_SRV_UNI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1985 | qcc->strms[QCS_SRV_UNI].nb_streams = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1986 | qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1987 | qcc->strms[QCS_SRV_UNI].tx.max_data = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1988 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1989 | LIST_INIT(&qcc->lfctl.frms); |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 1990 | qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi; |
Amaury Denoyelle | bf3c208 | 2022-08-16 11:29:08 +0200 | [diff] [blame] | 1991 | qcc->lfctl.ms_uni = lparams->initial_max_streams_uni; |
Amaury Denoyelle | 44d0912 | 2022-04-26 11:21:10 +0200 | [diff] [blame] | 1992 | qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local; |
| 1993 | qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote; |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 1994 | qcc->lfctl.cl_bidi_r = 0; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1995 | |
Amaury Denoyelle | c830e1e | 2022-05-16 16:19:59 +0200 | [diff] [blame] | 1996 | qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data; |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 1997 | qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0; |
Amaury Denoyelle | c830e1e | 2022-05-16 16:19:59 +0200 | [diff] [blame] | 1998 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1999 | rparams = &conn->handle.qc->tx.params; |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 2000 | qcc->rfctl.md = rparams->initial_max_data; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 2001 | qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local; |
| 2002 | qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote; |
| 2003 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 2004 | if (conn_is_back(conn)) { |
| 2005 | qcc->next_bidi_l = 0x00; |
| 2006 | qcc->largest_bidi_r = 0x01; |
| 2007 | qcc->next_uni_l = 0x02; |
| 2008 | qcc->largest_uni_r = 0x03; |
| 2009 | } |
| 2010 | else { |
| 2011 | qcc->largest_bidi_r = 0x00; |
| 2012 | qcc->next_bidi_l = 0x01; |
| 2013 | qcc->largest_uni_r = 0x02; |
| 2014 | qcc->next_uni_l = 0x03; |
| 2015 | } |
| 2016 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2017 | qcc->wait_event.tasklet = tasklet_new(); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2018 | if (!qcc->wait_event.tasklet) { |
| 2019 | TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2020 | goto fail_no_tasklet; |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2021 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2022 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 2023 | LIST_INIT(&qcc->send_retry_list); |
| 2024 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2025 | qcc->subs = NULL; |
| 2026 | qcc->wait_event.tasklet->process = qc_io_cb; |
| 2027 | qcc->wait_event.tasklet->context = qcc; |
Frédéric Lécaille | f27b66f | 2022-03-18 22:49:22 +0100 | [diff] [blame] | 2028 | qcc->wait_event.events = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2029 | |
Amaury Denoyelle | 07bf8f4 | 2022-07-22 16:16:03 +0200 | [diff] [blame] | 2030 | qcc->proxy = prx; |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2031 | /* haproxy timeouts */ |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 2032 | qcc->task = NULL; |
Amaury Denoyelle | b630945 | 2022-07-25 14:51:56 +0200 | [diff] [blame] | 2033 | qcc->timeout = conn_is_back(qcc->conn) ? prx->timeout.server : |
| 2034 | prx->timeout.client; |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 2035 | if (tick_isset(qcc->timeout)) { |
| 2036 | qcc->task = task_new_here(); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2037 | if (!qcc->task) { |
| 2038 | TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW); |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 2039 | goto fail_no_timeout_task; |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2040 | } |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 2041 | qcc->task->process = qc_timeout_task; |
| 2042 | qcc->task->context = qcc; |
| 2043 | qcc->task->expire = tick_add(now_ms, qcc->timeout); |
| 2044 | } |
Amaury Denoyelle | bd6ec1b | 2022-07-25 11:53:18 +0200 | [diff] [blame] | 2045 | qcc_reset_idle_start(qcc); |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 2046 | LIST_INIT(&qcc->opening_list); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2047 | |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2048 | if (!conn_is_back(conn)) { |
| 2049 | if (!LIST_INLIST(&conn->stopping_list)) { |
| 2050 | LIST_APPEND(&mux_stopping_data[tid].list, |
| 2051 | &conn->stopping_list); |
| 2052 | } |
| 2053 | } |
| 2054 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 2055 | HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2056 | /* init read cycle */ |
| 2057 | tasklet_wakeup(qcc->wait_event.tasklet); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2058 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2059 | TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2060 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2061 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2062 | fail_no_timeout_task: |
| 2063 | tasklet_free(qcc->wait_event.tasklet); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2064 | fail_no_tasklet: |
| 2065 | pool_free(pool_head_qcc, qcc); |
| 2066 | fail_no_qcc: |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2067 | TRACE_LEAVE(QMUX_EV_QCC_NEW); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2068 | return -1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2069 | } |
| 2070 | |
Amaury Denoyelle | 2461bd5 | 2022-04-13 16:54:52 +0200 | [diff] [blame] | 2071 | static void qc_destroy(void *ctx) |
| 2072 | { |
| 2073 | struct qcc *qcc = ctx; |
| 2074 | |
| 2075 | TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn); |
| 2076 | qc_release(qcc); |
| 2077 | TRACE_LEAVE(QMUX_EV_QCC_END); |
| 2078 | } |
| 2079 | |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2080 | static void qc_detach(struct sedesc *sd) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2081 | { |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2082 | struct qcs *qcs = sd->se; |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 2083 | struct qcc *qcc = qcs->qcc; |
| 2084 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2085 | TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs); |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 2086 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 2087 | /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach |
| 2088 | * from the stream even if it is not closed remotely at the QUIC layer. |
| 2089 | * This happens for example when a stream must be closed due to a |
| 2090 | * rejected request. To better handle these cases, it will be required |
| 2091 | * to implement shutr/shutw MUX operations. Once this is done, this |
| 2092 | * BUG_ON_HOT() statement can be adjusted. |
| 2093 | */ |
| 2094 | //BUG_ON_HOT(!qcs_is_close_remote(qcs)); |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 2095 | |
| 2096 | qcc_rm_sc(qcc); |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 2097 | |
Amaury Denoyelle | 20d1f84 | 2022-07-11 11:23:17 +0200 | [diff] [blame] | 2098 | if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 2099 | TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 2100 | qcs->flags |= QC_SF_DETACH; |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2101 | qcc_refresh_timeout(qcc); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2102 | |
| 2103 | TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 2104 | return; |
| 2105 | } |
| 2106 | |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 2107 | qcs_destroy(qcs); |
Amaury Denoyelle | 1136e92 | 2022-02-01 10:33:09 +0100 | [diff] [blame] | 2108 | |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 2109 | if (qcc_is_dead(qcc)) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 2110 | TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn); |
Amaury Denoyelle | 35a66c0 | 2022-08-12 15:56:21 +0200 | [diff] [blame] | 2111 | goto release; |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 2112 | } |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 2113 | else if (qcc->task) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2114 | TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn); |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2115 | qcc_refresh_timeout(qcc); |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 2116 | } |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2117 | else { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2118 | TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn); |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2119 | } |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2120 | |
| 2121 | TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn); |
Amaury Denoyelle | 35a66c0 | 2022-08-12 15:56:21 +0200 | [diff] [blame] | 2122 | return; |
| 2123 | |
| 2124 | release: |
| 2125 | qc_release(qcc); |
| 2126 | TRACE_LEAVE(QMUX_EV_STRM_END); |
| 2127 | return; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2128 | } |
| 2129 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2130 | /* Called from the upper layer, to receive data */ |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2131 | static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2132 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2133 | { |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2134 | struct qcs *qcs = __sc_mux_strm(sc); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2135 | struct htx *qcs_htx = NULL; |
| 2136 | struct htx *cs_htx = NULL; |
| 2137 | size_t ret = 0; |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 2138 | char fin = 0; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2139 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2140 | TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2141 | |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2142 | qcs_htx = htx_from_buf(&qcs->rx.app_buf); |
| 2143 | if (htx_is_empty(qcs_htx)) { |
| 2144 | /* Set buffer data to 0 as HTX is empty. */ |
| 2145 | htx_to_buf(qcs_htx, &qcs->rx.app_buf); |
| 2146 | goto end; |
| 2147 | } |
| 2148 | |
| 2149 | ret = qcs_htx->data; |
| 2150 | |
| 2151 | cs_htx = htx_from_buf(buf); |
| 2152 | if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) { |
Amaury Denoyelle | 72a78e8 | 2022-07-29 15:34:12 +0200 | [diff] [blame] | 2153 | /* EOM will be copied to cs_htx via b_xfer(). */ |
| 2154 | if (qcs_htx->flags & HTX_FL_EOM) |
| 2155 | fin = 1; |
| 2156 | |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2157 | htx_to_buf(cs_htx, buf); |
| 2158 | htx_to_buf(qcs_htx, &qcs->rx.app_buf); |
| 2159 | b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf)); |
| 2160 | goto end; |
| 2161 | } |
| 2162 | |
| 2163 | htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED); |
| 2164 | BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR); |
| 2165 | |
| 2166 | /* Copy EOM from src to dst buffer if all data copied. */ |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 2167 | if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) { |
| 2168 | cs_htx->flags |= HTX_FL_EOM; |
| 2169 | fin = 1; |
| 2170 | } |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2171 | |
| 2172 | cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0; |
| 2173 | htx_to_buf(cs_htx, buf); |
| 2174 | htx_to_buf(qcs_htx, &qcs->rx.app_buf); |
| 2175 | ret -= qcs_htx->data; |
| 2176 | |
| 2177 | end: |
| 2178 | if (b_data(&qcs->rx.app_buf)) { |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2179 | se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2180 | } |
| 2181 | else { |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2182 | se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM); |
| 2183 | if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING)) |
| 2184 | se_fl_set(qcs->sd, SE_FL_ERROR); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2185 | |
Amaury Denoyelle | 72a78e8 | 2022-07-29 15:34:12 +0200 | [diff] [blame] | 2186 | /* Set end-of-input if FIN received and all data extracted. */ |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 2187 | if (fin) |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2188 | se_fl_set(qcs->sd, SE_FL_EOI); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2189 | |
| 2190 | if (b_size(&qcs->rx.app_buf)) { |
| 2191 | b_free(&qcs->rx.app_buf); |
| 2192 | offer_buffers(NULL, 1); |
| 2193 | } |
| 2194 | } |
| 2195 | |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 2196 | if (ret) { |
| 2197 | qcs->flags &= ~QC_SF_DEM_FULL; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2198 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 2199 | } |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2200 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2201 | TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs); |
| 2202 | |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2203 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2204 | } |
| 2205 | |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2206 | static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2207 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2208 | { |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2209 | struct qcs *qcs = __sc_mux_strm(sc); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2210 | size_t ret; |
| 2211 | |
| 2212 | TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2213 | |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 2214 | if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) { |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 2215 | ret = count; |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 2216 | goto end; |
| 2217 | } |
| 2218 | |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2219 | ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2220 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 2221 | end: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2222 | TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs); |
| 2223 | |
| 2224 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 2228 | * event subscriber <es> is not allowed to change from a previous call as long |
| 2229 | * as at least one event is still subscribed. The <event_type> must only be a |
| 2230 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 2231 | */ |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2232 | static int qc_subscribe(struct stconn *sc, int event_type, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2233 | struct wait_event *es) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2234 | { |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2235 | return qcs_subscribe(__sc_mux_strm(sc), event_type, es); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 2239 | * The <es> pointer is not allowed to differ from the one passed to the |
| 2240 | * subscribe() call. It always returns zero. |
| 2241 | */ |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2242 | static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2243 | { |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2244 | struct qcs *qcs = __sc_mux_strm(sc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2245 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2246 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 2247 | BUG_ON(qcs->subs && qcs->subs != es); |
| 2248 | |
| 2249 | es->events &= ~event_type; |
| 2250 | if (!es->events) |
| 2251 | qcs->subs = NULL; |
| 2252 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2253 | return 0; |
| 2254 | } |
| 2255 | |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2256 | /* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection, |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2257 | * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and |
| 2258 | * wake them. |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2259 | */ |
| 2260 | static int qc_wake_some_streams(struct qcc *qcc) |
| 2261 | { |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2262 | struct qcs *qcs; |
| 2263 | struct eb64_node *node; |
| 2264 | |
| 2265 | for (node = eb64_first(&qcc->streams_by_id); node; |
| 2266 | node = eb64_next(node)) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 2267 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2268 | |
Amaury Denoyelle | 3abeb57 | 2022-07-04 11:42:27 +0200 | [diff] [blame] | 2269 | if (!qcs_sc(qcs)) |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2270 | continue; |
| 2271 | |
| 2272 | if (qcc->conn->flags & CO_FL_ERROR) { |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2273 | se_fl_set(qcs->sd, SE_FL_ERR_PENDING); |
| 2274 | if (se_fl_test(qcs->sd, SE_FL_EOS)) |
| 2275 | se_fl_set(qcs->sd, SE_FL_ERROR); |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2276 | |
Amaury Denoyelle | 4561f84 | 2022-07-06 14:54:34 +0200 | [diff] [blame] | 2277 | qcs_alert(qcs); |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | return 0; |
| 2282 | } |
| 2283 | |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2284 | static int qc_wake(struct connection *conn) |
| 2285 | { |
| 2286 | struct qcc *qcc = conn->ctx; |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 2287 | struct proxy *prx = conn->handle.qc->li->bind_conf->frontend; |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2288 | |
| 2289 | TRACE_ENTER(QMUX_EV_QCC_WAKE, conn); |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2290 | |
| 2291 | /* Check if a soft-stop is in progress. |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2292 | * |
| 2293 | * TODO this is revelant for frontend connections only. |
Amaury Denoyelle | d0c6232 | 2022-05-23 08:52:58 +0200 | [diff] [blame] | 2294 | * |
| 2295 | * TODO Client should be notified with a H3 GOAWAY and then a |
| 2296 | * CONNECTION_CLOSE. However, quic-conn uses the listener socket for |
| 2297 | * sending which at this stage is already closed. |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2298 | */ |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2299 | if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) |
Amaury Denoyelle | d0c6232 | 2022-05-23 08:52:58 +0200 | [diff] [blame] | 2300 | qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH); |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2301 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 2302 | if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2303 | qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH); |
| 2304 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2305 | qc_send(qcc); |
| 2306 | |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2307 | qc_wake_some_streams(qcc); |
| 2308 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2309 | if (qcc_is_dead(qcc)) |
| 2310 | goto release; |
| 2311 | |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2312 | qcc_refresh_timeout(qcc); |
| 2313 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2314 | TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn); |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2315 | return 0; |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2316 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2317 | release: |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2318 | TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2319 | qc_release(qcc); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2320 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2321 | return 1; |
| 2322 | } |
| 2323 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 2324 | |
| 2325 | static char *qcs_st_to_str(enum qcs_state st) |
| 2326 | { |
| 2327 | switch (st) { |
| 2328 | case QC_SS_IDLE: return "IDL"; |
| 2329 | case QC_SS_OPEN: return "OPN"; |
| 2330 | case QC_SS_HLOC: return "HCL"; |
| 2331 | case QC_SS_HREM: return "HCR"; |
| 2332 | case QC_SS_CLO: return "CLO"; |
| 2333 | default: return "???"; |
| 2334 | } |
| 2335 | } |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 2336 | |
Amaury Denoyelle | fa29f33 | 2022-03-25 09:09:40 +0100 | [diff] [blame] | 2337 | static void qmux_trace_frm(const struct quic_frame *frm) |
| 2338 | { |
| 2339 | switch (frm->type) { |
| 2340 | case QUIC_FT_MAX_STREAMS_BIDI: |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 2341 | chunk_appendf(&trace_buf, " max_streams=%llu", |
| 2342 | (ull)frm->max_streams_bidi.max_streams); |
Amaury Denoyelle | fa29f33 | 2022-03-25 09:09:40 +0100 | [diff] [blame] | 2343 | break; |
| 2344 | |
| 2345 | case QUIC_FT_MAX_STREAMS_UNI: |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 2346 | chunk_appendf(&trace_buf, " max_streams=%llu", |
| 2347 | (ull)frm->max_streams_uni.max_streams); |
Amaury Denoyelle | fa29f33 | 2022-03-25 09:09:40 +0100 | [diff] [blame] | 2348 | break; |
| 2349 | |
| 2350 | default: |
| 2351 | break; |
| 2352 | } |
| 2353 | } |
| 2354 | |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 2355 | /* quic-mux trace handler */ |
| 2356 | static void qmux_trace(enum trace_level level, uint64_t mask, |
| 2357 | const struct trace_source *src, |
| 2358 | const struct ist where, const struct ist func, |
| 2359 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 2360 | { |
| 2361 | const struct connection *conn = a1; |
| 2362 | const struct qcc *qcc = conn ? conn->ctx : NULL; |
| 2363 | const struct qcs *qcs = a2; |
| 2364 | |
| 2365 | if (!qcc) |
| 2366 | return; |
| 2367 | |
| 2368 | if (src->verbosity > QMUX_VERB_CLEAN) { |
| 2369 | chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc); |
| 2370 | |
| 2371 | if (qcs) |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 2372 | chunk_appendf(&trace_buf, " qcs=%p .id=%llu .st=%s", |
| 2373 | qcs, (ull)qcs->id, |
| 2374 | qcs_st_to_str(qcs->st)); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2375 | |
| 2376 | if (mask & QMUX_EV_QCC_NQCS) { |
| 2377 | const uint64_t *id = a3; |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 2378 | chunk_appendf(&trace_buf, " id=%llu", (ull)*id); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2379 | } |
Amaury Denoyelle | fa29f33 | 2022-03-25 09:09:40 +0100 | [diff] [blame] | 2380 | |
| 2381 | if (mask & QMUX_EV_SEND_FRM) |
| 2382 | qmux_trace_frm(a3); |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 2383 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 2384 | if (mask & QMUX_EV_QCS_XFER_DATA) { |
| 2385 | const struct qcs_xfer_data_trace_arg *arg = a3; |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 2386 | chunk_appendf(&trace_buf, " prep=%llu xfer=%d", |
| 2387 | (ull)arg->prep, arg->xfer); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | if (mask & QMUX_EV_QCS_BUILD_STRM) { |
| 2391 | const struct qcs_build_stream_trace_arg *arg = a3; |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 2392 | chunk_appendf(&trace_buf, " len=%llu fin=%d offset=%llu", |
| 2393 | (ull)arg->len, arg->fin, (ull)arg->offset); |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 2394 | } |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2399 | static const struct mux_ops qc_ops = { |
| 2400 | .init = qc_init, |
Amaury Denoyelle | 2461bd5 | 2022-04-13 16:54:52 +0200 | [diff] [blame] | 2401 | .destroy = qc_destroy, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2402 | .detach = qc_detach, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2403 | .rcv_buf = qc_rcv_buf, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2404 | .snd_buf = qc_snd_buf, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2405 | .subscribe = qc_subscribe, |
| 2406 | .unsubscribe = qc_unsubscribe, |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2407 | .wake = qc_wake, |
Willy Tarreau | b5821e1 | 2022-04-26 11:54:08 +0200 | [diff] [blame] | 2408 | .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED, |
Willy Tarreau | 671bd5a | 2022-04-11 09:29:21 +0200 | [diff] [blame] | 2409 | .name = "QUIC", |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2410 | }; |
| 2411 | |
| 2412 | static struct mux_proto_list mux_proto_quic = |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2413 | { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops }; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2414 | |
| 2415 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic); |