blob: b071cca8f421c75a7e509fb62905c789ce46dc8f [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01007#include <haproxy/dynbuf.h>
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01008#include <haproxy/h3.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02009#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020010#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010011#include <haproxy/pool.h>
Frédéric Lécaille9969adb2023-01-18 11:52:21 +010012#include <haproxy/proxy.h>
Amaury Denoyelled80fbca2022-09-19 17:02:28 +020013#include <haproxy/qmux_http.h>
Amaury Denoyelle36d50bf2022-09-19 16:12:38 +020014#include <haproxy/qmux_trace.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020015#include <haproxy/quic_conn.h>
Amaury Denoyelle40c24f12023-01-27 17:47:49 +010016#include <haproxy/quic_frame.h>
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +010017#include <haproxy/quic_sock.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020018#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020019#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020020#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020021#include <haproxy/stconn.h>
Amaury Denoyelle1a2faef2023-05-15 15:17:28 +020022#include <haproxy/time.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010023#include <haproxy/trace.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010024
Amaury Denoyelledeed7772021-12-03 11:36:46 +010025DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010026DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
27
Amaury Denoyelle4b167002022-12-12 09:59:50 +010028static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
29{
30 struct buffer buf;
31
32 if (ncb_is_null(ncbuf))
33 return;
34
35 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
36 b_free(&buf);
37 offer_buffers(NULL, 1);
38
39 *ncbuf = NCBUF_NULL;
40}
41
42/* Free <qcs> instance. This function is reserved for internal usage : it must
43 * only be called on qcs alloc error or on connection shutdown. Else
Ilya Shipitsin07be66d2023-04-01 12:26:42 +020044 * qcs_destroy must be preferred to handle QUIC flow-control increase.
Amaury Denoyelle4b167002022-12-12 09:59:50 +010045 */
46static void qcs_free(struct qcs *qcs)
47{
48 struct qcc *qcc = qcs->qcc;
49
50 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
51
Amaury Denoyelle15337fd2022-12-20 14:47:16 +010052 /* Safe to use even if already removed from the list. */
53 LIST_DEL_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +010054 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelle4b167002022-12-12 09:59:50 +010055
56 /* Release stream endpoint descriptor. */
57 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
58 sedesc_free(qcs->sd);
59
60 /* Release app-layer context. */
61 if (qcs->ctx && qcc->app_ops->detach)
62 qcc->app_ops->detach(qcs);
63
64 /* Release qc_stream_desc buffer from quic-conn layer. */
65 qc_stream_desc_release(qcs->stream);
66
67 /* Free Rx/Tx buffers. */
68 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
69 b_free(&qcs->tx.buf);
70
Amaury Denoyelle4b167002022-12-12 09:59:50 +010071 /* Remove qcs from qcc tree. */
72 eb64_delete(&qcs->by_id);
73
74 pool_free(pool_head_qcs, qcs);
75
76 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
77}
78
Amaury Denoyelledeed7772021-12-03 11:36:46 +010079/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +020080static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010081{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010082 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010083
Amaury Denoyelle4f137572022-03-24 17:10:00 +010084 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
85
Amaury Denoyelledeed7772021-12-03 11:36:46 +010086 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020087 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +020088 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +020089 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020090 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +020091
92 qcs->stream = NULL;
93 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +020094 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +020095 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +020096 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +020097 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010098
Amaury Denoyelle30e260e2022-08-03 11:17:57 +020099 /* App callback attach may register the stream for http-request wait.
100 * These fields must be initialed before.
101 */
102 LIST_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100103 LIST_INIT(&qcs->el_send);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200104 qcs->start = TICK_ETERNITY;
105
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100106 /* store transport layer stream descriptor in qcc tree */
107 qcs->id = qcs->by_id.key = id;
108 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
109
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200110 /* Allocate transport layer stream descriptor. Only needed for TX. */
111 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
112 struct quic_conn *qc = qcc->conn->handle.qc;
113 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200114 if (!qcs->stream) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200115 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200116 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200117 }
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200118 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200119
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100120 /* If stream is local, use peer remote-limit, or else the opposite. */
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200121 if (quic_stream_is_bidi(id)) {
122 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
123 qcc->rfctl.msd_bidi_l;
124 }
125 else if (quic_stream_is_local(qcc, id)) {
126 qcs->tx.msd = qcc->rfctl.msd_uni_l;
127 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100128
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200129 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100130 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200131 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100132
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200133 if (quic_stream_is_bidi(id)) {
134 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
135 qcc->lfctl.msd_bidi_r;
136 }
137 else if (quic_stream_is_remote(qcc, id)) {
138 qcs->rx.msd = qcc->lfctl.msd_uni_r;
139 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200140 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200141
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100142 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100143 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100144 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100145
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100146 qcs->wait_event.tasklet = NULL;
147 qcs->wait_event.events = 0;
148 qcs->subs = NULL;
149
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200150 qcs->err = 0;
151
Amaury Denoyelle3d550842023-01-24 17:42:21 +0100152 if (qcc->app_ops->attach && qcc->app_ops->attach(qcs, qcc->ctx)) {
153 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
154 goto err;
155 }
156
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100157 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100158 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100159 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200160
161 err:
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100162 qcs_free(qcs);
163 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200164 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100165}
166
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200167static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
168{
169 return qcs->sd ? qcs->sd->sc : NULL;
170}
171
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200172/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
173static forceinline void qcc_reset_idle_start(struct qcc *qcc)
174{
175 qcc->idle_start = now_ms;
176}
177
Amaury Denoyellec603de42022-07-25 11:21:46 +0200178/* Decrement <qcc> sc. */
179static forceinline void qcc_rm_sc(struct qcc *qcc)
180{
Amaury Denoyelle8d6d2462023-05-11 16:55:30 +0200181 BUG_ON(!qcc->nb_sc); /* Ensure sc count is always valid (ie >=0). */
Amaury Denoyellec603de42022-07-25 11:21:46 +0200182 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200183
184 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
185 * refreshed after this on stream detach.
186 */
187 if (!qcc->nb_sc && !qcc->nb_hreq)
188 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200189}
190
191/* Decrement <qcc> hreq. */
192static forceinline void qcc_rm_hreq(struct qcc *qcc)
193{
Amaury Denoyelle8d6d2462023-05-11 16:55:30 +0200194 BUG_ON(!qcc->nb_hreq); /* Ensure http req count is always valid (ie >=0). */
Amaury Denoyellec603de42022-07-25 11:21:46 +0200195 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200196
197 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
198 * refreshed after this on I/O handler.
199 */
200 if (!qcc->nb_sc && !qcc->nb_hreq)
201 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200202}
203
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200204static inline int qcc_is_dead(const struct qcc *qcc)
205{
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200206 /* Maintain connection if stream endpoints are still active. */
207 if (qcc->nb_sc)
208 return 0;
209
210 /* Connection considered dead if either :
211 * - remote error detected at tranport level
212 * - error detected locally
213 * - MUX timeout expired or unset
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200214 */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +0200215 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL_DONE) ||
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200216 !qcc->task) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200217 return 1;
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200218 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200219
220 return 0;
221}
222
223/* Return true if the mux timeout should be armed. */
224static inline int qcc_may_expire(struct qcc *qcc)
225{
226 return !qcc->nb_sc;
227}
228
229/* Refresh the timeout on <qcc> if needed depending on its state. */
230static void qcc_refresh_timeout(struct qcc *qcc)
231{
232 const struct proxy *px = qcc->proxy;
233
234 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
235
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200236 if (!qcc->task) {
237 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200238 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200239 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200240
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200241 /* Check if upper layer is responsible of timeout management. */
242 if (!qcc_may_expire(qcc)) {
243 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
244 qcc->task->expire = TICK_ETERNITY;
245 task_queue(qcc->task);
246 goto leave;
247 }
248
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200249 /* Frontend timeout management
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100250 * - shutdown done -> timeout client-fin
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200251 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200252 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200253 * - idle after stream processing -> timeout http-keep-alive
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +0100254 *
255 * If proxy stop-stop in progress, immediate or spread close will be
256 * processed if shutdown already one or connection is idle.
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200257 */
258 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100259 if (qcc->nb_hreq && !(qcc->flags & QC_CF_APP_SHUT)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200260 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
261 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200262 task_queue(qcc->task);
263 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200264 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200265
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100266 if ((!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) &&
267 !(qcc->flags & QC_CF_APP_SHUT)) {
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200268 int timeout = px->timeout.httpreq;
269 struct qcs *qcs = NULL;
270 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200271
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200272 /* Use start time of first stream waiting on HTTP or
273 * qcc idle if no stream not yet used.
274 */
275 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
276 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
277 base_time = qcs ? qcs->start : qcc->idle_start;
278
279 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
280 qcc->task->expire = tick_add_ifset(base_time, timeout);
281 }
282 else {
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100283 if (qcc->flags & QC_CF_APP_SHUT) {
284 TRACE_DEVEL("connection in closing", QMUX_EV_QCC_WAKE, qcc->conn);
285 qcc->task->expire = tick_add_ifset(now_ms,
286 qcc->shut_timeout);
287 }
288 else {
289 /* Use http-request timeout if keep-alive timeout not set */
290 int timeout = tick_isset(px->timeout.httpka) ?
291 px->timeout.httpka : px->timeout.httpreq;
292 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
293 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
294 }
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +0100295
296 /* If proxy soft-stop in progress and connection is
297 * inactive, close the connection immediately. If a
298 * close-spread-time is configured, randomly spread the
299 * timer over a closing window.
300 */
301 if ((qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
302 !(global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)) {
303
304 /* Wake timeout task immediately if window already expired. */
305 int remaining_window = tick_isset(global.close_spread_end) ?
306 tick_remain(now_ms, global.close_spread_end) : 0;
307
308 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
309 if (remaining_window) {
310 /* We don't need to reset the expire if it would
311 * already happen before the close window end.
312 */
313 if (!tick_isset(qcc->task->expire) ||
314 tick_is_le(global.close_spread_end, qcc->task->expire)) {
315 /* Set an expire value shorter than the current value
316 * because the close spread window end comes earlier.
317 */
318 qcc->task->expire = tick_add(now_ms,
319 statistical_prng_range(remaining_window));
320 }
321 }
322 else {
323 /* We are past the soft close window end, wake the timeout
324 * task up immediately.
325 */
326 qcc->task->expire = now_ms;
327 task_wakeup(qcc->task, TASK_WOKEN_TIMER);
328 }
329 }
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200330 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200331 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200332
333 /* fallback to default timeout if frontend specific undefined or for
334 * backend connections.
335 */
336 if (!tick_isset(qcc->task->expire)) {
337 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
338 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200339 }
340
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200341 task_queue(qcc->task);
342
343 leave:
344 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
345}
346
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200347/* Mark a stream as open if it was idle. This can be used on every
348 * successful emission/reception operation to update the stream state.
349 */
350static void qcs_idle_open(struct qcs *qcs)
351{
352 /* This operation must not be used if the stream is already closed. */
353 BUG_ON_HOT(qcs->st == QC_SS_CLO);
354
355 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200356 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200357 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200358 }
359}
360
361/* Close the local channel of <qcs> instance. */
362static void qcs_close_local(struct qcs *qcs)
363{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200364 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
365
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200366 /* The stream must have already been opened. */
367 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
368
369 /* This operation cannot be used multiple times. */
370 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
371
372 if (quic_stream_is_bidi(qcs->id)) {
373 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyelleafb7b9d2022-09-19 11:58:24 +0200374
375 if (qcs->flags & QC_SF_HREQ_RECV)
376 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200377 }
378 else {
379 /* Only local uni streams are valid for this operation. */
380 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
381 qcs->st = QC_SS_CLO;
382 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200383}
384
385/* Close the remote channel of <qcs> instance. */
386static void qcs_close_remote(struct qcs *qcs)
387{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200388 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
389
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200390 /* The stream must have already been opened. */
391 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
392
393 /* This operation cannot be used multiple times. */
394 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
395
396 if (quic_stream_is_bidi(qcs->id)) {
397 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
398 }
399 else {
400 /* Only remote uni streams are valid for this operation. */
401 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
402 qcs->st = QC_SS_CLO;
403 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200404}
405
406static int qcs_is_close_local(struct qcs *qcs)
407{
408 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
409}
410
Amaury Denoyelle6eb3c4b2022-12-09 16:26:03 +0100411static int qcs_is_close_remote(struct qcs *qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200412{
413 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
414}
415
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +0200416/* Allocate if needed buffer <bptr> for stream <qcs>.
417 *
418 * Returns the buffer instance or NULL on allocation failure.
419 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100420struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100421{
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +0200422 return b_alloc(bptr);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100423}
424
Amaury Denoyelled00b3092023-05-11 17:00:54 +0200425/* Allocate if needed buffer <ncbuf> for stream <qcs>.
426 *
427 * Returns the buffer instance or NULL on allocation failure.
428 */
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200429static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200430{
431 struct buffer buf = BUF_NULL;
432
433 if (ncb_is_null(ncbuf)) {
Amaury Denoyelled00b3092023-05-11 17:00:54 +0200434 if (!b_alloc(&buf))
435 return NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200436
437 *ncbuf = ncb_make(buf.area, buf.size, 0);
438 ncb_init(ncbuf, 0);
439 }
440
441 return ncbuf;
442}
443
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500444/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200445 * initialized.
446 */
447static void qcs_alert(struct qcs *qcs)
448{
449 if (qcs->subs) {
450 qcs_notify_recv(qcs);
451 qcs_notify_send(qcs);
452 }
453 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
Amaury Denoyelle2d5c3f52023-05-11 13:41:41 +0200454 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200455 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
456 }
457}
458
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100459int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
460{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100461 struct qcc *qcc = qcs->qcc;
462
463 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100464
465 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
466 BUG_ON(qcs->subs && qcs->subs != es);
467
468 es->events |= event_type;
469 qcs->subs = es;
470
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100471 if (event_type & SUB_RETRY_RECV)
472 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
473
474 if (event_type & SUB_RETRY_SEND)
475 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
476
477 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
478
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100479 return 0;
480}
481
482void qcs_notify_recv(struct qcs *qcs)
483{
484 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
Amaury Denoyelle2d5c3f52023-05-11 13:41:41 +0200485 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100486 tasklet_wakeup(qcs->subs->tasklet);
487 qcs->subs->events &= ~SUB_RETRY_RECV;
488 if (!qcs->subs->events)
489 qcs->subs = NULL;
490 }
491}
492
493void qcs_notify_send(struct qcs *qcs)
494{
495 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle2d5c3f52023-05-11 13:41:41 +0200496 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100497 tasklet_wakeup(qcs->subs->tasklet);
498 qcs->subs->events &= ~SUB_RETRY_SEND;
499 if (!qcs->subs->events)
500 qcs->subs = NULL;
501 }
502}
503
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200504/* A fatal error is detected locally for <qcc> connection. It should be closed
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200505 * with a CONNECTION_CLOSE using <err> code. Set <app> to true to indicate that
506 * the code must be considered as an application level error. This function
507 * must not be called more than once by connection.
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200508 */
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200509void qcc_set_error(struct qcc *qcc, int err, int app)
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200510{
511 /* This must not be called multiple times per connection. */
512 BUG_ON(qcc->flags & QC_CF_ERRL);
513
514 TRACE_STATE("connection on error", QMUX_EV_QCC_ERR, qcc->conn);
515
516 qcc->flags |= QC_CF_ERRL;
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200517 qcc->err = app ? quic_err_app(err) : quic_err_transport(err);
Amaury Denoyelleda24bcf2023-05-09 18:20:45 +0200518
519 /* TODO
520 * Ensure qc_send() will be conducted to convert QC_CF_ERRL in
521 * QC_CF_ERRL_DONE with CONNECTION_CLOSE frame emission. This may be
522 * unnecessary if we are currently in the MUX tasklet context, but it
523 * is too tedious too not forget a wakeup outside of this function for
524 * the moment.
525 */
526 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200527}
528
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200529/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
530 * bidirectional stream, else an unidirectional stream is opened. The next
531 * available ID on the connection will be used according to the stream type.
532 *
533 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100534 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200535struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100536{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200537 struct qcs *qcs;
538 enum qcs_type type;
539 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100540
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200541 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
542
543 if (bidi) {
544 next = &qcc->next_bidi_l;
545 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100546 }
547 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200548 next = &qcc->next_uni_l;
549 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
550 }
551
552 /* TODO ensure that we won't overflow remote peer flow control limit on
553 * streams. Else, we should emit a STREAMS_BLOCKED frame.
554 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100555
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200556 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200557 if (!qcs) {
558 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200559 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200560 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200561 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100562
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200563 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200564 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100565
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200566 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200567 return qcs;
568}
569
570/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
571 * caller is responsible to ensure that a stream with the same ID was not
572 * already opened. This function will also create all intermediaries streams
573 * with ID smaller than <id> not already opened before.
574 *
575 * Returns the allocated stream instance or NULL on error.
576 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200577static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200578{
579 struct qcs *qcs = NULL;
580 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200581 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100582
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200583 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200584
Amaury Denoyelle8d6d2462023-05-11 16:55:30 +0200585 /* Function reserved to remote stream IDs. */
586 BUG_ON(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100587
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200588 if (quic_stream_is_bidi(id)) {
589 largest = &qcc->largest_bidi_r;
590 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
591 }
592 else {
593 largest = &qcc->largest_uni_r;
594 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
595 }
596
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200597 /* RFC 9000 4.6. Controlling Concurrency
598 *
599 * An endpoint that receives a frame with a stream ID exceeding the
600 * limit it has sent MUST treat this as a connection error of type
601 * STREAM_LIMIT_ERROR
602 */
603 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
604 qcc->lfctl.ms_uni * 4;
605 if (id >= max_id) {
606 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200607 qcc_set_error(qcc, QC_ERR_STREAM_LIMIT_ERROR, 0);
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200608 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200609 }
610
611 /* Only stream ID not already opened can be used. */
612 BUG_ON(id < *largest);
613
614 while (id >= *largest) {
Amaury Denoyellefd79ddb2022-08-16 11:13:45 +0200615 const char *str = *largest < id ? "initializing intermediary remote stream" :
616 "initializing remote stream";
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200617
618 qcs = qcs_new(qcc, *largest, type);
619 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200620 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200621 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200622 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100623 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200624
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200625 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200626 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100627 }
628
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200629 out:
630 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200631 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200632
633 err:
634 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
635 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200636}
637
Amaury Denoyellebf86d892023-05-12 18:16:31 +0200638struct stconn *qc_attach_sc(struct qcs *qcs, struct buffer *buf, char fin)
Amaury Denoyelle1a2faef2023-05-15 15:17:28 +0200639{
640 struct qcc *qcc = qcs->qcc;
641 struct session *sess = qcc->conn->owner;
642
643 qcs->sd = sedesc_new();
644 if (!qcs->sd)
645 return NULL;
646
647 qcs->sd->se = qcs;
648 qcs->sd->conn = qcc->conn;
649 se_fl_set(qcs->sd, SE_FL_T_MUX | SE_FL_ORPHAN | SE_FL_NOT_FIRST);
650 se_expect_no_data(qcs->sd);
651
652 /* TODO duplicated from mux_h2 */
653 sess->t_idle = ns_to_ms(now_ns - sess->accept_ts) - sess->t_handshake;
654
655 if (!sc_new_from_endp(qcs->sd, sess, buf))
656 return NULL;
657
658 /* QC_SF_HREQ_RECV must be set once for a stream. Else, nb_hreq counter
659 * will be incorrect for the connection.
660 */
661 BUG_ON_HOT(qcs->flags & QC_SF_HREQ_RECV);
662 qcs->flags |= QC_SF_HREQ_RECV;
663 ++qcc->nb_sc;
664 ++qcc->nb_hreq;
665
666 /* TODO duplicated from mux_h2 */
667 sess->accept_date = date;
668 sess->accept_ts = now_ns;
669 sess->t_handshake = 0;
670 sess->t_idle = 0;
671
672 /* A stream must have been registered for HTTP wait before attaching
673 * it to sedesc. See <qcs_wait_http_req> for more info.
674 */
675 BUG_ON_HOT(!LIST_INLIST(&qcs->el_opening));
676 LIST_DEL_INIT(&qcs->el_opening);
677
Amaury Denoyellebf86d892023-05-12 18:16:31 +0200678 if (fin) {
679 TRACE_STATE("report end-of-input", QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyelle8de35922023-05-24 10:48:52 +0200680 se_fl_set(qcs->sd, SE_FL_EOI|SE_FL_EOS);
Amaury Denoyellebf86d892023-05-12 18:16:31 +0200681 }
682
Amaury Denoyelle1a2faef2023-05-15 15:17:28 +0200683 return qcs->sd->sc;
684}
685
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200686/* Use this function for a stream <id> which is not in <qcc> stream tree. It
687 * returns true if the associated stream is closed.
688 */
689static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
690{
691 uint64_t *largest;
692
693 /* This function must only be used for stream not present in the stream tree. */
694 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
695
696 if (quic_stream_is_local(qcc, id)) {
697 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
698 &qcc->next_bidi_l;
699 }
700 else {
701 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
702 &qcc->largest_bidi_r;
703 }
704
705 return id < *largest;
706}
707
708/* Retrieve the stream instance from <id> ID. This can be used when receiving
709 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200710 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200711 * of streams are not allowed. If the stream instance is found, it is stored in
712 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200713 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200714 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
715 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
716 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200717 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200718int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
719 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200720{
721 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200722
723 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200724 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200725
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200726 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200727 TRACE_ERROR("receive-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200728 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200729 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200730 }
731
732 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200733 TRACE_ERROR("send-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200734 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200735 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200736 }
737
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200738 /* Search the stream in the connection tree. */
739 node = eb64_lookup(&qcc->streams_by_id, id);
740 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200741 *out = eb64_entry(node, struct qcs, by_id);
742 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200743 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200744 }
745
746 /* Check if stream is already closed. */
747 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200748 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200749 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200750 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200751 }
752
753 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500754 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200755 * application protocol layer.
756 */
757 if (quic_stream_is_local(qcc, id)) {
758 /* RFC 9000 19.8. STREAM Frames
759 *
760 * An endpoint MUST terminate the connection with error
761 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
762 * initiated stream that has not yet been created, or for a send-only
763 * stream.
764 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200765 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 Denoyelle58721f22023-05-09 18:01:09 +0200766 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200767 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200768 }
769 else {
770 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200771 *out = qcc_init_stream_remote(qcc, id);
772 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200773 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200774 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200775 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200776 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100777
778 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200779 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200780 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200781
782 err:
783 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
784 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100785}
786
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200787/* Simple function to duplicate a buffer */
788static inline struct buffer qcs_b_dup(const struct ncbuf *b)
789{
790 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
791}
792
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200793/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
794 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200795 */
796static void qcs_consume(struct qcs *qcs, uint64_t bytes)
797{
798 struct qcc *qcc = qcs->qcc;
799 struct quic_frame *frm;
800 struct ncbuf *buf = &qcs->rx.ncbuf;
801 enum ncb_ret ret;
802
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200803 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
804
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200805 ret = ncb_advance(buf, bytes);
806 if (ret) {
807 ABORT_NOW(); /* should not happens because removal only in data */
808 }
809
810 if (ncb_is_empty(buf))
811 qc_free_ncbuf(qcs, buf);
812
813 qcs->rx.offset += bytes;
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100814 /* Not necessary to emit a MAX_STREAM_DATA if all data received. */
815 if (qcs->flags & QC_SF_SIZE_KNOWN)
816 goto conn_fctl;
817
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200818 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200819 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100820 frm = qc_frm_alloc(QUIC_FT_MAX_STREAM_DATA);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100821 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200822 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100823 return;
824 }
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200825
826 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
827
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200828 frm->max_stream_data.id = qcs->id;
829 frm->max_stream_data.max_stream_data = qcs->rx.msd;
830
831 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
832 tasklet_wakeup(qcc->wait_event.tasklet);
833 }
834
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100835 conn_fctl:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200836 qcc->lfctl.offsets_consume += bytes;
837 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200838 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100839 frm = qc_frm_alloc(QUIC_FT_MAX_DATA);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100840 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200841 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100842 return;
843 }
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200844
845 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
846
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200847 frm->max_data.max_data = qcc->lfctl.md;
848
849 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
850 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
851 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200852
853 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200854}
855
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200856/* Decode the content of STREAM frames already received on the stream instance
857 * <qcs>.
858 *
859 * Returns 0 on success else non-zero.
860 */
861static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
862{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200863 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200864 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200865 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200866
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200867 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
868
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200869 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200870
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200871 /* Signal FIN to application if STREAM FIN received with all data. */
872 if (qcs_is_close_remote(qcs))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200873 fin = 1;
874
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100875 if (!(qcs->flags & QC_SF_READ_ABORTED)) {
876 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
877 if (ret < 0) {
878 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
879 goto err;
880 }
Amaury Denoyelle152beee2023-05-24 14:43:43 +0200881
882 if (qcs->flags & QC_SF_TO_RESET) {
883 if (qcs_sc(qcs) && !se_fl_test(qcs->sd, SE_FL_ERROR|SE_FL_ERR_PENDING)) {
884 se_fl_set_error(qcs->sd);
885 qcs_alert(qcs);
886 }
887 }
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100888 }
889 else {
890 TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs);
891 ret = b_data(&b);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200892 }
893
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100894 if (ret)
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200895 qcs_consume(qcs, ret);
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100896 if (ret || (!b_data(&b) && fin))
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200897 qcs_notify_recv(qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200898
899 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200900 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200901
902 err:
903 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
904 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200905}
906
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200907/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
908void qcc_reset_stream(struct qcs *qcs, int err)
909{
910 struct qcc *qcc = qcs->qcc;
911
912 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
913 return;
914
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200915 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200916 qcs->flags |= QC_SF_TO_RESET;
917 qcs->err = err;
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100918
Amaury Denoyelle178fbff2023-03-22 11:17:59 +0100919 /* Remove prepared stream data from connection flow-control calcul. */
920 if (qcs->tx.offset > qcs->tx.sent_offset) {
921 const uint64_t diff = qcs->tx.offset - qcs->tx.sent_offset;
922 BUG_ON(qcc->tx.offsets - diff < qcc->tx.sent_offsets);
923 qcc->tx.offsets -= diff;
924 /* Reset qcs offset to prevent BUG_ON() on qcs_destroy(). */
925 qcs->tx.offset = qcs->tx.sent_offset;
926 }
927
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100928 qcc_send_stream(qcs, 1);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200929 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100930}
931
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100932/* Register <qcs> stream for emission of STREAM, STOP_SENDING or RESET_STREAM.
933 * Set <urg> to 1 if stream content should be treated in priority compared to
934 * other streams.
935 */
936void qcc_send_stream(struct qcs *qcs, int urg)
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100937{
938 struct qcc *qcc = qcs->qcc;
939
940 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
941
942 /* Cannot send if already closed. */
943 BUG_ON(qcs_is_close_local(qcs));
944
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100945 if (urg) {
946 LIST_DEL_INIT(&qcs->el_send);
947 LIST_INSERT(&qcc->send_list, &qcs->el_send);
948 }
949 else {
950 if (!LIST_INLIST(&qcs->el_send))
951 LIST_APPEND(&qcs->qcc->send_list, &qcs->el_send);
952 }
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100953
954 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
955}
956
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100957/* Prepare for the emission of STOP_SENDING on <qcs>. */
958void qcc_abort_stream_read(struct qcs *qcs)
959{
960 struct qcc *qcc = qcs->qcc;
961
962 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs);
963
964 if ((qcs->flags & QC_SF_TO_STOP_SENDING) || qcs_is_close_remote(qcs))
965 goto end;
966
967 TRACE_STATE("abort stream read", QMUX_EV_QCS_END, qcc->conn, qcs);
968 qcs->flags |= (QC_SF_TO_STOP_SENDING|QC_SF_READ_ABORTED);
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100969
970 qcc_send_stream(qcs, 1);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100971 tasklet_wakeup(qcc->wait_event.tasklet);
972
973 end:
974 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200975}
976
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200977/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
978 * Returns 0 on success else non-zero.
979 */
980int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
981{
982 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
983
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100984 if (app_ops->init && !app_ops->init(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200985 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
986 goto err;
987 }
988
989 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100990 qcc->app_ops = app_ops;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200991
Amaury Denoyelle71fd0362023-01-24 17:35:37 +0100992 /* RFC 9114 7.2.4.2. Initialization
993 *
994 * Endpoints MUST NOT require any data to be
995 * received from the peer prior to sending the SETTINGS frame;
996 * settings MUST be sent as soon as the transport is ready to
997 * send data.
998 */
999 if (qcc->app_ops->finalize) {
1000 if (qcc->app_ops->finalize(qcc->ctx)) {
1001 TRACE_ERROR("app ops finalize error", QMUX_EV_QCC_NEW, qcc->conn);
1002 goto err;
1003 }
1004 tasklet_wakeup(qcc->wait_event.tasklet);
1005 }
1006
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001007 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
1008 return 0;
1009
1010 err:
1011 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
1012 return 1;
1013}
1014
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001015/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
1016 * <data> with length <len> and represents the offset <offset>. <fin> is set if
1017 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001018 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001019 * Returns 0 on success else non-zero. On error, the received frame should not
1020 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001021 */
1022int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001023 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001024{
1025 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001026 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001027
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001028 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1029
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001030 if (qcc->flags & QC_CF_ERRL) {
1031 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001032 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001033 }
1034
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +02001035 /* RFC 9000 19.8. STREAM Frames
1036 *
1037 * An endpoint MUST terminate the connection with error
1038 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
1039 * initiated stream that has not yet been created, or for a send-only
1040 * stream.
1041 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001042 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001043 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
1044 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001045 }
1046
1047 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001048 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
1049 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001050 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001051
Amaury Denoyellebf91e392022-07-04 10:02:04 +02001052 /* RFC 9000 4.5. Stream Final Size
1053 *
1054 * Once a final size for a stream is known, it cannot change. If a
1055 * RESET_STREAM or STREAM frame is received indicating a change in the
1056 * final size for the stream, an endpoint SHOULD respond with an error
1057 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
1058 * handling.
1059 */
1060 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1061 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001062 TRACE_ERROR("final size error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001063 qcc_set_error(qcc, QC_ERR_FINAL_SIZE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001064 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +02001065 }
1066
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001067 if (qcs_is_close_remote(qcs)) {
1068 TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn);
1069 goto out;
1070 }
1071
Amaury Denoyellefa241932023-02-14 15:36:36 +01001072 if (offset + len < qcs->rx.offset ||
1073 (offset + len == qcs->rx.offset && (!fin || (qcs->flags & QC_SF_SIZE_KNOWN)))) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001074 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1075 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001076 }
1077
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001078 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001079 qcs_idle_open(qcs);
1080
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001081 if (offset + len > qcs->rx.offset_max) {
1082 uint64_t diff = offset + len - qcs->rx.offset_max;
1083 qcs->rx.offset_max = offset + len;
1084 qcc->lfctl.offsets_recv += diff;
1085
1086 if (offset + len > qcs->rx.msd ||
1087 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
1088 /* RFC 9000 4.1. Data Flow Control
1089 *
1090 * A receiver MUST close the connection with an error
1091 * of type FLOW_CONTROL_ERROR if the sender violates
1092 * the advertised connection or stream data limits
1093 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001094 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001095 qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001096 qcc_set_error(qcc, QC_ERR_FLOW_CONTROL_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001097 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001098 }
1099 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001100
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001101 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelled00b3092023-05-11 17:00:54 +02001102 TRACE_ERROR("receive ncbuf alloc failure", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1103 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
1104 goto err;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001105 }
1106
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001107 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001108 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +02001109 size_t diff = qcs->rx.offset - offset;
1110
1111 len -= diff;
1112 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001113 offset = qcs->rx.offset;
1114 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001115
Amaury Denoyellefa241932023-02-14 15:36:36 +01001116 if (len) {
1117 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
1118 switch (ret) {
1119 case NCB_RET_OK:
1120 break;
1121
1122 case NCB_RET_DATA_REJ:
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001123 /* RFC 9000 2.2. Sending and Receiving Data
1124 *
1125 * An endpoint could receive data for a stream at the
1126 * same stream offset multiple times. Data that has
1127 * already been received can be discarded. The data at
1128 * a given offset MUST NOT change if it is sent
1129 * multiple times; an endpoint MAY treat receipt of
1130 * different data at the same offset within a stream as
1131 * a connection error of type PROTOCOL_VIOLATION.
1132 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001133 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001134 qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001135 qcc_set_error(qcc, QC_ERR_PROTOCOL_VIOLATION, 0);
Amaury Denoyellefa241932023-02-14 15:36:36 +01001136 return 1;
1137
1138 case NCB_RET_GAP_SIZE:
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001139 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
1140 qcc->conn, qcs);
Amaury Denoyellefa241932023-02-14 15:36:36 +01001141 return 1;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001142 }
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001143 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001144
1145 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +02001146 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001147
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001148 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1149 qcs->rx.offset_max == qcs->rx.offset + ncb_data(&qcs->rx.ncbuf, 0)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001150 qcs_close_remote(qcs);
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001151 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001152
Amaury Denoyellefa241932023-02-14 15:36:36 +01001153 if ((ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) || fin) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001154 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001155 qcc_refresh_timeout(qcc);
1156 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001157
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001158 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001159 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001160 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001161
1162 err:
1163 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1164 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001165}
1166
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001167/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1168 * the frame.
1169 *
1170 * Returns 0 on success else non-zero.
1171 */
1172int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1173{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001174 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1175
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001176 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001177 if (qcc->rfctl.md < max) {
1178 qcc->rfctl.md = max;
Amaury Denoyelleb7143a82023-03-22 15:08:01 +01001179 TRACE_DATA("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001180
1181 if (qcc->flags & QC_CF_BLK_MFCTL) {
1182 qcc->flags &= ~QC_CF_BLK_MFCTL;
1183 tasklet_wakeup(qcc->wait_event.tasklet);
1184 }
1185 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001186
1187 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001188 return 0;
1189}
1190
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001191/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1192 * field of the frame and <id> is the identifier of the QUIC stream.
1193 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001194 * Returns 0 on success else non-zero. On error, the received frame should not
1195 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001196 */
1197int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1198{
1199 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001200
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001201 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1202
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001203 if (qcc->flags & QC_CF_ERRL) {
1204 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001205 goto err;
1206 }
1207
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001208 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1209 *
1210 * Receiving a MAX_STREAM_DATA frame for a locally
1211 * initiated stream that has not yet been created MUST be treated as a
1212 * connection error of type STREAM_STATE_ERROR. An endpoint that
1213 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1214 * terminate the connection with error STREAM_STATE_ERROR.
1215 */
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001216 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
1217 goto err;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001218
1219 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001220 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001221 if (max > qcs->tx.msd) {
1222 qcs->tx.msd = max;
Amaury Denoyelleb7143a82023-03-22 15:08:01 +01001223 TRACE_DATA("increase remote max-stream-data", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001224
1225 if (qcs->flags & QC_SF_BLK_SFCTL) {
1226 qcs->flags &= ~QC_SF_BLK_SFCTL;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001227 /* TODO optim: only wakeup IO-CB if stream has data to sent. */
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001228 tasklet_wakeup(qcc->wait_event.tasklet);
1229 }
1230 }
1231 }
1232
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001233 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1234 qcc_refresh_timeout(qcc);
1235
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001236 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1237 return 0;
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001238
1239 err:
1240 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1241 return 1;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001242}
1243
1244/* Handle a new RESET_STREAM frame from stream ID <id> with error code <err>
1245 * and final stream size <final_size>.
1246 *
1247 * Returns 0 on success else non-zero. On error, the received frame should not
1248 * be acknowledged.
1249 */
1250int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size)
1251{
1252 struct qcs *qcs;
1253
1254 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1255
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001256 if (qcc->flags & QC_CF_ERRL) {
1257 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001258 goto err;
1259 }
1260
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001261 /* RFC 9000 19.4. RESET_STREAM Frames
1262 *
1263 * An endpoint that receives a RESET_STREAM frame for a send-only stream
1264 * MUST terminate the connection with error STREAM_STATE_ERROR.
1265 */
1266 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
1267 TRACE_ERROR("RESET_STREAM for send-only stream received", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001268 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001269 goto err;
1270 }
1271
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02001272 /* RFC 9000 3.2. Receiving Stream States
1273 *
1274 * A RESET_STREAM signal might be suppressed or withheld
1275 * if stream data is completely received and is buffered to be read by
1276 * the application. If the RESET_STREAM is suppressed, the receiving
1277 * part of the stream remains in "Data Recvd".
1278 */
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001279 if (!qcs || qcs_is_close_remote(qcs))
1280 goto out;
1281
1282 TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1283 qcs_idle_open(qcs);
1284
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02001285 /* Ensure stream closure is not forbidden by application protocol. */
Amaury Denoyellee269aeb2023-01-30 12:13:22 +01001286 if (qcc->app_ops->close) {
1287 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_RD)) {
1288 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1289 goto out;
1290 }
1291 }
1292
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001293 if (qcs->rx.offset_max > final_size ||
1294 ((qcs->flags & QC_SF_SIZE_KNOWN) && qcs->rx.offset_max != final_size)) {
1295 TRACE_ERROR("final size error on RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001296 qcc_set_error(qcc, QC_ERR_FINAL_SIZE_ERROR, 0);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001297 goto err;
1298 }
1299
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02001300 /* RFC 9000 3.2. Receiving Stream States
1301 *
1302 * An
1303 * implementation MAY interrupt delivery of stream data, discard any
1304 * data that was not consumed, and signal the receipt of the
1305 * RESET_STREAM.
1306 */
1307 qcs->flags |= QC_SF_SIZE_KNOWN|QC_SF_RECV_RESET;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001308 qcs_close_remote(qcs);
1309 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
1310
1311 if (qcs_sc(qcs)) {
Amaury Denoyelle37d78992023-05-24 10:49:44 +02001312 se_fl_set(qcs->sd, SE_FL_EOS);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001313 qcs_alert(qcs);
1314 }
1315
1316 out:
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001317 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001318 return 0;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001319
1320 err:
1321 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1322 return 1;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001323}
1324
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001325/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1326 * specified in <err>.
1327 *
1328 * Returns 0 on success else non-zero. On error, the received frame should not
1329 * be acknowledged.
1330 */
1331int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1332{
1333 struct qcs *qcs;
1334
1335 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1336
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001337 if (qcc->flags & QC_CF_ERRL) {
1338 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001339 goto err;
1340 }
1341
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001342 /* RFC 9000 19.5. STOP_SENDING Frames
1343 *
1344 * Receiving a STOP_SENDING frame for a
1345 * locally initiated stream that has not yet been created MUST be
1346 * treated as a connection error of type STREAM_STATE_ERROR. An
1347 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1348 * MUST terminate the connection with error STREAM_STATE_ERROR.
1349 */
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001350 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
1351 goto err;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001352
1353 if (!qcs)
1354 goto out;
1355
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001356 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelled7755372022-10-03 17:20:31 +02001357
1358 /* RFC 9000 3.5. Solicited State Transitions
1359 *
1360 * An endpoint is expected to send another STOP_SENDING frame if a
1361 * packet containing a previous STOP_SENDING is lost. However, once
1362 * either all stream data or a RESET_STREAM frame has been received for
1363 * the stream -- that is, the stream is in any state other than "Recv"
1364 * or "Size Known" -- sending a STOP_SENDING frame is unnecessary.
1365 */
1366
1367 /* TODO thanks to previous RFC clause, STOP_SENDING is ignored if current stream
1368 * has already been closed locally. This is useful to not emit multiple
1369 * RESET_STREAM for a single stream. This is functional if stream is
1370 * locally closed due to all data transmitted, but in this case the RFC
1371 * advices to use an explicit RESET_STREAM.
1372 */
1373 if (qcs_is_close_local(qcs)) {
1374 TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1375 goto out;
1376 }
1377
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001378 qcs_idle_open(qcs);
1379
Amaury Denoyelle87f87662023-01-30 12:12:43 +01001380 if (qcc->app_ops->close) {
1381 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_WR)) {
1382 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1383 goto out;
1384 }
1385 }
1386
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001387 /* RFC 9000 3.5. Solicited State Transitions
1388 *
1389 * An endpoint that receives a STOP_SENDING frame
1390 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1391 * "Send" state. If the stream is in the "Data Sent" state, the
1392 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1393 * containing outstanding data are acknowledged or declared lost. If
1394 * any outstanding data is declared lost, the endpoint SHOULD send a
1395 * RESET_STREAM frame instead of retransmitting the data.
1396 *
1397 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1398 * the RESET_STREAM frame it sends, but it can use any application error
1399 * code.
1400 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001401 qcc_reset_stream(qcs, err);
1402
Amaury Denoyelle152beee2023-05-24 14:43:43 +02001403 /* Report send error to stream-endpoint layer. */
1404 if (qcs_sc(qcs)) {
1405 se_fl_set_error(qcs->sd);
1406 qcs_alert(qcs);
1407 }
1408
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001409 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1410 qcc_refresh_timeout(qcc);
1411
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001412 out:
1413 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1414 return 0;
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001415
1416 err:
1417 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1418 return 1;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001419}
1420
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001421/* Signal the closing of remote stream with id <id>. Flow-control for new
1422 * streams may be allocated for the peer if needed.
1423 */
1424static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001425{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001426 struct quic_frame *frm;
1427
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001428 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1429
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001430 if (quic_stream_is_bidi(id)) {
1431 ++qcc->lfctl.cl_bidi_r;
1432 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001433 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001434 frm = qc_frm_alloc(QUIC_FT_MAX_STREAMS_BIDI);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001435 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001436 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001437 goto err;
1438 }
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001439
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001440 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1441 qcc->lfctl.cl_bidi_r;
1442 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1443 tasklet_wakeup(qcc->wait_event.tasklet);
1444
1445 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1446 qcc->lfctl.cl_bidi_r = 0;
1447 }
1448 }
1449 else {
Amaury Denoyelle91077312022-12-22 18:56:09 +01001450 /* TODO unidirectional stream flow control with MAX_STREAMS_UNI
1451 * emission not implemented. It should be unnecessary for
1452 * HTTP/3 but may be required if other application protocols
1453 * are supported.
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001454 */
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001455 }
1456
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001457 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1458
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001459 return 0;
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001460
1461 err:
1462 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_END, qcc->conn);
1463 return 1;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001464}
1465
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001466/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001467static void qcs_destroy(struct qcs *qcs)
1468{
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001469 struct qcc *qcc = qcs->qcc;
1470 struct connection *conn = qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001471 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001472
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001473 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001474
Amaury Denoyelle178fbff2023-03-22 11:17:59 +01001475 /* MUST not removed a stream with sending prepared data left. This is
1476 * to ensure consistency on connection flow-control calculation.
1477 */
1478 BUG_ON(qcs->tx.offset < qcs->tx.sent_offset);
1479
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001480 if (!(qcc->flags & QC_CF_ERRL)) {
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001481 if (quic_stream_is_remote(qcc, id))
1482 qcc_release_remote_stream(qcc, id);
1483 }
Amaury Denoyellec055e302022-02-07 16:09:06 +01001484
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001485 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001486
1487 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001488}
1489
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001490/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1491 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001492 *
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001493 * Returns the total bytes of transferred data or a negative error code.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001494 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001495static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001496{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001497 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001498 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001499 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001500
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001501 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001502
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001503 if (!qc_get_buf(qcs, out)) {
1504 TRACE_ERROR("buffer alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1505 goto err;
1506 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001507
1508 /*
1509 * QCS out buffer diagram
1510 * head left to_xfer
1511 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001512 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001513 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001514 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001515 * ^ ack-off ^ sent-off ^ off
1516 *
1517 * STREAM frame
1518 * ^ ^
1519 * |xxxxxxxxxxxxxxxxx|
1520 */
1521
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001522 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001523 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001524 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001525
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001526 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001527 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001528
1529 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1530 /* do not exceed flow control limit */
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001531 if (qcs->tx.offset + to_xfer > qcs->tx.msd) {
1532 TRACE_DATA("do not exceed stream flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001533 to_xfer = qcs->tx.msd - qcs->tx.offset;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001534 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001535
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001536 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001537 /* do not overcome flow control limit on connection */
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001538 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md) {
1539 TRACE_DATA("do not exceed conn flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001540 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001541 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001542
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001543 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001544 goto out;
1545
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001546 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001547
1548 out:
1549 {
1550 struct qcs_xfer_data_trace_arg arg = {
1551 .prep = b_data(out), .xfer = total,
1552 };
1553 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1554 qcc->conn, qcs, &arg);
1555 }
1556
1557 return total;
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001558
1559 err:
1560 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1561 return -1;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001562}
1563
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001564/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1565 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001566 * stream frame. If <out> is NULL an empty STREAM frame is built : this may be
1567 * useful if FIN needs to be sent without any data left.
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001568 *
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001569 * Returns the payload length of the STREAM frame or a negative error code.
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001570 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001571static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1572 struct list *frm_list)
1573{
1574 struct qcc *qcc = qcs->qcc;
1575 struct quic_frame *frm;
1576 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001577 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001578
1579 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1580
Amaury Denoyellea4569202022-04-15 17:29:25 +02001581 /* if ack_offset < buf_offset, it points to an older buffer. */
1582 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1583 BUG_ON(qcs->tx.sent_offset < base_off);
1584
1585 head = qcs->tx.sent_offset - base_off;
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001586 total = out ? b_data(out) - head : 0;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001587 BUG_ON(total < 0);
1588
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001589 if (!total && !fin) {
1590 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001591 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1592 return 0;
1593 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001594 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1595 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001596 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001597 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001598
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001599 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001600 frm = qc_frm_alloc(QUIC_FT_STREAM_8);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001601 if (!frm) {
1602 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001603 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001604 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001605
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001606 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001607 frm->stream.id = qcs->id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01001608 frm->stream.offset.key = 0;
Amaury Denoyelleebfafc22023-03-07 18:07:08 +01001609 frm->stream.dup = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001610
Amaury Denoyelle42c5b752023-04-25 16:39:32 +02001611 if (total) {
1612 frm->stream.buf = out;
1613 frm->stream.data = (unsigned char *)b_peek(out, head);
1614 }
1615 else {
1616 /* Empty STREAM frame. */
1617 frm->stream.buf = NULL;
1618 frm->stream.data = NULL;
1619 }
1620
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001621 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001622 if (fin)
1623 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001624
1625 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001626 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001627 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001628 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001629
Amaury Denoyelle42c5b752023-04-25 16:39:32 +02001630 /* Always set length bit as we do not know if there is remaining frames
1631 * in the final packet after this STREAM.
1632 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001633 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1634 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001635
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001636 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001637
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001638 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001639 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001640 struct qcs_build_stream_trace_arg arg = {
1641 .len = frm->stream.len, .fin = fin,
1642 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001643 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001644 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001645 qcc->conn, qcs, &arg);
1646 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001647
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001648 return total;
1649
1650 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001651 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001652 return -1;
1653}
1654
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001655/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001656 * STREAM frame for <qcs>.
1657 *
1658 * Returns true if FIN must be set else false.
1659 */
1660static int qcs_stream_fin(struct qcs *qcs)
1661{
1662 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1663}
1664
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001665/* Return true if <qcs> has data to send in new STREAM frames. */
1666static forceinline int qcs_need_sending(struct qcs *qcs)
1667{
1668 return b_data(&qcs->tx.buf) || qcs->tx.sent_offset < qcs->tx.offset ||
1669 qcs_stream_fin(qcs);
1670}
1671
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001672/* This function must be called by the upper layer to inform about the sending
1673 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1674 * <offset>.
1675 */
1676void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1677{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001678 struct qcc *qcc = qcs->qcc;
1679 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001680
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001681 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1682
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001683 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001684 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001685
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001686 /* check if the STREAM frame has already been notified. It can happen
1687 * for retransmission.
1688 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001689 if (offset + data < qcs->tx.sent_offset) {
1690 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1691 goto out;
1692 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001693
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001694 qcs_idle_open(qcs);
1695
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001696 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001697 if (diff) {
1698 /* increase offset sum on connection */
1699 qcc->tx.sent_offsets += diff;
1700 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001701 if (qcc->tx.sent_offsets == qcc->rfctl.md) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001702 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001703 TRACE_STATE("connection flow-control reached", QMUX_EV_QCS_SEND, qcc->conn);
1704 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001705
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001706 /* increase offset on stream */
1707 qcs->tx.sent_offset += diff;
1708 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1709 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001710 if (qcs->tx.sent_offset == qcs->tx.msd) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001711 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001712 TRACE_STATE("stream flow-control reached", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1713 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001714
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001715 /* If qcs.stream.buf is full, release it to the lower layer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001716 if (qcs->tx.offset == qcs->tx.sent_offset &&
1717 b_full(&qcs->stream->buf->buf)) {
1718 qc_stream_buf_release(qcs->stream);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001719 }
Amaury Denoyelle1bcb6952023-04-28 16:24:44 +02001720
1721 /* Add measurement for send rate. This is done at the MUX layer
1722 * to account only for STREAM frames without retransmission.
Amaury Denoyelle1bcb6952023-04-28 16:24:44 +02001723 */
Amaury Denoyellebc0adfa2023-04-28 16:46:11 +02001724 increment_send_rate(diff, 0);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001725 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001726
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001727 if (qcs->tx.offset == qcs->tx.sent_offset && !b_data(&qcs->tx.buf)) {
1728 /* Remove stream from send_list if all was sent. */
1729 LIST_DEL_INIT(&qcs->el_send);
1730 TRACE_STATE("stream sent done", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1731
1732 if (qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) {
1733 /* Close stream locally. */
1734 qcs_close_local(qcs);
1735 /* Reset flag to not emit multiple FIN STREAM frames. */
1736 qcs->flags &= ~QC_SF_FIN_STREAM;
1737 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001738 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001739
1740 out:
1741 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001742}
1743
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001744/* Returns true if subscribe set, false otherwise. */
1745static int qcc_subscribe_send(struct qcc *qcc)
1746{
1747 struct connection *conn = qcc->conn;
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02001748
1749 /* Do not subscribe if lower layer in error. */
1750 if (conn->flags & CO_FL_ERROR)
1751 return 0;
1752
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001753 if (qcc->wait_event.events & SUB_RETRY_SEND)
1754 return 1;
1755
1756 TRACE_DEVEL("subscribe for send", QMUX_EV_QCC_SEND, qcc->conn);
1757 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &qcc->wait_event);
1758 return 1;
1759}
1760
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001761/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1762 * connection <qcc>.
1763 *
1764 * Returns 0 if all data sent with success else non-zero.
1765 */
1766static int qc_send_frames(struct qcc *qcc, struct list *frms)
1767{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001768 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1769
1770 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001771 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1772 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001773 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001774
Amaury Denoyellecaa16542023-02-28 15:11:26 +01001775 if (!qc_send_mux(qcc->conn->handle.qc, frms)) {
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001776 TRACE_DEVEL("error on sending", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001777 qcc_subscribe_send(qcc);
Amaury Denoyelle036cc5d2022-09-26 15:02:31 +02001778 goto err;
Amaury Denoyellecaa16542023-02-28 15:11:26 +01001779 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001780
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001781 /* If there is frames left at this stage, transport layer is blocked.
1782 * Subscribe on it to retry later.
1783 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001784 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001785 TRACE_DEVEL("remaining frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1786 qcc_subscribe_send(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001787 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001788 }
1789
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001790 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001791 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001792
1793 err:
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02001794 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001795 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001796}
1797
1798/* Emit a RESET_STREAM on <qcs>.
1799 *
1800 * Returns 0 if the frame has been successfully sent else non-zero.
1801 */
1802static int qcs_send_reset(struct qcs *qcs)
1803{
1804 struct list frms = LIST_HEAD_INIT(frms);
1805 struct quic_frame *frm;
1806
1807 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1808
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001809 frm = qc_frm_alloc(QUIC_FT_RESET_STREAM);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001810 if (!frm) {
1811 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001812 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001813 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001814
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001815 frm->reset_stream.id = qcs->id;
1816 frm->reset_stream.app_error_code = qcs->err;
1817 frm->reset_stream.final_size = qcs->tx.sent_offset;
1818
1819 LIST_APPEND(&frms, &frm->list);
1820 if (qc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle131f2d92023-05-09 14:10:55 +02001821 if (!LIST_ISEMPTY(&frms))
1822 qc_frm_free(&frm);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001823 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1824 return 1;
1825 }
1826
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001827 qcs_close_local(qcs);
1828 qcs->flags &= ~QC_SF_TO_RESET;
1829
1830 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001831 return 0;
1832}
1833
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001834/* Emit a STOP_SENDING on <qcs>.
1835 *
1836 * Returns 0 if the frame has been successfully sent else non-zero.
1837 */
1838static int qcs_send_stop_sending(struct qcs *qcs)
1839{
1840 struct list frms = LIST_HEAD_INIT(frms);
1841 struct quic_frame *frm;
1842 struct qcc *qcc = qcs->qcc;
1843
1844 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1845
1846 /* RFC 9000 3.3. Permitted Frame Types
1847 *
1848 * A
1849 * receiver MAY send a STOP_SENDING frame in any state where it has not
1850 * received a RESET_STREAM frame -- that is, states other than "Reset
1851 * Recvd" or "Reset Read". However, there is little value in sending a
1852 * STOP_SENDING frame in the "Data Recvd" state, as all stream data has
1853 * been received. A sender could receive either of these two types of
1854 * frames in any state as a result of delayed delivery of packets.¶
1855 */
1856 if (qcs_is_close_remote(qcs)) {
1857 TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1858 goto done;
1859 }
1860
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001861 frm = qc_frm_alloc(QUIC_FT_STOP_SENDING);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001862 if (!frm) {
1863 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1864 return 1;
1865 }
1866
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001867 frm->stop_sending.id = qcs->id;
1868 frm->stop_sending.app_error_code = qcs->err;
1869
1870 LIST_APPEND(&frms, &frm->list);
1871 if (qc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle131f2d92023-05-09 14:10:55 +02001872 if (!LIST_ISEMPTY(&frms))
1873 qc_frm_free(&frm);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001874 TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1875 return 1;
1876 }
1877
1878 done:
1879 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
1880
1881 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1882 return 0;
1883}
1884
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001885/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1886 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001887 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001888 *
1889 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001890 * be null if out buffer cannot be allocated. On error a negative error code is
1891 * used.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001892 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001893static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001894{
1895 struct qcc *qcc = qcs->qcc;
1896 struct buffer *buf = &qcs->tx.buf;
1897 struct buffer *out = qc_stream_buf_get(qcs->stream);
Amaury Denoyelle6c501ed2023-05-12 16:19:32 +02001898 int xfer = 0, buf_avail;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001899 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001900
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001901 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1902
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001903 /* Cannot send STREAM on remote unidirectional streams. */
1904 BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_remote(qcc, qcs->id));
1905
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001906 if (b_data(buf)) {
1907 /* Allocate <out> buffer if not already done. */
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001908 if (!out) {
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001909 if (qcc->flags & QC_CF_CONN_FULL)
1910 goto out;
1911
Amaury Denoyelle6c501ed2023-05-12 16:19:32 +02001912 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset,
1913 &buf_avail);
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001914 if (!out) {
Amaury Denoyelle1611a762023-05-15 13:56:46 +02001915 if (buf_avail) {
Amaury Denoyelle6c501ed2023-05-12 16:19:32 +02001916 TRACE_ERROR("stream desc alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1917 goto err;
1918 }
1919
Amaury Denoyelle1611a762023-05-15 13:56:46 +02001920 TRACE_STATE("hitting stream desc buffer limit", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001921 qcc->flags |= QC_CF_CONN_FULL;
1922 goto out;
1923 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001924 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001925
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001926 /* Transfer data from <buf> to <out>. */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001927 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001928 if (xfer < 0)
1929 goto err;
1930
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001931 if (xfer > 0) {
1932 qcs_notify_send(qcs);
1933 qcs->flags &= ~QC_SF_BLK_MROOM;
1934 }
1935
1936 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001937 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001938 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001939 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001940
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001941 /* out buffer cannot be emptied if qcs offsets differ. */
1942 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1943 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001944
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001945 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001946 fin = qcs_stream_fin(qcs);
1947
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001948 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001949 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle04b22082023-05-03 09:50:39 +02001950 /* Skip STREAM frame allocation if already subscribed for send.
1951 * Happens on sendto transient error or network congestion.
1952 */
1953 if (qcc->wait_event.events & SUB_RETRY_SEND) {
1954 TRACE_DEVEL("already subscribed for sending",
1955 QMUX_EV_QCS_SEND, qcc->conn, qcs);
1956 goto err;
1957 }
1958
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001959 if (qcs_build_stream_frm(qcs, out, fin, frms) < 0)
1960 goto err;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001961 }
1962
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001963 out:
1964 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001965 return xfer;
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001966
1967 err:
1968 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1969 return -1;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001970}
1971
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001972/* Proceed to sending. Loop through all available streams for the <qcc>
1973 * instance and try to send as much as possible.
1974 *
1975 * Returns the total of bytes sent to the transport layer.
1976 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001977static int qc_send(struct qcc *qcc)
1978{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001979 struct list frms = LIST_HEAD_INIT(frms);
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001980 /* Temporary list for QCS on error. */
1981 struct list qcs_failed = LIST_HEAD_INIT(qcs_failed);
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02001982 struct qcs *qcs, *qcs_tmp, *first_qcs = NULL;
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001983 int ret, total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001984
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001985 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001986
Amaury Denoyelle04b22082023-05-03 09:50:39 +02001987 /* TODO if socket in transient error, sending should be temporarily
1988 * disabled for all frames. However, checking for send subscription is
1989 * not valid as this may be caused by a congestion error which only
1990 * apply for STREAM frames.
1991 */
1992
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02001993 /* Check for transport error. */
1994 if (qcc->flags & QC_CF_ERR_CONN || qcc->conn->flags & CO_FL_ERROR) {
1995 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1996 goto out;
1997 }
1998
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001999 /* Check for locally detected connection error. */
2000 if (qcc->flags & QC_CF_ERRL) {
2001 /* Prepare a CONNECTION_CLOSE if not already done. */
2002 if (!(qcc->flags & QC_CF_ERRL_DONE)) {
2003 TRACE_DATA("report a connection error", QMUX_EV_QCC_SEND|QMUX_EV_QCC_ERR, qcc->conn);
2004 quic_set_connection_close(qcc->conn->handle.qc, qcc->err);
2005 qcc->flags |= QC_CF_ERRL_DONE;
2006 }
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002007 goto out;
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002008 }
2009
2010 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002011 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002012 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002013 goto out;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002014 }
2015
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002016 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
2017 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
2018 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
2019 goto out;
2020 }
2021 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02002022
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002023 if (qcc->flags & QC_CF_BLK_MFCTL)
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002024 goto out;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002025
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002026 /* Send STREAM/STOP_SENDING/RESET_STREAM data for registered streams. */
2027 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02002028 /* Check if all QCS were processed. */
2029 if (qcs == first_qcs)
2030 break;
2031
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002032 /* Stream must not be present in send_list if it has nothing to send. */
2033 BUG_ON(!(qcs->flags & (QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET)) &&
2034 !qcs_need_sending(qcs));
Amaury Denoyellec6195d72022-05-23 11:39:14 +02002035
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002036 /* Each STOP_SENDING/RESET_STREAM frame is sent individually to
2037 * guarantee its emission.
2038 *
2039 * TODO multiplex several frames in same datagram to optimize sending
2040 */
2041 if (qcs->flags & QC_SF_TO_STOP_SENDING) {
2042 if (qcs_send_stop_sending(qcs))
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002043 goto sent_done;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01002044
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002045 /* Remove stream from send_list if it had only STOP_SENDING
2046 * to send.
2047 */
2048 if (!(qcs->flags & QC_SF_TO_RESET) && !qcs_need_sending(qcs)) {
2049 LIST_DEL_INIT(&qcs->el_send);
2050 continue;
2051 }
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01002052 }
2053
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002054 if (qcs->flags & QC_SF_TO_RESET) {
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002055 if (qcs_send_reset(qcs))
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002056 goto sent_done;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002057
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002058 /* RFC 9000 3.3. Permitted Frame Types
2059 *
2060 * A sender MUST NOT send
2061 * a STREAM or STREAM_DATA_BLOCKED frame for a stream in the
2062 * "Reset Sent" state or any terminal state -- that is, after
2063 * sending a RESET_STREAM frame.
2064 */
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002065 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02002066 continue;
2067 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02002068
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002069 if (!(qcs->flags & QC_SF_BLK_SFCTL)) {
2070 if ((ret = _qc_send_qcs(qcs, &frms)) < 0) {
2071 /* Temporarily remove QCS from send-list. */
2072 LIST_DEL_INIT(&qcs->el_send);
2073 LIST_APPEND(&qcs_failed, &qcs->el_send);
2074 continue;
2075 }
2076
2077 total += ret;
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02002078 if (ret) {
2079 /* Move QCS with some bytes transferred at the
2080 * end of send-list for next iterations.
2081 */
2082 LIST_DEL_INIT(&qcs->el_send);
2083 LIST_APPEND(&qcc->send_list, &qcs->el_send);
2084 /* Remember first moved QCS as checkpoint to interrupt loop */
2085 if (!first_qcs)
2086 first_qcs = qcs;
2087 }
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002088 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002089 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002090
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01002091 /* Retry sending until no frame to send, data rejected or connection
2092 * flow-control limit reached.
2093 */
2094 while (qc_send_frames(qcc, &frms) == 0 && !(qcc->flags & QC_CF_BLK_MFCTL)) {
2095 /* Reloop over <qcc.send_list>. Useful for streams which have
2096 * fulfilled their qc_stream_desc buf and have now release it.
2097 */
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002098 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01002099 /* Only streams blocked on flow-control or waiting on a
2100 * new qc_stream_desc should be present in send_list as
2101 * long as transport layer can handle all data.
2102 */
2103 BUG_ON(qcs->stream->buf && !(qcs->flags & QC_SF_BLK_SFCTL));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002104
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002105 if (!(qcs->flags & QC_SF_BLK_SFCTL)) {
2106 if ((ret = _qc_send_qcs(qcs, &frms)) < 0) {
2107 LIST_DEL_INIT(&qcs->el_send);
2108 LIST_APPEND(&qcs_failed, &qcs->el_send);
2109 continue;
2110 }
2111
2112 total += ret;
2113 }
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01002114 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02002115 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02002116
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002117 sent_done:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02002118 /* Deallocate frames that the transport layer has rejected. */
2119 if (!LIST_ISEMPTY(&frms)) {
2120 struct quic_frame *frm, *frm2;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002121
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002122 list_for_each_entry_safe(frm, frm2, &frms, list)
2123 qc_frm_free(&frm);
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02002124 }
2125
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002126 /* Re-insert on-error QCS at the end of the send-list. */
2127 if (!LIST_ISEMPTY(&qcs_failed)) {
2128 list_for_each_entry_safe(qcs, qcs_tmp, &qcs_failed, el_send) {
2129 LIST_DEL_INIT(&qcs->el_send);
2130 LIST_APPEND(&qcc->send_list, &qcs->el_send);
2131 }
2132
2133 if (!(qcc->flags & QC_CF_BLK_MFCTL))
2134 tasklet_wakeup(qcc->wait_event.tasklet);
2135 }
2136
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002137 out:
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002138 if (qcc->conn->flags & CO_FL_ERROR && !(qcc->flags & QC_CF_ERR_CONN)) {
2139 TRACE_ERROR("error reported by transport layer",
2140 QMUX_EV_QCC_SEND, qcc->conn);
2141 qcc->flags |= QC_CF_ERR_CONN;
2142 }
2143
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002144 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01002145 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002146}
2147
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002148/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
2149 * operation.
2150 *
2151 * Returns 0 on success else non-zero.
2152 */
2153static int qc_recv(struct qcc *qcc)
2154{
2155 struct eb64_node *node;
2156 struct qcs *qcs;
2157
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002158 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02002159
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002160 if (qcc->flags & QC_CF_ERRL) {
2161 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002162 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02002163 return 0;
2164 }
2165
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002166 node = eb64_first(&qcc->streams_by_id);
2167 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002168 uint64_t id;
2169
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002170 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002171 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002172
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002173 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002174 node = eb64_next(node);
2175 continue;
2176 }
2177
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002178 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002179 node = eb64_next(node);
2180 continue;
2181 }
2182
2183 qcc_decode_qcs(qcc, qcs);
2184 node = eb64_next(node);
2185 }
2186
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002187 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002188 return 0;
2189}
2190
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002191
2192/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01002193 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002194 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01002195 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002196static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002197{
2198 struct eb64_node *node;
2199 int release = 0;
2200
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002201 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002202
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002203 node = eb64_first(&qcc->streams_by_id);
2204 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002205 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002206 node = eb64_next(node);
2207
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002208 /* Release not attached closed streams. */
2209 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002210 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002211 qcs_destroy(qcs);
2212 release = 1;
2213 continue;
2214 }
2215
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002216 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002217 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002218 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002219 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002220 qcs_destroy(qcs);
2221 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002222 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002223 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002224 }
2225 }
2226
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002227 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002228 return release;
2229}
2230
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002231/* Execute application layer shutdown. If this operation is not defined, a
2232 * CONNECTION_CLOSE will be prepared as a fallback. This function is protected
2233 * against multiple invocation with the flag QC_CF_APP_SHUT.
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002234 */
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002235static void qc_shutdown(struct qcc *qcc)
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002236{
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002237 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002238
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002239 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002240 TRACE_DATA("connection on error", QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002241 goto out;
Amaury Denoyelle665817a2023-03-20 17:34:22 +01002242 }
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002243
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002244 if (qcc->flags & QC_CF_APP_SHUT)
2245 goto out;
2246
2247 TRACE_STATE("perform graceful shutdown", QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002248 if (qcc->app_ops && qcc->app_ops->shutdown) {
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002249 qcc->app_ops->shutdown(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02002250 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002251 }
2252 else {
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002253 qcc->err = quic_err_app(QC_ERR_NO_ERROR);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002254 }
2255
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002256 /* Register "no error" code at transport layer. Do not use
2257 * quic_set_connection_close() as retransmission may be performed to
2258 * finalized transfers. Do not overwrite quic-conn existing code if
2259 * already set.
2260 *
2261 * TODO implement a wrapper function for this in quic-conn module
2262 */
2263 if (!(qcc->conn->handle.qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
2264 qcc->conn->handle.qc->err = qcc->err;
2265
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002266 out:
2267 qcc->flags |= QC_CF_APP_SHUT;
2268 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
2269}
2270
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002271/* Loop through all qcs from <qcc>. Report error on stream endpoint if
2272 * connection on error and wake them.
2273 */
2274static int qc_wake_some_streams(struct qcc *qcc)
2275{
2276 struct qcs *qcs;
2277 struct eb64_node *node;
2278
2279 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn);
2280
2281 for (node = eb64_first(&qcc->streams_by_id); node;
2282 node = eb64_next(node)) {
2283 qcs = eb64_entry(node, struct qcs, by_id);
2284
2285 if (!qcs_sc(qcs))
2286 continue;
2287
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002288 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002289 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn, qcs);
2290 se_fl_set_error(qcs->sd);
2291 qcs_alert(qcs);
2292 }
2293 }
2294
2295 return 0;
2296}
2297
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002298/* Conduct operations which should be made for <qcc> connection after
2299 * input/output. Most notably, closed streams are purged which may leave the
2300 * connection has ready to be released.
2301 *
2302 * Returns 1 if <qcc> must be released else 0.
2303 */
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002304static int qc_process(struct qcc *qcc)
2305{
2306 qc_purge_streams(qcc);
2307
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002308 /* Check if a soft-stop is in progress.
2309 *
2310 * TODO this is relevant for frontend connections only.
2311 */
2312 if (unlikely(qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
2313 int close = 1;
2314
2315 /* If using listener socket, soft-stop is not supported. The
2316 * connection must be closed immediately.
2317 */
2318 if (!qc_test_fd(qcc->conn->handle.qc)) {
2319 TRACE_DEVEL("proxy disabled with listener socket, closing connection", QMUX_EV_QCC_WAKE, qcc->conn);
2320 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2321 qc_send(qcc);
2322 goto out;
2323 }
2324
2325 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
2326
2327 /* If a close-spread-time option is set, we want to avoid
2328 * closing all the active HTTP3 connections at once so we add a
2329 * random factor that will spread the closing.
2330 */
2331 if (tick_isset(global.close_spread_end)) {
2332 int remaining_window = tick_remain(now_ms, global.close_spread_end);
2333 if (remaining_window) {
2334 /* This should increase the closing rate the
2335 * further along the window we are. */
2336 close = (remaining_window <= statistical_prng_range(global.close_spread_time));
2337 }
2338 }
2339 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) {
2340 close = 0; /* let the client close his connection himself */
2341 }
2342
2343 if (close)
2344 qc_shutdown(qcc);
2345 }
2346
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002347 /* Report error if set on stream endpoint layer. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002348 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002349 qc_wake_some_streams(qcc);
2350
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002351 out:
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002352 if (qcc_is_dead(qcc))
2353 return 1;
2354
2355 return 0;
2356}
2357
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002358/* release function. This one should be called to free all resources allocated
2359 * to the mux.
2360 */
2361static void qc_release(struct qcc *qcc)
2362{
2363 struct connection *conn = qcc->conn;
2364 struct eb64_node *node;
2365
2366 TRACE_ENTER(QMUX_EV_QCC_END, conn);
2367
2368 qc_shutdown(qcc);
2369
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002370 if (qcc->task) {
2371 task_destroy(qcc->task);
2372 qcc->task = NULL;
2373 }
2374
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02002375 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002376 if (conn && qcc->wait_event.events) {
2377 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
2378 qcc->wait_event.events,
2379 &qcc->wait_event);
2380 }
2381
2382 /* liberate remaining qcs instances */
2383 node = eb64_first(&qcc->streams_by_id);
2384 while (node) {
2385 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
2386 node = eb64_next(node);
2387 qcs_free(qcs);
2388 }
2389
2390 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
2391 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002392 qc_frm_free(&frm);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002393 }
2394
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002395 if (qcc->app_ops && qcc->app_ops->release)
2396 qcc->app_ops->release(qcc->ctx);
2397 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
2398
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002399 pool_free(pool_head_qcc, qcc);
2400
2401 if (conn) {
2402 LIST_DEL_INIT(&conn->stopping_list);
2403
2404 conn->handle.qc->conn = NULL;
2405 conn->mux = NULL;
2406 conn->ctx = NULL;
2407
2408 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
2409
2410 conn_stop_tracking(conn);
2411 conn_full_close(conn);
2412 if (conn->destroy_cb)
2413 conn->destroy_cb(conn);
2414 conn_free(conn);
2415 }
2416
2417 TRACE_LEAVE(QMUX_EV_QCC_END);
2418}
2419
Willy Tarreau41e701e2022-09-08 15:12:59 +02002420struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002421{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02002422 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002423
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002424 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002425
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002426 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002427
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002428 qc_recv(qcc);
2429
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002430 if (qc_process(qcc)) {
2431 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
2432 goto release;
2433 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002434
2435 qcc_refresh_timeout(qcc);
2436
Amaury Denoyelled3973852022-07-25 14:56:54 +02002437 end:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002438 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
2439 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002440
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002441 release:
2442 qc_release(qcc);
2443 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002444 return NULL;
2445}
2446
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002447static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
2448{
2449 struct qcc *qcc = ctx;
2450 int expired = tick_is_expired(t->expire, now_ms);
2451
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002452 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002453
2454 if (qcc) {
2455 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002456 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
2457 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002458 }
2459
2460 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002461 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002462 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002463 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002464 }
2465 }
2466
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002467 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002468
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002469 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002470 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
2471 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002472 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002473
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002474 qcc->task = NULL;
2475
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002476 /* TODO depending on the timeout condition, different shutdown mode
2477 * should be used. For http keep-alive or disabled proxy, a graceful
2478 * shutdown should occurs. For all other cases, an immediate close
2479 * seems legitimate.
2480 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002481 if (qcc_is_dead(qcc)) {
2482 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002483 qc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002484 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002485
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002486 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002487 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002488 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002489
2490 requeue:
2491 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
2492 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002493}
2494
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002495static int qc_init(struct connection *conn, struct proxy *prx,
2496 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002497{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002498 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002499 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002500
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002501 TRACE_ENTER(QMUX_EV_QCC_NEW);
2502
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002503 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002504 if (!qcc) {
2505 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002506 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002507 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002508
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002509 qcc->conn = conn;
2510 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02002511 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01002512 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002513
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002514 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002515
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002516 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002517
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002518 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02002519 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002520
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02002521 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002522
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002523 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002524 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02002525 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02002526 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
2527 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002528 qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002529 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01002530
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002531 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02002532 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002533
Willy Tarreau784b8682022-04-11 14:18:10 +02002534 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002535 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002536 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
2537 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002538 qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002539
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02002540 if (conn_is_back(conn)) {
2541 qcc->next_bidi_l = 0x00;
2542 qcc->largest_bidi_r = 0x01;
2543 qcc->next_uni_l = 0x02;
2544 qcc->largest_uni_r = 0x03;
2545 }
2546 else {
2547 qcc->largest_bidi_r = 0x00;
2548 qcc->next_bidi_l = 0x01;
2549 qcc->largest_uni_r = 0x02;
2550 qcc->next_uni_l = 0x03;
2551 }
2552
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002553 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002554 if (!qcc->wait_event.tasklet) {
2555 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002556 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002557 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002558
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002559 LIST_INIT(&qcc->send_list);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002560
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002561 qcc->wait_event.tasklet->process = qc_io_cb;
2562 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01002563 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002564
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02002565 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002566 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002567 qcc->task = NULL;
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +01002568 if (conn_is_back(qcc->conn)) {
2569 qcc->timeout = prx->timeout.server;
2570 qcc->shut_timeout = tick_isset(prx->timeout.serverfin) ?
2571 prx->timeout.serverfin : prx->timeout.server;
2572 }
2573 else {
2574 qcc->timeout = prx->timeout.client;
2575 qcc->shut_timeout = tick_isset(prx->timeout.clientfin) ?
2576 prx->timeout.clientfin : prx->timeout.client;
2577 }
2578
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002579 if (tick_isset(qcc->timeout)) {
2580 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002581 if (!qcc->task) {
2582 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002583 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002584 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002585 qcc->task->process = qc_timeout_task;
2586 qcc->task->context = qcc;
2587 qcc->task->expire = tick_add(now_ms, qcc->timeout);
2588 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02002589 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02002590 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002591
Willy Tarreau784b8682022-04-11 14:18:10 +02002592 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002593
2594 if (qcc_install_app_ops(qcc, conn->handle.qc->app_ops)) {
Amaury Denoyelle8d44bfa2023-05-04 15:16:01 +02002595 TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW|QMUX_EV_QCC_ERR, qcc->conn);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002596 /* prepare a CONNECTION_CLOSE frame */
2597 quic_set_connection_close(conn->handle.qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
2598 goto fail_install_app_ops;
2599 }
2600
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002601 if (qcc->app_ops == &h3_ops)
2602 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 3);
2603
Amaury Denoyelleed820822023-04-19 17:58:39 +02002604 /* Register conn for idle front closing. This is done once everything is allocated. */
2605 if (!conn_is_back(conn))
2606 LIST_APPEND(&mux_stopping_data[tid].list, &conn->stopping_list);
2607
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002608 /* init read cycle */
2609 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002610
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002611 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002612 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002613
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002614 fail_install_app_ops:
2615 if (qcc->app_ops && qcc->app_ops->release)
2616 qcc->app_ops->release(qcc->ctx);
Amaury Denoyelleee65efb2023-05-12 16:29:48 +02002617 task_destroy(qcc->task);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002618 fail_no_timeout_task:
2619 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002620 fail_no_tasklet:
2621 pool_free(pool_head_qcc, qcc);
2622 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002623 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002624 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002625}
2626
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002627static void qc_destroy(void *ctx)
2628{
2629 struct qcc *qcc = ctx;
2630
2631 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
2632 qc_release(qcc);
2633 TRACE_LEAVE(QMUX_EV_QCC_END);
2634}
2635
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002636static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002637{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002638 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002639 struct qcc *qcc = qcs->qcc;
2640
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002641 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002642
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002643 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2644 * from the stream even if it is not closed remotely at the QUIC layer.
2645 * This happens for example when a stream must be closed due to a
2646 * rejected request. To better handle these cases, it will be required
2647 * to implement shutr/shutw MUX operations. Once this is done, this
2648 * BUG_ON_HOT() statement can be adjusted.
2649 */
2650 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002651
2652 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002653
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002654 if (!qcs_is_close_local(qcs) &&
2655 !(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002656 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002657 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002658 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002659
2660 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002661 return;
2662 }
2663
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002664 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002665
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002666 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002667 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002668 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002669 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002670 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002671 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002672 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002673 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002674 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002675 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002676 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002677
2678 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002679 return;
2680
2681 release:
2682 qc_release(qcc);
2683 TRACE_LEAVE(QMUX_EV_STRM_END);
2684 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002685}
2686
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002687/* Called from the upper layer, to receive data */
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002688static size_t qc_recv_buf(struct stconn *sc, struct buffer *buf,
2689 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002690{
Willy Tarreau3215e732022-05-27 10:09:11 +02002691 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle16494692023-05-15 11:35:45 +02002692 struct qcc *qcc = qcs->qcc;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002693 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002694 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002695
Amaury Denoyelle16494692023-05-15 11:35:45 +02002696 TRACE_ENTER(QMUX_EV_STRM_RECV, qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002697
Amaury Denoyelled80fbca2022-09-19 17:02:28 +02002698 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002699
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002700 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002701 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002702 }
2703 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002704 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002705
Amaury Denoyellebf86d892023-05-12 18:16:31 +02002706 /* Set end-of-input when full message properly received. */
Christopher Faulet85eabfb2023-02-23 14:52:09 +01002707 if (fin) {
Amaury Denoyelle16494692023-05-15 11:35:45 +02002708 TRACE_STATE("report end-of-input", QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyelle8de35922023-05-24 10:48:52 +02002709 se_fl_set(qcs->sd, SE_FL_EOI|SE_FL_EOS);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002710
Christopher Faulet85eabfb2023-02-23 14:52:09 +01002711 /* If request EOM is reported to the upper layer, it means the
2712 * QCS now expects data from the opposite side.
2713 */
2714 se_expect_data(qcs->sd);
2715 }
2716
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02002717 /* Set end-of-stream on read closed. */
2718 if (qcs->flags & QC_SF_RECV_RESET ||
2719 qcc->conn->flags & CO_FL_SOCK_RD_SH) {
2720 TRACE_STATE("report end-of-stream", QMUX_EV_STRM_RECV, qcc->conn, qcs);
2721 se_fl_set(qcs->sd, SE_FL_EOS);
2722
2723 /* Set error if EOI not reached. This may happen on
2724 * RESET_STREAM reception or connection error.
2725 */
2726 if (!se_fl_test(qcs->sd, SE_FL_EOI)) {
2727 TRACE_STATE("report error on stream aborted", QMUX_EV_STRM_RECV, qcc->conn, qcs);
2728 se_fl_set(qcs->sd, SE_FL_EOS | SE_FL_ERROR);
2729 }
2730 }
2731
Amaury Denoyelle16494692023-05-15 11:35:45 +02002732 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING)) {
2733 TRACE_STATE("report error", QMUX_EV_STRM_RECV, qcc->conn, qcs);
2734 se_fl_set(qcs->sd, SE_FL_ERROR);
2735 }
2736
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002737 if (b_size(&qcs->rx.app_buf)) {
2738 b_free(&qcs->rx.app_buf);
2739 offer_buffers(NULL, 1);
2740 }
2741 }
2742
Amaury Denoyelleb8901d22023-05-03 15:30:04 +02002743 /* Restart demux if it was interrupted on full buffer. */
2744 if (ret && qcs->flags & QC_SF_DEM_FULL) {
2745 /* There must be data left for demux if it was interrupted on
2746 * full buffer. If this assumption is incorrect wakeup is not
2747 * necessary.
2748 */
2749 BUG_ON(!ncb_data(&qcs->rx.ncbuf, 0));
2750
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002751 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle16494692023-05-15 11:35:45 +02002752 if (!(qcc->flags & QC_CF_ERRL))
2753 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002754 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002755
Amaury Denoyelle16494692023-05-15 11:35:45 +02002756 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002757
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002758 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002759}
2760
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002761static size_t qc_send_buf(struct stconn *sc, struct buffer *buf,
2762 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002763{
Willy Tarreau3215e732022-05-27 10:09:11 +02002764 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002765 size_t ret = 0;
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002766 char fin;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002767
2768 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002769
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02002770 /* stream layer has been detached so no transfer must occur after. */
2771 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
2772
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002773 /* Report error if set on stream endpoint layer. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002774 if (qcs->qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002775 se_fl_set(qcs->sd, SE_FL_ERROR);
2776 TRACE_DEVEL("connection in error", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2777 goto end;
2778 }
2779
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002780 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle0ed617a2022-09-20 14:46:40 +02002781 ret = qcs_http_reset_buf(qcs, buf, count);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002782 goto end;
2783 }
2784
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002785 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002786 if (fin) {
2787 TRACE_STATE("reached stream fin", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002788 qcs->flags |= QC_SF_FIN_STREAM;
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002789 }
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002790
Amaury Denoyelleab6cdec2023-01-10 10:41:41 +01002791 if (ret || fin) {
Amaury Denoyellef9b03262023-01-09 10:34:25 +01002792 qcc_send_stream(qcs, 0);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002793 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
2794 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
2795 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002796
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002797 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002798 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2799
2800 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002801}
2802
2803/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2804 * event subscriber <es> is not allowed to change from a previous call as long
2805 * as at least one event is still subscribed. The <event_type> must only be a
2806 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2807 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002808static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002809 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002810{
Willy Tarreau3215e732022-05-27 10:09:11 +02002811 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002812}
2813
2814/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2815 * The <es> pointer is not allowed to differ from the one passed to the
2816 * subscribe() call. It always returns zero.
2817 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002818static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002819{
Willy Tarreau3215e732022-05-27 10:09:11 +02002820 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002821
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002822 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2823 BUG_ON(qcs->subs && qcs->subs != es);
2824
2825 es->events &= ~event_type;
2826 if (!es->events)
2827 qcs->subs = NULL;
2828
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002829 return 0;
2830}
2831
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002832static int qc_wake(struct connection *conn)
2833{
2834 struct qcc *qcc = conn->ctx;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002835
2836 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002837
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002838 if (qc_process(qcc)) {
2839 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002840 goto release;
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002841 }
2842
2843 qc_wake_some_streams(qcc);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002844
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002845 qcc_refresh_timeout(qcc);
2846
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002847 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002848 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002849
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002850 release:
2851 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002852 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002853 return 1;
2854}
2855
Amaury Denoyellea473f192022-12-21 10:21:58 +01002856static void qc_shutw(struct stconn *sc, enum co_shw_mode mode)
2857{
2858 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002859 struct qcc *qcc = qcs->qcc;
Amaury Denoyellea473f192022-12-21 10:21:58 +01002860
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002861 TRACE_ENTER(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002862
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002863 /* Early closure reported if QC_SF_FIN_STREAM not yet set. */
Amaury Denoyellea473f192022-12-21 10:21:58 +01002864 if (!qcs_is_close_local(qcs) &&
2865 !(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET))) {
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002866
2867 if (qcs->flags & QC_SF_UNKNOWN_PL_LENGTH) {
2868 /* Close stream with a FIN STREAM frame. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002869 if (!(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))) {
Amaury Denoyelle3fd40932023-05-10 10:41:47 +02002870 TRACE_STATE("set FIN STREAM",
2871 QMUX_EV_STRM_SHUT, qcc->conn, qcs);
2872 qcs->flags |= QC_SF_FIN_STREAM;
2873 qcc_send_stream(qcs, 0);
2874 }
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002875 }
2876 else {
2877 /* RESET_STREAM necessary. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002878 if (!(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)))
Amaury Denoyelle3fd40932023-05-10 10:41:47 +02002879 qcc_reset_stream(qcs, 0);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002880 se_fl_set_error(qcs->sd);
2881 }
2882
2883 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002884 }
2885
Amaury Denoyelled4af0412023-05-03 18:17:19 +02002886 out:
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002887 TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002888}
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002889
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002890/* for debugging with CLI's "show sess" command. May emit multiple lines, each
2891 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
2892 * line is used. Each field starts with a space so it's safe to print it after
2893 * existing fields.
2894 */
2895static int qc_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
2896{
2897 struct qcs *qcs = sd->se;
2898 struct qcc *qcc;
2899 int ret = 0;
2900
2901 if (!qcs)
2902 return ret;
2903
2904 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx",
2905 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err);
2906
2907 if (pfx)
2908 chunk_appendf(msg, "\n%s", pfx);
2909
2910 qcc = qcs->qcc;
2911 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
2912 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
2913 return ret;
2914}
2915
2916
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002917static const struct mux_ops qc_ops = {
2918 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002919 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002920 .detach = qc_detach,
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002921 .rcv_buf = qc_recv_buf,
2922 .snd_buf = qc_send_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002923 .subscribe = qc_subscribe,
2924 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002925 .wake = qc_wake,
Amaury Denoyellea473f192022-12-21 10:21:58 +01002926 .shutw = qc_shutw,
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002927 .show_sd = qc_show_sd,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002928 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002929 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002930};
2931
2932static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002933 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002934
2935INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);