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