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