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