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