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 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 216 | BUG_ON(qcs->endp && !(qcs->endp->flags & CS_EP_ORPHAN)); |
| 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 | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 587 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 591 | /* Handle a new MAX_DATA frame. <max> must contains the maximum data field of |
| 592 | * the frame. |
| 593 | * |
| 594 | * Returns 0 on success else non-zero. |
| 595 | */ |
| 596 | int qcc_recv_max_data(struct qcc *qcc, uint64_t max) |
| 597 | { |
| 598 | if (qcc->rfctl.md < max) { |
| 599 | qcc->rfctl.md = max; |
| 600 | |
| 601 | if (qcc->flags & QC_CF_BLK_MFCTL) { |
| 602 | qcc->flags &= ~QC_CF_BLK_MFCTL; |
| 603 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 604 | } |
| 605 | } |
| 606 | return 0; |
| 607 | } |
| 608 | |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 609 | /* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data |
| 610 | * field of the frame and <id> is the identifier of the QUIC stream. |
| 611 | * |
| 612 | * Returns 0 on success else non-zero. |
| 613 | */ |
| 614 | int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max) |
| 615 | { |
| 616 | struct qcs *qcs; |
| 617 | struct eb64_node *node; |
| 618 | |
| 619 | node = eb64_lookup(&qcc->streams_by_id, id); |
| 620 | if (node) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 621 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 622 | if (max > qcs->tx.msd) { |
| 623 | qcs->tx.msd = max; |
| 624 | |
| 625 | if (qcs->flags & QC_SF_BLK_SFCTL) { |
| 626 | qcs->flags &= ~QC_SF_BLK_SFCTL; |
| 627 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 635 | /* Signal the closing of remote stream with id <id>. Flow-control for new |
| 636 | * streams may be allocated for the peer if needed. |
| 637 | */ |
| 638 | static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id) |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 639 | { |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 640 | struct quic_frame *frm; |
| 641 | |
| 642 | if (quic_stream_is_bidi(id)) { |
| 643 | ++qcc->lfctl.cl_bidi_r; |
| 644 | if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) { |
| 645 | frm = pool_zalloc(pool_head_quic_frame); |
| 646 | BUG_ON(!frm); /* TODO handle this properly */ |
| 647 | |
| 648 | LIST_INIT(&frm->reflist); |
| 649 | frm->type = QUIC_FT_MAX_STREAMS_BIDI; |
| 650 | frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi + |
| 651 | qcc->lfctl.cl_bidi_r; |
| 652 | LIST_APPEND(&qcc->lfctl.frms, &frm->list); |
| 653 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 654 | |
| 655 | qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r; |
| 656 | qcc->lfctl.cl_bidi_r = 0; |
| 657 | } |
| 658 | } |
| 659 | else { |
| 660 | /* TODO */ |
| 661 | } |
| 662 | |
| 663 | return 0; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 666 | /* 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] | 667 | static void qcs_destroy(struct qcs *qcs) |
| 668 | { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 669 | struct connection *conn = qcs->qcc->conn; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 670 | const uint64_t id = qcs->id; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 671 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 672 | TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 673 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 674 | if (quic_stream_is_remote(qcs->qcc, id)) |
| 675 | qcc_release_remote_stream(qcs->qcc, id); |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 676 | |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 677 | qcs_free(qcs); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 678 | |
| 679 | TRACE_LEAVE(QMUX_EV_QCS_END, conn); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static inline int qcc_is_dead(const struct qcc *qcc) |
| 683 | { |
Amaury Denoyelle | 198d35f | 2022-04-01 17:56:58 +0200 | [diff] [blame] | 684 | if (qcc->app_ops && qcc->app_ops->is_active && |
| 685 | qcc->app_ops->is_active(qcc, qcc->ctx)) |
| 686 | return 0; |
| 687 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 688 | if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task) |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 689 | return 1; |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | /* Return true if the mux timeout should be armed. */ |
| 695 | static inline int qcc_may_expire(struct qcc *qcc) |
| 696 | { |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 697 | return !qcc->nb_cs; |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* release function. This one should be called to free all resources allocated |
| 701 | * to the mux. |
| 702 | */ |
| 703 | static void qc_release(struct qcc *qcc) |
| 704 | { |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 705 | struct connection *conn = qcc->conn; |
| 706 | struct eb64_node *node; |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 707 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 708 | TRACE_ENTER(QMUX_EV_QCC_END); |
| 709 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 710 | if (qcc->app_ops && qcc->app_ops->release) |
| 711 | qcc->app_ops->release(qcc->ctx); |
Amaury Denoyelle | f890945 | 2022-03-30 11:51:56 +0200 | [diff] [blame] | 712 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 713 | if (qcc->task) { |
| 714 | task_destroy(qcc->task); |
| 715 | qcc->task = NULL; |
| 716 | } |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 717 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 718 | if (qcc->wait_event.tasklet) |
| 719 | tasklet_free(qcc->wait_event.tasklet); |
Amaury Denoyelle | f3e03a4 | 2022-04-21 15:41:34 +0200 | [diff] [blame] | 720 | if (conn && qcc->wait_event.events) { |
| 721 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, |
| 722 | qcc->wait_event.events, |
| 723 | &qcc->wait_event); |
| 724 | } |
Amaury Denoyelle | f890945 | 2022-03-30 11:51:56 +0200 | [diff] [blame] | 725 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 726 | /* liberate remaining qcs instances */ |
| 727 | node = eb64_first(&qcc->streams_by_id); |
| 728 | while (node) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 729 | struct qcs *qcs = eb64_entry(node, struct qcs, by_id); |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 730 | node = eb64_next(node); |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 731 | qcs_free(qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 732 | } |
| 733 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 734 | while (!LIST_ISEMPTY(&qcc->lfctl.frms)) { |
| 735 | struct quic_frame *frm = LIST_ELEM(&qcc->lfctl.frms, struct quic_frame *, list); |
| 736 | LIST_DELETE(&frm->list); |
| 737 | pool_free(pool_head_quic_frame, frm); |
| 738 | } |
| 739 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 740 | pool_free(pool_head_qcc, qcc); |
| 741 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 742 | if (conn) { |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 743 | LIST_DEL_INIT(&conn->stopping_list); |
| 744 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 745 | conn->handle.qc->conn = NULL; |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 746 | conn->mux = NULL; |
| 747 | conn->ctx = NULL; |
| 748 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 749 | TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn); |
| 750 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 751 | conn_stop_tracking(conn); |
| 752 | conn_full_close(conn); |
| 753 | if (conn->destroy_cb) |
| 754 | conn->destroy_cb(conn); |
| 755 | conn_free(conn); |
| 756 | } |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 757 | |
| 758 | TRACE_LEAVE(QMUX_EV_QCC_END); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 759 | } |
| 760 | |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 761 | /* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is |
| 762 | * 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] | 763 | * |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 764 | * Returns the total bytes of transferred data. |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 765 | */ |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 766 | static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 767 | struct buffer *in, uint64_t max_data) |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 768 | { |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 769 | struct qcc *qcc = qcs->qcc; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 770 | int left, to_xfer; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 771 | int total = 0; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 772 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 773 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 774 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 775 | qc_get_buf(qcs, out); |
| 776 | |
| 777 | /* |
| 778 | * QCS out buffer diagram |
| 779 | * head left to_xfer |
| 780 | * -------------> ----------> -----> |
Amaury Denoyelle | e0320b8 | 2022-03-11 19:12:23 +0100 | [diff] [blame] | 781 | * -------------------------------------------------- |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 782 | * |...............|xxxxxxxxxxx|<<<<< |
Amaury Denoyelle | e0320b8 | 2022-03-11 19:12:23 +0100 | [diff] [blame] | 783 | * -------------------------------------------------- |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 784 | * ^ ack-off ^ sent-off ^ off |
| 785 | * |
| 786 | * STREAM frame |
| 787 | * ^ ^ |
| 788 | * |xxxxxxxxxxxxxxxxx| |
| 789 | */ |
| 790 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 791 | BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 792 | BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset); |
| 793 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 794 | left = qcs->tx.offset - qcs->tx.sent_offset; |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 795 | to_xfer = QUIC_MIN(b_data(in), b_room(out)); |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 796 | |
| 797 | BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd); |
| 798 | /* do not exceed flow control limit */ |
| 799 | if (qcs->tx.offset + to_xfer > qcs->tx.msd) |
| 800 | to_xfer = qcs->tx.msd - qcs->tx.offset; |
| 801 | |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 802 | BUG_ON_HOT(max_data > qcc->rfctl.md); |
| 803 | /* do not overcome flow control limit on connection */ |
| 804 | if (max_data + to_xfer > qcc->rfctl.md) |
| 805 | to_xfer = qcc->rfctl.md - max_data; |
| 806 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 807 | if (!left && !to_xfer) |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 808 | goto out; |
| 809 | |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 810 | total = b_force_xfer(out, in, to_xfer); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 811 | |
| 812 | out: |
| 813 | { |
| 814 | struct qcs_xfer_data_trace_arg arg = { |
| 815 | .prep = b_data(out), .xfer = total, |
| 816 | }; |
| 817 | TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA, |
| 818 | qcc->conn, qcs, &arg); |
| 819 | } |
| 820 | |
| 821 | return total; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 824 | /* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame |
| 825 | * is appended in <frm_list>. Set <fin> if this is supposed to be the last |
| 826 | * stream frame. |
| 827 | * |
| 828 | * Returns the length of the STREAM frame or a negative error code. |
| 829 | */ |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 830 | static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin, |
| 831 | struct list *frm_list) |
| 832 | { |
| 833 | struct qcc *qcc = qcs->qcc; |
| 834 | struct quic_frame *frm; |
| 835 | int head, total; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 836 | uint64_t base_off; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 837 | |
| 838 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 839 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 840 | /* if ack_offset < buf_offset, it points to an older buffer. */ |
| 841 | base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset); |
| 842 | BUG_ON(qcs->tx.sent_offset < base_off); |
| 843 | |
| 844 | head = qcs->tx.sent_offset - base_off; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 845 | total = b_data(out) - head; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 846 | BUG_ON(total < 0); |
| 847 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 848 | if (!total) { |
| 849 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 850 | return 0; |
| 851 | } |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 852 | BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset); |
| 853 | BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 854 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 855 | frm = pool_zalloc(pool_head_quic_frame); |
| 856 | if (!frm) |
| 857 | goto err; |
| 858 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 859 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 860 | frm->type = QUIC_FT_STREAM_8; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 861 | frm->stream.stream = qcs->stream; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 862 | frm->stream.id = qcs->id; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 863 | frm->stream.buf = out; |
| 864 | frm->stream.data = (unsigned char *)b_peek(out, head); |
| 865 | |
Amaury Denoyelle | fecfa0d | 2021-12-07 16:50:14 +0100 | [diff] [blame] | 866 | /* 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] | 867 | if (fin) |
| 868 | frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 869 | |
| 870 | if (qcs->tx.sent_offset) { |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 871 | frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 872 | frm->stream.offset.key = qcs->tx.sent_offset; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 873 | } |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 874 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 875 | frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 876 | frm->stream.len = total; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 877 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 878 | LIST_APPEND(frm_list, &frm->list); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 879 | |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 880 | out: |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 881 | { |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 882 | struct qcs_build_stream_trace_arg arg = { |
| 883 | .len = frm->stream.len, .fin = fin, |
| 884 | .offset = frm->stream.offset.key, |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 885 | }; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 886 | TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM, |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 887 | qcc->conn, qcs, &arg); |
| 888 | } |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 889 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 890 | return total; |
| 891 | |
| 892 | err: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 893 | 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] | 894 | return -1; |
| 895 | } |
| 896 | |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 897 | /* This function must be called by the upper layer to inform about the sending |
| 898 | * of a STREAM frame for <qcs> instance. The frame is of <data> length and on |
| 899 | * <offset>. |
| 900 | */ |
| 901 | void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset) |
| 902 | { |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 903 | struct qcc *qcc = qcs->qcc; |
| 904 | uint64_t diff; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 905 | |
| 906 | BUG_ON(offset > qcs->tx.sent_offset); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 907 | BUG_ON(offset >= qcs->tx.offset); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 908 | |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 909 | /* check if the STREAM frame has already been notified. It can happen |
| 910 | * for retransmission. |
| 911 | */ |
| 912 | if (offset + data <= qcs->tx.sent_offset) |
| 913 | return; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 914 | |
| 915 | diff = offset + data - qcs->tx.sent_offset; |
| 916 | |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 917 | /* increase offset sum on connection */ |
| 918 | qcc->tx.sent_offsets += diff; |
| 919 | BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md); |
| 920 | if (qcc->tx.sent_offsets == qcc->rfctl.md) |
| 921 | qcc->flags |= QC_CF_BLK_MFCTL; |
| 922 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 923 | /* increase offset on stream */ |
| 924 | qcs->tx.sent_offset += diff; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 925 | BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 926 | BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset); |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 927 | if (qcs->tx.sent_offset == qcs->tx.msd) |
| 928 | qcs->flags |= QC_SF_BLK_SFCTL; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 929 | |
| 930 | if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) { |
| 931 | qc_stream_buf_release(qcs->stream); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 932 | /* prepare qcs for immediate send retry if data to send */ |
| 933 | if (b_data(&qcs->tx.buf)) |
| 934 | LIST_APPEND(&qcc->send_retry_list, &qcs->el); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 935 | } |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 938 | /* Wrapper for send on transport layer. Send a list of frames <frms> for the |
| 939 | * connection <qcc>. |
| 940 | * |
| 941 | * Returns 0 if all data sent with success else non-zero. |
| 942 | */ |
| 943 | static int qc_send_frames(struct qcc *qcc, struct list *frms) |
| 944 | { |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 945 | /* TODO implement an opportunistic retry mechanism. This is needed |
| 946 | * because qc_send_app_pkts is not completed. It will only prepare data |
| 947 | * up to its Tx buffer. The frames left are not send even if the Tx |
| 948 | * buffer is emptied by the sendto call. |
| 949 | * |
| 950 | * To overcome this, we call repeatedly qc_send_app_pkts until we |
| 951 | * detect that the transport layer has send nothing. This could happen |
| 952 | * on congestion or sendto syscall error. |
| 953 | * |
| 954 | * When qc_send_app_pkts is improved to handle retry by itself, we can |
| 955 | * remove the looping from the MUX. |
| 956 | */ |
| 957 | struct quic_frame *first_frm; |
| 958 | uint64_t first_offset = 0; |
| 959 | char first_stream_frame_type; |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 960 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 961 | TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn); |
| 962 | |
| 963 | if (LIST_ISEMPTY(frms)) { |
| 964 | 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] | 965 | return 1; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 966 | } |
Frédéric Lécaille | 4e22f28 | 2022-03-18 18:38:19 +0100 | [diff] [blame] | 967 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 968 | LIST_INIT(&qcc->send_retry_list); |
| 969 | |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 970 | retry_send: |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 971 | first_frm = LIST_ELEM(frms->n, struct quic_frame *, list); |
| 972 | if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) { |
| 973 | first_offset = first_frm->stream.offset.key; |
| 974 | first_stream_frame_type = 1; |
| 975 | } |
| 976 | else { |
| 977 | first_stream_frame_type = 0; |
| 978 | } |
| 979 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 980 | if (!LIST_ISEMPTY(frms)) |
Frédéric Lécaille | 3e3a621 | 2022-04-25 10:17:00 +0200 | [diff] [blame] | 981 | qc_send_app_pkts(qcc->conn->handle.qc, 0, frms); |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 982 | |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 983 | /* If there is frames left, check if the transport layer has send some |
| 984 | * data or is blocked. |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 985 | */ |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 986 | if (!LIST_ISEMPTY(frms)) { |
| 987 | if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list)) |
| 988 | goto retry_send; |
| 989 | |
| 990 | /* If the first frame is STREAM, check if its offset has |
| 991 | * changed. |
| 992 | */ |
| 993 | if (first_stream_frame_type && |
| 994 | first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) { |
| 995 | goto retry_send; |
| 996 | } |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 997 | } |
| 998 | |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 999 | /* If there is frames left at this stage, transport layer is blocked. |
| 1000 | * Subscribe on it to retry later. |
| 1001 | */ |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1002 | if (!LIST_ISEMPTY(frms)) { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1003 | 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] | 1004 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 1005 | SUB_RETRY_SEND, &qcc->wait_event); |
| 1006 | return 1; |
| 1007 | } |
| 1008 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1009 | TRACE_LEAVE(QMUX_EV_QCC_SEND); |
| 1010 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1011 | return 0; |
| 1012 | } |
| 1013 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1014 | /* Used internally by qc_send function. Proceed to send for <qcs>. This will |
| 1015 | * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame |
| 1016 | * is then generated and inserted in <frms> list. <qcc_max_data> is the current |
| 1017 | * flow-control max-data at the connection level which must not be surpassed. |
| 1018 | * |
| 1019 | * Returns the total bytes transferred between qcs and quic_stream buffers. Can |
| 1020 | * be null if out buffer cannot be allocated. |
| 1021 | */ |
| 1022 | static int _qc_send_qcs(struct qcs *qcs, struct list *frms, |
| 1023 | uint64_t qcc_max_data) |
| 1024 | { |
| 1025 | struct qcc *qcc = qcs->qcc; |
| 1026 | struct buffer *buf = &qcs->tx.buf; |
| 1027 | struct buffer *out = qc_stream_buf_get(qcs->stream); |
| 1028 | int xfer = 0; |
| 1029 | |
| 1030 | /* Allocate <out> buffer if necessary. */ |
| 1031 | if (!out) { |
| 1032 | if (qcc->flags & QC_CF_CONN_FULL) |
| 1033 | return 0; |
| 1034 | |
| 1035 | out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset); |
| 1036 | if (!out) { |
| 1037 | qcc->flags |= QC_CF_CONN_FULL; |
| 1038 | return 0; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | /* Transfer data from <buf> to <out>. */ |
| 1043 | if (b_data(buf)) { |
| 1044 | xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1045 | if (xfer > 0) { |
| 1046 | qcs_notify_send(qcs); |
| 1047 | qcs->flags &= ~QC_SF_BLK_MROOM; |
| 1048 | } |
| 1049 | |
| 1050 | qcs->tx.offset += xfer; |
| 1051 | } |
| 1052 | |
| 1053 | /* out buffer cannot be emptied if qcs offsets differ. */ |
| 1054 | BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset); |
| 1055 | |
| 1056 | /* Build a new STREAM frame with <out> buffer. */ |
| 1057 | if (qcs->tx.sent_offset != qcs->tx.offset) { |
| 1058 | int ret; |
| 1059 | char fin = !!(qcs->flags & QC_SF_FIN_STREAM); |
| 1060 | |
| 1061 | /* FIN is set if all incoming data were transfered. */ |
| 1062 | fin = !!(fin && !b_data(buf)); |
| 1063 | |
| 1064 | ret = qcs_build_stream_frm(qcs, out, fin, frms); |
Amaury Denoyelle | b50f311 | 2022-04-28 14:41:50 +0200 | [diff] [blame] | 1065 | if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ } |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | return xfer; |
| 1069 | } |
| 1070 | |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1071 | /* Proceed to sending. Loop through all available streams for the <qcc> |
| 1072 | * instance and try to send as much as possible. |
| 1073 | * |
| 1074 | * Returns the total of bytes sent to the transport layer. |
| 1075 | */ |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1076 | static int qc_send(struct qcc *qcc) |
| 1077 | { |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1078 | struct list frms = LIST_HEAD_INIT(frms); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1079 | struct eb64_node *node; |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1080 | struct qcs *qcs, *qcs_tmp; |
| 1081 | int total = 0, tmp_total = 0; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1082 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1083 | TRACE_ENTER(QMUX_EV_QCC_SEND); |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 1084 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1085 | 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] | 1086 | qcc->conn->flags |= CO_FL_ERROR; |
| 1087 | TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn); |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1091 | if (!LIST_ISEMPTY(&qcc->lfctl.frms)) { |
| 1092 | if (qc_send_frames(qcc, &qcc->lfctl.frms)) { |
| 1093 | TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn); |
| 1094 | goto out; |
| 1095 | } |
| 1096 | } |
Amaury Denoyelle | c933780 | 2022-04-04 16:36:34 +0200 | [diff] [blame] | 1097 | |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1098 | if (qcc->flags & QC_CF_BLK_MFCTL) |
| 1099 | return 0; |
| 1100 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1101 | /* loop through all streams, construct STREAM frames if data available. |
| 1102 | * 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] | 1103 | */ |
| 1104 | node = eb64_first(&qcc->streams_by_id); |
| 1105 | while (node) { |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1106 | int ret; |
Amaury Denoyelle | c6195d7 | 2022-05-23 11:39:14 +0200 | [diff] [blame] | 1107 | uint64_t id; |
| 1108 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1109 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | c6195d7 | 2022-05-23 11:39:14 +0200 | [diff] [blame] | 1110 | id = qcs->id; |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1111 | |
Amaury Denoyelle | c6195d7 | 2022-05-23 11:39:14 +0200 | [diff] [blame] | 1112 | if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) { |
Amaury Denoyelle | e2ec942 | 2022-03-10 16:46:18 +0100 | [diff] [blame] | 1113 | node = eb64_next(node); |
| 1114 | continue; |
| 1115 | } |
| 1116 | |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 1117 | if (qcs->flags & QC_SF_BLK_SFCTL) { |
| 1118 | node = eb64_next(node); |
| 1119 | continue; |
| 1120 | } |
| 1121 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1122 | if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) { |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 1123 | node = eb64_next(node); |
| 1124 | continue; |
| 1125 | } |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1126 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1127 | ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total); |
| 1128 | total += ret; |
| 1129 | node = eb64_next(node); |
| 1130 | } |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1131 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1132 | if (qc_send_frames(qcc, &frms)) { |
| 1133 | /* data rejected by transport layer, do not retry. */ |
| 1134 | goto out; |
| 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 | retry: |
| 1138 | tmp_total = 0; |
| 1139 | list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) { |
| 1140 | int ret; |
| 1141 | BUG_ON(!b_data(&qcs->tx.buf)); |
| 1142 | BUG_ON(qc_stream_buf_get(qcs->stream)); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1143 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1144 | ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total); |
| 1145 | tmp_total += ret; |
| 1146 | LIST_DELETE(&qcs->el); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1147 | } |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 1148 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1149 | total += tmp_total; |
| 1150 | if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list)) |
| 1151 | goto retry; |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 1152 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1153 | out: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1154 | TRACE_LEAVE(QMUX_EV_QCC_SEND); |
| 1155 | |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1156 | return total; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1157 | } |
| 1158 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1159 | /* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs |
| 1160 | * operation. |
| 1161 | * |
| 1162 | * Returns 0 on success else non-zero. |
| 1163 | */ |
| 1164 | static int qc_recv(struct qcc *qcc) |
| 1165 | { |
| 1166 | struct eb64_node *node; |
| 1167 | struct qcs *qcs; |
| 1168 | |
Amaury Denoyelle | e1cad8b | 2022-05-23 18:52:11 +0200 | [diff] [blame] | 1169 | TRACE_ENTER(QMUX_EV_QCC_RECV); |
| 1170 | |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 1171 | if (qcc->flags & QC_CF_CC_EMIT) { |
| 1172 | TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn); |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1176 | node = eb64_first(&qcc->streams_by_id); |
| 1177 | while (node) { |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame^] | 1178 | uint64_t id; |
| 1179 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1180 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame^] | 1181 | id = qcs->id; |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1182 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame^] | 1183 | 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] | 1184 | node = eb64_next(node); |
| 1185 | continue; |
| 1186 | } |
| 1187 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame^] | 1188 | if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) { |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1189 | node = eb64_next(node); |
| 1190 | continue; |
| 1191 | } |
| 1192 | |
| 1193 | qcc_decode_qcs(qcc, qcs); |
| 1194 | node = eb64_next(node); |
| 1195 | } |
| 1196 | |
Amaury Denoyelle | e1cad8b | 2022-05-23 18:52:11 +0200 | [diff] [blame] | 1197 | TRACE_LEAVE(QMUX_EV_QCC_RECV); |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1198 | return 0; |
| 1199 | } |
| 1200 | |
Amaury Denoyelle | 6a4aebf | 2022-02-01 10:16:05 +0100 | [diff] [blame] | 1201 | /* Release all streams that are already marked as detached. This is only done |
| 1202 | * if their TX buffers are empty or if a CONNECTION_CLOSE has been received. |
| 1203 | * |
| 1204 | * Return the number of released stream. |
| 1205 | */ |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1206 | static int qc_release_detached_streams(struct qcc *qcc) |
| 1207 | { |
| 1208 | struct eb64_node *node; |
| 1209 | int release = 0; |
| 1210 | |
| 1211 | node = eb64_first(&qcc->streams_by_id); |
| 1212 | while (node) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1213 | struct qcs *qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1214 | node = eb64_next(node); |
| 1215 | |
| 1216 | if (qcs->flags & QC_SF_DETACH) { |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1217 | if (!b_data(&qcs->tx.buf) && |
| 1218 | qcs->tx.offset == qcs->tx.sent_offset) { |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1219 | qcs_destroy(qcs); |
| 1220 | release = 1; |
| 1221 | } |
| 1222 | else { |
| 1223 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 1224 | SUB_RETRY_SEND, &qcc->wait_event); |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | return release; |
| 1230 | } |
| 1231 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1232 | static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status) |
| 1233 | { |
Amaury Denoyelle | 769e9ff | 2021-10-05 11:43:50 +0200 | [diff] [blame] | 1234 | struct qcc *qcc = ctx; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1235 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1236 | TRACE_ENTER(QMUX_EV_QCC_WAKE); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1237 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1238 | qc_send(qcc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1239 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1240 | if (qc_release_detached_streams(qcc)) { |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1241 | if (qcc_is_dead(qcc)) { |
| 1242 | qc_release(qcc); |
| 1243 | } |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 1244 | else if (qcc->task) { |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1245 | if (qcc_may_expire(qcc)) |
| 1246 | qcc->task->expire = tick_add(now_ms, qcc->timeout); |
| 1247 | else |
| 1248 | qcc->task->expire = TICK_ETERNITY; |
Amaury Denoyelle | 1136e92 | 2022-02-01 10:33:09 +0100 | [diff] [blame] | 1249 | task_queue(qcc->task); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1253 | qc_recv(qcc); |
| 1254 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1255 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
| 1256 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1257 | return NULL; |
| 1258 | } |
| 1259 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1260 | static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state) |
| 1261 | { |
| 1262 | struct qcc *qcc = ctx; |
| 1263 | int expired = tick_is_expired(t->expire, now_ms); |
| 1264 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1265 | TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1266 | |
| 1267 | if (qcc) { |
| 1268 | if (!expired) { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1269 | TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1270 | return t; |
| 1271 | } |
| 1272 | |
| 1273 | if (!qcc_may_expire(qcc)) { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1274 | TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1275 | t->expire = TICK_ETERNITY; |
| 1276 | return t; |
| 1277 | } |
| 1278 | } |
| 1279 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1280 | task_destroy(t); |
Amaury Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 1281 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1282 | if (!qcc) { |
| 1283 | TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE); |
Amaury Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 1284 | return NULL; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1285 | } |
Amaury Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 1286 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1287 | qcc->task = NULL; |
| 1288 | |
| 1289 | if (qcc_is_dead(qcc)) |
| 1290 | qc_release(qcc); |
| 1291 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1292 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
| 1293 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1294 | return NULL; |
| 1295 | } |
| 1296 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1297 | static int qc_init(struct connection *conn, struct proxy *prx, |
| 1298 | struct session *sess, struct buffer *input) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1299 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1300 | struct qcc *qcc; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 1301 | struct quic_transport_params *lparams, *rparams; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1302 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1303 | qcc = pool_alloc(pool_head_qcc); |
| 1304 | if (!qcc) |
| 1305 | goto fail_no_qcc; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1306 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1307 | qcc->conn = conn; |
| 1308 | conn->ctx = qcc; |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1309 | qcc->nb_cs = 0; |
Amaury Denoyelle | ce1f30d | 2022-02-01 15:14:24 +0100 | [diff] [blame] | 1310 | qcc->flags = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1311 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1312 | qcc->app_ops = NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1313 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1314 | qcc->streams_by_id = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1315 | |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1316 | /* Server parameters, params used for RX flow control. */ |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1317 | lparams = &conn->handle.qc->rx.params; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1318 | |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1319 | qcc->rx.max_data = lparams->initial_max_data; |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1320 | qcc->tx.sent_offsets = 0; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1321 | |
| 1322 | /* Client initiated streams must respect the server flow control. */ |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1323 | qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1324 | qcc->strms[QCS_CLT_BIDI].nb_streams = 0; |
| 1325 | qcc->strms[QCS_CLT_BIDI].largest_id = -1; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1326 | qcc->strms[QCS_CLT_BIDI].rx.max_data = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1327 | 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] | 1328 | |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1329 | qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1330 | qcc->strms[QCS_CLT_UNI].nb_streams = 0; |
| 1331 | qcc->strms[QCS_CLT_UNI].largest_id = -1; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 1332 | qcc->strms[QCS_CLT_UNI].rx.max_data = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1333 | 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] | 1334 | |
| 1335 | /* Server initiated streams must respect the server flow control. */ |
| 1336 | qcc->strms[QCS_SRV_BIDI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1337 | qcc->strms[QCS_SRV_BIDI].nb_streams = 0; |
| 1338 | qcc->strms[QCS_SRV_BIDI].largest_id = -1; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1339 | 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] | 1340 | qcc->strms[QCS_SRV_BIDI].tx.max_data = 0; |
| 1341 | |
| 1342 | qcc->strms[QCS_SRV_UNI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1343 | qcc->strms[QCS_SRV_UNI].nb_streams = 0; |
| 1344 | qcc->strms[QCS_SRV_UNI].largest_id = -1; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 1345 | 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] | 1346 | qcc->strms[QCS_SRV_UNI].tx.max_data = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1347 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1348 | LIST_INIT(&qcc->lfctl.frms); |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 1349 | 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] | 1350 | qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local; |
| 1351 | qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote; |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 1352 | qcc->lfctl.cl_bidi_r = 0; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1353 | |
Amaury Denoyelle | c830e1e | 2022-05-16 16:19:59 +0200 | [diff] [blame] | 1354 | qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data; |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 1355 | qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0; |
Amaury Denoyelle | c830e1e | 2022-05-16 16:19:59 +0200 | [diff] [blame] | 1356 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1357 | rparams = &conn->handle.qc->tx.params; |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1358 | qcc->rfctl.md = rparams->initial_max_data; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 1359 | qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local; |
| 1360 | qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote; |
| 1361 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1362 | qcc->wait_event.tasklet = tasklet_new(); |
| 1363 | if (!qcc->wait_event.tasklet) |
| 1364 | goto fail_no_tasklet; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1365 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1366 | LIST_INIT(&qcc->send_retry_list); |
| 1367 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1368 | qcc->subs = NULL; |
| 1369 | qcc->wait_event.tasklet->process = qc_io_cb; |
| 1370 | qcc->wait_event.tasklet->context = qcc; |
Frédéric Lécaille | f27b66f | 2022-03-18 22:49:22 +0100 | [diff] [blame] | 1371 | qcc->wait_event.events = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1372 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1373 | /* haproxy timeouts */ |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 1374 | qcc->task = NULL; |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1375 | qcc->timeout = prx->timeout.client; |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 1376 | if (tick_isset(qcc->timeout)) { |
| 1377 | qcc->task = task_new_here(); |
| 1378 | if (!qcc->task) |
| 1379 | goto fail_no_timeout_task; |
| 1380 | qcc->task->process = qc_timeout_task; |
| 1381 | qcc->task->context = qcc; |
| 1382 | qcc->task->expire = tick_add(now_ms, qcc->timeout); |
| 1383 | } |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1384 | |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1385 | if (!conn_is_back(conn)) { |
| 1386 | if (!LIST_INLIST(&conn->stopping_list)) { |
| 1387 | LIST_APPEND(&mux_stopping_data[tid].list, |
| 1388 | &conn->stopping_list); |
| 1389 | } |
| 1390 | } |
| 1391 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1392 | HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1393 | /* init read cycle */ |
| 1394 | tasklet_wakeup(qcc->wait_event.tasklet); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1395 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1396 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1397 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 1398 | fail_no_timeout_task: |
| 1399 | tasklet_free(qcc->wait_event.tasklet); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1400 | fail_no_tasklet: |
| 1401 | pool_free(pool_head_qcc, qcc); |
| 1402 | fail_no_qcc: |
| 1403 | return -1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
Amaury Denoyelle | 2461bd5 | 2022-04-13 16:54:52 +0200 | [diff] [blame] | 1406 | static void qc_destroy(void *ctx) |
| 1407 | { |
| 1408 | struct qcc *qcc = ctx; |
| 1409 | |
| 1410 | TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn); |
| 1411 | qc_release(qcc); |
| 1412 | TRACE_LEAVE(QMUX_EV_QCC_END); |
| 1413 | } |
| 1414 | |
Willy Tarreau | 4201ab7 | 2022-05-10 19:18:52 +0200 | [diff] [blame] | 1415 | static void qc_detach(struct cs_endpoint *endp) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1416 | { |
Willy Tarreau | df94313 | 2022-05-13 16:31:23 +0200 | [diff] [blame] | 1417 | struct qcs *qcs = endp->target; |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 1418 | struct qcc *qcc = qcs->qcc; |
| 1419 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1420 | TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs); |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 1421 | |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1422 | --qcc->nb_cs; |
| 1423 | |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1424 | if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) && |
| 1425 | !(qcc->conn->flags & CO_FL_ERROR)) { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1426 | 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] | 1427 | qcs->flags |= QC_SF_DETACH; |
| 1428 | return; |
| 1429 | } |
| 1430 | |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 1431 | qcs_destroy(qcs); |
Amaury Denoyelle | 1136e92 | 2022-02-01 10:33:09 +0100 | [diff] [blame] | 1432 | |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1433 | if (qcc_is_dead(qcc)) { |
| 1434 | qc_release(qcc); |
| 1435 | } |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 1436 | else if (qcc->task) { |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 1437 | if (qcc_may_expire(qcc)) |
| 1438 | qcc->task->expire = tick_add(now_ms, qcc->timeout); |
| 1439 | else |
| 1440 | qcc->task->expire = TICK_ETERNITY; |
Amaury Denoyelle | 1136e92 | 2022-02-01 10:33:09 +0100 | [diff] [blame] | 1441 | task_queue(qcc->task); |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 1442 | } |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1443 | |
| 1444 | TRACE_LEAVE(QMUX_EV_STRM_END); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1445 | } |
| 1446 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1447 | /* Called from the upper layer, to receive data */ |
| 1448 | static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf, |
| 1449 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1450 | { |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 1451 | struct qcs *qcs = __cs_mux(cs); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1452 | struct htx *qcs_htx = NULL; |
| 1453 | struct htx *cs_htx = NULL; |
| 1454 | size_t ret = 0; |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 1455 | char fin = 0; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1456 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1457 | 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] | 1458 | |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1459 | qcs_htx = htx_from_buf(&qcs->rx.app_buf); |
| 1460 | if (htx_is_empty(qcs_htx)) { |
| 1461 | /* Set buffer data to 0 as HTX is empty. */ |
| 1462 | htx_to_buf(qcs_htx, &qcs->rx.app_buf); |
| 1463 | goto end; |
| 1464 | } |
| 1465 | |
| 1466 | ret = qcs_htx->data; |
| 1467 | |
| 1468 | cs_htx = htx_from_buf(buf); |
| 1469 | if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) { |
| 1470 | htx_to_buf(cs_htx, buf); |
| 1471 | htx_to_buf(qcs_htx, &qcs->rx.app_buf); |
| 1472 | b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf)); |
| 1473 | goto end; |
| 1474 | } |
| 1475 | |
| 1476 | htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED); |
| 1477 | BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR); |
| 1478 | |
| 1479 | /* Copy EOM from src to dst buffer if all data copied. */ |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 1480 | if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) { |
| 1481 | cs_htx->flags |= HTX_FL_EOM; |
| 1482 | fin = 1; |
| 1483 | } |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1484 | |
| 1485 | cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0; |
| 1486 | htx_to_buf(cs_htx, buf); |
| 1487 | htx_to_buf(qcs_htx, &qcs->rx.app_buf); |
| 1488 | ret -= qcs_htx->data; |
| 1489 | |
| 1490 | end: |
| 1491 | if (b_data(&qcs->rx.app_buf)) { |
Willy Tarreau | 15b0721 | 2022-05-10 11:24:26 +0200 | [diff] [blame] | 1492 | qcs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1493 | } |
| 1494 | else { |
Willy Tarreau | 15b0721 | 2022-05-10 11:24:26 +0200 | [diff] [blame] | 1495 | qcs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM); |
| 1496 | if (qcs->endp->flags & CS_EP_ERR_PENDING) |
| 1497 | qcs->endp->flags |= CS_EP_ERROR; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1498 | |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 1499 | if (fin) |
Willy Tarreau | 15b0721 | 2022-05-10 11:24:26 +0200 | [diff] [blame] | 1500 | qcs->endp->flags |= CS_EP_EOI; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1501 | |
| 1502 | if (b_size(&qcs->rx.app_buf)) { |
| 1503 | b_free(&qcs->rx.app_buf); |
| 1504 | offer_buffers(NULL, 1); |
| 1505 | } |
| 1506 | } |
| 1507 | |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 1508 | if (ret) { |
| 1509 | qcs->flags &= ~QC_SF_DEM_FULL; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1510 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 1511 | } |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1512 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1513 | TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs); |
| 1514 | |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 1515 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1516 | } |
| 1517 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1518 | static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf, |
| 1519 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1520 | { |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 1521 | struct qcs *qcs = __cs_mux(cs); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1522 | size_t ret; |
| 1523 | |
| 1524 | 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] | 1525 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1526 | 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] | 1527 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1528 | TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs); |
| 1529 | |
| 1530 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 1534 | * event subscriber <es> is not allowed to change from a previous call as long |
| 1535 | * as at least one event is still subscribed. The <event_type> must only be a |
| 1536 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 1537 | */ |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1538 | static int qc_subscribe(struct conn_stream *cs, int event_type, |
| 1539 | struct wait_event *es) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1540 | { |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 1541 | return qcs_subscribe(__cs_mux(cs), event_type, es); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 1545 | * The <es> pointer is not allowed to differ from the one passed to the |
| 1546 | * subscribe() call. It always returns zero. |
| 1547 | */ |
| 1548 | static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
| 1549 | { |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 1550 | struct qcs *qcs = __cs_mux(cs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1551 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1552 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 1553 | BUG_ON(qcs->subs && qcs->subs != es); |
| 1554 | |
| 1555 | es->events &= ~event_type; |
| 1556 | if (!es->events) |
| 1557 | qcs->subs = NULL; |
| 1558 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1559 | return 0; |
| 1560 | } |
| 1561 | |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1562 | /* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection, |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1563 | * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1564 | * them. |
| 1565 | */ |
| 1566 | static int qc_wake_some_streams(struct qcc *qcc) |
| 1567 | { |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1568 | struct qcs *qcs; |
| 1569 | struct eb64_node *node; |
| 1570 | |
| 1571 | for (node = eb64_first(&qcc->streams_by_id); node; |
| 1572 | node = eb64_next(node)) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1573 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1574 | |
Amaury Denoyelle | 1c25b18 | 2022-05-24 16:53:56 +0200 | [diff] [blame] | 1575 | if (!qcs->endp || !qcs->endp->cs) |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1576 | continue; |
| 1577 | |
| 1578 | if (qcc->conn->flags & CO_FL_ERROR) { |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1579 | qcs->endp->flags |= CS_EP_ERR_PENDING; |
| 1580 | if (qcs->endp->flags & CS_EP_EOS) |
| 1581 | qcs->endp->flags |= CS_EP_ERROR; |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1582 | |
| 1583 | if (qcs->subs) { |
| 1584 | qcs_notify_recv(qcs); |
| 1585 | qcs_notify_send(qcs); |
| 1586 | } |
Willy Tarreau | 01c2a4a | 2022-05-10 15:46:10 +0200 | [diff] [blame] | 1587 | else if (qcs->endp->cs->data_cb->wake) { |
| 1588 | qcs->endp->cs->data_cb->wake(qcs->endp->cs); |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | return 0; |
| 1594 | } |
| 1595 | |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1596 | static int qc_wake(struct connection *conn) |
| 1597 | { |
| 1598 | struct qcc *qcc = conn->ctx; |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1599 | struct proxy *prx = conn->handle.qc->li->bind_conf->frontend; |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1600 | |
| 1601 | TRACE_ENTER(QMUX_EV_QCC_WAKE, conn); |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1602 | |
| 1603 | /* Check if a soft-stop is in progress. |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1604 | * |
| 1605 | * TODO this is revelant for frontend connections only. |
Amaury Denoyelle | d0c6232 | 2022-05-23 08:52:58 +0200 | [diff] [blame] | 1606 | * |
| 1607 | * TODO Client should be notified with a H3 GOAWAY and then a |
| 1608 | * CONNECTION_CLOSE. However, quic-conn uses the listener socket for |
| 1609 | * sending which at this stage is already closed. |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1610 | */ |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1611 | if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) |
Amaury Denoyelle | d0c6232 | 2022-05-23 08:52:58 +0200 | [diff] [blame] | 1612 | 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] | 1613 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 1614 | if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 1615 | qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH); |
| 1616 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1617 | qc_send(qcc); |
| 1618 | |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 1619 | qc_wake_some_streams(qcc); |
| 1620 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1621 | if (qcc_is_dead(qcc)) |
| 1622 | goto release; |
| 1623 | |
| 1624 | TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn); |
| 1625 | |
| 1626 | return 0; |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1627 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1628 | release: |
| 1629 | qc_release(qcc); |
| 1630 | TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE); |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1631 | return 1; |
| 1632 | } |
| 1633 | |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 1634 | |
Amaury Denoyelle | fa29f33 | 2022-03-25 09:09:40 +0100 | [diff] [blame] | 1635 | static void qmux_trace_frm(const struct quic_frame *frm) |
| 1636 | { |
| 1637 | switch (frm->type) { |
| 1638 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 1639 | chunk_appendf(&trace_buf, " max_streams=%lu", |
| 1640 | frm->max_streams_bidi.max_streams); |
| 1641 | break; |
| 1642 | |
| 1643 | case QUIC_FT_MAX_STREAMS_UNI: |
| 1644 | chunk_appendf(&trace_buf, " max_streams=%lu", |
| 1645 | frm->max_streams_uni.max_streams); |
| 1646 | break; |
| 1647 | |
| 1648 | default: |
| 1649 | break; |
| 1650 | } |
| 1651 | } |
| 1652 | |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 1653 | /* quic-mux trace handler */ |
| 1654 | static void qmux_trace(enum trace_level level, uint64_t mask, |
| 1655 | const struct trace_source *src, |
| 1656 | const struct ist where, const struct ist func, |
| 1657 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 1658 | { |
| 1659 | const struct connection *conn = a1; |
| 1660 | const struct qcc *qcc = conn ? conn->ctx : NULL; |
| 1661 | const struct qcs *qcs = a2; |
| 1662 | |
| 1663 | if (!qcc) |
| 1664 | return; |
| 1665 | |
| 1666 | if (src->verbosity > QMUX_VERB_CLEAN) { |
| 1667 | chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc); |
| 1668 | |
| 1669 | if (qcs) |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 1670 | chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1671 | |
| 1672 | if (mask & QMUX_EV_QCC_NQCS) { |
| 1673 | const uint64_t *id = a3; |
| 1674 | chunk_appendf(&trace_buf, " id=%lu", *id); |
| 1675 | } |
Amaury Denoyelle | fa29f33 | 2022-03-25 09:09:40 +0100 | [diff] [blame] | 1676 | |
| 1677 | if (mask & QMUX_EV_SEND_FRM) |
| 1678 | qmux_trace_frm(a3); |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 1679 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1680 | if (mask & QMUX_EV_QCS_XFER_DATA) { |
| 1681 | const struct qcs_xfer_data_trace_arg *arg = a3; |
| 1682 | chunk_appendf(&trace_buf, " prep=%lu xfer=%d", |
| 1683 | arg->prep, arg->xfer); |
| 1684 | } |
| 1685 | |
| 1686 | if (mask & QMUX_EV_QCS_BUILD_STRM) { |
| 1687 | const struct qcs_build_stream_trace_arg *arg = a3; |
| 1688 | chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu", |
| 1689 | arg->len, arg->fin, arg->offset); |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 1690 | } |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1695 | static const struct mux_ops qc_ops = { |
| 1696 | .init = qc_init, |
Amaury Denoyelle | 2461bd5 | 2022-04-13 16:54:52 +0200 | [diff] [blame] | 1697 | .destroy = qc_destroy, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1698 | .detach = qc_detach, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1699 | .rcv_buf = qc_rcv_buf, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1700 | .snd_buf = qc_snd_buf, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1701 | .subscribe = qc_subscribe, |
| 1702 | .unsubscribe = qc_unsubscribe, |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 1703 | .wake = qc_wake, |
Willy Tarreau | b5821e1 | 2022-04-26 11:54:08 +0200 | [diff] [blame] | 1704 | .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED, |
Willy Tarreau | 671bd5a | 2022-04-11 09:29:21 +0200 | [diff] [blame] | 1705 | .name = "QUIC", |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1706 | }; |
| 1707 | |
| 1708 | static struct mux_proto_list mux_proto_quic = |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1709 | { .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] | 1710 | |
| 1711 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic); |