Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1 | #include <haproxy/mux_quic.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 3 | #include <import/eb64tree.h> |
| 4 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 5 | #include <haproxy/api.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 6 | #include <haproxy/connection.h> |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 7 | #include <haproxy/dynbuf.h> |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 8 | #include <haproxy/list.h> |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 9 | #include <haproxy/ncbuf.h> |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 10 | #include <haproxy/pool.h> |
Amaury Denoyelle | d80fbca | 2022-09-19 17:02:28 +0200 | [diff] [blame] | 11 | #include <haproxy/qmux_http.h> |
Amaury Denoyelle | 36d50bf | 2022-09-19 16:12:38 +0200 | [diff] [blame] | 12 | #include <haproxy/qmux_trace.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 13 | #include <haproxy/quic_conn.h> |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 14 | #include <haproxy/quic_stream.h> |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 15 | #include <haproxy/quic_tp-t.h> |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 16 | #include <haproxy/ssl_sock-t.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 17 | #include <haproxy/stconn.h> |
Amaury Denoyelle | dd4fbfb | 2022-03-24 16:09:16 +0100 | [diff] [blame] | 18 | #include <haproxy/trace.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 19 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 20 | DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc)); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 21 | DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs)); |
| 22 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 23 | /* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 24 | * send/receive operations. |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 25 | */ |
| 26 | static void qcc_emit_cc(struct qcc *qcc, int err) |
| 27 | { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 28 | TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn); |
| 29 | |
| 30 | TRACE_STATE("set CONNECTION_CLOSE on quic-conn", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | 57e6db7 | 2022-07-13 15:07:56 +0200 | [diff] [blame] | 31 | quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err)); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 32 | qcc->flags |= QC_CF_CC_EMIT; |
| 33 | tasklet_wakeup(qcc->wait_event.tasklet); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 34 | |
| 35 | TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn); |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Amaury Denoyelle | 4b16700 | 2022-12-12 09:59:50 +0100 | [diff] [blame] | 38 | static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf) |
| 39 | { |
| 40 | struct buffer buf; |
| 41 | |
| 42 | if (ncb_is_null(ncbuf)) |
| 43 | return; |
| 44 | |
| 45 | buf = b_make(ncbuf->area, ncbuf->size, 0, 0); |
| 46 | b_free(&buf); |
| 47 | offer_buffers(NULL, 1); |
| 48 | |
| 49 | *ncbuf = NCBUF_NULL; |
| 50 | } |
| 51 | |
| 52 | /* Free <qcs> instance. This function is reserved for internal usage : it must |
| 53 | * only be called on qcs alloc error or on connection shutdown. Else |
| 54 | * qcs_destroy must be prefered to handle QUIC flow-control increase. |
| 55 | */ |
| 56 | static void qcs_free(struct qcs *qcs) |
| 57 | { |
| 58 | struct qcc *qcc = qcs->qcc; |
| 59 | |
| 60 | TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs); |
| 61 | |
Amaury Denoyelle | 15337fd | 2022-12-20 14:47:16 +0100 | [diff] [blame] | 62 | /* Safe to use even if already removed from the list. */ |
| 63 | LIST_DEL_INIT(&qcs->el_opening); |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 64 | LIST_DEL_INIT(&qcs->el_send); |
Amaury Denoyelle | 4b16700 | 2022-12-12 09:59:50 +0100 | [diff] [blame] | 65 | |
| 66 | /* Release stream endpoint descriptor. */ |
| 67 | BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN)); |
| 68 | sedesc_free(qcs->sd); |
| 69 | |
| 70 | /* Release app-layer context. */ |
| 71 | if (qcs->ctx && qcc->app_ops->detach) |
| 72 | qcc->app_ops->detach(qcs); |
| 73 | |
| 74 | /* Release qc_stream_desc buffer from quic-conn layer. */ |
| 75 | qc_stream_desc_release(qcs->stream); |
| 76 | |
| 77 | /* Free Rx/Tx buffers. */ |
| 78 | qc_free_ncbuf(qcs, &qcs->rx.ncbuf); |
| 79 | b_free(&qcs->tx.buf); |
| 80 | |
| 81 | BUG_ON(!qcc->strms[qcs_id_type(qcs->id)].nb_streams); |
| 82 | --qcc->strms[qcs_id_type(qcs->id)].nb_streams; |
| 83 | |
| 84 | /* Remove qcs from qcc tree. */ |
| 85 | eb64_delete(&qcs->by_id); |
| 86 | |
| 87 | pool_free(pool_head_qcs, qcs); |
| 88 | |
| 89 | TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn); |
| 90 | } |
| 91 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 92 | /* Allocate a new QUIC streams with id <id> and type <type>. */ |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 93 | static 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] | 94 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 95 | struct qcs *qcs; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 96 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 97 | TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn); |
| 98 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 99 | qcs = pool_alloc(pool_head_qcs); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 100 | if (!qcs) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 101 | TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn); |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 102 | return NULL; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 103 | } |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 104 | |
| 105 | qcs->stream = NULL; |
| 106 | qcs->qcc = qcc; |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 107 | qcs->sd = NULL; |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 108 | qcs->flags = QC_SF_NONE; |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 109 | qcs->st = QC_SS_IDLE; |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 110 | qcs->ctx = NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 111 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 112 | /* App callback attach may register the stream for http-request wait. |
| 113 | * These fields must be initialed before. |
| 114 | */ |
| 115 | LIST_INIT(&qcs->el_opening); |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 116 | LIST_INIT(&qcs->el_send); |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 117 | qcs->start = TICK_ETERNITY; |
| 118 | |
Amaury Denoyelle | 4b16700 | 2022-12-12 09:59:50 +0100 | [diff] [blame] | 119 | /* store transport layer stream descriptor in qcc tree */ |
| 120 | qcs->id = qcs->by_id.key = id; |
| 121 | eb64_insert(&qcc->streams_by_id, &qcs->by_id); |
| 122 | |
| 123 | qcc->strms[type].nb_streams++; |
| 124 | |
Amaury Denoyelle | 93fba32 | 2022-05-24 16:53:14 +0200 | [diff] [blame] | 125 | /* Allocate transport layer stream descriptor. Only needed for TX. */ |
| 126 | if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) { |
| 127 | struct quic_conn *qc = qcc->conn->handle.qc; |
| 128 | qcs->stream = qc_stream_desc_new(id, type, qcs, qc); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 129 | if (!qcs->stream) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 130 | TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | 93fba32 | 2022-05-24 16:53:14 +0200 | [diff] [blame] | 131 | goto err; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 132 | } |
Amaury Denoyelle | 93fba32 | 2022-05-24 16:53:14 +0200 | [diff] [blame] | 133 | } |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 134 | |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 135 | if (qcc->app_ops->attach) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 136 | if (qcc->app_ops->attach(qcs, qcc->ctx)) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 137 | TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 138 | goto err; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 139 | } |
Amaury Denoyelle | 47447af | 2022-04-27 15:17:11 +0200 | [diff] [blame] | 140 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 141 | |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 142 | /* If stream is local, use peer remote-limit, or else the opposite. */ |
Amaury Denoyelle | 176174f | 2022-10-21 17:02:18 +0200 | [diff] [blame] | 143 | if (quic_stream_is_bidi(id)) { |
| 144 | qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r : |
| 145 | qcc->rfctl.msd_bidi_l; |
| 146 | } |
| 147 | else if (quic_stream_is_local(qcc, id)) { |
| 148 | qcs->tx.msd = qcc->rfctl.msd_uni_l; |
| 149 | } |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 150 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 151 | qcs->rx.ncbuf = NCBUF_NULL; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 152 | qcs->rx.app_buf = BUF_NULL; |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 153 | qcs->rx.offset = qcs->rx.offset_max = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 154 | |
Amaury Denoyelle | 176174f | 2022-10-21 17:02:18 +0200 | [diff] [blame] | 155 | if (quic_stream_is_bidi(id)) { |
| 156 | qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l : |
| 157 | qcc->lfctl.msd_bidi_r; |
| 158 | } |
| 159 | else if (quic_stream_is_remote(qcc, id)) { |
| 160 | qcs->rx.msd = qcc->lfctl.msd_uni_r; |
| 161 | } |
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 | |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 172 | qcs->err = 0; |
| 173 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 174 | out: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 175 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 176 | return qcs; |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 177 | |
| 178 | err: |
Amaury Denoyelle | 4b16700 | 2022-12-12 09:59:50 +0100 | [diff] [blame] | 179 | qcs_free(qcs); |
| 180 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn); |
Amaury Denoyelle | 17014a6 | 2022-04-27 15:09:27 +0200 | [diff] [blame] | 181 | return NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Amaury Denoyelle | 3abeb57 | 2022-07-04 11:42:27 +0200 | [diff] [blame] | 184 | static forceinline struct stconn *qcs_sc(const struct qcs *qcs) |
| 185 | { |
| 186 | return qcs->sd ? qcs->sd->sc : NULL; |
| 187 | } |
| 188 | |
Amaury Denoyelle | bd6ec1b | 2022-07-25 11:53:18 +0200 | [diff] [blame] | 189 | /* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */ |
| 190 | static forceinline void qcc_reset_idle_start(struct qcc *qcc) |
| 191 | { |
| 192 | qcc->idle_start = now_ms; |
| 193 | } |
| 194 | |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 195 | /* Decrement <qcc> sc. */ |
| 196 | static forceinline void qcc_rm_sc(struct qcc *qcc) |
| 197 | { |
| 198 | BUG_ON_HOT(!qcc->nb_sc); |
| 199 | --qcc->nb_sc; |
Amaury Denoyelle | bd6ec1b | 2022-07-25 11:53:18 +0200 | [diff] [blame] | 200 | |
| 201 | /* Reset qcc idle start for http-keep-alive timeout. Timeout will be |
| 202 | * refreshed after this on stream detach. |
| 203 | */ |
| 204 | if (!qcc->nb_sc && !qcc->nb_hreq) |
| 205 | qcc_reset_idle_start(qcc); |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* Decrement <qcc> hreq. */ |
| 209 | static forceinline void qcc_rm_hreq(struct qcc *qcc) |
| 210 | { |
| 211 | BUG_ON_HOT(!qcc->nb_hreq); |
| 212 | --qcc->nb_hreq; |
Amaury Denoyelle | bd6ec1b | 2022-07-25 11:53:18 +0200 | [diff] [blame] | 213 | |
| 214 | /* Reset qcc idle start for http-keep-alive timeout. Timeout will be |
| 215 | * refreshed after this on I/O handler. |
| 216 | */ |
| 217 | if (!qcc->nb_sc && !qcc->nb_hreq) |
| 218 | qcc_reset_idle_start(qcc); |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 221 | static inline int qcc_is_dead(const struct qcc *qcc) |
| 222 | { |
| 223 | /* Mux connection is considered dead if : |
| 224 | * - all stream-desc are detached AND |
| 225 | * = connection is on error OR |
| 226 | * = mux timeout has already fired or is unset |
| 227 | */ |
| 228 | if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)) |
| 229 | return 1; |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | /* Return true if the mux timeout should be armed. */ |
| 235 | static inline int qcc_may_expire(struct qcc *qcc) |
| 236 | { |
| 237 | return !qcc->nb_sc; |
| 238 | } |
| 239 | |
| 240 | /* Refresh the timeout on <qcc> if needed depending on its state. */ |
| 241 | static void qcc_refresh_timeout(struct qcc *qcc) |
| 242 | { |
| 243 | const struct proxy *px = qcc->proxy; |
| 244 | |
| 245 | TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn); |
| 246 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 247 | if (!qcc->task) { |
| 248 | TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 249 | goto leave; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 250 | } |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 251 | |
Amaury Denoyelle | 6ec9837 | 2022-08-01 17:59:38 +0200 | [diff] [blame] | 252 | /* Check if upper layer is responsible of timeout management. */ |
| 253 | if (!qcc_may_expire(qcc)) { |
| 254 | TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn); |
| 255 | qcc->task->expire = TICK_ETERNITY; |
| 256 | task_queue(qcc->task); |
| 257 | goto leave; |
| 258 | } |
| 259 | |
| 260 | /* TODO if connection is idle on frontend and proxy is disabled, remove |
| 261 | * it with global close_spread delay applied. |
| 262 | */ |
| 263 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 264 | /* TODO implement client/server-fin timeout for graceful shutdown */ |
Amaury Denoyelle | 6ec9837 | 2022-08-01 17:59:38 +0200 | [diff] [blame] | 265 | |
| 266 | /* Frontend timeout management |
| 267 | * - detached streams with data left to send -> default timeout |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 268 | * - stream waiting on incomplete request or no stream yet activated -> timeout http-request |
Amaury Denoyelle | 6ec9837 | 2022-08-01 17:59:38 +0200 | [diff] [blame] | 269 | * - idle after stream processing -> timeout http-keep-alive |
| 270 | */ |
| 271 | if (!conn_is_back(qcc->conn)) { |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 272 | if (qcc->nb_hreq) { |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 273 | TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn); |
| 274 | qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout); |
Amaury Denoyelle | 6ec9837 | 2022-08-01 17:59:38 +0200 | [diff] [blame] | 275 | task_queue(qcc->task); |
| 276 | goto leave; |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 277 | } |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 278 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 279 | if (!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) { |
| 280 | int timeout = px->timeout.httpreq; |
| 281 | struct qcs *qcs = NULL; |
| 282 | int base_time; |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 283 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 284 | /* Use start time of first stream waiting on HTTP or |
| 285 | * qcc idle if no stream not yet used. |
| 286 | */ |
| 287 | if (likely(!LIST_ISEMPTY(&qcc->opening_list))) |
| 288 | qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening); |
| 289 | base_time = qcs ? qcs->start : qcc->idle_start; |
| 290 | |
| 291 | TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs); |
| 292 | qcc->task->expire = tick_add_ifset(base_time, timeout); |
| 293 | } |
| 294 | else { |
| 295 | /* Use http-request timeout if keep-alive timeout not set */ |
| 296 | int timeout = tick_isset(px->timeout.httpka) ? |
| 297 | px->timeout.httpka : px->timeout.httpreq; |
| 298 | |
| 299 | TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn); |
| 300 | qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout); |
| 301 | } |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 302 | } |
Amaury Denoyelle | 6ec9837 | 2022-08-01 17:59:38 +0200 | [diff] [blame] | 303 | |
| 304 | /* fallback to default timeout if frontend specific undefined or for |
| 305 | * backend connections. |
| 306 | */ |
| 307 | if (!tick_isset(qcc->task->expire)) { |
| 308 | TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn); |
| 309 | qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout); |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 312 | task_queue(qcc->task); |
| 313 | |
| 314 | leave: |
| 315 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn); |
| 316 | } |
| 317 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 318 | /* Mark a stream as open if it was idle. This can be used on every |
| 319 | * successful emission/reception operation to update the stream state. |
| 320 | */ |
| 321 | static void qcs_idle_open(struct qcs *qcs) |
| 322 | { |
| 323 | /* This operation must not be used if the stream is already closed. */ |
| 324 | BUG_ON_HOT(qcs->st == QC_SS_CLO); |
| 325 | |
| 326 | if (qcs->st == QC_SS_IDLE) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 327 | TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 328 | qcs->st = QC_SS_OPEN; |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | /* Close the local channel of <qcs> instance. */ |
| 333 | static void qcs_close_local(struct qcs *qcs) |
| 334 | { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 335 | TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
| 336 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 337 | /* The stream must have already been opened. */ |
| 338 | BUG_ON_HOT(qcs->st == QC_SS_IDLE); |
| 339 | |
| 340 | /* This operation cannot be used multiple times. */ |
| 341 | BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO); |
| 342 | |
| 343 | if (quic_stream_is_bidi(qcs->id)) { |
| 344 | qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC; |
Amaury Denoyelle | afb7b9d | 2022-09-19 11:58:24 +0200 | [diff] [blame] | 345 | |
| 346 | if (qcs->flags & QC_SF_HREQ_RECV) |
| 347 | qcc_rm_hreq(qcs->qcc); |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 348 | } |
| 349 | else { |
| 350 | /* Only local uni streams are valid for this operation. */ |
| 351 | BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id)); |
| 352 | qcs->st = QC_SS_CLO; |
| 353 | } |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* Close the remote channel of <qcs> instance. */ |
| 357 | static void qcs_close_remote(struct qcs *qcs) |
| 358 | { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 359 | TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs); |
| 360 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 361 | /* The stream must have already been opened. */ |
| 362 | BUG_ON_HOT(qcs->st == QC_SS_IDLE); |
| 363 | |
| 364 | /* This operation cannot be used multiple times. */ |
| 365 | BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO); |
| 366 | |
| 367 | if (quic_stream_is_bidi(qcs->id)) { |
| 368 | qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM; |
| 369 | } |
| 370 | else { |
| 371 | /* Only remote uni streams are valid for this operation. */ |
| 372 | BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id)); |
| 373 | qcs->st = QC_SS_CLO; |
| 374 | } |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static int qcs_is_close_local(struct qcs *qcs) |
| 378 | { |
| 379 | return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO; |
| 380 | } |
| 381 | |
Amaury Denoyelle | 6eb3c4b | 2022-12-09 16:26:03 +0100 | [diff] [blame] | 382 | static int qcs_is_close_remote(struct qcs *qcs) |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 383 | { |
| 384 | return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO; |
| 385 | } |
| 386 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 387 | 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] | 388 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 389 | struct buffer *buf = b_alloc(bptr); |
| 390 | BUG_ON(!buf); |
| 391 | return buf; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Amaury Denoyelle | a441ec9 | 2022-07-04 15:48:57 +0200 | [diff] [blame] | 394 | static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf) |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 395 | { |
| 396 | struct buffer buf = BUF_NULL; |
| 397 | |
| 398 | if (ncb_is_null(ncbuf)) { |
| 399 | b_alloc(&buf); |
| 400 | BUG_ON(b_is_null(&buf)); |
| 401 | |
| 402 | *ncbuf = ncb_make(buf.area, buf.size, 0); |
| 403 | ncb_init(ncbuf, 0); |
| 404 | } |
| 405 | |
| 406 | return ncbuf; |
| 407 | } |
| 408 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 409 | /* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if |
Amaury Denoyelle | 4561f84 | 2022-07-06 14:54:34 +0200 | [diff] [blame] | 410 | * initialized. |
| 411 | */ |
| 412 | static void qcs_alert(struct qcs *qcs) |
| 413 | { |
| 414 | if (qcs->subs) { |
| 415 | qcs_notify_recv(qcs); |
| 416 | qcs_notify_send(qcs); |
| 417 | } |
| 418 | else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) { |
| 419 | qcs->sd->sc->app_ops->wake(qcs->sd->sc); |
| 420 | } |
| 421 | } |
| 422 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 423 | int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es) |
| 424 | { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 425 | struct qcc *qcc = qcs->qcc; |
| 426 | |
| 427 | 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] | 428 | |
| 429 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 430 | BUG_ON(qcs->subs && qcs->subs != es); |
| 431 | |
| 432 | es->events |= event_type; |
| 433 | qcs->subs = es; |
| 434 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 435 | if (event_type & SUB_RETRY_RECV) |
| 436 | TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs); |
| 437 | |
| 438 | if (event_type & SUB_RETRY_SEND) |
| 439 | TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs); |
| 440 | |
| 441 | TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs); |
| 442 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | void qcs_notify_recv(struct qcs *qcs) |
| 447 | { |
| 448 | if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) { |
| 449 | tasklet_wakeup(qcs->subs->tasklet); |
| 450 | qcs->subs->events &= ~SUB_RETRY_RECV; |
| 451 | if (!qcs->subs->events) |
| 452 | qcs->subs = NULL; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void qcs_notify_send(struct qcs *qcs) |
| 457 | { |
| 458 | if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) { |
| 459 | tasklet_wakeup(qcs->subs->tasklet); |
| 460 | qcs->subs->events &= ~SUB_RETRY_SEND; |
| 461 | if (!qcs->subs->events) |
| 462 | qcs->subs = NULL; |
| 463 | } |
| 464 | } |
| 465 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 466 | /* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a |
| 467 | * bidirectional stream, else an unidirectional stream is opened. The next |
| 468 | * available ID on the connection will be used according to the stream type. |
| 469 | * |
| 470 | * Returns the allocated stream instance or NULL on error. |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 471 | */ |
Amaury Denoyelle | b143723 | 2022-07-08 11:53:22 +0200 | [diff] [blame] | 472 | struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi) |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 473 | { |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 474 | struct qcs *qcs; |
| 475 | enum qcs_type type; |
| 476 | uint64_t *next; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 477 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 478 | TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn); |
| 479 | |
| 480 | if (bidi) { |
| 481 | next = &qcc->next_bidi_l; |
| 482 | type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 483 | } |
| 484 | else { |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 485 | next = &qcc->next_uni_l; |
| 486 | type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI; |
| 487 | } |
| 488 | |
| 489 | /* TODO ensure that we won't overflow remote peer flow control limit on |
| 490 | * streams. Else, we should emit a STREAMS_BLOCKED frame. |
| 491 | */ |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 492 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 493 | qcs = qcs_new(qcc, *next, type); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 494 | if (!qcs) { |
| 495 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn); |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 496 | return NULL; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 497 | } |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 498 | |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 499 | TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 500 | *next += 4; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 501 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 502 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 503 | return qcs; |
| 504 | } |
| 505 | |
| 506 | /* Open a remote initiated stream for the connection <qcc> with ID <id>. The |
| 507 | * caller is responsible to ensure that a stream with the same ID was not |
| 508 | * already opened. This function will also create all intermediaries streams |
| 509 | * with ID smaller than <id> not already opened before. |
| 510 | * |
| 511 | * Returns the allocated stream instance or NULL on error. |
| 512 | */ |
Amaury Denoyelle | b143723 | 2022-07-08 11:53:22 +0200 | [diff] [blame] | 513 | static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id) |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 514 | { |
| 515 | struct qcs *qcs = NULL; |
| 516 | enum qcs_type type; |
Amaury Denoyelle | bf3c208 | 2022-08-16 11:29:08 +0200 | [diff] [blame] | 517 | uint64_t *largest, max_id; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 518 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 519 | TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn); |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 520 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 521 | BUG_ON_HOT(quic_stream_is_local(qcc, id)); |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 522 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 523 | if (quic_stream_is_bidi(id)) { |
| 524 | largest = &qcc->largest_bidi_r; |
| 525 | type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI; |
| 526 | } |
| 527 | else { |
| 528 | largest = &qcc->largest_uni_r; |
| 529 | type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI; |
| 530 | } |
| 531 | |
Amaury Denoyelle | bf3c208 | 2022-08-16 11:29:08 +0200 | [diff] [blame] | 532 | /* RFC 9000 4.6. Controlling Concurrency |
| 533 | * |
| 534 | * An endpoint that receives a frame with a stream ID exceeding the |
| 535 | * limit it has sent MUST treat this as a connection error of type |
| 536 | * STREAM_LIMIT_ERROR |
| 537 | */ |
| 538 | max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 : |
| 539 | qcc->lfctl.ms_uni * 4; |
| 540 | if (id >= max_id) { |
| 541 | TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn); |
| 542 | qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR); |
| 543 | goto err; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* Only stream ID not already opened can be used. */ |
| 547 | BUG_ON(id < *largest); |
| 548 | |
| 549 | while (id >= *largest) { |
Amaury Denoyelle | fd79ddb | 2022-08-16 11:13:45 +0200 | [diff] [blame] | 550 | const char *str = *largest < id ? "initializing intermediary remote stream" : |
| 551 | "initializing remote stream"; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 552 | |
| 553 | qcs = qcs_new(qcc, *largest, type); |
| 554 | if (!qcs) { |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 555 | /* TODO emit RESET_STREAM */ |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 556 | TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 557 | goto err; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 558 | } |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 559 | |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 560 | TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 561 | *largest += 4; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 562 | } |
| 563 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 564 | out: |
| 565 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs); |
Amaury Denoyelle | 5074229 | 2022-03-29 14:57:19 +0200 | [diff] [blame] | 566 | return qcs; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 567 | |
| 568 | err: |
| 569 | TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn); |
| 570 | return NULL; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* Use this function for a stream <id> which is not in <qcc> stream tree. It |
| 574 | * returns true if the associated stream is closed. |
| 575 | */ |
| 576 | static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id) |
| 577 | { |
| 578 | uint64_t *largest; |
| 579 | |
| 580 | /* This function must only be used for stream not present in the stream tree. */ |
| 581 | BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id)); |
| 582 | |
| 583 | if (quic_stream_is_local(qcc, id)) { |
| 584 | largest = quic_stream_is_uni(id) ? &qcc->next_uni_l : |
| 585 | &qcc->next_bidi_l; |
| 586 | } |
| 587 | else { |
| 588 | largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r : |
| 589 | &qcc->largest_bidi_r; |
| 590 | } |
| 591 | |
| 592 | return id < *largest; |
| 593 | } |
| 594 | |
| 595 | /* Retrieve the stream instance from <id> ID. This can be used when receiving |
| 596 | * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 597 | * frames. Set to false <receive_only> or <send_only> if these particular types |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 598 | * of streams are not allowed. If the stream instance is found, it is stored in |
| 599 | * <out>. |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 600 | * |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 601 | * Returns 0 on success else non-zero. On error, a RESET_STREAM or a |
| 602 | * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL |
| 603 | * on success if the stream has already been closed. |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 604 | */ |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 605 | int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only, |
| 606 | struct qcs **out) |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 607 | { |
| 608 | struct eb64_node *node; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 609 | |
| 610 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 611 | *out = NULL; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 612 | |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 613 | if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 614 | TRACE_ERROR("receive-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id); |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 615 | qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 616 | goto err; |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 620 | TRACE_ERROR("send-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id); |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 621 | qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 622 | goto err; |
Amaury Denoyelle | 5fbb869 | 2022-07-06 15:43:21 +0200 | [diff] [blame] | 623 | } |
| 624 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 625 | /* Search the stream in the connection tree. */ |
| 626 | node = eb64_lookup(&qcc->streams_by_id, id); |
| 627 | if (node) { |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 628 | *out = eb64_entry(node, struct qcs, by_id); |
| 629 | TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 630 | goto out; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* Check if stream is already closed. */ |
| 634 | if (qcc_stream_id_is_closed(qcc, id)) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 635 | TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id); |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 636 | /* Consider this as a success even if <out> is left NULL. */ |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 637 | goto out; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /* Create the stream. This is valid only for remote initiated one. A |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 641 | * local stream must have already been explicitly created by the |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 642 | * application protocol layer. |
| 643 | */ |
| 644 | if (quic_stream_is_local(qcc, id)) { |
| 645 | /* RFC 9000 19.8. STREAM Frames |
| 646 | * |
| 647 | * An endpoint MUST terminate the connection with error |
| 648 | * STREAM_STATE_ERROR if it receives a STREAM frame for a locally |
| 649 | * initiated stream that has not yet been created, or for a send-only |
| 650 | * stream. |
| 651 | */ |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 652 | TRACE_ERROR("locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id); |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 653 | qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 654 | goto err; |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 655 | } |
| 656 | else { |
| 657 | /* Remote stream not found - try to open it. */ |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 658 | *out = qcc_init_stream_remote(qcc, id); |
| 659 | if (!*out) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 660 | TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 661 | goto err; |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 662 | } |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 663 | } |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 664 | |
| 665 | out: |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 666 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out); |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 667 | return 0; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 668 | |
| 669 | err: |
| 670 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 671 | return 1; |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 672 | } |
| 673 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 674 | /* Simple function to duplicate a buffer */ |
| 675 | static inline struct buffer qcs_b_dup(const struct ncbuf *b) |
| 676 | { |
| 677 | return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0)); |
| 678 | } |
| 679 | |
Amaury Denoyelle | 36d4b5e | 2022-07-01 11:25:40 +0200 | [diff] [blame] | 680 | /* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may |
| 681 | * be allocated for the peer if needed. |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 682 | */ |
| 683 | static void qcs_consume(struct qcs *qcs, uint64_t bytes) |
| 684 | { |
| 685 | struct qcc *qcc = qcs->qcc; |
| 686 | struct quic_frame *frm; |
| 687 | struct ncbuf *buf = &qcs->rx.ncbuf; |
| 688 | enum ncb_ret ret; |
| 689 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 690 | TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 691 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 692 | ret = ncb_advance(buf, bytes); |
| 693 | if (ret) { |
| 694 | ABORT_NOW(); /* should not happens because removal only in data */ |
| 695 | } |
| 696 | |
| 697 | if (ncb_is_empty(buf)) |
| 698 | qc_free_ncbuf(qcs, buf); |
| 699 | |
| 700 | qcs->rx.offset += bytes; |
Amaury Denoyelle | bb6296c | 2022-12-09 15:00:17 +0100 | [diff] [blame] | 701 | /* Not necessary to emit a MAX_STREAM_DATA if all data received. */ |
| 702 | if (qcs->flags & QC_SF_SIZE_KNOWN) |
| 703 | goto conn_fctl; |
| 704 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 705 | if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 706 | TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 707 | frm = pool_zalloc(pool_head_quic_frame); |
| 708 | BUG_ON(!frm); /* TODO handle this properly */ |
| 709 | |
| 710 | qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init; |
| 711 | |
| 712 | LIST_INIT(&frm->reflist); |
| 713 | frm->type = QUIC_FT_MAX_STREAM_DATA; |
| 714 | frm->max_stream_data.id = qcs->id; |
| 715 | frm->max_stream_data.max_stream_data = qcs->rx.msd; |
| 716 | |
| 717 | LIST_APPEND(&qcc->lfctl.frms, &frm->list); |
| 718 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 719 | } |
| 720 | |
Amaury Denoyelle | bb6296c | 2022-12-09 15:00:17 +0100 | [diff] [blame] | 721 | conn_fctl: |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 722 | qcc->lfctl.offsets_consume += bytes; |
| 723 | if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 724 | TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 725 | frm = pool_zalloc(pool_head_quic_frame); |
| 726 | BUG_ON(!frm); /* TODO handle this properly */ |
| 727 | |
| 728 | qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init; |
| 729 | |
| 730 | LIST_INIT(&frm->reflist); |
| 731 | frm->type = QUIC_FT_MAX_DATA; |
| 732 | frm->max_data.max_data = qcc->lfctl.md; |
| 733 | |
| 734 | LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list); |
| 735 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 736 | } |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 737 | |
| 738 | TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 741 | /* Decode the content of STREAM frames already received on the stream instance |
| 742 | * <qcs>. |
| 743 | * |
| 744 | * Returns 0 on success else non-zero. |
| 745 | */ |
| 746 | static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs) |
| 747 | { |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 748 | struct buffer b; |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 749 | ssize_t ret; |
Amaury Denoyelle | 6befccd | 2022-07-01 11:26:04 +0200 | [diff] [blame] | 750 | int fin = 0; |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 751 | |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 752 | TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 753 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 754 | b = qcs_b_dup(&qcs->rx.ncbuf); |
Amaury Denoyelle | 6befccd | 2022-07-01 11:26:04 +0200 | [diff] [blame] | 755 | |
Amaury Denoyelle | d1310f8 | 2022-09-16 13:30:59 +0200 | [diff] [blame] | 756 | /* Signal FIN to application if STREAM FIN received with all data. */ |
| 757 | if (qcs_is_close_remote(qcs)) |
Amaury Denoyelle | 6befccd | 2022-07-01 11:26:04 +0200 | [diff] [blame] | 758 | fin = 1; |
| 759 | |
Amaury Denoyelle | 663e872 | 2022-12-09 14:58:28 +0100 | [diff] [blame] | 760 | if (!(qcs->flags & QC_SF_READ_ABORTED)) { |
| 761 | ret = qcc->app_ops->decode_qcs(qcs, &b, fin); |
| 762 | if (ret < 0) { |
| 763 | TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 764 | goto err; |
| 765 | } |
| 766 | } |
| 767 | else { |
| 768 | TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 769 | ret = b_data(&b); |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 770 | } |
| 771 | |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 772 | if (ret) { |
| 773 | qcs_consume(qcs, ret); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 774 | qcs_notify_recv(qcs); |
| 775 | } |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 776 | |
| 777 | TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 778 | return 0; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 779 | |
| 780 | err: |
| 781 | TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 782 | return 1; |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 783 | } |
| 784 | |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 785 | /* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 786 | * code. To close the connection right away, set <immediate> : this is useful |
| 787 | * when dealing with a connection fatal error. Else a graceful shutdown will be |
| 788 | * conducted : the error-code is only registered. The lower layer is |
| 789 | * responsible to close the connection when deemed suitable. Note that in this |
| 790 | * case the error code might be overwritten if an immediate close is requested |
| 791 | * in the interval. |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 792 | */ |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 793 | void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate) |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 794 | { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 795 | TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn); |
| 796 | |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 797 | if (immediate) { |
| 798 | quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err)); |
| 799 | qcc->flags |= QC_CF_CC_EMIT; |
| 800 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 801 | } |
| 802 | else { |
| 803 | /* Only register the error code for graceful shutdown. */ |
| 804 | qcc->conn->handle.qc->err = quic_err_app(err); |
| 805 | } |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 806 | |
| 807 | TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn); |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */ |
| 811 | void qcc_reset_stream(struct qcs *qcs, int err) |
| 812 | { |
| 813 | struct qcc *qcc = qcs->qcc; |
| 814 | |
| 815 | if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs)) |
| 816 | return; |
| 817 | |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 818 | TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs); |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 819 | qcs->flags |= QC_SF_TO_RESET; |
| 820 | qcs->err = err; |
Amaury Denoyelle | 0a1154a | 2023-01-06 17:43:11 +0100 | [diff] [blame] | 821 | |
| 822 | qcc_send_stream(qcs, 1); |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 823 | tasklet_wakeup(qcc->wait_event.tasklet); |
Amaury Denoyelle | 663e872 | 2022-12-09 14:58:28 +0100 | [diff] [blame] | 824 | } |
| 825 | |
Amaury Denoyelle | f9b0326 | 2023-01-09 10:34:25 +0100 | [diff] [blame] | 826 | /* Register <qcs> stream for emission of STREAM, STOP_SENDING or RESET_STREAM. |
| 827 | * Set <urg> to 1 if stream content should be treated in priority compared to |
| 828 | * other streams. |
| 829 | */ |
| 830 | void qcc_send_stream(struct qcs *qcs, int urg) |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 831 | { |
| 832 | struct qcc *qcc = qcs->qcc; |
| 833 | |
| 834 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 835 | |
| 836 | /* Cannot send if already closed. */ |
| 837 | BUG_ON(qcs_is_close_local(qcs)); |
| 838 | |
Amaury Denoyelle | f9b0326 | 2023-01-09 10:34:25 +0100 | [diff] [blame] | 839 | if (urg) { |
| 840 | LIST_DEL_INIT(&qcs->el_send); |
| 841 | LIST_INSERT(&qcc->send_list, &qcs->el_send); |
| 842 | } |
| 843 | else { |
| 844 | if (!LIST_INLIST(&qcs->el_send)) |
| 845 | LIST_APPEND(&qcs->qcc->send_list, &qcs->el_send); |
| 846 | } |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 847 | |
| 848 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 849 | } |
| 850 | |
Amaury Denoyelle | 663e872 | 2022-12-09 14:58:28 +0100 | [diff] [blame] | 851 | /* Prepare for the emission of STOP_SENDING on <qcs>. */ |
| 852 | void qcc_abort_stream_read(struct qcs *qcs) |
| 853 | { |
| 854 | struct qcc *qcc = qcs->qcc; |
| 855 | |
| 856 | TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs); |
| 857 | |
| 858 | if ((qcs->flags & QC_SF_TO_STOP_SENDING) || qcs_is_close_remote(qcs)) |
| 859 | goto end; |
| 860 | |
| 861 | TRACE_STATE("abort stream read", QMUX_EV_QCS_END, qcc->conn, qcs); |
| 862 | qcs->flags |= (QC_SF_TO_STOP_SENDING|QC_SF_READ_ABORTED); |
Amaury Denoyelle | 0a1154a | 2023-01-06 17:43:11 +0100 | [diff] [blame] | 863 | |
| 864 | qcc_send_stream(qcs, 1); |
Amaury Denoyelle | 663e872 | 2022-12-09 14:58:28 +0100 | [diff] [blame] | 865 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 866 | |
| 867 | end: |
| 868 | TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs); |
Amaury Denoyelle | f9e190e | 2022-05-23 16:12:15 +0200 | [diff] [blame] | 869 | } |
| 870 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 871 | /* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>. |
| 872 | * Returns 0 on success else non-zero. |
| 873 | */ |
| 874 | int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops) |
| 875 | { |
| 876 | TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn); |
| 877 | |
Amaury Denoyelle | b4d119f | 2023-01-25 17:44:36 +0100 | [diff] [blame] | 878 | if (app_ops->init && !app_ops->init(qcc)) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 879 | TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn); |
| 880 | goto err; |
| 881 | } |
| 882 | |
| 883 | TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn); |
Amaury Denoyelle | b4d119f | 2023-01-25 17:44:36 +0100 | [diff] [blame] | 884 | qcc->app_ops = app_ops; |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 885 | |
Amaury Denoyelle | 71fd036 | 2023-01-24 17:35:37 +0100 | [diff] [blame] | 886 | /* RFC 9114 7.2.4.2. Initialization |
| 887 | * |
| 888 | * Endpoints MUST NOT require any data to be |
| 889 | * received from the peer prior to sending the SETTINGS frame; |
| 890 | * settings MUST be sent as soon as the transport is ready to |
| 891 | * send data. |
| 892 | */ |
| 893 | if (qcc->app_ops->finalize) { |
| 894 | if (qcc->app_ops->finalize(qcc->ctx)) { |
| 895 | TRACE_ERROR("app ops finalize error", QMUX_EV_QCC_NEW, qcc->conn); |
| 896 | goto err; |
| 897 | } |
| 898 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 899 | } |
| 900 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 901 | TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn); |
| 902 | return 0; |
| 903 | |
| 904 | err: |
| 905 | TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn); |
| 906 | return 1; |
| 907 | } |
| 908 | |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 909 | /* Handle a new STREAM frame for stream with id <id>. Payload is pointed by |
| 910 | * <data> with length <len> and represents the offset <offset>. <fin> is set if |
| 911 | * the QUIC frame FIN bit is set. |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 912 | * |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 913 | * Returns 0 on success else non-zero. On error, the received frame should not |
| 914 | * be acknowledged. |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 915 | */ |
| 916 | 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] | 917 | char fin, char *data) |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 918 | { |
| 919 | struct qcs *qcs; |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 920 | enum ncb_ret ret; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 921 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 922 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
| 923 | |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 924 | if (qcc->flags & QC_CF_CC_EMIT) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 925 | TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn); |
| 926 | goto err; |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 927 | } |
| 928 | |
Amaury Denoyelle | 6754d7e | 2022-05-23 16:12:49 +0200 | [diff] [blame] | 929 | /* RFC 9000 19.8. STREAM Frames |
| 930 | * |
| 931 | * An endpoint MUST terminate the connection with error |
| 932 | * STREAM_STATE_ERROR if it receives a STREAM frame for a locally |
| 933 | * initiated stream that has not yet been created, or for a send-only |
| 934 | * stream. |
| 935 | */ |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 936 | if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 937 | TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn); |
| 938 | goto err; |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | if (!qcs) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 942 | TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn); |
| 943 | goto out; |
Amaury Denoyelle | 57161b7 | 2022-07-07 15:02:32 +0200 | [diff] [blame] | 944 | } |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 945 | |
Amaury Denoyelle | bf91e39 | 2022-07-04 10:02:04 +0200 | [diff] [blame] | 946 | /* RFC 9000 4.5. Stream Final Size |
| 947 | * |
| 948 | * Once a final size for a stream is known, it cannot change. If a |
| 949 | * RESET_STREAM or STREAM frame is received indicating a change in the |
| 950 | * final size for the stream, an endpoint SHOULD respond with an error |
| 951 | * of type FINAL_SIZE_ERROR; see Section 11 for details on error |
| 952 | * handling. |
| 953 | */ |
| 954 | if (qcs->flags & QC_SF_SIZE_KNOWN && |
| 955 | (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) { |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 956 | TRACE_ERROR("final size error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, qcc->conn, qcs); |
Amaury Denoyelle | bf91e39 | 2022-07-04 10:02:04 +0200 | [diff] [blame] | 957 | qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 958 | goto err; |
Amaury Denoyelle | bf91e39 | 2022-07-04 10:02:04 +0200 | [diff] [blame] | 959 | } |
| 960 | |
Amaury Denoyelle | 5854fc0 | 2022-12-09 16:25:48 +0100 | [diff] [blame] | 961 | if (qcs_is_close_remote(qcs)) { |
| 962 | TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn); |
| 963 | goto out; |
| 964 | } |
| 965 | |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 966 | if (offset + len <= qcs->rx.offset) { |
Amaury Denoyelle | d1310f8 | 2022-09-16 13:30:59 +0200 | [diff] [blame] | 967 | /* TODO offset may have been received without FIN first and now |
| 968 | * with it. In this case, it must be notified to be able to |
| 969 | * close the stream. |
| 970 | */ |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 971 | TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 972 | goto out; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 973 | } |
| 974 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 975 | TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 976 | qcs_idle_open(qcs); |
| 977 | |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 978 | if (offset + len > qcs->rx.offset_max) { |
| 979 | uint64_t diff = offset + len - qcs->rx.offset_max; |
| 980 | qcs->rx.offset_max = offset + len; |
| 981 | qcc->lfctl.offsets_recv += diff; |
| 982 | |
| 983 | if (offset + len > qcs->rx.msd || |
| 984 | qcc->lfctl.offsets_recv > qcc->lfctl.md) { |
| 985 | /* RFC 9000 4.1. Data Flow Control |
| 986 | * |
| 987 | * A receiver MUST close the connection with an error |
| 988 | * of type FLOW_CONTROL_ERROR if the sender violates |
| 989 | * the advertised connection or stream data limits |
| 990 | */ |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 991 | TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 992 | qcc->conn, qcs); |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 993 | qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 994 | goto err; |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 995 | } |
| 996 | } |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 997 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 998 | 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] | 999 | /* TODO should mark qcs as full */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1000 | ABORT_NOW(); |
| 1001 | return 1; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 1004 | TRACE_DATA("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] | 1005 | if (offset < qcs->rx.offset) { |
Frédéric Lécaille | a18c333 | 2022-07-04 09:54:58 +0200 | [diff] [blame] | 1006 | size_t diff = qcs->rx.offset - offset; |
| 1007 | |
| 1008 | len -= diff; |
| 1009 | data += diff; |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1010 | offset = qcs->rx.offset; |
| 1011 | } |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1012 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1013 | ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE); |
| 1014 | if (ret != NCB_RET_OK) { |
| 1015 | if (ret == NCB_RET_DATA_REJ) { |
Amaury Denoyelle | cc3d716 | 2022-05-20 15:14:57 +0200 | [diff] [blame] | 1016 | /* RFC 9000 2.2. Sending and Receiving Data |
| 1017 | * |
| 1018 | * An endpoint could receive data for a stream at the |
| 1019 | * same stream offset multiple times. Data that has |
| 1020 | * already been received can be discarded. The data at |
| 1021 | * a given offset MUST NOT change if it is sent |
| 1022 | * multiple times; an endpoint MAY treat receipt of |
| 1023 | * different data at the same offset within a stream as |
| 1024 | * a connection error of type PROTOCOL_VIOLATION. |
| 1025 | */ |
Amaury Denoyelle | c7fb0d2 | 2022-08-10 16:39:54 +0200 | [diff] [blame] | 1026 | TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1027 | qcc->conn, qcs); |
Amaury Denoyelle | cc3d716 | 2022-05-20 15:14:57 +0200 | [diff] [blame] | 1028 | qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION); |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1029 | } |
| 1030 | else if (ret == NCB_RET_GAP_SIZE) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 1031 | TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, |
| 1032 | qcc->conn, qcs); |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1033 | } |
| 1034 | return 1; |
| 1035 | } |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1036 | |
| 1037 | if (fin) |
Amaury Denoyelle | 3f39b40 | 2022-07-01 16:11:03 +0200 | [diff] [blame] | 1038 | qcs->flags |= QC_SF_SIZE_KNOWN; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1039 | |
Amaury Denoyelle | d1310f8 | 2022-09-16 13:30:59 +0200 | [diff] [blame] | 1040 | if (qcs->flags & QC_SF_SIZE_KNOWN && |
| 1041 | qcs->rx.offset_max == qcs->rx.offset + ncb_data(&qcs->rx.ncbuf, 0)) { |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1042 | qcs_close_remote(qcs); |
Amaury Denoyelle | d1310f8 | 2022-09-16 13:30:59 +0200 | [diff] [blame] | 1043 | } |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1044 | |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 1045 | if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) { |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 1046 | qcc_decode_qcs(qcc, qcs); |
Amaury Denoyelle | 418ba21 | 2022-08-02 15:57:16 +0200 | [diff] [blame] | 1047 | qcc_refresh_timeout(qcc); |
| 1048 | } |
Amaury Denoyelle | 3a08640 | 2022-05-18 11:38:22 +0200 | [diff] [blame] | 1049 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1050 | out: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1051 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1052 | return 0; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1053 | |
| 1054 | err: |
| 1055 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 1056 | return 1; |
Amaury Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 1057 | } |
| 1058 | |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 1059 | /* Handle a new MAX_DATA frame. <max> must contains the maximum data field of |
| 1060 | * the frame. |
| 1061 | * |
| 1062 | * Returns 0 on success else non-zero. |
| 1063 | */ |
| 1064 | int qcc_recv_max_data(struct qcc *qcc, uint64_t max) |
| 1065 | { |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1066 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
| 1067 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1068 | TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 1069 | if (qcc->rfctl.md < max) { |
| 1070 | qcc->rfctl.md = max; |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1071 | TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 1072 | |
| 1073 | if (qcc->flags & QC_CF_BLK_MFCTL) { |
| 1074 | qcc->flags &= ~QC_CF_BLK_MFCTL; |
| 1075 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 1076 | } |
| 1077 | } |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1078 | |
| 1079 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 1e5e513 | 2022-03-08 16:23:03 +0100 | [diff] [blame] | 1080 | return 0; |
| 1081 | } |
| 1082 | |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1083 | /* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data |
| 1084 | * field of the frame and <id> is the identifier of the QUIC stream. |
| 1085 | * |
Amaury Denoyelle | b68559a | 2022-07-06 15:45:20 +0200 | [diff] [blame] | 1086 | * Returns 0 on success else non-zero. On error, the received frame should not |
| 1087 | * be acknowledged. |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1088 | */ |
| 1089 | int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max) |
| 1090 | { |
| 1091 | struct qcs *qcs; |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1092 | |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1093 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
| 1094 | |
Amaury Denoyelle | b68559a | 2022-07-06 15:45:20 +0200 | [diff] [blame] | 1095 | /* RFC 9000 19.10. MAX_STREAM_DATA Frames |
| 1096 | * |
| 1097 | * Receiving a MAX_STREAM_DATA frame for a locally |
| 1098 | * initiated stream that has not yet been created MUST be treated as a |
| 1099 | * connection error of type STREAM_STATE_ERROR. An endpoint that |
| 1100 | * receives a MAX_STREAM_DATA frame for a receive-only stream MUST |
| 1101 | * terminate the connection with error STREAM_STATE_ERROR. |
| 1102 | */ |
| 1103 | if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) { |
| 1104 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 1105 | return 1; |
| 1106 | } |
| 1107 | |
| 1108 | if (qcs) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1109 | TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1110 | if (max > qcs->tx.msd) { |
| 1111 | qcs->tx.msd = max; |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1112 | TRACE_DEVEL("increase remote max-stream-data", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1113 | |
| 1114 | if (qcs->flags & QC_SF_BLK_SFCTL) { |
| 1115 | qcs->flags &= ~QC_SF_BLK_SFCTL; |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 1116 | /* TODO optim: only wakeup IO-CB if stream has data to sent. */ |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1117 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 1122 | if (qcc_may_expire(qcc) && !qcc->nb_hreq) |
| 1123 | qcc_refresh_timeout(qcc); |
| 1124 | |
Amaury Denoyelle | 5854fc0 | 2022-12-09 16:25:48 +0100 | [diff] [blame] | 1125 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | /* Handle a new RESET_STREAM frame from stream ID <id> with error code <err> |
| 1130 | * and final stream size <final_size>. |
| 1131 | * |
| 1132 | * Returns 0 on success else non-zero. On error, the received frame should not |
| 1133 | * be acknowledged. |
| 1134 | */ |
| 1135 | int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size) |
| 1136 | { |
| 1137 | struct qcs *qcs; |
| 1138 | |
| 1139 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
| 1140 | |
| 1141 | /* RFC 9000 19.4. RESET_STREAM Frames |
| 1142 | * |
| 1143 | * An endpoint that receives a RESET_STREAM frame for a send-only stream |
| 1144 | * MUST terminate the connection with error STREAM_STATE_ERROR. |
| 1145 | */ |
| 1146 | if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) { |
| 1147 | TRACE_ERROR("RESET_STREAM for send-only stream received", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 1148 | qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR); |
| 1149 | goto err; |
| 1150 | } |
| 1151 | |
| 1152 | if (!qcs || qcs_is_close_remote(qcs)) |
| 1153 | goto out; |
| 1154 | |
| 1155 | TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 1156 | qcs_idle_open(qcs); |
| 1157 | |
| 1158 | if (qcs->rx.offset_max > final_size || |
| 1159 | ((qcs->flags & QC_SF_SIZE_KNOWN) && qcs->rx.offset_max != final_size)) { |
| 1160 | TRACE_ERROR("final size error on RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 1161 | qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR); |
| 1162 | goto err; |
| 1163 | } |
| 1164 | |
| 1165 | qcs->flags |= QC_SF_SIZE_KNOWN; |
| 1166 | qcs_close_remote(qcs); |
| 1167 | qc_free_ncbuf(qcs, &qcs->rx.ncbuf); |
| 1168 | |
| 1169 | if (qcs_sc(qcs)) { |
| 1170 | se_fl_set_error(qcs->sd); |
| 1171 | qcs_alert(qcs); |
| 1172 | } |
| 1173 | |
| 1174 | out: |
Amaury Denoyelle | 392e94e | 2022-07-06 15:44:16 +0200 | [diff] [blame] | 1175 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1176 | return 0; |
Amaury Denoyelle | 5854fc0 | 2022-12-09 16:25:48 +0100 | [diff] [blame] | 1177 | |
| 1178 | err: |
| 1179 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 1180 | return 1; |
Amaury Denoyelle | 8727ff4 | 2022-03-08 10:39:55 +0100 | [diff] [blame] | 1181 | } |
| 1182 | |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 1183 | /* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be |
| 1184 | * specified in <err>. |
| 1185 | * |
| 1186 | * Returns 0 on success else non-zero. On error, the received frame should not |
| 1187 | * be acknowledged. |
| 1188 | */ |
| 1189 | int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err) |
| 1190 | { |
| 1191 | struct qcs *qcs; |
| 1192 | |
| 1193 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
| 1194 | |
| 1195 | /* RFC 9000 19.5. STOP_SENDING Frames |
| 1196 | * |
| 1197 | * Receiving a STOP_SENDING frame for a |
| 1198 | * locally initiated stream that has not yet been created MUST be |
| 1199 | * treated as a connection error of type STREAM_STATE_ERROR. An |
| 1200 | * endpoint that receives a STOP_SENDING frame for a receive-only stream |
| 1201 | * MUST terminate the connection with error STREAM_STATE_ERROR. |
| 1202 | */ |
| 1203 | if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) { |
| 1204 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 1205 | return 1; |
| 1206 | } |
| 1207 | |
| 1208 | if (!qcs) |
| 1209 | goto out; |
| 1210 | |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 1211 | TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
Amaury Denoyelle | d775537 | 2022-10-03 17:20:31 +0200 | [diff] [blame] | 1212 | |
| 1213 | /* RFC 9000 3.5. Solicited State Transitions |
| 1214 | * |
| 1215 | * An endpoint is expected to send another STOP_SENDING frame if a |
| 1216 | * packet containing a previous STOP_SENDING is lost. However, once |
| 1217 | * either all stream data or a RESET_STREAM frame has been received for |
| 1218 | * the stream -- that is, the stream is in any state other than "Recv" |
| 1219 | * or "Size Known" -- sending a STOP_SENDING frame is unnecessary. |
| 1220 | */ |
| 1221 | |
| 1222 | /* TODO thanks to previous RFC clause, STOP_SENDING is ignored if current stream |
| 1223 | * has already been closed locally. This is useful to not emit multiple |
| 1224 | * RESET_STREAM for a single stream. This is functional if stream is |
| 1225 | * locally closed due to all data transmitted, but in this case the RFC |
| 1226 | * advices to use an explicit RESET_STREAM. |
| 1227 | */ |
| 1228 | if (qcs_is_close_local(qcs)) { |
| 1229 | TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs); |
| 1230 | goto out; |
| 1231 | } |
| 1232 | |
Amaury Denoyelle | 96ca1b7 | 2022-08-09 17:36:38 +0200 | [diff] [blame] | 1233 | qcs_idle_open(qcs); |
| 1234 | |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 1235 | /* RFC 9000 3.5. Solicited State Transitions |
| 1236 | * |
| 1237 | * An endpoint that receives a STOP_SENDING frame |
| 1238 | * MUST send a RESET_STREAM frame if the stream is in the "Ready" or |
| 1239 | * "Send" state. If the stream is in the "Data Sent" state, the |
| 1240 | * endpoint MAY defer sending the RESET_STREAM frame until the packets |
| 1241 | * containing outstanding data are acknowledged or declared lost. If |
| 1242 | * any outstanding data is declared lost, the endpoint SHOULD send a |
| 1243 | * RESET_STREAM frame instead of retransmitting the data. |
| 1244 | * |
| 1245 | * An endpoint SHOULD copy the error code from the STOP_SENDING frame to |
| 1246 | * the RESET_STREAM frame it sends, but it can use any application error |
| 1247 | * code. |
| 1248 | */ |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 1249 | qcc_reset_stream(qcs, err); |
| 1250 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 1251 | if (qcc_may_expire(qcc) && !qcc->nb_hreq) |
| 1252 | qcc_refresh_timeout(qcc); |
| 1253 | |
Amaury Denoyelle | a5b5075 | 2022-07-04 11:44:53 +0200 | [diff] [blame] | 1254 | out: |
| 1255 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1259 | /* Signal the closing of remote stream with id <id>. Flow-control for new |
| 1260 | * streams may be allocated for the peer if needed. |
| 1261 | */ |
| 1262 | static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id) |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1263 | { |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1264 | struct quic_frame *frm; |
| 1265 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1266 | TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn); |
| 1267 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1268 | if (quic_stream_is_bidi(id)) { |
| 1269 | ++qcc->lfctl.cl_bidi_r; |
| 1270 | if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1271 | TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1272 | frm = pool_zalloc(pool_head_quic_frame); |
| 1273 | BUG_ON(!frm); /* TODO handle this properly */ |
| 1274 | |
| 1275 | LIST_INIT(&frm->reflist); |
| 1276 | frm->type = QUIC_FT_MAX_STREAMS_BIDI; |
| 1277 | frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi + |
| 1278 | qcc->lfctl.cl_bidi_r; |
| 1279 | LIST_APPEND(&qcc->lfctl.frms, &frm->list); |
| 1280 | tasklet_wakeup(qcc->wait_event.tasklet); |
| 1281 | |
| 1282 | qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r; |
| 1283 | qcc->lfctl.cl_bidi_r = 0; |
| 1284 | } |
| 1285 | } |
| 1286 | else { |
Amaury Denoyelle | 9107731 | 2022-12-22 18:56:09 +0100 | [diff] [blame] | 1287 | /* TODO unidirectional stream flow control with MAX_STREAMS_UNI |
| 1288 | * emission not implemented. It should be unnecessary for |
| 1289 | * HTTP/3 but may be required if other application protocols |
| 1290 | * are supported. |
Amaury Denoyelle | bf3c208 | 2022-08-16 11:29:08 +0200 | [diff] [blame] | 1291 | */ |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1292 | } |
| 1293 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1294 | TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn); |
| 1295 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1296 | return 0; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1297 | } |
| 1298 | |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 1299 | /* 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] | 1300 | static void qcs_destroy(struct qcs *qcs) |
| 1301 | { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1302 | struct connection *conn = qcs->qcc->conn; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 1303 | const uint64_t id = qcs->id; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1304 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1305 | TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1306 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1307 | if (quic_stream_is_remote(qcs->qcc, id)) |
| 1308 | qcc_release_remote_stream(qcs->qcc, id); |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 1309 | |
Amaury Denoyelle | dccbd73 | 2022-03-29 18:36:59 +0200 | [diff] [blame] | 1310 | qcs_free(qcs); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1311 | |
| 1312 | TRACE_LEAVE(QMUX_EV_QCS_END, conn); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1313 | } |
| 1314 | |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1315 | /* Transfer as much as possible data on <qcs> from <in> to <out>. This is done |
| 1316 | * in respect with available flow-control at stream and connection level. |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1317 | * |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 1318 | * Returns the total bytes of transferred data. |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1319 | */ |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1320 | static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in) |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1321 | { |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1322 | struct qcc *qcc = qcs->qcc; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1323 | int left, to_xfer; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1324 | int total = 0; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1325 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1326 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1327 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1328 | qc_get_buf(qcs, out); |
| 1329 | |
| 1330 | /* |
| 1331 | * QCS out buffer diagram |
| 1332 | * head left to_xfer |
| 1333 | * -------------> ----------> -----> |
Amaury Denoyelle | e0320b8 | 2022-03-11 19:12:23 +0100 | [diff] [blame] | 1334 | * -------------------------------------------------- |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1335 | * |...............|xxxxxxxxxxx|<<<<< |
Amaury Denoyelle | e0320b8 | 2022-03-11 19:12:23 +0100 | [diff] [blame] | 1336 | * -------------------------------------------------- |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1337 | * ^ ack-off ^ sent-off ^ off |
| 1338 | * |
| 1339 | * STREAM frame |
| 1340 | * ^ ^ |
| 1341 | * |xxxxxxxxxxxxxxxxx| |
| 1342 | */ |
| 1343 | |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1344 | BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1345 | BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset); |
Amaury Denoyelle | 78fa559 | 2022-06-10 15:18:12 +0200 | [diff] [blame] | 1346 | BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1347 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1348 | left = qcs->tx.offset - qcs->tx.sent_offset; |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 1349 | to_xfer = QUIC_MIN(b_data(in), b_room(out)); |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 1350 | |
| 1351 | BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd); |
| 1352 | /* do not exceed flow control limit */ |
| 1353 | if (qcs->tx.offset + to_xfer > qcs->tx.msd) |
| 1354 | to_xfer = qcs->tx.msd - qcs->tx.offset; |
| 1355 | |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1356 | BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md); |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1357 | /* do not overcome flow control limit on connection */ |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1358 | if (qcc->tx.offsets + to_xfer > qcc->rfctl.md) |
| 1359 | to_xfer = qcc->rfctl.md - qcc->tx.offsets; |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1360 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1361 | if (!left && !to_xfer) |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 1362 | goto out; |
| 1363 | |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 1364 | total = b_force_xfer(out, in, to_xfer); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1365 | |
| 1366 | out: |
| 1367 | { |
| 1368 | struct qcs_xfer_data_trace_arg arg = { |
| 1369 | .prep = b_data(out), .xfer = total, |
| 1370 | }; |
| 1371 | TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA, |
| 1372 | qcc->conn, qcs, &arg); |
| 1373 | } |
| 1374 | |
| 1375 | return total; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1376 | } |
| 1377 | |
Amaury Denoyelle | fe8f555 | 2022-04-27 16:44:49 +0200 | [diff] [blame] | 1378 | /* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame |
| 1379 | * is appended in <frm_list>. Set <fin> if this is supposed to be the last |
| 1380 | * stream frame. |
| 1381 | * |
| 1382 | * Returns the length of the STREAM frame or a negative error code. |
| 1383 | */ |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1384 | static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin, |
| 1385 | struct list *frm_list) |
| 1386 | { |
| 1387 | struct qcc *qcc = qcs->qcc; |
| 1388 | struct quic_frame *frm; |
| 1389 | int head, total; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1390 | uint64_t base_off; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1391 | |
| 1392 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1393 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1394 | /* if ack_offset < buf_offset, it points to an older buffer. */ |
| 1395 | base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset); |
| 1396 | BUG_ON(qcs->tx.sent_offset < base_off); |
| 1397 | |
| 1398 | head = qcs->tx.sent_offset - base_off; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1399 | total = b_data(out) - head; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1400 | BUG_ON(total < 0); |
| 1401 | |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1402 | if (!total && !fin) { |
| 1403 | /* No need to send anything if total is NULL and no FIN to signal. */ |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1404 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1405 | return 0; |
| 1406 | } |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1407 | BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) || |
| 1408 | (total && qcs->tx.sent_offset >= qcs->tx.offset)); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1409 | BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset); |
Amaury Denoyelle | 78fa559 | 2022-06-10 15:18:12 +0200 | [diff] [blame] | 1410 | BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1411 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1412 | TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1413 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1414 | if (!frm) { |
| 1415 | TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1416 | goto err; |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1417 | } |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1418 | |
Frédéric Lécaille | b917191 | 2022-04-21 17:32:10 +0200 | [diff] [blame] | 1419 | LIST_INIT(&frm->reflist); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1420 | frm->type = QUIC_FT_STREAM_8; |
Amaury Denoyelle | 7272cd7 | 2022-03-29 15:15:54 +0200 | [diff] [blame] | 1421 | frm->stream.stream = qcs->stream; |
Amaury Denoyelle | d8e680c | 2022-03-29 15:18:44 +0200 | [diff] [blame] | 1422 | frm->stream.id = qcs->id; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1423 | frm->stream.buf = out; |
| 1424 | frm->stream.data = (unsigned char *)b_peek(out, head); |
| 1425 | |
Amaury Denoyelle | fecfa0d | 2021-12-07 16:50:14 +0100 | [diff] [blame] | 1426 | /* 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] | 1427 | if (fin) |
| 1428 | frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1429 | |
| 1430 | if (qcs->tx.sent_offset) { |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1431 | frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1432 | frm->stream.offset.key = qcs->tx.sent_offset; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1433 | } |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1434 | |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1435 | frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 1436 | frm->stream.len = total; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1437 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1438 | LIST_APPEND(frm_list, &frm->list); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1439 | |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 1440 | out: |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 1441 | { |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1442 | struct qcs_build_stream_trace_arg arg = { |
| 1443 | .len = frm->stream.len, .fin = fin, |
| 1444 | .offset = frm->stream.offset.key, |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 1445 | }; |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1446 | TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM, |
Amaury Denoyelle | fdcec36 | 2022-03-25 09:28:10 +0100 | [diff] [blame] | 1447 | qcc->conn, qcs, &arg); |
| 1448 | } |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1449 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1450 | return total; |
| 1451 | |
| 1452 | err: |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1453 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1454 | return -1; |
| 1455 | } |
| 1456 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 1457 | /* Check after transferring data from qcs.tx.buf if FIN must be set on the next |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1458 | * STREAM frame for <qcs>. |
| 1459 | * |
| 1460 | * Returns true if FIN must be set else false. |
| 1461 | */ |
| 1462 | static int qcs_stream_fin(struct qcs *qcs) |
| 1463 | { |
| 1464 | return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf); |
| 1465 | } |
| 1466 | |
Amaury Denoyelle | 0a1154a | 2023-01-06 17:43:11 +0100 | [diff] [blame] | 1467 | /* Return true if <qcs> has data to send in new STREAM frames. */ |
| 1468 | static forceinline int qcs_need_sending(struct qcs *qcs) |
| 1469 | { |
| 1470 | return b_data(&qcs->tx.buf) || qcs->tx.sent_offset < qcs->tx.offset || |
| 1471 | qcs_stream_fin(qcs); |
| 1472 | } |
| 1473 | |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 1474 | /* This function must be called by the upper layer to inform about the sending |
| 1475 | * of a STREAM frame for <qcs> instance. The frame is of <data> length and on |
| 1476 | * <offset>. |
| 1477 | */ |
| 1478 | void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset) |
| 1479 | { |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1480 | struct qcc *qcc = qcs->qcc; |
| 1481 | uint64_t diff; |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1482 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1483 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1484 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1485 | BUG_ON(offset > qcs->tx.sent_offset); |
Amaury Denoyelle | 78fa559 | 2022-06-10 15:18:12 +0200 | [diff] [blame] | 1486 | BUG_ON(offset + data > qcs->tx.offset); |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1487 | |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 1488 | /* check if the STREAM frame has already been notified. It can happen |
| 1489 | * for retransmission. |
| 1490 | */ |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1491 | if (offset + data < qcs->tx.sent_offset) { |
| 1492 | TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1493 | goto out; |
| 1494 | } |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1495 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1496 | qcs_idle_open(qcs); |
| 1497 | |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1498 | diff = offset + data - qcs->tx.sent_offset; |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1499 | if (diff) { |
| 1500 | /* increase offset sum on connection */ |
| 1501 | qcc->tx.sent_offsets += diff; |
| 1502 | BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md); |
Amaury Denoyelle | 31d2057 | 2023-01-06 15:29:59 +0100 | [diff] [blame] | 1503 | if (qcc->tx.sent_offsets == qcc->rfctl.md) { |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1504 | qcc->flags |= QC_CF_BLK_MFCTL; |
Amaury Denoyelle | 31d2057 | 2023-01-06 15:29:59 +0100 | [diff] [blame] | 1505 | TRACE_STATE("connection flow-control reached", QMUX_EV_QCS_SEND, qcc->conn); |
| 1506 | } |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1507 | |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1508 | /* increase offset on stream */ |
| 1509 | qcs->tx.sent_offset += diff; |
| 1510 | BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd); |
| 1511 | BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset); |
Amaury Denoyelle | 31d2057 | 2023-01-06 15:29:59 +0100 | [diff] [blame] | 1512 | if (qcs->tx.sent_offset == qcs->tx.msd) { |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1513 | qcs->flags |= QC_SF_BLK_SFCTL; |
Amaury Denoyelle | 31d2057 | 2023-01-06 15:29:59 +0100 | [diff] [blame] | 1514 | TRACE_STATE("stream flow-control reached", QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1515 | } |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1516 | |
Amaury Denoyelle | a9de7ea | 2023-01-06 17:16:47 +0100 | [diff] [blame] | 1517 | /* If qcs.stream.buf is full, release it to the lower layer. */ |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1518 | if (qcs->tx.offset == qcs->tx.sent_offset && |
| 1519 | b_full(&qcs->stream->buf->buf)) { |
| 1520 | qc_stream_buf_release(qcs->stream); |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1521 | } |
| 1522 | } |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1523 | |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 1524 | if (qcs->tx.offset == qcs->tx.sent_offset && !b_data(&qcs->tx.buf)) { |
| 1525 | /* Remove stream from send_list if all was sent. */ |
| 1526 | LIST_DEL_INIT(&qcs->el_send); |
| 1527 | TRACE_STATE("stream sent done", QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1528 | |
| 1529 | if (qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) { |
| 1530 | /* Close stream locally. */ |
| 1531 | qcs_close_local(qcs); |
| 1532 | /* Reset flag to not emit multiple FIN STREAM frames. */ |
| 1533 | qcs->flags &= ~QC_SF_FIN_STREAM; |
| 1534 | } |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1535 | } |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 1536 | |
| 1537 | out: |
| 1538 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs); |
Amaury Denoyelle | 54445d0 | 2022-03-10 16:44:14 +0100 | [diff] [blame] | 1539 | } |
| 1540 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1541 | /* Wrapper for send on transport layer. Send a list of frames <frms> for the |
| 1542 | * connection <qcc>. |
| 1543 | * |
| 1544 | * Returns 0 if all data sent with success else non-zero. |
| 1545 | */ |
| 1546 | static int qc_send_frames(struct qcc *qcc, struct list *frms) |
| 1547 | { |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1548 | TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn); |
| 1549 | |
| 1550 | if (LIST_ISEMPTY(frms)) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1551 | TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn); |
| 1552 | goto err; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 1553 | } |
Frédéric Lécaille | 4e22f28 | 2022-03-18 18:38:19 +0100 | [diff] [blame] | 1554 | |
Amaury Denoyelle | 036cc5d | 2022-09-26 15:02:31 +0200 | [diff] [blame] | 1555 | if (!qc_send_mux(qcc->conn->handle.qc, frms)) |
| 1556 | goto err; |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 1557 | |
Amaury Denoyelle | db5d1a1 | 2022-03-10 16:42:23 +0100 | [diff] [blame] | 1558 | /* If there is frames left at this stage, transport layer is blocked. |
| 1559 | * Subscribe on it to retry later. |
| 1560 | */ |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1561 | if (!LIST_ISEMPTY(frms)) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1562 | TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1563 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 1564 | SUB_RETRY_SEND, &qcc->wait_event); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1565 | goto err; |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1566 | } |
| 1567 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1568 | TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 1569 | return 0; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1570 | |
| 1571 | err: |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1572 | TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1573 | return 1; |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | /* Emit a RESET_STREAM on <qcs>. |
| 1577 | * |
| 1578 | * Returns 0 if the frame has been successfully sent else non-zero. |
| 1579 | */ |
| 1580 | static int qcs_send_reset(struct qcs *qcs) |
| 1581 | { |
| 1582 | struct list frms = LIST_HEAD_INIT(frms); |
| 1583 | struct quic_frame *frm; |
| 1584 | |
| 1585 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
| 1586 | |
| 1587 | frm = pool_zalloc(pool_head_quic_frame); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1588 | if (!frm) { |
| 1589 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 1590 | return 1; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1591 | } |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 1592 | |
| 1593 | LIST_INIT(&frm->reflist); |
| 1594 | frm->type = QUIC_FT_RESET_STREAM; |
| 1595 | frm->reset_stream.id = qcs->id; |
| 1596 | frm->reset_stream.app_error_code = qcs->err; |
| 1597 | frm->reset_stream.final_size = qcs->tx.sent_offset; |
| 1598 | |
| 1599 | LIST_APPEND(&frms, &frm->list); |
| 1600 | if (qc_send_frames(qcs->qcc, &frms)) { |
| 1601 | pool_free(pool_head_quic_frame, frm); |
| 1602 | TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
| 1603 | return 1; |
| 1604 | } |
| 1605 | |
| 1606 | if (qcs_sc(qcs)) { |
| 1607 | se_fl_set_error(qcs->sd); |
| 1608 | qcs_alert(qcs); |
| 1609 | } |
| 1610 | |
| 1611 | qcs_close_local(qcs); |
| 1612 | qcs->flags &= ~QC_SF_TO_RESET; |
| 1613 | |
| 1614 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1615 | return 0; |
| 1616 | } |
| 1617 | |
Amaury Denoyelle | 663e872 | 2022-12-09 14:58:28 +0100 | [diff] [blame] | 1618 | /* Emit a STOP_SENDING on <qcs>. |
| 1619 | * |
| 1620 | * Returns 0 if the frame has been successfully sent else non-zero. |
| 1621 | */ |
| 1622 | static int qcs_send_stop_sending(struct qcs *qcs) |
| 1623 | { |
| 1624 | struct list frms = LIST_HEAD_INIT(frms); |
| 1625 | struct quic_frame *frm; |
| 1626 | struct qcc *qcc = qcs->qcc; |
| 1627 | |
| 1628 | TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
| 1629 | |
| 1630 | /* RFC 9000 3.3. Permitted Frame Types |
| 1631 | * |
| 1632 | * A |
| 1633 | * receiver MAY send a STOP_SENDING frame in any state where it has not |
| 1634 | * received a RESET_STREAM frame -- that is, states other than "Reset |
| 1635 | * Recvd" or "Reset Read". However, there is little value in sending a |
| 1636 | * STOP_SENDING frame in the "Data Recvd" state, as all stream data has |
| 1637 | * been received. A sender could receive either of these two types of |
| 1638 | * frames in any state as a result of delayed delivery of packets.¶ |
| 1639 | */ |
| 1640 | if (qcs_is_close_remote(qcs)) { |
| 1641 | TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs); |
| 1642 | goto done; |
| 1643 | } |
| 1644 | |
| 1645 | frm = pool_zalloc(pool_head_quic_frame); |
| 1646 | if (!frm) { |
| 1647 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
| 1648 | return 1; |
| 1649 | } |
| 1650 | |
| 1651 | LIST_INIT(&frm->reflist); |
| 1652 | frm->type = QUIC_FT_STOP_SENDING; |
| 1653 | frm->stop_sending.id = qcs->id; |
| 1654 | frm->stop_sending.app_error_code = qcs->err; |
| 1655 | |
| 1656 | LIST_APPEND(&frms, &frm->list); |
| 1657 | if (qc_send_frames(qcs->qcc, &frms)) { |
| 1658 | pool_free(pool_head_quic_frame, frm); |
| 1659 | TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
| 1660 | return 1; |
| 1661 | } |
| 1662 | |
| 1663 | done: |
| 1664 | qcs->flags &= ~QC_SF_TO_STOP_SENDING; |
| 1665 | |
| 1666 | TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs); |
| 1667 | return 0; |
| 1668 | } |
| 1669 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1670 | /* Used internally by qc_send function. Proceed to send for <qcs>. This will |
| 1671 | * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1672 | * is then generated and inserted in <frms> list. |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1673 | * |
| 1674 | * Returns the total bytes transferred between qcs and quic_stream buffers. Can |
| 1675 | * be null if out buffer cannot be allocated. |
| 1676 | */ |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1677 | static int _qc_send_qcs(struct qcs *qcs, struct list *frms) |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1678 | { |
| 1679 | struct qcc *qcc = qcs->qcc; |
| 1680 | struct buffer *buf = &qcs->tx.buf; |
| 1681 | struct buffer *out = qc_stream_buf_get(qcs->stream); |
| 1682 | int xfer = 0; |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1683 | char fin = 0; |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1684 | |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 1685 | /* Cannot send STREAM on remote unidirectional streams. */ |
| 1686 | BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_remote(qcc, qcs->id)); |
| 1687 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1688 | /* Allocate <out> buffer if necessary. */ |
| 1689 | if (!out) { |
| 1690 | if (qcc->flags & QC_CF_CONN_FULL) |
| 1691 | return 0; |
| 1692 | |
| 1693 | out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset); |
| 1694 | if (!out) { |
| 1695 | qcc->flags |= QC_CF_CONN_FULL; |
| 1696 | return 0; |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | /* Transfer data from <buf> to <out>. */ |
| 1701 | if (b_data(buf)) { |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1702 | xfer = qcs_xfer_data(qcs, out, buf); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1703 | if (xfer > 0) { |
| 1704 | qcs_notify_send(qcs); |
| 1705 | qcs->flags &= ~QC_SF_BLK_MROOM; |
| 1706 | } |
| 1707 | |
| 1708 | qcs->tx.offset += xfer; |
Amaury Denoyelle | 78fa559 | 2022-06-10 15:18:12 +0200 | [diff] [blame] | 1709 | BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd); |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 1710 | qcc->tx.offsets += xfer; |
Amaury Denoyelle | 78fa559 | 2022-06-10 15:18:12 +0200 | [diff] [blame] | 1711 | BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | /* out buffer cannot be emptied if qcs offsets differ. */ |
| 1715 | BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset); |
| 1716 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 1717 | /* FIN is set if all incoming data were transferred. */ |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1718 | fin = qcs_stream_fin(qcs); |
| 1719 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1720 | /* Build a new STREAM frame with <out> buffer. */ |
Amaury Denoyelle | e53b489 | 2022-07-08 17:19:40 +0200 | [diff] [blame] | 1721 | if (qcs->tx.sent_offset != qcs->tx.offset || fin) { |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1722 | int ret; |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1723 | ret = qcs_build_stream_frm(qcs, out, fin, frms); |
Amaury Denoyelle | b50f311 | 2022-04-28 14:41:50 +0200 | [diff] [blame] | 1724 | if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ } |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | return xfer; |
| 1728 | } |
| 1729 | |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1730 | /* Proceed to sending. Loop through all available streams for the <qcc> |
| 1731 | * instance and try to send as much as possible. |
| 1732 | * |
| 1733 | * Returns the total of bytes sent to the transport layer. |
| 1734 | */ |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1735 | static int qc_send(struct qcc *qcc) |
| 1736 | { |
Amaury Denoyelle | 6ccfa3c | 2022-03-10 16:45:53 +0100 | [diff] [blame] | 1737 | struct list frms = LIST_HEAD_INIT(frms); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1738 | struct qcs *qcs, *qcs_tmp; |
Amaury Denoyelle | a9de7ea | 2023-01-06 17:16:47 +0100 | [diff] [blame] | 1739 | int total = 0; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1740 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1741 | TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn); |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 1742 | |
Amaury Denoyelle | 9fab9fd | 2022-05-20 15:04:38 +0200 | [diff] [blame] | 1743 | 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] | 1744 | qcc->conn->flags |= CO_FL_ERROR; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1745 | TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn); |
| 1746 | goto err; |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 1747 | } |
| 1748 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 1749 | if (!LIST_ISEMPTY(&qcc->lfctl.frms)) { |
| 1750 | if (qc_send_frames(qcc, &qcc->lfctl.frms)) { |
| 1751 | TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn); |
| 1752 | goto out; |
| 1753 | } |
| 1754 | } |
Amaury Denoyelle | c933780 | 2022-04-04 16:36:34 +0200 | [diff] [blame] | 1755 | |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1756 | if (qcc->flags & QC_CF_BLK_MFCTL) |
Amaury Denoyelle | 0a1154a | 2023-01-06 17:43:11 +0100 | [diff] [blame] | 1757 | goto err; |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 1758 | |
Amaury Denoyelle | 0a1154a | 2023-01-06 17:43:11 +0100 | [diff] [blame] | 1759 | /* Send STREAM/STOP_SENDING/RESET_STREAM data for registered streams. */ |
| 1760 | list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) { |
| 1761 | /* Stream must not be present in send_list if it has nothing to send. */ |
| 1762 | BUG_ON(!(qcs->flags & (QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET)) && |
| 1763 | !qcs_need_sending(qcs)); |
Amaury Denoyelle | c6195d7 | 2022-05-23 11:39:14 +0200 | [diff] [blame] | 1764 | |
Amaury Denoyelle | 0a1154a | 2023-01-06 17:43:11 +0100 | [diff] [blame] | 1765 | /* Each STOP_SENDING/RESET_STREAM frame is sent individually to |
| 1766 | * guarantee its emission. |
| 1767 | * |
| 1768 | * TODO multiplex several frames in same datagram to optimize sending |
| 1769 | */ |
| 1770 | if (qcs->flags & QC_SF_TO_STOP_SENDING) { |
| 1771 | if (qcs_send_stop_sending(qcs)) |
| 1772 | goto out; |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 1773 | |
Amaury Denoyelle | 0a1154a | 2023-01-06 17:43:11 +0100 | [diff] [blame] | 1774 | /* Remove stream from send_list if it had only STOP_SENDING |
| 1775 | * to send. |
| 1776 | */ |
| 1777 | if (!(qcs->flags & QC_SF_TO_RESET) && !qcs_need_sending(qcs)) { |
| 1778 | LIST_DEL_INIT(&qcs->el_send); |
| 1779 | continue; |
| 1780 | } |
Amaury Denoyelle | e2ec942 | 2022-03-10 16:46:18 +0100 | [diff] [blame] | 1781 | } |
| 1782 | |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 1783 | if (qcs->flags & QC_SF_TO_RESET) { |
Amaury Denoyelle | 0a1154a | 2023-01-06 17:43:11 +0100 | [diff] [blame] | 1784 | if (qcs_send_reset(qcs)) |
| 1785 | goto out; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 1786 | |
Amaury Denoyelle | 0a1154a | 2023-01-06 17:43:11 +0100 | [diff] [blame] | 1787 | /* RFC 9000 3.3. Permitted Frame Types |
| 1788 | * |
| 1789 | * A sender MUST NOT send |
| 1790 | * a STREAM or STREAM_DATA_BLOCKED frame for a stream in the |
| 1791 | * "Reset Sent" state or any terminal state -- that is, after |
| 1792 | * sending a RESET_STREAM frame. |
| 1793 | */ |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 1794 | LIST_DEL_INIT(&qcs->el_send); |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 1795 | continue; |
| 1796 | } |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 1797 | |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 1798 | if (!(qcs->flags & QC_SF_BLK_SFCTL)) |
| 1799 | total += _qc_send_qcs(qcs, &frms); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1800 | } |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1801 | |
Amaury Denoyelle | a9de7ea | 2023-01-06 17:16:47 +0100 | [diff] [blame] | 1802 | /* Retry sending until no frame to send, data rejected or connection |
| 1803 | * flow-control limit reached. |
| 1804 | */ |
| 1805 | while (qc_send_frames(qcc, &frms) == 0 && !(qcc->flags & QC_CF_BLK_MFCTL)) { |
| 1806 | /* Reloop over <qcc.send_list>. Useful for streams which have |
| 1807 | * fulfilled their qc_stream_desc buf and have now release it. |
| 1808 | */ |
| 1809 | list_for_each_entry(qcs, &qcc->send_list, el_send) { |
| 1810 | /* Only streams blocked on flow-control or waiting on a |
| 1811 | * new qc_stream_desc should be present in send_list as |
| 1812 | * long as transport layer can handle all data. |
| 1813 | */ |
| 1814 | BUG_ON(qcs->stream->buf && !(qcs->flags & QC_SF_BLK_SFCTL)); |
Amaury Denoyelle | da6ad20 | 2022-04-12 11:41:04 +0200 | [diff] [blame] | 1815 | |
Amaury Denoyelle | a9de7ea | 2023-01-06 17:16:47 +0100 | [diff] [blame] | 1816 | if (!(qcs->flags & QC_SF_BLK_SFCTL)) |
| 1817 | total += _qc_send_qcs(qcs, &frms); |
| 1818 | } |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 1819 | } |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 1820 | |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 1821 | out: |
Amaury Denoyelle | 43c090c | 2022-06-10 15:16:40 +0200 | [diff] [blame] | 1822 | /* Deallocate frames that the transport layer has rejected. */ |
| 1823 | if (!LIST_ISEMPTY(&frms)) { |
| 1824 | struct quic_frame *frm, *frm2; |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 1825 | |
Amaury Denoyelle | 43c090c | 2022-06-10 15:16:40 +0200 | [diff] [blame] | 1826 | list_for_each_entry_safe(frm, frm2, &frms, list) { |
| 1827 | LIST_DELETE(&frm->list); |
| 1828 | pool_free(pool_head_quic_frame, frm); |
| 1829 | } |
| 1830 | } |
| 1831 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1832 | TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn); |
Amaury Denoyelle | 75d14ad | 2022-03-22 15:10:29 +0100 | [diff] [blame] | 1833 | return total; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1834 | |
| 1835 | err: |
| 1836 | TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn); |
| 1837 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1840 | /* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs |
| 1841 | * operation. |
| 1842 | * |
| 1843 | * Returns 0 on success else non-zero. |
| 1844 | */ |
| 1845 | static int qc_recv(struct qcc *qcc) |
| 1846 | { |
| 1847 | struct eb64_node *node; |
| 1848 | struct qcs *qcs; |
| 1849 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1850 | TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | e1cad8b | 2022-05-23 18:52:11 +0200 | [diff] [blame] | 1851 | |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 1852 | if (qcc->flags & QC_CF_CC_EMIT) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1853 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 5c4373a | 2022-05-24 14:47:48 +0200 | [diff] [blame] | 1854 | return 0; |
| 1855 | } |
| 1856 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1857 | node = eb64_first(&qcc->streams_by_id); |
| 1858 | while (node) { |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1859 | uint64_t id; |
| 1860 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1861 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1862 | id = qcs->id; |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1863 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1864 | 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] | 1865 | node = eb64_next(node); |
| 1866 | continue; |
| 1867 | } |
| 1868 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1869 | if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) { |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1870 | node = eb64_next(node); |
| 1871 | continue; |
| 1872 | } |
| 1873 | |
| 1874 | qcc_decode_qcs(qcc, qcs); |
| 1875 | node = eb64_next(node); |
| 1876 | } |
| 1877 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 1878 | TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn); |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 1879 | return 0; |
| 1880 | } |
| 1881 | |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1882 | |
| 1883 | /* Release all streams which have their transfer operation achieved. |
Amaury Denoyelle | 6a4aebf | 2022-02-01 10:16:05 +0100 | [diff] [blame] | 1884 | * |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1885 | * Returns true if at least one stream is released. |
Amaury Denoyelle | 6a4aebf | 2022-02-01 10:16:05 +0100 | [diff] [blame] | 1886 | */ |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1887 | static int qc_purge_streams(struct qcc *qcc) |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1888 | { |
| 1889 | struct eb64_node *node; |
| 1890 | int release = 0; |
| 1891 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1892 | TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1893 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1894 | node = eb64_first(&qcc->streams_by_id); |
| 1895 | while (node) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 1896 | struct qcs *qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1897 | node = eb64_next(node); |
| 1898 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1899 | /* Release not attached closed streams. */ |
| 1900 | if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 1901 | TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 1902 | qcs_destroy(qcs); |
| 1903 | release = 1; |
| 1904 | continue; |
| 1905 | } |
| 1906 | |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1907 | /* Release detached streams with empty buffer. */ |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1908 | if (qcs->flags & QC_SF_DETACH) { |
Amaury Denoyelle | 20d1f84 | 2022-07-11 11:23:17 +0200 | [diff] [blame] | 1909 | if (qcs_is_close_local(qcs)) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 1910 | TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1911 | qcs_destroy(qcs); |
| 1912 | release = 1; |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1913 | continue; |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1914 | } |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 1915 | |
| 1916 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 1917 | SUB_RETRY_SEND, &qcc->wait_event); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1918 | } |
| 1919 | } |
| 1920 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1921 | TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 1922 | return release; |
| 1923 | } |
| 1924 | |
Amaury Denoyelle | c49d5d1 | 2022-07-15 10:32:53 +0200 | [diff] [blame] | 1925 | /* release function. This one should be called to free all resources allocated |
| 1926 | * to the mux. |
| 1927 | */ |
| 1928 | static void qc_release(struct qcc *qcc) |
| 1929 | { |
| 1930 | struct connection *conn = qcc->conn; |
| 1931 | struct eb64_node *node; |
| 1932 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 1933 | TRACE_ENTER(QMUX_EV_QCC_END, conn); |
Amaury Denoyelle | c49d5d1 | 2022-07-15 10:32:53 +0200 | [diff] [blame] | 1934 | |
Amaury Denoyelle | f8aaf8b | 2022-09-14 16:23:47 +0200 | [diff] [blame] | 1935 | if (qcc->app_ops && qcc->app_ops->shutdown) { |
Amaury Denoyelle | c49d5d1 | 2022-07-15 10:32:53 +0200 | [diff] [blame] | 1936 | /* Application protocol with dedicated connection closing |
| 1937 | * procedure. |
| 1938 | */ |
Amaury Denoyelle | f8aaf8b | 2022-09-14 16:23:47 +0200 | [diff] [blame] | 1939 | qcc->app_ops->shutdown(qcc->ctx); |
Amaury Denoyelle | a154dc0 | 2022-06-13 17:09:01 +0200 | [diff] [blame] | 1940 | |
| 1941 | /* useful if application protocol should emit some closing |
| 1942 | * frames. For example HTTP/3 GOAWAY frame. |
| 1943 | */ |
| 1944 | qc_send(qcc); |
Amaury Denoyelle | c49d5d1 | 2022-07-15 10:32:53 +0200 | [diff] [blame] | 1945 | } |
| 1946 | else { |
| 1947 | qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0); |
| 1948 | } |
| 1949 | |
| 1950 | if (qcc->task) { |
| 1951 | task_destroy(qcc->task); |
| 1952 | qcc->task = NULL; |
| 1953 | } |
| 1954 | |
| 1955 | if (qcc->wait_event.tasklet) |
| 1956 | tasklet_free(qcc->wait_event.tasklet); |
| 1957 | if (conn && qcc->wait_event.events) { |
| 1958 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, |
| 1959 | qcc->wait_event.events, |
| 1960 | &qcc->wait_event); |
| 1961 | } |
| 1962 | |
| 1963 | /* liberate remaining qcs instances */ |
| 1964 | node = eb64_first(&qcc->streams_by_id); |
| 1965 | while (node) { |
| 1966 | struct qcs *qcs = eb64_entry(node, struct qcs, by_id); |
| 1967 | node = eb64_next(node); |
| 1968 | qcs_free(qcs); |
| 1969 | } |
| 1970 | |
| 1971 | while (!LIST_ISEMPTY(&qcc->lfctl.frms)) { |
| 1972 | struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list); |
| 1973 | LIST_DELETE(&frm->list); |
| 1974 | pool_free(pool_head_quic_frame, frm); |
| 1975 | } |
| 1976 | |
Amaury Denoyelle | f8aaf8b | 2022-09-14 16:23:47 +0200 | [diff] [blame] | 1977 | if (qcc->app_ops && qcc->app_ops->release) |
| 1978 | qcc->app_ops->release(qcc->ctx); |
| 1979 | TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn); |
| 1980 | |
Amaury Denoyelle | c49d5d1 | 2022-07-15 10:32:53 +0200 | [diff] [blame] | 1981 | pool_free(pool_head_qcc, qcc); |
| 1982 | |
| 1983 | if (conn) { |
| 1984 | LIST_DEL_INIT(&conn->stopping_list); |
| 1985 | |
| 1986 | conn->handle.qc->conn = NULL; |
| 1987 | conn->mux = NULL; |
| 1988 | conn->ctx = NULL; |
| 1989 | |
| 1990 | TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn); |
| 1991 | |
| 1992 | conn_stop_tracking(conn); |
| 1993 | conn_full_close(conn); |
| 1994 | if (conn->destroy_cb) |
| 1995 | conn->destroy_cb(conn); |
| 1996 | conn_free(conn); |
| 1997 | } |
| 1998 | |
| 1999 | TRACE_LEAVE(QMUX_EV_QCC_END); |
| 2000 | } |
| 2001 | |
Willy Tarreau | 41e701e | 2022-09-08 15:12:59 +0200 | [diff] [blame] | 2002 | struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2003 | { |
Amaury Denoyelle | 769e9ff | 2021-10-05 11:43:50 +0200 | [diff] [blame] | 2004 | struct qcc *qcc = ctx; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2005 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 2006 | TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2007 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2008 | qc_send(qcc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2009 | |
Amaury Denoyelle | c1a6dfd | 2022-07-08 14:04:21 +0200 | [diff] [blame] | 2010 | if (qc_purge_streams(qcc)) { |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 2011 | if (qcc_is_dead(qcc)) { |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2012 | TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 2013 | goto release; |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 2014 | } |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 2015 | } |
| 2016 | |
Amaury Denoyelle | 37c2e4a | 2022-05-16 13:54:59 +0200 | [diff] [blame] | 2017 | qc_recv(qcc); |
| 2018 | |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2019 | /* TODO check if qcc proxy is disabled. If yes, use graceful shutdown |
| 2020 | * to close the connection. |
| 2021 | */ |
| 2022 | |
| 2023 | qcc_refresh_timeout(qcc); |
| 2024 | |
Amaury Denoyelle | d397385 | 2022-07-25 14:56:54 +0200 | [diff] [blame] | 2025 | end: |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 2026 | TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn); |
| 2027 | return NULL; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2028 | |
Amaury Denoyelle | 3baab74 | 2022-08-11 18:35:55 +0200 | [diff] [blame] | 2029 | release: |
| 2030 | qc_release(qcc); |
| 2031 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2032 | return NULL; |
| 2033 | } |
| 2034 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2035 | static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state) |
| 2036 | { |
| 2037 | struct qcc *qcc = ctx; |
| 2038 | int expired = tick_is_expired(t->expire, now_ms); |
| 2039 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2040 | TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2041 | |
| 2042 | if (qcc) { |
| 2043 | if (!expired) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2044 | TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn); |
| 2045 | goto requeue; |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | if (!qcc_may_expire(qcc)) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2049 | TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2050 | t->expire = TICK_ETERNITY; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2051 | goto requeue; |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2055 | task_destroy(t); |
Amaury Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 2056 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2057 | if (!qcc) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2058 | TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE); |
| 2059 | goto out; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2060 | } |
Amaury Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 2061 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2062 | qcc->task = NULL; |
| 2063 | |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2064 | /* TODO depending on the timeout condition, different shutdown mode |
| 2065 | * should be used. For http keep-alive or disabled proxy, a graceful |
| 2066 | * shutdown should occurs. For all other cases, an immediate close |
| 2067 | * seems legitimate. |
| 2068 | */ |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2069 | if (qcc_is_dead(qcc)) { |
| 2070 | TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2071 | qc_release(qcc); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2072 | } |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2073 | |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2074 | out: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2075 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2076 | return NULL; |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2077 | |
| 2078 | requeue: |
| 2079 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
| 2080 | return t; |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2081 | } |
| 2082 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2083 | static int qc_init(struct connection *conn, struct proxy *prx, |
| 2084 | struct session *sess, struct buffer *input) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2085 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2086 | struct qcc *qcc; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 2087 | struct quic_transport_params *lparams, *rparams; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2088 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2089 | TRACE_ENTER(QMUX_EV_QCC_NEW); |
| 2090 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2091 | qcc = pool_alloc(pool_head_qcc); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2092 | if (!qcc) { |
| 2093 | TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2094 | goto fail_no_qcc; |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2095 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2096 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2097 | qcc->conn = conn; |
| 2098 | conn->ctx = qcc; |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 2099 | qcc->nb_hreq = qcc->nb_sc = 0; |
Amaury Denoyelle | ce1f30d | 2022-02-01 15:14:24 +0100 | [diff] [blame] | 2100 | qcc->flags = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2101 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2102 | qcc->app_ops = NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2103 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2104 | qcc->streams_by_id = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2105 | |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 2106 | /* Server parameters, params used for RX flow control. */ |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 2107 | lparams = &conn->handle.qc->rx.params; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 2108 | |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 2109 | qcc->rx.max_data = lparams->initial_max_data; |
Amaury Denoyelle | b9e0640 | 2022-06-10 15:16:21 +0200 | [diff] [blame] | 2110 | qcc->tx.sent_offsets = qcc->tx.offsets = 0; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 2111 | |
| 2112 | /* Client initiated streams must respect the server flow control. */ |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 2113 | qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2114 | qcc->strms[QCS_CLT_BIDI].nb_streams = 0; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 2115 | qcc->strms[QCS_CLT_BIDI].rx.max_data = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 2116 | 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] | 2117 | |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 2118 | qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2119 | qcc->strms[QCS_CLT_UNI].nb_streams = 0; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 2120 | qcc->strms[QCS_CLT_UNI].rx.max_data = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 2121 | 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] | 2122 | |
| 2123 | /* Server initiated streams must respect the server flow control. */ |
| 2124 | qcc->strms[QCS_SRV_BIDI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2125 | qcc->strms[QCS_SRV_BIDI].nb_streams = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 2126 | 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] | 2127 | qcc->strms[QCS_SRV_BIDI].tx.max_data = 0; |
| 2128 | |
| 2129 | qcc->strms[QCS_SRV_UNI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2130 | qcc->strms[QCS_SRV_UNI].nb_streams = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 2131 | 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] | 2132 | qcc->strms[QCS_SRV_UNI].tx.max_data = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2133 | |
Amaury Denoyelle | c985cb1 | 2022-05-16 14:29:59 +0200 | [diff] [blame] | 2134 | LIST_INIT(&qcc->lfctl.frms); |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 2135 | qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi; |
Amaury Denoyelle | bf3c208 | 2022-08-16 11:29:08 +0200 | [diff] [blame] | 2136 | qcc->lfctl.ms_uni = lparams->initial_max_streams_uni; |
Amaury Denoyelle | 44d0912 | 2022-04-26 11:21:10 +0200 | [diff] [blame] | 2137 | qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local; |
| 2138 | qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote; |
Amaury Denoyelle | 176174f | 2022-10-21 17:02:18 +0200 | [diff] [blame] | 2139 | qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni; |
Amaury Denoyelle | 78396e5 | 2022-03-21 17:13:32 +0100 | [diff] [blame] | 2140 | qcc->lfctl.cl_bidi_r = 0; |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 2141 | |
Amaury Denoyelle | c830e1e | 2022-05-16 16:19:59 +0200 | [diff] [blame] | 2142 | qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data; |
Amaury Denoyelle | d46b0f5 | 2022-05-20 15:05:07 +0200 | [diff] [blame] | 2143 | qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0; |
Amaury Denoyelle | c830e1e | 2022-05-16 16:19:59 +0200 | [diff] [blame] | 2144 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 2145 | rparams = &conn->handle.qc->tx.params; |
Amaury Denoyelle | 05ce55e | 2022-03-08 10:35:42 +0100 | [diff] [blame] | 2146 | qcc->rfctl.md = rparams->initial_max_data; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 2147 | qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local; |
| 2148 | qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote; |
Amaury Denoyelle | 176174f | 2022-10-21 17:02:18 +0200 | [diff] [blame] | 2149 | qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni; |
Amaury Denoyelle | 6ea7819 | 2022-03-07 15:47:02 +0100 | [diff] [blame] | 2150 | |
Amaury Denoyelle | a509ffb | 2022-07-04 15:50:33 +0200 | [diff] [blame] | 2151 | if (conn_is_back(conn)) { |
| 2152 | qcc->next_bidi_l = 0x00; |
| 2153 | qcc->largest_bidi_r = 0x01; |
| 2154 | qcc->next_uni_l = 0x02; |
| 2155 | qcc->largest_uni_r = 0x03; |
| 2156 | } |
| 2157 | else { |
| 2158 | qcc->largest_bidi_r = 0x00; |
| 2159 | qcc->next_bidi_l = 0x01; |
| 2160 | qcc->largest_uni_r = 0x02; |
| 2161 | qcc->next_uni_l = 0x03; |
| 2162 | } |
| 2163 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2164 | qcc->wait_event.tasklet = tasklet_new(); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2165 | if (!qcc->wait_event.tasklet) { |
| 2166 | TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2167 | goto fail_no_tasklet; |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2168 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2169 | |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 2170 | LIST_INIT(&qcc->send_list); |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 2171 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2172 | qcc->wait_event.tasklet->process = qc_io_cb; |
| 2173 | qcc->wait_event.tasklet->context = qcc; |
Frédéric Lécaille | f27b66f | 2022-03-18 22:49:22 +0100 | [diff] [blame] | 2174 | qcc->wait_event.events = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2175 | |
Amaury Denoyelle | 07bf8f4 | 2022-07-22 16:16:03 +0200 | [diff] [blame] | 2176 | qcc->proxy = prx; |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2177 | /* haproxy timeouts */ |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 2178 | qcc->task = NULL; |
Amaury Denoyelle | b630945 | 2022-07-25 14:51:56 +0200 | [diff] [blame] | 2179 | qcc->timeout = conn_is_back(qcc->conn) ? prx->timeout.server : |
| 2180 | prx->timeout.client; |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 2181 | if (tick_isset(qcc->timeout)) { |
| 2182 | qcc->task = task_new_here(); |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2183 | if (!qcc->task) { |
| 2184 | TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW); |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 2185 | goto fail_no_timeout_task; |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2186 | } |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 2187 | qcc->task->process = qc_timeout_task; |
| 2188 | qcc->task->context = qcc; |
| 2189 | qcc->task->expire = tick_add(now_ms, qcc->timeout); |
| 2190 | } |
Amaury Denoyelle | bd6ec1b | 2022-07-25 11:53:18 +0200 | [diff] [blame] | 2191 | qcc_reset_idle_start(qcc); |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 2192 | LIST_INIT(&qcc->opening_list); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2193 | |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2194 | if (!conn_is_back(conn)) { |
| 2195 | if (!LIST_INLIST(&conn->stopping_list)) { |
| 2196 | LIST_APPEND(&mux_stopping_data[tid].list, |
| 2197 | &conn->stopping_list); |
| 2198 | } |
| 2199 | } |
| 2200 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 2201 | HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc); |
Amaury Denoyelle | b4d119f | 2023-01-25 17:44:36 +0100 | [diff] [blame] | 2202 | |
| 2203 | if (qcc_install_app_ops(qcc, conn->handle.qc->app_ops)) { |
| 2204 | TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW, qcc->conn); |
| 2205 | /* prepare a CONNECTION_CLOSE frame */ |
| 2206 | quic_set_connection_close(conn->handle.qc, quic_err_transport(QC_ERR_APPLICATION_ERROR)); |
| 2207 | goto fail_install_app_ops; |
| 2208 | } |
| 2209 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2210 | /* init read cycle */ |
| 2211 | tasklet_wakeup(qcc->wait_event.tasklet); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2212 | |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2213 | TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2214 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2215 | |
Amaury Denoyelle | b4d119f | 2023-01-25 17:44:36 +0100 | [diff] [blame] | 2216 | fail_install_app_ops: |
| 2217 | if (qcc->app_ops && qcc->app_ops->release) |
| 2218 | qcc->app_ops->release(qcc->ctx); |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 2219 | fail_no_timeout_task: |
| 2220 | tasklet_free(qcc->wait_event.tasklet); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2221 | fail_no_tasklet: |
| 2222 | pool_free(pool_head_qcc, qcc); |
| 2223 | fail_no_qcc: |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2224 | TRACE_LEAVE(QMUX_EV_QCC_NEW); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2225 | return -1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2226 | } |
| 2227 | |
Amaury Denoyelle | 2461bd5 | 2022-04-13 16:54:52 +0200 | [diff] [blame] | 2228 | static void qc_destroy(void *ctx) |
| 2229 | { |
| 2230 | struct qcc *qcc = ctx; |
| 2231 | |
| 2232 | TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn); |
| 2233 | qc_release(qcc); |
| 2234 | TRACE_LEAVE(QMUX_EV_QCC_END); |
| 2235 | } |
| 2236 | |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2237 | static void qc_detach(struct sedesc *sd) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2238 | { |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2239 | struct qcs *qcs = sd->se; |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 2240 | struct qcc *qcc = qcs->qcc; |
| 2241 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2242 | TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs); |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 2243 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 2244 | /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach |
| 2245 | * from the stream even if it is not closed remotely at the QUIC layer. |
| 2246 | * This happens for example when a stream must be closed due to a |
| 2247 | * rejected request. To better handle these cases, it will be required |
| 2248 | * to implement shutr/shutw MUX operations. Once this is done, this |
| 2249 | * BUG_ON_HOT() statement can be adjusted. |
| 2250 | */ |
| 2251 | //BUG_ON_HOT(!qcs_is_close_remote(qcs)); |
Amaury Denoyelle | c603de4 | 2022-07-25 11:21:46 +0200 | [diff] [blame] | 2252 | |
| 2253 | qcc_rm_sc(qcc); |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 2254 | |
Amaury Denoyelle | 20d1f84 | 2022-07-11 11:23:17 +0200 | [diff] [blame] | 2255 | if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 2256 | TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 2257 | qcs->flags |= QC_SF_DETACH; |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2258 | qcc_refresh_timeout(qcc); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2259 | |
| 2260 | TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 2261 | return; |
| 2262 | } |
| 2263 | |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 2264 | qcs_destroy(qcs); |
Amaury Denoyelle | 1136e92 | 2022-02-01 10:33:09 +0100 | [diff] [blame] | 2265 | |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 2266 | if (qcc_is_dead(qcc)) { |
Amaury Denoyelle | 047d86a | 2022-08-10 16:42:35 +0200 | [diff] [blame] | 2267 | TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn); |
Amaury Denoyelle | 35a66c0 | 2022-08-12 15:56:21 +0200 | [diff] [blame] | 2268 | goto release; |
Amaury Denoyelle | 06890aa | 2022-04-04 16:15:06 +0200 | [diff] [blame] | 2269 | } |
Amaury Denoyelle | a3daaec | 2022-04-21 16:29:27 +0200 | [diff] [blame] | 2270 | else if (qcc->task) { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2271 | TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn); |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2272 | qcc_refresh_timeout(qcc); |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 2273 | } |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2274 | else { |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2275 | TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn); |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2276 | } |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2277 | |
| 2278 | TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn); |
Amaury Denoyelle | 35a66c0 | 2022-08-12 15:56:21 +0200 | [diff] [blame] | 2279 | return; |
| 2280 | |
| 2281 | release: |
| 2282 | qc_release(qcc); |
| 2283 | TRACE_LEAVE(QMUX_EV_STRM_END); |
| 2284 | return; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2285 | } |
| 2286 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2287 | /* Called from the upper layer, to receive data */ |
Amaury Denoyelle | b7ce79c | 2022-11-24 10:51:19 +0100 | [diff] [blame] | 2288 | static size_t qc_recv_buf(struct stconn *sc, struct buffer *buf, |
| 2289 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2290 | { |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2291 | struct qcs *qcs = __sc_mux_strm(sc); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2292 | size_t ret = 0; |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 2293 | char fin = 0; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2294 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2295 | 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] | 2296 | |
Amaury Denoyelle | d80fbca | 2022-09-19 17:02:28 +0200 | [diff] [blame] | 2297 | ret = qcs_http_rcv_buf(qcs, buf, count, &fin); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2298 | |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2299 | if (b_data(&qcs->rx.app_buf)) { |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2300 | se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2301 | } |
| 2302 | else { |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2303 | se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM); |
| 2304 | if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING)) |
| 2305 | se_fl_set(qcs->sd, SE_FL_ERROR); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2306 | |
Amaury Denoyelle | 72a78e8 | 2022-07-29 15:34:12 +0200 | [diff] [blame] | 2307 | /* Set end-of-input if FIN received and all data extracted. */ |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 2308 | if (fin) |
Willy Tarreau | d7b7e0d | 2022-05-27 16:09:35 +0200 | [diff] [blame] | 2309 | se_fl_set(qcs->sd, SE_FL_EOI); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2310 | |
| 2311 | if (b_size(&qcs->rx.app_buf)) { |
| 2312 | b_free(&qcs->rx.app_buf); |
| 2313 | offer_buffers(NULL, 1); |
| 2314 | } |
| 2315 | } |
| 2316 | |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 2317 | if (ret) { |
| 2318 | qcs->flags &= ~QC_SF_DEM_FULL; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2319 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 2320 | } |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2321 | |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2322 | TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs); |
| 2323 | |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 2324 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2325 | } |
| 2326 | |
Amaury Denoyelle | b7ce79c | 2022-11-24 10:51:19 +0100 | [diff] [blame] | 2327 | static size_t qc_send_buf(struct stconn *sc, struct buffer *buf, |
| 2328 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2329 | { |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2330 | struct qcs *qcs = __sc_mux_strm(sc); |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2331 | size_t ret; |
Amaury Denoyelle | 9534e59 | 2022-09-19 17:14:27 +0200 | [diff] [blame] | 2332 | char fin; |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2333 | |
| 2334 | 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] | 2335 | |
Amaury Denoyelle | 3dc4e5a | 2022-09-13 16:49:21 +0200 | [diff] [blame] | 2336 | /* stream layer has been detached so no transfer must occur after. */ |
| 2337 | BUG_ON_HOT(qcs->flags & QC_SF_DETACH); |
| 2338 | |
Amaury Denoyelle | 843a119 | 2022-07-04 11:44:38 +0200 | [diff] [blame] | 2339 | if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) { |
Amaury Denoyelle | 0ed617a | 2022-09-20 14:46:40 +0200 | [diff] [blame] | 2340 | ret = qcs_http_reset_buf(qcs, buf, count); |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 2341 | goto end; |
| 2342 | } |
| 2343 | |
Amaury Denoyelle | 9534e59 | 2022-09-19 17:14:27 +0200 | [diff] [blame] | 2344 | ret = qcs_http_snd_buf(qcs, buf, count, &fin); |
| 2345 | if (fin) |
| 2346 | qcs->flags |= QC_SF_FIN_STREAM; |
| 2347 | |
Amaury Denoyelle | ab6cdec | 2023-01-10 10:41:41 +0100 | [diff] [blame] | 2348 | if (ret || fin) { |
Amaury Denoyelle | f9b0326 | 2023-01-09 10:34:25 +0100 | [diff] [blame] | 2349 | qcc_send_stream(qcs, 0); |
Amaury Denoyelle | 9534e59 | 2022-09-19 17:14:27 +0200 | [diff] [blame] | 2350 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 2351 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 2352 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2353 | |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 2354 | end: |
Amaury Denoyelle | 4f13757 | 2022-03-24 17:10:00 +0100 | [diff] [blame] | 2355 | TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs); |
| 2356 | |
| 2357 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 2361 | * event subscriber <es> is not allowed to change from a previous call as long |
| 2362 | * as at least one event is still subscribed. The <event_type> must only be a |
| 2363 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 2364 | */ |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2365 | static int qc_subscribe(struct stconn *sc, int event_type, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2366 | struct wait_event *es) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2367 | { |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2368 | return qcs_subscribe(__sc_mux_strm(sc), event_type, es); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 2372 | * The <es> pointer is not allowed to differ from the one passed to the |
| 2373 | * subscribe() call. It always returns zero. |
| 2374 | */ |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2375 | static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2376 | { |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 2377 | struct qcs *qcs = __sc_mux_strm(sc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2378 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2379 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 2380 | BUG_ON(qcs->subs && qcs->subs != es); |
| 2381 | |
| 2382 | es->events &= ~event_type; |
| 2383 | if (!es->events) |
| 2384 | qcs->subs = NULL; |
| 2385 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2386 | return 0; |
| 2387 | } |
| 2388 | |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2389 | /* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection, |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2390 | * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and |
| 2391 | * wake them. |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2392 | */ |
| 2393 | static int qc_wake_some_streams(struct qcc *qcc) |
| 2394 | { |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2395 | struct qcs *qcs; |
| 2396 | struct eb64_node *node; |
| 2397 | |
| 2398 | for (node = eb64_first(&qcc->streams_by_id); node; |
| 2399 | node = eb64_next(node)) { |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 2400 | qcs = eb64_entry(node, struct qcs, by_id); |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2401 | |
Amaury Denoyelle | 3abeb57 | 2022-07-04 11:42:27 +0200 | [diff] [blame] | 2402 | if (!qcs_sc(qcs)) |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2403 | continue; |
| 2404 | |
| 2405 | if (qcc->conn->flags & CO_FL_ERROR) { |
Willy Tarreau | 35c4dd0 | 2023-01-17 16:25:29 +0100 | [diff] [blame] | 2406 | se_fl_set_error(qcs->sd); |
Amaury Denoyelle | 4561f84 | 2022-07-06 14:54:34 +0200 | [diff] [blame] | 2407 | qcs_alert(qcs); |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | return 0; |
| 2412 | } |
| 2413 | |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2414 | static int qc_wake(struct connection *conn) |
| 2415 | { |
| 2416 | struct qcc *qcc = conn->ctx; |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 2417 | struct proxy *prx = conn->handle.qc->li->bind_conf->frontend; |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2418 | |
| 2419 | TRACE_ENTER(QMUX_EV_QCC_WAKE, conn); |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2420 | |
| 2421 | /* Check if a soft-stop is in progress. |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2422 | * |
cui fliter | a94bedc | 2022-08-29 14:42:57 +0800 | [diff] [blame] | 2423 | * TODO this is relevant for frontend connections only. |
Amaury Denoyelle | d0c6232 | 2022-05-23 08:52:58 +0200 | [diff] [blame] | 2424 | * |
| 2425 | * TODO Client should be notified with a H3 GOAWAY and then a |
| 2426 | * CONNECTION_CLOSE. However, quic-conn uses the listener socket for |
| 2427 | * sending which at this stage is already closed. |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2428 | */ |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2429 | if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) |
Amaury Denoyelle | d0c6232 | 2022-05-23 08:52:58 +0200 | [diff] [blame] | 2430 | 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] | 2431 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 2432 | if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
Amaury Denoyelle | b515b0a | 2022-04-06 10:28:43 +0200 | [diff] [blame] | 2433 | qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH); |
| 2434 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2435 | qc_send(qcc); |
| 2436 | |
Amaury Denoyelle | fe035ec | 2022-04-06 15:46:30 +0200 | [diff] [blame] | 2437 | qc_wake_some_streams(qcc); |
| 2438 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2439 | if (qcc_is_dead(qcc)) |
| 2440 | goto release; |
| 2441 | |
Amaury Denoyelle | 5fc05d1 | 2022-07-25 14:58:48 +0200 | [diff] [blame] | 2442 | qcc_refresh_timeout(qcc); |
| 2443 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2444 | TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn); |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2445 | return 0; |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2446 | |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2447 | release: |
Amaury Denoyelle | 4c9a164 | 2022-08-10 16:58:01 +0200 | [diff] [blame] | 2448 | TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn); |
Amaury Denoyelle | d97fc80 | 2022-04-06 16:13:09 +0200 | [diff] [blame] | 2449 | qc_release(qcc); |
Amaury Denoyelle | f0b67f9 | 2022-08-10 16:14:32 +0200 | [diff] [blame] | 2450 | TRACE_LEAVE(QMUX_EV_QCC_WAKE); |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2451 | return 1; |
| 2452 | } |
| 2453 | |
Amaury Denoyelle | a473f19 | 2022-12-21 10:21:58 +0100 | [diff] [blame] | 2454 | static void qc_shutw(struct stconn *sc, enum co_shw_mode mode) |
| 2455 | { |
| 2456 | struct qcs *qcs = __sc_mux_strm(sc); |
| 2457 | |
| 2458 | TRACE_ENTER(QMUX_EV_STRM_SHUT, qcs->qcc->conn, qcs); |
| 2459 | |
| 2460 | /* If QC_SF_FIN_STREAM is not set and stream is not closed locally, it |
| 2461 | * means that upper layer reported an early closure. A RESET_STREAM is |
| 2462 | * necessary if not already scheduled. |
| 2463 | */ |
| 2464 | |
| 2465 | if (!qcs_is_close_local(qcs) && |
| 2466 | !(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET))) { |
| 2467 | qcc_reset_stream(qcs, 0); |
| 2468 | se_fl_set_error(qcs->sd); |
| 2469 | } |
| 2470 | |
| 2471 | TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcs->qcc->conn, qcs); |
| 2472 | } |
Amaury Denoyelle | 38e6006 | 2022-07-01 16:48:42 +0200 | [diff] [blame] | 2473 | |
Willy Tarreau | b4a4fee | 2022-09-02 16:00:40 +0200 | [diff] [blame] | 2474 | /* for debugging with CLI's "show sess" command. May emit multiple lines, each |
| 2475 | * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single |
| 2476 | * line is used. Each field starts with a space so it's safe to print it after |
| 2477 | * existing fields. |
| 2478 | */ |
| 2479 | static int qc_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx) |
| 2480 | { |
| 2481 | struct qcs *qcs = sd->se; |
| 2482 | struct qcc *qcc; |
| 2483 | int ret = 0; |
| 2484 | |
| 2485 | if (!qcs) |
| 2486 | return ret; |
| 2487 | |
| 2488 | chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx", |
| 2489 | qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err); |
| 2490 | |
| 2491 | if (pfx) |
| 2492 | chunk_appendf(msg, "\n%s", pfx); |
| 2493 | |
| 2494 | qcc = qcs->qcc; |
| 2495 | chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p", |
| 2496 | qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task); |
| 2497 | return ret; |
| 2498 | } |
| 2499 | |
| 2500 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2501 | static const struct mux_ops qc_ops = { |
| 2502 | .init = qc_init, |
Amaury Denoyelle | 2461bd5 | 2022-04-13 16:54:52 +0200 | [diff] [blame] | 2503 | .destroy = qc_destroy, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2504 | .detach = qc_detach, |
Amaury Denoyelle | b7ce79c | 2022-11-24 10:51:19 +0100 | [diff] [blame] | 2505 | .rcv_buf = qc_recv_buf, |
| 2506 | .snd_buf = qc_send_buf, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2507 | .subscribe = qc_subscribe, |
| 2508 | .unsubscribe = qc_unsubscribe, |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 2509 | .wake = qc_wake, |
Amaury Denoyelle | a473f19 | 2022-12-21 10:21:58 +0100 | [diff] [blame] | 2510 | .shutw = qc_shutw, |
Willy Tarreau | b4a4fee | 2022-09-02 16:00:40 +0200 | [diff] [blame] | 2511 | .show_sd = qc_show_sd, |
Willy Tarreau | b5821e1 | 2022-04-26 11:54:08 +0200 | [diff] [blame] | 2512 | .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED, |
Willy Tarreau | 671bd5a | 2022-04-11 09:29:21 +0200 | [diff] [blame] | 2513 | .name = "QUIC", |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2514 | }; |
| 2515 | |
| 2516 | static struct mux_proto_list mux_proto_quic = |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 2517 | { .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] | 2518 | |
| 2519 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic); |