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