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