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