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> |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 7 | #include <haproxy/conn_stream.h> |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 8 | #include <haproxy/dynbuf.h> |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 9 | #include <haproxy/htx.h> |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 10 | #include <haproxy/list.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> |
Amaury Denoyelle | 251eadf | 2022-03-24 17:14:52 +0100 | [diff] [blame] | 13 | #include <haproxy/sink.h> |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 14 | #include <haproxy/ssl_sock-t.h> |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 15 | #include <haproxy/trace.h> |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 16 | #include <haproxy/xprt_quic.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 17 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 18 | DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc)); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 19 | DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs)); |
| 20 | |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 21 | /* trace source and events */ |
| 22 | static void qmux_trace(enum trace_level level, uint64_t mask, |
| 23 | const struct trace_source *src, |
| 24 | const struct ist where, const struct ist func, |
| 25 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 26 | |
| 27 | static const struct trace_event qmux_trace_events[] = { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 28 | #define QMUX_EV_QCC_RECV (1ULL << 1) |
| 29 | { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" }, |
| 30 | #define QMUX_EV_QCC_SEND (1ULL << 2) |
| 31 | { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" }, |
| 32 | #define QMUX_EV_QCC_WAKE (1ULL << 3) |
| 33 | { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" }, |
| 34 | #define QMUX_EV_QCC_END (1ULL << 4) |
| 35 | { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" }, |
| 36 | #define QMUX_EV_QCC_NQCS (1ULL << 5) |
| 37 | { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" }, |
| 38 | #define QMUX_EV_QCS_NEW (1ULL << 6) |
| 39 | { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" }, |
| 40 | #define QMUX_EV_QCS_RECV (1ULL << 7) |
| 41 | { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" }, |
| 42 | #define QMUX_EV_QCS_SEND (1ULL << 8) |
| 43 | { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" }, |
| 44 | #define QMUX_EV_QCS_END (1ULL << 9) |
| 45 | { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" }, |
| 46 | #define QMUX_EV_STRM_RECV (1ULL << 10) |
| 47 | { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" }, |
| 48 | #define QMUX_EV_STRM_SEND (1ULL << 11) |
| 49 | { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" }, |
| 50 | #define QMUX_EV_STRM_END (1ULL << 12) |
| 51 | { .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] | 52 | #define QMUX_EV_SEND_FRM (1ULL << 13) |
| 53 | { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" }, |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 54 | /* special event dedicated to qcs_xfer_data */ |
| 55 | #define QMUX_EV_QCS_XFER_DATA (1ULL << 14) |
| 56 | { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" }, |
| 57 | /* special event dedicated to qcs_build_stream_frm */ |
| 58 | #define QMUX_EV_QCS_BUILD_STRM (1ULL << 15) |
| 59 | { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" }, |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 60 | { } |
| 61 | }; |
| 62 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 63 | /* custom arg for QMUX_EV_QCS_XFER_DATA */ |
| 64 | struct qcs_xfer_data_trace_arg { |
| 65 | size_t prep; |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 66 | int xfer; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 67 | }; |
| 68 | /* custom arg for QMUX_EV_QCS_BUILD_STRM */ |
| 69 | struct qcs_build_stream_trace_arg { |
| 70 | size_t len; |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 71 | char fin; |
| 72 | uint64_t offset; |
| 73 | }; |
| 74 | |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 75 | static const struct name_desc qmux_trace_lockon_args[4] = { |
| 76 | /* arg1 */ { /* already used by the connection */ }, |
| 77 | /* arg2 */ { .name="qcs", .desc="QUIC stream" }, |
| 78 | /* arg3 */ { }, |
| 79 | /* arg4 */ { } |
| 80 | }; |
| 81 | |
| 82 | static const struct name_desc qmux_trace_decoding[] = { |
| 83 | #define QMUX_VERB_CLEAN 1 |
| 84 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 85 | #define QMUX_VERB_MINIMAL 2 |
| 86 | { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" }, |
| 87 | { /* end */ } |
| 88 | }; |
| 89 | |
| 90 | struct trace_source trace_qmux = { |
| 91 | .name = IST("qmux"), |
| 92 | .desc = "QUIC multiplexer", |
| 93 | .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */ |
| 94 | .default_cb = qmux_trace, |
| 95 | .known_events = qmux_trace_events, |
| 96 | .lockon_args = qmux_trace_lockon_args, |
| 97 | .decoding = qmux_trace_decoding, |
| 98 | .report_events = ~0, /* report everything by default */ |
| 99 | }; |
| 100 | |
| 101 | #define TRACE_SOURCE &trace_qmux |
| 102 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 103 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 104 | /* Allocate a new QUIC streams with id <id> and type <type>. */ |
| 105 | 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] | 106 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 107 | struct qcs *qcs; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 108 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 109 | TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn); |
| 110 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 111 | qcs = pool_alloc(pool_head_qcs); |
| 112 | if (!qcs) |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 113 | return NULL; |
| 114 | |
| 115 | qcs->stream = NULL; |
| 116 | qcs->qcc = qcc; |
| 117 | qcs->cs = NULL; |
| 118 | qcs->flags = QC_SF_NONE; |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 119 | qcs->ctx = NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 120 | |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 121 | /* allocate transport layer stream descriptor |
| 122 | * |
| 123 | * TODO qc_stream_desc is only useful for Tx buffering. It should not |
| 124 | * be required for unidirectional remote streams. |
| 125 | */ |
Frédéric Lécaille | 664741e | 2022-05-02 18:46:58 +0200 | [diff] [blame] | 126 | qcs->stream = qc_stream_desc_new(id, type, qcs, qcc->conn->handle.qc); |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 127 | if (!qcs->stream) |
| 128 | goto err; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 129 | |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 130 | if (qcc->app_ops->attach) { |
| 131 | if (qcc->app_ops->attach(qcs)) |
| 132 | goto err; |
| 133 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 134 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 135 | qcs->endp = cs_endpoint_new(); |
| 136 | if (!qcs->endp) { |
| 137 | pool_free(pool_head_qcs, qcs); |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 138 | goto err; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 139 | } |
| 140 | qcs->endp->target = qcs; |
| 141 | qcs->endp->ctx = qcc->conn; |
| 142 | qcs->endp->flags |= (CS_EP_T_MUX|CS_EP_ORPHAN|CS_EP_NOT_FIRST); |
| 143 | |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 144 | qcs->id = qcs->by_id.key = id; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 145 | /* store transport layer stream descriptor in qcc tree */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 146 | eb64_insert(&qcc->streams_by_id, &qcs->by_id); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 147 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 148 | qcc->strms[type].nb_streams++; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 149 | |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 150 | /* If stream is local, use peer remote-limit, or else the opposite. */ |
| 151 | /* TODO use uni limit for unidirectional streams */ |
| 152 | qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r : |
| 153 | qcc->rfctl.msd_bidi_l; |
| 154 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 155 | qcs->rx.buf = BUF_NULL; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 156 | qcs->rx.app_buf = BUF_NULL; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 157 | qcs->rx.offset = 0; |
| 158 | qcs->rx.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 159 | |
Amaury Denoyelle | 44d0912 | 2022-04-26 11:21:10 +0200 | [diff] [blame] | 160 | /* TODO use uni limit for unidirectional streams */ |
| 161 | qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l : |
| 162 | qcc->lfctl.msd_bidi_r; |
| 163 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 164 | qcs->tx.buf = BUF_NULL; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 165 | qcs->tx.offset = 0; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 166 | qcs->tx.sent_offset = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 167 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 168 | qcs->wait_event.tasklet = NULL; |
| 169 | qcs->wait_event.events = 0; |
| 170 | qcs->subs = NULL; |
| 171 | |
| 172 | out: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 173 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 174 | return qcs; |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 175 | |
| 176 | err: |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 177 | if (qcs->ctx && qcc->app_ops->detach) |
| 178 | qcc->app_ops->detach(qcs); |
| 179 | |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 180 | if (qcs->stream) |
| 181 | qc_stream_desc_release(qcs->stream); |
| 182 | |
| 183 | pool_free(pool_head_qcs, qcs); |
| 184 | return NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 187 | /* Free a qcs. This function must only be done to remove a stream on allocation |
| 188 | * error or connection shutdown. Else use qcs_destroy which handle all the |
| 189 | * QUIC connection mechanism. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 190 | */ |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 191 | void qcs_free(struct qcs *qcs) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 192 | { |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 193 | b_free(&qcs->rx.buf); |
| 194 | b_free(&qcs->tx.buf); |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 195 | |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 196 | BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams); |
| 197 | --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams; |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 198 | |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 199 | if (qcs->ctx && qcs->qcc->app_ops->detach) |
| 200 | qcs->qcc->app_ops->detach(qcs); |
| 201 | |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 202 | qc_stream_desc_release(qcs->stream); |
| 203 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 204 | BUG_ON(qcs->endp && !(qcs->endp->flags & CS_EP_ORPHAN)); |
| 205 | cs_endpoint_free(qcs->endp); |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 206 | |
| 207 | eb64_delete(&qcs->by_id); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 208 | pool_free(pool_head_qcs, qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 211 | 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] | 212 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 213 | struct buffer *buf = b_alloc(bptr); |
| 214 | BUG_ON(!buf); |
| 215 | return buf; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 218 | int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es) |
| 219 | { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 220 | struct qcc *qcc = qcs->qcc; |
| 221 | |
| 222 | 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] | 223 | |
| 224 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 225 | BUG_ON(qcs->subs && qcs->subs != es); |
| 226 | |
| 227 | es->events |= event_type; |
| 228 | qcs->subs = es; |
| 229 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 230 | if (event_type & SUB_RETRY_RECV) |
| 231 | TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs); |
| 232 | |
| 233 | if (event_type & SUB_RETRY_SEND) |
| 234 | TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs); |
| 235 | |
| 236 | TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs); |
| 237 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | void qcs_notify_recv(struct qcs *qcs) |
| 242 | { |
| 243 | if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) { |
| 244 | tasklet_wakeup(qcs->subs->tasklet); |
| 245 | qcs->subs->events &= ~SUB_RETRY_RECV; |
| 246 | if (!qcs->subs->events) |
| 247 | qcs->subs = NULL; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void qcs_notify_send(struct qcs *qcs) |
| 252 | { |
| 253 | if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) { |
| 254 | tasklet_wakeup(qcs->subs->tasklet); |
| 255 | qcs->subs->events &= ~SUB_RETRY_SEND; |
| 256 | if (!qcs->subs->events) |
| 257 | qcs->subs = NULL; |
| 258 | } |
| 259 | } |
| 260 | |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 261 | /* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates |
| 262 | * several streams, depending on the already open ones. |
| 263 | * Return this node if succeeded, NULL if not. |
| 264 | */ |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 265 | struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id) |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 266 | { |
| 267 | unsigned int strm_type; |
| 268 | int64_t sub_id; |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 269 | struct eb64_node *node; |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 270 | struct qcs *qcs = NULL; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 271 | |
| 272 | strm_type = id & QCS_ID_TYPE_MASK; |
| 273 | sub_id = id >> QCS_ID_TYPE_SHIFT; |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 274 | node = NULL; |
Amaury Denoyelle | 0dc40f0 | 2022-02-07 11:44:17 +0100 | [diff] [blame] | 275 | if (quic_stream_is_local(qcc, id)) { |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 276 | /* Local streams: this stream must be already opened. */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 277 | node = eb64_lookup(&qcc->streams_by_id, id); |
| 278 | if (!node) { |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 279 | /* unknown stream id */ |
| 280 | goto out; |
| 281 | } |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 282 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 283 | } |
| 284 | else { |
| 285 | /* Remote streams. */ |
| 286 | struct eb_root *strms; |
| 287 | uint64_t largest_id; |
| 288 | enum qcs_type qcs_type; |
| 289 | |
| 290 | strms = &qcc->streams_by_id; |
| 291 | qcs_type = qcs_id_type(id); |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 292 | |
| 293 | /* TODO also checks max-streams for uni streams */ |
| 294 | if (quic_stream_is_bidi(id)) { |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 295 | if (sub_id + 1 > qcc->lfctl.ms_bidi) { |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 296 | /* streams limit reached */ |
| 297 | goto out; |
| 298 | } |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a |
| 302 | * correct value. |
| 303 | */ |
| 304 | largest_id = qcc->strms[qcs_type].largest_id; |
| 305 | if (sub_id > (int64_t)largest_id) { |
| 306 | /* RFC: "A stream ID that is used out of order results in all streams |
| 307 | * of that type with lower-numbered stream IDs also being opened". |
| 308 | * So, let's "open" these streams. |
| 309 | */ |
| 310 | int64_t i; |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 311 | struct qcs *tmp_qcs; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 312 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 313 | tmp_qcs = NULL; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 314 | for (i = largest_id + 1; i <= sub_id; i++) { |
| 315 | uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type; |
| 316 | enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 317 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 318 | tmp_qcs = qcs_new(qcc, id, type); |
| 319 | if (!tmp_qcs) { |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 320 | /* allocation failure */ |
| 321 | goto out; |
| 322 | } |
| 323 | |
| 324 | qcc->strms[qcs_type].largest_id = i; |
| 325 | } |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 326 | if (tmp_qcs) |
| 327 | qcs = tmp_qcs; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 328 | } |
| 329 | else { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 330 | node = eb64_lookup(strms, id); |
| 331 | if (node) |
| 332 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 336 | return qcs; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 337 | |
| 338 | out: |
| 339 | return NULL; |
| 340 | } |
| 341 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 342 | /* Handle a new STREAM frame <strm_frm>. The frame content will be copied in |
| 343 | * the buffer of the stream instance. The stream instance will be stored in |
| 344 | * <out_qcs>. In case of success, the caller can immediatly call qcc_decode_qcs |
| 345 | * to process the frame content. |
| 346 | * |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 347 | * Returns a code indicating how the frame was handled. |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 348 | * - 0: frame received completely and can be dropped. |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 349 | * - 1: frame not received but can be dropped. |
| 350 | * - 2: frame cannot be handled, either partially or not at all. <done> |
| 351 | * indicated the number of bytes handled. The rest should be buffered. |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 352 | */ |
| 353 | int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset, |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 354 | char fin, char *data, struct qcs **out_qcs, size_t *done) |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 355 | { |
| 356 | struct qcs *qcs; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 357 | size_t total, diff; |
| 358 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 359 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
| 360 | |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 361 | *out_qcs = NULL; |
| 362 | *done = 0; |
| 363 | |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 364 | qcs = qcc_get_qcs(qcc, id); |
| 365 | if (!qcs) { |
Frédéric Lécaille | b57de07 | 2022-05-02 18:58:27 +0200 | [diff] [blame] | 366 | if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) { |
| 367 | TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id); |
| 368 | return 0; |
| 369 | } |
| 370 | else { |
| 371 | TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id); |
| 372 | return 1; |
| 373 | } |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 374 | } |
| 375 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 376 | *out_qcs = qcs; |
| 377 | |
| 378 | if (offset > qcs->rx.offset) |
| 379 | return 2; |
| 380 | |
| 381 | if (offset + len <= qcs->rx.offset) { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 382 | TRACE_DEVEL("leaving on already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 74cf237 | 2022-04-29 15:58:22 +0200 | [diff] [blame] | 383 | return 0; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* Last frame already handled for this stream. */ |
| 387 | BUG_ON(qcs->flags & QC_SF_FIN_RECV); |
Amaury Denoyelle | 44d0912 | 2022-04-26 11:21:10 +0200 | [diff] [blame] | 388 | /* TODO initial max-stream-data overflow. Implement FLOW_CONTROL_ERROR emission. */ |
| 389 | BUG_ON(offset + len > qcs->rx.msd); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 390 | |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 391 | if (!qc_get_buf(qcs, &qcs->rx.buf) || b_full(&qcs->rx.buf)) { |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 392 | /* TODO should mark qcs as full */ |
| 393 | return 2; |
| 394 | } |
| 395 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 396 | TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 397 | diff = qcs->rx.offset - offset; |
| 398 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 399 | len -= diff; |
| 400 | data += diff; |
| 401 | |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 402 | /* TODO handle STREAM frames larger than RX buffer. */ |
| 403 | BUG_ON(len > b_size(&qcs->rx.buf)); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 404 | |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 405 | total = b_putblk(&qcs->rx.buf, data, len); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 406 | qcs->rx.offset += total; |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 407 | *done = total; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 408 | |
Amaury Denoyelle | 44d0912 | 2022-04-26 11:21:10 +0200 | [diff] [blame] | 409 | /* TODO initial max-stream-data reached. Implement MAX_STREAM_DATA emission. */ |
| 410 | BUG_ON(qcs->rx.offset == qcs->rx.msd); |
| 411 | |
Amaury Denoyelle | 3df8ca0 | 2022-04-26 11:36:40 +0200 | [diff] [blame] | 412 | if (total < len) { |
| 413 | TRACE_DEVEL("leaving on partially received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 414 | return 2; |
| 415 | } |
| 416 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 417 | if (fin) |
| 418 | qcs->flags |= QC_SF_FIN_RECV; |
| 419 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 420 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 424 | /* Handle a new MAX_DATA frame. <max> must contains the maximum data field of |
| 425 | * the frame. |
| 426 | * |
| 427 | * Returns 0 on success else non-zero. |
| 428 | */ |
| 429 | int qcc_recv_max_data(struct qcc *qcc, uint64_t max) |
| 430 | { |
| 431 | if (qcc->rfctl.md < max) { |
| 432 | qcc->rfctl.md = max; |
| 433 | |
| 434 | if (qcc->flags & QC_CF_BLK_MFCTL) { |
| 435 | qcc->flags &= ~QC_CF_BLK_MFCTL; |
| 436 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 437 | } |
| 438 | } |
| 439 | return 0; |
| 440 | } |
| 441 | |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 442 | /* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data |
| 443 | * field of the frame and <id> is the identifier of the QUIC stream. |
| 444 | * |
| 445 | * Returns 0 on success else non-zero. |
| 446 | */ |
| 447 | int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max) |
| 448 | { |
| 449 | struct qcs *qcs; |
| 450 | struct eb64_node *node; |
| 451 | |
| 452 | node = eb64_lookup(&qcc->streams_by_id, id); |
| 453 | if (node) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 454 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 455 | if (max > qcs->tx.msd) { |
| 456 | qcs->tx.msd = max; |
| 457 | |
| 458 | if (qcs->flags & QC_SF_BLK_SFCTL) { |
| 459 | qcs->flags &= ~QC_SF_BLK_SFCTL; |
| 460 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 468 | /* Decode the content of STREAM frames already received on the stream instance |
| 469 | * <qcs>. |
| 470 | * |
| 471 | * Returns 0 on success else non-zero. |
| 472 | */ |
| 473 | int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs) |
| 474 | { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 475 | TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 476 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 477 | if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 478 | TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 479 | return 1; |
| 480 | } |
| 481 | |
Amaury Denoyelle | b710415 | 2022-04-22 17:38:43 +0200 | [diff] [blame] | 482 | qcs_notify_recv(qcs); |
| 483 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 484 | TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 485 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 489 | static int qc_is_max_streams_needed(struct qcc *qcc) |
| 490 | { |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 491 | return qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 494 | /* 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] | 495 | static void qcs_destroy(struct qcs *qcs) |
| 496 | { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 497 | struct connection *conn = qcs->qcc->conn; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 498 | const uint64_t id = qcs->id; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 499 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 500 | TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 501 | |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 502 | if (quic_stream_is_remote(qcs->qcc, id)) { |
| 503 | if (quic_stream_is_bidi(id)) { |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 504 | ++qcs->qcc->lfctl.cl_bidi_r; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 505 | if (qc_is_max_streams_needed(qcs->qcc)) |
| 506 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 507 | } |
| 508 | } |
| 509 | |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 510 | qcs_free(qcs); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 511 | |
| 512 | TRACE_LEAVE(QMUX_EV_QCS_END, conn); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static inline int qcc_is_dead(const struct qcc *qcc) |
| 516 | { |
Amaury Denoyelle | 198d35f | 2022-04-01 17:56:58 +0200 | [diff] [blame] | 517 | if (qcc->app_ops && qcc->app_ops->is_active && |
| 518 | qcc->app_ops->is_active(qcc, qcc->ctx)) |
| 519 | return 0; |
| 520 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 521 | if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task) |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 522 | return 1; |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* Return true if the mux timeout should be armed. */ |
| 528 | static inline int qcc_may_expire(struct qcc *qcc) |
| 529 | { |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 530 | return !qcc->nb_cs; |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* release function. This one should be called to free all resources allocated |
| 534 | * to the mux. |
| 535 | */ |
| 536 | static void qc_release(struct qcc *qcc) |
| 537 | { |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 538 | struct connection *conn = qcc->conn; |
| 539 | struct eb64_node *node; |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 540 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 541 | TRACE_ENTER(QMUX_EV_QCC_END); |
| 542 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 543 | if (qcc->app_ops && qcc->app_ops->release) |
| 544 | qcc->app_ops->release(qcc->ctx); |
Amaury Denoyelle | f890945 | 2022-03-30 11:51:56 +0200 | [diff] [blame] | 545 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 546 | if (qcc->task) { |
| 547 | task_destroy(qcc->task); |
| 548 | qcc->task = NULL; |
| 549 | } |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 550 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 551 | if (qcc->wait_event.tasklet) |
| 552 | tasklet_free(qcc->wait_event.tasklet); |
Amaury Denoyelle | f3e03a4 | 2022-04-21 15:41:34 +0200 | [diff] [blame] | 553 | if (conn && qcc->wait_event.events) { |
| 554 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, |
| 555 | qcc->wait_event.events, |
| 556 | &qcc->wait_event); |
| 557 | } |
Amaury Denoyelle | f890945 | 2022-03-30 11:51:56 +0200 | [diff] [blame] | 558 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 559 | /* liberate remaining qcs instances */ |
| 560 | node = eb64_first(&qcc->streams_by_id); |
| 561 | while (node) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 562 | struct qcs *qcs = eb64_entry(node, struct qcs, by_id); |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 563 | node = eb64_next(node); |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 564 | qcs_free(qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 567 | pool_free(pool_head_qcc, qcc); |
| 568 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 569 | if (conn) { |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 570 | LIST_DEL_INIT(&conn->stopping_list); |
| 571 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 572 | conn->handle.qc->conn = NULL; |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 573 | conn->mux = NULL; |
| 574 | conn->ctx = NULL; |
| 575 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 576 | TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn); |
| 577 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 578 | conn_stop_tracking(conn); |
| 579 | conn_full_close(conn); |
| 580 | if (conn->destroy_cb) |
| 581 | conn->destroy_cb(conn); |
| 582 | conn_free(conn); |
| 583 | } |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 584 | |
| 585 | TRACE_LEAVE(QMUX_EV_QCC_END); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 588 | /* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is |
| 589 | * the current flow-control limit on the connection which must not be exceeded. |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 590 | * |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 591 | * Returns the total bytes of transferred data. |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 592 | */ |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 593 | static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 594 | struct buffer *in, uint64_t max_data) |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 595 | { |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 596 | struct qcc *qcc = qcs->qcc; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 597 | int left, to_xfer; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 598 | int total = 0; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 599 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 600 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 601 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 602 | qc_get_buf(qcs, out); |
| 603 | |
| 604 | /* |
| 605 | * QCS out buffer diagram |
| 606 | * head left to_xfer |
| 607 | * -------------> ----------> -----> |
Amaury Denoyelle | e0320b8 | 2022-03-11 19:12:23 +0100 | [diff] [blame] | 608 | * -------------------------------------------------- |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 609 | * |...............|xxxxxxxxxxx|<<<<< |
Amaury Denoyelle | e0320b8 | 2022-03-11 19:12:23 +0100 | [diff] [blame] | 610 | * -------------------------------------------------- |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 611 | * ^ ack-off ^ sent-off ^ off |
| 612 | * |
| 613 | * STREAM frame |
| 614 | * ^ ^ |
| 615 | * |xxxxxxxxxxxxxxxxx| |
| 616 | */ |
| 617 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 618 | BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 619 | BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset); |
| 620 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 621 | left = qcs->tx.offset - qcs->tx.sent_offset; |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 622 | to_xfer = QUIC_MIN(b_data(in), b_room(out)); |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 623 | |
| 624 | BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd); |
| 625 | /* do not exceed flow control limit */ |
| 626 | if (qcs->tx.offset + to_xfer > qcs->tx.msd) |
| 627 | to_xfer = qcs->tx.msd - qcs->tx.offset; |
| 628 | |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 629 | BUG_ON_HOT(max_data > qcc->rfctl.md); |
| 630 | /* do not overcome flow control limit on connection */ |
| 631 | if (max_data + to_xfer > qcc->rfctl.md) |
| 632 | to_xfer = qcc->rfctl.md - max_data; |
| 633 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 634 | if (!left && !to_xfer) |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 635 | goto out; |
| 636 | |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 637 | total = b_force_xfer(out, in, to_xfer); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 638 | |
| 639 | out: |
| 640 | { |
| 641 | struct qcs_xfer_data_trace_arg arg = { |
| 642 | .prep = b_data(out), .xfer = total, |
| 643 | }; |
| 644 | TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA, |
| 645 | qcc->conn, qcs, &arg); |
| 646 | } |
| 647 | |
| 648 | return total; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 651 | /* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame |
| 652 | * is appended in <frm_list>. Set <fin> if this is supposed to be the last |
| 653 | * stream frame. |
| 654 | * |
| 655 | * Returns the length of the STREAM frame or a negative error code. |
| 656 | */ |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 657 | static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin, |
| 658 | struct list *frm_list) |
| 659 | { |
| 660 | struct qcc *qcc = qcs->qcc; |
| 661 | struct quic_frame *frm; |
| 662 | int head, total; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 663 | uint64_t base_off; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 664 | |
| 665 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 666 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 667 | /* if ack_offset < buf_offset, it points to an older buffer. */ |
| 668 | base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset); |
| 669 | BUG_ON(qcs->tx.sent_offset < base_off); |
| 670 | |
| 671 | head = qcs->tx.sent_offset - base_off; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 672 | total = b_data(out) - head; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 673 | BUG_ON(total < 0); |
| 674 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 675 | if (!total) { |
| 676 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 677 | return 0; |
| 678 | } |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 679 | BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset); |
| 680 | BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 681 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 682 | frm = pool_zalloc(pool_head_quic_frame); |
| 683 | if (!frm) |
| 684 | goto err; |
| 685 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 686 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 687 | frm->type = QUIC_FT_STREAM_8; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 688 | frm->stream.stream = qcs->stream; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 689 | frm->stream.id = qcs->id; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 690 | frm->stream.buf = out; |
| 691 | frm->stream.data = (unsigned char *)b_peek(out, head); |
| 692 | |
Amaury Denoyelle | fecfa0d | 2021-12-07 16:50:14 +0100 | [diff] [blame] | 693 | /* 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] | 694 | if (fin) |
| 695 | frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 696 | |
| 697 | if (qcs->tx.sent_offset) { |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 698 | frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 699 | frm->stream.offset.key = qcs->tx.sent_offset; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 700 | } |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 701 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 702 | frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 703 | frm->stream.len = total; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 704 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 705 | LIST_APPEND(frm_list, &frm->list); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 706 | |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 707 | out: |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 708 | { |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 709 | struct qcs_build_stream_trace_arg arg = { |
| 710 | .len = frm->stream.len, .fin = fin, |
| 711 | .offset = frm->stream.offset.key, |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 712 | }; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 713 | TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM, |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 714 | qcc->conn, qcs, &arg); |
| 715 | } |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 716 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 717 | return total; |
| 718 | |
| 719 | err: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 720 | TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 721 | return -1; |
| 722 | } |
| 723 | |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 724 | /* This function must be called by the upper layer to inform about the sending |
| 725 | * of a STREAM frame for <qcs> instance. The frame is of <data> length and on |
| 726 | * <offset>. |
| 727 | */ |
| 728 | void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset) |
| 729 | { |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 730 | struct qcc *qcc = qcs->qcc; |
| 731 | uint64_t diff; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 732 | |
| 733 | BUG_ON(offset > qcs->tx.sent_offset); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 734 | BUG_ON(offset >= qcs->tx.offset); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 735 | |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 736 | /* check if the STREAM frame has already been notified. It can happen |
| 737 | * for retransmission. |
| 738 | */ |
| 739 | if (offset + data <= qcs->tx.sent_offset) |
| 740 | return; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 741 | |
| 742 | diff = offset + data - qcs->tx.sent_offset; |
| 743 | |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 744 | /* increase offset sum on connection */ |
| 745 | qcc->tx.sent_offsets += diff; |
| 746 | BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md); |
| 747 | if (qcc->tx.sent_offsets == qcc->rfctl.md) |
| 748 | qcc->flags |= QC_CF_BLK_MFCTL; |
| 749 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 750 | /* increase offset on stream */ |
| 751 | qcs->tx.sent_offset += diff; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 752 | BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 753 | BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset); |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 754 | if (qcs->tx.sent_offset == qcs->tx.msd) |
| 755 | qcs->flags |= QC_SF_BLK_SFCTL; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 756 | |
| 757 | if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) { |
| 758 | qc_stream_buf_release(qcs->stream); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 759 | /* prepare qcs for immediate send retry if data to send */ |
| 760 | if (b_data(&qcs->tx.buf)) |
| 761 | LIST_APPEND(&qcc->send_retry_list, &qcs->el); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 762 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 765 | /* Wrapper for send on transport layer. Send a list of frames <frms> for the |
| 766 | * connection <qcc>. |
| 767 | * |
| 768 | * Returns 0 if all data sent with success else non-zero. |
| 769 | */ |
| 770 | static int qc_send_frames(struct qcc *qcc, struct list *frms) |
| 771 | { |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 772 | /* TODO implement an opportunistic retry mechanism. This is needed |
| 773 | * because qc_send_app_pkts is not completed. It will only prepare data |
| 774 | * up to its Tx buffer. The frames left are not send even if the Tx |
| 775 | * buffer is emptied by the sendto call. |
| 776 | * |
| 777 | * To overcome this, we call repeatedly qc_send_app_pkts until we |
| 778 | * detect that the transport layer has send nothing. This could happen |
| 779 | * on congestion or sendto syscall error. |
| 780 | * |
| 781 | * When qc_send_app_pkts is improved to handle retry by itself, we can |
| 782 | * remove the looping from the MUX. |
| 783 | */ |
| 784 | struct quic_frame *first_frm; |
| 785 | uint64_t first_offset = 0; |
| 786 | char first_stream_frame_type; |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 787 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 788 | TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn); |
| 789 | |
| 790 | if (LIST_ISEMPTY(frms)) { |
| 791 | TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 792 | return 1; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 793 | } |
Frédéric Lécaille | 4e22f28 | 2022-03-18 18:38:19 +0100 | [diff] [blame] | 794 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 795 | LIST_INIT(&qcc->send_retry_list); |
| 796 | |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 797 | retry_send: |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 798 | first_frm = LIST_ELEM(frms->n, struct quic_frame *, list); |
| 799 | if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) { |
| 800 | first_offset = first_frm->stream.offset.key; |
| 801 | first_stream_frame_type = 1; |
| 802 | } |
| 803 | else { |
| 804 | first_stream_frame_type = 0; |
| 805 | } |
| 806 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 807 | if (!LIST_ISEMPTY(frms)) |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 808 | qc_send_app_pkts(qcc->conn->handle.qc, 0, frms); |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 809 | |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 810 | /* If there is frames left, check if the transport layer has send some |
| 811 | * data or is blocked. |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 812 | */ |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 813 | if (!LIST_ISEMPTY(frms)) { |
| 814 | if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list)) |
| 815 | goto retry_send; |
| 816 | |
| 817 | /* If the first frame is STREAM, check if its offset has |
| 818 | * changed. |
| 819 | */ |
| 820 | if (first_stream_frame_type && |
| 821 | first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) { |
| 822 | goto retry_send; |
| 823 | } |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 824 | } |
| 825 | |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 826 | /* If there is frames left at this stage, transport layer is blocked. |
| 827 | * Subscribe on it to retry later. |
| 828 | */ |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 829 | if (!LIST_ISEMPTY(frms)) { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 830 | TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 831 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 832 | SUB_RETRY_SEND, &qcc->wait_event); |
| 833 | return 1; |
| 834 | } |
| 835 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 836 | TRACE_LEAVE(QMUX_EV_QCC_SEND); |
| 837 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 838 | return 0; |
| 839 | } |
| 840 | |
Amaury Denoyelle | c933780 | 2022-04-04 16:36:34 +0200 | [diff] [blame] | 841 | /* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams |
| 842 | * allowed to be opened by the peer. The caller should have first checked if |
| 843 | * this is required with qc_is_max_streams_needed. |
| 844 | * |
| 845 | * Returns 0 on success else non-zero. |
| 846 | */ |
| 847 | static int qc_send_max_streams(struct qcc *qcc) |
| 848 | { |
| 849 | struct list frms = LIST_HEAD_INIT(frms); |
| 850 | struct quic_frame *frm; |
| 851 | |
| 852 | frm = pool_zalloc(pool_head_quic_frame); |
| 853 | BUG_ON(!frm); /* TODO handle this properly */ |
| 854 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 855 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | c933780 | 2022-04-04 16:36:34 +0200 | [diff] [blame] | 856 | frm->type = QUIC_FT_MAX_STREAMS_BIDI; |
| 857 | frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi + |
| 858 | qcc->lfctl.cl_bidi_r; |
| 859 | TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm); |
| 860 | LIST_APPEND(&frms, &frm->list); |
| 861 | |
| 862 | if (qc_send_frames(qcc, &frms)) |
| 863 | return 1; |
| 864 | |
| 865 | /* save the new limit if the frame has been send. */ |
| 866 | qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r; |
| 867 | qcc->lfctl.cl_bidi_r = 0; |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 872 | /* Used internally by qc_send function. Proceed to send for <qcs>. This will |
| 873 | * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame |
| 874 | * is then generated and inserted in <frms> list. <qcc_max_data> is the current |
| 875 | * flow-control max-data at the connection level which must not be surpassed. |
| 876 | * |
| 877 | * Returns the total bytes transferred between qcs and quic_stream buffers. Can |
| 878 | * be null if out buffer cannot be allocated. |
| 879 | */ |
| 880 | static int _qc_send_qcs(struct qcs *qcs, struct list *frms, |
| 881 | uint64_t qcc_max_data) |
| 882 | { |
| 883 | struct qcc *qcc = qcs->qcc; |
| 884 | struct buffer *buf = &qcs->tx.buf; |
| 885 | struct buffer *out = qc_stream_buf_get(qcs->stream); |
| 886 | int xfer = 0; |
| 887 | |
| 888 | /* Allocate <out> buffer if necessary. */ |
| 889 | if (!out) { |
| 890 | if (qcc->flags & QC_CF_CONN_FULL) |
| 891 | return 0; |
| 892 | |
| 893 | out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset); |
| 894 | if (!out) { |
| 895 | qcc->flags |= QC_CF_CONN_FULL; |
| 896 | return 0; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | /* Transfer data from <buf> to <out>. */ |
| 901 | if (b_data(buf)) { |
| 902 | xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 903 | if (xfer > 0) { |
| 904 | qcs_notify_send(qcs); |
| 905 | qcs->flags &= ~QC_SF_BLK_MROOM; |
| 906 | } |
| 907 | |
| 908 | qcs->tx.offset += xfer; |
| 909 | } |
| 910 | |
| 911 | /* out buffer cannot be emptied if qcs offsets differ. */ |
| 912 | BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset); |
| 913 | |
| 914 | /* Build a new STREAM frame with <out> buffer. */ |
| 915 | if (qcs->tx.sent_offset != qcs->tx.offset) { |
| 916 | int ret; |
| 917 | char fin = !!(qcs->flags & QC_SF_FIN_STREAM); |
| 918 | |
| 919 | /* FIN is set if all incoming data were transfered. */ |
| 920 | fin = !!(fin && !b_data(buf)); |
| 921 | |
| 922 | ret = qcs_build_stream_frm(qcs, out, fin, frms); |
Amaury Denoyelle | b50f311 | 2022-04-28 14:41:50 +0200 | [diff] [blame] | 923 | if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ } |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | return xfer; |
| 927 | } |
| 928 | |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 929 | /* Proceed to sending. Loop through all available streams for the <qcc> |
| 930 | * instance and try to send as much as possible. |
| 931 | * |
| 932 | * Returns the total of bytes sent to the transport layer. |
| 933 | */ |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 934 | static int qc_send(struct qcc *qcc) |
| 935 | { |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 936 | struct list frms = LIST_HEAD_INIT(frms); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 937 | struct eb64_node *node; |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 938 | struct qcs *qcs, *qcs_tmp; |
| 939 | int total = 0, tmp_total = 0; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 940 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 941 | TRACE_ENTER(QMUX_EV_QCC_SEND); |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 942 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 943 | if (qcc->conn->flags & CO_FL_SOCK_WR_SH) { |
| 944 | qcc->conn->flags |= CO_FL_ERROR; |
| 945 | TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn); |
| 946 | return 0; |
| 947 | } |
| 948 | |
Amaury Denoyelle | c933780 | 2022-04-04 16:36:34 +0200 | [diff] [blame] | 949 | if (qc_is_max_streams_needed(qcc)) |
| 950 | qc_send_max_streams(qcc); |
| 951 | |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 952 | if (qcc->flags & QC_CF_BLK_MFCTL) |
| 953 | return 0; |
| 954 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 955 | /* loop through all streams, construct STREAM frames if data available. |
| 956 | * 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] | 957 | */ |
| 958 | node = eb64_first(&qcc->streams_by_id); |
| 959 | while (node) { |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 960 | int ret; |
| 961 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 962 | |
Amaury Denoyelle | e2ec942 | 2022-03-10 16:46:18 +0100 | [diff] [blame] | 963 | /* TODO |
| 964 | * for the moment, unidirectional streams have their own |
| 965 | * mechanism for sending. This should be unified in the future, |
| 966 | * in this case the next check will be removed. |
| 967 | */ |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 968 | if (quic_stream_is_uni(qcs->id)) { |
Amaury Denoyelle | e2ec942 | 2022-03-10 16:46:18 +0100 | [diff] [blame] | 969 | node = eb64_next(node); |
| 970 | continue; |
| 971 | } |
| 972 | |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 973 | if (qcs->flags & QC_SF_BLK_SFCTL) { |
| 974 | node = eb64_next(node); |
| 975 | continue; |
| 976 | } |
| 977 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 978 | if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) { |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 979 | node = eb64_next(node); |
| 980 | continue; |
| 981 | } |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 982 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 983 | ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total); |
| 984 | total += ret; |
| 985 | node = eb64_next(node); |
| 986 | } |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 987 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 988 | if (qc_send_frames(qcc, &frms)) { |
| 989 | /* data rejected by transport layer, do not retry. */ |
| 990 | goto out; |
| 991 | } |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 992 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 993 | retry: |
| 994 | tmp_total = 0; |
| 995 | list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) { |
| 996 | int ret; |
| 997 | BUG_ON(!b_data(&qcs->tx.buf)); |
| 998 | BUG_ON(qc_stream_buf_get(qcs->stream)); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 999 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1000 | ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total); |
| 1001 | tmp_total += ret; |
| 1002 | LIST_DELETE(&qcs->el); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1003 | } |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 1004 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1005 | total += tmp_total; |
| 1006 | if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list)) |
| 1007 | goto retry; |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 1008 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1009 | out: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1010 | TRACE_LEAVE(QMUX_EV_QCC_SEND); |
| 1011 | |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1012 | return total; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Amaury Denoyelle | 6a4aebf | 2022-02-01 10:16:05 +0100 | [diff] [blame] | 1015 | /* Release all streams that are already marked as detached. This is only done |
| 1016 | * if their TX buffers are empty or if a CONNECTION_CLOSE has been received. |
| 1017 | * |
| 1018 | * Return the number of released stream. |
| 1019 | */ |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1020 | static int qc_release_detached_streams(struct qcc *qcc) |
| 1021 | { |
| 1022 | struct eb64_node *node; |
| 1023 | int release = 0; |
| 1024 | |
| 1025 | node = eb64_first(&qcc->streams_by_id); |
| 1026 | while (node) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1027 | struct qcs *qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1028 | node = eb64_next(node); |
| 1029 | |
| 1030 | if (qcs->flags & QC_SF_DETACH) { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1031 | if (!b_data(&qcs->tx.buf) && |
| 1032 | qcs->tx.offset == qcs->tx.sent_offset) { |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1033 | qcs_destroy(qcs); |
| 1034 | release = 1; |
| 1035 | } |
| 1036 | else { |
| 1037 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 1038 | SUB_RETRY_SEND, &qcc->wait_event); |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | return release; |
| 1044 | } |
| 1045 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1046 | static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status) |
| 1047 | { |
Amaury Denoyelle | 769e9ff | 2021-10-05 11:43:50 +0200 | [diff] [blame] | 1048 | struct qcc *qcc = ctx; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1049 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1050 | TRACE_ENTER(QMUX_EV_QCC_WAKE); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1051 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1052 | qc_send(qcc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1053 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1054 | if (qc_release_detached_streams(qcc)) { |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1055 | if (qcc_is_dead(qcc)) { |
| 1056 | qc_release(qcc); |
| 1057 | } |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 1058 | else if (qcc->task) { |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1059 | if (qcc_may_expire(qcc)) |
| 1060 | qcc->task->expire = tick_add(now_ms, qcc->timeout); |
| 1061 | else |
| 1062 | qcc->task->expire = TICK_ETERNITY; |
Amaury Denoyelle | 1136e92 | 2022-02-01 10:33:09 +0100 | [diff] [blame] | 1063 | task_queue(qcc->task); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1067 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
| 1068 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1069 | return NULL; |
| 1070 | } |
| 1071 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1072 | static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state) |
| 1073 | { |
| 1074 | struct qcc *qcc = ctx; |
| 1075 | int expired = tick_is_expired(t->expire, now_ms); |
| 1076 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1077 | TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1078 | |
| 1079 | if (qcc) { |
| 1080 | if (!expired) { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1081 | TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1082 | return t; |
| 1083 | } |
| 1084 | |
| 1085 | if (!qcc_may_expire(qcc)) { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1086 | TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1087 | t->expire = TICK_ETERNITY; |
| 1088 | return t; |
| 1089 | } |
| 1090 | } |
| 1091 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1092 | task_destroy(t); |
Amaury Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 1093 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1094 | if (!qcc) { |
| 1095 | TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE); |
Amaury Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 1096 | return NULL; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1097 | } |
Amaury Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 1098 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1099 | qcc->task = NULL; |
| 1100 | |
| 1101 | if (qcc_is_dead(qcc)) |
| 1102 | qc_release(qcc); |
| 1103 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1104 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
| 1105 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1106 | return NULL; |
| 1107 | } |
| 1108 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1109 | static int qc_init(struct connection *conn, struct proxy *prx, |
| 1110 | struct session *sess, struct buffer *input) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1111 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1112 | struct qcc *qcc; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 1113 | struct quic_transport_params *lparams, *rparams; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1114 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1115 | qcc = pool_alloc(pool_head_qcc); |
| 1116 | if (!qcc) |
| 1117 | goto fail_no_qcc; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1118 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1119 | qcc->conn = conn; |
| 1120 | conn->ctx = qcc; |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1121 | qcc->nb_cs = 0; |
Amaury Denoyelle | ce1f30d | 2022-02-01 15:14:24 +0100 | [diff] [blame] | 1122 | qcc->flags = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1123 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1124 | qcc->app_ops = NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1125 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1126 | qcc->streams_by_id = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1127 | |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1128 | /* Server parameters, params used for RX flow control. */ |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1129 | lparams = &conn->handle.qc->rx.params; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1130 | |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1131 | qcc->rx.max_data = lparams->initial_max_data; |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1132 | qcc->tx.sent_offsets = 0; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1133 | |
| 1134 | /* Client initiated streams must respect the server flow control. */ |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1135 | qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1136 | qcc->strms[QCS_CLT_BIDI].nb_streams = 0; |
| 1137 | qcc->strms[QCS_CLT_BIDI].largest_id = -1; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1138 | qcc->strms[QCS_CLT_BIDI].rx.max_data = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1139 | 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] | 1140 | |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1141 | qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1142 | qcc->strms[QCS_CLT_UNI].nb_streams = 0; |
| 1143 | qcc->strms[QCS_CLT_UNI].largest_id = -1; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1144 | qcc->strms[QCS_CLT_UNI].rx.max_data = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1145 | 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] | 1146 | |
| 1147 | /* Server initiated streams must respect the server flow control. */ |
| 1148 | qcc->strms[QCS_SRV_BIDI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1149 | qcc->strms[QCS_SRV_BIDI].nb_streams = 0; |
| 1150 | qcc->strms[QCS_SRV_BIDI].largest_id = -1; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1151 | 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] | 1152 | qcc->strms[QCS_SRV_BIDI].tx.max_data = 0; |
| 1153 | |
| 1154 | qcc->strms[QCS_SRV_UNI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1155 | qcc->strms[QCS_SRV_UNI].nb_streams = 0; |
| 1156 | qcc->strms[QCS_SRV_UNI].largest_id = -1; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1157 | 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] | 1158 | qcc->strms[QCS_SRV_UNI].tx.max_data = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1159 | |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 1160 | qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi; |
Amaury Denoyelle | 44d0912 | 2022-04-26 11:21:10 +0200 | [diff] [blame] | 1161 | qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local; |
| 1162 | qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote; |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 1163 | qcc->lfctl.cl_bidi_r = 0; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1164 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1165 | rparams = &conn->handle.qc->tx.params; |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1166 | qcc->rfctl.md = rparams->initial_max_data; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 1167 | qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local; |
| 1168 | qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote; |
| 1169 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1170 | qcc->wait_event.tasklet = tasklet_new(); |
| 1171 | if (!qcc->wait_event.tasklet) |
| 1172 | goto fail_no_tasklet; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1173 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1174 | LIST_INIT(&qcc->send_retry_list); |
| 1175 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1176 | qcc->subs = NULL; |
| 1177 | qcc->wait_event.tasklet->process = qc_io_cb; |
| 1178 | qcc->wait_event.tasklet->context = qcc; |
Frédéric Lécaille | f27b66f | 2022-03-18 22:49:22 +0100 | [diff] [blame] | 1179 | qcc->wait_event.events = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1180 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1181 | /* haproxy timeouts */ |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 1182 | qcc->task = NULL; |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1183 | qcc->timeout = prx->timeout.client; |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 1184 | if (tick_isset(qcc->timeout)) { |
| 1185 | qcc->task = task_new_here(); |
| 1186 | if (!qcc->task) |
| 1187 | goto fail_no_timeout_task; |
| 1188 | qcc->task->process = qc_timeout_task; |
| 1189 | qcc->task->context = qcc; |
| 1190 | qcc->task->expire = tick_add(now_ms, qcc->timeout); |
| 1191 | } |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1192 | |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1193 | if (!conn_is_back(conn)) { |
| 1194 | if (!LIST_INLIST(&conn->stopping_list)) { |
| 1195 | LIST_APPEND(&mux_stopping_data[tid].list, |
| 1196 | &conn->stopping_list); |
| 1197 | } |
| 1198 | } |
| 1199 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1200 | HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1201 | /* init read cycle */ |
| 1202 | tasklet_wakeup(qcc->wait_event.tasklet); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1203 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1204 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1205 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1206 | fail_no_timeout_task: |
| 1207 | tasklet_free(qcc->wait_event.tasklet); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1208 | fail_no_tasklet: |
| 1209 | pool_free(pool_head_qcc, qcc); |
| 1210 | fail_no_qcc: |
| 1211 | return -1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
Amaury Denoyelle | 2461bd5 | 2022-04-13 16:54:52 +0200 | [diff] [blame] | 1214 | static void qc_destroy(void *ctx) |
| 1215 | { |
| 1216 | struct qcc *qcc = ctx; |
| 1217 | |
| 1218 | TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn); |
| 1219 | qc_release(qcc); |
| 1220 | TRACE_LEAVE(QMUX_EV_QCC_END); |
| 1221 | } |
| 1222 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1223 | static void qc_detach(struct conn_stream *cs) |
| 1224 | { |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 1225 | struct qcs *qcs = __cs_mux(cs); |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 1226 | struct qcc *qcc = qcs->qcc; |
| 1227 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1228 | TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs); |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 1229 | |
Amaury Denoyelle | 8038821 | 2022-04-08 12:00:12 +0200 | [diff] [blame] | 1230 | qcs->cs = NULL; |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1231 | --qcc->nb_cs; |
| 1232 | |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1233 | if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) && |
| 1234 | !(qcc->conn->flags & CO_FL_ERROR)) { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1235 | TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1236 | qcs->flags |= QC_SF_DETACH; |
| 1237 | return; |
| 1238 | } |
| 1239 | |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 1240 | qcs_destroy(qcs); |
Amaury Denoyelle | 1136e92 | 2022-02-01 10:33:09 +0100 | [diff] [blame] | 1241 | |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1242 | if (qcc_is_dead(qcc)) { |
| 1243 | qc_release(qcc); |
| 1244 | } |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 1245 | else if (qcc->task) { |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1246 | if (qcc_may_expire(qcc)) |
| 1247 | qcc->task->expire = tick_add(now_ms, qcc->timeout); |
| 1248 | else |
| 1249 | qcc->task->expire = TICK_ETERNITY; |
Amaury Denoyelle | 1136e92 | 2022-02-01 10:33:09 +0100 | [diff] [blame] | 1250 | task_queue(qcc->task); |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 1251 | } |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1252 | |
| 1253 | TRACE_LEAVE(QMUX_EV_STRM_END); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1254 | } |
| 1255 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1256 | /* Called from the upper layer, to receive data */ |
| 1257 | static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf, |
| 1258 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1259 | { |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 1260 | struct qcs *qcs = __cs_mux(cs); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1261 | struct htx *qcs_htx = NULL; |
| 1262 | struct htx *cs_htx = NULL; |
| 1263 | size_t ret = 0; |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 1264 | char fin = 0; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1265 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1266 | 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] | 1267 | |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1268 | qcs_htx = htx_from_buf(&qcs->rx.app_buf); |
| 1269 | if (htx_is_empty(qcs_htx)) { |
| 1270 | /* Set buffer data to 0 as HTX is empty. */ |
| 1271 | htx_to_buf(qcs_htx, &qcs->rx.app_buf); |
| 1272 | goto end; |
| 1273 | } |
| 1274 | |
| 1275 | ret = qcs_htx->data; |
| 1276 | |
| 1277 | cs_htx = htx_from_buf(buf); |
| 1278 | if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) { |
| 1279 | htx_to_buf(cs_htx, buf); |
| 1280 | htx_to_buf(qcs_htx, &qcs->rx.app_buf); |
| 1281 | b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf)); |
| 1282 | goto end; |
| 1283 | } |
| 1284 | |
| 1285 | htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED); |
| 1286 | BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR); |
| 1287 | |
| 1288 | /* Copy EOM from src to dst buffer if all data copied. */ |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 1289 | if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) { |
| 1290 | cs_htx->flags |= HTX_FL_EOM; |
| 1291 | fin = 1; |
| 1292 | } |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1293 | |
| 1294 | cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0; |
| 1295 | htx_to_buf(cs_htx, buf); |
| 1296 | htx_to_buf(qcs_htx, &qcs->rx.app_buf); |
| 1297 | ret -= qcs_htx->data; |
| 1298 | |
| 1299 | end: |
| 1300 | if (b_data(&qcs->rx.app_buf)) { |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1301 | cs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1302 | } |
| 1303 | else { |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1304 | cs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM); |
| 1305 | if (cs->endp->flags & CS_EP_ERR_PENDING) |
| 1306 | cs->endp->flags |= CS_EP_ERROR; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1307 | |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 1308 | if (fin) |
Amaury Denoyelle | f6df6b4 | 2022-04-22 16:52:14 +0200 | [diff] [blame] | 1309 | cs->endp->flags |= CS_EP_EOI; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1310 | |
| 1311 | if (b_size(&qcs->rx.app_buf)) { |
| 1312 | b_free(&qcs->rx.app_buf); |
| 1313 | offer_buffers(NULL, 1); |
| 1314 | } |
| 1315 | } |
| 1316 | |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 1317 | /* TODO QUIC MUX iocb does not treat RX : following wake-up is thus |
| 1318 | * useless for the moment. This may causes freezing transfer on POST. |
| 1319 | */ |
| 1320 | if (ret) { |
| 1321 | qcs->flags &= ~QC_SF_DEM_FULL; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1322 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 1323 | } |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1324 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1325 | TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs); |
| 1326 | |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1327 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1330 | static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf, |
| 1331 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1332 | { |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 1333 | struct qcs *qcs = __cs_mux(cs); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1334 | size_t ret; |
| 1335 | |
| 1336 | 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] | 1337 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1338 | ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1339 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1340 | TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs); |
| 1341 | |
| 1342 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 1346 | * event subscriber <es> is not allowed to change from a previous call as long |
| 1347 | * as at least one event is still subscribed. The <event_type> must only be a |
| 1348 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 1349 | */ |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1350 | static int qc_subscribe(struct conn_stream *cs, int event_type, |
| 1351 | struct wait_event *es) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1352 | { |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 1353 | return qcs_subscribe(__cs_mux(cs), event_type, es); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 1357 | * The <es> pointer is not allowed to differ from the one passed to the |
| 1358 | * subscribe() call. It always returns zero. |
| 1359 | */ |
| 1360 | static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
| 1361 | { |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 1362 | struct qcs *qcs = __cs_mux(cs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1363 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1364 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 1365 | BUG_ON(qcs->subs && qcs->subs != es); |
| 1366 | |
| 1367 | es->events &= ~event_type; |
| 1368 | if (!es->events) |
| 1369 | qcs->subs = NULL; |
| 1370 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1371 | return 0; |
| 1372 | } |
| 1373 | |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1374 | /* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection, |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1375 | * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1376 | * them. |
| 1377 | */ |
| 1378 | static int qc_wake_some_streams(struct qcc *qcc) |
| 1379 | { |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1380 | struct qcs *qcs; |
| 1381 | struct eb64_node *node; |
| 1382 | |
| 1383 | for (node = eb64_first(&qcc->streams_by_id); node; |
| 1384 | node = eb64_next(node)) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1385 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1386 | |
| 1387 | if (!qcs->cs) |
| 1388 | continue; |
| 1389 | |
| 1390 | if (qcc->conn->flags & CO_FL_ERROR) { |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1391 | qcs->endp->flags |= CS_EP_ERR_PENDING; |
| 1392 | if (qcs->endp->flags & CS_EP_EOS) |
| 1393 | qcs->endp->flags |= CS_EP_ERROR; |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1394 | |
| 1395 | if (qcs->subs) { |
| 1396 | qcs_notify_recv(qcs); |
| 1397 | qcs_notify_send(qcs); |
| 1398 | } |
| 1399 | else if (qcs->cs->data_cb->wake) { |
| 1400 | qcs->cs->data_cb->wake(qcs->cs); |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1408 | static int qc_wake(struct connection *conn) |
| 1409 | { |
| 1410 | struct qcc *qcc = conn->ctx; |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1411 | struct proxy *prx = conn->handle.qc->li->bind_conf->frontend; |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1412 | |
| 1413 | TRACE_ENTER(QMUX_EV_QCC_WAKE, conn); |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1414 | |
| 1415 | /* Check if a soft-stop is in progress. |
| 1416 | * Release idling front connection if this is the case. |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1417 | * |
| 1418 | * TODO this is revelant for frontend connections only. |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1419 | */ |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1420 | if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) |
| 1421 | goto release; |
| 1422 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1423 | if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 1424 | qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH); |
| 1425 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1426 | qc_send(qcc); |
| 1427 | |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1428 | qc_wake_some_streams(qcc); |
| 1429 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1430 | if (qcc_is_dead(qcc)) |
| 1431 | goto release; |
| 1432 | |
| 1433 | TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn); |
| 1434 | |
| 1435 | return 0; |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1436 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1437 | release: |
| 1438 | qc_release(qcc); |
| 1439 | TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE); |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1440 | return 1; |
| 1441 | } |
| 1442 | |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 1443 | |
Amaury Denoyelle | fa29f33 | 2022-03-25 09:09:40 +0100 | [diff] [blame] | 1444 | static void qmux_trace_frm(const struct quic_frame *frm) |
| 1445 | { |
| 1446 | switch (frm->type) { |
| 1447 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 1448 | chunk_appendf(&trace_buf, " max_streams=%lu", |
| 1449 | frm->max_streams_bidi.max_streams); |
| 1450 | break; |
| 1451 | |
| 1452 | case QUIC_FT_MAX_STREAMS_UNI: |
| 1453 | chunk_appendf(&trace_buf, " max_streams=%lu", |
| 1454 | frm->max_streams_uni.max_streams); |
| 1455 | break; |
| 1456 | |
| 1457 | default: |
| 1458 | break; |
| 1459 | } |
| 1460 | } |
| 1461 | |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 1462 | /* quic-mux trace handler */ |
| 1463 | static void qmux_trace(enum trace_level level, uint64_t mask, |
| 1464 | const struct trace_source *src, |
| 1465 | const struct ist where, const struct ist func, |
| 1466 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 1467 | { |
| 1468 | const struct connection *conn = a1; |
| 1469 | const struct qcc *qcc = conn ? conn->ctx : NULL; |
| 1470 | const struct qcs *qcs = a2; |
| 1471 | |
| 1472 | if (!qcc) |
| 1473 | return; |
| 1474 | |
| 1475 | if (src->verbosity > QMUX_VERB_CLEAN) { |
| 1476 | chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc); |
| 1477 | |
| 1478 | if (qcs) |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 1479 | chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1480 | |
| 1481 | if (mask & QMUX_EV_QCC_NQCS) { |
| 1482 | const uint64_t *id = a3; |
| 1483 | chunk_appendf(&trace_buf, " id=%lu", *id); |
| 1484 | } |
Amaury Denoyelle | fa29f33 | 2022-03-25 09:09:40 +0100 | [diff] [blame] | 1485 | |
| 1486 | if (mask & QMUX_EV_SEND_FRM) |
| 1487 | qmux_trace_frm(a3); |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 1488 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1489 | if (mask & QMUX_EV_QCS_XFER_DATA) { |
| 1490 | const struct qcs_xfer_data_trace_arg *arg = a3; |
| 1491 | chunk_appendf(&trace_buf, " prep=%lu xfer=%d", |
| 1492 | arg->prep, arg->xfer); |
| 1493 | } |
| 1494 | |
| 1495 | if (mask & QMUX_EV_QCS_BUILD_STRM) { |
| 1496 | const struct qcs_build_stream_trace_arg *arg = a3; |
| 1497 | chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu", |
| 1498 | arg->len, arg->fin, arg->offset); |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 1499 | } |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 1500 | } |
| 1501 | } |
| 1502 | |
Amaury Denoyelle | 251eadf | 2022-03-24 17:14:52 +0100 | [diff] [blame] | 1503 | /* Function to automatically activate QUIC MUX traces on stdout. |
| 1504 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 1505 | * Main use for now is in the docker image for QUIC interop testing. |
| 1506 | */ |
| 1507 | static void qmux_init_stdout_traces(void) |
| 1508 | { |
| 1509 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 1510 | trace_qmux.sink = sink_find("stdout"); |
| 1511 | trace_qmux.level = TRACE_LEVEL_DEVELOPER; |
| 1512 | trace_qmux.state = TRACE_STATE_RUNNING; |
| 1513 | trace_qmux.verbosity = QMUX_VERB_MINIMAL; |
| 1514 | #endif |
| 1515 | } |
| 1516 | INITCALL0(STG_INIT, qmux_init_stdout_traces); |
| 1517 | |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 1518 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1519 | static const struct mux_ops qc_ops = { |
| 1520 | .init = qc_init, |
Amaury Denoyelle | 2461bd5 | 2022-04-13 16:54:52 +0200 | [diff] [blame] | 1521 | .destroy = qc_destroy, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1522 | .detach = qc_detach, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1523 | .rcv_buf = qc_rcv_buf, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1524 | .snd_buf = qc_snd_buf, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1525 | .subscribe = qc_subscribe, |
| 1526 | .unsubscribe = qc_unsubscribe, |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1527 | .wake = qc_wake, |
Christopher Faulet | a97cced | 2022-04-12 18:04:10 +0200 | [diff] [blame] | 1528 | .flags = MX_FL_HTX|MX_FL_NO_UPG, |
Willy Tarreau | 671bd5a | 2022-04-11 09:29:21 +0200 | [diff] [blame] | 1529 | .name = "QUIC", |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1530 | }; |
| 1531 | |
| 1532 | static struct mux_proto_list mux_proto_quic = |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1533 | { .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] | 1534 | |
| 1535 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic); |