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