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